controller.php 6.4 KB

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