controller.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 getArray($str) {
  49. return explode('|', $str);
  50. }
  51. function noneValue($str) {
  52. return $str == 'none' ? '' : $str;
  53. }
  54. function is_connected_through_hotspot($ip6_net, $ip4_nat_prefix) {
  55. $ip = $_SERVER['REMOTE_ADDR'];
  56. $ip6_regex = '/^'.preg_quote(preg_replace('/::$/', '', $ip6_net)).':/';
  57. $ip4_regex = '/^'.preg_quote($ip4_nat_prefix).'\./';
  58. return (preg_match($ip6_regex, $ip) || preg_match($ip4_regex, $ip));
  59. }
  60. dispatch('/', function() {
  61. $ssids = array();
  62. $devs_list = '';
  63. exec('sudo iwconfig', $devs);
  64. $wifi_device = moulinette_get('wifi_device');
  65. $multissid = moulinette_get('multissid');
  66. $wifi_channel = moulinette_get('wifi_channel');
  67. foreach($devs AS $dev) {
  68. if(preg_match('/802.11/', $dev)) {
  69. $dev = explode(' ', $dev);
  70. $dev = $dev[0];
  71. $active = ($dev == $wifi_device) ? 'class="active"' : '';
  72. $devs_list .= "<li $active><a href='#'>$dev</a></li>\n";
  73. }
  74. }
  75. $wifi_ssid = getArray(moulinette_get('wifi_ssid'));
  76. $wifi_secure = getArray(moulinette_get('wifi_secure'));
  77. $wifi_passphrase = getArray(moulinette_get('wifi_passphrase'));
  78. $ip6_net = getArray(moulinette_get('ip6_net'));
  79. $ip6_dns0 = getArray(moulinette_get('ip6_dns0'));
  80. $ip6_dns1 = getArray(moulinette_get('ip6_dns1'));
  81. $ip4_nat_prefix = getArray(moulinette_get('ip4_nat_prefix'));
  82. $ip4_dns0 = getArray(moulinette_get('ip4_dns0'));
  83. $ip4_dns1 = getArray(moulinette_get('ip4_dns1'));
  84. for($i = 0; $i < $multissid; $i++) {
  85. $ssid = [
  86. 'id' => $i,
  87. 'wifi_ssid' => noneValue($wifi_ssid[$i]),
  88. 'wifi_secure' => noneValue($wifi_secure[$i]),
  89. 'wifi_passphrase' => noneValue($wifi_passphrase[$i]),
  90. 'ip6_net' => noneValue($ip6_net[$i]),
  91. 'ip6_dns0' => noneValue($ip6_dns0[$i]),
  92. 'ip6_dns1' => noneValue($ip6_dns1[$i]),
  93. 'ip4_nat_prefix' => noneValue($ip4_nat_prefix[$i]),
  94. 'ip4_dns0' => noneValue($ip4_dns0[$i]),
  95. 'ip4_dns1' => noneValue($ip4_dns1[$i]),
  96. ];
  97. array_push($ssids, $ssid);
  98. }
  99. $ip6_net = moulinette_get('ip6_net');
  100. $ip6_net = ($ip6_net == 'none') ? '' : $ip6_net;
  101. $ip4_nat_prefix = moulinette_get('ip4_nat_prefix');
  102. set('service_enabled', moulinette_get('service_enabled'));
  103. set('ssids', $ssids);
  104. set('wifi_device', $wifi_device);
  105. set('wifi_channel', $wifi_channel);
  106. set('wifi_device_list', $devs_list);
  107. set('faststatus', service_faststatus() == 0);
  108. set('is_connected_through_hotspot', is_connected_through_hotspot($ip6_net, $ip4_nat_prefix));
  109. return render('settings.html.php');
  110. });
  111. dispatch_put('/settings', function() {
  112. exec('ip link show '.escapeshellarg($_POST['wifi_device']), $output, $retcode);
  113. $wifi_device_exists = ($retcode == 0);
  114. $service_enabled = isset($_POST['service_enabled']) ? 1 : 0;
  115. $ssids = array();
  116. $id = 0;
  117. if($service_enabled == 1) {
  118. try {
  119. foreach($_POST['ssid'] as $key => $ssid) {
  120. $id = $key + 1;
  121. $ssid['ip6_net'] = empty($ssid['ip6_net']) ? 'none' : $ssid['ip6_net'];
  122. $ssid['ip6_addr'] = 'none';
  123. $ssid['wifi_secure'] = isset($ssid['wifi_secure']) ? 1 : 0;
  124. if(!$ssid['wifi_secure']) {
  125. $ssid['wifi_passphrase'] = 'none';
  126. }
  127. if(empty($ssid['wifi_ssid']) || empty($ssid['wifi_passphrase'])) {
  128. throw new Exception(T_('Your Wifi Hotspot needs a name and a password'));
  129. }
  130. if($ssid['wifi_secure'] && (strlen($ssid['wifi_passphrase']) < 8 || strlen($ssid['wifi_passphrase']) > 63)) {
  131. throw new Exception(T_('Your password must from 8 to 63 characters (WPA2 passphrase)'));
  132. }
  133. if($ssid['wifi_secure'] && preg_match('/[^[:print:]]/', $ssid['wifi_passphrase'])) {
  134. throw new Exception(T_('Only printable ASCII characters are permitted in your password'));
  135. }
  136. if(!$wifi_device_exists) {
  137. throw new Exception(T_('The wifi antenna interface seems not exist on the system'));
  138. }
  139. if($ssid['ip6_net'] != 'none') {
  140. $ssid['ip6_net'] = ipv6_expanded($ssid['ip6_net']);
  141. if(empty($ssid['ip6_net'])) {
  142. throw new Exception(T_('The IPv6 Delegated Prefix format looks bad'));
  143. }
  144. $ip6_blocs = explode(':', $ssid['ip6_net']);
  145. $ssid['ip6_addr'] = "${ip6_blocs[0]}:${ip6_blocs[1]}:${ip6_blocs[2]}:${ip6_blocs[3]}:${ip6_blocs[4]}:${ip6_blocs[5]}:${ip6_blocs[6]}:42";
  146. $ssid['ip6_net'] = ipv6_compressed($ssid['ip6_net']);
  147. $ssid['ip6_addr'] = ipv6_compressed($ssid['ip6_addr']);
  148. }
  149. if(!empty($ssid['ip6_dns0'])) {
  150. $ssid['ip6_dns0'] = ipv6_expanded($ssid['ip6_dns0']);
  151. if(empty($ssid['ip6_dns0'])) {
  152. throw new Exception(T_('The format of the first IPv6 DNS Resolver looks bad'));
  153. }
  154. $ssid['ip6_dns0'] = ipv6_compressed($ssid['ip6_dns0']);
  155. if(!empty($ssid['ip6_dns1'])) {
  156. $ssid['ip6_dns1'] = ipv6_expanded($ssid['ip6_dns1']);
  157. if(empty($ssid['ip6_dns1'])) {
  158. throw new Exception(T_('The format of the second IPv6 DNS Resolver looks bad'));
  159. }
  160. $ssid['ip6_dns1'] = ipv6_compressed($ssid['ip6_dns1']);
  161. }
  162. }
  163. if(inet_pton($ssid['ip4_dns0']) === false) {
  164. throw new Exception(T_('The format of the first IPv4 DNS Resolver looks bad'));
  165. }
  166. if(inet_pton($ssid['ip4_dns1']) === false) {
  167. throw new Exception(T_('The format of the second IPv4 DNS Resolver looks bad'));
  168. }
  169. if(inet_pton("${ssid['ip4_nat_prefix']}.0") === false) {
  170. throw new Exception(T_('The format of the IPv4 NAT Prefix (/24) looks bad : x.x.x expected)'));
  171. }
  172. if(filter_var("${ssid['ip4_nat_prefix']}.0", FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE) !== false) {
  173. throw new Exception(T_('The IPv4 NAT Prefix must be from a private range'));
  174. }
  175. array_push($ssids, $ssid);
  176. }
  177. } catch(Exception $e) {
  178. flash('error', T_('SSID')." $id: ".$e->getMessage().' ('.T_('configuration not updated').').');
  179. goto redirect;
  180. }
  181. }
  182. stop_service();
  183. moulinette_set('service_enabled', $service_enabled);
  184. $settings = array();
  185. if($service_enabled == 1) {
  186. foreach($ssids as $ssid) {
  187. foreach($ssid as $setting => $value) {
  188. $settings[$setting] .= "$value|";
  189. }
  190. }
  191. moulinette_set('multissid', count($ssids));
  192. moulinette_set('wifi_device', $_POST['wifi_device']);
  193. moulinette_set('wifi_channel', $_POST['wifi_channel']);
  194. foreach($settings as $setting => $value) {
  195. moulinette_set($setting, preg_replace('/\|$/', '', $value));
  196. }
  197. $retcode = start_service();
  198. if($retcode == 0) {
  199. flash('success', T_('Configuration updated and service successfully reloaded'));
  200. } else {
  201. flash('error', T_('Configuration updated but service reload failed'));
  202. }
  203. } else {
  204. flash('success', T_('Service successfully disabled'));
  205. }
  206. redirect:
  207. redirect_to('/');
  208. });
  209. dispatch('/status', function() {
  210. $status_lines = service_status();
  211. $status_list = '';
  212. foreach($status_lines AS $status_line) {
  213. if(preg_match('/^\[INFO\]/', $status_line)) {
  214. $status_list .= '<li class="status-info">'.htmlspecialchars($status_line).'</li>';
  215. }
  216. elseif(preg_match('/^\[OK\]/', $status_line)) {
  217. $status_list .= '<li class="status-success">'.htmlspecialchars($status_line).'</li>';
  218. }
  219. elseif(preg_match('/^\[WARN\]/', $status_line)) {
  220. $status_list .= '<li class="status-warning">'.htmlspecialchars($status_line).'</li>';
  221. }
  222. elseif(preg_match('/^\[ERR\]/', $status_line)) {
  223. $status_list .= '<li class="status-danger">'.htmlspecialchars($status_line).'</li>';
  224. }
  225. }
  226. echo $status_list;
  227. });
  228. dispatch('/lang/:locale', function($locale = 'en') {
  229. switch ($locale) {
  230. case 'fr':
  231. $_SESSION['locale'] = 'fr';
  232. break;
  233. default:
  234. $_SESSION['locale'] = 'en';
  235. }
  236. if(!empty($_GET['redirect_to'])) {
  237. redirect_to($_GET['redirect_to']);
  238. } else {
  239. redirect_to('/');
  240. }
  241. });