index.php 2.6 KB

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