From ef1172766c90d084f00f7987d2c466767829bb95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Thu, 3 Aug 2023 14:15:26 +0200 Subject: [PATCH] Make use of `ini_parse_quantity()` in FileUtil::getMemoryLimit() If `ini_parse_quantity()` is available (PHP 8.2+) it will certainly be more accurate and simpler than our home-grown calculator. --- .../install/files/lib/util/FileUtil.class.php | 40 ++++++++++--------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/wcfsetup/install/files/lib/util/FileUtil.class.php b/wcfsetup/install/files/lib/util/FileUtil.class.php index 5e7293d66a6..0d4cc2f8172 100644 --- a/wcfsetup/install/files/lib/util/FileUtil.class.php +++ b/wcfsetup/install/files/lib/util/FileUtil.class.php @@ -514,29 +514,31 @@ public static function getMemoryLimit(): int $memoryLimit = \ini_get('memory_limit'); // no limit - if ($memoryLimit == -1) { + if ($memoryLimit == "-1") { self::$memoryLimit = -1; - } - - // completely numeric, PHP assumes byte - if (\is_numeric($memoryLimit)) { - self::$memoryLimit = $memoryLimit; - } + } else if (\function_exists('ini_parse_quantity')) { + self::$memoryLimit = \ini_parse_quantity($memoryLimit); + } else { + // completely numeric, PHP assumes byte + if (\is_numeric($memoryLimit)) { + self::$memoryLimit = $memoryLimit; + } - // PHP supports 'K', 'M' and 'G' shorthand notation - if (\preg_match('~^(\d+)\s*([KMG])$~i', $memoryLimit, $matches)) { - switch (\strtoupper($matches[2])) { - case 'K': - self::$memoryLimit = $matches[1] * 1024; - break; + // PHP supports 'K', 'M' and 'G' shorthand notation + if (\preg_match('~^(\d+)\s*([KMG])$~i', $memoryLimit, $matches)) { + switch (\strtoupper($matches[2])) { + case 'K': + self::$memoryLimit = $matches[1] * 1024; + break; - case 'M': - self::$memoryLimit = $matches[1] * 1024 * 1024; - break; + case 'M': + self::$memoryLimit = $matches[1] * 1024 * 1024; + break; - case 'G': - self::$memoryLimit = $matches[1] * 1024 * 1024 * 1024; - break; + case 'G': + self::$memoryLimit = $matches[1] * 1024 * 1024 * 1024; + break; + } } } }