index.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 POST request
  8. if (!empty($_POST['ok'])) {
  9. // Check token
  10. verify_csrf($_POST);
  11. // Check empty fields
  12. if (empty($_POST['v_database'])) {
  13. $errors[] = _('database');
  14. }
  15. if (empty($_POST['v_dbuser'])) {
  16. $errors[] = _('username');
  17. }
  18. if (empty($_POST['v_password'])) {
  19. $errors[] = _('password');
  20. }
  21. if (empty($_POST['v_type'])) {
  22. $errors[] = _('type');
  23. }
  24. if (empty($_POST['v_host'])) {
  25. $errors[] = _('host');
  26. }
  27. if (empty($_POST['v_charset'])) {
  28. $errors[] = _('charset');
  29. }
  30. if (!empty($errors[0])) {
  31. foreach ($errors as $i => $error) {
  32. if ($i == 0) {
  33. $error_msg = $error;
  34. } else {
  35. $error_msg = $error_msg.", ".$error;
  36. }
  37. }
  38. $_SESSION['error_msg'] = sprintf(_('Field "%s" can not be blank.'), $error_msg);
  39. }
  40. // Validate email
  41. if ((!empty($_POST['v_db_email'])) && (empty($_SESSION['error_msg']))) {
  42. if (!filter_var($_POST['v_db_email'], FILTER_VALIDATE_EMAIL)) {
  43. $_SESSION['error_msg'] = _('Please enter valid email address.');
  44. }
  45. }
  46. // Check password length
  47. if (empty($_SESSION['error_msg'])) {
  48. if (!validate_password($_POST['v_password'])) {
  49. $_SESSION['error_msg'] = _('Password does not match the minimum requirements');
  50. }
  51. }
  52. // Protect input
  53. $v_database = quoteshellarg($_POST['v_database']);
  54. $v_dbuser = quoteshellarg($_POST['v_dbuser']);
  55. $v_type = $_POST['v_type'];
  56. $v_charset = $_POST['v_charset'];
  57. $v_host = $_POST['v_host'];
  58. $v_db_email = $_POST['v_db_email'];
  59. // Add database
  60. if (empty($_SESSION['error_msg'])) {
  61. $v_type = quoteshellarg($_POST['v_type']);
  62. $v_charset = quoteshellarg($_POST['v_charset']);
  63. $v_host = quoteshellarg($_POST['v_host']);
  64. $v_password = tempnam("/tmp", "vst");
  65. $fp = fopen($v_password, "w");
  66. fwrite($fp, $_POST['v_password']."\n");
  67. fclose($fp);
  68. exec(HESTIA_CMD."v-add-database ".$user." ".$v_database." ".$v_dbuser." ".$v_password." ".$v_type." ".$v_host." ".$v_charset, $output, $return_var);
  69. check_return_code($return_var, $output);
  70. unset($output);
  71. unlink($v_password);
  72. $v_password = quoteshellarg($_POST['v_password']);
  73. $v_type = $_POST['v_type'];
  74. $v_host = $_POST['v_host'];
  75. $v_charset = $_POST['v_charset'];
  76. }
  77. // Get database manager url
  78. if (empty($_SESSION['error_msg'])) {
  79. list($http_host, $port) = explode(':', $_SERVER["HTTP_HOST"] . ":");
  80. if ($_POST['v_host'] != 'localhost') {
  81. $http_host = $_POST['v_host'];
  82. }
  83. if ($_POST['v_type'] == 'mysql') {
  84. $db_admin = "phpMyAdmin";
  85. }
  86. if ($_POST['v_type'] == 'mysql') {
  87. $db_admin_link = "http://".$http_host."/phpmyadmin/";
  88. }
  89. if (($_POST['v_type'] == 'mysql') && (!empty($_SESSION['DB_PMA_ALIAS']))) {
  90. $db_admin_link = "http://".$http_host."/".$_SESSION['DB_PMA_ALIAS'];
  91. }
  92. if ($_POST['v_type'] == 'pgsql') {
  93. $db_admin = "phpPgAdmin";
  94. }
  95. if ($_POST['v_type'] == 'pgsql') {
  96. $db_admin_link = "http://".$http_host."/phppgadmin/";
  97. }
  98. if (($_POST['v_type'] == 'pgsql') && (!empty($_SESSION['DB_PGA_ALIAS']))) {
  99. $db_admin_link = "http://".$http_host."/".$_SESSION['DB_PGA_ALIAS'];
  100. }
  101. }
  102. // Email login credentials
  103. if ((!empty($v_db_email)) && (empty($_SESSION['error_msg']))) {
  104. $to = $v_db_email;
  105. $subject = _("Database Credentials");
  106. $hostname = get_hostname();
  107. $from = "noreply@".$hostname;
  108. $from_name = _('Hestia Control Panel');
  109. $mailtext = sprintf(_('DATABASE_READY'), $user_plain."_".$_POST['v_database'], $user_plain."_".$_POST['v_dbuser'], $_POST['v_password'], $db_admin_link);
  110. send_email($to, $subject, $mailtext, $from, $from_name);
  111. }
  112. // Flush field values on success
  113. if (empty($_SESSION['error_msg'])) {
  114. $_SESSION['ok_msg'] = sprintf(_('DATABASE_CREATED_OK'), htmlentities($user_plain)."_".htmlentities($_POST['v_database']), htmlentities($user_plain)."_".htmlentities($_POST['v_database']));
  115. $_SESSION['ok_msg'] .= " / <a href=".$db_admin_link." target='_blank'>" . sprintf(_('open %s'), $db_admin) . "</a>";
  116. unset($v_database);
  117. unset($v_dbuser);
  118. unset($v_password);
  119. unset($v_type);
  120. unset($v_charset);
  121. }
  122. }
  123. // Get user email
  124. $v_db_email = '';
  125. if (empty($v_database)) {
  126. $v_database = '';
  127. }
  128. if (empty($v_dbuser)) {
  129. $v_dbuser = '';
  130. }
  131. // List avaiable database types
  132. $db_types = explode(',', $_SESSION['DB_SYSTEM']);
  133. // List available database servers
  134. exec(HESTIA_CMD."v-list-database-hosts json", $output, $return_var);
  135. $db_hosts_tmp1 = json_decode(implode('', $output), true);
  136. $db_hosts_tmp2 = array_map(function ($host) {
  137. return $host['HOST'];
  138. }, $db_hosts_tmp1);
  139. $db_hosts = array_values(array_unique($db_hosts_tmp2));
  140. unset($output);
  141. unset($db_hosts_tmp1);
  142. unset($db_hosts_tmp2);
  143. render_page($user, $TAB, 'add_db');
  144. // Flush session messages
  145. unset($_SESSION['error_msg']);
  146. unset($_SESSION['ok_msg']);