diff --git a/.docker/php/entrypoint.sh b/.docker/php/entrypoint.sh index 05d37fe70bb..631b3b58472 100644 --- a/.docker/php/entrypoint.sh +++ b/.docker/php/entrypoint.sh @@ -128,6 +128,18 @@ chown -R www-data:www-data \ "$BOARD_DIR/Settings.php" \ "$BOARD_DIR/Settings_bak.php" 2>/dev/null || true +# ---------------------------------------------------------------- manifest +# The installer and upgrader both try to check the manifest in order to +# verify that all required files are present. However, the manifest is +# intentionally not included in development versions of SMF. Although the +# installer and upgrader will silently ignore a missing manifest file in +# development versions, creating one on the fly harms nothing and allows +# more complete testing of the install and upgrade processes. +if [ ! -f "$BOARD_DIR/Sources/Maintenance/manifest.php" ]; then + log 'building manifest.php' + php -f "$BOARD_DIR/other/update_manifest.php" &>/dev/null || true +fi + log 'ready' exec "$@" diff --git a/.gitignore b/.gitignore index d68f074d24d..283e7e49228 100644 --- a/.gitignore +++ b/.gitignore @@ -29,9 +29,6 @@ Themes/default/scripts/autolinker.js custom_avatar/ !custom_avatar/index.php !custom_avatar/blank.png -Sources/Unicode/*.php -!Sources/Unicode/index.php -!Sources/Unicode/Utf8String.php logs # Compiled source # diff --git a/Languages/en_US/Maintenance.php b/Languages/en_US/Maintenance.php index a12ad693d89..2ef5b54c2eb 100644 --- a/Languages/en_US/Maintenance.php +++ b/Languages/en_US/Maintenance.php @@ -59,6 +59,8 @@ $txt['query_unsuccessful'] = 'Unsuccessful!'; $txt['query_failed'] = 'This query: {QUERY_STRING} Caused the error: {QUERY_ERROR}'; +$txt['error_missing_files'] = 'Some required files are either missing or have unexpected content.

Please make sure you uploaded all of the files included in the SMF package and then try again.'; +$txt['error_missing_files_debug'] = 'The following files are either missing or have unexpected content: {missing_files}

Please make sure you uploaded all of the files included in the SMF package and then try again.'; // Progress bars and steps. $txt['maintenance_progress'] = 'Progress'; @@ -126,7 +128,6 @@ $txt['error_db_missing'] = 'The installer was unable to detect any database support in PHP. Please ask your host to ensure that PHP was compiled with the desired database, or that the proper extension is being loaded.'; $txt['error_db_type_unknown'] = '“{db_type}” is not a database type this server supports. Supported types: {supported}.'; $txt['error_session_missing'] = 'The installer was unable to detect sessions support in your server’s installation of PHP. Please ask your host to ensure that PHP was compiled with session support (which in fact is the PHP default, meaning your host currently has explicitly disabled it).'; -$txt['error_missing_files'] = 'Unable to find crucial installation files in the directory of this script!

Please make sure you uploaded the entire installation package, including the sql file, and then try again.'; $txt['error_session_save_path'] = 'Please inform your host that the session.save_path specified in php.ini is not valid! It needs to be changed to a directory that exists and is writable by the user PHP is running under.
'; $txt['error_mod_security'] = 'The installer has detected the mod_security module is installed on your web server. Mod_security will block submitted forms even before SMF gets a say in anything. SMF has a built-in security scanner that will work more effectively than mod_security and that will not block submitted forms.

More information about disabling mod_security'; $txt['error_mod_security_no_write'] = 'The installer has detected the mod_security module is installed on your web server. Mod_security will block submitted forms even before SMF gets a say in anything. SMF has a built-in security scanner that will work more effectively than mod_security and that will not block submitted forms.

More information about disabling mod_security

Alternatively, you may wish to use your ftp client to chmod .htaccess in the forum directory to be writable (777), and then refresh this page.'; @@ -447,7 +448,6 @@ // Upgrade error messages $txt['upgrade_unsuccessful'] = 'Unsuccessful!'; -$txt['error_upgrade_files_missing'] = 'The upgrader was unable to find some crucial files.

Please make sure you uploaded all of the files included in the package, including the Themes, Sources, and other directories.'; $txt['error_upgrade_old_files'] = 'The upgrader found some old or outdated files.

Please make certain you uploaded the new versions of all the files included in the package.'; $txt['error_cache_not_found'] = 'The cache directory could not be found.

