Index: install.php =================================================================== RCS file: /cvs/drupal/drupal/install.php,v retrieving revision 1.108 diff -u -p -r1.108 install.php --- install.php 20 Dec 2007 08:26:42 -0000 1.108 +++ install.php 27 Dec 2007 12:12:28 -0000 @@ -322,6 +322,44 @@ function install_settings_form(&$form_st '#description' => st('If more than one application will be sharing this database, enter a table prefix such as %prefix for your @drupal site here.', array('@drupal' => drupal_install_profile_name(), '%prefix' => $prefix)), ); + // Create database + $db_create = $_POST['db_create']; + + $form['create_database'] = array( + '#type' => 'fieldset', + '#title' => st('Create database'), + '#collapsible' => TRUE, + '#collapsed' => !$db_create, + '#description' => st("If database is not exists, you can create it. This is useful when you develop sites on your desktop."), + ); + + // Database master username + $form['create_database']['db_create'] = array( + '#type' => 'checkbox', + '#title' => st('Create new database'), + '#default_value' => $db_create, + '#description' => st("Check if you want to create new database"), + ); + + // Database master username + $form['create_database']['db_master_user'] = array( + '#type' => 'textfield', + '#title' => st('Database master username'), + '#default_value' => 'root', + '#size' => 45, + '#maxlength' => 45, + '#description' => st("Enter database master username, usually 'root'"), + ); + + // Database master password + $form['create_database']['db_master_pass'] = array( + '#type' => 'password', + '#title' => st('Database master password'), + '#size' => 45, + '#maxlength' => 45, + ); + + $form['save'] = array( '#type' => 'submit', '#value' => st('Save and continue'), @@ -341,7 +379,208 @@ function install_settings_form(&$form_st */ function install_settings_form_validate($form, &$form_state) { global $db_url; - _install_settings_form_validate($form_state['values']['db_prefix'], $form_state['values']['db_type'], $form_state['values']['db_user'], $form_state['values']['db_pass'], $form_state['values']['db_host'], $form_state['values']['db_port'], $form_state['values']['db_path'], $form_state['values']['settings_file'], $form_state, $form); + if(_install_settings_form_create_database_validate($form_state['values']['db_create'], $form_state['values']['db_type'], $form_state['values']['db_master_user'], $form_state['values']['db_master_pass'], $form_state['values']['db_user'], $form_state['values']['db_pass'], $form_state['values']['db_host'], $form_state['values']['db_port'], $form_state['values']['db_path'])) + _install_settings_form_validate($form_state['values']['db_prefix'], $form_state['values']['db_type'], $form_state['values']['db_user'], $form_state['values']['db_pass'], $form_state['values']['db_host'], $form_state['values']['db_port'], $form_state['values']['db_path'], $form_state['values']['settings_file'], $form_state, $form); +} + +/** + * Create database before future validation + */ +function _install_settings_form_create_database_validate($db_create, $db_type, $db_master_user, $db_master_pass, $db_user, $db_pass, $db_host, $db_port, $db_path) +{ + global $db_url; + + if ($db_create) { + + if (!$db_master_user) { + form_set_error('db_master_user', st('Master username is required')); + return; + } + + $function = '_install_create_db_' . $db_type; + if (function_exists($function)) + return $function($db_master_user, $db_master_pass, $db_user, $db_pass, $db_host, $db_port, $db_path); + else + form_set_error('', 'Create database feature is not suported for this database type yet.'); + + } + return true; +} + +function _install_create_db_mysql($db_master_user, $db_master_pass, $db_user, $db_pass, $db_host, $db_port, $db_path) +{ + // Verify database connection + $connect_host = ($db_host ? $db_host : 'localhost') . ($db_port ? ":$db_port" : ''); + $conn = @mysql_connect($connect_host, $db_master_user, $db_master_pass); + if (!$conn) { + form_set_error('', st('Failed to connect to your MySQL database server by master. MySQL reports the following message: ') . mysql_error()); + return false; + } + + // Get version of database + $result = @mysql_query("SHOW variables WHERE Variable_name='version'"); + if (!$result) { + form_set_error('', st('Failed get version of database. MySQL reports the following message: ' . mysql_error())); + mysql_close($conn); + return false; + } + + $row = mysql_fetch_array($result); + $version = $row['Value']; + if (!$version) { + form_set_error('', st('Failed to get version of database')); + mysql_close($conn); + return false; + } + + // Create databse + if (strcmp($version, "4.1") >= 0) + $utf8_suffix = " DEFAULT CHARACTER SET 'utf8' DEFAULT COLLATE 'utf8_general_ci'"; + $result = @mysql_query(sprintf("CREATE DATABASE IF NOT EXISTS `%s`" . $utf8_suffix, mysql_escape_string($db_path))); + if (!$result) { + form_set_error('', st('Failed to create database. MySQL reports the following message: ' . mysql_error())); + mysql_close($conn); + return false; + } + + // Grant permissions + $result = @mysql_query(sprintf("GRANT ALL ON `%s`.* TO `%s`@'%%' IDENTIFIED BY '%s'", + mysql_escape_string($db_path), mysql_escape_string($db_user), mysql_escape_string($db_pass))); + if (!$result) { + form_set_error('', st('Failed to grant permissions. MySQL reports the following message: ' . mysql_error())); + mysql_close($conn); + return false; + } + + mysql_close($conn); + return true; +} + +function _install_create_db_mysqli($db_master_user, $db_master_pass, $db_user, $db_pass, $db_host, $db_port, $db_path) +{ + // Verify database connection + $connect_host = ($db_host ? $db_host : 'localhost') . ($db_port ? ":$db_port" : ''); + $conn = @mysqli_connect($connect_host, $db_master_user, $db_master_pass); + if (!$conn) { + form_set_error('', st('Failed to connect to your MySQL database server by master. MySQL reports the following message: ') . mysqli_connect_error()); + return false; + } + + // Get version of database + $result = @mysqli_query($conn, "SHOW variables WHERE Variable_name='version'"); + if (!$result) { + form_set_error('', st('Failed get version of database. MySQL reports the following message: ' . mysqli_error($conn))); + mysqli_close($conn); + return false; + } + + $row = mysqli_fetch_array($result); + $version = $row['Value']; + if (!$version) { + form_set_error('', st('Failed to get version of database')); + mysqli_close($conn); + return false; + } + + // Create databse + if (strcmp($version, "4.1") >= 0) + $utf8_suffix = " DEFAULT CHARACTER SET 'utf8' DEFAULT COLLATE 'utf8_general_ci'"; + $result = @mysqli_query($conn, sprintf("CREATE DATABASE IF NOT EXISTS `%s`" . $utf8_suffix, mysqli_escape_string($db_path))); + if (!$result) { + form_set_error('', st('Failed to create database. MySQL reports the following message: ' . mysqli_error($conn))); + mysqli_close($conn); + return false; + } + + // Grant permissions + $result = @mysqli_query($conn, sprintf("GRANT ALL ON `%s`.* TO `%s`@'%%' IDENTIFIED BY '%s'", + mysqli_escape_string($db_path), mysqli_escape_string($db_user), mysqli_escape_string($db_pass))); + if (!$result){ + form_set_error('', st('Failed to grant permissions. MySQL reports the following message: ' . mysqli_error($conn))); + mysqli_close($conn); + return false; + } + + mysqli_close($conn); + return true; +} + +function _install_create_db_pgsql($db_master_user, $db_master_pass, $db_user, $db_pass, $db_host, $db_port, $db_path) +{ + $conn_string = ''; + + //Make connection string + if (!empty($db_master_user)) { + $conn_string .= ' user='. $db_master_user; + } + if (!empty($db_master_pass)) { + $conn_string .= ' password='. $db_master_pass; + } + if (!empty($db_host)) { + $conn_string .= ' host='. $db_host; + } + if (!empty($db_path)) { + $conn_string .= ' dbname=postgres'; + } + if (!empty($db_port)) { + $conn_string .= ' port='. $db_port; + } + + // Connect to the database. + $conn = @pg_connect($conn_string); + if (!$conn) { + drupal_set_message(st('Failed to connect to your PostgreSQL database server. PostgreSQL reports the following message: %error.For more help, see the Installation and upgrading handbook. If you are unsure what these terms mean you should probably contact your hosting provider.', array('%error' => 'Connection failed. See log file for failure reason')), 'error'); + return false; + } + + // Verify if database exists + $result = @pg_query($conn, sprintf("SELECT datname FROM pg_database WHERE datname='%s'", pg_escape_string($db_path))); + if (!$result) { + form_set_error('', st('Failed to select from pg_database. PostgreSQL reports the following message: ' . pg_last_error($conn))); + pg_close($conn); + return false; + } + + if (!pg_fetch_row($result)) + { + // Create database + $result = @pg_query($conn, sprintf("CREATE DATABASE %s WITH ENCODING 'UNICODE'", pg_escape_string($db_path))); + if (!$result) { + form_set_error('', st('Failed to create database. PostgreSQL reports the following message: ' . pg_last_error($conn))); + pg_close($conn); + return false; + } + } + + // Verify if user exists + $result = @pg_query($conn, sprintf("SELECT usename FROM pg_user WHERE usename='%s'", pg_escape_string($db_user))); + if (!$result) { + form_set_error('', st('Failed to select from pg_user. PostgreSQL reports the following message: ' . pg_last_error($conn))); + pg_close($conn); + return false; + } + + if (!pg_fetch_row($result)) + { + // Create user + $result = @pg_query($conn, sprintf("CREATE USER %s PASSWORD '%s'", pg_escape_string($db_user), pg_escape_string($db_pass))); + if (!$result) { + form_set_error('', st('Failed to create user. PostgreSQL reports the following message: ' . pg_last_error($conn))); + pg_close($conn); + return false; + } + } + + // Grant privileges + $result = @pg_query($conn, sprintf("GRANT ALL ON DATABASE %s TO %s", pg_escape_string($db_path), pg_escape_string($db_user))); + if (!$result) { + form_set_error('', st('Failed to grant privileges. PostgreSQL reports the following message: ' . pg_last_error($conn))); + pg_close($conn); + return false; + } + + pg_close($conn); + return true; } /**