controller.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /* Tor Client app for YunoHost
  3. * Copyright (C) 2015 Émile Morel <emile@bleuchtang.fr>
  4. * Copyright (C) 2015 Julien Vaubourg <julien@vaubourg.com>
  5. * Contribute at https://github.com/labriqueinternet/torclient_ynh
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. function ynh_setting_get($setting, $app = 'torclient') {
  21. $value = exec("sudo grep \"^$setting:\" /etc/yunohost/apps/$app/settings.yml");
  22. $value = preg_replace('/^[^:]+:\s*["\']?/', '', $value);
  23. $value = preg_replace('/\s*["\']$/', '', $value);
  24. return htmlspecialchars($value);
  25. }
  26. function ynh_setting_set($setting, $value) {
  27. return exec('sudo yunohost app setting torclient '.escapeshellarg($setting).' -v '.escapeshellarg($value));
  28. }
  29. function stop_service() {
  30. exec('sudo systemctl stop ynh-torclient');
  31. }
  32. function start_service() {
  33. exec('sudo systemctl start ynh-torclient', $output, $retcode);
  34. return $retcode;
  35. }
  36. function service_status() {
  37. exec('sudo ynh-torclient status', $output);
  38. return $output;
  39. }
  40. function service_faststatus() {
  41. exec('sudo systemctl is-active ynh-torclient', $output, $retcode);
  42. return $retcode;
  43. }
  44. dispatch('/', function() {
  45. $ssids = explode('|', ynh_setting_get('wifi_ssid', 'hotspot'));
  46. $wifi_device_id = ynh_setting_get('wifi_device_id');
  47. $wifi_ssid_list = '';
  48. $wifi_ssid = '';
  49. for($i = 0; $i < count($ssids); $i++) {
  50. $active = '';
  51. if($i == $wifi_device_id) {
  52. $active = 'class="active"';
  53. $wifi_ssid = htmlentities($ssids[$i]);
  54. }
  55. $wifi_ssid_list .= "<li $active data-device-id='$i'><a href='javascript:;'>".htmlentities($ssids[$i]).'</a></li>';
  56. }
  57. set('faststatus', service_faststatus() == 0);
  58. set('service_enabled', ynh_setting_get('service_enabled'));
  59. set('wifi_device_id', $wifi_device_id);
  60. set('wifi_ssid', $wifi_ssid);
  61. set('wifi_ssid_list', $wifi_ssid_list);
  62. return render('settings.html.php');
  63. });
  64. dispatch_put('/settings', function() {
  65. $service_enabled = isset($_POST['service_enabled']) ? 1 : 0;
  66. if($service_enabled == 1) {
  67. try {
  68. if($_POST['wifi_device_id'] == -1) {
  69. throw new Exception(_('You need to select an associated hotspot'));
  70. }
  71. } catch(Exception $e) {
  72. flash('error', $e->getMessage().' ('._('configuration not updated').').');
  73. goto redirect;
  74. }
  75. }
  76. stop_service();
  77. ynh_setting_set('service_enabled', $service_enabled);
  78. if($service_enabled == 1) {
  79. ynh_setting_set('wifi_device_id', $_POST['wifi_device_id']);
  80. $retcode = start_service();
  81. if($retcode == 0) {
  82. flash('success', _('Configuration updated and service successfully reloaded'));
  83. } else {
  84. flash('error', _('Configuration updated but service reload failed'));
  85. }
  86. } else {
  87. flash('success', _('Service successfully disabled'));
  88. }
  89. redirect:
  90. redirect_to('/');
  91. });
  92. dispatch('/status', function() {
  93. $status_lines = service_status();
  94. $status_list = '';
  95. foreach($status_lines AS $status_line) {
  96. if(preg_match('/^\[INFO\]/', $status_line)) {
  97. $status_list .= '<li class="status-info">'.htmlspecialchars($status_line).'</li>';
  98. }
  99. elseif(preg_match('/^\[OK\]/', $status_line)) {
  100. $status_list .= '<li class="status-success">'.htmlspecialchars($status_line).'</li>';
  101. }
  102. elseif(preg_match('/^\[WARN\]/', $status_line)) {
  103. $status_list .= '<li class="status-warning">'.htmlspecialchars($status_line).'</li>';
  104. }
  105. elseif(preg_match('/^\[ERR\]/', $status_line)) {
  106. $status_list .= '<li class="status-danger">'.htmlspecialchars($status_line).'</li>';
  107. }
  108. }
  109. echo $status_list;
  110. });
  111. dispatch('/lang/:locale', function($locale = 'en') {
  112. switch($locale) {
  113. case 'fr':
  114. $_SESSION['locale'] = 'fr';
  115. break;
  116. default:
  117. $_SESSION['locale'] = 'en';
  118. }
  119. redirect_to('/');
  120. });