From c48ac77d472cd60ee5c7abc904c9d2a3bd43186c Mon Sep 17 00:00:00 2001 From: Jon Stovell Date: Tue, 8 Sep 2026 15:08:27 -0600 Subject: [PATCH 1/6] Implements Utils::standardizeVersionString() Signed-off-by: Jon Stovell --- Sources/Actions/ViewQuery.php | 2 +- Sources/Config.php | 4 +-- Sources/Db/APIs/MySQL.php | 2 +- Sources/Db/Schema/Table.php | 3 +- .../Db/Schema/v3_0/Initialize/PostgreSQL.php | 4 ++- Sources/MailAgent/MailAgent.php | 13 +++++++- .../Migration/v2_1/BoardDescriptions.php | 2 +- .../Maintenance/Migration/v2_1/UserDrafts.php | 3 +- Sources/Maintenance/Tools/Install.php | 4 +-- Sources/Maintenance/Tools/Upgrade.php | 18 +++++------ Sources/Maintenance/Utf8ConverterStep.php | 2 +- Sources/PackageManager/PackageManager.php | 4 +-- Sources/Search/APIs/Fulltext.php | 2 +- Sources/ServerSideIncludes.php | 2 +- Sources/Utils.php | 31 +++++++++++++++++++ tests/Unit/UtilsTest.php | 23 ++++++++++++++ 16 files changed, 94 insertions(+), 25 deletions(-) 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/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..8a30b95fba3 100644 --- a/Sources/Maintenance/Tools/Install.php +++ b/Sources/Maintenance/Tools/Install.php @@ -543,8 +543,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()), '<', ) ) { diff --git a/Sources/Maintenance/Tools/Upgrade.php b/Sources/Maintenance/Tools/Upgrade.php index b612262d28d..0a06c6d0448 100644 --- a/Sources/Maintenance/Tools/Upgrade.php +++ b/Sources/Maintenance/Tools/Upgrade.php @@ -420,7 +420,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', '<=', ) @@ -586,7 +586,7 @@ public function welcomeLogin(): bool try { 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; } @@ -620,8 +620,8 @@ public function welcomeLogin(): bool 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), '<', ) @@ -1116,7 +1116,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 +1148,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 +1180,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; } 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/tests/Unit/UtilsTest.php b/tests/Unit/UtilsTest.php index ffc139b707d..f9ac52937d8 100644 --- a/tests/Unit/UtilsTest.php +++ b/tests/Unit/UtilsTest.php @@ -163,6 +163,12 @@ public function testEntityStrlenAcrossInputs(string $input, int $expected): void $this->assertSame($expected, Utils::entityStrlen($input)); } + #[DataProvider('versionStringProvider')] + public function testStandardizeVersionString(string $input, string $expected): void + { + $this->assertSame($expected, Utils::standardizeVersionString($input)); + } + /*********************** * Public static methods ***********************/ @@ -181,4 +187,21 @@ public static function entityLengthProvider(): array 'mixed' => ['a&é', 3], ]; } + + /** + * @return array + */ + public static function versionStringProvider(): array + { + return [ + 'pre-release-dev' => ['3.0 Alpha 5-dev', '3.0.alpha.5.dev'], + 'pre-release' => ['3.0 Beta 5', '3.0.beta.5'], + 'release-candidate' => ['3.0 RC1', '3.0.rc.1'], + 'patch-dev' => ['3.0.7-dev', '3.0.7.dev'], + 'patch' => ['3.0.7', '3.0.7'], + 'mixed' => ['17.6 (Debian 17.6-1.pgdg13+1)', '17.6.debian.17.6.1.pgdg.13.1'], + 'prefixed' => ['SMF 3.0.7', '3.0.7'], + 'invalid' => ['not a version string', ''], + ]; + } } From e5ad7f5eea4bb8d6ad0b8e43f72c6cb5f3628e39 Mon Sep 17 00:00:00 2001 From: Jon Stovell Date: Sat, 12 Sep 2026 21:59:24 -0600 Subject: [PATCH 2/6] Improves handling of debug mode in installer and upgrader Signed-off-by: Jon Stovell --- Sources/Maintenance/Tools/Install.php | 15 +++++++++------ Sources/Maintenance/Tools/Upgrade.php | 11 +++++++---- Themes/default/InstallTemplate.php | 13 +++++++++++++ Themes/default/UpgradeTemplate.php | 2 +- 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/Sources/Maintenance/Tools/Install.php b/Sources/Maintenance/Tools/Install.php index 8a30b95fba3..f440fecfc03 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; } @@ -336,11 +344,6 @@ public function welcome(): bool Utils::$context['continue'] = true; } - // Are we doing debug? - if (isset($_REQUEST['debug'])) { - $this->debug = true; - } - return false; } @@ -1414,7 +1417,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/Upgrade.php b/Sources/Maintenance/Tools/Upgrade.php index 0a06c6d0448..aae47471683 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)) { @@ -1019,9 +1024,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; @@ -1437,7 +1440,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/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 '