index.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. ob_start();
  3. $TAB = 'Access Key';
  4. // Main include
  5. include($_SERVER['DOCUMENT_ROOT']."/inc/main.php");
  6. // Checks if API access is enabled
  7. $api_status = (!empty($_SESSION['API_SYSTEM']) && is_numeric($_SESSION['API_SYSTEM'])) ? $_SESSION['API_SYSTEM'] : 0;
  8. if (($user_plain == 'admin' && $api_status < 1) || ($user_plain != 'admin' && $api_status < 2)) {
  9. header("Location: /edit/user/");
  10. exit;
  11. }
  12. // APIs available
  13. exec(HESTIA_CMD."v-list-apis json", $output, $return_var);
  14. $apis = json_decode(implode('', $output), true);
  15. $apis = array_filter($apis, function ($api) use ($user_plain) {
  16. return ($user_plain == 'admin' || $api['ROLE'] == 'user');
  17. });
  18. ksort($apis);
  19. unset($output);
  20. // Check POST request
  21. if (!empty($_POST['ok'])) {
  22. // Check token
  23. verify_csrf($_POST);
  24. // Validate apis
  25. $apis_selected = (!empty($_POST['v_apis']) && is_array($_POST['v_apis'])) ? $_POST['v_apis'] : [];
  26. $check_invalid_apis = array_filter($apis_selected, function ($selected) use ($apis) {
  27. return !array_key_exists($selected, $apis);
  28. });
  29. if (empty($apis_selected)) {
  30. $errors[] = _('Permissions');
  31. } else if (count($check_invalid_apis) > 0) {
  32. //$errors[] = sprintf("%d apis not allowed", count($check_invalid_apis));
  33. foreach ($check_invalid_apis as $api_name) {
  34. $errors[] = sprintf("api %s not allowed", $api_name);
  35. }
  36. }
  37. if (!empty($errors[0])) {
  38. foreach ($errors as $i => $error) {
  39. if ($i == 0) {
  40. $error_msg = $error;
  41. } else {
  42. $error_msg = $error_msg.", ".$error;
  43. }
  44. }
  45. $_SESSION['error_msg'] = sprintf(_('Field "%s" can not be blank.'), $error_msg);
  46. }
  47. // Protect input
  48. $v_apis = escapeshellarg(implode(',', $apis_selected));
  49. $v_comment = escapeshellarg(trim($_POST['v_comment'] ?? ''));
  50. // Add access key
  51. if (empty($_SESSION['error_msg'])) {
  52. exec(HESTIA_CMD."v-add-access-key ".$user." ".$v_apis." ".$v_comment." json", $output, $return_var);
  53. $key_data = json_decode(implode('', $output), true);
  54. check_return_code($return_var, $output);
  55. unset($output);
  56. }
  57. // Flush field values on success
  58. if (empty($_SESSION['error_msg'])) {
  59. $_SESSION['ok_msg'] = sprintf(_('Access key %s has been created'), htmlentities($key_data['ACCESS_KEY_ID']));
  60. unset($apis_selected);
  61. unset($check_invalid_apis);
  62. unset($v_apis);
  63. unset($v_comment);
  64. }
  65. }
  66. // Render
  67. if (empty($key_data)) {
  68. render_page($user, $TAB, 'add_access_key');
  69. } else {
  70. render_page($user, $TAB, 'list_access_key');
  71. }
  72. // Flush session messages
  73. unset($_SESSION['error_msg']);
  74. unset($_SESSION['ok_msg']);