get_user_config.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. session_start();
  3. function dieAjaxError($message) {
  4. header('HTTP/1.0 403 Forbidden');
  5. header('Content-Type: application/json');
  6. die(json_encode(['error' => $message]));
  7. }
  8. // Проверка AJAX-запроса
  9. if (empty($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest') {
  10. dieAjaxError('Direct access not allowed');
  11. }
  12. // Опционально: CSRF проверка
  13. // if (empty($_GET['csrf']) || $_GET['csrf'] !== $_SESSION['csrf_token']) {
  14. // dieAjaxError('Invalid CSRF token');
  15. // }
  16. define("CONFIG", 1);
  17. $config_file = __DIR__ . '/config.php';
  18. if (!file_exists($config_file)) {
  19. dieAjaxError("Configuration file not found: $config_file");
  20. }
  21. $servers = require $config_file;
  22. $server_name = $_GET['server'] ?? '';
  23. $username = $_GET['username'] ?? '';
  24. if (!isset($servers[$server_name]) || empty($username)) {
  25. http_response_code(400);
  26. die('Invalid parameters');
  27. }
  28. // CCD-файл
  29. $ccd_file = $servers[$server_name]['ccd'] . "/" . $username;
  30. // Путь к скрипту
  31. $script_path = '/etc/openvpn/server/cmd/show_user_config.sh'; // GET_USER_CCD
  32. $command = sprintf(
  33. 'sudo %s %s 2>&1',
  34. escapeshellcmd($script_path),
  35. escapeshellarg($ccd_file)
  36. );
  37. exec($command, $output, $return_var);
  38. if ($return_var !== 0) {
  39. http_response_code(500);
  40. echo json_encode(['error' => implode("\n", $output)]);
  41. exit;
  42. }
  43. // Вывод конфига
  44. header('Content-Type: text/plain');
  45. echo implode("\n", $output);