From 670fbb9a6c8d67020e2384e099a45caf55392ba8 Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Wed, 25 Jan 2012 14:38:58 +0100 Subject: [PATCH] Improving installation DB errors messages see #4219 --- main/inc/lib/database.lib.php | 2 +- main/install/index.php | 8 +++-- main/install/install.lib.php | 49 ++++++++++++++++++++++++++----- main/install/install_db.inc.php | 5 ++-- main/lang/english/install.inc.php | 4 +++ 5 files changed, 55 insertions(+), 13 deletions(-) diff --git a/main/inc/lib/database.lib.php b/main/inc/lib/database.lib.php index 61a5c1bca2..f3168262aa 100644 --- a/main/inc/lib/database.lib.php +++ b/main/inc/lib/database.lib.php @@ -1338,7 +1338,7 @@ class Database { } } return false; - } + } /* DEPRECATED METHODS diff --git a/main/install/index.php b/main/install/index.php index 9653ecc2da..413133df66 100644 --- a/main/install/index.php +++ b/main/install/index.php @@ -742,11 +742,12 @@ if ($_POST['step2']) { $perm_file = api_get_permissions_for_new_files(); if (empty($my_old_version)) { $my_old_version = '1.8.6.2'; } //we guess + $_configuration['main_database'] = $dbNameForm; //$urlAppendPath = get_config_param('urlAppend'); - error_log('Starting migration process from '.$my_old_version.' ('.time().')', 0); + error_log('Starting migration process from '.$my_old_version.' ('.time().')', 0); - if ($userPasswordCrypted == '1') { + if ($userPasswordCrypted == '1') { $userPasswordCrypted = 'md5'; } elseif ($userPasswordCrypted == '0') { $userPasswordCrypted = 'none'; @@ -822,7 +823,8 @@ if ($_POST['step2']) { default: break; } - } else { + } else { + set_file_folder_permissions(); database_server_connect(); diff --git a/main/install/install.lib.php b/main/install/install.lib.php index 26c4c9d662..2d6075e338 100755 --- a/main/install/install.lib.php +++ b/main/install/install.lib.php @@ -597,12 +597,19 @@ function database_server_connect() { @Database::query("set session sql_mode='';"); // Disabling special SQL modes (MySQL 5) } +/** + * Database exists for the MYSQL user + * @param type $database_name + * @return boolean + */ function database_exists($database_name) { + $select_database = Database::select_db($database_name); + $show_database = false; $result = @Database::query("SHOW DATABASES LIKE '".Database::escape_string($database_name)."' "); if (Database::num_rows($result)) { - return true; - } - return false; + $show_database = true; + } + return $select_database || $show_database; } /** @@ -618,7 +625,7 @@ function test_db_connect($dbHostForm, $dbUsernameForm, $dbPassForm, $singleDbFor $dbConnect = -1; //Checking user credentials if (@Database::connect(array('server' => $dbHostForm, 'username' => $dbUsernameForm, 'password' => $dbPassForm)) !== false) { - $check_user_can_create_databases = true; + //$check_user_can_create_databases = true; //Checking if single database exist /*if ($singleDbForm) { @@ -653,7 +660,7 @@ function test_db_connect($dbHostForm, $dbUsernameForm, $dbPassForm, $singleDbFor } else { $dbConnect = -1; } - return $dbConnect; //return "1"if no problems, "0" if, in case we can't create a new DB and "-1" if there is no connection. + return $dbConnect; //return 1, if no problems, "0" if, in case we can't create a new DB and "-1" if there is no connection. } /** @@ -1731,9 +1738,35 @@ function display_database_settings_form($installType, $dbHostForm, $dbUsernameFo '.get_lang('ADatabaseWithTheSameNameAlreadyExists').''; + } else { + if (!$dbConnect) { + $database_exists_text = '
'.sprintf(get_lang('UserXCantHaveAccessInTheDatabaseX'), $dbNameForm, $dbUsernameForm).'
'; + } else { + //Try to create the database + $user_can_create_databases = false; + $multipleDbCheck = @Database::query("CREATE DATABASE test_chamilo_connection"); + if ($multipleDbCheck !== false) { + $multipleDbCheck = @Database::query("DROP DATABASE IF EXISTS test_chamilo_connection"); + $user_can_create_databases = true; + } + + if ($user_can_create_databases) { + $database_exists_text = '
'.sprintf(get_lang('DatabaseXWillBeCreated'), $dbNameForm, $dbUsernameForm).'
'; + } else { + $dbConnect = 0; + $database_exists_text = '
'.sprintf(get_lang('DatabaseXCantBeCreatedUserXDoestHaveEnoughtPermissions'), $dbNameForm, $dbUsernameForm).'
'; + } + } + } + if ($dbConnect == 1): ?> +
Database host:
Database server version:
@@ -1742,13 +1775,15 @@ function display_database_settings_form($installType, $dbHostForm, $dbUsernameFo
- + +
+
Database error:
'; ?> -
+
diff --git a/main/install/install_db.inc.php b/main/install/install_db.inc.php index 97c3174e96..4bb63c030b 100644 --- a/main/install/install_db.inc.php +++ b/main/install/install_db.inc.php @@ -132,6 +132,7 @@ if (!defined('CLI_INSTALLATION')) { /** * Creating the tables of the main database */ + Database::select_db($mysqlMainDb) or die(Database::error()); $installation_settings['{ORGANISATIONNAME}'] = $institutionForm; @@ -149,13 +150,13 @@ $installation_settings['{ADMINPHONE}'] = $adminPhoneForm; $installation_settings['{PLATFORM_AUTH_SOURCE}'] = PLATFORM_AUTH_SOURCE; $installation_settings['{ADMINLANGUAGE}'] = $languageForm; $installation_settings['{HASHFUNCTIONMODE}'] = $encryptPassForm; -load_main_database($installation_settings); +load_main_database($installation_settings); //Adds the c_XXX courses tables see #3910 require_once api_get_path(LIBRARY_PATH).'add_course.lib.inc.php'; -update_Db_course(); +update_Db_course(); /** * Creating the tables of the tracking database diff --git a/main/lang/english/install.inc.php b/main/lang/english/install.inc.php index f9484f15b8..8d8bdec3d8 100644 --- a/main/lang/english/install.inc.php +++ b/main/lang/english/install.inc.php @@ -125,4 +125,8 @@ $PHPVersionError = "Your PHP version does not match the requirements for this so $ExtensionSessionsNotAvailable = "Sessions extension not available"; $ExtensionZlibNotAvailable = "Zlib extension not available"; $ExtensionPCRENotAvailable = "PCRE extension not available"; +$DatabaseXWillBeCreated = "Database %s will be created"; +$ADatabaseWithTheSameNameAlreadyExistsGoOnOrCheckTheDatabase = "A database with the same name already exists. The contents in the database will be lost."; +$UserXCantHaveAccessInTheDatabaseX = "User %s can't have access in the database %s"; +$DatabaseXCantBeCreatedUserXDoestHaveEnoughtPermissions = "Database %s can't be created user %s doesn't have enought permissions"; ?> \ No newline at end of file