-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14333 from ian4hu/bugfix/issue-13919
Fixes #13919, make response->setFileToSend support non-ASCII filename.
- Loading branch information
Showing
4 changed files
with
136 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
|
||
/** | ||
* This file is part of the Phalcon. | ||
* | ||
* (c) Phalcon Team <team@phalcon.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Phalcon\Helper; | ||
|
||
/** | ||
* This class offers file operation helper | ||
*/ | ||
class Fs { | ||
|
||
/** | ||
* Gets the filename from a given path, Same as PHP's basename() but has non-ASCII support. | ||
* PHP's basename() does not properly support streams or filenames beginning with a non-US-ASCII character. | ||
* see https://bugs.php.net/bug.php?id=37738 | ||
* | ||
* @param string $uri | ||
* @param string $suffix | ||
* | ||
* @return string | ||
*/ | ||
final public static function basename(string! uri, var suffix = null) -> string | ||
{ | ||
var filename, matches; | ||
let uri = rtrim(uri, DIRECTORY_SEPARATOR); | ||
let filename = preg_match( | ||
"@[^" . preg_quote(DIRECTORY_SEPARATOR, "@") . "]+$@", | ||
uri, | ||
matches | ||
) ? matches[0] : ""; | ||
if suffix { | ||
let filename = preg_replace("@" . preg_quote(suffix, "@") . "$@", "", filename); | ||
} | ||
|
||
return filename; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* This file is part of the Phalcon Framework. | ||
* | ||
* (c) Phalcon Team <team@phalconphp.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE.txt | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Phalcon\Test\Unit\Helper\Fs; | ||
|
||
use Phalcon\Helper\Fs; | ||
use UnitTester; | ||
|
||
class FsBasenameCest | ||
{ | ||
/** | ||
* Tests Phalcon\Helper\Fs :: basename() | ||
* with ASCII $uri it should be same as PHP's basename | ||
* | ||
* @author Ian Hu <hu2008yinxiang@163.com> | ||
* @since 2019-08-27 | ||
*/ | ||
public function helperFsBasenamePureASCII(UnitTester $I) | ||
{ | ||
$I->wantToTest('Helper\Fs - basename() with pure ASCII uri'); | ||
$filePathAndSuffixes = [ | ||
["/etc/sudoers.d", ".d"], | ||
["/etc/sudoers.d", ''], | ||
['/etc/passwd', ''], | ||
['/etc/', ''], | ||
['.', ''], | ||
['/', ''] | ||
]; | ||
|
||
foreach ($filePathAndSuffixes as $filePathAndSuffix) { | ||
list($filePath, $suffix) = $filePathAndSuffix; | ||
$I->assertEquals( | ||
basename($filePath, $suffix), | ||
Fs::basename($filePath, $suffix) | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Tests Phalcon\Helper\Fs :: basename() | ||
* with non-ASCII $uri support | ||
* | ||
* @author Ian Hu <hu2008yinxiang@163.com> | ||
* @since 2019-08-27 | ||
*/ | ||
public function helperFsBasenameNonASCII(UnitTester $I) | ||
{ | ||
$I->wantToTest('Helper\Fs - basename() with non-ASCII uri'); | ||
$filePathAndExpects = [ | ||
'/file/热爱中文.txt' => '热爱中文.txt', | ||
'/中文目录/热爱中文.txt' => '热爱中文.txt', | ||
'/myfolder/日本語のファイル名.txt' => '日本語のファイル名.txt', | ||
'/のファ/日本語のファイル名.txt' => '日本語のファイル名.txt', | ||
'/root/ελληνικά.txt' => 'ελληνικά.txt', | ||
'/νικά/ελληνικά.txt' => 'ελληνικά.txt' | ||
]; | ||
foreach ($filePathAndExpects as $filePath => $expect) { | ||
$I->assertEquals( | ||
$expect, | ||
Fs::basename($filePath) | ||
); | ||
} | ||
} | ||
} |