index.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. $v_database = $_POST['v_database'];
  44. $v_dbuser = $_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_password = tempnam('/tmp', 'vst');
  52. $fp = fopen($v_password, 'w');
  53. fwrite($fp, $_POST['v_password']."\n");
  54. fclose($fp);
  55. v_exec('v-add-database', [$user, $v_database, $v_dbuser, $v_password, $v_type, $v_host, $v_charset]);
  56. unlink($v_password);
  57. $v_password = $_POST['v_password'];
  58. }
  59. // Get database manager url
  60. if (empty($_SESSION['error_msg'])) {
  61. list($http_host, $port) = explode(':', $_SERVER['HTTP_HOST'] . ':');
  62. if ($_POST['v_host'] != 'localhost' ) $http_host = $_POST['v_host'];
  63. if ($_POST['v_type'] == 'mysql') $db_admin = 'phpMyAdmin';
  64. if ($_POST['v_type'] == 'mysql') $db_admin_link = "http://$http_host/phpmyadmin/";
  65. if (($_POST['v_type'] == 'mysql') && (!empty($_SESSION['DB_PMA_URL']))) $db_admin_link = $_SESSION['DB_PMA_URL'];
  66. if ($_POST['v_type'] == 'pgsql') $db_admin = 'phpPgAdmin';
  67. if ($_POST['v_type'] == 'pgsql') $db_admin_link = "http://$http_host/phppgadmin/";
  68. if (($_POST['v_type'] == 'pgsql') && (!empty($_SESSION['DB_PGA_URL']))) $db_admin_link = $_SESSION['DB_PGA_URL'];
  69. }
  70. // Email login credentials
  71. if ((!empty($v_db_email)) && (empty($_SESSION['error_msg']))) {
  72. $to = $v_db_email;
  73. $subject = __("Database Credentials");
  74. $hostname = exec('hostname');
  75. $from = __('MAIL_FROM', $hostname);
  76. $mailtext = __('DATABASE_READY', $user.'_'.$_POST['v_database'], $user.'_'.$_POST['v_dbuser'], $_POST['v_password'], $db_admin_link);
  77. send_email($to, $subject, $mailtext, $from);
  78. }
  79. // Flush field values on success
  80. if (empty($_SESSION['error_msg'])) {
  81. $_SESSION['ok_msg'] = __('DATABASE_CREATED_OK', htmlentities($user.'_'.$_POST['v_database']), htmlentities($user.'_'.$_POST['v_database']));
  82. $_SESSION['ok_msg'] .= " / <a href=$db_admin_link target='_blank'>" . __('open %s', $db_admin) . '</a>';
  83. unset($v_database);
  84. unset($v_dbuser);
  85. unset($v_password);
  86. unset($v_type);
  87. unset($v_charset);
  88. }
  89. }
  90. // Header
  91. include($_SERVER['DOCUMENT_ROOT'].'/templates/header.html');
  92. // Panel
  93. top_panel($user,$TAB);
  94. // Get user email
  95. $v_db_email = $panel[$user]['CONTACT'];
  96. // List avaiable database types
  97. $db_types = explode(',', $_SESSION['DB_SYSTEM']);
  98. // List available database servers
  99. $db_hosts = array();
  100. foreach ($db_types as $db_type ) {
  101. v_exec('v-list-database-hosts', [$db_type, 'json'], false, $output);
  102. $db_hosts_tmp = json_decode($output, true);
  103. $db_hosts = array_merge($db_hosts, $db_hosts_tmp);
  104. unset($db_hosts_tmp);
  105. }
  106. // Display body
  107. include($_SERVER['DOCUMENT_ROOT'].'/templates/admin/add_db.html');
  108. // Flush session messages
  109. unset($_SESSION['error_msg']);
  110. unset($_SESSION['ok_msg']);
  111. // Footer
  112. include($_SERVER['DOCUMENT_ROOT'].'/templates/footer.html');
  113. ?>