Make WordPress Core


Ignore:
Timestamp:
01/11/2017 01:14:31 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.5 branch.

Location:
branches/4.5
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.5

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

    r37286 r39834  
    22462246 * then the "proper_filename" value will be set with a proper filename and extension.
    22472247 *
    2248  * Currently this function only supports validating images known to getimagesize().
     2248 * Currently this function only supports renaming images validated via wp_get_image_mime().
    22492249 *
    22502250 * @since 3.0.0
     
    22702270    }
    22712271
    2272     // We're able to validate images using GD
    2273     if ( $type && 0 === strpos( $type, 'image/' ) && function_exists('getimagesize') ) {
     2272    // Validate image types.
     2273    if ( $type && 0 === strpos( $type, 'image/' ) ) {
    22742274
    22752275        // Attempt to figure out what type of image it actually is
    2276         $imgstats = @getimagesize( $file );
    2277 
    2278         // If getimagesize() knows what kind of image it really is and if the real MIME doesn't match the claimed MIME
    2279         if ( !empty($imgstats['mime']) && $imgstats['mime'] != $type ) {
     2276        $real_mime = wp_get_image_mime( $file );
     2277
     2278        if ( ! $real_mime ) {
     2279            $type = $ext = false;
     2280        } elseif ( $real_mime != $type ) {
    22802281            /**
    22812282             * Filter the list mapping image mime types to their respective extensions.
     
    22942295
    22952296            // Replace whatever is after the last period in the filename with the correct extension
    2296             if ( ! empty( $mime_to_ext[ $imgstats['mime'] ] ) ) {
     2297            if ( ! empty( $mime_to_ext[ $real_mime ] ) ) {
    22972298                $filename_parts = explode( '.', $filename );
    22982299                array_pop( $filename_parts );
    2299                 $filename_parts[] = $mime_to_ext[ $imgstats['mime'] ];
     2300                $filename_parts[] = $mime_to_ext[ $real_mime ];
    23002301                $new_filename = implode( '.', $filename_parts );
    23012302
     
    23072308                $ext = $wp_filetype['ext'];
    23082309                $type = $wp_filetype['type'];
     2310            } else {
     2311                $type = $ext = false;
    23092312            }
     2313        }
     2314    } elseif ( function_exists( 'finfo_file' ) ) {
     2315        // Use finfo_file if available to validate non-image files.
     2316        $finfo = finfo_open( FILEINFO_MIME_TYPE );
     2317        $real_mime = finfo_file( $finfo, $file );
     2318        finfo_close( $finfo );
     2319
     2320        // If the extension does not match the file's real type, return false.
     2321        if ( $real_mime !== $type ) {
     2322            $type = $ext = false;
    23102323        }
    23112324    }
     
    23242337     */
    23252338    return apply_filters( 'wp_check_filetype_and_ext', compact( 'ext', 'type', 'proper_filename' ), $file, $filename, $mimes );
     2339}
     2340
     2341/**
     2342 * Returns the real mime type of an image file.
     2343 *
     2344 * This depends on exif_imagetype() or getimagesize() to determine real mime types.
     2345 *
     2346 * @since 4.7.1
     2347 *
     2348 * @param string $file Full path to the file.
     2349 * @return string|false The actual mime type or false if the type cannot be determined.
     2350 */
     2351function wp_get_image_mime( $file ) {
     2352    /*
     2353     * Use exif_imagetype() to check the mimetype if available or fall back to
     2354     * getimagesize() if exif isn't avaialbe. If either function throws an Exception
     2355     * we assume the file could not be validated.
     2356     */
     2357    try {
     2358        if ( ! is_callable( 'exif_imagetype' ) ) {
     2359            $mime = image_type_to_mime_type( exif_imagetype( $file ) );
     2360        } elseif ( function_exists( 'getimagesize' ) ) {
     2361            $imagesize = getimagesize( $file );
     2362            $mime = ( isset( $imagesize['mime'] ) ) ? $imagesize['mime'] : false;
     2363        } else {
     2364            $mime = false;
     2365        }
     2366    } catch ( Exception $e ) {
     2367        $mime = false;
     2368    }
     2369
     2370    return $mime;
    23262371}
    23272372
Note: See TracChangeset for help on using the changeset viewer.