From da689558a529510b8c794b2ba8f3652151909d86 Mon Sep 17 00:00:00 2001 From: Dion Hulse Date: Tue, 12 Dec 2017 04:15:54 +0000 Subject: [PATCH] Filesystem: Allow `wp_normalise_path()` to handle PHP stream wrappers such as `php://` correctly. Props calin, dd32. Fixes #42837. git-svn-id: https://develop.svn.wordpress.org/trunk@42387 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/functions.php | 15 ++++++++++++++- tests/phpunit/tests/functions.php | 5 +++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index 23e04cf504..9b5f292eec 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -1764,17 +1764,30 @@ function path_join( $base, $path ) { * @since 3.9.0 * @since 4.4.0 Ensures upper-case drive letters on Windows systems. * @since 4.5.0 Allows for Windows network shares. + * @since 5.0.0 Allows for PHP file wrappers. * * @param string $path Path to normalize. * @return string Normalized path. */ function wp_normalize_path( $path ) { + $wrapper = ''; + if ( wp_is_stream( $path ) ) { + list( $wrapper, $path ) = explode( '://', $path, 2 ); + $wrapper .= '://'; + } + + // Standardise all paths to use / $path = str_replace( '\\', '/', $path ); + + // Replace multiple slashes down to a singular, allowing for network shares having two slashes. $path = preg_replace( '|(?<=.)/+|', '/', $path ); + + // Windows paths should uppercase the drive letter if ( ':' === substr( $path, 1, 1 ) ) { $path = ucfirst( $path ); } - return $path; + + return $wrapper . $path; } /** diff --git a/tests/phpunit/tests/functions.php b/tests/phpunit/tests/functions.php index 329e784dae..52c606537d 100644 --- a/tests/phpunit/tests/functions.php +++ b/tests/phpunit/tests/functions.php @@ -150,6 +150,11 @@ class Tests_Functions extends WP_UnitTestCase { array( '/www/path/', '/www/path/' ), array( '/www/path/////', '/www/path/' ), array( '/www/path', '/www/path' ), + + // PHP Stream wrappers + array( 'php://input', 'php://input' ), + array( 'http://example.com//path.ext', 'http://example.com/path.ext' ), + array( 'file://c:\\www\\path\\', 'file://C:/www/path/' ), ); }