Use `FileUtil::getMemoryLimit()` in WCFSetup
authorTim Düsterhus <duesterhus@woltlab.com>
Mon, 28 Aug 2023 09:59:07 +0000 (11:59 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Mon, 28 Aug 2023 10:12:03 +0000 (12:12 +0200)
wcfsetup/install/files/lib/system/WCFSetup.class.php
wcfsetup/setup/template/stepShowSystemRequirements.tpl

index 2a39c816480e7a8814ab3b73dde8d1e471bd51f4..b020b0ac321d88aa3feaf0583b92471232e5381b 100644 (file)
@@ -31,6 +31,7 @@ use wcf\system\session\SessionHandler;
 use wcf\system\setup\Installer;
 use wcf\system\setup\SetupFileHandler;
 use wcf\system\template\SetupTemplateEngine;
+use wcf\util\FileUtil;
 use wcf\util\HeaderUtil;
 use wcf\util\StringUtil;
 use wcf\util\UserUtil;
@@ -398,8 +399,8 @@ final class WCFSetup extends WCF
         }
 
         // memory limit
-        $system['memoryLimit']['value'] = \ini_get('memory_limit');
-        $system['memoryLimit']['result'] = $this->compareMemoryLimit();
+        $system['memoryLimit']['value'] = FileUtil::getMemoryLimit();
+        $system['memoryLimit']['result'] = $system['memoryLimit']['value'] === -1 || $system['memoryLimit']['value'] >= 128 * 1024 * 1024;
 
         // openssl extension
         $system['openssl']['result'] = \extension_loaded('openssl');
@@ -438,44 +439,6 @@ final class WCFSetup extends WCF
         return $this->gotoNextStep('configureDB');
     }
 
-    /**
-     * Returns true if memory_limit is set to at least 128 MB
-     */
-    protected function compareMemoryLimit(): bool
-    {
-        $memoryLimit = \ini_get('memory_limit');
-
-        // no limit
-        if ($memoryLimit == -1) {
-            return true;
-        }
-
-        // completely numeric, PHP assumes byte
-        if (\is_numeric($memoryLimit)) {
-            $memoryLimit = $memoryLimit / 1024 / 1024;
-
-            return $memoryLimit >= 128;
-        }
-
-        // PHP supports 'K', 'M' and 'G' shorthand notation
-        if (\preg_match('~^(\d+)([KMG])$~', $memoryLimit, $matches)) {
-            switch ($matches[2]) {
-                case 'K':
-                    $memoryLimit = $matches[1] * 1024;
-
-                    return $memoryLimit >= 128;
-
-                case 'M':
-                    return $matches[1] >= 128;
-
-                case 'G':
-                    return $matches[1] >= 1;
-            }
-        }
-
-        return false;
-    }
-
     /**
      * Shows the page for configuring the database connection.
      */
index 2303b8e522f7201455fe525d79109aa14b9ded96..5e3d0bd954ff979014cc72ac51ac9da4d591e9e6 100644 (file)
                        <div class="row rowColGap formGrid">
                                <dl class="col-xs-12 col-md-6">
                                        <dt>{lang}wcf.global.systemRequirements.element.required{/lang}</dt>
-                                       <dd>128M</dd>
+                                       <dd>128 MiB</dd>
                                </dl>
                                
                                <dl class="col-xs-12 col-md-6">
                                        <dt>{lang}wcf.global.systemRequirements.element.yours{/lang}</dt>
                                        <dd>
-                                               <span class="badge {if !$system.memoryLimit.result}red{else}green{/if}">{$system.memoryLimit.value}</span>
+                                               <span class="badge {if !$system.memoryLimit.result}red{else}green{/if}">{$system.memoryLimit.value|filesizeBinary}</span>
                                                {if !$system.memoryLimit.result}<small>{lang}wcf.global.systemRequirements.memoryLimit.description{/lang}</small>{/if}
                                        </dd>
                                </dl>