Make WordPress Core

Ticket #22363: 22363.4.rev24527.patch

File 22363.4.rev24527.patch, 2.7 KB (added by NumidWasNotAvailable, 11 years ago)
  • wp-includes/formatting.php

    diff --git a/wp-includes/formatting.php b/wp-includes/formatting.php
    index c9ebd78..e97fc14 100644
    a b function remove_accents($string) { 
    826826}
    827827
    828828/**
    829  * Sanitizes a filename, replacing whitespace with dashes.
    830  *
    831  * Removes special characters that are illegal in filenames on certain
    832  * operating systems and special characters requiring special escaping
    833  * to manipulate at the command line. Replaces spaces and consecutive
    834  * dashes with a single dash. Trims period, dash and underscore from beginning
    835  * and end of filename.
     829 * Sanitizes a filename using sanitize_title.
    836830 *
    837831 * @since 2.1.0
    838832 *
    function remove_accents($string) { 
    841835 */
    842836function sanitize_file_name( $filename ) {
    843837        $filename_raw = $filename;
    844         $special_chars = array("?", "[", "]", "/", "\\", "=", "<", ">", ":", ";", ",", "'", "\"", "&", "$", "#", "*", "(", ")", "|", "~", "`", "!", "{", "}", chr(0));
    845         $special_chars = apply_filters('sanitize_file_name_chars', $special_chars, $filename_raw);
    846         $filename = str_replace($special_chars, '', $filename);
    847         $filename = preg_replace('/[\s-]+/', '-', $filename);
    848         $filename = trim($filename, '.-_');
    849 
    850         // Split the filename into a base and extension[s]
    851         $parts = explode('.', $filename);
    852 
    853         // Return if only one extension
    854         if ( count($parts) <= 2 )
    855                 return apply_filters('sanitize_file_name', $filename, $filename_raw);
    856 
    857         // Process multiple extensions
    858         $filename = array_shift($parts);
    859         $extension = array_pop($parts);
    860         $mimes = get_allowed_mime_types();
    861 
    862         // Loop over any intermediate extensions. Munge them with a trailing underscore if they are a 2 - 5 character
    863         // long alpha string not in the extension whitelist.
    864         foreach ( (array) $parts as $part) {
    865                 $filename .= '.' . $part;
    866 
    867                 if ( preg_match("/^[a-zA-Z]{2,5}\d?$/", $part) ) {
    868                         $allowed = false;
    869                         foreach ( $mimes as $ext_preg => $mime_match ) {
    870                                 $ext_preg = '!^(' . $ext_preg . ')$!i';
    871                                 if ( preg_match( $ext_preg, $part ) ) {
    872                                         $allowed = true;
    873                                         break;
    874                                 }
    875                         }
    876                         if ( !$allowed )
    877                                 $filename .= '_';
    878                 }
    879         }
    880         $filename .= '.' . $extension;
    881 
     838        $pathinfo = pathinfo( $filename );
     839        extract( $pathinfo, EXTR_PREFIX_ALL, 'pathinfo' );
     840        if ( $pathinfo_extension ) {
     841                if ( !isset( $pathinfo_filename ) ) // $pathinfo_filename is available since PHP 5.2.0.
     842                        $pathinfo_filename = substr( $pathinfo_basename, 0, - ( 1 + strlen( $pathinfo_extension ) ) );
     843                $filename = sanitize_title( $pathinfo_filename ) . '.' . $pathinfo_extension;
     844        } else
     845                $filename = sanitize_title( $filename );
    882846        return apply_filters('sanitize_file_name', $filename, $filename_raw);
    883847}
    884848