index.php 2.7 KB

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