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