2014-11-09 22:49:07 +01:00
|
|
|
<?php
|
|
|
|
|
2014-12-26 20:18:03 +01:00
|
|
|
/* Wifi Hotspot app for YunoHost
|
|
|
|
* Copyright (C) 2015 Julien Vaubourg <julien@vaubourg.com>
|
|
|
|
* Contribute at https://github.com/jvaubourg/hotspot_ynh
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2014-11-09 22:49:07 +01:00
|
|
|
function moulinette_get($var) {
|
2014-11-16 23:27:36 +01:00
|
|
|
return htmlspecialchars(exec('sudo yunohost app setting hotspot '.escapeshellarg($var)));
|
2014-11-09 22:49:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function moulinette_set($var, $value) {
|
2014-11-16 23:27:36 +01:00
|
|
|
return exec('sudo yunohost app setting hotspot '.escapeshellarg($var).' -v '.escapeshellarg($value));
|
2014-11-09 22:49:07 +01:00
|
|
|
}
|
|
|
|
|
2014-11-10 22:27:59 +01:00
|
|
|
function stop_service() {
|
2014-11-09 22:49:07 +01:00
|
|
|
exec('sudo service ynh-hotspot stop');
|
2014-11-10 22:27:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function start_service() {
|
2014-11-09 22:49:07 +01:00
|
|
|
exec('sudo service ynh-hotspot start', $output, $retcode);
|
|
|
|
|
|
|
|
return $retcode;
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:18 +01:00
|
|
|
function service_status() {
|
|
|
|
exec('sudo service ynh-hotspot status', $output);
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
function service_faststatus() {
|
|
|
|
exec('sudo service hostapd status', $output, $retcode);
|
|
|
|
|
|
|
|
return $retcode;
|
|
|
|
}
|
|
|
|
|
2014-11-16 23:27:36 +01:00
|
|
|
function ipv6_expanded($ip) {
|
|
|
|
exec('ipv6_expanded '.escapeshellarg($ip), $output);
|
|
|
|
|
|
|
|
return $output[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
function ipv6_compressed($ip) {
|
|
|
|
exec('ipv6_compressed '.escapeshellarg($ip), $output);
|
|
|
|
|
|
|
|
return $output[0];
|
|
|
|
}
|
|
|
|
|
2014-12-26 18:58:58 +01:00
|
|
|
function is_connected_through_hotspot($ip6_net, $ip4_nat_prefix) {
|
|
|
|
$ip = $_SERVER['REMOTE_ADDR'];
|
|
|
|
$ip6_regex = '/^'.preg_quote(preg_replace('/::$/', '', $ip6_net)).':/';
|
|
|
|
$ip4_regex = '/^'.preg_quote($ip4_nat_prefix).'\./';
|
|
|
|
|
|
|
|
return (preg_match($ip6_regex, $ip) || preg_match($ip4_regex, $ip));
|
|
|
|
}
|
|
|
|
|
2014-11-09 22:49:07 +01:00
|
|
|
dispatch('/', function() {
|
2014-11-16 23:27:36 +01:00
|
|
|
exec('sudo iwconfig', $devs);
|
2014-11-09 22:49:07 +01:00
|
|
|
$wifi_device = moulinette_get('wifi_device');
|
2014-11-17 23:44:18 +01:00
|
|
|
$devs_list = '';
|
2014-11-09 22:49:07 +01:00
|
|
|
|
|
|
|
foreach($devs AS $dev) {
|
2014-11-16 23:27:36 +01:00
|
|
|
if(preg_match('/802.11/', $dev)) {
|
|
|
|
$dev = explode(' ', $dev);
|
|
|
|
$dev = $dev[0];
|
2014-11-09 22:49:07 +01:00
|
|
|
|
2014-11-16 23:27:36 +01:00
|
|
|
$active = ($dev == $wifi_device) ? 'class="active"' : '';
|
|
|
|
$devs_list .= "<li $active><a href='#'>$dev</a></li>\n";
|
2014-11-09 22:49:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-10 22:27:59 +01:00
|
|
|
$ip6_net = moulinette_get('ip6_net');
|
|
|
|
$ip6_net = ($ip6_net == 'none') ? '' : $ip6_net;
|
2014-12-26 18:58:58 +01:00
|
|
|
$ip4_nat_prefix = moulinette_get('ip4_nat_prefix');
|
2014-11-10 22:27:59 +01:00
|
|
|
|
2015-03-14 15:44:48 +01:00
|
|
|
set('service_enabled', moulinette_get('service_enabled'));
|
2014-11-09 22:49:07 +01:00
|
|
|
set('wifi_ssid', moulinette_get('wifi_ssid'));
|
2015-03-14 15:44:48 +01:00
|
|
|
set('wifi_secure', moulinette_get('wifi_secure'));
|
2014-11-09 22:49:07 +01:00
|
|
|
set('wifi_passphrase', moulinette_get('wifi_passphrase'));
|
|
|
|
set('wifi_channel', moulinette_get('wifi_channel'));
|
2014-11-09 23:49:06 +01:00
|
|
|
set('wifi_n', moulinette_get('wifi_n'));
|
2014-11-09 22:49:07 +01:00
|
|
|
set('wifi_device', $wifi_device);
|
|
|
|
set('wifi_device_list', $devs_list);
|
2014-11-10 22:27:59 +01:00
|
|
|
set('ip6_net', $ip6_net);
|
2014-11-09 22:49:07 +01:00
|
|
|
set('ip6_dns0', moulinette_get('ip6_dns0'));
|
|
|
|
set('ip6_dns1', moulinette_get('ip6_dns1'));
|
2014-12-26 18:58:58 +01:00
|
|
|
set('ip4_nat_prefix', $ip4_nat_prefix);
|
2014-11-09 22:49:07 +01:00
|
|
|
set('ip4_dns0', moulinette_get('ip4_dns0'));
|
|
|
|
set('ip4_dns1', moulinette_get('ip4_dns1'));
|
2014-11-17 23:44:18 +01:00
|
|
|
set('faststatus', service_faststatus() == 0);
|
2014-12-26 18:58:58 +01:00
|
|
|
set('is_connected_through_hotspot', is_connected_through_hotspot($ip6_net, $ip4_nat_prefix));
|
2014-11-09 22:49:07 +01:00
|
|
|
|
|
|
|
return render('settings.html.php');
|
|
|
|
});
|
|
|
|
|
|
|
|
dispatch_put('/settings', function() {
|
2014-11-16 23:27:36 +01:00
|
|
|
exec('ip link show '.escapeshellarg($_POST['wifi_device']), $output, $retcode);
|
|
|
|
$wifi_device_exists = ($retcode == 0);
|
|
|
|
|
2014-11-10 22:27:59 +01:00
|
|
|
$ip6_net = empty($_POST['ip6_net']) ? 'none' : $_POST['ip6_net'];
|
2014-11-16 23:27:36 +01:00
|
|
|
$ip6_addr = 'none';
|
2015-03-23 20:20:27 +01:00
|
|
|
$ip6_dns0 = $_POST['ip6_dns0'];
|
|
|
|
$ip6_dns1 = $_POST['ip6_dns1'];
|
2015-03-14 15:44:48 +01:00
|
|
|
$service_enabled = isset($_POST['service_enabled']) ? 1 : 0;
|
|
|
|
$wifi_secure = isset($_POST['wifi_secure']) ? 1 : 0;
|
2014-11-16 23:27:36 +01:00
|
|
|
|
2015-03-14 15:44:48 +01:00
|
|
|
if($service_enabled == 1) {
|
|
|
|
try {
|
|
|
|
if(empty($_POST['wifi_ssid']) || empty($_POST['wifi_passphrase']) || empty($_POST['wifi_channel'])) {
|
|
|
|
throw new Exception(T_('Your Wifi Hotspot needs a name, a password and a channel'));
|
|
|
|
}
|
2014-11-16 23:27:36 +01:00
|
|
|
|
2015-03-14 15:44:48 +01:00
|
|
|
if(strlen($_POST['wifi_passphrase']) < 8 || strlen($_POST['wifi_passphrase']) > 63) {
|
|
|
|
throw new Exception(T_('Your password must from 8 to 63 characters (WPA2 passphrase)'));
|
2014-11-16 23:27:36 +01:00
|
|
|
}
|
|
|
|
|
2015-03-14 15:44:48 +01:00
|
|
|
if(preg_match('/[^[:print:]]/', $_POST['wifi_passphrase'])) {
|
|
|
|
throw new Exception(T_('Only printable ASCII characters are permitted in your password'));
|
|
|
|
}
|
2014-11-16 23:27:36 +01:00
|
|
|
|
2015-03-14 15:44:48 +01:00
|
|
|
if(!$wifi_device_exists) {
|
|
|
|
throw new Exception(T_('The wifi antenna interface seems not exist on the system'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if($ip6_net != 'none') {
|
|
|
|
$ip6_net = ipv6_expanded($ip6_net);
|
|
|
|
|
|
|
|
if(empty($ip6_net)) {
|
|
|
|
throw new Exception(T_('The IPv6 Delegated Prefix format looks bad'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$ip6_blocs = explode(':', $ip6_net);
|
|
|
|
$ip6_addr = "${ip6_blocs[0]}:${ip6_blocs[1]}:${ip6_blocs[2]}:${ip6_blocs[3]}:${ip6_blocs[4]}:${ip6_blocs[5]}:${ip6_blocs[6]}:42";
|
|
|
|
|
|
|
|
$ip6_net = ipv6_compressed($ip6_net);
|
|
|
|
$ip6_addr = ipv6_compressed($ip6_addr);
|
|
|
|
}
|
|
|
|
|
2015-03-23 20:20:27 +01:00
|
|
|
if(!empty($ip6_dns0)) {
|
|
|
|
$ip6_dns0 = ipv6_expanded($ip6_dns0);
|
2015-03-14 15:44:48 +01:00
|
|
|
|
2015-03-23 20:20:27 +01:00
|
|
|
if(empty($ip6_dns0)) {
|
|
|
|
throw new Exception(T_('The format of the first IPv6 DNS Resolver looks bad'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$ip6_dns0 = ipv6_compressed($ip6_dns0);
|
|
|
|
|
|
|
|
if(!empty($ip6_dns1)) {
|
|
|
|
$ip6_dns1 = ipv6_expanded($ip6_dns1);
|
|
|
|
|
|
|
|
if(empty($ip6_dns1)) {
|
|
|
|
throw new Exception(T_('The format of the second IPv6 DNS Resolver looks bad'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$ip6_dns1 = ipv6_compressed($ip6_dns1);
|
|
|
|
}
|
2015-03-14 15:44:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(inet_pton($_POST['ip4_dns0']) === false) {
|
|
|
|
throw new Exception(T_('The format of the first IPv4 DNS Resolver looks bad'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(inet_pton($_POST['ip4_dns1']) === false) {
|
|
|
|
throw new Exception(T_('The format of the second IPv4 DNS Resolver looks bad'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(inet_pton("${_POST['ip4_nat_prefix']}.0") === false) {
|
|
|
|
throw new Exception(T_('The format of the IPv4 NAT Prefix (/24) looks bad : x.x.x expected)'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(filter_var("${_POST['ip4_nat_prefix']}.0", FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE) !== false) {
|
|
|
|
throw new Exception(T_('The IPv4 NAT Prefix must be from a private range'));
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch(Exception $e) {
|
|
|
|
flash('error', $e->getMessage().' ('.T_('configuration not updated').').');
|
|
|
|
goto redirect;
|
2014-11-16 23:27:36 +01:00
|
|
|
}
|
2015-03-14 15:44:48 +01:00
|
|
|
}
|
2014-11-16 23:27:36 +01:00
|
|
|
|
2015-03-14 15:44:48 +01:00
|
|
|
stop_service();
|
2014-11-16 23:27:36 +01:00
|
|
|
|
2015-03-14 15:44:48 +01:00
|
|
|
moulinette_set('service_enabled', $service_enabled);
|
2014-11-16 23:27:36 +01:00
|
|
|
|
2015-03-14 15:44:48 +01:00
|
|
|
if($service_enabled == 1) {
|
|
|
|
moulinette_set('wifi_ssid', $_POST['wifi_ssid']);
|
|
|
|
moulinette_set('wifi_secure', $wifi_secure);
|
2014-11-16 23:27:36 +01:00
|
|
|
|
2015-03-14 15:44:48 +01:00
|
|
|
if($wifi_secure == 1) {
|
|
|
|
moulinette_set('wifi_passphrase', $_POST['wifi_passphrase']);
|
2014-11-16 23:27:36 +01:00
|
|
|
}
|
|
|
|
|
2015-03-14 15:44:48 +01:00
|
|
|
moulinette_set('wifi_channel', $_POST['wifi_channel']);
|
|
|
|
moulinette_set('wifi_n', isset($_POST['wifi_n']) ? 1 : 0);
|
|
|
|
moulinette_set('wifi_device', $_POST['wifi_device']);
|
|
|
|
moulinette_set('ip6_net', $ip6_net);
|
|
|
|
moulinette_set('ip6_addr', $ip6_addr);
|
|
|
|
moulinette_set('ip6_dns0', $_POST['ip6_dns0']);
|
|
|
|
moulinette_set('ip6_dns1', $_POST['ip6_dns1']);
|
|
|
|
moulinette_set('ip4_nat_prefix', $_POST['ip4_nat_prefix']);
|
|
|
|
moulinette_set('ip4_dns0', $_POST['ip4_dns0']);
|
|
|
|
moulinette_set('ip4_dns1', $_POST['ip4_dns1']);
|
|
|
|
|
|
|
|
$retcode = start_service();
|
|
|
|
|
|
|
|
if($retcode == 0) {
|
|
|
|
flash('success', T_('Configuration updated and service successfully reloaded'));
|
|
|
|
} else {
|
|
|
|
flash('error', T_('Configuration updated but service reload failed'));
|
2014-11-16 23:27:36 +01:00
|
|
|
}
|
|
|
|
|
2014-11-09 22:49:07 +01:00
|
|
|
} else {
|
2015-03-14 15:44:48 +01:00
|
|
|
flash('success', T_('Service successfully disabled'));
|
2014-11-09 22:49:07 +01:00
|
|
|
}
|
|
|
|
|
2014-11-16 23:27:36 +01:00
|
|
|
redirect:
|
2014-11-09 22:49:07 +01:00
|
|
|
redirect_to('/');
|
|
|
|
});
|
|
|
|
|
2014-11-17 23:44:18 +01:00
|
|
|
dispatch('/status', function() {
|
|
|
|
$status_lines = service_status();
|
|
|
|
$status_list = '';
|
|
|
|
|
|
|
|
foreach($status_lines AS $status_line) {
|
|
|
|
if(preg_match('/^\[INFO\]/', $status_line)) {
|
2014-11-20 20:03:24 +01:00
|
|
|
$status_list .= '<li class="status-info">'.htmlspecialchars($status_line).'</li>';
|
2014-11-17 23:44:18 +01:00
|
|
|
}
|
|
|
|
elseif(preg_match('/^\[OK\]/', $status_line)) {
|
2014-11-20 20:03:24 +01:00
|
|
|
$status_list .= '<li class="status-success">'.htmlspecialchars($status_line).'</li>';
|
2014-11-17 23:44:18 +01:00
|
|
|
}
|
|
|
|
elseif(preg_match('/^\[WARN\]/', $status_line)) {
|
2014-11-20 20:03:24 +01:00
|
|
|
$status_list .= '<li class="status-warning">'.htmlspecialchars($status_line).'</li>';
|
2014-11-17 23:44:18 +01:00
|
|
|
}
|
|
|
|
elseif(preg_match('/^\[ERR\]/', $status_line)) {
|
2014-11-20 20:03:24 +01:00
|
|
|
$status_list .= '<li class="status-danger">'.htmlspecialchars($status_line).'</li>';
|
2014-11-17 23:44:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $status_list;
|
|
|
|
});
|
|
|
|
|
2014-11-09 22:49:07 +01:00
|
|
|
dispatch('/lang/:locale', function($locale = 'en') {
|
|
|
|
switch ($locale) {
|
|
|
|
case 'fr':
|
|
|
|
$_SESSION['locale'] = 'fr';
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
$_SESSION['locale'] = 'en';
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!empty($_GET['redirect_to'])) {
|
|
|
|
redirect_to($_GET['redirect_to']);
|
|
|
|
} else {
|
|
|
|
redirect_to('/');
|
|
|
|
}
|
|
|
|
});
|