index.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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(
  69. HESTIA_CMD .
  70. "v-add-database " .
  71. $user .
  72. " " .
  73. $v_database .
  74. " " .
  75. $v_dbuser .
  76. " " .
  77. $v_password .
  78. " " .
  79. $v_type .
  80. " " .
  81. $v_host .
  82. " " .
  83. $v_charset,
  84. $output,
  85. $return_var,
  86. );
  87. check_return_code($return_var, $output);
  88. unset($output);
  89. unlink($v_password);
  90. $v_password = quoteshellarg($_POST["v_password"]);
  91. $v_type = $_POST["v_type"];
  92. $v_host = $_POST["v_host"];
  93. $v_charset = $_POST["v_charset"];
  94. }
  95. // Get database manager url
  96. if (empty($_SESSION["error_msg"])) {
  97. [$http_host, $port] = explode(":", $_SERVER["HTTP_HOST"] . ":");
  98. if ($_POST["v_host"] != "localhost") {
  99. $http_host = $_POST["v_host"];
  100. }
  101. if ($_POST["v_type"] == "mysql") {
  102. $db_admin = "phpMyAdmin";
  103. }
  104. if ($_POST["v_type"] == "mysql") {
  105. $db_admin_link = "http://" . $http_host . "/phpmyadmin/";
  106. }
  107. if ($_POST["v_type"] == "mysql" && !empty($_SESSION["DB_PMA_ALIAS"])) {
  108. $db_admin_link = "http://" . $http_host . "/" . $_SESSION["DB_PMA_ALIAS"];
  109. }
  110. if ($_POST["v_type"] == "pgsql") {
  111. $db_admin = "phpPgAdmin";
  112. }
  113. if ($_POST["v_type"] == "pgsql") {
  114. $db_admin_link = "http://" . $http_host . "/phppgadmin/";
  115. }
  116. if ($_POST["v_type"] == "pgsql" && !empty($_SESSION["DB_PGA_ALIAS"])) {
  117. $db_admin_link = "http://" . $http_host . "/" . $_SESSION["DB_PGA_ALIAS"];
  118. }
  119. }
  120. // Email login credentials
  121. if (!empty($v_db_email) && empty($_SESSION["error_msg"])) {
  122. $to = $v_db_email;
  123. $subject = _("Database Credentials");
  124. $hostname = get_hostname();
  125. $from = "noreply@" . $hostname;
  126. $from_name = _("Hestia Control Panel");
  127. $mailtext = sprintf(
  128. _("DATABASE_READY"),
  129. $user_plain . "_" . $_POST["v_database"],
  130. $user_plain . "_" . $_POST["v_dbuser"],
  131. $_POST["v_password"],
  132. $db_admin_link,
  133. );
  134. send_email($to, $subject, $mailtext, $from, $from_name);
  135. }
  136. // Flush field values on success
  137. if (empty($_SESSION["error_msg"])) {
  138. $_SESSION["ok_msg"] = sprintf(
  139. _("DATABASE_CREATED_OK"),
  140. htmlentities($user_plain) . "_" . htmlentities($_POST["v_database"]),
  141. htmlentities($user_plain) . "_" . htmlentities($_POST["v_database"]),
  142. );
  143. $_SESSION["ok_msg"] .=
  144. " / <a href=" .
  145. $db_admin_link .
  146. " target='_blank'>" .
  147. sprintf(_("open %s"), $db_admin) .
  148. "</a>";
  149. unset($v_database);
  150. unset($v_dbuser);
  151. unset($v_password);
  152. unset($v_type);
  153. unset($v_charset);
  154. }
  155. }
  156. // Get user email
  157. $v_db_email = "";
  158. if (empty($v_database)) {
  159. $v_database = "";
  160. }
  161. if (empty($v_dbuser)) {
  162. $v_dbuser = "";
  163. }
  164. // List avaiable database types
  165. $db_types = explode(",", $_SESSION["DB_SYSTEM"]);
  166. // List available database servers
  167. exec(HESTIA_CMD . "v-list-database-hosts json", $output, $return_var);
  168. $db_hosts_tmp1 = json_decode(implode("", $output), true);
  169. $db_hosts_tmp2 = array_map(function ($host) {
  170. return $host["HOST"];
  171. }, $db_hosts_tmp1);
  172. $db_hosts = array_values(array_unique($db_hosts_tmp2));
  173. unset($output);
  174. unset($db_hosts_tmp1);
  175. unset($db_hosts_tmp2);
  176. render_page($user, $TAB, "add_db");
  177. // Flush session messages
  178. unset($_SESSION["error_msg"]);
  179. unset($_SESSION["ok_msg"]);