~canonical-sysadmins/wordpress/5.1

« back to all changes in this revision

Viewing changes to wp-includes/functions.php

  • Committer: Jamon Camisso
  • Date: 2017-01-12 15:30:45 UTC
  • mto: This revision was merged to the branch mainline in revision 25.
  • Revision ID: jamon.camisso@canonical.com-20170112153045-dekfwsu8mcsdxa7x
New upstream release 4.7.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
2254
2254
 * If it's determined that the extension does not match the file's real type,
2255
2255
 * then the "proper_filename" value will be set with a proper filename and extension.
2256
2256
 *
2257
 
 * Currently this function only supports validating images known to getimagesize().
 
2257
 * Currently this function only supports renaming images validated via wp_get_image_mime().
2258
2258
 *
2259
2259
 * @since 3.0.0
2260
2260
 *
2278
2278
                return compact( 'ext', 'type', 'proper_filename' );
2279
2279
        }
2280
2280
 
2281
 
        // We're able to validate images using GD
2282
 
        if ( $type && 0 === strpos( $type, 'image/' ) && function_exists('getimagesize') ) {
 
2281
        // Validate image types.
 
2282
        if ( $type && 0 === strpos( $type, 'image/' ) ) {
2283
2283
 
2284
2284
                // Attempt to figure out what type of image it actually is
2285
 
                $imgstats = @getimagesize( $file );
 
2285
                $real_mime = wp_get_image_mime( $file );
2286
2286
 
2287
 
                // If getimagesize() knows what kind of image it really is and if the real MIME doesn't match the claimed MIME
2288
 
                if ( !empty($imgstats['mime']) && $imgstats['mime'] != $type ) {
 
2287
                if ( ! $real_mime ) {
 
2288
                        $type = $ext = false;
 
2289
                } elseif ( $real_mime != $type ) {
2289
2290
                        /**
2290
2291
                         * Filters the list mapping image mime types to their respective extensions.
2291
2292
                         *
2302
2303
                        ) );
2303
2304
 
2304
2305
                        // Replace whatever is after the last period in the filename with the correct extension
2305
 
                        if ( ! empty( $mime_to_ext[ $imgstats['mime'] ] ) ) {
 
2306
                        if ( ! empty( $mime_to_ext[ $real_mime ] ) ) {
2306
2307
                                $filename_parts = explode( '.', $filename );
2307
2308
                                array_pop( $filename_parts );
2308
 
                                $filename_parts[] = $mime_to_ext[ $imgstats['mime'] ];
 
2309
                                $filename_parts[] = $mime_to_ext[ $real_mime ];
2309
2310
                                $new_filename = implode( '.', $filename_parts );
2310
2311
 
2311
2312
                                if ( $new_filename != $filename ) {
2315
2316
                                $wp_filetype = wp_check_filetype( $new_filename, $mimes );
2316
2317
                                $ext = $wp_filetype['ext'];
2317
2318
                                $type = $wp_filetype['type'];
 
2319
                        } else {
 
2320
                                $type = $ext = false;
2318
2321
                        }
2319
2322
                }
 
2323
        } elseif ( function_exists( 'finfo_file' ) ) {
 
2324
                // Use finfo_file if available to validate non-image files.
 
2325
                $finfo = finfo_open( FILEINFO_MIME_TYPE );
 
2326
                $real_mime = finfo_file( $finfo, $file );
 
2327
                finfo_close( $finfo );
 
2328
 
 
2329
                // If the extension does not match the file's real type, return false.
 
2330
                if ( $real_mime !== $type ) {
 
2331
                        $type = $ext = false;
 
2332
                }
2320
2333
        }
2321
2334
 
2322
2335
        /**
2335
2348
}
2336
2349
 
2337
2350
/**
 
2351
 * Returns the real mime type of an image file.
 
2352
 *
 
2353
 * This depends on exif_imagetype() or getimagesize() to determine real mime types.
 
2354
 *
 
2355
 * @since 4.7.1
 
2356
 *
 
2357
 * @param string $file Full path to the file.
 
2358
 * @return string|false The actual mime type or false if the type cannot be determined.
 
2359
 */
 
2360
function wp_get_image_mime( $file ) {
 
2361
        /*
 
2362
         * Use exif_imagetype() to check the mimetype if available or fall back to
 
2363
         * getimagesize() if exif isn't avaialbe. If either function throws an Exception
 
2364
         * we assume the file could not be validated.
 
2365
         */
 
2366
        try {
 
2367
                if ( is_callable( 'exif_imagetype' ) ) {
 
2368
                        $mime = image_type_to_mime_type( exif_imagetype( $file ) );
 
2369
                } elseif ( function_exists( 'getimagesize' ) ) {
 
2370
                        $imagesize = getimagesize( $file );
 
2371
                        $mime = ( isset( $imagesize['mime'] ) ) ? $imagesize['mime'] : false;
 
2372
                } else {
 
2373
                        $mime = false;
 
2374
                }
 
2375
        } catch ( Exception $e ) {
 
2376
                $mime = false;
 
2377
        }
 
2378
 
 
2379
        return $mime;
 
2380
}
 
2381
 
 
2382
/**
2338
2383
 * Retrieve list of mime types and file extensions.
2339
2384
 *
2340
2385
 * @since 3.5.0