Make WordPress Core

Changeset 39835


Ignore:
Timestamp:
01/11/2017 01:15:30 PM (9 years ago)
Author:
joemcgill
Message:

Media: Improve image filetype checking.

This adds a new function wp_get_image_mime() which is used by
wp_check_filetype_and_ext() to validate image files using
exif_imagetype() if available instead of getimagesize().

getimagesize() is less performant than exif_imagetype() and is
dependent on GD. If exif_imagetype() is not available, it falls back to
getimagesize() as before.

If wp_check_filetype_and_ext() can't validate the filetype, we now return
false for ext/MIME values.

Merges [39831] to the 4.4 branch.

Location:
branches/4.4
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.4

  • branches/4.4/src/wp-includes/functions.php

    r36063 r39835  
    21622162 * then the "proper_filename" value will be set with a proper filename and extension.
    21632163 *
    2164  * Currently this function only supports validating images known to getimagesize().
     2164 * Currently this function only supports renaming images validated via wp_get_image_mime().
    21652165 *
    21662166 * @since 3.0.0
     
    21862186    }
    21872187
    2188     // We're able to validate images using GD
    2189     if ( $type && 0 === strpos( $type, 'image/' ) && function_exists('getimagesize') ) {
     2188    // Validate image types.
     2189    if ( $type && 0 === strpos( $type, 'image/' ) ) {
    21902190
    21912191        // Attempt to figure out what type of image it actually is
    2192         $imgstats = @getimagesize( $file );
    2193 
    2194         // If getimagesize() knows what kind of image it really is and if the real MIME doesn't match the claimed MIME
    2195         if ( !empty($imgstats['mime']) && $imgstats['mime'] != $type ) {
     2192        $real_mime = wp_get_image_mime( $file );
     2193
     2194        if ( ! $real_mime ) {
     2195            $type = $ext = false;
     2196        } elseif ( $real_mime != $type ) {
    21962197            /**
    21972198             * Filter the list mapping image mime types to their respective extensions.
     
    22102211
    22112212            // Replace whatever is after the last period in the filename with the correct extension
    2212             if ( ! empty( $mime_to_ext[ $imgstats['mime'] ] ) ) {
     2213            if ( ! empty( $mime_to_ext[ $real_mime ] ) ) {
    22132214                $filename_parts = explode( '.', $filename );
    22142215                array_pop( $filename_parts );
    2215                 $filename_parts[] = $mime_to_ext[ $imgstats['mime'] ];
     2216                $filename_parts[] = $mime_to_ext[ $real_mime ];
    22162217                $new_filename = implode( '.', $filename_parts );
    22172218
     
    22232224                $ext = $wp_filetype['ext'];
    22242225                $type = $wp_filetype['type'];
     2226            } else {
     2227                $type = $ext = false;
    22252228            }
     2229        }
     2230    } elseif ( function_exists( 'finfo_file' ) ) {
     2231        // Use finfo_file if available to validate non-image files.
     2232        $finfo = finfo_open( FILEINFO_MIME_TYPE );
     2233        $real_mime = finfo_file( $finfo, $file );
     2234        finfo_close( $finfo );
     2235
     2236        // If the extension does not match the file's real type, return false.
     2237        if ( $real_mime !== $type ) {
     2238            $type = $ext = false;
    22262239        }
    22272240    }
     
    22402253     */
    22412254    return apply_filters( 'wp_check_filetype_and_ext', compact( 'ext', 'type', 'proper_filename' ), $file, $filename, $mimes );
     2255}
     2256
     2257/**
     2258 * Returns the real mime type of an image file.
     2259 *
     2260 * This depends on exif_imagetype() or getimagesize() to determine real mime types.
     2261 *
     2262 * @since 4.7.1
     2263 *
     2264 * @param string $file Full path to the file.
     2265 * @return string|false The actual mime type or false if the type cannot be determined.
     2266 */
     2267function wp_get_image_mime( $file ) {
     2268    /*
     2269     * Use exif_imagetype() to check the mimetype if available or fall back to
     2270     * getimagesize() if exif isn't avaialbe. If either function throws an Exception
     2271     * we assume the file could not be validated.
     2272     */
     2273    try {
     2274        if ( ! is_callable( 'exif_imagetype' ) ) {
     2275            $mime = image_type_to_mime_type( exif_imagetype( $file ) );
     2276        } elseif ( function_exists( 'getimagesize' ) ) {
     2277            $imagesize = getimagesize( $file );
     2278            $mime = ( isset( $imagesize['mime'] ) ) ? $imagesize['mime'] : false;
     2279        } else {
     2280            $mime = false;
     2281        }
     2282    } catch ( Exception $e ) {
     2283        $mime = false;
     2284    }
     2285
     2286    return $mime;
    22422287}
    22432288
Note: See TracChangeset for help on using the changeset viewer.