From 9bc6e641bfbe53c3920662ee8e24a723ac34e6a1 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Thu, 9 Jan 2014 23:50:40 +0100 Subject: [ticket/11148] Add mimetype guesser to filespec and fileupload class The mimetype guesser will be used to get the mimetype of uploaded files. Until now, this was only used for files uploaded with plupload. If a file doesn't have a mimetype supplied, we will now try to get the correct mimetype. PHPBB3-11148 --- phpBB/includes/functions_upload.php | 58 +++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 32 deletions(-) (limited to 'phpBB/includes/functions_upload.php') diff --git a/phpBB/includes/functions_upload.php b/phpBB/includes/functions_upload.php index c640865212..fb153a9e0c 100644 --- a/phpBB/includes/functions_upload.php +++ b/phpBB/includes/functions_upload.php @@ -52,11 +52,17 @@ class filespec */ protected $plupload; + /** + * phpBB Mimetype guesser + * @var \phpbb\mimetype\guesser + */ + protected $mimetype_guesser; + /** * File Class * @access private */ - function filespec($upload_ary, $upload_namespace, \phpbb\plupload\plupload $plupload = null) + function filespec($upload_ary, $upload_namespace, \phpbb\mimetype\guesser $mimetype_guesser = null, \phpbb\plupload\plupload $plupload = null) { if (!isset($upload_ary)) { @@ -90,6 +96,7 @@ class filespec $this->local = (isset($upload_ary['local_mode'])) ? true : false; $this->upload = $upload_namespace; $this->plupload = $plupload; + $this->mimetype_guesser = $mimetype_guesser; } /** @@ -215,25 +222,22 @@ class filespec } /** - * Get mimetype. Utilize mime_content_type if the function exist. - * Not used at the moment... + * Get mimetype + * */ function get_mimetype($filename) { - $mimetype = ''; - - if (function_exists('mime_content_type')) + if ($this->mimetype_guesser !== null) { - $mimetype = mime_content_type($filename); - } + $mimetype = $this->mimetype_guesser->guess($filename); - // Some browsers choke on a mimetype of application/octet-stream - if (!$mimetype || $mimetype == 'application/octet-stream') - { - $mimetype = 'application/octetstream'; + if ($mimetype !== 'application/octet-stream') + { + $this->mimetype = $mimetype; + } } - return $mimetype; + return $this->mimetype; } /** @@ -372,6 +376,9 @@ class filespec // Try to get real filesize from destination folder $this->filesize = (@filesize($this->destination_file)) ? @filesize($this->destination_file) : $this->filesize; + // Get mimetype of supplied file + $this->mimetype = $this->get_mimetype($this->destination_file); + if ($this->is_image() && !$skip_image_check) { $this->width = $this->height = 0; @@ -580,7 +587,7 @@ class fileupload * @return object $file Object "filespec" is returned, all further operations can be done with this object * @access public */ - function form_upload($form_name, \phpbb\plupload\plupload $plupload = null) + function form_upload($form_name, \phpbb\mimetype\guesser $mimetype_guesser = null, \phpbb\plupload\plupload $plupload = null) { global $user, $request; @@ -596,7 +603,7 @@ class fileupload } } - $file = new filespec($upload, $this, $plupload); + $file = new filespec($upload, $this, $mimetype_guesser, $plupload); if ($file->init_error) { @@ -656,7 +663,7 @@ class fileupload /** * Move file from another location to phpBB */ - function local_upload($source_file, $filedata = false) + function local_upload($source_file, $filedata = false, \phpbb\mimetype\guesser $mimetype_guesser = null) { global $user, $request; @@ -670,19 +677,6 @@ class fileupload $upload['name'] = utf8_basename($source_file); $upload['size'] = 0; $mimetype = ''; - - if (function_exists('mime_content_type')) - { - $mimetype = mime_content_type($source_file); - } - - // Some browsers choke on a mimetype of application/octet-stream - if (!$mimetype || $mimetype == 'application/octet-stream') - { - $mimetype = 'application/octetstream'; - } - - $upload['type'] = $mimetype; } else { @@ -691,7 +685,7 @@ class fileupload $upload['type'] = $filedata['type']; } - $file = new filespec($upload, $this); + $file = new filespec($upload, $this, $mimetype_guesser); if ($file->init_error) { @@ -749,7 +743,7 @@ class fileupload * @return object $file Object "filespec" is returned, all further operations can be done with this object * @access public */ - function remote_upload($upload_url) + function remote_upload($upload_url, \phpbb\mimetype\guesser $mimetype_guesser = null) { global $user, $phpbb_root_path; @@ -904,7 +898,7 @@ class fileupload $upload_ary['tmp_name'] = $filename; - $file = new filespec($upload_ary, $this); + $file = new filespec($upload_ary, $this, $mimetype_guesser); $this->common_checks($file); return $file; -- cgit v1.2.1 From de404002c75f770cc923262b9c16d899662ae114 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Thu, 30 Jan 2014 23:04:29 +0100 Subject: [ticket/11148] Default to application/octet-stream if no mimetype given This should prevent us from having an empty mimetype while uploading a file using local_upload(). PHPBB3-11148 --- phpBB/includes/functions_upload.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/includes/functions_upload.php') diff --git a/phpBB/includes/functions_upload.php b/phpBB/includes/functions_upload.php index fb153a9e0c..c3381aa68b 100644 --- a/phpBB/includes/functions_upload.php +++ b/phpBB/includes/functions_upload.php @@ -676,7 +676,7 @@ class fileupload { $upload['name'] = utf8_basename($source_file); $upload['size'] = 0; - $mimetype = ''; + $mimetype = 'application/octet-stream'; } else { -- cgit v1.2.1 From ea5bc9c8339327afb1519f8c3986c257880c54dc Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Thu, 30 Jan 2014 23:14:48 +0100 Subject: [ticket/11148] Add missing parts to docblock of get_mimetype() method PHPBB3-11148 --- phpBB/includes/functions_upload.php | 2 ++ 1 file changed, 2 insertions(+) (limited to 'phpBB/includes/functions_upload.php') diff --git a/phpBB/includes/functions_upload.php b/phpBB/includes/functions_upload.php index c3381aa68b..6ca430d9ac 100644 --- a/phpBB/includes/functions_upload.php +++ b/phpBB/includes/functions_upload.php @@ -224,6 +224,8 @@ class filespec /** * Get mimetype * + * @param string $filename Filename that needs to be checked + * @return string Mimetype of supplied filename */ function get_mimetype($filename) { -- cgit v1.2.1 From 4eb7485b397733c8765f8bd37a04b7a034b29792 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Sun, 1 Jun 2014 00:19:22 +0200 Subject: [ticket/11148] Always use the output of the mimetype guesser in get_mimetype PHPBB3-11148 --- phpBB/includes/functions_upload.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) (limited to 'phpBB/includes/functions_upload.php') diff --git a/phpBB/includes/functions_upload.php b/phpBB/includes/functions_upload.php index 6ca430d9ac..f50ce9432f 100644 --- a/phpBB/includes/functions_upload.php +++ b/phpBB/includes/functions_upload.php @@ -231,12 +231,7 @@ class filespec { if ($this->mimetype_guesser !== null) { - $mimetype = $this->mimetype_guesser->guess($filename); - - if ($mimetype !== 'application/octet-stream') - { - $this->mimetype = $mimetype; - } + $this->mimetype = $this->mimetype_guesser->guess($filename); } return $this->mimetype; -- cgit v1.2.1 From ff56f0dcfe6355a9bd24c1e33dee15c60d01526b Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Mon, 2 Jun 2014 11:57:37 +0200 Subject: [ticket/11148] Remove unneeded variable mimetype and use type octet-stream The variable $mimetype is not used in the method local_upload() afterwards so it shouldn't be assigned. The correct default mimetype should be application/octet-stream and not application/octetstream according to RFC 2046. PHPBB3-11148 --- phpBB/includes/functions_upload.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'phpBB/includes/functions_upload.php') diff --git a/phpBB/includes/functions_upload.php b/phpBB/includes/functions_upload.php index f50ce9432f..0d33d32a1f 100644 --- a/phpBB/includes/functions_upload.php +++ b/phpBB/includes/functions_upload.php @@ -82,7 +82,7 @@ class filespec if (!$this->mimetype) { - $this->mimetype = 'application/octetstream'; + $this->mimetype = 'application/octet-stream'; } $this->extension = strtolower(self::get_extension($this->realname)); @@ -673,7 +673,6 @@ class fileupload { $upload['name'] = utf8_basename($source_file); $upload['size'] = 0; - $mimetype = 'application/octet-stream'; } else { -- cgit v1.2.1