controller.php 5.0 KB

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