From 9e500a9b06573ca270e8db6ca8fc067937e1ffec Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 10 Apr 2017 14:27:23 +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. Merges [40397] to the 4.7 branch. git-svn-id: https://develop.svn.wordpress.org/branches/4.7@40403 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 6b6a044bff..a3e813dd61 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -2366,7 +2366,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 3ad52351f9..bcfa185251 100644 --- a/tests/phpunit/tests/functions.php +++ b/tests/phpunit/tests/functions.php @@ -899,6 +899,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 @@ -977,6 +989,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.