index.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. error_reporting(NULL);
  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. if ((!isset($_POST['token'])) || ($_SESSION['token'] != $_POST['token'])) {
  11. header('location: /login/');
  12. exit();
  13. }
  14. // Check empty fields
  15. if (empty($_POST['v_database'])) $errors[] = __('database');
  16. if (empty($_POST['v_dbuser'])) $errors[] = __('username');
  17. if (empty($_POST['v_password'])) $errors[] = __('password');
  18. if (empty($_POST['v_type'])) $errors[] = __('type');
  19. if (empty($_POST['v_host'])) $errors[] = __('host');
  20. if (empty($_POST['v_charset'])) $errors[] = __('charset');
  21. if (!empty($errors[0])) {
  22. foreach ($errors as $i => $error) {
  23. if ( $i == 0 ) {
  24. $error_msg = $error;
  25. } else {
  26. $error_msg = $error_msg.", ".$error;
  27. }
  28. }
  29. $_SESSION['error_msg'] = __('Field "%s" can not be blank.',$error_msg);
  30. }
  31. // Validate email
  32. if ((!empty($_POST['v_db_email'])) && (empty($_SESSION['error_msg']))) {
  33. if (!filter_var($_POST['v_db_email'], FILTER_VALIDATE_EMAIL)) {
  34. $_SESSION['error_msg'] = __('Please enter valid email address.');
  35. }
  36. }
  37. // Check password length
  38. if (empty($_SESSION['error_msg'])) {
  39. $pw_len = strlen($_POST['v_password']);
  40. if ($pw_len < 6 ) $_SESSION['error_msg'] = __('Password is too short.',$error_msg);
  41. }
  42. // Protect input
  43. $v_database = escapeshellarg($_POST['v_database']);
  44. $v_dbuser = escapeshellarg($_POST['v_dbuser']);
  45. $v_type = $_POST['v_type'];
  46. $v_charset = $_POST['v_charset'];
  47. $v_host = $_POST['v_host'];
  48. $v_db_email = $_POST['v_db_email'];
  49. // Add database
  50. if (empty($_SESSION['error_msg'])) {
  51. $v_type = escapeshellarg($_POST['v_type']);
  52. $v_charset = escapeshellarg($_POST['v_charset']);
  53. $v_host = escapeshellarg($_POST['v_host']);
  54. $v_password = tempnam("/tmp","vst");
  55. $fp = fopen($v_password, "w");
  56. fwrite($fp, $_POST['v_password']."\n");
  57. fclose($fp);
  58. exec (VESTA_CMD."v-add-database ".$user." ".$v_database." ".$v_dbuser." ".$v_password." ".$v_type." ".$v_host." ".$v_charset, $output, $return_var);
  59. check_return_code($return_var,$output);
  60. unset($output);
  61. unlink($v_password);
  62. $v_password = escapeshellarg($_POST['v_password']);
  63. $v_type = $_POST['v_type'];
  64. $v_host = $_POST['v_host'];
  65. $v_charset = $_POST['v_charset'];
  66. }
  67. // Get database manager url
  68. if (empty($_SESSION['error_msg'])) {
  69. list($http_host, $port) = explode(':', $_SERVER["HTTP_HOST"] . ":");
  70. if ($_POST['v_host'] != 'localhost' ) $http_host = $_POST['v_host'];
  71. if ($_POST['v_type'] == 'mysql') $db_admin = "phpMyAdmin";
  72. if ($_POST['v_type'] == 'mysql') $db_admin_link = "http://".$http_host."/phpmyadmin/";
  73. if (($_POST['v_type'] == 'mysql') && (!empty($_SESSION['DB_PMA_URL']))) $db_admin_link = $_SESSION['DB_PMA_URL'];
  74. if ($_POST['v_type'] == 'pgsql') $db_admin = "phpPgAdmin";
  75. if ($_POST['v_type'] == 'pgsql') $db_admin_link = "http://".$http_host."/phppgadmin/";
  76. if (($_POST['v_type'] == 'pgsql') && (!empty($_SESSION['DB_PGA_URL']))) $db_admin_link = $_SESSION['DB_PGA_URL'];
  77. }
  78. // Email login credentials
  79. if ((!empty($v_db_email)) && (empty($_SESSION['error_msg']))) {
  80. $to = $v_db_email;
  81. $subject = __("Database Credentials");
  82. $hostname = exec('hostname');
  83. $from = __('MAIL_FROM',$hostname);
  84. $mailtext = __('DATABASE_READY',$user."_".$_POST['v_database'],$user."_".$_POST['v_dbuser'],$_POST['v_password'],$db_admin_link);
  85. send_email($to, $subject, $mailtext, $from);
  86. }
  87. // Flush field values on success
  88. if (empty($_SESSION['error_msg'])) {
  89. $_SESSION['ok_msg'] = __('DATABASE_CREATED_OK',htmlentities($user)."_".htmlentities($_POST['v_database']),htmlentities($user)."_".htmlentities($_POST['v_database']));
  90. $_SESSION['ok_msg'] .= " / <a href=".$db_admin_link." target='_blank'>" . __('open %s',$db_admin) . "</a>";
  91. unset($v_database);
  92. unset($v_dbuser);
  93. unset($v_password);
  94. unset($v_type);
  95. unset($v_charset);
  96. }
  97. }
  98. // Get user email
  99. $v_db_email = $panel[$user]['CONTACT'];
  100. // List avaiable database types
  101. $db_types = split(',', $_SESSION['DB_SYSTEM']);
  102. // List available database servers
  103. $db_hosts = array();
  104. exec (VESTA_CMD."v-list-database-hosts 'json'", $output, $return_var);
  105. $db_hosts_tmp = json_decode(implode('', $output), true);
  106. $db_hosts = array_merge($db_hosts, $db_hosts_tmp);
  107. unset($db_hosts_tmp);
  108. unset($output);
  109. render_page($user, $TAB, 'add_db');
  110. // Flush session messages
  111. unset($_SESSION['error_msg']);
  112. unset($_SESSION['ok_msg']);