index.php 5.1 KB

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