Make WordPress Core

Changeset 39833


Ignore:
Timestamp:
01/11/2017 01:13:44 PM (8 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.6 branch.

Location:
branches/4.6
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.6

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

    r38488 r39833  
    22352235 * then the "proper_filename" value will be set with a proper filename and extension.
    22362236 *
    2237  * Currently this function only supports validating images known to getimagesize().
     2237 * Currently this function only supports renaming images validated via wp_get_image_mime().
    22382238 *
    22392239 * @since 3.0.0
     
    22592259    }
    22602260
    2261     // We're able to validate images using GD
    2262     if ( $type && 0 === strpos( $type, 'image/' ) && function_exists('getimagesize') ) {
     2261    // Validate image types.
     2262    if ( $type && 0 === strpos( $type, 'image/' ) ) {
    22632263
    22642264        // Attempt to figure out what type of image it actually is
    2265         $imgstats = @getimagesize( $file );
    2266 
    2267         // If getimagesize() knows what kind of image it really is and if the real MIME doesn't match the claimed MIME
    2268         if ( !empty($imgstats['mime']) && $imgstats['mime'] != $type ) {
     2265        $real_mime = wp_get_image_mime( $file );
     2266
     2267        if ( ! $real_mime ) {
     2268            $type = $ext = false;
     2269        } elseif ( $real_mime != $type ) {
    22692270            /**
    22702271             * Filters the list mapping image mime types to their respective extensions.
     
    22832284
    22842285            // Replace whatever is after the last period in the filename with the correct extension
    2285             if ( ! empty( $mime_to_ext[ $imgstats['mime'] ] ) ) {
     2286            if ( ! empty( $mime_to_ext[ $real_mime ] ) ) {
    22862287                $filename_parts = explode( '.', $filename );
    22872288                array_pop( $filename_parts );
    2288                 $filename_parts[] = $mime_to_ext[ $imgstats['mime'] ];
     2289                $filename_parts[] = $mime_to_ext[ $real_mime ];
    22892290                $new_filename = implode( '.', $filename_parts );
    22902291
     
    22962297                $ext = $wp_filetype['ext'];
    22972298                $type = $wp_filetype['type'];
     2299            } else {
     2300                $type = $ext = false;
    22982301            }
     2302        }
     2303    } elseif ( function_exists( 'finfo_file' ) ) {
     2304        // Use finfo_file if available to validate non-image files.
     2305        $finfo = finfo_open( FILEINFO_MIME_TYPE );
     2306        $real_mime = finfo_file( $finfo, $file );
     2307        finfo_close( $finfo );
     2308
     2309        // If the extension does not match the file's real type, return false.
     2310        if ( $real_mime !== $type ) {
     2311            $type = $ext = false;
    22992312        }
    23002313    }
     
    23132326     */
    23142327    return apply_filters( 'wp_check_filetype_and_ext', compact( 'ext', 'type', 'proper_filename' ), $file, $filename, $mimes );
     2328}
     2329
     2330/**
     2331 * Returns the real mime type of an image file.
     2332 *
     2333 * This depends on exif_imagetype() or getimagesize() to determine real mime types.
     2334 *
     2335 * @since 4.7.1
     2336 *
     2337 * @param string $file Full path to the file.
     2338 * @return string|false The actual mime type or false if the type cannot be determined.
     2339 */
     2340function wp_get_image_mime( $file ) {
     2341    /*
     2342     * Use exif_imagetype() to check the mimetype if available or fall back to
     2343     * getimagesize() if exif isn't avaialbe. If either function throws an Exception
     2344     * we assume the file could not be validated.
     2345     */
     2346    try {
     2347        if ( ! is_callable( 'exif_imagetype' ) ) {
     2348            $mime = image_type_to_mime_type( exif_imagetype( $file ) );
     2349        } elseif ( function_exists( 'getimagesize' ) ) {
     2350            $imagesize = getimagesize( $file );
     2351            $mime = ( isset( $imagesize['mime'] ) ) ? $imagesize['mime'] : false;
     2352        } else {
     2353            $mime = false;
     2354        }
     2355    } catch ( Exception $e ) {
     2356        $mime = false;
     2357    }
     2358
     2359    return $mime;
    23152360}
    23162361
Note: See TracChangeset for help on using the changeset viewer.