From 8a617607a713541f1f4632a3148b60ac9385241e Mon Sep 17 00:00:00 2001 From: Andrew Nacin Date: Sat, 1 Mar 2014 21:44:43 +0000 Subject: [PATCH] Strip backslashes, not just forward slashes, from untrailingslashit(). trailingslashit() will now remove any forward or backslashes from the end of a string before appending a forward slash. props knutsp, willmot. fixes #22267. git-svn-id: https://develop.svn.wordpress.org/trunk@27344 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/formatting.php | 19 +++++++++--------- src/wp-includes/functions.php | 6 +++--- tests/phpunit/tests/formatting/Slashit.php | 23 ++++++++++++++++++++++ 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php index 1e8c59835d..9edea6a704 100644 --- a/src/wp-includes/formatting.php +++ b/src/wp-includes/formatting.php @@ -1482,35 +1482,34 @@ function backslashit($string) { /** * Appends a trailing slash. * - * Will remove trailing slash if it exists already before adding a trailing - * slash. This prevents double slashing a string or path. + * Will remove trailing forward and backslashes if it exists already before adding + * a trailing forward slash. This prevents double slashing a string or path. * * The primary use of this is for paths and thus should be used for paths. It is * not restricted to paths and offers no specific path support. * * @since 1.2.0 - * @uses untrailingslashit() Unslashes string if it was slashed already. * * @param string $string What to add the trailing slash to. * @return string String with trailing slash added. */ -function trailingslashit($string) { - return untrailingslashit($string) . '/'; +function trailingslashit( $string ) { + return untrailingslashit( $string ) . '/'; } /** - * Removes trailing slash if it exists. + * Removes trailing forward slashes and backslashes if they exist. * * The primary use of this is for paths and thus should be used for paths. It is * not restricted to paths and offers no specific path support. * * @since 2.2.0 * - * @param string $string What to remove the trailing slash from. - * @return string String without the trailing slash. + * @param string $string What to remove the trailing slashes from. + * @return string String without the trailing slashes. */ -function untrailingslashit($string) { - return rtrim($string, '/'); +function untrailingslashit( $string ) { + return rtrim( $string, '/\\' ); } /** diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index c5a9fea79f..0e34a5e5d4 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -1455,17 +1455,17 @@ function get_temp_dir() { return trailingslashit(WP_TEMP_DIR); if ( $temp ) - return trailingslashit( rtrim( $temp, '\\' ) ); + return trailingslashit( $temp ); if ( function_exists('sys_get_temp_dir') ) { $temp = sys_get_temp_dir(); if ( @is_dir( $temp ) && wp_is_writable( $temp ) ) - return trailingslashit( rtrim( $temp, '\\' ) ); + return trailingslashit( $temp ); } $temp = ini_get('upload_tmp_dir'); if ( @is_dir( $temp ) && wp_is_writable( $temp ) ) - return trailingslashit( rtrim( $temp, '\\' ) ); + return trailingslashit( $temp ); $temp = WP_CONTENT_DIR . '/'; if ( is_dir( $temp ) && wp_is_writable( $temp ) ) diff --git a/tests/phpunit/tests/formatting/Slashit.php b/tests/phpunit/tests/formatting/Slashit.php index 9db62a5f91..4ba3382738 100644 --- a/tests/phpunit/tests/formatting/Slashit.php +++ b/tests/phpunit/tests/formatting/Slashit.php @@ -21,6 +21,22 @@ class Tests_Formatting_Slashit extends WP_UnitTestCase { $this->assertEquals("a", untrailingslashit("a////")); } + /** + * @ticket 22267 + */ + function test_removes_trailing_backslashes() { + $this->assertEquals("a", untrailingslashit("a\\")); + $this->assertEquals("a", untrailingslashit("a\\\\\\\\")); + } + + /** + * @ticket 22267 + */ + function test_removes_trailing_mixed_slashes() { + $this->assertEquals("a", untrailingslashit("a/\\")); + $this->assertEquals("a", untrailingslashit("a\\/\\///\\\\//")); + } + function test_adds_trailing_slash() { $this->assertEquals("a/", trailingslashit("a")); } @@ -28,4 +44,11 @@ class Tests_Formatting_Slashit extends WP_UnitTestCase { function test_does_not_add_trailing_slash_if_one_exists() { $this->assertEquals("a/", trailingslashit("a/")); } + + /** + * @ticket 22267 + */ + function test_converts_trailing_backslash_to_slash_if_one_exists() { + $this->assertEquals("a/", trailingslashit("a\\")); + } }