diff --git a/app/code/core/Mage/Catalog/Model/Product/Image.php b/app/code/core/Mage/Catalog/Model/Product/Image.php index fba5baf895f..bbd30f55fe2 100644 --- a/app/code/core/Mage/Catalog/Model/Product/Image.php +++ b/app/code/core/Mage/Catalog/Model/Product/Image.php @@ -240,8 +240,8 @@ protected function _checkMemory($file = null) } /** + * @return int * @deprecated - * @return float|int|string */ protected function _getMemoryLimit() { @@ -251,18 +251,7 @@ protected function _getMemoryLimit() $memoryLimit = "128M"; } - $value = (int)substr($memoryLimit, 0, -1); - - if (substr($memoryLimit, -1) == 'K') { - return $value * 1024; - } - if (substr($memoryLimit, -1) == 'M') { - return $value * 1024 * 1024; - } - if (substr($memoryLimit, -1) == 'G') { - return $value * 1024 * 1024 * 1024; - } - return $memoryLimit; + return ini_parse_quantity($memoryLimit); } /** diff --git a/app/code/core/Mage/Catalog/Model/Product/Option/Type/File.php b/app/code/core/Mage/Catalog/Model/Product/Option/Type/File.php index 7f9e039d90f..5b08b5ca993 100644 --- a/app/code/core/Mage/Catalog/Model/Product/Option/Type/File.php +++ b/app/code/core/Mage/Catalog/Model/Product/Option/Type/File.php @@ -801,21 +801,11 @@ protected function _getBytesIniValue($ini_key) { $_bytes = @ini_get($ini_key); - if (stristr($_bytes, 'k')) { - // kilobytes - $_bytes = (int) $_bytes * 1024; - } elseif (stristr($_bytes, 'm')) { - // megabytes - $_bytes = (int) $_bytes * 1024 * 1024; - } elseif (stristr($_bytes, 'g')) { - // gigabytes - $_bytes = (int) $_bytes * 1024 * 1024 * 1024; - } - return (int)$_bytes; + return ini_parse_quantity($_bytes); } /** - * Simple converrt bytes to Megabytes + * Simple convert bytes to Megabytes * * @param int $bytes * @return float diff --git a/app/code/core/Mage/Uploader/Helper/File.php b/app/code/core/Mage/Uploader/Helper/File.php index 4ed940445d8..3d068014145 100644 --- a/app/code/core/Mage/Uploader/Helper/File.php +++ b/app/code/core/Mage/Uploader/Helper/File.php @@ -707,7 +707,12 @@ public function getUploadMaxSize() */ public function getDataMaxSize() { - return min($this->getPostMaxSize(), $this->getUploadMaxSize()); + $postMaxSize = $this->getPostMaxSize(); + $uploadMaxSize = $this->getUploadMaxSize(); + $postMaxSizeBytes = ini_parse_quantity($postMaxSize); + $uploadMaxSizeBytes = ini_parse_quantity($uploadMaxSize); + + return min($postMaxSizeBytes, $uploadMaxSizeBytes) === $postMaxSizeBytes ? $postMaxSize : $uploadMaxSize; } /** @@ -717,27 +722,6 @@ public function getDataMaxSize() */ public function getDataMaxSizeInBytes() { - $iniSize = $this->getDataMaxSize(); - $size = (int)substr($iniSize, 0, -1); - $parsedSize = 0; - switch (strtolower(substr($iniSize, strlen($iniSize) - 1))) { - case 't': - $parsedSize = $size * (1024 * 1024 * 1024 * 1024); - break; - case 'g': - $parsedSize = $size * (1024 * 1024 * 1024); - break; - case 'm': - $parsedSize = $size * (1024 * 1024); - break; - case 'k': - $parsedSize = $size * 1024; - break; - case 'b': - default: - $parsedSize = $size; - break; - } - return (int)$parsedSize; + return ini_parse_quantity($this->getDataMaxSize()); } } diff --git a/composer.json b/composer.json index 33272d87991..663b91a6660 100644 --- a/composer.json +++ b/composer.json @@ -31,9 +31,10 @@ "phpseclib/mcrypt_compat": "^2.0.3", "phpseclib/phpseclib": "^3.0.14", "shardj/zf1-future": "1.24.0", - "symfony/polyfill-php74": "^1.27", - "symfony/polyfill-php80": "^1.27", - "symfony/polyfill-php81": "^1.27" + "symfony/polyfill-php74": "^1.29", + "symfony/polyfill-php80": "^1.29", + "symfony/polyfill-php81": "^1.29", + "symfony/polyfill-php82": "^1.29" }, "require-dev": { "dealerdirect/phpcodesniffer-composer-installer": "^1.0.0", diff --git a/composer.lock b/composer.lock index caf87a629ff..560a713c94c 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "3d728362b534d576376af960b6dc160f", + "content-hash": "76dfca273981ae0e9388059e1108e257", "packages": [ { "name": "colinmollenhour/cache-backend-redis", @@ -1969,6 +1969,82 @@ ], "time": "2024-01-29T20:11:03+00:00" }, + { + "name": "symfony/polyfill-php82", + "version": "v1.29.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-php82.git", + "reference": "559d488c38784112c78b9bf17c5ce8366a265643" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-php82/zipball/559d488c38784112c78b9bf17c5ce8366a265643", + "reference": "559d488c38784112c78b9bf17c5ce8366a265643", + "shasum": "" + }, + "require": { + "php": ">=7.1" + }, + "type": "library", + "extra": { + "thanks": { + "name": "symfony/polyfill", + "url": "https://github.com/symfony/polyfill" + } + }, + "autoload": { + "files": [ + "bootstrap.php" + ], + "psr-4": { + "Symfony\\Polyfill\\Php82\\": "" + }, + "classmap": [ + "Resources/stubs" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill backporting some PHP 8.2+ features to lower PHP versions", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "polyfill", + "portable", + "shim" + ], + "support": { + "source": "https://github.com/symfony/polyfill-php82/tree/v1.29.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2024-01-29T20:11:03+00:00" + }, { "name": "symfony/service-contracts", "version": "v2.5.3", diff --git a/dev/tests/unit/Mage/Uploader/Helper/FileTest.php b/dev/tests/unit/Mage/Uploader/Helper/FileTest.php new file mode 100644 index 00000000000..72db51d7ec1 --- /dev/null +++ b/dev/tests/unit/Mage/Uploader/Helper/FileTest.php @@ -0,0 +1,130 @@ +setNode('global/mime/types/test-new-node', 'application/octet-stream'); + $this->subject = Mage::helper('uploader/file'); + } + + /** + * @dataProvider provideGetMimeTypeFromExtensionListData + * @param array $expectedResult + * @param string|array $extensionsList + * @return void + */ + public function testGetMimeTypeFromExtensionList(array $expectedResult, $extensionsList): void + { + self::assertSame($expectedResult, $this->subject->getMimeTypeFromExtensionList($extensionsList)); + } + + /** + * @return array|string>> + */ + public function provideGetMimeTypeFromExtensionListData(): array + { + return [ + 'string exists' => [ + [ + 0 => 'application/vnd.lotus-1-2-3' + ], + '123' + ], + 'string not exists' => [ + [ + 0 => 'application/octet-stream' + ], + 'not-exists' + ], + 'array' => [ + [ + 0 => 'application/vnd.lotus-1-2-3', + 1 => 'application/octet-stream', + 2 => 'application/octet-stream', + ], + [ + '123', + 'not-exists', + 'test-new-node', + ] + ], + ]; + } + + public function testGetPostMaxSize(): void + { + self::assertIsString($this->subject->getPostMaxSize()); + } + + public function testGetUploadMaxSize(): void + { + self::assertIsString($this->subject->getUploadMaxSize()); + } + + public function testGetDataMaxSize(): void + { + $mock = $this->getMockBuilder(Mage_Uploader_Helper_File::class) + ->setMethods(['getPostMaxSize', 'getUploadMaxSize']) + ->getMock(); + + $mock->expects($this->once())->method('getPostMaxSize')->willReturn('1G'); + $mock->expects($this->once())->method('getUploadMaxSize')->willReturn('1M'); + self::assertSame('1M', $mock->getDataMaxSize()); + } + + /** + * @dataProvider provideGetDataMaxSizeInBytesData + * @param int $expectedResult + * @param string $maxSize + * @return void + */ + public function testGetDataMaxSizeInBytes(int $expectedResult, string $maxSize): void + { + $mock = $this->getMockBuilder(Mage_Uploader_Helper_File::class) + ->setMethods(['getDataMaxSize']) + ->getMock(); + + $mock->expects($this->once())->method('getDataMaxSize')->willReturn($maxSize); + self::assertSame($expectedResult, $mock->getDataMaxSizeInBytes()); + } + + /** + * @return array> + */ + public function provideGetDataMaxSizeInBytesData(): array + { + return [ + 'no unit' => [ + 1024, + '1024' + ], + 'kilobyte' => [ + 1024, + '1K' + ], + 'megabyte' => [ + 1048576, + '1M' + ], + 'gigabyte' => [ + 1073741824, + '1G' + ] + ]; + } +}