index.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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(HESTIA_CMD."v-list-database ".$user." ".quoteshellarg($v_database)." 'json'", $output, $return_var);
  20. check_return_code_redirect($return_var, $output, '/list/db/');
  21. $data = json_decode(implode('', $output), true);
  22. unset($output);
  23. // Parse database
  24. $v_username = $user;
  25. $v_dbuser = preg_replace("/^".$user_plain."_/", "", $data[$v_database]['DBUSER']);
  26. $v_password = "";
  27. $v_host = $data[$v_database]['HOST'];
  28. $v_type = $data[$v_database]['TYPE'];
  29. $v_charset = $data[$v_database]['CHARSET'];
  30. $v_date = $data[$v_database]['DATE'];
  31. $v_time = $data[$v_database]['TIME'];
  32. $v_suspended = $data[$v_database]['SUSPENDED'];
  33. if ($v_suspended == 'yes') {
  34. $v_status = 'suspended';
  35. } else {
  36. $v_status = 'active';
  37. }
  38. // Check POST request
  39. if (!empty($_POST['save'])) {
  40. $v_username = $user;
  41. // Check token
  42. verify_csrf($_POST);
  43. // Change database user
  44. if (($v_dbuser != $_POST['v_dbuser']) && (empty($_SESSION['error_msg']))) {
  45. $cmd = implode(" ", array(
  46. HESTIA_CMD . "v-change-database-user",
  47. // $user is already shell-quoted
  48. $user,
  49. quoteshellarg($v_database),
  50. quoteshellarg($_POST['v_dbuser']),
  51. ));
  52. exec($cmd, $output, $return_var);
  53. check_return_code($return_var, $output);
  54. unset($output);
  55. }
  56. // Change database password
  57. if ((!empty($_POST['v_password'])) && (empty($_SESSION['error_msg']))) {
  58. if (!validate_password($_POST['v_password'])) {
  59. $_SESSION['error_msg'] = _('Password does not match the minimum requirements');
  60. } else {
  61. $v_password = tempnam("/tmp", "vst");
  62. $fp = fopen($v_password, "w");
  63. fwrite($fp, $_POST['v_password']."\n");
  64. fclose($fp);
  65. exec(HESTIA_CMD."v-change-database-password ".$user." ".quoteshellarg($v_database)." ".$v_password, $output, $return_var);
  66. check_return_code($return_var, $output);
  67. unset($output);
  68. unlink($v_password);
  69. $v_password = quoteshellarg($_POST['v_password']);
  70. }
  71. }
  72. // Set success message
  73. if (empty($_SESSION['error_msg'])) {
  74. $_SESSION['ok_msg'] = _('Changes has been saved.');
  75. }
  76. // if the mysql username was changed, render_page() below will render with the OLD mysql username,
  77. // to prvent that, make the browser refresh the page.
  78. http_response_code(303);
  79. header("Location: " . $_SERVER['REQUEST_URI']);
  80. die();
  81. }
  82. // Render page
  83. render_page($user, $TAB, 'edit_db');
  84. // Flush session messages
  85. unset($_SESSION['error_msg']);
  86. unset($_SESSION['ok_msg']);