controller.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 stop_service() {
  9. exec('sudo service ynh-hotspot stop');
  10. }
  11. function start_service() {
  12. exec('sudo service ynh-hotspot start', $output, $retcode);
  13. return $retcode;
  14. }
  15. function ipv6_expanded($ip) {
  16. exec('ipv6_expanded '.escapeshellarg($ip), $output);
  17. return $output[0];
  18. }
  19. function ipv6_compressed($ip) {
  20. exec('ipv6_compressed '.escapeshellarg($ip), $output);
  21. return $output[0];
  22. }
  23. dispatch('/', function() {
  24. exec('sudo iwconfig', $devs);
  25. $wifi_device = moulinette_get('wifi_device');
  26. $devs_list = "";
  27. foreach($devs AS $dev) {
  28. if(preg_match('/802.11/', $dev)) {
  29. $dev = explode(' ', $dev);
  30. $dev = $dev[0];
  31. $active = ($dev == $wifi_device) ? 'class="active"' : '';
  32. $devs_list .= "<li $active><a href='#'>$dev</a></li>\n";
  33. }
  34. }
  35. $ip6_net = moulinette_get('ip6_net');
  36. $ip6_net = ($ip6_net == 'none') ? '' : $ip6_net;
  37. set('wifi_ssid', moulinette_get('wifi_ssid'));
  38. set('wifi_passphrase', moulinette_get('wifi_passphrase'));
  39. set('wifi_channel', moulinette_get('wifi_channel'));
  40. set('wifi_n', moulinette_get('wifi_n'));
  41. set('wifi_device', $wifi_device);
  42. set('wifi_device_list', $devs_list);
  43. set('ip6_net', $ip6_net);
  44. set('ip6_dns0', moulinette_get('ip6_dns0'));
  45. set('ip6_dns1', moulinette_get('ip6_dns1'));
  46. set('ip4_nat_prefix', moulinette_get('ip4_nat_prefix'));
  47. set('ip4_dns0', moulinette_get('ip4_dns0'));
  48. set('ip4_dns1', moulinette_get('ip4_dns1'));
  49. return render('settings.html.php');
  50. });
  51. dispatch_put('/settings', function() {
  52. exec('ip link show '.escapeshellarg($_POST['wifi_device']), $output, $retcode);
  53. $wifi_device_exists = ($retcode == 0);
  54. $ip6_net = empty($_POST['ip6_net']) ? 'none' : $_POST['ip6_net'];
  55. $ip6_addr = 'none';
  56. try {
  57. if(empty($_POST['wifi_ssid']) || empty($_POST['wifi_passphrase']) || empty($_POST['wifi_channel'])) {
  58. throw new Exception(T_('Your Wifi Hotspot needs a name, a password and a channel'));
  59. }
  60. if(strlen($_POST['wifi_passphrase']) < 8 || strlen($_POST['wifi_passphrase']) > 63) {
  61. throw new Exception(T_('Your password must from 8 to 63 characters (WPA2 passphrase)'));
  62. }
  63. if(preg_match('/[^[:print:]]/', $_POST['wifi_passphrase'])) {
  64. throw new Exception(T_('Only printable ASCII characters are permitted in your password'));
  65. }
  66. if(!$wifi_device_exists) {
  67. throw new Exception(T_('The wifi antenna interface seems not exist on the system'));
  68. }
  69. if($ip6_net != 'none') {
  70. $ip6_net = ipv6_expanded($ip6_net);
  71. if(empty($ip6_net)) {
  72. throw new Exception(T_('The IPv6 Delegated Prefix format looks bad'));
  73. }
  74. $ip6_blocs = explode(':', $ip6_net);
  75. $ip6_addr = "${ip6_blocs[0]}:${ip6_blocs[1]}:${ip6_blocs[2]}:${ip6_blocs[3]}:${ip6_blocs[4]}:${ip6_blocs[5]}:${ip6_blocs[6]}:42";
  76. $ip6_net = ipv6_compressed($ip6_net);
  77. $ip6_addr = ipv6_compressed($ip6_addr);
  78. }
  79. $ip6_dns0 = ipv6_expanded($ip6_dns0);
  80. if(empty($_POST['ip6_dns0'])) {
  81. throw new Exception(T_('The format of the first IPv6 DNS Resolver looks bad'));
  82. }
  83. $ip6_dns0 = ipv6_compressed($ip6_dns0);
  84. $ip6_dns1 = ipv6_expanded($ip6_dns1);
  85. if(empty($_POST['ip6_dns1'])) {
  86. throw new Exception(T_('The format of the second IPv6 DNS Resolver looks bad'));
  87. }
  88. $ip6_dns1 = ipv6_compressed($ip6_dns1);
  89. if(inet_pton($_POST['ip4_dns0']) === false) {
  90. throw new Exception(T_('The format of the first IPv4 DNS Resolver looks bad'));
  91. }
  92. if(inet_pton($_POST['ip4_dns1']) === false) {
  93. throw new Exception(T_('The format of the second IPv4 DNS Resolver looks bad'));
  94. }
  95. if(inet_pton("${_POST['ip4_nat_prefix']}.0") === false) {
  96. throw new Exception(T_('The format of the IPv4 NAT Prefix (/24) looks bad : x.x.x expected)'));
  97. }
  98. if(filter_var("${_POST['ip4_nat_prefix']}.0", FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE) !== false) {
  99. throw new Exception(T_('The IPv4 NAT Prefix must be from a private range'));
  100. }
  101. } catch(Exception $e) {
  102. flash('error', $e->getMessage().T_(' (configuration not updated).'));
  103. goto redirect;
  104. }
  105. stop_service();
  106. moulinette_set('wifi_ssid', $_POST['wifi_ssid']);
  107. moulinette_set('wifi_passphrase', $_POST['wifi_passphrase']);
  108. moulinette_set('wifi_channel', $_POST['wifi_channel']);
  109. moulinette_set('wifi_n', isset($_POST['wifi_n']) ? 1 : 0);
  110. moulinette_set('wifi_device', $_POST['wifi_device']);
  111. moulinette_set('ip6_net', $ip6_net);
  112. moulinette_set('ip6_addr', $ip6_addr);
  113. moulinette_set('ip6_dns0', $_POST['ip6_dns0']);
  114. moulinette_set('ip6_dns1', $_POST['ip6_dns1']);
  115. moulinette_set('ip4_nat_prefix', $_POST['ip4_nat_prefix']);
  116. moulinette_set('ip4_dns0', $_POST['ip4_dns0']);
  117. moulinette_set('ip4_dns1', $_POST['ip4_dns1']);
  118. $retcode = start_service();
  119. if($retcode == 0) {
  120. flash('success', T_('Configuration updated and service successfully reloaded'));
  121. } else {
  122. flash('error', T_('Configuration updated but service reload failed'));
  123. }
  124. redirect:
  125. redirect_to('/');
  126. });
  127. dispatch('/lang/:locale', function($locale = 'en') {
  128. switch ($locale) {
  129. case 'fr':
  130. $_SESSION['locale'] = 'fr';
  131. break;
  132. default:
  133. $_SESSION['locale'] = 'en';
  134. }
  135. if(!empty($_GET['redirect_to'])) {
  136. redirect_to($_GET['redirect_to']);
  137. } else {
  138. redirect_to('/');
  139. }
  140. });