Ticket #22363: 22363.diff
File 22363.diff, 8.9 KB (added by , 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' ); 197 197 add_filter( 'teeny_mce_before_init', '_mce_set_direction' ); 198 198 add_filter( 'pre_kses', 'wp_pre_kses_less_than' ); 199 199 add_filter( 'sanitize_title', 'sanitize_title_with_dashes', 10, 3 ); 200 add_filter( 'sanitize_file_name', 'remove_accents' ); 200 201 add_action( 'check_comment_flood', 'check_comment_flood_db', 10, 3 ); 201 202 add_filter( 'comment_flood_filter', 'wp_throttle_comment_flood', 10, 3 ); 202 203 add_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 ) { 1727 1727 } 1728 1728 1729 1729 /** 1730 * Sanitizes a file name, replacing whitespacewith dashes.1730 * Sanitizes a file name, replacing whitespace and illegal characters with dashes. 1731 1731 * 1732 * Re moves special characters that are illegal in filenames on certain1733 * operating systems and special characters requiring special escaping1734 * to manipulate at the command line. Replaces spaces and consecutive1735 * dashes with a single dash. Trims period, dash and underscore from beginning1736 * and end of filename. It is not guaranteed that this function will return a1737 * 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. 1738 1738 * 1739 1739 * @since 2.1.0 1740 * @since 4.7.0 The function now also replaces illegal characters with dashes. 1740 1741 * 1741 1742 * @param string $filename The filename to be sanitized 1742 1743 * @return string The sanitized filename 1743 1744 */ 1744 1745 function sanitize_file_name( $filename ) { 1745 1746 $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 1746 1757 $special_chars = array("?", "[", "]", "/", "\\", "=", "<", ">", ":", ";", ",", "'", "\"", "&", "$", "#", "*", "(", ")", "|", "~", "`", "!", "{", "}", "%", "+", chr(0)); 1747 1758 /** 1748 1759 * Filters the list of characters to remove from a filename. … … function sanitize_file_name( $filename ) { 1752 1763 * @param array $special_chars Characters to remove. 1753 1764 * @param string $filename_raw Filename as it was passed into sanitize_file_name(). 1754 1765 */ 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 1760 1780 $filename = trim( $filename, '.-_' ); 1761 1781 1782 if ( function_exists( 'mb_strtolower' ) ) { 1783 $filename = mb_strtolower( $filename, mb_detect_encoding( $filename ) ); 1784 } else { 1785 $filename = strtolower( $filename ); 1786 } 1787 1762 1788 if ( false === strpos( $filename, '.' ) ) { 1763 1789 $mime_types = wp_get_mime_types(); 1764 1790 $filetype = wp_check_filetype( 'test.' . $filename, $mime_types ); … … function sanitize_file_name( $filename ) { 1795 1821 foreach ( (array) $parts as $part) { 1796 1822 $filename .= '.' . $part; 1797 1823 1798 if ( preg_match(" /^[a-zA-Z]{2,5}\d?$/", $part) ) {1824 if ( preg_match("`^[a-zA-Z]{2,5}\d?$`$utf8_modifier", $part) ) { 1799 1825 $allowed = false; 1800 1826 foreach ( $mimes as $ext_preg => $mime_match ) { 1801 $ext_preg = '!^(' . $ext_preg . ')$!i';1827 $ext_preg = "`^($ext_preg)$`i$utf8_modifier"; 1802 1828 if ( preg_match( $ext_preg, $part ) ) { 1803 1829 $allowed = true; 1804 1830 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 { 16 16 foreach ( $special_chars as $char ) 17 17 $string .= $char; 18 18 $string .= 'test'; 19 $this->assertEquals( 'test test', sanitize_file_name( $string ) );19 $this->assertEquals( 'test-test', sanitize_file_name( $string ) ); 20 20 } 21 21 22 22 /** … … class Tests_Formatting_SanitizeFileName extends WP_UnitTestCase { 28 28 $urls = array( 29 29 'unencoded space.png' => 'unencoded-space.png', 30 30 'encoded-space.jpg' => 'encoded-space.jpg', 31 'plus+space.jpg' => 'plus space.jpg',31 'plus+space.jpg' => 'plus-space.jpg', 32 32 'multi %20 +space.png' => 'multi-20-space.png', 33 33 ); 34 34 … … class Tests_Formatting_SanitizeFileName extends WP_UnitTestCase { 47 47 48 48 function test_replaces_any_amount_of_whitespace_with_one_hyphen() { 49 49 $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")); 51 51 } 52 52 53 53 /** 54 54 * @ticket 16226 55 55 */ 56 56 function test_replaces_percent_sign() { 57 $this->assertEquals( 'a 22b.jpg', sanitize_file_name( 'a%22b.jpg' ) );57 $this->assertEquals( 'a-22b.jpg', sanitize_file_name( 'a%22b.jpg' ) ); 58 58 } 59 59 60 60 function test_replaces_unnammed_file_extensions() { … … class Tests_Formatting_SanitizeFileName extends WP_UnitTestCase { 67 67 // Test a filenames that becomes extensionless. 68 68 $this->assertEquals( 'no-extension', sanitize_file_name( '_.no-extension' ) ); 69 69 } 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 © & € – 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 à ê ñ ø æ 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 } 70 134 }