Please make sure you have a directory called "cache" in your forum directory before continuing.'; $txt['error_agreement_not_writable'] = 'The upgrader was unable to obtain write access to agreement.txt.

If you are using a linux or unix based server, please ensure that the file is chmod’d to 777, or if it does not exist that the directory this upgrader is in is 777.
If your server is running Windows, please ensure that the internet guest account has the proper permissions on it or its directory.'; diff --git a/Sources/Actions/ViewQuery.php b/Sources/Actions/ViewQuery.php index b3edf092643..6c7dfae85e5 100644 --- a/Sources/Actions/ViewQuery.php +++ b/Sources/Actions/ViewQuery.php @@ -205,7 +205,7 @@ public function execute(): void $result = Db::$db->query('ANALYZE FORMAT=JSON ' . $select); } elseif ($vendor == 'PostgreSQL') { $result = Db::$db->query('(ANALYZE, FORMAT JSON) ' . $select); - } elseif ($vendor == 'MySQL' && version_compare($version, '8.3.0', '>=')) { + } elseif ($vendor == 'MySQL' && version_compare(Utils::standardizeVersionString($version), '8.3.0', '>=')) { Db::$db->query('SET explain_json_format_version=2'); $result = Db::$db->query('EXPLAIN ANALYZE FORMAT=JSON ' . $select); } else { diff --git a/Sources/Config.php b/Sources/Config.php index 0014252111a..b5d1e7e0f53 100644 --- a/Sources/Config.php +++ b/Sources/Config.php @@ -1135,8 +1135,8 @@ public static function reloadModSettings(): void ) && !empty(self::$modSettings['smfVersion']) && version_compare( - strtolower(strtr(self::$modSettings['smfVersion'], [' ' => '.'])), - strtolower(strtr(SMF_VERSION, [' ' => '.'])), + strtolower(Utils::standardizeVersionString(self::$modSettings['smfVersion'])), + strtolower(Utils::standardizeVersionString(SMF_VERSION)), '!=', ) ) { diff --git a/Sources/Db/APIs/MySQL.php b/Sources/Db/APIs/MySQL.php index 698b50836d6..9ff45ed598c 100644 --- a/Sources/Db/APIs/MySQL.php +++ b/Sources/Db/APIs/MySQL.php @@ -867,7 +867,7 @@ public function cte_support(): bool $min_version = str_contains(strtolower($this->version), 'mariadb') ? '10.2.2' : '8.0.1'; - $this->supports_cte = version_compare($this->version, $min_version, '>='); + $this->supports_cte = version_compare(Utils::standardizeVersionString($this->version), $min_version, '>='); return $this->supports_cte; } diff --git a/Sources/Db/Schema/Table.php b/Sources/Db/Schema/Table.php index dfb4278817c..b391c970819 100644 --- a/Sources/Db/Schema/Table.php +++ b/Sources/Db/Schema/Table.php @@ -18,6 +18,7 @@ use SMF\Config; use SMF\Db\DatabaseApi as Db; use SMF\Lang; +use SMF\Utils; /** * Represents a database table. @@ -67,7 +68,7 @@ abstract class Table if ( !isset($this->default_charset) && preg_match('/\\\\v(\d+_\d+)\\\\/', $this::class, $matches) - && version_compare(strtr($matches[1], '_', '.'), '3.0', '>=') + && version_compare(Utils::standardizeVersionString($matches[1]), '3.0', '>=') ) { $this->default_charset = Db::$db->title === MYSQL_TITLE ? 'utf8mb4' : 'utf8'; } diff --git a/Sources/Db/Schema/v3_0/Initialize/PostgreSQL.php b/Sources/Db/Schema/v3_0/Initialize/PostgreSQL.php index 5f4b9fd1cc6..61ee45f9904 100644 --- a/Sources/Db/Schema/v3_0/Initialize/PostgreSQL.php +++ b/Sources/Db/Schema/v3_0/Initialize/PostgreSQL.php @@ -15,6 +15,8 @@ namespace SMF\Db\Schema\v3_0\Initialize; +use SMF\Utils; + class PostgreSQL extends Base { /**************** @@ -33,7 +35,7 @@ public function functions(): array // PostgreSQL changed how we can do some aggregation functions in 14 and beyond // Idea for the GROUP_CONCAT from: https://dba.stackexchange.com/questions/24984/is-it-possible-to-wrap-aggregate-functions-in-postgres - if (version_compare($this->version, '14', '>')) { + if (version_compare(Utils::standardizeVersionString($this->version), '14', '>')) { // This isn't even my final form. $functions[] = ' CREATE OR REPLACE FUNCTION GROUP_CONCAT_FINAL(anycompatiblearray) diff --git a/Sources/MailAgent/MailAgent.php b/Sources/MailAgent/MailAgent.php index 6a4c0c1f9e3..90792dbfe26 100644 --- a/Sources/MailAgent/MailAgent.php +++ b/Sources/MailAgent/MailAgent.php @@ -151,7 +151,18 @@ public function agentSettings(array &$config_vars): void {} */ public function isCompatible(string $smfVersion): bool { - return version_compare($this->min_smf_version, $smfVersion, '<=') && version_compare($smfVersion, $this->version_compatible, '>='); + return ( + version_compare( + Utils::standardizeVersionString($this->min_smf_version), + Utils::standardizeVersionString($smfVersion), + '<=', + ) + && version_compare( + Utils::standardizeVersionString($smfVersion), + Utils::standardizeVersionString($this->version_compatible), + '>=', + ) + ); } /** diff --git a/Sources/Maintenance/Maintenance.php b/Sources/Maintenance/Maintenance.php index ae1ed8a67b9..e1fdc6c199e 100644 --- a/Sources/Maintenance/Maintenance.php +++ b/Sources/Maintenance/Maintenance.php @@ -426,6 +426,217 @@ function ($matches) { return $boarddir ?? \dirname(__DIR__); } + /** + * Checks the files listed in manifest.php and returns a list of any that are + * missing or have incorrect content. + * + * @param string $start_version The version of SMF we are starting from. + * For upgrades, this should be the version of SMF that was in use before + * the upgrade began. For new installs, this should be SMF_VERSION. + * @param bool $check_content Whether to validate the content of the files. + * @return array A list of missing or corrupt files. + */ + final public static function checkManifest(string $start_version, bool $check_content): array + { + $boarddir = self::getBaseDir(); + + $missing_or_corrupt_files = []; + + // Does the manifest file exist? + if (!is_file(__DIR__ . DIRECTORY_SEPARATOR . 'manifest.php')) { + // Development versions normally don't include a manifest. + // This is by design and receives special handling here. + if (str_ends_with(SMF_VERSION, '-dev')) { + // Just check the migrations and cleanups and call it done. + foreach (Tools\Upgrade::VERSION_MAP as $version => $ns) { + if ( + version_compare( + Utils::standardizeVersionString($start_version), + $version, + '>', + ) + ) { + continue; + } + + foreach (Tools\Upgrade::MIGRATIONS[$ns] as $class) { + if (!class_exists($class)) { + $missing_or_corrupt_files[] = preg_replace( + ['/^SMF\b/', '/\\\\/', '/$/'], + ['Sources', DIRECTORY_SEPARATOR, '.php'], + $class, + ); + } + } + + foreach (Tools\Upgrade::CLEANUPS[$ns] as $class) { + if (!class_exists($class)) { + $missing_or_corrupt_files[] = preg_replace( + ['/^SMF\b/', '/\\\\/', '/$/'], + ['Sources', DIRECTORY_SEPARATOR, '.php'], + $class, + ); + } + } + } + + return $missing_or_corrupt_files; + } + + // We can't do anything without the manifest file. + $sourcedir = Config::$sourcedir ?? $boarddir . DIRECTORY_SEPARATOR . 'Sources'; + + $missing_or_corrupt_files[] = str_replace( + Sapi::canonicalPath($sourcedir), + basename($sourcedir), + __DIR__ . DIRECTORY_SEPARATOR . 'manifest.php', + ); + + return $missing_or_corrupt_files; + } + + // Get the manifest. + try { + $contents = @file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'manifest.php'); + + $valid = ( + $contents !== false + // Must start with an opening PHP tag. + && str_starts_with($contents, '<' . '?php') + // Must not contain a closing tag. + && !str_contains($contents, '?' . '>') + // Does it at least appear to have the expected contents? + && str_contains($contents, "\nreturn ") + ); + + unset($contents); + + $manifest = !$valid ? false : include __DIR__ . DIRECTORY_SEPARATOR . 'manifest.php'; + } catch (\Throwable $e) { + $manifest = false; + } + + // Manifest must be an array. + if (!\is_array($manifest)) { + $sourcedir = Config::$sourcedir ?? $boarddir . DIRECTORY_SEPARATOR . 'Sources'; + + $missing_or_corrupt_files[] = str_replace( + Sapi::canonicalPath($sourcedir), + basename($sourcedir), + __DIR__ . DIRECTORY_SEPARATOR . 'manifest.php', + ); + + return $missing_or_corrupt_files; + } + + // Which sections of the manifest do we care about? + $max = preg_replace('/^(\d)\.(\d).*/', '$1.$2', SMF_VERSION); + $min = preg_replace('/^(\d)\.(\d).*/', '$1.$2', $start_version); + + $required_versions = array_filter( + array_map( + fn($v) => preg_replace('/^(\d)\.(\d).*/', '$1.$2', $v), + array_keys(Tools\Upgrade::VERSION_MAP), + ), + fn($v) => version_compare($v, $min, '>=') && version_compare($v, $max, '<='), + ); + + // Verify that each section of the manifest is structured correctly. + foreach ($required_versions as $version) { + if ( + // $manifest[$version] must exist. + !\array_key_exists($version, $manifest) + // Keys must be strings. + || ( + array_keys($manifest[$version]) !== array_map( + 'strval', + array_keys($manifest[$version]), + ) + ) + // Values must be strings or bools. + || ( + $manifest[$version] !== array_filter( + $manifest[$version], + fn($v) => \is_bool($v) || \is_string($v), + ) + ) + ) { + $sourcedir = Config::$sourcedir ?? $boarddir . DIRECTORY_SEPARATOR . 'Sources'; + + $missing_or_corrupt_files[] = str_replace( + Sapi::canonicalPath($sourcedir), + basename($sourcedir), + __DIR__ . DIRECTORY_SEPARATOR . 'manifest.php', + ); + + return $missing_or_corrupt_files; + } + } + + // Do all the files in the manifest exist and have the expected content? + foreach ($required_versions as $version) { + foreach ($manifest[$version] as $path => $checksum) { + $is_valid = false; + $possible_paths = []; + + // Check for special directories that could have been moved. + switch (strstr($path, '/', true)) { + case 'Languages': + if (isset(Config::$languagesdir)) { + $possible_paths[] = Sapi::canonicalPath(Config::$languagesdir . strstr($path, '/')); + } + break; + + case 'Packages': + if (isset(Config::$packagesdir)) { + $possible_paths[] = Sapi::canonicalPath(Config::$packagesdir . strstr($path, '/')); + } + break; + + case 'Sources': + if (isset(Config::$sourcedir)) { + $possible_paths[] = Sapi::canonicalPath(Config::$sourcedir . strstr($path, '/')); + } + break; + + case 'vendor': + if (isset(Config::$vendordir)) { + $possible_paths[] = Sapi::canonicalPath(Config::$vendordir . strstr($path, '/')); + } + break; + } + + // Standard path. + $possible_paths[] = Sapi::canonicalPath($boarddir . '/' . $path); + + // Check each possible path. + foreach (array_unique($possible_paths) as $possible_path) { + $is_valid = ( + file_exists($possible_path) + && ( + !$check_content + || $checksum === true + || ( + is_file($possible_path) + && $checksum === @md5_file($possible_path) + ) + ) + ); + + if ($is_valid) { + break; + } + } + + if (!$is_valid) { + $missing_or_corrupt_files[] = $path; + } + } + } + + return $missing_or_corrupt_files; + } + /** * Fetch our current step. * diff --git a/Sources/Maintenance/Migration/v2_1/BoardDescriptions.php b/Sources/Maintenance/Migration/v2_1/BoardDescriptions.php index 97c1aea00fd..1d1fbc4f50f 100644 --- a/Sources/Maintenance/Migration/v2_1/BoardDescriptions.php +++ b/Sources/Maintenance/Migration/v2_1/BoardDescriptions.php @@ -43,7 +43,7 @@ class BoardDescriptions extends MigrationBase public function isCandidate(): bool { return version_compare( - str_replace(' ', '.', strtolower(Config::$modSettings['smfVersion'] ?? '0.0.dev.0')), + Utils::standardizeVersionString(Config::$modSettings['smfVersion'] ?? '0.0.dev.0'), '2.1.dev.0', '<', ); diff --git a/Sources/Maintenance/Migration/v2_1/UserDrafts.php b/Sources/Maintenance/Migration/v2_1/UserDrafts.php index dbc8cc516b6..8fd0ba543d5 100644 --- a/Sources/Maintenance/Migration/v2_1/UserDrafts.php +++ b/Sources/Maintenance/Migration/v2_1/UserDrafts.php @@ -20,6 +20,7 @@ use SMF\Db\Schema; use SMF\Maintenance\Maintenance; use SMF\Maintenance\Migration\MigrationBase; +use SMF\Utils; class UserDrafts extends MigrationBase { @@ -64,7 +65,7 @@ public function execute(): bool if ( $start <= 1 && version_compare( - str_replace(' ', '.', strtolower(Config::$modSettings['smfVersion'] ?? '0.0.dev.0')), + Utils::standardizeVersionString(Config::$modSettings['smfVersion'] ?? '0.0.dev.0'), '2.1.dev.0', '<', ) diff --git a/Sources/Maintenance/Tools/Install.php b/Sources/Maintenance/Tools/Install.php index 457bc6e9237..4ae4b1e426d 100644 --- a/Sources/Maintenance/Tools/Install.php +++ b/Sources/Maintenance/Tools/Install.php @@ -112,6 +112,11 @@ class Install extends ToolsBase implements ToolsInterface */ public function __construct() { + // Are we doing debug? + if (isset($_GET['debug']) || isset($_POST['debug'])) { + $this->debug = true; + } + Maintenance::$languages = $this->detectLanguages(['General', 'Maintenance']); if (empty(Maintenance::$languages)) { @@ -252,6 +257,9 @@ public function welcome(): bool { // Done the submission? if (isset($_POST['contbutt'])) { + // Does the admin want debug info? + $this->debug = isset($_POST['debug']); + return true; } @@ -295,13 +303,31 @@ public function welcome(): bool } // Make sure they uploaded all the files. - if (!file_exists(Config::$boarddir . '/index.php')) { - Maintenance::$errors[] = Lang::getTxt('error_missing_files', file: 'Maintenance'); - $this->logProgress(Lang::getTxt('error_missing_files', file: 'Maintenance')); + if (($missing_files = Maintenance::checkManifest(SMF_VERSION, true)) !== []) { + $error_message = Lang::getTxt( + 'error_missing_files' . ($this->isDebug() ? '_debug' : ''), + [ + 'missing_files' => '
  1. ' . implode('
  2. ', $missing_files) . '
