index.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 a 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 = "https://" . $http_host . "/phpmyadmin/";
  106. }
  107. if ($_POST["v_type"] == "mysql" && !empty($_SESSION["DB_PMA_ALIAS"])) {
  108. $db_admin_link = "https://" . $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 = "https://" . $http_host . "/phppgadmin/";
  115. }
  116. if ($_POST["v_type"] == "pgsql" && !empty($_SESSION["DB_PGA_ALIAS"])) {
  117. $db_admin_link = "https://" . $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. $template = get_email_template("database_credentials", $_SESSION["language"]);
  124. if (!empty($template)) {
  125. preg_match("/<subject>(.*?)<\/subject>/si", $template, $matches);
  126. $subject = $matches[1];
  127. $subject = str_replace(
  128. ["{{hostname}}", "{{appname}}", "{{dabase}}", "{{dbuser}}"],
  129. [
  130. get_hostname(),
  131. $_SESSION["APP_NAME"],
  132. $user_plain . "_" . $_POST["v_database"],
  133. $user_plain . "_" . $_POST["v_dbuser"],
  134. ],
  135. $subject,
  136. );
  137. $template = str_replace($matches[0], "", $template);
  138. } else {
  139. $template = _(
  140. "Database has been created.\n" .
  141. "\n" .
  142. "Database: {{database}}\n" .
  143. "Username: {{username}}\n" .
  144. "Password: {{password}}\n" .
  145. "SQL Manager: {{dbadmin}}\n" .
  146. "\n" .
  147. "Best regards,\n" .
  148. "\n" .
  149. "--\n" .
  150. "{{appname}}",
  151. );
  152. }
  153. if (empty($subject)) {
  154. $subject = str_replace(
  155. ["{{subject}}", "{{hostname}}", "{{appname}}"],
  156. [
  157. sprintf(
  158. _("Database Credentials: %s"),
  159. $user_plain . "_" . $_POST["v_database"],
  160. ),
  161. get_hostname(),
  162. $_SESSION["APP_NAME"],
  163. ],
  164. $_SESSION["SUBJECT_EMAIL"],
  165. );
  166. }
  167. $hostname = get_hostname();
  168. $from = !empty($_SESSION["FROM_EMAIL"]) ? $_SESSION["FROM_EMAIL"] : "noreply@" . $hostname;
  169. $from_name = !empty($_SESSION["FROM_NAME"])
  170. ? $_SESSION["FROM_NAME"]
  171. : $_SESSION["APP_NAME"];
  172. $mailtext = translate_email($template, [
  173. "database" => htmlentities($user_plain . "_" . $_POST["v_database"]),
  174. "username" => htmlentities($user_plain . "_" . $_POST["v_dbuser"]),
  175. "password" => htmlentities($_POST["v_password"]),
  176. "dbadmin" => $db_admin_link,
  177. "appname" => $_SESSION["APP_NAME"],
  178. ]);
  179. send_email($to, $subject, $mailtext, $from, $from_name);
  180. }
  181. // Flush field values on success
  182. if (empty($_SESSION["error_msg"])) {
  183. $_SESSION["ok_msg"] = htmlify_trans(
  184. sprintf(
  185. _("Database {%s} has been created successfully. / {Open %s}"),
  186. htmlentities($user_plain) . "_" . htmlentities($_POST["v_database"]),
  187. htmlentities($user_plain) . "_" . htmlentities($_POST["v_database"]),
  188. ),
  189. "</a>",
  190. '<a class="u-text-bold" href="/edit/db/?database=' .
  191. htmlentities($user_plain) .
  192. "_" .
  193. htmlentities($_POST["v_database"]) .
  194. '">',
  195. '<a class="u-text-bold" href="' . $db_admin_link . '" target="_blank">',
  196. );
  197. unset($v_database);
  198. unset($v_dbuser);
  199. unset($v_password);
  200. unset($v_type);
  201. unset($v_charset);
  202. }
  203. }
  204. // Get user email
  205. $v_db_email = "";
  206. if (empty($v_database)) {
  207. $v_database = "";
  208. }
  209. if (empty($v_dbuser)) {
  210. $v_dbuser = "";
  211. }
  212. // List avaiable database types
  213. $db_types = explode(",", $_SESSION["DB_SYSTEM"]);
  214. // List available database servers
  215. exec(HESTIA_CMD . "v-list-database-hosts json", $output, $return_var);
  216. $db_hosts_tmp1 = json_decode(implode("", $output), true);
  217. $db_hosts_tmp2 = array_map(function ($host) {
  218. return $host["HOST"];
  219. }, $db_hosts_tmp1);
  220. $db_hosts = array_values(array_unique($db_hosts_tmp2));
  221. unset($output);
  222. unset($db_hosts_tmp1);
  223. unset($db_hosts_tmp2);
  224. $accept = $_GET["accept"] ?? "";
  225. render_page($user, $TAB, "add_db");
  226. // Flush session messages
  227. unset($_SESSION["error_msg"]);
  228. unset($_SESSION["ok_msg"]);