From c7783dc49a016b98480d2966b14a59674bf41588 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Fri, 24 Jul 2020 18:01:48 +0000 Subject: [PATCH] Media: Remove accents in `sanitize_file_name()`. This brings some consistency with `sanitize_title()` and `sanitize_user()`. Props tar.gz, NumidWasNotAvailable, juliobox, p_enrique, cristovaov, zodiac1978, mikeschroder, markoheijnen, chriscct7, swissspidy, DrProtocols, pento, gitlost, joemcgill, dustinbolton, programmin, Vayu, MaximeCulea, lucasbustamante, nilovelez, RavanH, audrasjb, SergeyBiryukov. See #22363. git-svn-id: https://develop.svn.wordpress.org/trunk@48603 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/formatting.php | 15 ++++++++++----- .../phpunit/tests/formatting/SanitizeFileName.php | 15 ++++++++++++--- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php index 9dba689b32..28b65135a0 100644 --- a/src/wp-includes/formatting.php +++ b/src/wp-includes/formatting.php @@ -1984,7 +1984,9 @@ function remove_accents( $string ) { * @return string The sanitized filename. */ function sanitize_file_name( $filename ) { - $filename_raw = $filename; + $filename_raw = $filename; + $filename = remove_accents( $filename ); + $special_chars = array( '?', '[', ']', '/', '\\', '=', '<', '>', ':', ';', ',', "'", '"', '&', '$', '#', '*', '(', ')', '|', '~', '`', '!', '{', '}', '%', '+', '’', '«', '»', '”', '“', chr( 0 ) ); // Check for support for utf8 in the installed PCRE library once and store the result in a static. @@ -2013,10 +2015,11 @@ function sanitize_file_name( $filename ) { * @param string $filename_raw The original filename to be sanitized. */ $special_chars = apply_filters( 'sanitize_file_name_chars', $special_chars, $filename_raw ); - $filename = str_replace( $special_chars, '', $filename ); - $filename = str_replace( array( '%20', '+' ), '-', $filename ); - $filename = preg_replace( '/[\r\n\t -]+/', '-', $filename ); - $filename = trim( $filename, '.-_' ); + + $filename = str_replace( $special_chars, '', $filename ); + $filename = str_replace( array( '%20', '+' ), '-', $filename ); + $filename = preg_replace( '/[\r\n\t -]+/', '-', $filename ); + $filename = trim( $filename, '.-_' ); if ( false === strpos( $filename, '.' ) ) { $mime_types = wp_get_mime_types(); @@ -2068,7 +2071,9 @@ function sanitize_file_name( $filename ) { } } } + $filename .= '.' . $extension; + /** This filter is documented in wp-includes/formatting.php */ return apply_filters( 'sanitize_file_name', $filename, $filename_raw ); } diff --git a/tests/phpunit/tests/formatting/SanitizeFileName.php b/tests/phpunit/tests/formatting/SanitizeFileName.php index 76c0d9705c..3e01469329 100644 --- a/tests/phpunit/tests/formatting/SanitizeFileName.php +++ b/tests/phpunit/tests/formatting/SanitizeFileName.php @@ -20,12 +20,21 @@ class Tests_Formatting_SanitizeFileName extends WP_UnitTestCase { $this->assertEquals( 'testtest', sanitize_file_name( $string ) ); } + /** + * @ticket 22363 + */ + function test_removes_accents() { + $in = 'àáâãäåæçèéêëìíîïñòóôõöøùúûüýÿ'; + $out = 'aaaaaaaeceeeeiiiinoooooouuuuyy'; + $this->assertEquals( $out, sanitize_file_name( $in ) ); + } + /** * Test that spaces are correctly replaced with dashes. * * @ticket 16330 */ - function test_replace_spaces() { + function test_replaces_spaces() { $urls = array( 'unencoded space.png' => 'unencoded-space.png', 'encoded-space.jpg' => 'encoded-space.jpg', @@ -58,13 +67,13 @@ class Tests_Formatting_SanitizeFileName extends WP_UnitTestCase { $this->assertEquals( 'a22b.jpg', sanitize_file_name( 'a%22b.jpg' ) ); } - function test_replaces_unnammed_file_extensions() { + function test_replaces_unnamed_file_extensions() { // Test filenames with both supported and unsupported extensions. $this->assertEquals( 'unnamed-file.exe', sanitize_file_name( '_.exe' ) ); $this->assertEquals( 'unnamed-file.jpg', sanitize_file_name( '_.jpg' ) ); } - function test_replaces_unnammed_file_extensionless() { + function test_replaces_unnamed_file_extensionless() { // Test a filenames that becomes extensionless. $this->assertEquals( 'no-extension', sanitize_file_name( '_.no-extension' ) ); }