controller.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. function moulinette_hotspot_get($var) {
  3. return htmlspecialchars(exec('sudo yunohost app setting hotspot '.escapeshellarg($var)));
  4. }
  5. function moulinette_get($var) {
  6. return htmlspecialchars(exec('sudo yunohost app setting torclient '.escapeshellarg($var)));
  7. }
  8. function moulinette_set($var, $value) {
  9. return exec('sudo yunohost app setting torclient '.escapeshellarg($var).' -v '.escapeshellarg($value));
  10. }
  11. function stop_service() {
  12. exec('sudo systemctl stop ynh-torclient');
  13. }
  14. function start_service() {
  15. exec('sudo systemctl start ynh-torclient', $output, $retcode);
  16. return $retcode;
  17. }
  18. function service_status() {
  19. exec('sudo ynh-torclient status', $output);
  20. return $output;
  21. }
  22. function service_faststatus() {
  23. exec('sudo systemctl is-active ynh-torclient', $output, $retcode);
  24. return $retcode;
  25. }
  26. dispatch('/', function() {
  27. $ssids = explode('|', moulinette_hotspot_get('wifi_ssid'));
  28. $wifi_device_id = moulinette_get('wifi_device_id');
  29. $wifi_ssid_list = '';
  30. $wifi_ssid = '';
  31. for($i = 0; $i < count($ssids); $i++) {
  32. $active = '';
  33. if($i == $wifi_device_id) {
  34. $active = 'class="active"';
  35. $wifi_ssid = htmlentities($ssids[$i]);
  36. }
  37. $wifi_ssid_list .= "<li $active data-device-id='$i'><a href='javascript:;'>".htmlentities($ssids[$i]).'</a></li>';
  38. }
  39. set('faststatus', service_faststatus() == 0);
  40. set('service_enabled', moulinette_get('service_enabled'));
  41. set('wifi_device_id', $wifi_device_id);
  42. set('wifi_ssid', $wifi_ssid);
  43. set('wifi_ssid_list', $wifi_ssid_list);
  44. return render('settings.html.php');
  45. });
  46. dispatch_put('/settings', function() {
  47. $service_enabled = isset($_POST['service_enabled']) ? 1 : 0;
  48. if($service_enabled == 1) {
  49. try {
  50. if($_POST['wifi_device_id'] == -1) {
  51. throw new Exception(_('You need to select an associated hotspot'));
  52. }
  53. } catch(Exception $e) {
  54. flash('error', $e->getMessage().' ('._('configuration not updated').').');
  55. goto redirect;
  56. }
  57. }
  58. stop_service();
  59. moulinette_set('service_enabled', $service_enabled);
  60. if($service_enabled == 1) {
  61. moulinette_set('wifi_device_id', $_POST['wifi_device_id']);
  62. $retcode = start_service();
  63. if($retcode == 0) {
  64. flash('success', _('Configuration updated and service successfully reloaded'));
  65. } else {
  66. flash('error', _('Configuration updated but service reload failed'));
  67. }
  68. } else {
  69. flash('success', _('Service successfully disabled'));
  70. }
  71. redirect:
  72. redirect_to('/');
  73. });
  74. dispatch('/status', function() {
  75. $status_lines = service_status();
  76. $status_list = '';
  77. foreach($status_lines AS $status_line) {
  78. if(preg_match('/^\[INFO\]/', $status_line)) {
  79. $status_list .= '<li class="status-info">'.htmlspecialchars($status_line).'</li>';
  80. }
  81. elseif(preg_match('/^\[OK\]/', $status_line)) {
  82. $status_list .= '<li class="status-success">'.htmlspecialchars($status_line).'</li>';
  83. }
  84. elseif(preg_match('/^\[WARN\]/', $status_line)) {
  85. $status_list .= '<li class="status-warning">'.htmlspecialchars($status_line).'</li>';
  86. }
  87. elseif(preg_match('/^\[ERR\]/', $status_line)) {
  88. $status_list .= '<li class="status-danger">'.htmlspecialchars($status_line).'</li>';
  89. }
  90. }
  91. echo $status_list;
  92. });
  93. dispatch('/lang/:locale', function($locale = 'en') {
  94. switch($locale) {
  95. case 'fr':
  96. $_SESSION['locale'] = 'fr';
  97. break;
  98. default:
  99. $_SESSION['locale'] = 'en';
  100. }
  101. redirect_to('/');
  102. });