From bc7ff47537bae4f6db9de781cf8ba3487e28a30b Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Thu, 24 Oct 2013 12:05:00 +0200 Subject: [PATCH] [ticket/11912] Supply filename to content_guesser for guessing on windows The filename of the files sent to the guesser by plupload do not contain the file extension. Therefore, it's impossible to guess the mimetype if only the content_guesser is available and the function mime_content_type() doesn't exist. By supplying the filename we can circumvent this issue. PHPBB3-11912 --- phpBB/phpbb/mimetype/content_guesser.php | 16 ++++++++++++---- phpBB/phpbb/mimetype/guesser.php | 4 ++-- phpBB/phpbb/plupload/plupload.php | 2 +- tests/mimetype/guesser_test.php | 3 ++- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/phpBB/phpbb/mimetype/content_guesser.php b/phpBB/phpbb/mimetype/content_guesser.php index 6bdd410af4..6326bf73fa 100644 --- a/phpBB/phpbb/mimetype/content_guesser.php +++ b/phpBB/phpbb/mimetype/content_guesser.php @@ -492,7 +492,7 @@ class content_guesser * * @return string Guess for mimetype of file */ - public function guess($file) + public function guess($file, $file_name = '') { $mimetype = null; if (function_exists('mime_content_type')) @@ -501,14 +501,22 @@ class content_guesser } else { - $mimetype = $this->map_extension_to_type($file); + $file_name = (empty($file_name)) ? $file : $file_name; + $mimetype = $this->map_extension_to_type($file_name); } return $mimetype; } - protected function map_extension_to_type($file) + /** + * Map extension of supplied file_name to mime type + * + * @param string $file_name Path to file or filename + * + * @return string|null Mimetype if known or null if not + */ + protected function map_extension_to_type($file_name) { - $extension = pathinfo($file, PATHINFO_EXTENSION); + $extension = pathinfo($file_name, PATHINFO_EXTENSION); if (isset($this->extension_map[$extension])) { diff --git a/phpBB/phpbb/mimetype/guesser.php b/phpBB/phpbb/mimetype/guesser.php index 5ae094cd63..231b75f604 100644 --- a/phpBB/phpbb/mimetype/guesser.php +++ b/phpBB/phpbb/mimetype/guesser.php @@ -77,7 +77,7 @@ class guesser * * @return string Guess for mimetype of file */ - public function guess($file) + public function guess($file, $file_name = '') { if (!is_file($file)) { @@ -91,7 +91,7 @@ class guesser foreach ($this->guessers as $guesser) { - $mimetype = $guesser->guess($file); + $mimetype = $guesser->guess($file, $file_name); // Try to guess something that is not the fallback application/octet-stream if ($mimetype !== null && $mimetype !== 'application/octet-stream') diff --git a/phpBB/phpbb/plupload/plupload.php b/phpBB/phpbb/plupload/plupload.php index 29a4aff39b..dedc3cbcd4 100644 --- a/phpBB/phpbb/plupload/plupload.php +++ b/phpBB/phpbb/plupload/plupload.php @@ -135,7 +135,7 @@ class plupload 'tmp_name' => $file_path, 'name' => $this->request->variable('real_filename', ''), 'size' => filesize($file_path), - 'type' => $this->mimetype_guesser->guess($file_path), + 'type' => $this->mimetype_guesser->guess($file_path, $file_name), ); } else diff --git a/tests/mimetype/guesser_test.php b/tests/mimetype/guesser_test.php index 6484606210..9d4d965d63 100644 --- a/tests/mimetype/guesser_test.php +++ b/tests/mimetype/guesser_test.php @@ -119,8 +119,9 @@ class guesser_test extends \phpbb_test_case public function test_content_guesser($expected, $overload = false) { self::$function_exists = ($overload) ? false : true; - $guesser = new \phpbb\mimetype\guesser(array(new \phpbb\mimetype\content_guesser($this->phpbb_root_path, new \phpbb\php\ini))); + $guesser = new \phpbb\mimetype\guesser(array(new \phpbb\mimetype\content_guesser)); $this->assertEquals($expected[0], $guesser->guess($this->jpg_file)); + $this->assertEquals($expected[1], $guesser->guess($this->jpg_file, $this->jpg_file . '.jpg')); @copy($this->jpg_file, $this->jpg_file . '.jpg'); $this->assertEquals($expected[1], $guesser->guess($this->jpg_file . '.jpg')); @unlink($this->jpg_file . '.jpg');