index.php 5.0 KB

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