Make WordPress Core

Ticket #22363: 22363.diff

File 22363.diff, 8.9 KB (added by swissspidy, 9 years ago)
  • src/wp-includes/default-filters.php

    diff --git src/wp-includes/default-filters.php src/wp-includes/default-filters.php
    index 3402e48..c7ebe29 100644
    add_filter( 'tiny_mce_before_init', '_mce_set_direction' ); 
    197197add_filter( 'teeny_mce_before_init',    '_mce_set_direction'                  );
    198198add_filter( 'pre_kses',                 'wp_pre_kses_less_than'               );
    199199add_filter( 'sanitize_title',           'sanitize_title_with_dashes',   10, 3 );
     200add_filter( 'sanitize_file_name',       'remove_accents'                      );
    200201add_action( 'check_comment_flood',      'check_comment_flood_db',       10, 3 );
    201202add_filter( 'comment_flood_filter',     'wp_throttle_comment_flood',    10, 3 );
    202203add_filter( 'pre_comment_content',      'wp_rel_nofollow',              15    );
  • src/wp-includes/formatting.php

    diff --git src/wp-includes/formatting.php src/wp-includes/formatting.php
    index 37298ec..d86addc 100644
    function remove_accents( $string ) { 
    17271727}
    17281728
    17291729/**
    1730  * Sanitizes a filename, replacing whitespace with dashes.
     1730 * Sanitizes a file name, replacing whitespace and illegal characters with dashes.
    17311731 *
    1732  * Removes special characters that are illegal in filenames on certain
    1733  * operating systems and special characters requiring special escaping
    1734  * to manipulate at the command line. Replaces spaces and consecutive
    1735  * dashes with a single dash. Trims period, dash and underscore from beginning
    1736  * and end of filename. It is not guaranteed that this function will return a
    1737  * filename that is allowed to be uploaded.
     1732 * Replaces all non-alphabetical, non-decimal characters (including spaces) with dashes.
     1733 * Strips HTML tags and sanitizes HTML entities.  Munges extraneous file extensions with underscores.
     1734 * Converts the file names to lowercase when possible.
     1735 *
     1736 * If the PCRE UTF-8 extension is available, this function converts all characters
     1737 * that don't have the Unicode property "Letter" or "Decimal number" to dashes.
    17381738 *
    17391739 * @since 2.1.0
     1740 * @since 4.7.0 The function now also replaces illegal characters with dashes.
    17401741 *
    17411742 * @param string $filename The filename to be sanitized
    17421743 * @return string The sanitized filename
    17431744 */
    17441745function sanitize_file_name( $filename ) {
    17451746        $filename_raw = $filename;
     1747        $encoding = seems_utf8( $filename ) ? 'UTF-8' : get_bloginfo( 'charset' );
     1748        $utf8_modifier = ( _wp_can_use_pcre_u() && 'UTF-8' == $encoding ) ? 'u' : '';
     1749
     1750        $filename = wp_strip_all_tags( $filename );
     1751
     1752        // Decode all HTML entities available in current encoding and strip the rest
     1753        $filename = html_entity_decode( $filename, ENT_QUOTES, $encoding );
     1754        $filename = preg_replace( "`&[a-zA-Z]{2,8};`$utf8_modifier", '', $filename );
     1755
     1756        // Convert illegal characters to dashes
    17461757        $special_chars = array("?", "[", "]", "/", "\\", "=", "<", ">", ":", ";", ",", "'", "\"", "&", "$", "#", "*", "(", ")", "|", "~", "`", "!", "{", "}", "%", "+", chr(0));
    17471758        /**
    17481759         * Filters the list of characters to remove from a filename.
    function sanitize_file_name( $filename ) { 
    17521763         * @param array  $special_chars Characters to remove.
    17531764         * @param string $filename_raw  Filename as it was passed into sanitize_file_name().
    17541765         */
    1755         $special_chars = apply_filters( 'sanitize_file_name_chars', $special_chars, $filename_raw );
    1756         $filename = preg_replace( "#\x{00a0}#siu", ' ', $filename );
    1757         $filename = str_replace( $special_chars, '', $filename );
    1758         $filename = str_replace( array( '%20', '+' ), '-', $filename );
    1759         $filename = preg_replace( '/[\r\n\t -]+/', '-', $filename );
     1766        $special_chars    = apply_filters( 'sanitize_file_name_chars', $special_chars, $filename_raw );
     1767        $strip_characters = preg_quote( implode( '', $special_chars ), '`' );
     1768        $filename         = preg_replace( "`[$strip_characters]`$utf8_modifier", '-', $filename );
     1769
     1770        if ( _wp_can_use_pcre_u() ) {
     1771                // Convert everything except letters, decimal numbers, and "." (dot) to dashes if the PCRE UTF-8 extension is available
     1772                $filename = preg_replace( "`(?!\.)[^\p{L}\p{Nd}]+`$utf8_modifier", '-', $filename );
     1773                if ( ! $filename ) { // Invalid UTF-8 string or empty
     1774                        return '';
     1775                }
     1776        }
     1777
     1778        $filename = preg_replace( "`[\s-]+`$utf8_modifier", '-', $filename ); // Check whitespace and multiple dashes
     1779        $filename = preg_replace( "`-\.`$utf8_modifier", '.', $filename );  // Trim dashes before a dot
    17601780        $filename = trim( $filename, '.-_' );
    17611781
     1782        if ( function_exists( 'mb_strtolower' ) ) {
     1783                $filename = mb_strtolower( $filename, mb_detect_encoding( $filename ) );
     1784        } else {
     1785                $filename = strtolower( $filename );
     1786        }
     1787
    17621788        if ( false === strpos( $filename, '.' ) ) {
    17631789                $mime_types = wp_get_mime_types();
    17641790                $filetype = wp_check_filetype( 'test.' . $filename, $mime_types );
    function sanitize_file_name( $filename ) { 
    17951821        foreach ( (array) $parts as $part) {
    17961822                $filename .= '.' . $part;
    17971823
    1798                 if ( preg_match("/^[a-zA-Z]{2,5}\d?$/", $part) ) {
     1824                if ( preg_match("`^[a-zA-Z]{2,5}\d?$`$utf8_modifier", $part) ) {
    17991825                        $allowed = false;
    18001826                        foreach ( $mimes as $ext_preg => $mime_match ) {
    1801                                 $ext_preg = '!^(' . $ext_preg . ')$!i';
     1827                                $ext_preg = "`^($ext_preg)$`i$utf8_modifier";
    18021828                                if ( preg_match( $ext_preg, $part ) ) {
    18031829                                        $allowed = true;
    18041830                                        break;
  • tests/phpunit/tests/formatting/SanitizeFileName.php

    diff --git tests/phpunit/tests/formatting/SanitizeFileName.php tests/phpunit/tests/formatting/SanitizeFileName.php
    index d26b871..cac5336 100644
    class Tests_Formatting_SanitizeFileName extends WP_UnitTestCase { 
    1616                foreach ( $special_chars as $char )
    1717                        $string .= $char;
    1818                $string .= 'test';
    19                 $this->assertEquals( 'testtest', sanitize_file_name( $string ) );
     19                $this->assertEquals( 'test-test', sanitize_file_name( $string ) );
    2020        }
    2121
    2222        /**
    class Tests_Formatting_SanitizeFileName extends WP_UnitTestCase { 
    2828                $urls = array(
    2929                        'unencoded space.png' => 'unencoded-space.png',
    3030                        'encoded-space.jpg' => 'encoded-space.jpg',
    31                         'plus+space.jpg' => 'plusspace.jpg',
     31                        'plus+space.jpg' => 'plus-space.jpg',
    3232                        'multi %20 +space.png' => 'multi-20-space.png',
    3333                );
    3434
    class Tests_Formatting_SanitizeFileName extends WP_UnitTestCase { 
    4747
    4848        function test_replaces_any_amount_of_whitespace_with_one_hyphen() {
    4949                $this->assertEquals("a-t", sanitize_file_name("a          t"));
    50                 $this->assertEquals("a-t", sanitize_file_name("a    \n\n\nt"));
     50                $this->assertEquals("a-t", sanitize_file_name("a    \t\r\n\n\nt"));
    5151        }
    5252
    5353        /**
    5454         * @ticket 16226
    5555         */
    5656        function test_replaces_percent_sign() {
    57                 $this->assertEquals( 'a22b.jpg', sanitize_file_name( 'a%22b.jpg' ) );
     57                $this->assertEquals( 'a-22b.jpg', sanitize_file_name( 'a%22b.jpg' ) );
    5858        }
    5959
    6060        function test_replaces_unnammed_file_extensions() {
    class Tests_Formatting_SanitizeFileName extends WP_UnitTestCase { 
    6767                // Test a filenames that becomes extensionless.
    6868                $this->assertEquals( 'no-extension', sanitize_file_name( '_.no-extension' ) );
    6969        }
     70
     71        function test_replaces_utf8_whitespace() {
     72                if ( ! _wp_can_use_pcre_u() ) {
     73                        $this->markTestSkipped();
     74                }
     75
     76                $this->assertEquals("non-breaking", sanitize_file_name("non\xc2\xa0breaking"));
     77        }
     78
     79        function test_returns_lowercase() {
     80                if ( ! function_exists( 'mb_strtolower' ) ) {
     81                        $this->markTestSkipped();
     82                }
     83
     84                $this->assertEquals( 'abcd', sanitize_file_name('ABCD') );
     85        }
     86
     87        function test_replaces_accents() {
     88                $in  = 'àáâãäåæçèéêëìíîïñòóôõöøùúûüýÿ';
     89                $out = 'aaaaaaaeceeeeiiiinoooooouuuuyy';
     90                $this->assertEquals( $out, sanitize_file_name( $in ) );
     91        }
     92
     93        function test_strips_non_alpha_html_entities() {
     94                if ( ! _wp_can_use_pcre_u() ) {
     95                        $this->markTestSkipped();
     96                }
     97
     98                $this->assertEquals("start-end", sanitize_file_name( "start &copy; &amp; &euro; &ndash; &nbsp; end" ) );
     99                $this->assertEquals("start-end", sanitize_file_name( "start &invalid; end" ) );
     100        }
     101
     102        function test_converts_alpha_html_entities() {
     103                if ( ! _wp_can_use_pcre_u() ) {
     104                        $this->markTestSkipped();
     105                }
     106
     107                $this->assertEquals("start-a-e-n-o-ae-end", sanitize_file_name("start &agrave; &ecirc; &ntilde; &oslash; &aelig; end" ) );
     108        }
     109
     110        function test_removes_non_alphanum_characters() {
     111                if ( ! _wp_can_use_pcre_u() ) {
     112                        $this->markTestSkipped();
     113                }
     114
     115                $test_input = "start ¿”©“? «±€—°» middle “√¼ = ♫” & „☺ + ☻ = ♥‟ end";
     116                $this->assertEquals("start-middle-end", sanitize_file_name( $test_input ) );
     117        }
     118
     119        function test_returns_empty_on_invalid_utf8() {
     120                if ( ! _wp_can_use_pcre_u() ) {
     121                        $this->markTestSkipped();
     122                }
     123
     124                $this->assertEquals( '', sanitize_file_name( "Invalid \xff utf8" ) );
     125        }
     126
     127        function test_returns_lowercase_utf8() {
     128                if ( ! _wp_can_use_pcre_u() || ! function_exists( 'mb_strtolower' ) ) {
     129                        $this->markTestSkipped();
     130                }
     131
     132                $this->assertEquals( 'βασιλ-правах', sanitize_file_name( 'ΒΑΣΙΛ ПРАВАХ' ) );
     133        }
    70134}