', + ], + file: 'Maintenance', + ); + + // Special message for developers. + if ( + str_ends_with(SMF_VERSION, '-dev') + && file_exists(Maintenance::getBaseDir() . '/other/update_manifest.php') + ) { + // Not translated because it should never show up for anyone but devs. + $error_message .= '
Run ./other/update_manifest.php, then try again.'; + } + + Maintenance::$errors[] = $error_message; + $this->logProgress($error_message); } + // Very simple check on the session.save_path for Windows. // @todo Move this down later if they don't use database-driven sessions? - elseif (@\ini_get('session.save_path') == '/tmp' && Sapi::isOS(Sapi::OS_WINDOWS)) { + if (@\ini_get('session.save_path') == '/tmp' && Sapi::isOS(Sapi::OS_WINDOWS)) { Maintenance::$errors[] = Lang::getTxt('error_session_save_path', file: 'Maintenance'); $this->logProgress(Lang::getTxt('error_session_save_path', file: 'Maintenance')); } @@ -336,11 +362,6 @@ public function welcome(): bool Utils::$context['continue'] = true; } - // Are we doing debug? - if (isset($_REQUEST['debug'])) { - $this->debug = true; - } - return false; } @@ -543,8 +564,8 @@ public function databaseSettings(): bool // @todo Old client, new server? if ( version_compare( - preg_replace('~^\D*|\-.+?$~', '', Db::$db->get_version()), - Db::$db->getMinimumVersion(), + Utils::standardizeVersionString(Db::$db->get_version()), + Utils::standardizeVersionString(Db::$db->getMinimumVersion()), '<', ) ) { @@ -1414,7 +1435,7 @@ private function getProgress(): void $data = isset($defined_vars['maintenance_tool_progress']) ? Utils::jsonDecode($defined_vars['maintenance_tool_progress'], true) : []; $this->time_started = (int) ($data['started'] ?? time()); - $this->debug = !empty($data['debug']); + $this->debug = $this->debug || !empty($data['debug']); } /** diff --git a/Sources/Maintenance/Tools/ToolsBase.php b/Sources/Maintenance/Tools/ToolsBase.php index ddd83f5c36b..fbaaf71aa89 100644 --- a/Sources/Maintenance/Tools/ToolsBase.php +++ b/Sources/Maintenance/Tools/ToolsBase.php @@ -125,7 +125,21 @@ public function logProgress(mixed $message, bool $ongoing = false, bool $reset = $message = print_r($message, true); } - $message = preg_replace('/(]*>)+|\R/', PHP_EOL, $message); + $message = preg_replace( + [ + // Convert line breaks into the correct EOL for this platform. + '/(]*>)+|\R/', + // Convert HTML lists into plain text lists. + '/\s*<\/?((ol|ul)\b[^>]*|\/li)>\s*/', + '/]*>\s*/', + ], + [ + PHP_EOL, + PHP_EOL, + ' - ', + ], + $message, + ); $message .= $ongoing ? '... ' : PHP_EOL; diff --git a/Sources/Maintenance/Tools/Upgrade.php b/Sources/Maintenance/Tools/Upgrade.php index b612262d28d..519a618b489 100644 --- a/Sources/Maintenance/Tools/Upgrade.php +++ b/Sources/Maintenance/Tools/Upgrade.php @@ -367,6 +367,11 @@ class Upgrade extends ToolsBase implements ToolsInterface */ public function __construct() { + // Are we doing debug? + if (isset($_GET['debug']) || isset($_POST['debug'])) { + $this->debug = true; + } + Maintenance::$languages = $this->detectLanguages(['General', 'Maintenance']); if (empty(Maintenance::$languages)) { @@ -420,7 +425,7 @@ public function __construct() // Is this a large (and old) forum? We may do special logic then. Utils::$context['is_large_forum'] = $this->is_large_forum = ( version_compare( - str_replace(' ', '.', strtolower($this->start_smf_version)), + Utils::standardizeVersionString($this->start_smf_version), '1.1.rc.1', '<=', ) @@ -552,6 +557,41 @@ public function welcomeLogin(): bool return true; } + // Make sure they uploaded all the files. + $missing_files = Maintenance::checkManifest( + start_version: $this->start_smf_version, + // If upgrading from an old SMF version (e.g. 2.1 -> 3.0), validate + // the file content. Otherwise, just check that the files exist. + // This allows the admin to re-run the upgrader at any time even if + // installed modification packages have made file changes. + check_content: version_compare( + Utils::standardizeVersionString($this->start_smf_version), + preg_replace('/^(\d+\.\d+).*/', '$1.dev.0', SMF_VERSION), + '<', + ), + ); + + if (!empty($missing_files)) { + Maintenance::$fatal_error = Lang::getTxt( + 'error_missing_files' . ($this->isDebug() ? '_debug' : ''), + ['missing_files' => '
  1. ' . implode('
  2. ', $missing_files) . '
