controller.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /* PirateBox app for YunoHost
  3. * Copyright (C) 2015 Julien Vaubourg <julien@vaubourg.com>
  4. * Contribute at https://github.com/jvaubourg/piratebox_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 piratebox '.escapeshellarg($var)));
  21. }
  22. function moulinette_set($var, $value) {
  23. return exec('sudo yunohost app setting piratebox '.escapeshellarg($var).' -v '.escapeshellarg($value));
  24. }
  25. function moulinette_hotspot_get($var) {
  26. return htmlspecialchars(exec('sudo yunohost app setting hotspot '.escapeshellarg($var)));
  27. }
  28. function stop_service() {
  29. exec('sudo service ynh-piratebox stop');
  30. }
  31. function start_service() {
  32. exec('sudo service ynh-piratebox start', $output, $retcode);
  33. return $retcode;
  34. }
  35. function service_status() {
  36. exec('sudo service ynh-piratebox status', $output);
  37. return $output;
  38. }
  39. function service_faststatus() {
  40. exec('ls /etc/nginx/conf.d/captive-piratebox.conf', $output, $retcode);
  41. return $retcode;
  42. }
  43. dispatch('/', function() {
  44. $ssids = explode('|', moulinette_hotspot_get('wifi_ssid'));
  45. $wifi_device_id = moulinette_get('wifi_device_id');
  46. $wifi_ssid_list = '';
  47. $wifi_ssid = '';
  48. for($i = 0; $i < count($ssids); $i++) {
  49. $active = '';
  50. if($i == $wifi_device_id) {
  51. $active = 'class="active"';
  52. $wifi_ssid = htmlentities($ssids[$i]);
  53. }
  54. $wifi_ssid_list .= "<li $active data-device-id='$i'><a href='#'>".htmlentities($ssids[$i]).'</a></li>';
  55. }
  56. set('faststatus', service_faststatus() == 0);
  57. set('service_enabled', moulinette_get('service_enabled'));
  58. set('wifi_device_id', $wifi_device_id);
  59. set('wifi_ssid', $wifi_ssid);
  60. set('wifi_ssid_list', $wifi_ssid_list);
  61. set('opt_renaming', moulinette_get('opt_renaming'));
  62. set('opt_deleting', moulinette_get('opt_deleting'));
  63. set('opt_chat', moulinette_get('opt_chat'));
  64. set('opt_name', moulinette_get('opt_name'));
  65. set('opt_domain', moulinette_get('opt_domain'));
  66. return render('settings.html.php');
  67. });
  68. dispatch_put('/settings', function() {
  69. $service_enabled = isset($_POST['service_enabled']) ? 1 : 0;
  70. if($service_enabled == 1) {
  71. try {
  72. $_POST['opt_name'] = htmlentities(str_replace('"', '', $_POST['opt_name']));
  73. if(empty($_POST['opt_name'])) {
  74. throw new Exception(T_('The name cannot be empty'));
  75. }
  76. } catch(Exception $e) {
  77. flash('error', T_('PirateBox')." $id: ".$e->getMessage().' ('.T_('configuration not updated').').');
  78. goto redirect;
  79. }
  80. }
  81. stop_service();
  82. moulinette_set('service_enabled', $service_enabled);
  83. if($service_enabled == 1) {
  84. moulinette_set('opt_name', $_POST['opt_name']);
  85. moulinette_set('opt_renaming', isset($_POST['opt_renaming']) ? 1 : 0);
  86. moulinette_set('opt_deleting', isset($_POST['opt_deleting']) ? 1 : 0);
  87. moulinette_set('opt_chat', isset($_POST['opt_chat']) ? 1 : 0);
  88. moulinette_set('wifi_device_id', $_POST['wifi_device_id']);
  89. $retcode = start_service();
  90. if($retcode == 0) {
  91. flash('success', T_('Configuration updated and service successfully reloaded'));
  92. } else {
  93. flash('error', T_('Configuration updated but service reload failed'));
  94. }
  95. } else {
  96. flash('success', T_('Service successfully disabled'));
  97. }
  98. redirect:
  99. redirect_to('/');
  100. });
  101. dispatch('/status', function() {
  102. $status_lines = service_status();
  103. $status_list = '';
  104. foreach($status_lines AS $status_line) {
  105. if(preg_match('/^\[INFO\]/', $status_line)) {
  106. $status_list .= '<li class="status-info">'.htmlspecialchars($status_line).'</li>';
  107. }
  108. elseif(preg_match('/^\[OK\]/', $status_line)) {
  109. $status_list .= '<li class="status-success">'.htmlspecialchars($status_line).'</li>';
  110. }
  111. elseif(preg_match('/^\[WARN\]/', $status_line)) {
  112. $status_list .= '<li class="status-warning">'.htmlspecialchars($status_line).'</li>';
  113. }
  114. elseif(preg_match('/^\[ERR\]/', $status_line)) {
  115. $status_list .= '<li class="status-danger">'.htmlspecialchars($status_line).'</li>';
  116. }
  117. }
  118. echo $status_list;
  119. });
  120. dispatch('/lang/:locale', function($locale = 'en') {
  121. switch ($locale) {
  122. case 'fr':
  123. $_SESSION['locale'] = 'fr';
  124. break;
  125. default:
  126. $_SESSION['locale'] = 'en';
  127. }
  128. if(!empty($_GET['redirect_to'])) {
  129. redirect_to($_GET['redirect_to']);
  130. } else {
  131. redirect_to('/');
  132. }
  133. });