Make use of `ini_parse_quantity()` in FileUtil::getMemoryLimit()
authorTim Düsterhus <duesterhus@woltlab.com>
Thu, 3 Aug 2023 12:15:26 +0000 (14:15 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Thu, 3 Aug 2023 12:15:26 +0000 (14:15 +0200)
If `ini_parse_quantity()` is available (PHP 8.2+) it will certainly be more
accurate and simpler than our home-grown calculator.

wcfsetup/install/files/lib/util/FileUtil.class.php

index 5e7293d66a6c57401dbdc6d8ac4273330d982418..0d4cc2f8172f01740ee6c67bf07d563ccabda33b 100644 (file)
@@ -514,29 +514,31 @@ final class FileUtil
             $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;
+                    }
                 }
             }
         }