Changeset 39833
- Timestamp:
- 01/11/2017 01:13:44 PM (8 years ago)
- Location:
- branches/4.6
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/4.6
-
branches/4.6/src/wp-includes/functions.php
r38488 r39833 2235 2235 * then the "proper_filename" value will be set with a proper filename and extension. 2236 2236 * 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(). 2238 2238 * 2239 2239 * @since 3.0.0 … … 2259 2259 } 2260 2260 2261 // We're able to validate images using GD2262 if ( $type && 0 === strpos( $type, 'image/' ) && function_exists('getimagesize')) {2261 // Validate image types. 2262 if ( $type && 0 === strpos( $type, 'image/' ) ) { 2263 2263 2264 2264 // 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 ) { 2269 2270 /** 2270 2271 * Filters the list mapping image mime types to their respective extensions. … … 2283 2284 2284 2285 // 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 ] ) ) { 2286 2287 $filename_parts = explode( '.', $filename ); 2287 2288 array_pop( $filename_parts ); 2288 $filename_parts[] = $mime_to_ext[ $ imgstats['mime']];2289 $filename_parts[] = $mime_to_ext[ $real_mime ]; 2289 2290 $new_filename = implode( '.', $filename_parts ); 2290 2291 … … 2296 2297 $ext = $wp_filetype['ext']; 2297 2298 $type = $wp_filetype['type']; 2299 } else { 2300 $type = $ext = false; 2298 2301 } 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; 2299 2312 } 2300 2313 } … … 2313 2326 */ 2314 2327 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 */ 2340 function 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; 2315 2360 } 2316 2361
Note: See TracChangeset
for help on using the changeset viewer.