index.php 5.3 KB

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