Make WordPress Core


Ignore:
Timestamp:
11/11/2009 11:10:13 PM (15 years ago)
Author:
ryan
Message:

Sanitize filenames with multiple extensions. see #11122

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.8/wp-includes/formatting.php

    r12073 r12166  
    606606    $filename = preg_replace('/[\s-]+/', '-', $filename);
    607607    $filename = trim($filename, '.-_');
     608
     609    // Split the filename into a base and extension[s]
     610    $parts = explode('.', $filename);
     611
     612    // Return if only one extension
     613    if ( count($parts) <= 2 )
     614        return apply_filters('sanitize_file_name', $filename, $filename_raw);
     615
     616    // Process multiple extensions
     617    $filename = array_shift($parts);
     618    $extension = array_pop($parts);
     619    $mimes = get_allowed_mime_types();
     620
     621    // Loop over any intermediate extensions.  Munge them with a trailing underscore if they are a 2 - 5 character
     622    // long alpha string not in the extension whitelist.
     623    foreach ( (array) $parts as $part) {
     624        $filename .= '.' . $part;
     625       
     626        if ( preg_match("/^[a-zA-Z]{2,5}\d?$/", $part) ) {
     627            $allowed = false;
     628            foreach ( $mimes as $ext_preg => $mime_match ) {
     629                $ext_preg = '!(^' . $ext_preg . ')$!i';
     630                if ( preg_match( $ext_preg, $part ) ) {
     631                    $allowed = true;
     632                    break;
     633                }
     634            }
     635            if ( !$allowed )
     636                $filename .= '_';
     637        }
     638    }
     639    $filename .= '.' . $extension;
     640
    608641    return apply_filters('sanitize_file_name', $filename, $filename_raw);
    609642}
Note: See TracChangeset for help on using the changeset viewer.