controller.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. /* Wifi Hotspot app for YunoHost
  3. * Copyright (C) 2015 Julien Vaubourg <julien@vaubourg.com>
  4. * Contribute at https://github.com/jvaubourg/hotspot_ynh
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. function moulinette_get($var) {
  20. return htmlspecialchars(exec('sudo yunohost app setting hotspot '.escapeshellarg($var)));
  21. }
  22. function moulinette_set($var, $value) {
  23. return exec('sudo yunohost app setting hotspot '.escapeshellarg($var).' -v '.escapeshellarg($value));
  24. }
  25. function stop_service() {
  26. exec('sudo service ynh-hotspot stop');
  27. }
  28. function start_service() {
  29. exec('sudo service ynh-hotspot start', $output, $retcode);
  30. return $retcode;
  31. }
  32. function service_status() {
  33. exec('sudo service ynh-hotspot status', $output);
  34. return $output;
  35. }
  36. function service_faststatus() {
  37. exec('sudo service hostapd status', $output, $retcode);
  38. return $retcode;
  39. }
  40. function ipv6_expanded($ip) {
  41. exec('ipv6_expanded '.escapeshellarg($ip), $output);
  42. return $output[0];
  43. }
  44. function ipv6_compressed($ip) {
  45. exec('ipv6_compressed '.escapeshellarg($ip), $output);
  46. return $output[0];
  47. }
  48. function is_connected_through_hotspot($ip6_net, $ip4_nat_prefix) {
  49. $ip = $_SERVER['REMOTE_ADDR'];
  50. $ip6_regex = '/^'.preg_quote(preg_replace('/::$/', '', $ip6_net)).':/';
  51. $ip4_regex = '/^'.preg_quote($ip4_nat_prefix).'\./';
  52. return (preg_match($ip6_regex, $ip) || preg_match($ip4_regex, $ip));
  53. }
  54. dispatch('/', function() {
  55. exec('sudo iwconfig', $devs);
  56. $wifi_device = moulinette_get('wifi_device');
  57. $devs_list = '';
  58. foreach($devs AS $dev) {
  59. if(preg_match('/802.11/', $dev)) {
  60. $dev = explode(' ', $dev);
  61. $dev = $dev[0];
  62. $active = ($dev == $wifi_device) ? 'class="active"' : '';
  63. $devs_list .= "<li $active><a href='#'>$dev</a></li>\n";
  64. }
  65. }
  66. $ip6_net = moulinette_get('ip6_net');
  67. $ip6_net = ($ip6_net == 'none') ? '' : $ip6_net;
  68. $ip4_nat_prefix = moulinette_get('ip4_nat_prefix');
  69. set('service_enabled', moulinette_get('service_enabled'));
  70. set('wifi_ssid', moulinette_get('wifi_ssid'));
  71. set('wifi_secure', moulinette_get('wifi_secure'));
  72. set('wifi_passphrase', moulinette_get('wifi_passphrase'));
  73. set('wifi_channel', moulinette_get('wifi_channel'));
  74. set('wifi_n', moulinette_get('wifi_n'));
  75. set('wifi_device', $wifi_device);
  76. set('wifi_device_list', $devs_list);
  77. set('ip6_net', $ip6_net);
  78. set('ip6_dns0', moulinette_get('ip6_dns0'));
  79. set('ip6_dns1', moulinette_get('ip6_dns1'));
  80. set('ip4_nat_prefix', $ip4_nat_prefix);
  81. set('ip4_dns0', moulinette_get('ip4_dns0'));
  82. set('ip4_dns1', moulinette_get('ip4_dns1'));
  83. set('faststatus', service_faststatus() == 0);
  84. set('is_connected_through_hotspot', is_connected_through_hotspot($ip6_net, $ip4_nat_prefix));
  85. return render('settings.html.php');
  86. });
  87. dispatch_put('/settings', function() {
  88. exec('ip link show '.escapeshellarg($_POST['wifi_device']), $output, $retcode);
  89. $wifi_device_exists = ($retcode == 0);
  90. $ip6_net = empty($_POST['ip6_net']) ? 'none' : $_POST['ip6_net'];
  91. $ip6_addr = 'none';
  92. $ip6_dns0 = $_POST['ip6_dns0'];
  93. $ip6_dns1 = $_POST['ip6_dns1'];
  94. $service_enabled = isset($_POST['service_enabled']) ? 1 : 0;
  95. $wifi_secure = isset($_POST['wifi_secure']) ? 1 : 0;
  96. if($service_enabled == 1) {
  97. try {
  98. if(empty($_POST['wifi_ssid']) || empty($_POST['wifi_passphrase']) || empty($_POST['wifi_channel'])) {
  99. throw new Exception(T_('Your Wifi Hotspot needs a name, a password and a channel'));
  100. }
  101. if(strlen($_POST['wifi_passphrase']) < 8 || strlen($_POST['wifi_passphrase']) > 63) {
  102. throw new Exception(T_('Your password must from 8 to 63 characters (WPA2 passphrase)'));
  103. }
  104. if(preg_match('/[^[:print:]]/', $_POST['wifi_passphrase'])) {
  105. throw new Exception(T_('Only printable ASCII characters are permitted in your password'));
  106. }
  107. if(!$wifi_device_exists) {
  108. throw new Exception(T_('The wifi antenna interface seems not exist on the system'));
  109. }
  110. if($ip6_net != 'none') {
  111. $ip6_net = ipv6_expanded($ip6_net);
  112. if(empty($ip6_net)) {
  113. throw new Exception(T_('The IPv6 Delegated Prefix format looks bad'));
  114. }
  115. $ip6_blocs = explode(':', $ip6_net);
  116. $ip6_addr = "${ip6_blocs[0]}:${ip6_blocs[1]}:${ip6_blocs[2]}:${ip6_blocs[3]}:${ip6_blocs[4]}:${ip6_blocs[5]}:${ip6_blocs[6]}:42";
  117. $ip6_net = ipv6_compressed($ip6_net);
  118. $ip6_addr = ipv6_compressed($ip6_addr);
  119. }
  120. if(!empty($ip6_dns0)) {
  121. $ip6_dns0 = ipv6_expanded($ip6_dns0);
  122. if(empty($ip6_dns0)) {
  123. throw new Exception(T_('The format of the first IPv6 DNS Resolver looks bad'));
  124. }
  125. $ip6_dns0 = ipv6_compressed($ip6_dns0);
  126. if(!empty($ip6_dns1)) {
  127. $ip6_dns1 = ipv6_expanded($ip6_dns1);
  128. if(empty($ip6_dns1)) {
  129. throw new Exception(T_('The format of the second IPv6 DNS Resolver looks bad'));
  130. }
  131. $ip6_dns1 = ipv6_compressed($ip6_dns1);
  132. }
  133. }
  134. if(inet_pton($_POST['ip4_dns0']) === false) {
  135. throw new Exception(T_('The format of the first IPv4 DNS Resolver looks bad'));
  136. }
  137. if(inet_pton($_POST['ip4_dns1']) === false) {
  138. throw new Exception(T_('The format of the second IPv4 DNS Resolver looks bad'));
  139. }
  140. if(inet_pton("${_POST['ip4_nat_prefix']}.0") === false) {
  141. throw new Exception(T_('The format of the IPv4 NAT Prefix (/24) looks bad : x.x.x expected)'));
  142. }
  143. if(filter_var("${_POST['ip4_nat_prefix']}.0", FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE) !== false) {
  144. throw new Exception(T_('The IPv4 NAT Prefix must be from a private range'));
  145. }
  146. } catch(Exception $e) {
  147. flash('error', $e->getMessage().' ('.T_('configuration not updated').').');
  148. goto redirect;
  149. }
  150. }
  151. stop_service();
  152. moulinette_set('service_enabled', $service_enabled);
  153. if($service_enabled == 1) {
  154. moulinette_set('wifi_ssid', $_POST['wifi_ssid']);
  155. moulinette_set('wifi_secure', $wifi_secure);
  156. if($wifi_secure == 1) {
  157. moulinette_set('wifi_passphrase', $_POST['wifi_passphrase']);
  158. }
  159. moulinette_set('wifi_channel', $_POST['wifi_channel']);
  160. moulinette_set('wifi_n', isset($_POST['wifi_n']) ? 1 : 0);
  161. moulinette_set('wifi_device', $_POST['wifi_device']);
  162. moulinette_set('ip6_net', $ip6_net);
  163. moulinette_set('ip6_addr', $ip6_addr);
  164. moulinette_set('ip6_dns0', $_POST['ip6_dns0']);
  165. moulinette_set('ip6_dns1', $_POST['ip6_dns1']);
  166. moulinette_set('ip4_nat_prefix', $_POST['ip4_nat_prefix']);
  167. moulinette_set('ip4_dns0', $_POST['ip4_dns0']);
  168. moulinette_set('ip4_dns1', $_POST['ip4_dns1']);
  169. $retcode = start_service();
  170. if($retcode == 0) {
  171. flash('success', T_('Configuration updated and service successfully reloaded'));
  172. } else {
  173. flash('error', T_('Configuration updated but service reload failed'));
  174. }
  175. } else {
  176. flash('success', T_('Service successfully disabled'));
  177. }
  178. redirect:
  179. redirect_to('/');
  180. });
  181. dispatch('/status', function() {
  182. $status_lines = service_status();
  183. $status_list = '';
  184. foreach($status_lines AS $status_line) {
  185. if(preg_match('/^\[INFO\]/', $status_line)) {
  186. $status_list .= '<li class="status-info">'.htmlspecialchars($status_line).'</li>';
  187. }
  188. elseif(preg_match('/^\[OK\]/', $status_line)) {
  189. $status_list .= '<li class="status-success">'.htmlspecialchars($status_line).'</li>';
  190. }
  191. elseif(preg_match('/^\[WARN\]/', $status_line)) {
  192. $status_list .= '<li class="status-warning">'.htmlspecialchars($status_line).'</li>';
  193. }
  194. elseif(preg_match('/^\[ERR\]/', $status_line)) {
  195. $status_list .= '<li class="status-danger">'.htmlspecialchars($status_line).'</li>';
  196. }
  197. }
  198. echo $status_list;
  199. });
  200. dispatch('/lang/:locale', function($locale = 'en') {
  201. switch ($locale) {
  202. case 'fr':
  203. $_SESSION['locale'] = 'fr';
  204. break;
  205. default:
  206. $_SESSION['locale'] = 'en';
  207. }
  208. if(!empty($_GET['redirect_to'])) {
  209. redirect_to($_GET['redirect_to']);
  210. } else {
  211. redirect_to('/');
  212. }
  213. });