controller.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. function moulinette_get($var) {
  3. return htmlspecialchars(exec("sudo yunohost app setting hotspot ".escapeshellarg($var)));
  4. }
  5. function moulinette_set($var, $value) {
  6. return exec("sudo yunohost app setting hotspot ".escapeshellarg($var)." -v ".escapeshellarg($value));
  7. }
  8. function restart_service() {
  9. exec('sudo service ynh-hotspot stop');
  10. exec('sudo service ynh-hotspot start', $output, $retcode);
  11. return $retcode;
  12. }
  13. dispatch('/', function() {
  14. exec('ip link', $devs);
  15. $wifi_device = moulinette_get('wifi_device');
  16. $devs_list = "";
  17. foreach($devs AS $dev) {
  18. if(preg_match('/^[0-9]/', $dev)) {
  19. $dev = explode(':', $dev);
  20. $dev = trim($dev[1]);
  21. if($dev != 'lo') {
  22. $active = ($dev == $wifi_device) ? 'class="active"' : '';
  23. $devs_list .= "<li $active><a href='#'>$dev</a></li>\n";
  24. }
  25. }
  26. }
  27. set('wifi_ssid', moulinette_get('wifi_ssid'));
  28. set('wifi_passphrase', moulinette_get('wifi_passphrase'));
  29. set('wifi_channel', moulinette_get('wifi_channel'));
  30. set('wifi_n', moulinette_get('wifi_n'));
  31. set('wifi_device', $wifi_device);
  32. set('wifi_device_list', $devs_list);
  33. set('ip6_net', moulinette_get('ip6_net'));
  34. set('ip6_dns0', moulinette_get('ip6_dns0'));
  35. set('ip6_dns1', moulinette_get('ip6_dns1'));
  36. set('ip4_nat_prefix', moulinette_get('ip4_nat_prefix'));
  37. set('ip4_dns0', moulinette_get('ip4_dns0'));
  38. set('ip4_dns1', moulinette_get('ip4_dns1'));
  39. return render('settings.html.php');
  40. });
  41. dispatch_put('/settings', function() {
  42. moulinette_set('wifi_ssid', $_POST['wifi_ssid']);
  43. moulinette_set('wifi_passphrase', $_POST['wifi_passphrase']);
  44. moulinette_set('wifi_channel', $_POST['wifi_channel']);
  45. moulinette_set('wifi_n', isset($_POST['wifi_n']) ? 1 : 0);
  46. moulinette_set('wifi_device', $_POST['wifi_device']);
  47. moulinette_set('ip6_net', $_POST['ip6_net']);
  48. moulinette_set('ip6_dns0', $_POST['ip6_dns0']);
  49. moulinette_set('ip6_dns1', $_POST['ip6_dns1']);
  50. moulinette_set('ip4_nat_prefix', $_POST['ip4_nat_prefix']);
  51. moulinette_set('ip4_dns0', $_POST['ip4_dns0']);
  52. moulinette_set('ip4_dns1', $_POST['ip4_dns1']);
  53. $retcode = restart_service();
  54. if($retcode == 0) {
  55. flash('success', T_('Configuration updated and service successfully reloaded'));
  56. } else {
  57. flash('error', T_('Configuration updated but service reload failed'));
  58. }
  59. redirect_to('/');
  60. });
  61. dispatch('/lang/:locale', function($locale = 'en') {
  62. switch ($locale) {
  63. case 'fr':
  64. $_SESSION['locale'] = 'fr';
  65. break;
  66. default:
  67. $_SESSION['locale'] = 'en';
  68. }
  69. if(!empty($_GET['redirect_to'])) {
  70. redirect_to($_GET['redirect_to']);
  71. } else {
  72. redirect_to('/');
  73. }
  74. });