controller.php 6.9 KB

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