Changeset 39835
- Timestamp:
- 01/11/2017 01:15:30 PM (9 years ago)
- Location:
- branches/4.4
- Files:
-
- 2 edited
-
. (modified) (1 prop)
-
src/wp-includes/functions.php (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/4.4
-
branches/4.4/src/wp-includes/functions.php
r36063 r39835 2162 2162 * then the "proper_filename" value will be set with a proper filename and extension. 2163 2163 * 2164 * Currently this function only supports validating images known to getimagesize().2164 * Currently this function only supports renaming images validated via wp_get_image_mime(). 2165 2165 * 2166 2166 * @since 3.0.0 … … 2186 2186 } 2187 2187 2188 // We're able to validate images using GD2189 if ( $type && 0 === strpos( $type, 'image/' ) && function_exists('getimagesize')) {2188 // Validate image types. 2189 if ( $type && 0 === strpos( $type, 'image/' ) ) { 2190 2190 2191 2191 // Attempt to figure out what type of image it actually is 2192 $imgstats = @getimagesize( $file ); 2193 2194 // If getimagesize() knows what kind of image it really is and if the real MIME doesn't match the claimed MIME 2195 if ( !empty($imgstats['mime']) && $imgstats['mime'] != $type ) { 2192 $real_mime = wp_get_image_mime( $file ); 2193 2194 if ( ! $real_mime ) { 2195 $type = $ext = false; 2196 } elseif ( $real_mime != $type ) { 2196 2197 /** 2197 2198 * Filter the list mapping image mime types to their respective extensions. … … 2210 2211 2211 2212 // Replace whatever is after the last period in the filename with the correct extension 2212 if ( ! empty( $mime_to_ext[ $ imgstats['mime']] ) ) {2213 if ( ! empty( $mime_to_ext[ $real_mime ] ) ) { 2213 2214 $filename_parts = explode( '.', $filename ); 2214 2215 array_pop( $filename_parts ); 2215 $filename_parts[] = $mime_to_ext[ $ imgstats['mime']];2216 $filename_parts[] = $mime_to_ext[ $real_mime ]; 2216 2217 $new_filename = implode( '.', $filename_parts ); 2217 2218 … … 2223 2224 $ext = $wp_filetype['ext']; 2224 2225 $type = $wp_filetype['type']; 2226 } else { 2227 $type = $ext = false; 2225 2228 } 2229 } 2230 } elseif ( function_exists( 'finfo_file' ) ) { 2231 // Use finfo_file if available to validate non-image files. 2232 $finfo = finfo_open( FILEINFO_MIME_TYPE ); 2233 $real_mime = finfo_file( $finfo, $file ); 2234 finfo_close( $finfo ); 2235 2236 // If the extension does not match the file's real type, return false. 2237 if ( $real_mime !== $type ) { 2238 $type = $ext = false; 2226 2239 } 2227 2240 } … … 2240 2253 */ 2241 2254 return apply_filters( 'wp_check_filetype_and_ext', compact( 'ext', 'type', 'proper_filename' ), $file, $filename, $mimes ); 2255 } 2256 2257 /** 2258 * Returns the real mime type of an image file. 2259 * 2260 * This depends on exif_imagetype() or getimagesize() to determine real mime types. 2261 * 2262 * @since 4.7.1 2263 * 2264 * @param string $file Full path to the file. 2265 * @return string|false The actual mime type or false if the type cannot be determined. 2266 */ 2267 function wp_get_image_mime( $file ) { 2268 /* 2269 * Use exif_imagetype() to check the mimetype if available or fall back to 2270 * getimagesize() if exif isn't avaialbe. If either function throws an Exception 2271 * we assume the file could not be validated. 2272 */ 2273 try { 2274 if ( ! is_callable( 'exif_imagetype' ) ) { 2275 $mime = image_type_to_mime_type( exif_imagetype( $file ) ); 2276 } elseif ( function_exists( 'getimagesize' ) ) { 2277 $imagesize = getimagesize( $file ); 2278 $mime = ( isset( $imagesize['mime'] ) ) ? $imagesize['mime'] : false; 2279 } else { 2280 $mime = false; 2281 } 2282 } catch ( Exception $e ) { 2283 $mime = false; 2284 } 2285 2286 return $mime; 2242 2287 } 2243 2288
Note: See TracChangeset
for help on using the changeset viewer.