From 04e3680d1c39b1b08f331f54cd3388f694c19d41 Mon Sep 17 00:00:00 2001 From: Joe McGill Date: Sun, 9 Apr 2017 22:43:04 +0000 Subject: [PATCH] Media: Improve handling of non-image files in wp_get_image_mime. This prevents non-image fileypes from returning a mime type of "application/octet-stream" when `exif_imagetype()` returns `false`. Props blobfolio. Fixes #40017. git-svn-id: https://develop.svn.wordpress.org/trunk@40397 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/functions.php | 3 +- tests/phpunit/tests/functions.php | 47 +++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index ee214e923a..966dcd8265 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -2367,7 +2367,8 @@ function wp_get_image_mime( $file ) { */ try { if ( is_callable( 'exif_imagetype' ) ) { - $mime = image_type_to_mime_type( exif_imagetype( $file ) ); + $imagetype = exif_imagetype( $file ); + $mime = ( $imagetype ) ? image_type_to_mime_type( $imagetype ) : false; } elseif ( function_exists( 'getimagesize' ) ) { $imagesize = getimagesize( $file ); $mime = ( isset( $imagesize['mime'] ) ) ? $imagesize['mime'] : false; diff --git a/tests/phpunit/tests/functions.php b/tests/phpunit/tests/functions.php index a3aba79c44..47704f8036 100644 --- a/tests/phpunit/tests/functions.php +++ b/tests/phpunit/tests/functions.php @@ -915,6 +915,18 @@ class Tests_Functions extends WP_UnitTestCase { $this->assertEquals( $uuids, $unique_uuids ); } + /** + * @ticket 40017 + * @dataProvider _wp_get_image_mime + */ + public function test_wp_get_image_mime( $file, $expected ) { + if ( ! is_callable( 'exif_imagetype' ) && ! function_exists( 'getimagesize' ) ) { + $this->markTestSkipped( 'The exif PHP extension is not loaded.' ); + } + + $this->assertEquals( $expected, wp_get_image_mime( $file ) ); + } + /** * @ticket 39550 * @dataProvider _wp_check_filetype_and_ext_data @@ -993,6 +1005,41 @@ class Tests_Functions extends WP_UnitTestCase { return $mimes; } + /** + * Data profider for test_wp_get_image_mime(); + */ + public function _wp_get_image_mime() { + $data = array( + // Standard JPEG. + array( + DIR_TESTDATA . '/images/test-image.jpg', + 'image/jpeg', + ), + // Standard GIF. + array( + DIR_TESTDATA . '/images/test-image.gif', + 'image/gif', + ), + // Standard PNG. + array( + DIR_TESTDATA . '/images/test-image.png', + 'image/png', + ), + // Image with wrong extension. + array( + DIR_TESTDATA . '/images/test-image-mime-jpg.png', + 'image/jpeg', + ), + // Not an image. + array( + DIR_TESTDATA . '/uploads/dashicons.woff', + false, + ), + ); + + return $data; + } + public function _wp_check_filetype_and_ext_data() { $data = array( // Standard image.