Fixes #17235: yii\helpers\FileHelper::normalizePath() now accepts stream wrappers

This commit is contained in:
Razvan Grigore
2019-03-29 16:26:31 +01:00
committed by Alexander Makarov
parent e9b33916b6
commit 9c37016dac
3 changed files with 13 additions and 0 deletions

View File

@ -5,6 +5,7 @@ Yii Framework 2 Change Log
------------------------ ------------------------
- Bug #17220: Fixed error when using non-InputWidget in active form field (s1lver) - Bug #17220: Fixed error when using non-InputWidget in active form field (s1lver)
- Bug #17235: `yii\helpers\FileHelper::normalizePath()` now accepts stream wrappers (razvanphp)
2.0.17 March 22, 2019 2.0.17 March 22, 2019

View File

@ -50,6 +50,9 @@ class BaseFileHelper
* - Turn multiple consecutive slashes into a single one (e.g. "/a///b/c" becomes "/a/b/c") * - Turn multiple consecutive slashes into a single one (e.g. "/a///b/c" becomes "/a/b/c")
* - Remove ".." and "." based on their meanings (e.g. "/a/./b/../c" becomes "/a/c") * - Remove ".." and "." based on their meanings (e.g. "/a/./b/../c" becomes "/a/c")
* *
* Note: For registered stream wrappers, the consecutive slashes rule
* and ".."/"." translations are skipped.
*
* @param string $path the file/directory path to be normalized * @param string $path the file/directory path to be normalized
* @param string $ds the directory separator to be used in the normalized result. Defaults to `DIRECTORY_SEPARATOR`. * @param string $ds the directory separator to be used in the normalized result. Defaults to `DIRECTORY_SEPARATOR`.
* @return string the normalized file/directory path * @return string the normalized file/directory path
@ -60,6 +63,12 @@ class BaseFileHelper
if (strpos($ds . $path, "{$ds}.") === false && strpos($path, "{$ds}{$ds}") === false) { if (strpos($ds . $path, "{$ds}.") === false && strpos($path, "{$ds}{$ds}") === false) {
return $path; return $path;
} }
// fix #17235 stream wrappers
foreach (stream_get_wrappers() as $protocol) {
if (strpos($path, "{$protocol}://") === 0) {
return $path;
}
}
// the path may contain ".", ".." or double slashes, need to clean them up // the path may contain ".", ".." or double slashes, need to clean them up
if (strpos($path, "{$ds}{$ds}") === 0 && $ds == '\\') { if (strpos($path, "{$ds}{$ds}") === 0 && $ds == '\\') {
$parts = [$ds]; $parts = [$ds];

View File

@ -745,6 +745,9 @@ class FileHelperTest extends TestCase
// https://github.com/yiisoft/yii2/issues/13034 // https://github.com/yiisoft/yii2/issues/13034
$this->assertEquals('\\\\server\share\path\file', FileHelper::normalizePath('\\\\server\share\path\file', '\\')); $this->assertEquals('\\\\server\share\path\file', FileHelper::normalizePath('\\\\server\share\path\file', '\\'));
// Stream Wrappers should not have the double slashes stripped
// https://github.com/yiisoft/yii2/issues/17235
$this->assertEquals('ftp://192.168.1.100/test', FileHelper::normalizePath('ftp://192.168.1.100/test/'));
} }
public function testLocalizedDirectory() public function testLocalizedDirectory()