'], + file: 'Maintenance', + ); + + // Special message for developers. + if ( + str_ends_with(SMF_VERSION, '-dev') + && file_exists(Maintenance::getBaseDir() . '/other/update_manifest.php') + ) { + // Not translated because it should never show up for anyone but devs. + Maintenance::$fatal_error .= '
Run ./other/update_manifest.php, then try again.'; + } + + $this->logProgress(Maintenance::$fatal_error); + + return false; + } + // Needs to at least meet our minium version. if (version_compare(Maintenance::PHP_MIN_VERSION, PHP_VERSION, '>=')) { Maintenance::$fatal_error = Lang::getTxt('error_php_too_low', file: 'Maintenance'); @@ -576,52 +616,12 @@ public function welcomeLogin(): bool return false; } - // Check for some key files. - $check = ( - @file_exists(Maintenance::$theme_dir . '/index.template.php') - && @file_exists(Config::$sourcedir . '/Forum.php') - && @file_exists(Config::$sourcedir . '/QueryString.php') - && @file_exists(Config::$sourcedir . '/Db/APIs/' . Db::getClass(Config::$db_type) . '.php') - ); - - try { - foreach (self::VERSION_MAP as $search => $ns) { - if (version_compare($this->start_smf_version, $search, '>')) { - continue; - } - - foreach (self::MIGRATIONS[$ns] as $class) { - if (!class_exists($class)) { - throw new \Exception("{$class} does not exist"); - } - } - - foreach (self::CLEANUPS[$ns] as $class) { - if (!class_exists($class)) { - throw new \Exception("{$class} does not exist"); - } - } - } - } - // Developers, set break point here to figure out what you did wrong. - catch (\Exception $ex) { - $check = false; - } - - if (!$check) { - // Don't tell them what files exactly because it's a spot check - just like teachers don't tell which problems they are spot checking, that's dumb. - Maintenance::$fatal_error = Lang::getTxt('error_upgrade_files_missing', file: 'Maintenance'); - $this->logProgress(Maintenance::$fatal_error); - - return false; - } - Db::load(); if ( version_compare( - preg_replace('~^\D*|\-.+?$~', '', Db::$db->get_version()), - Db::$db->getMinimumVersion(), + Utils::standardizeVersionString(Db::$db->get_version()), + Utils::standardizeVersionString(Db::$db->getMinimumVersion()), '<', ) ) { @@ -821,7 +821,7 @@ public function welcomeLogin(): bool return true; } - $use_old_hashing = version_compare(str_replace(' ', '.', strtolower(Config::$modSettings['smfVersion'] ?? '0.0.dev.0')), '2.1.dev.0', '<'); + $use_old_hashing = version_compare(Utils::standardizeVersionString(Config::$modSettings['smfVersion'] ?? '0.0.dev.0'), '2.1.dev.0', '<'); if (($id = Maintenance::loginAdmin((string) $_POST['user'], (string) $_POST['passwrd'], $use_old_hashing)) > 0) { $this->user['id'] = $id; @@ -862,7 +862,7 @@ public function upgradeOptions(): bool Utils::$context['migrate_settings_recommended'] = ( empty(Config::$modSettings['smfVersion']) || version_compare( - str_replace(' ', '.', strtolower(Config::$modSettings['smfVersion'])), + Utils::standardizeVersionString(Config::$modSettings['smfVersion']), preg_replace('/^(\d+\.\d+).*/', '$1.dev.0', SMF_VERSION), '<', ) @@ -1019,9 +1019,7 @@ public function upgradeOptions(): bool } // Are we doing debug? - if (isset($_POST['debug'])) { - $this->debug = true; - } + $this->debug = isset($_POST['debug']); // If we've got here then let's proceed to the next step! return true; @@ -1116,7 +1114,7 @@ public function migrations(): bool foreach (self::VERSION_MAP as $search => $ns) { $substeps = []; - if (version_compare($this->start_smf_version, $search, '>')) { + if (version_compare(Utils::standardizeVersionString($this->start_smf_version), $search, '>')) { continue; } @@ -1148,7 +1146,7 @@ public function migrations(): bool // Update Config::$modSettings['smfVersion'] incrementally as we go. // This lets us avoid redoing unnecessary migration steps if the // upgrader gets interrupted and restarted for some reason. - if (version_compare($search, SMF_VERSION, '<')) { + if (version_compare($search, Utils::standardizeVersionString(SMF_VERSION), '<')) { $this->updateModSettings([ 'smfVersion' => substr($search, 0, strrpos($search, '.') + 1) . str_increment(substr($search, strrpos($search, '.') + 1)), ]); @@ -1180,7 +1178,7 @@ public function cleanup(): bool $substeps = []; foreach (self::VERSION_MAP as $search => $ns) { - if (version_compare($this->start_smf_version, $search, '>')) { + if (version_compare(Utils::standardizeVersionString($this->start_smf_version), $search, '>')) { continue; } @@ -1437,7 +1435,7 @@ private function getProgress(): void $this->time_started = (int) ($data['started'] ?? time()); $this->time_updated = (int) ($data['updated'] ?? time()); - $this->debug = !empty($data['debug']); + $this->debug = $this->debug || !empty($data['debug']); $this->skipped_migrations = (array) ($data['skipped'] ?? []); $this->user['id'] = (int) ($data['user_id'] ?? 0); $this->user['name'] = (string) ($data['user_name'] ?? ''); diff --git a/Sources/Maintenance/Utf8ConverterStep.php b/Sources/Maintenance/Utf8ConverterStep.php index 81c4c7a22f3..17a03fc6bbb 100644 --- a/Sources/Maintenance/Utf8ConverterStep.php +++ b/Sources/Maintenance/Utf8ConverterStep.php @@ -500,7 +500,7 @@ public function __construct(int $id, string $name, int $progress, ?string $title // Which character set have they been using for interacting with the browser? if (isset(Config::$modSettings['global_character_set'])) { $this->lang_charset = Config::$modSettings['global_character_set']; - } elseif (version_compare(strtolower(str_replace(' ', '.', Config::$modSettings['smfVersion'])), '3.0.dev.1', '>=')) { + } elseif (version_compare(Utils::standardizeVersionString(Config::$modSettings['smfVersion']), '3.0.dev.0', '>=')) { $this->lang_charset = 'UTF-8'; } else { // Figure it out the hard way. diff --git a/Sources/PackageManager/PackageManager.php b/Sources/PackageManager/PackageManager.php index 5dd1de57648..5dd14b043b2 100644 --- a/Sources/PackageManager/PackageManager.php +++ b/Sources/PackageManager/PackageManager.php @@ -1448,7 +1448,7 @@ public function updateBackwardCompatibility(): void while ($row = Db::$db->fetch_assoc($request)) { $row['smf_version'] = strtr(strtolower($row['smf_version']), ' ', '.'); - if (version_compare($row['smf_version'], $lowest_found_version, '<')) { + if (version_compare(Utils::standardizeVersionString($row['smf_version']), Utils::standardizeVersionString($lowest_found_version), '<')) { $lowest_found_version = $row['smf_version']; } } @@ -1456,7 +1456,7 @@ public function updateBackwardCompatibility(): void Db::$db->free_result($request); Config::updateSettingsFile([ - 'backward_compatibility' => (int) version_compare($lowest_found_version, $min_version, '<'), + 'backward_compatibility' => (int) version_compare(Utils::standardizeVersionString($lowest_found_version), Utils::standardizeVersionString($min_version), '<'), ]); } diff --git a/Sources/Search/APIs/Fulltext.php b/Sources/Search/APIs/Fulltext.php index ddd7e05d626..a2cb4e6fce9 100644 --- a/Sources/Search/APIs/Fulltext.php +++ b/Sources/Search/APIs/Fulltext.php @@ -599,7 +599,7 @@ public static function detectIndex(): void if ( isset($row['Engine']) && strtolower($row['Engine']) != 'myisam' - && !(strtolower($row['Engine']) == 'innodb' && version_compare(Db::$db->get_version(), '5.6.4', '>=')) + && !(strtolower($row['Engine']) == 'innodb' && version_compare(Utils::standardizeVersionString(Db::$db->get_version()), '5.6.4', '>=')) ) { Utils::$context['cannot_create_fulltext'] = true; } diff --git a/Sources/ServerSideIncludes.php b/Sources/ServerSideIncludes.php index 9e0cd01ae3c..87e22bca887 100644 --- a/Sources/ServerSideIncludes.php +++ b/Sources/ServerSideIncludes.php @@ -237,7 +237,7 @@ public function __construct() } // Gzip output? (because it must be boolean and true, this can't be hacked.) - if ($this->gzip === true && \ini_get('zlib.output_compression') != '1' && \ini_get('output_handler') != 'ob_gzhandler' && version_compare(PHP_VERSION, '4.2.0', '>=')) { + if ($this->gzip === true && \ini_get('zlib.output_compression') != '1' && \ini_get('output_handler') != 'ob_gzhandler') { ob_start('ob_gzhandler'); } else { Config::$modSettings['enableCompressedOutput'] = '0'; diff --git a/Sources/Utils.php b/Sources/Utils.php index 844440c73c5..0f2e0cc134f 100644 --- a/Sources/Utils.php +++ b/Sources/Utils.php @@ -2481,6 +2481,37 @@ public static function getCallable(string|callable $input, ?bool $ignore_errors return $callable; } + /** + * Rewrites a version string in "PHP standardized" format. + * + * - Converts the string to lowercase. + * - Removes any non-digits at the start of the string. + * - Inserts dots (i.e. `.`) between digits and non-digits. + * - Replaces non-alphanumeric characters with dots. + * - Ensures runs of dots are collapsed into a single dot. + * + * @param string $version The version string + * @return string "PHP standardized" version string. + */ + public static function standardizeVersionString(string $version): string + { + return preg_replace( + [ + '/^\D+|[^0-9a-z]+$/', + '/(\d)(\D)/', + '/(\D)(\d)/', + '/[^0-9a-z]+/', + ], + [ + '', + '$1.$2', + '$1.$2', + '.', + ], + strtolower($version), + ); + } + /************************* * Internal static methods *************************/ diff --git a/Themes/default/InstallTemplate.php b/Themes/default/InstallTemplate.php index b37d5b25fa8..4c6a94bde8e 100644 --- a/Themes/default/InstallTemplate.php +++ b/Themes/default/InstallTemplate.php @@ -89,6 +89,19 @@ public static function welcome(): void MaintenanceTemplate::warningsAndErrors(); } + // Offer a checkbox to enable debug mode. + echo ' +
+
+ +
+
+ isDebug() ? ' checked' : '') . '> +
+
'; + // For the latest version stuff. echo '