Changeset 39832 for branches/4.7/src/wp-includes/functions.php
- Timestamp:
- 01/11/2017 01:11:52 PM (8 years ago)
- Location:
- branches/4.7
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/4.7
-
branches/4.7/src/wp-includes/functions.php
r39323 r39832 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 … … 2279 2279 } 2280 2280 2281 // We're able to validate images using GD2282 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 ); 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 ) { 2285 $real_mime = wp_get_image_mime( $file ); 2286 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. … … 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 … … 2316 2317 $ext = $wp_filetype['ext']; 2317 2318 $type = $wp_filetype['type']; 2319 } else { 2320 $type = $ext = false; 2318 2321 } 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; 2319 2332 } 2320 2333 } … … 2333 2346 */ 2334 2347 return apply_filters( 'wp_check_filetype_and_ext', compact( 'ext', 'type', 'proper_filename' ), $file, $filename, $mimes ); 2348 } 2349 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; 2335 2380 } 2336 2381
Note: See TracChangeset
for help on using the changeset viewer.