Make WordPress Core

Ticket #22363: 22363.11.patch

File 22363.11.patch, 8.1 KB (added by markoheijnen, 9 years ago)
  • src/wp-includes/default-filters.php

     
    190190add_filter( 'teeny_mce_before_init',    '_mce_set_direction'                  );
    191191add_filter( 'pre_kses',                 'wp_pre_kses_less_than'               );
    192192add_filter( 'sanitize_title',           'sanitize_title_with_dashes',   10, 3 );
     193add_filter( 'sanitize_file_name',       'remove_accents'                      );
    193194add_action( 'check_comment_flood',      'check_comment_flood_db',       10, 3 );
    194195add_filter( 'comment_flood_filter',     'wp_throttle_comment_flood',    10, 3 );
    195196add_filter( 'pre_comment_content',      'wp_rel_nofollow',              15    );
  • src/wp-includes/formatting.php

     
    13581358}
    13591359
    13601360/**
    1361  * Sanitizes a filename, replacing whitespace with dashes.
     1361 * Sanitizes a filename, replacing whitespace and illegal characters with dashes.
    13621362 *
    1363  * Removes special characters that are illegal in filenames on certain
    1364  * operating systems and special characters requiring special escaping
    1365  * to manipulate at the command line. Replaces spaces and consecutive
    1366  * dashes with a single dash. Trims period, dash and underscore from beginning
    1367  * and end of filename.
     1363 * Replaces all non-alphabetical, non-decimal characters (including
     1364 * spaces) with dashes. Strips HTML tags and sanitizes HTML entities. Munges
     1365 * extraneous file extensions with underscores. Converts the filenames to lowercase
     1366 * when possible.
    13681367 *
     1368 * If the PCRE UTF-8 extension is available, this function converts all characters
     1369 * that don't have the Unicode property "Letter" or "Decimal number" to dashes.
     1370 *
    13691371 * @since 2.1.0
    13701372 *
    13711373 * @param string $filename The filename to be sanitized
     
    13731375 */
    13741376function sanitize_file_name( $filename ) {
    13751377        $filename_raw = $filename;
     1378        $pcre_utf8 = _wp_can_use_pcre_u();
     1379        $encoding = seems_utf8( $filename ) ? 'UTF-8' : get_bloginfo( 'charset' );
     1380        $utf8_modifier = ( $pcre_utf8 && 'UTF-8' == $encoding ) ? 'u' : '';
     1381
     1382        $filename = wp_strip_all_tags( $filename );
     1383
     1384        // Decode all HTML entities available in current encoding and strip the rest
     1385        $filename = html_entity_decode( $filename, ENT_QUOTES, $encoding );
     1386        $filename = preg_replace( "`&[a-zA-Z]{2,8};`$utf8_modifier", '', $filename );
     1387
     1388        // Convert illegal characters to dashes
    13761389        $special_chars = array("?", "[", "]", "/", "\\", "=", "<", ">", ":", ";", ",", "'", "\"", "&", "$", "#", "*", "(", ")", "|", "~", "`", "!", "{", "}", "%", "+", chr(0));
    13771390        /**
    13781391         * Filter the list of characters to remove from a filename.
     
    13831396         * @param string $filename_raw  Filename as it was passed into sanitize_file_name().
    13841397         */
    13851398        $special_chars = apply_filters( 'sanitize_file_name_chars', $special_chars, $filename_raw );
    1386         $filename = preg_replace( "#\x{00a0}#siu", ' ', $filename );
    1387         $filename = str_replace( $special_chars, '', $filename );
    1388         $filename = str_replace( array( '%20', '+' ), '-', $filename );
    1389         $filename = preg_replace( '/[\r\n\t -]+/', '-', $filename );
     1399        $strip_characters = preg_quote( implode( '', $special_chars ), '`' );
     1400        $filename = preg_replace( "`[$strip_characters]`$utf8_modifier", '-', $filename );
     1401
     1402        if ( $pcre_utf8 ) {
     1403                // Convert everything except letters, decimal numbers, and "." (dot) to dashes if the PCRE UTF-8 extension is available
     1404                $filename = preg_replace( "`(?!\.)[^\p{L}\p{Nd}]+`$utf8_modifier", '-', $filename );
     1405
     1406                if ( ! $filename ) { // Invalid UTF-8 string or empty
     1407                        return '';
     1408                }
     1409        }
     1410
     1411        $filename = preg_replace( "`[\s-]+`$utf8_modifier", '-', $filename ); // Check whitespace and multiple dashes
     1412        $filename = preg_replace( "`-\.`$utf8_modifier", '.', $filename );  // Trim dashes before a dot
    13901413        $filename = trim( $filename, '.-_' );
    13911414
     1415        if ( function_exists( 'mb_strtolower' ) ) {
     1416                $filename = mb_strtolower( $filename, mb_detect_encoding( $filename ) );
     1417        }
     1418        else {
     1419                $filename = strtolower( $filename );
     1420        }
     1421
    13921422        // Split the filename into a base and extension[s]
    13931423        $parts = explode('.', $filename);
    13941424
     
    14171447        foreach ( (array) $parts as $part) {
    14181448                $filename .= '.' . $part;
    14191449
    1420                 if ( preg_match("/^[a-zA-Z]{2,5}\d?$/", $part) ) {
     1450                if ( preg_match("`^[a-zA-Z]{2,5}\d?$`$utf8_modifier", $part) ) {
    14211451                        $allowed = false;
    14221452                        foreach ( $mimes as $ext_preg => $mime_match ) {
    1423                                 $ext_preg = '!^(' . $ext_preg . ')$!i';
     1453                                $ext_preg = "`^($ext_preg)$`i$utf8_modifier";
    14241454                                if ( preg_match( $ext_preg, $part ) ) {
    14251455                                        $allowed = true;
    14261456                                        break;
  • tests/phpunit/tests/formatting/SanitizeFileName.php

     
    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        /**
     
    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
     
    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        }
     59       
     60        function test_replaces_utf8_whitespace() {
     61                if ( ! $this->pcre_utf8_support() ) {
     62                        $this->markTestSkipped();
     63                }
     64
     65                $this->assertEquals("non-breaking", sanitize_file_name("non\xc2\xa0breaking"));
     66        }
     67       
     68        function test_returns_lowercase() {
     69                if ( ! function_exists( 'mb_strtolower' ) ) {
     70                        $this->markTestSkipped();
     71                }
     72
     73                $this->assertEquals( 'abcd', sanitize_file_name('ABCD') );
     74        }
     75       
     76        function test_replaces_accents() {
     77                $in  = 'àáâãäåæçèéêëìíîïñòóôõöøùúûüýÿ';
     78                $out = 'aaaaaaaeceeeeiiiinoooooouuuuyy';
     79                $this->assertEquals( $out, sanitize_file_name( $in ) );
     80        }
     81       
     82        function test_strips_non_alpha_html_entities() {
     83                if ( ! $this->pcre_utf8_support() ) {
     84                        $this->markTestSkipped();
     85                }
     86
     87                $this->assertEquals("start-end", sanitize_file_name( "start &copy; &amp; &euro; &ndash; &nbsp; end" ) );
     88                $this->assertEquals("start-end", sanitize_file_name( "start &invalid; end" ) );
     89        }
     90       
     91        function test_converts_alpha_html_entities() {
     92                if ( ! $this->pcre_utf8_support() ) {
     93                        $this->markTestSkipped();
     94                }
     95
     96                $this->assertEquals("start-a-e-n-o-ae-end", sanitize_file_name("start &agrave; &ecirc; &ntilde; &oslash; &aelig; end" ) );
     97        }
     98       
     99        function test_removes_non_alphanum_characters() {
     100                if ( ! $this->pcre_utf8_support() ) {
     101                        $this->markTestSkipped();
     102                }
     103
     104                $test_input = "start ¿”©“? «±€—°» middle “√¼ = ♫” & „☺ + ☻ = ♥‟ end";
     105                $this->assertEquals("start-middle-end", sanitize_file_name( $test_input ) );
     106        }
     107       
     108        function test_returns_empty_on_invalid_utf8() {
     109                if ( ! $this->pcre_utf8_support() ) {
     110                        $this->markTestSkipped();
     111                }
     112
     113                $this->assertEquals( '', sanitize_file_name( "Invalid \xff utf8" ) );
     114        }
     115
     116        function test_returns_lowercase_utf8() {
     117                if ( ! $this->pcre_utf8_support() || ! function_exists( 'mb_strtolower' ) ) {
     118                        $this->markTestSkipped();
     119                }
     120
     121                $this->assertEquals( 'βασιλ-правах', sanitize_file_name( 'ΒΑΣΙΛ ПРАВАХ' ) );
     122        }
     123
     124        private function pcre_utf8_support() {
     125                static $support = null;
     126
     127                if ( is_null($support) ) {
     128                        $support = _wp_can_use_pcre_u();
     129                }
     130
     131                return $support;
     132        }
     133
    59134}