1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/hotspot_ynh.git synced 2024-09-03 19:25:53 +02:00
hotspot_ynh/sources/controller.php

109 lines
3 KiB
PHP
Raw Normal View History

<?php
function moulinette_get($var) {
return htmlspecialchars(exec("sudo yunohost app setting hotspot ".escapeshellarg($var)));
}
function moulinette_set($var, $value) {
return exec("sudo yunohost app setting hotspot ".escapeshellarg($var)." -v ".escapeshellarg($value));
}
2014-11-10 22:27:59 +01:00
function stop_service() {
exec('sudo service ynh-hotspot stop');
2014-11-10 22:27:59 +01:00
}
function start_service() {
exec('sudo service ynh-hotspot start', $output, $retcode);
return $retcode;
}
dispatch('/', function() {
exec('ip link', $devs);
$wifi_device = moulinette_get('wifi_device');
$devs_list = "";
foreach($devs AS $dev) {
if(preg_match('/^[0-9]/', $dev)) {
$dev = explode(':', $dev);
$dev = trim($dev[1]);
if($dev != 'lo') {
$active = ($dev == $wifi_device) ? 'class="active"' : '';
$devs_list .= "<li $active><a href='#'>$dev</a></li>\n";
}
}
}
2014-11-10 22:27:59 +01:00
$ip6_net = moulinette_get('ip6_net');
$ip6_net = ($ip6_net == 'none') ? '' : $ip6_net;
set('wifi_ssid', moulinette_get('wifi_ssid'));
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'));
set('wifi_device', $wifi_device);
set('wifi_device_list', $devs_list);
2014-11-10 22:27:59 +01:00
set('ip6_net', $ip6_net);
set('ip6_dns0', moulinette_get('ip6_dns0'));
set('ip6_dns1', moulinette_get('ip6_dns1'));
set('ip4_nat_prefix', moulinette_get('ip4_nat_prefix'));
set('ip4_dns0', moulinette_get('ip4_dns0'));
set('ip4_dns1', moulinette_get('ip4_dns1'));
return render('settings.html.php');
});
dispatch_put('/settings', function() {
2014-11-10 22:27:59 +01:00
$ip6_net = empty($_POST['ip6_net']) ? 'none' : $_POST['ip6_net'];
stop_service();
moulinette_set('wifi_ssid', $_POST['wifi_ssid']);
moulinette_set('wifi_passphrase', $_POST['wifi_passphrase']);
moulinette_set('wifi_channel', $_POST['wifi_channel']);
2014-11-09 23:49:06 +01:00
moulinette_set('wifi_n', isset($_POST['wifi_n']) ? 1 : 0);
moulinette_set('wifi_device', $_POST['wifi_device']);
2014-11-10 22:27:59 +01:00
moulinette_set('ip6_net', $ip6_net);
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']);
2014-11-10 22:27:59 +01:00
# TODO: format ip6_net
if($ip6_net == 'none') {
moulinette_set('ip6_addr', 'none');
} else {
$ip6_addr = "${ip6_net}1";
moulinette_set('ip6_addr', $ip6_addr);
}
$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'));
}
redirect_to('/');
});
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('/');
}
});