index.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. use function Hestiacp\quoteshellarg\quoteshellarg;
  3. ob_start();
  4. $TAB = "DB";
  5. // Main include
  6. include $_SERVER["DOCUMENT_ROOT"] . "/inc/main.php";
  7. // Check database id
  8. if (empty($_GET["database"])) {
  9. header("Location: /list/db/");
  10. exit();
  11. }
  12. // Edit as someone else?
  13. if ($_SESSION["userContext"] === "admin" && !empty($_GET["user"])) {
  14. $user = quoteshellarg($_GET["user"]);
  15. $user_plain = htmlentities($_GET["user"]);
  16. }
  17. // List datbase
  18. $v_database = $_GET["database"];
  19. exec(
  20. HESTIA_CMD . "v-list-database " . $user . " " . quoteshellarg($v_database) . " 'json'",
  21. $output,
  22. $return_var,
  23. );
  24. check_return_code_redirect($return_var, $output, "/list/db/");
  25. $data = json_decode(implode("", $output), true);
  26. unset($output);
  27. // Parse database
  28. $v_username = $user;
  29. $v_dbuser = preg_replace("/^" . $user_plain . "_/", "", $data[$v_database]["DBUSER"]);
  30. $v_password = "";
  31. $v_host = $data[$v_database]["HOST"];
  32. $v_type = $data[$v_database]["TYPE"];
  33. $v_charset = $data[$v_database]["CHARSET"];
  34. $v_date = $data[$v_database]["DATE"];
  35. $v_time = $data[$v_database]["TIME"];
  36. $v_suspended = $data[$v_database]["SUSPENDED"];
  37. if ($v_suspended == "yes") {
  38. $v_status = "suspended";
  39. } else {
  40. $v_status = "active";
  41. }
  42. // Check POST request
  43. if (!empty($_POST["save"])) {
  44. $v_username = $user;
  45. // Check token
  46. verify_csrf($_POST);
  47. // Change database user
  48. if ($v_dbuser != $_POST["v_dbuser"] && empty($_SESSION["error_msg"])) {
  49. $cmd = implode(" ", [
  50. HESTIA_CMD . "v-change-database-user",
  51. // $user is already shell-quoted
  52. $user,
  53. quoteshellarg($v_database),
  54. quoteshellarg($_POST["v_dbuser"]),
  55. ]);
  56. exec($cmd, $output, $return_var);
  57. check_return_code($return_var, $output);
  58. unset($output);
  59. }
  60. // Change database password
  61. if (!empty($_POST["v_password"]) && empty($_SESSION["error_msg"])) {
  62. if (!validate_password($_POST["v_password"])) {
  63. $_SESSION["error_msg"] = _("Password does not match the minimum requirements.");
  64. } else {
  65. $v_password = tempnam("/tmp", "vst");
  66. $fp = fopen($v_password, "w");
  67. fwrite($fp, $_POST["v_password"] . "\n");
  68. fclose($fp);
  69. exec(
  70. HESTIA_CMD .
  71. "v-change-database-password " .
  72. $user .
  73. " " .
  74. quoteshellarg($v_database) .
  75. " " .
  76. $v_password,
  77. $output,
  78. $return_var,
  79. );
  80. check_return_code($return_var, $output);
  81. unset($output);
  82. unlink($v_password);
  83. $v_password = quoteshellarg($_POST["v_password"]);
  84. }
  85. }
  86. // Set success message
  87. if (empty($_SESSION["error_msg"])) {
  88. $_SESSION["ok_msg"] = _("Changes have been saved.");
  89. }
  90. // if the mysql username was changed, render_page() below will render with the OLD mysql username,
  91. // to prvent that, make the browser refresh the page.
  92. http_response_code(303);
  93. header("Location: " . $_SERVER["REQUEST_URI"]);
  94. die();
  95. }
  96. // Render page
  97. render_page($user, $TAB, "edit_db");
  98. // Flush session messages
  99. unset($_SESSION["error_msg"]);
  100. unset($_SESSION["ok_msg"]);