index.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. // Header
  9. include($_SERVER['DOCUMENT_ROOT'].'/templates/header.html');
  10. // Panel
  11. top_panel($user,$TAB);
  12. $v_db_email = $panel[$user]['CONTACT'];
  13. if (!empty($_POST['ok'])) {
  14. // Check input
  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. // Check for errors
  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'])) {
  34. if (!filter_var($_POST['v_db_email'], FILTER_VALIDATE_EMAIL)) {
  35. $_SESSION['error_msg'] = __('Please enter valid email address.');
  36. }
  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. // Check password length
  47. if (empty($_SESSION['error_msg'])) {
  48. $pw_len = strlen($_POST['v_password']);
  49. if ($pw_len < 6 ) $_SESSION['error_msg'] = __('Password is too short.',$error_msg);
  50. }
  51. if (empty($_SESSION['error_msg'])) {
  52. // Add Database
  53. $v_type = escapeshellarg($_POST['v_type']);
  54. $v_charset = escapeshellarg($_POST['v_charset']);
  55. $v_host = escapeshellarg($_POST['v_host']);
  56. exec (VESTA_CMD."v-add-database ".$user." ".$v_database." ".$v_dbuser." ".$v_password." ".$v_type." ".$v_host." ".$v_charset, $output, $return_var);
  57. $v_type = $_POST['v_type'];
  58. $v_host = $_POST['v_host'];
  59. $v_charset = $_POST['v_charset'];
  60. check_return_code($return_var,$output);
  61. unset($output);
  62. if ((!empty($v_db_email)) && (empty($_SESSION['error_msg']))) {
  63. list($http_host, $port) = explode(':', $_SERVER["HTTP_HOST"]);
  64. if ($_POST['v_type'] == 'mysql') $db_admin_link = "http://".$http_host."/phpmyadmin/";
  65. if ($_POST['v_type'] == 'pgsql') $db_admin_link = "http://".$http_host."/phppgadmin/";
  66. $to = $v_db_email;
  67. $subject = __("Database Credentials");
  68. $hostname = exec('hostname');
  69. $from = __('MAIL_FROM',$hostname);
  70. $mailtext = __('DATABASE_READY',$user."_".$_POST['v_database'],$user."_".$_POST['v_dbuser'],$_POST['v_password'],$db_admin_link);
  71. send_email($to, $subject, $mailtext, $from);
  72. }
  73. if (empty($_SESSION['error_msg'])) {
  74. exec (VESTA_CMD."v-list-sys-config json", $output, $return_var);
  75. $sys = json_decode(implode('', $output), true);
  76. unset($output);
  77. list($http_host, $port) = explode(':', $_SERVER["HTTP_HOST"] . ":");
  78. if ($_POST['v_host'] != 'localhost' ) $http_host = $_POST['v_host'];
  79. if ($_POST['v_type'] == 'mysql') $db_admin = "phpMyAdmin";
  80. if ($_POST['v_type'] == 'mysql') $db_admin_link = "http://".$http_host."/phpmyadmin/";
  81. if (($_POST['v_type'] == 'mysql') && (!empty($sys['config']['DB_PMA_URL']))) $db_admin_link = $sys['config']['DB_PMA_URL'];
  82. if ($_POST['v_type'] == 'pgsql') $db_admin = "phpPgAdmin";
  83. if ($_POST['v_type'] == 'pgsql') $db_admin_link = "http://".$http_host."/phppgadmin/";
  84. if (($_POST['v_type'] == 'pgsql') && (!empty($sys['config']['DB_PGA_URL']))) $db_admin_link = $sys['config']['DB_PGA_URL'];
  85. $_SESSION['ok_msg'] = __('DATABASE_CREATED_OK',$user."_".$_POST['v_database'],$user."_".$_POST['v_database']);
  86. $_SESSION['ok_msg'] .= " / <a href=".$db_admin_link." target='_blank'>" . __('open %s',$db_admin) . "</a>";
  87. unset($v_database);
  88. unset($v_dbuser);
  89. unset($v_password);
  90. unset($v_type);
  91. unset($v_charset);
  92. }
  93. }
  94. }
  95. exec (VESTA_CMD."v-list-database-types 'json'", $output, $return_var);
  96. $db_types = json_decode(implode('', $output), true);
  97. unset($output);
  98. $db_hosts = array();
  99. foreach ($db_types as $db_type ) {
  100. exec (VESTA_CMD."v-list-database-hosts ".$db_type." 'json'", $output, $return_var);
  101. $db_hosts_tmp = json_decode(implode('', $output), true);
  102. $db_hosts = array_merge($db_hosts, $db_hosts_tmp);
  103. unset($db_hosts_tmp);
  104. unset($output);
  105. }
  106. include($_SERVER['DOCUMENT_ROOT'].'/templates/admin/add_db.html');
  107. unset($_SESSION['error_msg']);
  108. unset($_SESSION['ok_msg']);
  109. // Footer
  110. include($_SERVER['DOCUMENT_ROOT'].'/templates/footer.html');
  111. ?>