index.php 3.0 KB

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