*
* CHD OpenWRT is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CHD OpenWRT is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CHD OpenWRT. If not, see .
**/
function validate($regex, $index, $message, &$errors) {
if (isset($_POST[$index]) && 1 === preg_match($regex, $_POST[$index])) {
return true;
}
if (is_array($errors)) $errors[$index] = $message . ' (' . (isset($_POST[$index])?$_POST[$index]:'vide') . ')';
return false;
}
function checksame($index1, $index2, $message, &$errors) {
if (
isset($_POST[$index1])
&& isset($_POST[$index2])
&& ($_POST[$index1] === $_POST[$index2])
) {
return true;
}
$errors[$index] = $message;
return false;
}
$pattern_ipv4_optionnal='/(^|((^|\.)((25[0-5])|(2[0-4]\d)|(1\d\d)|([1-9]?\d))){4})$/';
$pattern_ipv4='/((^|\.)((25[0-5])|(2[0-4]\d)|(1\d\d)|([1-9]?\d))){4}$/';
$pattern_ipv6='/^(((?=.*(::))(?!.*\3.+\3))\3?|([\dA-F]{1,4}(\3|:\b|$)|\2))(?4){5}((?4){2}|(((2[0-4]|1\d|[1-9])?\d|25[0-5])\.?\b){4})\z/i';
$pattern_pass6='/^[^\']{6,64}$/u';
$pattern_pass8='/^[^\']{8,64}$/u';
$pattern_ssid='/^[\w._-]{1,32}$/';
$pattern_iden='/^[\w._-]*$/';
$pattern_rev='/^\d[\d.]*$/';
$pattern_int='/^-?\d+$/';
$errors=array();
$ignore=NULL;
$res = validate($pattern_iden, 'profile', 'Profil matériel du routeur incorrect', $errors);
$res &= validate($pattern_rev, 'revision', 'Révision matérielle du routeur incorrecte', $errors);
$res &= validate($pattern_ipv4, 'ip4pub', 'Adresse IPv4 publique du routeur incorrecte', $errors);
$res &= validate($pattern_ipv4_optionnal, 'ip4wan', 'Adresse IPv4 WAN du routeur incorrecte', $errors);
$res &= validate($pattern_ipv4, 'ip4lan', 'Adresse IPv4 LAN du routeur incorrecte', $errors);
$res &= validate($pattern_ipv6, 'ip6prefix', 'Préfixe IPv6 du routeur incorrect', $errors);
//$res &= validate($pattern_ipv6, 'ip6wanll', 'Adresse IPv6 WAN Link Local incorrecte', $errors);
$res &= validate($pattern_pass6, 'pwd1', 'Mot de passe du routeur incorrect', $errors);
$res &= validate($pattern_pass6, 'pwd2', 'Mot de passe du routeur incorrect', $ignore);
$res &= validate($pattern_ssid, 'ssid', 'Nom du réseau WiFi (SSID) incorrect', $errors);
$res &= validate($pattern_pass8, 'key1', 'Mot de passe du WiFi incorrect', $errors);
$res &= validate($pattern_pass8, 'key2', 'Mot de passe du WiFi incorrect', $ignore);
$res &= validate($pattern_int, 'txpower', 'Puissance d\'émission WiFi incorrecte', $errors);
$res &= checksame('pwd1', 'pwd2', 'Les mots de passes ne correspondent pas', $errors);
$res &= checksame('key1', 'key2', 'Les mots de passes ne correspondent pas', $errors);
switch ($_POST['profile']) {
case 'TLWR740':
switch($_POST['revision']) {
case '4':
$profile='TLWR740';
$revision='4';
$user_filename='mise-a-jour-routeur-TLWR740v4.bin';
$gen_filename='bin/ar71xx/openwrt-15.05-ar71xx-generic-tl-wr740n-v4-squashfs-factory.bin';
break;
default:
$errors['revision'] = 'Routeur non supporté (' . (isset($_POST['revision'])?$_POST[$index]:'vide') . ')';
$res = false;
}
break;
case 'TLWR841':
switch($_POST['revision']) {
case '9':
$profile='TLWR841';
$revision='9';
//$user_filename='wr841nv9_wr841ndv9_en_3_16_9_up(150310).bin';
$user_filename='mise-a-jour-routeur-TLWR841v9.bin';
$gen_filename='bin/ar71xx/openwrt-15.05-ar71xx-generic-tl-wr841n-v9-squashfs-factory.bin';
break;
default:
$errors['revision'] = 'Routeur non supporté (' . (isset($_POST['revision'])?$_POST[$index]:'vide') . ')';
$res = false;
}
break;
default:
$errors['profile'] = 'Routeur non supporté (' . (isset($_POST[$index])?$_POST['profile']:'vide') . ')';
$res = false;
}
if ( !$res ) {
echo "\n\n
\n\n\n\nOops, une erreur s'est produite
\n\n\n";
exit();
}
// Prevent escapeshellarg to eat UTF-8 non ASCII chars
setlocale(LC_CTYPE, "fr_FR.UTF-8");
$basepath='/var/cache/build-openwrt-dev';
$scriptfile='build-openwrt-dev.sh';
$lockfile='/run/lock/build-openwrt-dev.lock';
// Escape everything and build command-line
$ip4pub=escapeshellarg($_POST['ip4pub']);
$lanip4addr=escapeshellarg($_POST['ip4lan']);
$ip6prefix=escapeshellarg($_POST['ip6prefix']);
//$wanip6ll=escapeshellarg($_POST['ip6wanll']);
$root_passwd=escapeshellarg($_POST['pwd1']);
$wireless_ssid=escapeshellarg($_POST['ssid']);
$wireless_key=escapeshellarg($_POST['key1']);
$wireless_txpower=escapeshellarg($_POST['txpower']);
$wanip4addr=escapeshellarg($_POST['ip4wan']);
# build-openwrt [ip4wan]
$command=escapeshellcmd($scriptfile);
$command .= " $profile $ip4pub $lanip4addr $ip6prefix $root_passwd $wireless_ssid $wireless_key $wireless_txpower $wanip4addr 2>&1";
// Prevent multiple parallel executions of the generation part
$fh=fopen($lockfile, 'w+');
if ( ! is_resource($fh) ) {
echo "PROBLEM opening lock file\n";
exit();
}
if ( ! flock($fh, LOCK_EX) ) {
echo "PROBLEM acquiring lock file\n";
exit();
}
putenv("CACHE_DIR=$basepath/");
putenv("BUILD_DIR=$basepath/build/");
exec($command, $output, $return_val);
// Dump script output in /tmp for troobleshooting
file_put_contents(tempnam('/tmp' , $scriptfile.'_'), array($command, "\n", print_r($output,true), "\n",$return_val,"\n"));
if ( $return_val === 0 ) {
header('Content-Type: application/octet-stream');
header("Content-Disposition: attachment; filename=$user_filename");
readfile("$basepath/build/$gen_filename");
} else {
echo "\n$command\n\n";
echo join($output, "\n") . "\n\n";
echo "Command return value : "; var_dump($return_val);
}
fclose($fh);