diff --git a/Sources/Autoloader.php b/Sources/Autoloader.php index 0a9ffacf16..7587143848 100644 --- a/Sources/Autoloader.php +++ b/Sources/Autoloader.php @@ -41,7 +41,7 @@ } // Do any third-party scripts want in on the fun? - if (class_exists('SMF\\Config', false) && $hook_value !== (Config::$modSettings['integrate_autoload'] ?? '')) { + if (!defined('SMF_INSTALLING') && class_exists('SMF\\Config', false) && $hook_value !== (Config::$modSettings['integrate_autoload'] ?? '')) { if (!class_exists('SMF\\IntegrationHook', false) && is_file($sourcedir . '/IntegrationHook.php')) { require_once $sourcedir . '/IntegrationHook.php'; } diff --git a/Sources/Db/APIs/MySQL.php b/Sources/Db/APIs/MySQL.php index faa524c072..d3f7d473a0 100644 --- a/Sources/Db/APIs/MySQL.php +++ b/Sources/Db/APIs/MySQL.php @@ -1984,14 +1984,19 @@ protected function __construct(array $options = []) $this->prefixReservedTables(); } - $this->get_version(); - $this->supports_pcre = version_compare($this->version, strpos($this->version, 'MariaDB') !== false ? '10.0.5' : '8.0.4', '>='); + // At this point, if we don't have a connection, nothing else can be done. + if (empty($this->connection)) { + return; + } // For backward compatibility. if (!is_object(self::$db_connection)) { self::$db_connection = $this->connection; } + $this->get_version(); + $this->supports_pcre = version_compare($this->version, strpos($this->version, 'MariaDB') !== false ? '10.0.5' : '8.0.4', '>='); + // Ensure database has UTF-8 as its default input charset. $this->query( '', diff --git a/Sources/Db/APIs/PostgreSQL.php b/Sources/Db/APIs/PostgreSQL.php index 3d5493c690..c668f5d4fa 100644 --- a/Sources/Db/APIs/PostgreSQL.php +++ b/Sources/Db/APIs/PostgreSQL.php @@ -2112,6 +2112,11 @@ protected function __construct(array $options = []) ErrorHandler::displayDbError(); } + // At this point, if we don't have a connection, nothing else can be done. + if (empty($this->connection)) { + return; + } + // For backward compatibility. if (!is_object(self::$db_connection)) { self::$db_connection = $this->connection; diff --git a/Sources/Db/DatabaseApi.php b/Sources/Db/DatabaseApi.php index 53fc3504b4..752f69f63a 100644 --- a/Sources/Db/DatabaseApi.php +++ b/Sources/Db/DatabaseApi.php @@ -326,7 +326,27 @@ final public static function load(array $options = []) } // Figure out what type of database we are using. - switch (!empty(Config::$db_type) ? strtolower(Config::$db_type) : 'mysql') { + $class = self::getClass(!empty(Config::$db_type) ? strtolower(Config::$db_type) : 'mysql'); + + if (!class_exists(__NAMESPACE__ . '\\APIs\\' . $class)) { + ErrorHandler::displayDbError(); + } + + $class = __NAMESPACE__ . '\\APIs\\' . $class; + self::$db = new $class($options); + + // Double check that we found what we expected. + if (!(self::$db instanceof DatabaseApi)) { + unset(self::$db); + ErrorHandler::displayDbError(); + } + + return self::$db; + } + + public static function getClass($db_type) + { + switch (strtolower($db_type)) { // PostgreSQL is known by many names. case 'postgresql': case 'postgres': @@ -335,14 +355,14 @@ final public static function load(array $options = []) $class = POSTGRE_TITLE; break; - // MySQL and its forks. + // MySQL and its forks. case 'mysql': case 'mariadb': case 'percona': $class = MYSQL_TITLE; break; - // Something else? + // Something else? default: $class = ucwords(Config::$db_type); @@ -354,20 +374,7 @@ final public static function load(array $options = []) break; } - if (!class_exists(__NAMESPACE__ . '\\APIs\\' . $class)) { - ErrorHandler::displayDbError(); - } - - $class = __NAMESPACE__ . '\\APIs\\' . $class; - self::$db = new $class($options); - - // Double check that we found what we expected. - if (!(self::$db instanceof DatabaseApi)) { - unset(self::$db); - ErrorHandler::displayDbError(); - } - - return self::$db; + return $class; } /** diff --git a/other/install.php b/other/install.php index b603e15cfb..e43c7b262c 100644 --- a/other/install.php +++ b/other/install.php @@ -254,7 +254,7 @@ function initialize_inputs() foreach ($databases as $key => $dummy) { $type = ($key == 'mysqli') ? 'mysql' : $key; - $ftp->unlink('install_' . DB_SCRIPT_VERSION . '_' . $type . '.sql'); + $ftp->unlink('install_' . DB_SCRIPT_VERSION . '_' . Db::getClass($type) . '.sql'); } $ftp->close(); @@ -268,7 +268,7 @@ function initialize_inputs() foreach ($databases as $key => $dummy) { $type = ($key == 'mysqli') ? 'mysql' : $key; - @unlink(Config::$boarddir . '/install_' . DB_SCRIPT_VERSION . '_' . $type . '.sql'); + @unlink(Config::$boarddir . '/install_' . DB_SCRIPT_VERSION . '_' . Db::getClass($type) . '.sql'); } } @@ -479,11 +479,11 @@ function Welcome() if ($db['supported']) { $type = ($key == 'mysqli') ? 'mysql' : $key; - if (!file_exists(Config::$boarddir . '/install_' . DB_SCRIPT_VERSION . '_' . $type . '.sql')) + if (!file_exists(Config::$boarddir . '/install_' . DB_SCRIPT_VERSION . '_' . Db::getClass($type) . '.sql')) { $databases[$key]['supported'] = false; $notFoundSQLFile = true; - Lang::$txt['error_db_script_missing'] = sprintf(Lang::$txt['error_db_script_missing'], 'install_' . DB_SCRIPT_VERSION . '_' . $type . '.sql'); + Lang::$txt['error_db_script_missing'] = sprintf(Lang::$txt['error_db_script_missing'], 'install_' . DB_SCRIPT_VERSION . '_' . Db::getClass($type) . '.sql'); } else $incontext['supported_databases'][] = $db; @@ -868,9 +868,9 @@ function DatabaseSettings() Config::load(); // Better find the database file! - if (!file_exists(Config::$sourcedir . '/Db/APIs/' . Config::$db_type . '.php')) + if (!file_exists(Config::$sourcedir . '/Db/APIs/' . Db::getClass(Config::$db_type) . '.php')) { - $incontext['error'] = sprintf(Lang::$txt['error_db_file'], 'Db/APIs/' . Config::$db_type . '.php'); + $incontext['error'] = sprintf(Lang::$txt['error_db_file'], 'Db/APIs/' . Db::getClass(Config::$db_type) . '.php'); return false; } @@ -1222,7 +1222,7 @@ function DatabasePopulation() // Read in the SQL. Turn this on and that off... internationalize... etc. $type = (Config::$db_type == 'mysqli' ? 'mysql' : Config::$db_type); - $sql_lines = explode("\n", strtr(implode(' ', file(Config::$boarddir . '/install_' . DB_SCRIPT_VERSION . '_' . $type . '.sql')), $replaces)); + $sql_lines = explode("\n", strtr(implode(' ', file(Config::$boarddir . '/install_' . DB_SCRIPT_VERSION . '_' . Db::getClass($type) . '.sql')), $replaces)); // Execute the SQL. $current_statement = ''; diff --git a/other/install_2-1_mysql.sql b/other/install_2-1_MySQL.sql similarity index 100% rename from other/install_2-1_mysql.sql rename to other/install_2-1_MySQL.sql diff --git a/other/install_2-1_postgresql.sql b/other/install_2-1_PostgreSQL.sql similarity index 100% rename from other/install_2-1_postgresql.sql rename to other/install_2-1_PostgreSQL.sql diff --git a/other/upgrade.php b/other/upgrade.php index 2defe17506..30a0b8e58e 100644 --- a/other/upgrade.php +++ b/other/upgrade.php @@ -733,7 +733,7 @@ function loadEssentialData() require_once(Config::$sourcedir . '/Autoloader.php'); - if (class_exists('SMF\\Db\\APIs\\' . Config::$db_type)) + if (class_exists('SMF\\Db\\APIs\\' . Db::getClass(Config::$db_type))) { // Make the connection... if (empty(Db::$db_connection)) @@ -782,10 +782,10 @@ function loadEssentialData() Db::$db->free_result($request); } else - return throw_error(sprintf(Lang::$txt['error_sourcefile_missing'], 'Db/APIs/' . Config::$db_type . '.php')); + return die(sprintf(Lang::$txt['error_sourcefile_missing'], 'Db/APIs/' . Db::getClass(Config::$db_type) . '.php')); // If they don't have the file, they're going to get a warning anyway so we won't need to clean request vars. - if (class_exists('SMF\\QueryString', false) && php_version_check()) + if (class_exists('SMF\\QueryString') && php_version_check()) { QueryString::cleanRequest(); } @@ -815,8 +815,8 @@ function initialize_inputs() // And the extra little files ;). deleteFile(dirname(__FILE__) . '/upgrade_1-0.sql'); deleteFile(dirname(__FILE__) . '/upgrade_1-1.sql'); - deleteFile(dirname(__FILE__) . '/upgrade_2-0_' . Config::$db_type . '.sql'); - deleteFile(dirname(__FILE__) . '/upgrade_2-1_' . Config::$db_type . '.sql'); + deleteFile(dirname(__FILE__) . '/upgrade_2-0_' . Db::getClass(Config::$db_type) . '.sql'); + deleteFile(dirname(__FILE__) . '/upgrade_2-1_' . Db::getClass(Config::$db_type) . '.sql'); deleteFile(dirname(__FILE__) . '/upgrade-helper.php'); $dh = opendir(dirname(__FILE__)); @@ -873,12 +873,12 @@ function WelcomeLogin() // Check for some key files - one template, one language, and a new and an old source file. $check = @file_exists(Config::$modSettings['theme_dir'] . '/index.template.php') && @file_exists(Config::$sourcedir . '/QueryString.php') - && @file_exists(Config::$sourcedir . '/Db/APIs/' . Config::$db_type . '.php') - && @file_exists(dirname(__FILE__) . '/upgrade_2-1_' . Config::$db_type . '.sql'); + && @file_exists(Config::$sourcedir . '/Db/APIs/' . Db::getClass(Config::$db_type) . '.php') + && @file_exists(dirname(__FILE__) . '/upgrade_2-1_' . Db::getClass(Config::$db_type) . '.sql'); // Need legacy scripts? if (!isset(Config::$modSettings['smfVersion']) || Config::$modSettings['smfVersion'] < 2.1) - $check &= @file_exists(dirname(__FILE__) . '/upgrade_2-0_' . Config::$db_type . '.sql'); + $check &= @file_exists(dirname(__FILE__) . '/upgrade_2-0_' . Db::getClass(Config::$db_type) . '.sql'); if (!isset(Config::$modSettings['smfVersion']) || Config::$modSettings['smfVersion'] < 2.0) $check &= @file_exists(dirname(__FILE__) . '/upgrade_1-1.sql'); if (!isset(Config::$modSettings['smfVersion']) || Config::$modSettings['smfVersion'] < 1.1) @@ -1677,8 +1677,8 @@ function DatabaseChanges() $files = array( array('upgrade_1-0.sql', '1.1', '1.1 RC0', false), array('upgrade_1-1.sql', '2.0', '2.0 a', false), - array('upgrade_2-0_' . Config::$db_type . '.sql', '2.1', '2.1 dev0', false), - array('upgrade_2-1_' . Config::$db_type . '.sql', '3.0', SMF_VERSION, true), + array('upgrade_2-0_' . Db::getClass(Config::$db_type) . '.sql', '2.1', '2.1 dev0', false), + array('upgrade_2-1_' . Db::getClass(Config::$db_type) . '.sql', '3.0', SMF_VERSION, true), ); // How many files are there in total? @@ -4098,13 +4098,13 @@ function template_welcome_message() { $ago = time() - $upcontext['started']; $ago_hours = floor($ago / 3600); - $ago_minutes = intval(($ago / 60) % 60); + $ago_minutes = (int) (((int) ($ago / 60)) % 60); $ago_seconds = intval($ago % 60); $agoTxt = $ago < 60 ? 'upgrade_time_s' : ($ago < 3600 ? 'upgrade_time_ms' : 'upgrade_time_hms'); $updated = time() - $upcontext['updated']; $updated_hours = floor($updated / 3600); - $updated_minutes = intval(($updated / 60) % 60); + $updated_minutes = intval(((int) ($updated / 60)) % 60); $updated_seconds = intval($updated % 60); $updatedTxt = $updated < 60 ? 'upgrade_time_updated_s' : ($updated < 3600 ? 'upgrade_time_updated_hm' : 'upgrade_time_updated_hms'); diff --git a/other/upgrade_2-0_mysql.sql b/other/upgrade_2-0_MySQL.sql similarity index 100% rename from other/upgrade_2-0_mysql.sql rename to other/upgrade_2-0_MySQL.sql diff --git a/other/upgrade_2-0_postgresql.sql b/other/upgrade_2-0_PostgreSQL.sql similarity index 100% rename from other/upgrade_2-0_postgresql.sql rename to other/upgrade_2-0_PostgreSQL.sql diff --git a/other/upgrade_2-1_mysql.sql b/other/upgrade_2-1_MySQL.sql similarity index 99% rename from other/upgrade_2-1_mysql.sql rename to other/upgrade_2-1_MySQL.sql index 1d35c1c0b6..c7675628e5 100644 --- a/other/upgrade_2-1_mysql.sql +++ b/other/upgrade_2-1_MySQL.sql @@ -321,7 +321,7 @@ INSERT INTO {$db_prefix}settings (variable, value) VALUES ('defaultMaxListItems' Db::$db->insert('replace', '{db_prefix}settings', array('variable' => 'string', 'value' => 'string'), - array('bcrypt_hash_cost', Security::hashVerifyPassword()), + array('bcrypt_hash_cost', Security::hashBenchmark()), array('variable') ); ---} diff --git a/other/upgrade_2-1_postgresql.sql b/other/upgrade_2-1_PostgreSQL.sql similarity index 99% rename from other/upgrade_2-1_postgresql.sql rename to other/upgrade_2-1_PostgreSQL.sql index 3285e2fa2e..e40d913292 100644 --- a/other/upgrade_2-1_postgresql.sql +++ b/other/upgrade_2-1_PostgreSQL.sql @@ -2089,7 +2089,7 @@ WHERE variable IN ('show_board_desc', 'display_quick_reply', 'show_mark_read', ' Db::$db->insert('replace', '{db_prefix}settings', array('variable' => 'string', 'value' => 'string'), - array('bcrypt_hash_cost', Security::hashVerifyPassword()), + array('bcrypt_hash_cost', Security::hashBenchmark()), array('variable') ); ---}