mirror of
https://github.com/phpbb/phpbb.git
synced 2025-08-01 06:20:46 +02:00
[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
This commit is contained in:
@@ -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);
|
||||
|
||||
if ($mimetype !== 'application/octet-stream')
|
||||
{
|
||||
$this->mimetype = $mimetype;
|
||||
}
|
||||
}
|
||||
|
||||
// Some browsers choke on a mimetype of application/octet-stream
|
||||
if (!$mimetype || $mimetype == 'application/octet-stream')
|
||||
{
|
||||
$mimetype = 'application/octetstream';
|
||||
}
|
||||
|
||||
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;
|
||||
|
Reference in New Issue
Block a user