Browse Source

Fix #19243: Handle `finfo_open` for tar.xz as `application/octet-stream` on PHP 8.1

master
Long TRAN 3 years ago committed by GitHub
parent
commit
7b8c29d874
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      build/controllers/MimeTypeController.php
  2. 3
      framework/CHANGELOG.md
  3. 4
      framework/helpers/BaseFileHelper.php
  4. 8
      framework/helpers/mimeTypes.php
  5. 18
      tests/framework/helpers/FileHelperTest.php
  6. 12
      tests/framework/helpers/MimeTest.php
  7. 10
      tests/framework/validators/FileValidatorTest.php

5
build/controllers/MimeTypeController.php

@ -99,6 +99,11 @@ class MimeTypeController extends Controller
$mimeMap = array_merge($mimeMap, $this->additionalMimeTypes); $mimeMap = array_merge($mimeMap, $this->additionalMimeTypes);
ksort($mimeMap); ksort($mimeMap);
$array = VarDumper::export($mimeMap); $array = VarDumper::export($mimeMap);
if (PHP_VERSION_ID >= 80100) {
$array = array_replace($array, array('xz' => 'application/octet-stream'));
}
$content = <<<EOD $content = <<<EOD
<?php <?php
/** /**

3
framework/CHANGELOG.md

@ -4,9 +4,10 @@ Yii Framework 2 Change Log
2.0.46 under development 2.0.46 under development
------------------------ ------------------------
- no changes in this release. - Bug #19243: Handle `finfo_open` for tar.xz as `application/octet-stream` on PHP 8.1 (longthanhtran)
- Bug #19235: Fix return type compatibility of `yii\web\SessionIterator` class methods for PHP 8.1 (virtual-designer) - Bug #19235: Fix return type compatibility of `yii\web\SessionIterator` class methods for PHP 8.1 (virtual-designer)
2.0.45 February 11, 2022 2.0.45 February 11, 2022
------------------------ ------------------------

4
framework/helpers/BaseFileHelper.php

@ -162,10 +162,6 @@ class BaseFileHelper
throw new InvalidConfigException('The fileinfo PHP extension is not installed.'); throw new InvalidConfigException('The fileinfo PHP extension is not installed.');
} }
if (PHP_VERSION_ID >= 80100) {
return static::getMimeTypeByExtension($file, $magicFile);
}
$info = finfo_open(FILEINFO_MIME_TYPE, $magicFile); $info = finfo_open(FILEINFO_MIME_TYPE, $magicFile);
if ($info) { if ($info) {

8
framework/helpers/mimeTypes.php

@ -8,7 +8,7 @@
* http://svn.apache.org/viewvc/httpd/httpd/trunk/docs/conf/mime.types?view=markup * http://svn.apache.org/viewvc/httpd/httpd/trunk/docs/conf/mime.types?view=markup
* This file has been placed in the public domain for unlimited redistribution. * This file has been placed in the public domain for unlimited redistribution.
*/ */
return [ $mimeTypes = [
'3dml' => 'text/vnd.in3d.3dml', '3dml' => 'text/vnd.in3d.3dml',
'3ds' => 'image/x-3ds', '3ds' => 'image/x-3ds',
'3g2' => 'video/3gpp2', '3g2' => 'video/3gpp2',
@ -995,3 +995,9 @@ return [
'zirz' => 'application/vnd.zul', 'zirz' => 'application/vnd.zul',
'zmm' => 'application/vnd.handheld-entertainment+xml', 'zmm' => 'application/vnd.handheld-entertainment+xml',
]; ];
if (PHP_VERSION_ID >= 80100) {
$mimeTypes = array_replace($mimeTypes, array('xz' => 'application/octet-stream'));
}
return $mimeTypes;

18
tests/framework/helpers/FileHelperTest.php

@ -717,6 +717,24 @@ class FileHelperTest extends TestCase
$this->assertTrue(in_array(FileHelper::getMimeType($file), ['application/json', 'text/plain'])); $this->assertTrue(in_array(FileHelper::getMimeType($file), ['application/json', 'text/plain']));
} }
public function testGetUploadedImageMimeTypes()
{
$ds = DIRECTORY_SEPARATOR;
$phpunitPath = Yii::getAlias('@yiiunit');
$runtimeLocation = Yii::getAlias('@yiiunit/runtime');
$resourceSourceLocation = "{$phpunitPath}{$ds}framework{$ds}validators{$ds}data{$ds}mimeType";
$pngFile = "{$runtimeLocation}{$ds}php1234";
copy("{$resourceSourceLocation}{$ds}test.png", $pngFile);
$this->assertEquals('image/png', FileHelper::getMimeType($pngFile));
$jpgFile = "{$runtimeLocation}{$ds}php4567";
copy("{$resourceSourceLocation}{$ds}test.jpg", $jpgFile);
$this->assertEquals('image/jpeg', FileHelper::getMimeType($jpgFile));
}
public function testNormalizePath() public function testNormalizePath()
{ {
$ds = DIRECTORY_SEPARATOR; $ds = DIRECTORY_SEPARATOR;

12
tests/framework/helpers/MimeTest.php

@ -37,8 +37,8 @@ class MimeTest extends TestCase
public function testMimeTypes() public function testMimeTypes()
{ {
$this->assertSame(
[ $coreMimeTypes = [
'3dml' => 'text/vnd.in3d.3dml', '3dml' => 'text/vnd.in3d.3dml',
'3ds' => 'image/x-3ds', '3ds' => 'image/x-3ds',
'3g2' => 'video/3gpp2', '3g2' => 'video/3gpp2',
@ -1024,7 +1024,13 @@ class MimeTest extends TestCase
'zir' => 'application/vnd.zul', 'zir' => 'application/vnd.zul',
'zirz' => 'application/vnd.zul', 'zirz' => 'application/vnd.zul',
'zmm' => 'application/vnd.handheld-entertainment+xml', 'zmm' => 'application/vnd.handheld-entertainment+xml',
], ];
if (PHP_VERSION_ID >= 80100) {
$coreMimeTypes = array_replace($coreMimeTypes, array('xz' => 'application/octet-stream'));
}
$this->assertSame($coreMimeTypes,
require __DIR__ . '/../../../framework/helpers/mimeTypes.php' require __DIR__ . '/../../../framework/helpers/mimeTypes.php'
); );
} }

10
tests/framework/validators/FileValidatorTest.php

@ -535,7 +535,7 @@ class FileValidatorTest extends TestCase
public function validMimeTypes() public function validMimeTypes()
{ {
return array_filter([ $validMimeTypes = array_filter([
['test.svg', 'image/*', 'svg'], ['test.svg', 'image/*', 'svg'],
['test.jpg', 'image/*', 'jpg'], ['test.jpg', 'image/*', 'jpg'],
['test.png', 'image/*', 'png'], ['test.png', 'image/*', 'png'],
@ -545,6 +545,14 @@ class FileValidatorTest extends TestCase
['test.odt', 'application/vnd*', 'odt'], ['test.odt', 'application/vnd*', 'odt'],
['test.tar.xz', 'application/x-xz', 'tar.xz'], ['test.tar.xz', 'application/x-xz', 'tar.xz'],
]); ]);
if (PHP_VERSION_ID >= 80100) {
$v81_zx = ['test.tar.xz', 'application/octet-stream', 'tar.xz'];
array_pop($validMimeTypes);
$validMimeTypes[] = $v81_zx;
}
return $validMimeTypes;
} }
public function invalidMimeTypes() public function invalidMimeTypes()

Loading…
Cancel
Save