index.php 2.6 KB

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