From b971f20a5d9f6f163731b533fdb1242ebd27ff8f Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Sun, 31 May 2015 13:29:49 +0200 Subject: [PATCH 01/66] [ticket/13904] Add initial factory for handling file classes PHPBB3-13904 --- phpBB/phpbb/files/factory.php | 79 +++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 phpBB/phpbb/files/factory.php diff --git a/phpBB/phpbb/files/factory.php b/phpBB/phpbb/files/factory.php new file mode 100644 index 0000000000..9385ad678e --- /dev/null +++ b/phpBB/phpbb/files/factory.php @@ -0,0 +1,79 @@ + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +namespace phpbb\files; + +class factory +{ + /** + * @var \Symfony\Component\DependencyInjection\ContainerInterface + */ + private $container; + + /** + * Constructor + * + * @param \Symfony\Component\DependencyInjection\ContainerInterface $container + */ + public function __construct(\Symfony\Component\DependencyInjection\ContainerInterface $container) + { + $this->container = $container; + } + + /** + * Get files service + * + * @param string $name Service name + * + * @return object|false Requested service or false if service could not be + * found by the container + */ + public function get($name) + { + $service = false; + + try + { + $service = $this->container->get($name); + } + catch (\Exception $e) + { + // do nothing + } + + return $service; + } + + /** + * Magic function for handling get calls, e.g. get_fileupload() or + * get_filespec() and turning them into call for files. services like + * files.fileupload. + * + * @param string $name Name of called function + * @param mixed $arguments Possible supplied arguments + * + * @return object|false Requested service or false if service could not be + * found by the container + */ + public function __call($name, $arguments) + { + if (substr($name, 0, 4) === 'get_') + { + return $this->get('files.' . substr($name, 4)); + } + else + { + return false; + } + } +} From 557f1a89d512e6c4f4c96033091ab5b429825e6d Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Fri, 14 Aug 2015 09:01:29 +0200 Subject: [PATCH 02/66] [ticket/13904] Add filespec class to files classes PHPBB3-13904 --- .../default/container/services_files.yml | 3 + phpBB/phpbb/files/filespec.php | 480 ++++++++++++++++++ 2 files changed, 483 insertions(+) create mode 100644 phpBB/phpbb/files/filespec.php diff --git a/phpBB/config/default/container/services_files.yml b/phpBB/config/default/container/services_files.yml index 828f9076dd..a1cec843c5 100644 --- a/phpBB/config/default/container/services_files.yml +++ b/phpBB/config/default/container/services_files.yml @@ -1,3 +1,6 @@ services: filesystem: class: phpbb\filesystem\filesystem + + files.filespec: + class: phpbb\files\filespec diff --git a/phpBB/phpbb/files/filespec.php b/phpBB/phpbb/files/filespec.php new file mode 100644 index 0000000000..f74e91b0d5 --- /dev/null +++ b/phpBB/phpbb/files/filespec.php @@ -0,0 +1,480 @@ + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +namespace phpbb\files; + +/** + * Responsible for holding all file relevant information, as well as doing file-specific operations. + * The {@link fileupload fileupload class} can be used to upload several files, each of them being this object to operate further on. + */ +class filespec +{ + var $filename = ''; + var $realname = ''; + var $uploadname = ''; + var $mimetype = ''; + var $extension = ''; + var $filesize = 0; + var $width = 0; + var $height = 0; + var $image_info = array(); + + var $destination_file = ''; + var $destination_path = ''; + + var $file_moved = false; + var $init_error = false; + var $local = false; + + var $error = array(); + + var $upload = ''; + + /** + * @var \phpbb\filesystem\filesystem_interface + */ + protected $filesystem; + + /** + * The plupload object + * @var \phpbb\plupload\plupload + */ + protected $plupload; + + /** + * phpBB Mimetype guesser + * @var \phpbb\mimetype\guesser + */ + protected $mimetype_guesser; + + /** + * File Class + * @access private + */ + function filespec($upload_ary, $upload_namespace, \phpbb\filesystem\filesystem_interface $phpbb_filesystem, \phpbb\mimetype\guesser $mimetype_guesser = null, \phpbb\plupload\plupload $plupload = null) + { + if (!isset($upload_ary)) + { + $this->init_error = true; + return; + } + + $this->filename = $upload_ary['tmp_name']; + $this->filesize = $upload_ary['size']; + $name = (STRIP) ? stripslashes($upload_ary['name']) : $upload_ary['name']; + $name = trim(utf8_basename($name)); + $this->realname = $this->uploadname = $name; + $this->mimetype = $upload_ary['type']; + + // Opera adds the name to the mime type + $this->mimetype = (strpos($this->mimetype, '; name') !== false) ? str_replace(strstr($this->mimetype, '; name'), '', $this->mimetype) : $this->mimetype; + + if (!$this->mimetype) + { + $this->mimetype = 'application/octet-stream'; + } + + $this->extension = strtolower(self::get_extension($this->realname)); + + // Try to get real filesize from temporary folder (not always working) ;) + $this->filesize = (@filesize($this->filename)) ? @filesize($this->filename) : $this->filesize; + + $this->width = $this->height = 0; + $this->file_moved = false; + + $this->local = (isset($upload_ary['local_mode'])) ? true : false; + $this->upload = $upload_namespace; + $this->plupload = $plupload; + $this->mimetype_guesser = $mimetype_guesser; + $this->filesystem = $phpbb_filesystem; + } + + /** + * Cleans destination filename + * + * @param real|unique|unique_ext $mode real creates a realname, filtering some characters, lowering every character. Unique creates an unique filename + * @param string $prefix Prefix applied to filename + * @param string $user_id The user_id is only needed for when cleaning a user's avatar + * @access public + */ + function clean_filename($mode = 'unique', $prefix = '', $user_id = '') + { + if ($this->init_error) + { + return; + } + + switch ($mode) + { + case 'real': + // Remove every extension from filename (to not let the mime bug being exposed) + if (strpos($this->realname, '.') !== false) + { + $this->realname = substr($this->realname, 0, strpos($this->realname, '.')); + } + + // Replace any chars which may cause us problems with _ + $bad_chars = array("'", "\\", ' ', '/', ':', '*', '?', '"', '<', '>', '|'); + + $this->realname = rawurlencode(str_replace($bad_chars, '_', strtolower($this->realname))); + $this->realname = preg_replace("/%(\w{2})/", '_', $this->realname); + + $this->realname = $prefix . $this->realname . '.' . $this->extension; + break; + + case 'unique': + $this->realname = $prefix . md5(unique_id()); + break; + + case 'avatar': + $this->extension = strtolower($this->extension); + $this->realname = $prefix . $user_id . '.' . $this->extension; + + break; + + case 'unique_ext': + default: + $this->realname = $prefix . md5(unique_id()) . '.' . $this->extension; + break; + } + } + + /** + * Get property from file object + */ + function get($property) + { + if ($this->init_error || !isset($this->$property)) + { + return false; + } + + return $this->$property; + } + + /** + * Check if file is an image (mimetype) + * + * @return true if it is an image, false if not + */ + function is_image() + { + return (strpos($this->mimetype, 'image/') === 0); + } + + /** + * Check if the file got correctly uploaded + * + * @return true if it is a valid upload, false if not + */ + function is_uploaded() + { + $is_plupload = $this->plupload && $this->plupload->is_active(); + + if (!$this->local && !$is_plupload && !is_uploaded_file($this->filename)) + { + return false; + } + + if (($this->local || $is_plupload) && !file_exists($this->filename)) + { + return false; + } + + return true; + } + + /** + * Remove file + */ + function remove() + { + if ($this->file_moved) + { + @unlink($this->destination_file); + } + } + + /** + * Get file extension + * + * @param string Filename that needs to be checked + * @return string Extension of the supplied filename + */ + static public function get_extension($filename) + { + $filename = utf8_basename($filename); + + if (strpos($filename, '.') === false) + { + return ''; + } + + $filename = explode('.', $filename); + return array_pop($filename); + } + + /** + * Get mimetype + * + * @param string $filename Filename that needs to be checked + * @return string Mimetype of supplied filename + */ + function get_mimetype($filename) + { + if ($this->mimetype_guesser !== null) + { + $mimetype = $this->mimetype_guesser->guess($filename, $this->uploadname); + + if ($mimetype !== 'application/octet-stream') + { + $this->mimetype = $mimetype; + } + } + + return $this->mimetype; + } + + /** + * Get filesize + */ + function get_filesize($filename) + { + return @filesize($filename); + } + + + /** + * Check the first 256 bytes for forbidden content + */ + function check_content($disallowed_content) + { + if (empty($disallowed_content)) + { + return true; + } + + $fp = @fopen($this->filename, 'rb'); + + if ($fp !== false) + { + $ie_mime_relevant = fread($fp, 256); + fclose($fp); + foreach ($disallowed_content as $forbidden) + { + if (stripos($ie_mime_relevant, '<' . $forbidden) !== false) + { + return false; + } + } + } + return true; + } + + /** + * Move file to destination folder + * The phpbb_root_path variable will be applied to the destination path + * + * @param string $destination Destination path, for example $config['avatar_path'] + * @param bool $overwrite If set to true, an already existing file will be overwritten + * @param bool $skip_image_check If set to true, the check for the file to be a valid image is skipped + * @param string $chmod Permission mask for chmodding the file after a successful move. The mode entered here reflects the mode defined by {@link phpbb_chmod()} + * + * @access public + */ + function move_file($destination, $overwrite = false, $skip_image_check = false, $chmod = false) + { + global $user, $phpbb_root_path; + + if (sizeof($this->error)) + { + return false; + } + + $chmod = ($chmod === false) ? CHMOD_READ | CHMOD_WRITE : $chmod; + + // We need to trust the admin in specifying valid upload directories and an attacker not being able to overwrite it... + $this->destination_path = $phpbb_root_path . $destination; + + // Check if the destination path exist... + if (!file_exists($this->destination_path)) + { + @unlink($this->filename); + return false; + } + + $upload_mode = (@ini_get('open_basedir') || @ini_get('safe_mode') || strtolower(@ini_get('safe_mode')) == 'on') ? 'move' : 'copy'; + $upload_mode = ($this->local) ? 'local' : $upload_mode; + $this->destination_file = $this->destination_path . '/' . utf8_basename($this->realname); + + // Check if the file already exist, else there is something wrong... + if (file_exists($this->destination_file) && !$overwrite) + { + @unlink($this->filename); + $this->error[] = $user->lang($this->upload->error_prefix . 'GENERAL_UPLOAD_ERROR', $this->destination_file); + $this->file_moved = false; + return false; + } + else + { + if (file_exists($this->destination_file)) + { + @unlink($this->destination_file); + } + + switch ($upload_mode) + { + case 'copy': + + if (!@copy($this->filename, $this->destination_file)) + { + if (!@move_uploaded_file($this->filename, $this->destination_file)) + { + $this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'GENERAL_UPLOAD_ERROR'], $this->destination_file); + } + } + + break; + + case 'move': + + if (!@move_uploaded_file($this->filename, $this->destination_file)) + { + if (!@copy($this->filename, $this->destination_file)) + { + $this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'GENERAL_UPLOAD_ERROR'], $this->destination_file); + } + } + + break; + + case 'local': + + if (!@copy($this->filename, $this->destination_file)) + { + $this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'GENERAL_UPLOAD_ERROR'], $this->destination_file); + } + + break; + } + + // Remove temporary filename + @unlink($this->filename); + + if (sizeof($this->error)) + { + return false; + } + + try + { + $this->filesystem->phpbb_chmod($this->destination_file, $chmod); + } + catch (\phpbb\filesystem\exception\filesystem_exception $e) + { + // Do nothing + } + } + + // 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; + + // Get imagesize class + $imagesize = new \fastImageSize\fastImageSize(); + + $this->image_info = $imagesize->getImageSize($this->destination_file, $this->mimetype); + + if ($this->image_info !== false) + { + $this->width = $this->image_info['width']; + $this->height = $this->image_info['height']; + + // Check image type + $types = fileupload::image_types(); + + if (!isset($types[$this->image_info['type']]) || !in_array($this->extension, $types[$this->image_info['type']])) + { + if (!isset($types[$this->image_info['type']])) + { + $this->error[] = $user->lang('IMAGE_FILETYPE_INVALID', $this->image_info['type'], $this->mimetype); + } + else + { + $this->error[] = $user->lang('IMAGE_FILETYPE_MISMATCH', $types[$this->image_info['type']][0], $this->extension); + } + } + + // Make sure the dimensions match a valid image + if (empty($this->width) || empty($this->height)) + { + $this->error[] = $user->lang['ATTACHED_IMAGE_NOT_IMAGE']; + } + } + else + { + $this->error[] = $user->lang['UNABLE_GET_IMAGE_SIZE']; + } + } + + $this->file_moved = true; + $this->additional_checks(); + unset($this->upload); + + return true; + } + + /** + * Performing additional checks + */ + function additional_checks() + { + global $user; + + if (!$this->file_moved) + { + return false; + } + + // Filesize is too big or it's 0 if it was larger than the maxsize in the upload form + if ($this->upload->max_filesize && ($this->get('filesize') > $this->upload->max_filesize || $this->filesize == 0)) + { + $max_filesize = get_formatted_filesize($this->upload->max_filesize, false); + + $this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'WRONG_FILESIZE'], $max_filesize['value'], $max_filesize['unit']); + + return false; + } + + if (!$this->upload->valid_dimensions($this)) + { + $this->error[] = $user->lang($this->upload->error_prefix . 'WRONG_SIZE', + $user->lang('PIXELS', (int) $this->upload->min_width), + $user->lang('PIXELS', (int) $this->upload->min_height), + $user->lang('PIXELS', (int) $this->upload->max_width), + $user->lang('PIXELS', (int) $this->upload->max_height), + $user->lang('PIXELS', (int) $this->width), + $user->lang('PIXELS', (int) $this->height)); + + return false; + } + + return true; + } +} From c72d6a71bb71b5abeae05b4a6494ffb166624179 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Sun, 31 May 2015 14:02:02 +0200 Subject: [PATCH 03/66] [ticket/13904] Modify constructor to be instantiatable by container PHPBB3-13904 --- .../default/container/services_files.yml | 4 ++ phpBB/phpbb/files/filespec.php | 41 +++++++++++++------ 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/phpBB/config/default/container/services_files.yml b/phpBB/config/default/container/services_files.yml index a1cec843c5..64d63776a2 100644 --- a/phpBB/config/default/container/services_files.yml +++ b/phpBB/config/default/container/services_files.yml @@ -4,3 +4,7 @@ services: files.filespec: class: phpbb\files\filespec + arguments: + - @filesystem + - @mimetype.guesser + - @plupload diff --git a/phpBB/phpbb/files/filespec.php b/phpBB/phpbb/files/filespec.php index f74e91b0d5..4e52d13d01 100644 --- a/phpBB/phpbb/files/filespec.php +++ b/phpBB/phpbb/files/filespec.php @@ -33,7 +33,6 @@ class filespec var $destination_path = ''; var $file_moved = false; - var $init_error = false; var $local = false; var $error = array(); @@ -61,14 +60,19 @@ class filespec * File Class * @access private */ - function filespec($upload_ary, $upload_namespace, \phpbb\filesystem\filesystem_interface $phpbb_filesystem, \phpbb\mimetype\guesser $mimetype_guesser = null, \phpbb\plupload\plupload $plupload = null) + function filespec(\phpbb\filesystem\filesystem_interface $phpbb_filesystem, \phpbb\mimetype\guesser $mimetype_guesser = null, \phpbb\plupload\plupload $plupload = null) { - if (!isset($upload_ary)) - { - $this->init_error = true; - return; - } + // @todo call this via files + //$this->set_upload_ary($upload_ary); + //$this->set_upload_namespace($upload_namespace); + $this->plupload = $plupload; + $this->mimetype_guesser = $mimetype_guesser; + $this->filesystem = $phpbb_filesystem; + } + + public function set_upload_ary($upload_ary) + { $this->filename = $upload_ary['tmp_name']; $this->filesize = $upload_ary['size']; $name = (STRIP) ? stripslashes($upload_ary['name']) : $upload_ary['name']; @@ -93,10 +97,21 @@ class filespec $this->file_moved = false; $this->local = (isset($upload_ary['local_mode'])) ? true : false; - $this->upload = $upload_namespace; - $this->plupload = $plupload; - $this->mimetype_guesser = $mimetype_guesser; - $this->filesystem = $phpbb_filesystem; + } + + public function set_upload_namespace($namespace) + { + $this->upload = $namespace; + } + + /** + * Check if class members were not properly initalised yet + * + * @return bool True if there was an init error, false if not + */ + protected function init_error() + { + return !isset($upload_ary); } /** @@ -109,7 +124,7 @@ class filespec */ function clean_filename($mode = 'unique', $prefix = '', $user_id = '') { - if ($this->init_error) + if ($this->init_error()) { return; } @@ -154,7 +169,7 @@ class filespec */ function get($property) { - if ($this->init_error || !isset($this->$property)) + if ($this->init_error() || !isset($this->$property)) { return false; } From 891ffb8ac76637f1ee84c16464ec6ad654131126 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Wed, 9 Sep 2015 08:27:13 +0200 Subject: [PATCH 04/66] [ticket/13904] Remove filespec class from functions_upload PHPBB3-13904 --- phpBB/includes/functions_upload.php | 468 +--------------------------- 1 file changed, 1 insertion(+), 467 deletions(-) diff --git a/phpBB/includes/functions_upload.php b/phpBB/includes/functions_upload.php index 3ab87ee794..220d31098c 100644 --- a/phpBB/includes/functions_upload.php +++ b/phpBB/includes/functions_upload.php @@ -19,476 +19,10 @@ if (!defined('IN_PHPBB')) exit; } -/** -* Responsible for holding all file relevant information, as well as doing file-specific operations. -* The {@link fileupload fileupload class} can be used to upload several files, each of them being this object to operate further on. -*/ -class filespec -{ - var $filename = ''; - var $realname = ''; - var $uploadname = ''; - var $mimetype = ''; - var $extension = ''; - var $filesize = 0; - var $width = 0; - var $height = 0; - var $image_info = array(); - - var $destination_file = ''; - var $destination_path = ''; - - var $file_moved = false; - var $init_error = false; - var $local = false; - - var $error = array(); - - var $upload = ''; - - /** - * @var \phpbb\filesystem\filesystem_interface - */ - protected $filesystem; - - /** - * The plupload object - * @var \phpbb\plupload\plupload - */ - protected $plupload; - - /** - * phpBB Mimetype guesser - * @var \phpbb\mimetype\guesser - */ - protected $mimetype_guesser; - - /** - * File Class - * @access private - */ - function filespec($upload_ary, $upload_namespace, \phpbb\filesystem\filesystem_interface $phpbb_filesystem, \phpbb\mimetype\guesser $mimetype_guesser = null, \phpbb\plupload\plupload $plupload = null) - { - if (!isset($upload_ary)) - { - $this->init_error = true; - return; - } - - $this->filename = $upload_ary['tmp_name']; - $this->filesize = $upload_ary['size']; - $name = (STRIP) ? stripslashes($upload_ary['name']) : $upload_ary['name']; - $name = trim(utf8_basename($name)); - $this->realname = $this->uploadname = $name; - $this->mimetype = $upload_ary['type']; - - // Opera adds the name to the mime type - $this->mimetype = (strpos($this->mimetype, '; name') !== false) ? str_replace(strstr($this->mimetype, '; name'), '', $this->mimetype) : $this->mimetype; - - if (!$this->mimetype) - { - $this->mimetype = 'application/octet-stream'; - } - - $this->extension = strtolower(self::get_extension($this->realname)); - - // Try to get real filesize from temporary folder (not always working) ;) - $this->filesize = (@filesize($this->filename)) ? @filesize($this->filename) : $this->filesize; - - $this->width = $this->height = 0; - $this->file_moved = false; - - $this->local = (isset($upload_ary['local_mode'])) ? true : false; - $this->upload = $upload_namespace; - $this->plupload = $plupload; - $this->mimetype_guesser = $mimetype_guesser; - $this->filesystem = $phpbb_filesystem; - } - - /** - * Cleans destination filename - * - * @param real|unique|unique_ext $mode real creates a realname, filtering some characters, lowering every character. Unique creates an unique filename - * @param string $prefix Prefix applied to filename - * @param string $user_id The user_id is only needed for when cleaning a user's avatar - * @access public - */ - function clean_filename($mode = 'unique', $prefix = '', $user_id = '') - { - if ($this->init_error) - { - return; - } - - switch ($mode) - { - case 'real': - // Remove every extension from filename (to not let the mime bug being exposed) - if (strpos($this->realname, '.') !== false) - { - $this->realname = substr($this->realname, 0, strpos($this->realname, '.')); - } - - // Replace any chars which may cause us problems with _ - $bad_chars = array("'", "\\", ' ', '/', ':', '*', '?', '"', '<', '>', '|'); - - $this->realname = rawurlencode(str_replace($bad_chars, '_', strtolower($this->realname))); - $this->realname = preg_replace("/%(\w{2})/", '_', $this->realname); - - $this->realname = $prefix . $this->realname . '.' . $this->extension; - break; - - case 'unique': - $this->realname = $prefix . md5(unique_id()); - break; - - case 'avatar': - $this->extension = strtolower($this->extension); - $this->realname = $prefix . $user_id . '.' . $this->extension; - - break; - - case 'unique_ext': - default: - $this->realname = $prefix . md5(unique_id()) . '.' . $this->extension; - break; - } - } - - /** - * Get property from file object - */ - function get($property) - { - if ($this->init_error || !isset($this->$property)) - { - return false; - } - - return $this->$property; - } - - /** - * Check if file is an image (mimetype) - * - * @return true if it is an image, false if not - */ - function is_image() - { - return (strpos($this->mimetype, 'image/') === 0); - } - - /** - * Check if the file got correctly uploaded - * - * @return true if it is a valid upload, false if not - */ - function is_uploaded() - { - $is_plupload = $this->plupload && $this->plupload->is_active(); - - if (!$this->local && !$is_plupload && !is_uploaded_file($this->filename)) - { - return false; - } - - if (($this->local || $is_plupload) && !file_exists($this->filename)) - { - return false; - } - - return true; - } - - /** - * Remove file - */ - function remove() - { - if ($this->file_moved) - { - @unlink($this->destination_file); - } - } - - /** - * Get file extension - * - * @param string Filename that needs to be checked - * @return string Extension of the supplied filename - */ - static public function get_extension($filename) - { - $filename = utf8_basename($filename); - - if (strpos($filename, '.') === false) - { - return ''; - } - - $filename = explode('.', $filename); - return array_pop($filename); - } - - /** - * Get mimetype - * - * @param string $filename Filename that needs to be checked - * @return string Mimetype of supplied filename - */ - function get_mimetype($filename) - { - if ($this->mimetype_guesser !== null) - { - $mimetype = $this->mimetype_guesser->guess($filename, $this->uploadname); - - if ($mimetype !== 'application/octet-stream') - { - $this->mimetype = $mimetype; - } - } - - return $this->mimetype; - } - - /** - * Get filesize - */ - function get_filesize($filename) - { - return @filesize($filename); - } - - - /** - * Check the first 256 bytes for forbidden content - */ - function check_content($disallowed_content) - { - if (empty($disallowed_content)) - { - return true; - } - - $fp = @fopen($this->filename, 'rb'); - - if ($fp !== false) - { - $ie_mime_relevant = fread($fp, 256); - fclose($fp); - foreach ($disallowed_content as $forbidden) - { - if (stripos($ie_mime_relevant, '<' . $forbidden) !== false) - { - return false; - } - } - } - return true; - } - - /** - * Move file to destination folder - * The phpbb_root_path variable will be applied to the destination path - * - * @param string $destination Destination path, for example $config['avatar_path'] - * @param bool $overwrite If set to true, an already existing file will be overwritten - * @param bool $skip_image_check If set to true, the check for the file to be a valid image is skipped - * @param string $chmod Permission mask for chmodding the file after a successful move. The mode entered here reflects the mode defined by {@link phpbb_chmod()} - * - * @access public - */ - function move_file($destination, $overwrite = false, $skip_image_check = false, $chmod = false) - { - global $user, $phpbb_root_path; - - if (sizeof($this->error)) - { - return false; - } - - $chmod = ($chmod === false) ? CHMOD_READ | CHMOD_WRITE : $chmod; - - // We need to trust the admin in specifying valid upload directories and an attacker not being able to overwrite it... - $this->destination_path = $phpbb_root_path . $destination; - - // Check if the destination path exist... - if (!file_exists($this->destination_path)) - { - @unlink($this->filename); - return false; - } - - $upload_mode = (@ini_get('open_basedir') || @ini_get('safe_mode') || strtolower(@ini_get('safe_mode')) == 'on') ? 'move' : 'copy'; - $upload_mode = ($this->local) ? 'local' : $upload_mode; - $this->destination_file = $this->destination_path . '/' . utf8_basename($this->realname); - - // Check if the file already exist, else there is something wrong... - if (file_exists($this->destination_file) && !$overwrite) - { - @unlink($this->filename); - $this->error[] = $user->lang($this->upload->error_prefix . 'GENERAL_UPLOAD_ERROR', $this->destination_file); - $this->file_moved = false; - return false; - } - else - { - if (file_exists($this->destination_file)) - { - @unlink($this->destination_file); - } - - switch ($upload_mode) - { - case 'copy': - - if (!@copy($this->filename, $this->destination_file)) - { - if (!@move_uploaded_file($this->filename, $this->destination_file)) - { - $this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'GENERAL_UPLOAD_ERROR'], $this->destination_file); - } - } - - break; - - case 'move': - - if (!@move_uploaded_file($this->filename, $this->destination_file)) - { - if (!@copy($this->filename, $this->destination_file)) - { - $this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'GENERAL_UPLOAD_ERROR'], $this->destination_file); - } - } - - break; - - case 'local': - - if (!@copy($this->filename, $this->destination_file)) - { - $this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'GENERAL_UPLOAD_ERROR'], $this->destination_file); - } - - break; - } - - // Remove temporary filename - @unlink($this->filename); - - if (sizeof($this->error)) - { - return false; - } - - try - { - $this->filesystem->phpbb_chmod($this->destination_file, $chmod); - } - catch (\phpbb\filesystem\exception\filesystem_exception $e) - { - // Do nothing - } - } - - // 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; - - // Get imagesize class - $imagesize = new \FastImageSize\FastImageSize(); - - $this->image_info = $imagesize->getImageSize($this->destination_file, $this->mimetype); - - if ($this->image_info !== false) - { - $this->width = $this->image_info['width']; - $this->height = $this->image_info['height']; - - // Check image type - $types = fileupload::image_types(); - - if (!isset($types[$this->image_info['type']]) || !in_array($this->extension, $types[$this->image_info['type']])) - { - if (!isset($types[$this->image_info['type']])) - { - $this->error[] = $user->lang('IMAGE_FILETYPE_INVALID', $this->image_info['type'], $this->mimetype); - } - else - { - $this->error[] = $user->lang('IMAGE_FILETYPE_MISMATCH', $types[$this->image_info['type']][0], $this->extension); - } - } - - // Make sure the dimensions match a valid image - if (empty($this->width) || empty($this->height)) - { - $this->error[] = $user->lang['ATTACHED_IMAGE_NOT_IMAGE']; - } - } - else - { - $this->error[] = $user->lang['UNABLE_GET_IMAGE_SIZE']; - } - } - - $this->file_moved = true; - $this->additional_checks(); - unset($this->upload); - - return true; - } - - /** - * Performing additional checks - */ - function additional_checks() - { - global $user; - - if (!$this->file_moved) - { - return false; - } - - // Filesize is too big or it's 0 if it was larger than the maxsize in the upload form - if ($this->upload->max_filesize && ($this->get('filesize') > $this->upload->max_filesize || $this->filesize == 0)) - { - $max_filesize = get_formatted_filesize($this->upload->max_filesize, false); - - $this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'WRONG_FILESIZE'], $max_filesize['value'], $max_filesize['unit']); - - return false; - } - - if (!$this->upload->valid_dimensions($this)) - { - $this->error[] = $user->lang($this->upload->error_prefix . 'WRONG_SIZE', - $user->lang('PIXELS', (int) $this->upload->min_width), - $user->lang('PIXELS', (int) $this->upload->min_height), - $user->lang('PIXELS', (int) $this->upload->max_width), - $user->lang('PIXELS', (int) $this->upload->max_height), - $user->lang('PIXELS', (int) $this->width), - $user->lang('PIXELS', (int) $this->height)); - - return false; - } - - return true; - } -} - /** * Class for assigning error messages before a real filespec class can be assigned */ -class fileerror extends filespec +class fileerror extends \phpbb\files\filespec { function fileerror($error_msg) { From 0cbb713cc2a6249cb12507db7d0fa78ce8663ae6 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Sun, 31 May 2015 14:47:57 +0200 Subject: [PATCH 05/66] [ticket/13904] Fix uploading for use with new filespec class PHPBB3-13904 --- phpBB/includes/functions_posting.php | 2 +- phpBB/includes/functions_upload.php | 16 +++++++++++----- phpBB/phpbb/files/filespec.php | 8 ++++---- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/phpBB/includes/functions_posting.php b/phpBB/includes/functions_posting.php index e4520d7f03..e91f8bb56d 100644 --- a/phpBB/includes/functions_posting.php +++ b/phpBB/includes/functions_posting.php @@ -435,7 +435,7 @@ function upload_attachment($form_name, $forum_id, $local = false, $local_storage $file = ($local) ? $upload->local_upload($local_storage, $local_filedata, $mimetype_guesser) : $upload->form_upload($form_name, $mimetype_guesser, $plupload); - if ($file->init_error) + if ($file->init_error()) { $filedata['post_attach'] = false; return $filedata; diff --git a/phpBB/includes/functions_upload.php b/phpBB/includes/functions_upload.php index 220d31098c..c759631c47 100644 --- a/phpBB/includes/functions_upload.php +++ b/phpBB/includes/functions_upload.php @@ -169,9 +169,11 @@ class fileupload } } - $file = new filespec($upload, $this, $this->filesystem, $mimetype_guesser, $plupload); + $file = new \phpbb\files\filespec($this->filesystem, $mimetype_guesser, $plupload); + $file->set_upload_ary($upload); + $file->set_upload_namespace($this); - if ($file->init_error) + if ($file->init_error()) { $file->error[] = ''; return $file; @@ -250,9 +252,11 @@ class fileupload $upload['type'] = $filedata['type']; } - $file = new filespec($upload, $this, $this->filesystem, $mimetype_guesser); + $file = new \phpbb\files\filespec($this->filesystem, $mimetype_guesser); + $file->set_upload_ary($upload); + $file->set_upload_namespace($this); - if ($file->init_error) + if ($file->init_error()) { $file->error[] = ''; return $file; @@ -488,7 +492,9 @@ class fileupload $upload_ary['tmp_name'] = $filename; - $file = new filespec($upload_ary, $this, $this->filesystem, $mimetype_guesser); + $file = new \phpbb\files\filespec($this->filesystem, $mimetype_guesser); + $file->set_upload_ary($upload_ary); + $file->set_upload_namespace($this); $this->common_checks($file); return $file; diff --git a/phpBB/phpbb/files/filespec.php b/phpBB/phpbb/files/filespec.php index 4e52d13d01..8501b217f7 100644 --- a/phpBB/phpbb/files/filespec.php +++ b/phpBB/phpbb/files/filespec.php @@ -60,7 +60,7 @@ class filespec * File Class * @access private */ - function filespec(\phpbb\filesystem\filesystem_interface $phpbb_filesystem, \phpbb\mimetype\guesser $mimetype_guesser = null, \phpbb\plupload\plupload $plupload = null) + function __construct(\phpbb\filesystem\filesystem_interface $phpbb_filesystem, \phpbb\mimetype\guesser $mimetype_guesser = null, \phpbb\plupload\plupload $plupload = null) { // @todo call this via files //$this->set_upload_ary($upload_ary); @@ -109,9 +109,9 @@ class filespec * * @return bool True if there was an init error, false if not */ - protected function init_error() + public function init_error() { - return !isset($upload_ary); + return !isset($this->filename); } /** @@ -422,7 +422,7 @@ class filespec $this->height = $this->image_info['height']; // Check image type - $types = fileupload::image_types(); + $types = \fileupload::image_types(); if (!isset($types[$this->image_info['type']]) || !in_array($this->extension, $types[$this->image_info['type']])) { From 92e49cd0acef56b78fda3bcffebb7a0958891b82 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Mon, 1 Jun 2015 13:27:29 +0200 Subject: [PATCH 06/66] [ticket/13904] Turn filespec into prototype and improve init methods PHPBB3-13904 --- .../default/container/services_files.yml | 1 + phpBB/includes/functions_upload.php | 27 ++++++++++--------- phpBB/phpbb/files/filespec.php | 4 +++ 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/phpBB/config/default/container/services_files.yml b/phpBB/config/default/container/services_files.yml index 64d63776a2..6f0b10cce1 100644 --- a/phpBB/config/default/container/services_files.yml +++ b/phpBB/config/default/container/services_files.yml @@ -4,6 +4,7 @@ services: files.filespec: class: phpbb\files\filespec + scope: prototype arguments: - @filesystem - @mimetype.guesser diff --git a/phpBB/includes/functions_upload.php b/phpBB/includes/functions_upload.php index c759631c47..3170a47033 100644 --- a/phpBB/includes/functions_upload.php +++ b/phpBB/includes/functions_upload.php @@ -155,7 +155,7 @@ class fileupload */ function form_upload($form_name, \phpbb\mimetype\guesser $mimetype_guesser = null, \phpbb\plupload\plupload $plupload = null) { - global $user, $request; + global $user, $request, $phpbb_container; $upload = $request->file($form_name); unset($upload['local_mode']); @@ -169,9 +169,10 @@ class fileupload } } - $file = new \phpbb\files\filespec($this->filesystem, $mimetype_guesser, $plupload); - $file->set_upload_ary($upload); - $file->set_upload_namespace($this); + /** @var \phpbb\files\filespec $file */ + $file = $phpbb_container->get('files.filespec') + ->set_upload_ary($upload) + ->set_upload_namespace($this); if ($file->init_error()) { @@ -233,7 +234,7 @@ class fileupload */ function local_upload($source_file, $filedata = false, \phpbb\mimetype\guesser $mimetype_guesser = null) { - global $user, $request; + global $user, $request, $phpbb_container; $upload = array(); @@ -252,9 +253,10 @@ class fileupload $upload['type'] = $filedata['type']; } - $file = new \phpbb\files\filespec($this->filesystem, $mimetype_guesser); - $file->set_upload_ary($upload); - $file->set_upload_namespace($this); + /** @var \phpbb\files\filespec $file */ + $file = $phpbb_container->get('files.filespec') + ->set_upload_ary($upload) + ->set_upload_namespace($this); if ($file->init_error()) { @@ -315,7 +317,7 @@ class fileupload */ function remote_upload($upload_url, \phpbb\mimetype\guesser $mimetype_guesser = null) { - global $user, $phpbb_root_path; + global $user, $phpbb_root_path, $phpbb_container; $upload_ary = array(); $upload_ary['local_mode'] = true; @@ -492,9 +494,10 @@ class fileupload $upload_ary['tmp_name'] = $filename; - $file = new \phpbb\files\filespec($this->filesystem, $mimetype_guesser); - $file->set_upload_ary($upload_ary); - $file->set_upload_namespace($this); + /** @var \phpbb\files\filespec $file */ + $file = $phpbb_container->get('files.filespec') + ->set_upload_ary($upload_ary) + ->set_upload_namespace($this); $this->common_checks($file); return $file; diff --git a/phpBB/phpbb/files/filespec.php b/phpBB/phpbb/files/filespec.php index 8501b217f7..3f50488e7c 100644 --- a/phpBB/phpbb/files/filespec.php +++ b/phpBB/phpbb/files/filespec.php @@ -97,11 +97,15 @@ class filespec $this->file_moved = false; $this->local = (isset($upload_ary['local_mode'])) ? true : false; + + return $this; } public function set_upload_namespace($namespace) { $this->upload = $namespace; + + return $this; } /** From d2be8e1503db3686c62b23973511c61dea7a6616 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Mon, 1 Jun 2015 13:30:17 +0200 Subject: [PATCH 07/66] [ticket/13904] Add fileupload class to files classes PHPBB3-13904 --- phpBB/phpbb/files/upload.php | 655 +++++++++++++++++++++++++++++++++++ 1 file changed, 655 insertions(+) create mode 100644 phpBB/phpbb/files/upload.php diff --git a/phpBB/phpbb/files/upload.php b/phpBB/phpbb/files/upload.php new file mode 100644 index 0000000000..7501247e06 --- /dev/null +++ b/phpBB/phpbb/files/upload.php @@ -0,0 +1,655 @@ + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +namespace phpbb\files; + +/** + * File upload class + * Init class (all parameters optional and able to be set/overwritten separately) - scope is global and valid for all uploads + */ +class upload +{ + var $allowed_extensions = array(); + var $disallowed_content = array('body', 'head', 'html', 'img', 'plaintext', 'a href', 'pre', 'script', 'table', 'title'); + var $max_filesize = 0; + var $min_width = 0; + var $min_height = 0; + var $max_width = 0; + var $max_height = 0; + var $error_prefix = ''; + + /** @var int Timeout for remote upload */ + var $upload_timeout = 6; + + /** + * @var \phpbb\filesystem\filesystem_interface + */ + protected $filesystem; + + /** + * Init file upload class. + * + * @param \phpbb\filesystem\filesystem_interface $filesystem + * @param string $error_prefix Used error messages will get prefixed by this string + * @param array $allowed_extensions Array of allowed extensions, for example array('jpg', 'jpeg', 'gif', 'png') + * @param int $max_filesize Maximum filesize + * @param int $min_width Minimum image width (only checked for images) + * @param int $min_height Minimum image height (only checked for images) + * @param int $max_width Maximum image width (only checked for images) + * @param int $max_height Maximum image height (only checked for images) + * @param bool|array $disallowed_content If enabled, the first 256 bytes of the file must not + * contain any of its values. Defaults to false. + * + */ + function fileupload(\phpbb\filesystem\filesystem_interface $filesystem, $error_prefix = '', $allowed_extensions = false, $max_filesize = false, $min_width = false, $min_height = false, $max_width = false, $max_height = false, $disallowed_content = false) + { + $this->set_allowed_extensions($allowed_extensions); + $this->set_max_filesize($max_filesize); + $this->set_allowed_dimensions($min_width, $min_height, $max_width, $max_height); + $this->set_error_prefix($error_prefix); + $this->set_disallowed_content($disallowed_content); + $this->filesystem = $filesystem; + } + + /** + * Reset vars + */ + function reset_vars() + { + $this->max_filesize = 0; + $this->min_width = $this->min_height = $this->max_width = $this->max_height = 0; + $this->error_prefix = ''; + $this->allowed_extensions = array(); + $this->disallowed_content = array(); + } + + /** + * Set allowed extensions + */ + function set_allowed_extensions($allowed_extensions) + { + if ($allowed_extensions !== false && is_array($allowed_extensions)) + { + $this->allowed_extensions = $allowed_extensions; + } + } + + /** + * Set allowed dimensions + */ + function set_allowed_dimensions($min_width, $min_height, $max_width, $max_height) + { + $this->min_width = (int) $min_width; + $this->min_height = (int) $min_height; + $this->max_width = (int) $max_width; + $this->max_height = (int) $max_height; + } + + /** + * Set maximum allowed filesize + */ + function set_max_filesize($max_filesize) + { + if ($max_filesize !== false && (int) $max_filesize) + { + $this->max_filesize = (int) $max_filesize; + } + } + + /** + * Set disallowed strings + */ + function set_disallowed_content($disallowed_content) + { + if ($disallowed_content !== false && is_array($disallowed_content)) + { + $this->disallowed_content = array_diff($disallowed_content, array('')); + } + } + + /** + * Set error prefix + */ + function set_error_prefix($error_prefix) + { + $this->error_prefix = $error_prefix; + } + + /** + * Form upload method + * Upload file from users harddisk + * + * @param string $form_name Form name assigned to the file input field (if it is an array, the key has to be specified) + * @param \phpbb\mimetype\guesser $mimetype_guesser Mimetype guesser + * @param \phpbb\plupload\plupload $plupload The plupload object + * + * @return object $file Object "filespec" is returned, all further operations can be done with this object + * @access public + */ + function form_upload($form_name, \phpbb\mimetype\guesser $mimetype_guesser = null, \phpbb\plupload\plupload $plupload = null) + { + global $user, $request, $phpbb_container; + + $upload = $request->file($form_name); + unset($upload['local_mode']); + + if ($plupload) + { + $result = $plupload->handle_upload($form_name); + if (is_array($result)) + { + $upload = array_merge($upload, $result); + } + } + + /** @var \phpbb\files\filespec $file */ + $file = $phpbb_container->get('files.filespec') + ->set_upload_ary($upload) + ->set_upload_namespace($this); + + if ($file->init_error()) + { + $file->error[] = ''; + return $file; + } + + // Error array filled? + if (isset($upload['error'])) + { + $error = $this->assign_internal_error($upload['error']); + + if ($error !== false) + { + $file->error[] = $error; + return $file; + } + } + + // Check if empty file got uploaded (not catched by is_uploaded_file) + if (isset($upload['size']) && $upload['size'] == 0) + { + $file->error[] = $user->lang[$this->error_prefix . 'EMPTY_FILEUPLOAD']; + return $file; + } + + // PHP Upload filesize exceeded + if ($file->get('filename') == 'none') + { + $max_filesize = @ini_get('upload_max_filesize'); + $unit = 'MB'; + + if (!empty($max_filesize)) + { + $unit = strtolower(substr($max_filesize, -1, 1)); + $max_filesize = (int) $max_filesize; + + $unit = ($unit == 'k') ? 'KB' : (($unit == 'g') ? 'GB' : 'MB'); + } + + $file->error[] = (empty($max_filesize)) ? $user->lang[$this->error_prefix . 'PHP_SIZE_NA'] : sprintf($user->lang[$this->error_prefix . 'PHP_SIZE_OVERRUN'], $max_filesize, $user->lang[$unit]); + return $file; + } + + // Not correctly uploaded + if (!$file->is_uploaded()) + { + $file->error[] = $user->lang[$this->error_prefix . 'NOT_UPLOADED']; + return $file; + } + + $this->common_checks($file); + + return $file; + } + + /** + * Move file from another location to phpBB + */ + function local_upload($source_file, $filedata = false, \phpbb\mimetype\guesser $mimetype_guesser = null) + { + global $user, $request, $phpbb_container; + + $upload = array(); + + $upload['local_mode'] = true; + $upload['tmp_name'] = $source_file; + + if ($filedata === false) + { + $upload['name'] = utf8_basename($source_file); + $upload['size'] = 0; + } + else + { + $upload['name'] = $filedata['realname']; + $upload['size'] = $filedata['size']; + $upload['type'] = $filedata['type']; + } + + /** @var \phpbb\files\filespec $file */ + $file = $phpbb_container->get('files.filespec') + ->set_upload_ary($upload) + ->set_upload_namespace($this); + + if ($file->init_error()) + { + $file->error[] = ''; + return $file; + } + + if (isset($upload['error'])) + { + $error = $this->assign_internal_error($upload['error']); + + if ($error !== false) + { + $file->error[] = $error; + return $file; + } + } + + // PHP Upload filesize exceeded + if ($file->get('filename') == 'none') + { + $max_filesize = @ini_get('upload_max_filesize'); + $unit = 'MB'; + + if (!empty($max_filesize)) + { + $unit = strtolower(substr($max_filesize, -1, 1)); + $max_filesize = (int) $max_filesize; + + $unit = ($unit == 'k') ? 'KB' : (($unit == 'g') ? 'GB' : 'MB'); + } + + $file->error[] = (empty($max_filesize)) ? $user->lang[$this->error_prefix . 'PHP_SIZE_NA'] : sprintf($user->lang[$this->error_prefix . 'PHP_SIZE_OVERRUN'], $max_filesize, $user->lang[$unit]); + return $file; + } + + // Not correctly uploaded + if (!$file->is_uploaded()) + { + $file->error[] = $user->lang[$this->error_prefix . 'NOT_UPLOADED']; + return $file; + } + + $this->common_checks($file); + $request->overwrite('local', $upload, \phpbb\request\request_interface::FILES); + + return $file; + } + + /** + * Remote upload method + * Uploads file from given url + * + * @param string $upload_url URL pointing to file to upload, for example http://www.foobar.com/example.gif + * @param \phpbb\mimetype\guesser $mimetype_guesser Mimetype guesser + * @return object $file Object "filespec" is returned, all further operations can be done with this object + * @access public + */ + function remote_upload($upload_url, \phpbb\mimetype\guesser $mimetype_guesser = null) + { + global $user, $phpbb_root_path, $phpbb_container; + + $upload_ary = array(); + $upload_ary['local_mode'] = true; + + if (!preg_match('#^(https?://).*?\.(' . implode('|', $this->allowed_extensions) . ')$#i', $upload_url, $match)) + { + $file = new fileerror($user->lang[$this->error_prefix . 'URL_INVALID']); + return $file; + } + + if (empty($match[2])) + { + $file = new fileerror($user->lang[$this->error_prefix . 'URL_INVALID']); + return $file; + } + + $url = parse_url($upload_url); + + $host = $url['host']; + $path = $url['path']; + $port = (!empty($url['port'])) ? (int) $url['port'] : 80; + + $upload_ary['type'] = 'application/octet-stream'; + + $url['path'] = explode('.', $url['path']); + $ext = array_pop($url['path']); + + $url['path'] = implode('', $url['path']); + $upload_ary['name'] = utf8_basename($url['path']) . (($ext) ? '.' . $ext : ''); + $filename = $url['path']; + $filesize = 0; + + $remote_max_filesize = $this->max_filesize; + if (!$remote_max_filesize) + { + $max_filesize = @ini_get('upload_max_filesize'); + + if (!empty($max_filesize)) + { + $unit = strtolower(substr($max_filesize, -1, 1)); + $remote_max_filesize = (int) $max_filesize; + + switch ($unit) + { + case 'g': + $remote_max_filesize *= 1024; + // no break + case 'm': + $remote_max_filesize *= 1024; + // no break + case 'k': + $remote_max_filesize *= 1024; + // no break + } + } + } + + $errno = 0; + $errstr = ''; + + if (!($fsock = @fsockopen($host, $port, $errno, $errstr))) + { + $file = new fileerror($user->lang[$this->error_prefix . 'NOT_UPLOADED']); + return $file; + } + + // Make sure $path not beginning with / + if (strpos($path, '/') === 0) + { + $path = substr($path, 1); + } + + fputs($fsock, 'GET /' . $path . " HTTP/1.1\r\n"); + fputs($fsock, "HOST: " . $host . "\r\n"); + fputs($fsock, "Connection: close\r\n\r\n"); + + // Set a proper timeout for the socket + socket_set_timeout($fsock, $this->upload_timeout); + + $get_info = false; + $data = ''; + $length = false; + $timer_stop = time() + $this->upload_timeout; + + while ((!$length || $filesize < $length) && !@feof($fsock)) + { + if ($get_info) + { + if ($length) + { + // Don't attempt to read past end of file if server indicated length + $block = @fread($fsock, min($length - $filesize, 1024)); + } + else + { + $block = @fread($fsock, 1024); + } + + $filesize += strlen($block); + + if ($remote_max_filesize && $filesize > $remote_max_filesize) + { + $max_filesize = get_formatted_filesize($remote_max_filesize, false); + + $file = new fileerror(sprintf($user->lang[$this->error_prefix . 'WRONG_FILESIZE'], $max_filesize['value'], $max_filesize['unit'])); + return $file; + } + + $data .= $block; + } + else + { + $line = @fgets($fsock, 1024); + + if ($line == "\r\n") + { + $get_info = true; + } + else + { + if (stripos($line, 'content-type: ') !== false) + { + $upload_ary['type'] = rtrim(str_replace('content-type: ', '', strtolower($line))); + } + else if ($this->max_filesize && stripos($line, 'content-length: ') !== false) + { + $length = (int) str_replace('content-length: ', '', strtolower($line)); + + if ($remote_max_filesize && $length && $length > $remote_max_filesize) + { + $max_filesize = get_formatted_filesize($remote_max_filesize, false); + + $file = new fileerror(sprintf($user->lang[$this->error_prefix . 'WRONG_FILESIZE'], $max_filesize['value'], $max_filesize['unit'])); + return $file; + } + } + else if (stripos($line, '404 not found') !== false) + { + $file = new fileerror($user->lang[$this->error_prefix . 'URL_NOT_FOUND']); + return $file; + } + } + } + + $stream_meta_data = stream_get_meta_data($fsock); + + // Cancel upload if we exceed timeout + if (!empty($stream_meta_data['timed_out']) || time() >= $timer_stop) + { + $file = new fileerror($user->lang[$this->error_prefix . 'REMOTE_UPLOAD_TIMEOUT']); + return $file; + } + } + @fclose($fsock); + + if (empty($data)) + { + $file = new fileerror($user->lang[$this->error_prefix . 'EMPTY_REMOTE_DATA']); + return $file; + } + + $tmp_path = (!@ini_get('safe_mode') || strtolower(@ini_get('safe_mode')) == 'off') ? false : $phpbb_root_path . 'cache'; + $filename = tempnam($tmp_path, unique_id() . '-'); + + if (!($fp = @fopen($filename, 'wb'))) + { + $file = new fileerror($user->lang[$this->error_prefix . 'NOT_UPLOADED']); + return $file; + } + + $upload_ary['size'] = fwrite($fp, $data); + fclose($fp); + unset($data); + + $upload_ary['tmp_name'] = $filename; + + /** @var \phpbb\files\filespec $file */ + $file = $phpbb_container->get('files.filespec') + ->set_upload_ary($upload_ary) + ->set_upload_namespace($this); + $this->common_checks($file); + + return $file; + } + + /** + * Assign internal error + * @access private + */ + function assign_internal_error($errorcode) + { + global $user; + + switch ($errorcode) + { + case 1: + $max_filesize = @ini_get('upload_max_filesize'); + $unit = 'MB'; + + if (!empty($max_filesize)) + { + $unit = strtolower(substr($max_filesize, -1, 1)); + $max_filesize = (int) $max_filesize; + + $unit = ($unit == 'k') ? 'KB' : (($unit == 'g') ? 'GB' : 'MB'); + } + + $error = (empty($max_filesize)) ? $user->lang[$this->error_prefix . 'PHP_SIZE_NA'] : sprintf($user->lang[$this->error_prefix . 'PHP_SIZE_OVERRUN'], $max_filesize, $user->lang[$unit]); + break; + + case 2: + $max_filesize = get_formatted_filesize($this->max_filesize, false); + + $error = sprintf($user->lang[$this->error_prefix . 'WRONG_FILESIZE'], $max_filesize['value'], $max_filesize['unit']); + break; + + case 3: + $error = $user->lang[$this->error_prefix . 'PARTIAL_UPLOAD']; + break; + + case 4: + $error = $user->lang[$this->error_prefix . 'NOT_UPLOADED']; + break; + + case 6: + $error = 'Temporary folder could not be found. Please check your PHP installation.'; + break; + + default: + $error = false; + break; + } + + return $error; + } + + /** + * Perform common checks + */ + function common_checks(&$file) + { + global $user; + + // Filesize is too big or it's 0 if it was larger than the maxsize in the upload form + if ($this->max_filesize && ($file->get('filesize') > $this->max_filesize || $file->get('filesize') == 0)) + { + $max_filesize = get_formatted_filesize($this->max_filesize, false); + + $file->error[] = sprintf($user->lang[$this->error_prefix . 'WRONG_FILESIZE'], $max_filesize['value'], $max_filesize['unit']); + } + + // check Filename + if (preg_match("#[\\/:*?\"<>|]#i", $file->get('realname'))) + { + $file->error[] = sprintf($user->lang[$this->error_prefix . 'INVALID_FILENAME'], $file->get('realname')); + } + + // Invalid Extension + if (!$this->valid_extension($file)) + { + $file->error[] = sprintf($user->lang[$this->error_prefix . 'DISALLOWED_EXTENSION'], $file->get('extension')); + } + + // MIME Sniffing + if (!$this->valid_content($file)) + { + $file->error[] = sprintf($user->lang[$this->error_prefix . 'DISALLOWED_CONTENT']); + } + } + + /** + * Check for allowed extension + */ + function valid_extension(&$file) + { + return (in_array($file->get('extension'), $this->allowed_extensions)) ? true : false; + } + + /** + * Check for allowed dimension + */ + function valid_dimensions(&$file) + { + if (!$this->max_width && !$this->max_height && !$this->min_width && !$this->min_height) + { + return true; + } + + if (($file->get('width') > $this->max_width && $this->max_width) || + ($file->get('height') > $this->max_height && $this->max_height) || + ($file->get('width') < $this->min_width && $this->min_width) || + ($file->get('height') < $this->min_height && $this->min_height)) + { + return false; + } + + return true; + } + + /** + * Check if form upload is valid + */ + function is_valid($form_name) + { + global $request; + $upload = $request->file($form_name); + + return (!empty($upload) && $upload['name'] !== 'none'); + } + + + /** + * Check for bad content (IE mime-sniffing) + */ + function valid_content(&$file) + { + return ($file->check_content($this->disallowed_content)); + } + + /** + * Get image type/extension mapping + * + * @return array Array containing the image types and their extensions + */ + static public function image_types() + { + $result = array( + IMAGETYPE_GIF => array('gif'), + IMAGETYPE_JPEG => array('jpg', 'jpeg'), + IMAGETYPE_PNG => array('png'), + IMAGETYPE_SWF => array('swf'), + IMAGETYPE_PSD => array('psd'), + IMAGETYPE_BMP => array('bmp'), + IMAGETYPE_TIFF_II => array('tif', 'tiff'), + IMAGETYPE_TIFF_MM => array('tif', 'tiff'), + IMAGETYPE_JPC => array('jpg', 'jpeg'), + IMAGETYPE_JP2 => array('jpg', 'jpeg'), + IMAGETYPE_JPX => array('jpg', 'jpeg'), + IMAGETYPE_JB2 => array('jpg', 'jpeg'), + IMAGETYPE_IFF => array('iff'), + IMAGETYPE_WBMP => array('wbmp'), + IMAGETYPE_XBM => array('xbm'), + ); + + if (defined('IMAGETYPE_SWC')) + { + $result[IMAGETYPE_SWC] = array('swc'); + } + + return $result; + } +} From 1af6f052d80e693d289258d490c1187a064093b9 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Fri, 14 Aug 2015 09:03:55 +0200 Subject: [PATCH 08/66] [ticket/13904] Load upload class using factory PHPBB3-13904 --- .../default/container/services_files.yml | 6 ++++ phpBB/includes/functions_posting.php | 5 +-- phpBB/phpbb/avatar/driver/remote.php | 9 ++---- phpBB/phpbb/avatar/driver/upload.php | 26 +++++++++++----- phpBB/phpbb/files/upload.php | 31 ++++++++++--------- phpBB/phpbb/plupload/plupload.php | 2 +- 6 files changed, 47 insertions(+), 32 deletions(-) diff --git a/phpBB/config/default/container/services_files.yml b/phpBB/config/default/container/services_files.yml index 6f0b10cce1..cd27d0745c 100644 --- a/phpBB/config/default/container/services_files.yml +++ b/phpBB/config/default/container/services_files.yml @@ -9,3 +9,9 @@ services: - @filesystem - @mimetype.guesser - @plupload + + files.upload: + class: phpbb\files\upload + scope: prototype + arguments: + - @filesystem diff --git a/phpBB/includes/functions_posting.php b/phpBB/includes/functions_posting.php index e91f8bb56d..75ac20a337 100644 --- a/phpBB/includes/functions_posting.php +++ b/phpBB/includes/functions_posting.php @@ -404,14 +404,14 @@ function posting_gen_topic_types($forum_id, $cur_topic_type = POST_NORMAL) function upload_attachment($form_name, $forum_id, $local = false, $local_storage = '', $is_message = false, $local_filedata = false, \phpbb\mimetype\guesser $mimetype_guesser = null, \phpbb\plupload\plupload $plupload = null) { global $auth, $user, $config, $db, $cache; - global $phpbb_root_path, $phpEx, $phpbb_dispatcher, $phpbb_filesystem; + global $phpbb_root_path, $phpEx, $phpbb_dispatcher, $phpbb_container; $filedata = array( 'error' => array() ); include_once($phpbb_root_path . 'includes/functions_upload.' . $phpEx); - $upload = new fileupload($phpbb_filesystem); + $upload = $phpbb_container->get('files.upload'); if ($config['check_attachment_content'] && isset($config['mime_triggers'])) { @@ -433,6 +433,7 @@ function upload_attachment($form_name, $forum_id, $local = false, $local_storage $extensions = $cache->obtain_attach_extensions((($is_message) ? false : (int) $forum_id)); $upload->set_allowed_extensions(array_keys($extensions['_allowed_'])); + /** @var \phpbb\files\filespec $file */ $file = ($local) ? $upload->local_upload($local_storage, $local_filedata, $mimetype_guesser) : $upload->form_upload($form_name, $mimetype_guesser, $plupload); if ($file->init_error()) diff --git a/phpBB/phpbb/avatar/driver/remote.php b/phpBB/phpbb/avatar/driver/remote.php index 90443c9b4e..0526b9184e 100644 --- a/phpBB/phpbb/avatar/driver/remote.php +++ b/phpBB/phpbb/avatar/driver/remote.php @@ -114,13 +114,8 @@ class remote extends \phpbb\avatar\driver\driver return false; } - if (!class_exists('fileupload')) - { - include($this->phpbb_root_path . 'includes/functions_upload.' . $this->php_ext); - } - - $types = \fileupload::image_types(); - $extension = strtolower(\filespec::get_extension($url)); + $types = \phpbb\files\upload::image_types(); + $extension = strtolower(\phpbb\files\filespec::get_extension($url)); // Check if this is actually an image if ($file_stream = @fopen($url, 'r')) diff --git a/phpBB/phpbb/avatar/driver/upload.php b/phpBB/phpbb/avatar/driver/upload.php index b31609b982..4cdb65ce84 100644 --- a/phpBB/phpbb/avatar/driver/upload.php +++ b/phpBB/phpbb/avatar/driver/upload.php @@ -33,6 +33,11 @@ class upload extends \phpbb\avatar\driver\driver */ protected $dispatcher; + /** + * @var \phpbb\files\factory + */ + protected $files_factory; + /** * Construct a driver object * @@ -43,9 +48,10 @@ class upload extends \phpbb\avatar\driver\driver * @param \phpbb\path_helper $path_helper phpBB path helper * @param \phpbb\mimetype\guesser $mimetype_guesser Mimetype guesser * @param \phpbb\event\dispatcher_interface $dispatcher phpBB Event dispatcher object + * @param \phpbb\files\factory $files_factory File classes factory * @param \phpbb\cache\driver\driver_interface $cache Cache driver */ - public function __construct(\phpbb\config\config $config, $phpbb_root_path, $php_ext, \phpbb\filesystem\filesystem_interface $filesystem, \phpbb\path_helper $path_helper, \phpbb\mimetype\guesser $mimetype_guesser, \phpbb\event\dispatcher_interface $dispatcher, \phpbb\cache\driver\driver_interface $cache = null) + public function __construct(\phpbb\config\config $config, $phpbb_root_path, $php_ext, \phpbb\filesystem\filesystem_interface $filesystem, \phpbb\path_helper $path_helper, \phpbb\mimetype\guesser $mimetype_guesser, \phpbb\event\dispatcher_interface $dispatcher, \phpbb\files\factory $files_factory, \phpbb\cache\driver\driver_interface $cache = null) { $this->config = $config; $this->phpbb_root_path = $phpbb_root_path; @@ -54,6 +60,7 @@ class upload extends \phpbb\avatar\driver\driver $this->path_helper = $path_helper; $this->mimetype_guesser = $mimetype_guesser; $this->dispatcher = $dispatcher; + $this->files_factory = $files_factory; $this->cache = $cache; } @@ -99,12 +106,17 @@ class upload extends \phpbb\avatar\driver\driver return false; } - if (!class_exists('fileupload')) - { - include($this->phpbb_root_path . 'includes/functions_upload.' . $this->php_ext); - } - - $upload = new \fileupload($this->filesystem, 'AVATAR_', $this->allowed_extensions, $this->config['avatar_filesize'], $this->config['avatar_min_width'], $this->config['avatar_min_height'], $this->config['avatar_max_width'], $this->config['avatar_max_height'], (isset($this->config['mime_triggers']) ? explode('|', $this->config['mime_triggers']) : false)); + /** @var \phpbb\files\upload $upload */ + $upload = $this->files_factory->get('upload') + ->set_error_prefix('AVATAR_') + ->set_allowed_extensions($this->allowed_extensions) + ->set_max_filesize($this->config['avatar_filesize']) + ->set_allowed_dimensions( + $this->config['avatar_min_width'], + $this->config['avatar_min_height'], + $this->config['avatar_max_width'], + $this->config['avatar_max_height']) + ->set_disallowed_content((isset($this->config['mime_triggers']) ? explode('|', $this->config['mime_triggers']) : false)); $url = $request->variable('avatar_upload_url', ''); $upload_file = $request->file('avatar_upload_file'); diff --git a/phpBB/phpbb/files/upload.php b/phpBB/phpbb/files/upload.php index 7501247e06..8666b857a5 100644 --- a/phpBB/phpbb/files/upload.php +++ b/phpBB/phpbb/files/upload.php @@ -40,24 +40,15 @@ class upload * Init file upload class. * * @param \phpbb\filesystem\filesystem_interface $filesystem - * @param string $error_prefix Used error messages will get prefixed by this string - * @param array $allowed_extensions Array of allowed extensions, for example array('jpg', 'jpeg', 'gif', 'png') - * @param int $max_filesize Maximum filesize - * @param int $min_width Minimum image width (only checked for images) - * @param int $min_height Minimum image height (only checked for images) - * @param int $max_width Maximum image width (only checked for images) - * @param int $max_height Maximum image height (only checked for images) - * @param bool|array $disallowed_content If enabled, the first 256 bytes of the file must not - * contain any of its values. Defaults to false. * */ - function fileupload(\phpbb\filesystem\filesystem_interface $filesystem, $error_prefix = '', $allowed_extensions = false, $max_filesize = false, $min_width = false, $min_height = false, $max_width = false, $max_height = false, $disallowed_content = false) + public function __construct(\phpbb\filesystem\filesystem_interface $filesystem) { - $this->set_allowed_extensions($allowed_extensions); - $this->set_max_filesize($max_filesize); - $this->set_allowed_dimensions($min_width, $min_height, $max_width, $max_height); - $this->set_error_prefix($error_prefix); - $this->set_disallowed_content($disallowed_content); +// $this->set_allowed_extensions($allowed_extensions); +// $this->set_max_filesize($max_filesize); +// $this->set_allowed_dimensions($min_width, $min_height, $max_width, $max_height); +// $this->set_error_prefix($error_prefix); +// $this->set_disallowed_content($disallowed_content); $this->filesystem = $filesystem; } @@ -82,6 +73,8 @@ class upload { $this->allowed_extensions = $allowed_extensions; } + + return $this; } /** @@ -93,6 +86,8 @@ class upload $this->min_height = (int) $min_height; $this->max_width = (int) $max_width; $this->max_height = (int) $max_height; + + return $this; } /** @@ -104,6 +99,8 @@ class upload { $this->max_filesize = (int) $max_filesize; } + + return $this; } /** @@ -115,6 +112,8 @@ class upload { $this->disallowed_content = array_diff($disallowed_content, array('')); } + + return $this; } /** @@ -123,6 +122,8 @@ class upload function set_error_prefix($error_prefix) { $this->error_prefix = $error_prefix; + + return $this; } /** diff --git a/phpBB/phpbb/plupload/plupload.php b/phpBB/phpbb/plupload/plupload.php index ca78167ec0..35f3a0071e 100644 --- a/phpBB/phpbb/plupload/plupload.php +++ b/phpBB/phpbb/plupload/plupload.php @@ -303,7 +303,7 @@ class plupload $this->temporary_directory, $this->config['plupload_salt'], md5($file_name), - \filespec::get_extension($file_name) + \phpbb\files\filespec::get_extension($file_name) ); } From 8c2cbc0599b9d62fde730b2891c73c9c053682e7 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Mon, 1 Jun 2015 13:51:05 +0200 Subject: [PATCH 09/66] [ticket/13904] Remove magic method from factory and allow short names PHPBB3-13904 --- phpBB/phpbb/files/factory.php | 27 +++------------------------ 1 file changed, 3 insertions(+), 24 deletions(-) diff --git a/phpBB/phpbb/files/factory.php b/phpBB/phpbb/files/factory.php index 9385ad678e..508c50c6ce 100644 --- a/phpBB/phpbb/files/factory.php +++ b/phpBB/phpbb/files/factory.php @@ -35,13 +35,15 @@ class factory * * @param string $name Service name * - * @return object|false Requested service or false if service could not be + * @return object|bool Requested service or false if service could not be * found by the container */ public function get($name) { $service = false; + $name = (strpos($name, 'files.') === false) ? 'files.' . $name : $name; + try { $service = $this->container->get($name); @@ -53,27 +55,4 @@ class factory return $service; } - - /** - * Magic function for handling get calls, e.g. get_fileupload() or - * get_filespec() and turning them into call for files. services like - * files.fileupload. - * - * @param string $name Name of called function - * @param mixed $arguments Possible supplied arguments - * - * @return object|false Requested service or false if service could not be - * found by the container - */ - public function __call($name, $arguments) - { - if (substr($name, 0, 4) === 'get_') - { - return $this->get('files.' . substr($name, 4)); - } - else - { - return false; - } - } } From 186b4495b625a40459618f01784b814a29f98ae0 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Mon, 1 Jun 2015 13:52:01 +0200 Subject: [PATCH 10/66] [ticket/13904] Allow using factory for none files. classes PHPBB3-13904 --- phpBB/phpbb/files/factory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/phpbb/files/factory.php b/phpBB/phpbb/files/factory.php index 508c50c6ce..b28a7ab659 100644 --- a/phpBB/phpbb/files/factory.php +++ b/phpBB/phpbb/files/factory.php @@ -42,7 +42,7 @@ class factory { $service = false; - $name = (strpos($name, 'files.') === false) ? 'files.' . $name : $name; + $name = (strpos($name, 'files.') === false && strpos($name, '.') === false) ? 'files.' . $name : $name; try { From a96e7a8ec6efa483b47dca3395ee2de608cfc675 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Mon, 1 Jun 2015 13:57:17 +0200 Subject: [PATCH 11/66] [ticket/13904] Get rid of useless parameters and variables PHPBB3-13904 --- phpBB/includes/functions_posting.php | 3 +-- phpBB/phpbb/avatar/driver/upload.php | 4 ++-- phpBB/phpbb/files/upload.php | 8 +++----- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/phpBB/includes/functions_posting.php b/phpBB/includes/functions_posting.php index 75ac20a337..f625555d49 100644 --- a/phpBB/includes/functions_posting.php +++ b/phpBB/includes/functions_posting.php @@ -410,7 +410,6 @@ function upload_attachment($form_name, $forum_id, $local = false, $local_storage 'error' => array() ); - include_once($phpbb_root_path . 'includes/functions_upload.' . $phpEx); $upload = $phpbb_container->get('files.upload'); if ($config['check_attachment_content'] && isset($config['mime_triggers'])) @@ -434,7 +433,7 @@ function upload_attachment($form_name, $forum_id, $local = false, $local_storage $upload->set_allowed_extensions(array_keys($extensions['_allowed_'])); /** @var \phpbb\files\filespec $file */ - $file = ($local) ? $upload->local_upload($local_storage, $local_filedata, $mimetype_guesser) : $upload->form_upload($form_name, $mimetype_guesser, $plupload); + $file = ($local) ? $upload->local_upload($local_storage, $local_filedata) : $upload->form_upload($form_name, $plupload); if ($file->init_error()) { diff --git a/phpBB/phpbb/avatar/driver/upload.php b/phpBB/phpbb/avatar/driver/upload.php index 4cdb65ce84..d330eadd18 100644 --- a/phpBB/phpbb/avatar/driver/upload.php +++ b/phpBB/phpbb/avatar/driver/upload.php @@ -123,7 +123,7 @@ class upload extends \phpbb\avatar\driver\driver if (!empty($upload_file['name'])) { - $file = $upload->form_upload('avatar_upload_file', $this->mimetype_guesser); + $file = $upload->form_upload('avatar_upload_file'); } else if (!empty($this->config['allow_avatar_remote_upload']) && !empty($url)) { @@ -153,7 +153,7 @@ class upload extends \phpbb\avatar\driver\driver return false; } - $file = $upload->remote_upload($url, $this->mimetype_guesser); + $file = $upload->remote_upload($url); } else { diff --git a/phpBB/phpbb/files/upload.php b/phpBB/phpbb/files/upload.php index 8666b857a5..1892d22adf 100644 --- a/phpBB/phpbb/files/upload.php +++ b/phpBB/phpbb/files/upload.php @@ -131,13 +131,12 @@ class upload * Upload file from users harddisk * * @param string $form_name Form name assigned to the file input field (if it is an array, the key has to be specified) - * @param \phpbb\mimetype\guesser $mimetype_guesser Mimetype guesser * @param \phpbb\plupload\plupload $plupload The plupload object * * @return object $file Object "filespec" is returned, all further operations can be done with this object * @access public */ - function form_upload($form_name, \phpbb\mimetype\guesser $mimetype_guesser = null, \phpbb\plupload\plupload $plupload = null) + function form_upload($form_name, \phpbb\plupload\plupload $plupload = null) { global $user, $request, $phpbb_container; @@ -216,7 +215,7 @@ class upload /** * Move file from another location to phpBB */ - function local_upload($source_file, $filedata = false, \phpbb\mimetype\guesser $mimetype_guesser = null) + function local_upload($source_file, $filedata = false) { global $user, $request, $phpbb_container; @@ -295,11 +294,10 @@ class upload * Uploads file from given url * * @param string $upload_url URL pointing to file to upload, for example http://www.foobar.com/example.gif - * @param \phpbb\mimetype\guesser $mimetype_guesser Mimetype guesser * @return object $file Object "filespec" is returned, all further operations can be done with this object * @access public */ - function remote_upload($upload_url, \phpbb\mimetype\guesser $mimetype_guesser = null) + function remote_upload($upload_url) { global $user, $phpbb_root_path, $phpbb_container; From 0a98fc256157c2d4861693e693731f7b2d7a9ac7 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Mon, 1 Jun 2015 13:57:33 +0200 Subject: [PATCH 12/66] [ticket/13904] Remove fileupload class PHPBB3-13904 --- phpBB/includes/functions_upload.php | 641 ---------------------------- 1 file changed, 641 deletions(-) diff --git a/phpBB/includes/functions_upload.php b/phpBB/includes/functions_upload.php index 3170a47033..398771e92a 100644 --- a/phpBB/includes/functions_upload.php +++ b/phpBB/includes/functions_upload.php @@ -29,644 +29,3 @@ class fileerror extends \phpbb\files\filespec $this->error[] = $error_msg; } } - -/** -* File upload class -* Init class (all parameters optional and able to be set/overwritten separately) - scope is global and valid for all uploads -*/ -class fileupload -{ - var $allowed_extensions = array(); - var $disallowed_content = array('body', 'head', 'html', 'img', 'plaintext', 'a href', 'pre', 'script', 'table', 'title'); - var $max_filesize = 0; - var $min_width = 0; - var $min_height = 0; - var $max_width = 0; - var $max_height = 0; - var $error_prefix = ''; - - /** @var int Timeout for remote upload */ - var $upload_timeout = 6; - - /** - * @var \phpbb\filesystem\filesystem_interface - */ - protected $filesystem; - - /** - * Init file upload class. - * - * @param \phpbb\filesystem\filesystem_interface $filesystem - * @param string $error_prefix Used error messages will get prefixed by this string - * @param array $allowed_extensions Array of allowed extensions, for example array('jpg', 'jpeg', 'gif', 'png') - * @param int $max_filesize Maximum filesize - * @param int $min_width Minimum image width (only checked for images) - * @param int $min_height Minimum image height (only checked for images) - * @param int $max_width Maximum image width (only checked for images) - * @param int $max_height Maximum image height (only checked for images) - * @param bool|array $disallowed_content If enabled, the first 256 bytes of the file must not - * contain any of its values. Defaults to false. - * - */ - function fileupload(\phpbb\filesystem\filesystem_interface $filesystem, $error_prefix = '', $allowed_extensions = false, $max_filesize = false, $min_width = false, $min_height = false, $max_width = false, $max_height = false, $disallowed_content = false) - { - $this->set_allowed_extensions($allowed_extensions); - $this->set_max_filesize($max_filesize); - $this->set_allowed_dimensions($min_width, $min_height, $max_width, $max_height); - $this->set_error_prefix($error_prefix); - $this->set_disallowed_content($disallowed_content); - $this->filesystem = $filesystem; - } - - /** - * Reset vars - */ - function reset_vars() - { - $this->max_filesize = 0; - $this->min_width = $this->min_height = $this->max_width = $this->max_height = 0; - $this->error_prefix = ''; - $this->allowed_extensions = array(); - $this->disallowed_content = array(); - } - - /** - * Set allowed extensions - */ - function set_allowed_extensions($allowed_extensions) - { - if ($allowed_extensions !== false && is_array($allowed_extensions)) - { - $this->allowed_extensions = $allowed_extensions; - } - } - - /** - * Set allowed dimensions - */ - function set_allowed_dimensions($min_width, $min_height, $max_width, $max_height) - { - $this->min_width = (int) $min_width; - $this->min_height = (int) $min_height; - $this->max_width = (int) $max_width; - $this->max_height = (int) $max_height; - } - - /** - * Set maximum allowed filesize - */ - function set_max_filesize($max_filesize) - { - if ($max_filesize !== false && (int) $max_filesize) - { - $this->max_filesize = (int) $max_filesize; - } - } - - /** - * Set disallowed strings - */ - function set_disallowed_content($disallowed_content) - { - if ($disallowed_content !== false && is_array($disallowed_content)) - { - $this->disallowed_content = array_diff($disallowed_content, array('')); - } - } - - /** - * Set error prefix - */ - function set_error_prefix($error_prefix) - { - $this->error_prefix = $error_prefix; - } - - /** - * Form upload method - * Upload file from users harddisk - * - * @param string $form_name Form name assigned to the file input field (if it is an array, the key has to be specified) - * @param \phpbb\mimetype\guesser $mimetype_guesser Mimetype guesser - * @param \phpbb\plupload\plupload $plupload The plupload object - * - * @return object $file Object "filespec" is returned, all further operations can be done with this object - * @access public - */ - function form_upload($form_name, \phpbb\mimetype\guesser $mimetype_guesser = null, \phpbb\plupload\plupload $plupload = null) - { - global $user, $request, $phpbb_container; - - $upload = $request->file($form_name); - unset($upload['local_mode']); - - if ($plupload) - { - $result = $plupload->handle_upload($form_name); - if (is_array($result)) - { - $upload = array_merge($upload, $result); - } - } - - /** @var \phpbb\files\filespec $file */ - $file = $phpbb_container->get('files.filespec') - ->set_upload_ary($upload) - ->set_upload_namespace($this); - - if ($file->init_error()) - { - $file->error[] = ''; - return $file; - } - - // Error array filled? - if (isset($upload['error'])) - { - $error = $this->assign_internal_error($upload['error']); - - if ($error !== false) - { - $file->error[] = $error; - return $file; - } - } - - // Check if empty file got uploaded (not catched by is_uploaded_file) - if (isset($upload['size']) && $upload['size'] == 0) - { - $file->error[] = $user->lang[$this->error_prefix . 'EMPTY_FILEUPLOAD']; - return $file; - } - - // PHP Upload filesize exceeded - if ($file->get('filename') == 'none') - { - $max_filesize = @ini_get('upload_max_filesize'); - $unit = 'MB'; - - if (!empty($max_filesize)) - { - $unit = strtolower(substr($max_filesize, -1, 1)); - $max_filesize = (int) $max_filesize; - - $unit = ($unit == 'k') ? 'KB' : (($unit == 'g') ? 'GB' : 'MB'); - } - - $file->error[] = (empty($max_filesize)) ? $user->lang[$this->error_prefix . 'PHP_SIZE_NA'] : sprintf($user->lang[$this->error_prefix . 'PHP_SIZE_OVERRUN'], $max_filesize, $user->lang[$unit]); - return $file; - } - - // Not correctly uploaded - if (!$file->is_uploaded()) - { - $file->error[] = $user->lang[$this->error_prefix . 'NOT_UPLOADED']; - return $file; - } - - $this->common_checks($file); - - return $file; - } - - /** - * Move file from another location to phpBB - */ - function local_upload($source_file, $filedata = false, \phpbb\mimetype\guesser $mimetype_guesser = null) - { - global $user, $request, $phpbb_container; - - $upload = array(); - - $upload['local_mode'] = true; - $upload['tmp_name'] = $source_file; - - if ($filedata === false) - { - $upload['name'] = utf8_basename($source_file); - $upload['size'] = 0; - } - else - { - $upload['name'] = $filedata['realname']; - $upload['size'] = $filedata['size']; - $upload['type'] = $filedata['type']; - } - - /** @var \phpbb\files\filespec $file */ - $file = $phpbb_container->get('files.filespec') - ->set_upload_ary($upload) - ->set_upload_namespace($this); - - if ($file->init_error()) - { - $file->error[] = ''; - return $file; - } - - if (isset($upload['error'])) - { - $error = $this->assign_internal_error($upload['error']); - - if ($error !== false) - { - $file->error[] = $error; - return $file; - } - } - - // PHP Upload filesize exceeded - if ($file->get('filename') == 'none') - { - $max_filesize = @ini_get('upload_max_filesize'); - $unit = 'MB'; - - if (!empty($max_filesize)) - { - $unit = strtolower(substr($max_filesize, -1, 1)); - $max_filesize = (int) $max_filesize; - - $unit = ($unit == 'k') ? 'KB' : (($unit == 'g') ? 'GB' : 'MB'); - } - - $file->error[] = (empty($max_filesize)) ? $user->lang[$this->error_prefix . 'PHP_SIZE_NA'] : sprintf($user->lang[$this->error_prefix . 'PHP_SIZE_OVERRUN'], $max_filesize, $user->lang[$unit]); - return $file; - } - - // Not correctly uploaded - if (!$file->is_uploaded()) - { - $file->error[] = $user->lang[$this->error_prefix . 'NOT_UPLOADED']; - return $file; - } - - $this->common_checks($file); - $request->overwrite('local', $upload, \phpbb\request\request_interface::FILES); - - return $file; - } - - /** - * Remote upload method - * Uploads file from given url - * - * @param string $upload_url URL pointing to file to upload, for example http://www.foobar.com/example.gif - * @param \phpbb\mimetype\guesser $mimetype_guesser Mimetype guesser - * @return object $file Object "filespec" is returned, all further operations can be done with this object - * @access public - */ - function remote_upload($upload_url, \phpbb\mimetype\guesser $mimetype_guesser = null) - { - global $user, $phpbb_root_path, $phpbb_container; - - $upload_ary = array(); - $upload_ary['local_mode'] = true; - - if (!preg_match('#^(https?://).*?\.(' . implode('|', $this->allowed_extensions) . ')$#i', $upload_url, $match)) - { - $file = new fileerror($user->lang[$this->error_prefix . 'URL_INVALID']); - return $file; - } - - if (empty($match[2])) - { - $file = new fileerror($user->lang[$this->error_prefix . 'URL_INVALID']); - return $file; - } - - $url = parse_url($upload_url); - - $host = $url['host']; - $path = $url['path']; - $port = (!empty($url['port'])) ? (int) $url['port'] : 80; - - $upload_ary['type'] = 'application/octet-stream'; - - $url['path'] = explode('.', $url['path']); - $ext = array_pop($url['path']); - - $url['path'] = implode('', $url['path']); - $upload_ary['name'] = utf8_basename($url['path']) . (($ext) ? '.' . $ext : ''); - $filename = $url['path']; - $filesize = 0; - - $remote_max_filesize = $this->max_filesize; - if (!$remote_max_filesize) - { - $max_filesize = @ini_get('upload_max_filesize'); - - if (!empty($max_filesize)) - { - $unit = strtolower(substr($max_filesize, -1, 1)); - $remote_max_filesize = (int) $max_filesize; - - switch ($unit) - { - case 'g': - $remote_max_filesize *= 1024; - // no break - case 'm': - $remote_max_filesize *= 1024; - // no break - case 'k': - $remote_max_filesize *= 1024; - // no break - } - } - } - - $errno = 0; - $errstr = ''; - - if (!($fsock = @fsockopen($host, $port, $errno, $errstr))) - { - $file = new fileerror($user->lang[$this->error_prefix . 'NOT_UPLOADED']); - return $file; - } - - // Make sure $path not beginning with / - if (strpos($path, '/') === 0) - { - $path = substr($path, 1); - } - - fputs($fsock, 'GET /' . $path . " HTTP/1.1\r\n"); - fputs($fsock, "HOST: " . $host . "\r\n"); - fputs($fsock, "Connection: close\r\n\r\n"); - - // Set a proper timeout for the socket - socket_set_timeout($fsock, $this->upload_timeout); - - $get_info = false; - $data = ''; - $length = false; - $timer_stop = time() + $this->upload_timeout; - - while ((!$length || $filesize < $length) && !@feof($fsock)) - { - if ($get_info) - { - if ($length) - { - // Don't attempt to read past end of file if server indicated length - $block = @fread($fsock, min($length - $filesize, 1024)); - } - else - { - $block = @fread($fsock, 1024); - } - - $filesize += strlen($block); - - if ($remote_max_filesize && $filesize > $remote_max_filesize) - { - $max_filesize = get_formatted_filesize($remote_max_filesize, false); - - $file = new fileerror(sprintf($user->lang[$this->error_prefix . 'WRONG_FILESIZE'], $max_filesize['value'], $max_filesize['unit'])); - return $file; - } - - $data .= $block; - } - else - { - $line = @fgets($fsock, 1024); - - if ($line == "\r\n") - { - $get_info = true; - } - else - { - if (stripos($line, 'content-type: ') !== false) - { - $upload_ary['type'] = rtrim(str_replace('content-type: ', '', strtolower($line))); - } - else if ($this->max_filesize && stripos($line, 'content-length: ') !== false) - { - $length = (int) str_replace('content-length: ', '', strtolower($line)); - - if ($remote_max_filesize && $length && $length > $remote_max_filesize) - { - $max_filesize = get_formatted_filesize($remote_max_filesize, false); - - $file = new fileerror(sprintf($user->lang[$this->error_prefix . 'WRONG_FILESIZE'], $max_filesize['value'], $max_filesize['unit'])); - return $file; - } - } - else if (stripos($line, '404 not found') !== false) - { - $file = new fileerror($user->lang[$this->error_prefix . 'URL_NOT_FOUND']); - return $file; - } - } - } - - $stream_meta_data = stream_get_meta_data($fsock); - - // Cancel upload if we exceed timeout - if (!empty($stream_meta_data['timed_out']) || time() >= $timer_stop) - { - $file = new fileerror($user->lang[$this->error_prefix . 'REMOTE_UPLOAD_TIMEOUT']); - return $file; - } - } - @fclose($fsock); - - if (empty($data)) - { - $file = new fileerror($user->lang[$this->error_prefix . 'EMPTY_REMOTE_DATA']); - return $file; - } - - $tmp_path = (!@ini_get('safe_mode') || strtolower(@ini_get('safe_mode')) == 'off') ? false : $phpbb_root_path . 'cache'; - $filename = tempnam($tmp_path, unique_id() . '-'); - - if (!($fp = @fopen($filename, 'wb'))) - { - $file = new fileerror($user->lang[$this->error_prefix . 'NOT_UPLOADED']); - return $file; - } - - $upload_ary['size'] = fwrite($fp, $data); - fclose($fp); - unset($data); - - $upload_ary['tmp_name'] = $filename; - - /** @var \phpbb\files\filespec $file */ - $file = $phpbb_container->get('files.filespec') - ->set_upload_ary($upload_ary) - ->set_upload_namespace($this); - $this->common_checks($file); - - return $file; - } - - /** - * Assign internal error - * @access private - */ - function assign_internal_error($errorcode) - { - global $user; - - switch ($errorcode) - { - case 1: - $max_filesize = @ini_get('upload_max_filesize'); - $unit = 'MB'; - - if (!empty($max_filesize)) - { - $unit = strtolower(substr($max_filesize, -1, 1)); - $max_filesize = (int) $max_filesize; - - $unit = ($unit == 'k') ? 'KB' : (($unit == 'g') ? 'GB' : 'MB'); - } - - $error = (empty($max_filesize)) ? $user->lang[$this->error_prefix . 'PHP_SIZE_NA'] : sprintf($user->lang[$this->error_prefix . 'PHP_SIZE_OVERRUN'], $max_filesize, $user->lang[$unit]); - break; - - case 2: - $max_filesize = get_formatted_filesize($this->max_filesize, false); - - $error = sprintf($user->lang[$this->error_prefix . 'WRONG_FILESIZE'], $max_filesize['value'], $max_filesize['unit']); - break; - - case 3: - $error = $user->lang[$this->error_prefix . 'PARTIAL_UPLOAD']; - break; - - case 4: - $error = $user->lang[$this->error_prefix . 'NOT_UPLOADED']; - break; - - case 6: - $error = 'Temporary folder could not be found. Please check your PHP installation.'; - break; - - default: - $error = false; - break; - } - - return $error; - } - - /** - * Perform common checks - */ - function common_checks(&$file) - { - global $user; - - // Filesize is too big or it's 0 if it was larger than the maxsize in the upload form - if ($this->max_filesize && ($file->get('filesize') > $this->max_filesize || $file->get('filesize') == 0)) - { - $max_filesize = get_formatted_filesize($this->max_filesize, false); - - $file->error[] = sprintf($user->lang[$this->error_prefix . 'WRONG_FILESIZE'], $max_filesize['value'], $max_filesize['unit']); - } - - // check Filename - if (preg_match("#[\\/:*?\"<>|]#i", $file->get('realname'))) - { - $file->error[] = sprintf($user->lang[$this->error_prefix . 'INVALID_FILENAME'], $file->get('realname')); - } - - // Invalid Extension - if (!$this->valid_extension($file)) - { - $file->error[] = sprintf($user->lang[$this->error_prefix . 'DISALLOWED_EXTENSION'], $file->get('extension')); - } - - // MIME Sniffing - if (!$this->valid_content($file)) - { - $file->error[] = sprintf($user->lang[$this->error_prefix . 'DISALLOWED_CONTENT']); - } - } - - /** - * Check for allowed extension - */ - function valid_extension(&$file) - { - return (in_array($file->get('extension'), $this->allowed_extensions)) ? true : false; - } - - /** - * Check for allowed dimension - */ - function valid_dimensions(&$file) - { - if (!$this->max_width && !$this->max_height && !$this->min_width && !$this->min_height) - { - return true; - } - - if (($file->get('width') > $this->max_width && $this->max_width) || - ($file->get('height') > $this->max_height && $this->max_height) || - ($file->get('width') < $this->min_width && $this->min_width) || - ($file->get('height') < $this->min_height && $this->min_height)) - { - return false; - } - - return true; - } - - /** - * Check if form upload is valid - */ - function is_valid($form_name) - { - global $request; - $upload = $request->file($form_name); - - return (!empty($upload) && $upload['name'] !== 'none'); - } - - - /** - * Check for bad content (IE mime-sniffing) - */ - function valid_content(&$file) - { - return ($file->check_content($this->disallowed_content)); - } - - /** - * Get image type/extension mapping - * - * @return array Array containing the image types and their extensions - */ - static public function image_types() - { - $result = array( - IMAGETYPE_GIF => array('gif'), - IMAGETYPE_JPEG => array('jpg', 'jpeg'), - IMAGETYPE_PNG => array('png'), - IMAGETYPE_SWF => array('swf'), - IMAGETYPE_PSD => array('psd'), - IMAGETYPE_BMP => array('bmp'), - IMAGETYPE_TIFF_II => array('tif', 'tiff'), - IMAGETYPE_TIFF_MM => array('tif', 'tiff'), - IMAGETYPE_JPC => array('jpg', 'jpeg'), - IMAGETYPE_JP2 => array('jpg', 'jpeg'), - IMAGETYPE_JPX => array('jpg', 'jpeg'), - IMAGETYPE_JB2 => array('jpg', 'jpeg'), - IMAGETYPE_IFF => array('iff'), - IMAGETYPE_WBMP => array('wbmp'), - IMAGETYPE_XBM => array('xbm'), - ); - - if (defined('IMAGETYPE_SWC')) - { - $result[IMAGETYPE_SWC] = array('swc'); - } - - return $result; - } -} From 57de89628622a80fcb386c0d95724fe4c6148929 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Fri, 14 Aug 2015 09:10:05 +0200 Subject: [PATCH 13/66] [ticket/13904] Remove unneeded parameters from avatars and fix tests PHPBB3-13904 --- .../default/container/services_avatar.yml | 3 +-- phpBB/phpbb/avatar/driver/upload.php | 18 ++---------------- tests/avatar/manager_test.php | 4 +++- 3 files changed, 6 insertions(+), 19 deletions(-) diff --git a/phpBB/config/default/container/services_avatar.yml b/phpBB/config/default/container/services_avatar.yml index 1b0c109298..67a8097e6f 100644 --- a/phpBB/config/default/container/services_avatar.yml +++ b/phpBB/config/default/container/services_avatar.yml @@ -61,10 +61,9 @@ services: - @config - %core.root_path% - %core.php_ext% - - @filesystem - @path_helper - - @mimetype.guesser - @dispatcher + - @files.factory - @cache.driver calls: - [set_name, [avatar.driver.upload]] diff --git a/phpBB/phpbb/avatar/driver/upload.php b/phpBB/phpbb/avatar/driver/upload.php index d330eadd18..e848f9d2e7 100644 --- a/phpBB/phpbb/avatar/driver/upload.php +++ b/phpBB/phpbb/avatar/driver/upload.php @@ -18,22 +18,12 @@ namespace phpbb\avatar\driver; */ class upload extends \phpbb\avatar\driver\driver { - /** - * @var \phpbb\filesystem\filesystem_interface - */ - protected $filesystem; - - /** - * @var \phpbb\mimetype\guesser - */ - protected $mimetype_guesser; - /** * @var \phpbb\event\dispatcher_interface */ protected $dispatcher; - /** + /**sts * @var \phpbb\files\factory */ protected $files_factory; @@ -44,21 +34,17 @@ class upload extends \phpbb\avatar\driver\driver * @param \phpbb\config\config $config phpBB configuration * @param string $phpbb_root_path Path to the phpBB root * @param string $php_ext PHP file extension - * @param \phpbb\filesystem\filesystem_interface phpBB filesystem helper * @param \phpbb\path_helper $path_helper phpBB path helper - * @param \phpbb\mimetype\guesser $mimetype_guesser Mimetype guesser * @param \phpbb\event\dispatcher_interface $dispatcher phpBB Event dispatcher object * @param \phpbb\files\factory $files_factory File classes factory * @param \phpbb\cache\driver\driver_interface $cache Cache driver */ - public function __construct(\phpbb\config\config $config, $phpbb_root_path, $php_ext, \phpbb\filesystem\filesystem_interface $filesystem, \phpbb\path_helper $path_helper, \phpbb\mimetype\guesser $mimetype_guesser, \phpbb\event\dispatcher_interface $dispatcher, \phpbb\files\factory $files_factory, \phpbb\cache\driver\driver_interface $cache = null) + public function __construct(\phpbb\config\config $config, $phpbb_root_path, $php_ext, \phpbb\path_helper $path_helper, \phpbb\event\dispatcher_interface $dispatcher, \phpbb\files\factory $files_factory, \phpbb\cache\driver\driver_interface $cache = null) { $this->config = $config; $this->phpbb_root_path = $phpbb_root_path; $this->php_ext = $php_ext; - $this->filesystem = $filesystem; $this->path_helper = $path_helper; - $this->mimetype_guesser = $mimetype_guesser; $this->dispatcher = $dispatcher; $this->files_factory = $files_factory; $this->cache = $cache; diff --git a/tests/avatar/manager_test.php b/tests/avatar/manager_test.php index 9804869c81..faf6976028 100644 --- a/tests/avatar/manager_test.php +++ b/tests/avatar/manager_test.php @@ -73,6 +73,8 @@ class phpbb_avatar_manager_test extends \phpbb_database_test_case ->will($this->returnValue('avatar.driver.barfoo')); $avatar_drivers = array($this->avatar_foobar, $this->avatar_barfoo); + $files_factory = new \phpbb\files\factory($phpbb_container); + foreach ($this->avatar_drivers() as $driver) { if ($driver !== 'upload') @@ -81,7 +83,7 @@ class phpbb_avatar_manager_test extends \phpbb_database_test_case } else { - $cur_avatar = $this->getMock('\phpbb\avatar\driver\\' . $driver, array('get_name'), array($this->config, $phpbb_root_path, $phpEx, $filesystem, $path_helper, $guesser, $dispatcher, $cache)); + $cur_avatar = $this->getMock('\phpbb\avatar\driver\\' . $driver, array('get_name'), array($this->config, $phpbb_root_path, $phpEx, $path_helper, $dispatcher, $files_factory, $cache)); } $cur_avatar->expects($this->any()) ->method('get_name') From 2915647a546b4c0733a0e1a0cdc924272e41615b Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Tue, 2 Jun 2015 00:29:03 +0200 Subject: [PATCH 14/66] [ticket/13904] Fix filespec tests PHPBB3-13904 --- phpBB/phpbb/files/filespec.php | 2 +- tests/upload/filespec_test.php | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/phpBB/phpbb/files/filespec.php b/phpBB/phpbb/files/filespec.php index 3f50488e7c..2fdba2d793 100644 --- a/phpBB/phpbb/files/filespec.php +++ b/phpBB/phpbb/files/filespec.php @@ -426,7 +426,7 @@ class filespec $this->height = $this->image_info['height']; // Check image type - $types = \fileupload::image_types(); + $types = upload::image_types(); if (!isset($types[$this->image_info['type']]) || !in_array($this->extension, $types[$this->image_info['type']])) { diff --git a/tests/upload/filespec_test.php b/tests/upload/filespec_test.php index f953970f64..f3260bf1a9 100644 --- a/tests/upload/filespec_test.php +++ b/tests/upload/filespec_test.php @@ -91,7 +91,8 @@ class phpbb_filespec_test extends phpbb_test_case 'error' => '', ); - return new filespec(array_merge($upload_ary, $override), null, $this->filesystem, $this->mimetype_guesser); + $filespec = new \phpbb\files\filespec($this->filesystem, $this->mimetype_guesser); + return $filespec->set_upload_ary(array_merge($upload_ary, $override)); } protected function tearDown() @@ -226,7 +227,7 @@ class phpbb_filespec_test extends phpbb_test_case */ public function test_get_extension($filename, $expected) { - $this->assertEquals($expected, filespec::get_extension($filename)); + $this->assertEquals($expected, \phpbb\files\filespec::get_extension($filename)); } public function is_image_variables() From dbfdb61f829166989149ac26bda36bbb4642e1af Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Tue, 2 Jun 2015 00:45:56 +0200 Subject: [PATCH 15/66] [ticket/13904] Fix fileupload tests PHPBB3-13904 --- tests/upload/fileupload_test.php | 42 +++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/tests/upload/fileupload_test.php b/tests/upload/fileupload_test.php index 9de384b64f..fe6096aebf 100644 --- a/tests/upload/fileupload_test.php +++ b/tests/upload/fileupload_test.php @@ -27,7 +27,7 @@ class phpbb_fileupload_test extends phpbb_test_case // Global $config required by unique_id // Global $user required by several functions dealing with translations // Global $request required by form_upload, local_upload and is_valid - global $config, $user, $request, $phpbb_filesystem; + global $config, $user, $request, $phpbb_filesystem, $phpbb_container; if (!is_array($config)) { @@ -44,6 +44,13 @@ class phpbb_fileupload_test extends phpbb_test_case $this->filesystem = $phpbb_filesystem = new \phpbb\filesystem\filesystem(); + $phpbb_container = new phpbb_mock_container_builder($this->phpbb_root_path, $this->phpEx); + $phpbb_container->set('files.filespec', new \phpbb\files\filespec( + $this->filesystem, + new \phpbb\mimetype\guesser(array( + 'mimetype.extension_guesser' => new \phpbb\mimetype\extension_guesser(), + )))); + $this->path = __DIR__ . '/fixture/'; } @@ -69,7 +76,9 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_common_checks_invalid_extension() { - $upload = new fileupload($this->filesystem, '', array('png'), 100); + $upload = new \phpbb\files\upload($this->filesystem); + $upload->set_allowed_extensions(array('png')) + ->set_max_filesize(100); $file = $this->gen_valid_filespec(); $upload->common_checks($file); $this->assertEquals('DISALLOWED_EXTENSION', $file->error[0]); @@ -77,7 +86,9 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_common_checks_invalid_filename() { - $upload = new fileupload($this->filesystem, '', array('jpg'), 100); + $upload = new \phpbb\files\upload($this->filesystem); + $upload->set_allowed_extensions(array('jpg')) + ->set_max_filesize(100); $file = $this->gen_valid_filespec(); $file->realname = 'invalid?'; $upload->common_checks($file); @@ -86,7 +97,9 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_common_checks_too_large() { - $upload = new fileupload($this->filesystem, '', array('jpg'), 100); + $upload = new \phpbb\files\upload($this->filesystem); + $upload->set_allowed_extensions(array('jpg')) + ->set_max_filesize(100); $file = $this->gen_valid_filespec(); $file->filesize = 1000; $upload->common_checks($file); @@ -95,7 +108,9 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_common_checks_valid_file() { - $upload = new fileupload($this->filesystem, '', array('jpg'), 1000); + $upload = new \phpbb\files\upload($this->filesystem); + $upload->set_allowed_extensions(array('jpg')) + ->set_max_filesize(1000); $file = $this->gen_valid_filespec(); $upload->common_checks($file); $this->assertEquals(0, sizeof($file->error)); @@ -103,7 +118,9 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_local_upload() { - $upload = new fileupload($this->filesystem, '', array('jpg'), 1000); + $upload = new \phpbb\files\upload($this->filesystem); + $upload->set_allowed_extensions(array('jpg')) + ->set_max_filesize(1000); copy($this->path . 'jpg', $this->path . 'jpg.jpg'); $file = $upload->local_upload($this->path . 'jpg.jpg'); @@ -113,7 +130,9 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_move_existent_file() { - $upload = new fileupload($this->filesystem, '', array('jpg'), 1000); + $upload = new \phpbb\files\upload($this->filesystem); + $upload->set_allowed_extensions(array('jpg')) + ->set_max_filesize(1000); copy($this->path . 'jpg', $this->path . 'jpg.jpg'); $file = $upload->local_upload($this->path . 'jpg.jpg'); @@ -125,7 +144,9 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_move_existent_file_overwrite() { - $upload = new fileupload($this->filesystem, '', array('jpg'), 1000); + $upload = new \phpbb\files\upload($this->filesystem); + $upload->set_allowed_extensions(array('jpg')) + ->set_max_filesize(1000); copy($this->path . 'jpg', $this->path . 'jpg.jpg'); copy($this->path . 'jpg', $this->path . 'copies/jpg.jpg'); @@ -138,7 +159,10 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_valid_dimensions() { - $upload = new fileupload($this->filesystem, '', false, false, 1, 1, 100, 100); + $upload = new \phpbb\files\upload($this->filesystem); + $upload->set_allowed_extensions(false) + ->set_max_filesize(false) + ->set_allowed_dimensions(1, 1, 100, 100); $file1 = $this->gen_valid_filespec(); $file2 = $this->gen_valid_filespec(); From f32a94ae5d5df156cc33e34a98d9a2e92385c393 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Fri, 14 Aug 2015 09:12:43 +0200 Subject: [PATCH 16/66] [ticket/13904] Pass filesystem to upload avatar again PHPBB3-13904 --- phpBB/config/default/container/services_avatar.yml | 1 + phpBB/phpbb/avatar/driver/upload.php | 11 +++++++++-- tests/avatar/manager_test.php | 2 +- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/phpBB/config/default/container/services_avatar.yml b/phpBB/config/default/container/services_avatar.yml index 67a8097e6f..76057cf5a7 100644 --- a/phpBB/config/default/container/services_avatar.yml +++ b/phpBB/config/default/container/services_avatar.yml @@ -61,6 +61,7 @@ services: - @config - %core.root_path% - %core.php_ext% + - @filesystem - @path_helper - @dispatcher - @files.factory diff --git a/phpBB/phpbb/avatar/driver/upload.php b/phpBB/phpbb/avatar/driver/upload.php index e848f9d2e7..c1ce18cb12 100644 --- a/phpBB/phpbb/avatar/driver/upload.php +++ b/phpBB/phpbb/avatar/driver/upload.php @@ -18,12 +18,17 @@ namespace phpbb\avatar\driver; */ class upload extends \phpbb\avatar\driver\driver { + /** + * @var \phpbb\filesystem\filesystem_interface + */ + protected $filesystem; + /** * @var \phpbb\event\dispatcher_interface */ protected $dispatcher; - /**sts + /** * @var \phpbb\files\factory */ protected $files_factory; @@ -34,16 +39,18 @@ class upload extends \phpbb\avatar\driver\driver * @param \phpbb\config\config $config phpBB configuration * @param string $phpbb_root_path Path to the phpBB root * @param string $php_ext PHP file extension + * @param \phpbb\filesystem\filesystem_interface $filesystem phpBB filesystem helper * @param \phpbb\path_helper $path_helper phpBB path helper * @param \phpbb\event\dispatcher_interface $dispatcher phpBB Event dispatcher object * @param \phpbb\files\factory $files_factory File classes factory * @param \phpbb\cache\driver\driver_interface $cache Cache driver */ - public function __construct(\phpbb\config\config $config, $phpbb_root_path, $php_ext, \phpbb\path_helper $path_helper, \phpbb\event\dispatcher_interface $dispatcher, \phpbb\files\factory $files_factory, \phpbb\cache\driver\driver_interface $cache = null) + public function __construct(\phpbb\config\config $config, $phpbb_root_path, $php_ext, \phpbb\filesystem\filesystem_interface $filesystem, \phpbb\path_helper $path_helper, \phpbb\event\dispatcher_interface $dispatcher, \phpbb\files\factory $files_factory, \phpbb\cache\driver\driver_interface $cache = null) { $this->config = $config; $this->phpbb_root_path = $phpbb_root_path; $this->php_ext = $php_ext; + $this->filesystem = $filesystem; $this->path_helper = $path_helper; $this->dispatcher = $dispatcher; $this->files_factory = $files_factory; diff --git a/tests/avatar/manager_test.php b/tests/avatar/manager_test.php index faf6976028..9003f72de2 100644 --- a/tests/avatar/manager_test.php +++ b/tests/avatar/manager_test.php @@ -83,7 +83,7 @@ class phpbb_avatar_manager_test extends \phpbb_database_test_case } else { - $cur_avatar = $this->getMock('\phpbb\avatar\driver\\' . $driver, array('get_name'), array($this->config, $phpbb_root_path, $phpEx, $path_helper, $dispatcher, $files_factory, $cache)); + $cur_avatar = $this->getMock('\phpbb\avatar\driver\\' . $driver, array('get_name'), array($this->config, $phpbb_root_path, $phpEx, $filesystem, $path_helper, $dispatcher, $files_factory, $cache)); } $cur_avatar->expects($this->any()) ->method('get_name') From eb11973ea8af41623a9e6e6c320cb1fb0df7a222 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Fri, 14 Aug 2015 09:13:27 +0200 Subject: [PATCH 17/66] [ticket/13904] Use factory instead of container and add factory to services PHPBB3-13904 --- .../default/container/services_files.yml | 6 +++++ phpBB/phpbb/files/upload.php | 24 +++++++++---------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/phpBB/config/default/container/services_files.yml b/phpBB/config/default/container/services_files.yml index cd27d0745c..8f3357022c 100644 --- a/phpBB/config/default/container/services_files.yml +++ b/phpBB/config/default/container/services_files.yml @@ -2,6 +2,11 @@ services: filesystem: class: phpbb\filesystem\filesystem + files.factory: + class: phpbb\files\factory + arguments: + - @service_container + files.filespec: class: phpbb\files\filespec scope: prototype @@ -15,3 +20,4 @@ services: scope: prototype arguments: - @filesystem + - @files.factory diff --git a/phpBB/phpbb/files/upload.php b/phpBB/phpbb/files/upload.php index 1892d22adf..7ca29efe1a 100644 --- a/phpBB/phpbb/files/upload.php +++ b/phpBB/phpbb/files/upload.php @@ -36,20 +36,20 @@ class upload */ protected $filesystem; + /** @var \phpbb\files\factory Files factory */ + protected $factory; + /** * Init file upload class. * * @param \phpbb\filesystem\filesystem_interface $filesystem + * @param \phpbb\files\factory $factory Files factory * */ - public function __construct(\phpbb\filesystem\filesystem_interface $filesystem) + public function __construct(\phpbb\filesystem\filesystem_interface $filesystem, factory $factory) { -// $this->set_allowed_extensions($allowed_extensions); -// $this->set_max_filesize($max_filesize); -// $this->set_allowed_dimensions($min_width, $min_height, $max_width, $max_height); -// $this->set_error_prefix($error_prefix); -// $this->set_disallowed_content($disallowed_content); $this->filesystem = $filesystem; + $this->factory = $factory; } /** @@ -138,7 +138,7 @@ class upload */ function form_upload($form_name, \phpbb\plupload\plupload $plupload = null) { - global $user, $request, $phpbb_container; + global $user, $request; $upload = $request->file($form_name); unset($upload['local_mode']); @@ -153,7 +153,7 @@ class upload } /** @var \phpbb\files\filespec $file */ - $file = $phpbb_container->get('files.filespec') + $file = $this->factory->get('filespec') ->set_upload_ary($upload) ->set_upload_namespace($this); @@ -217,7 +217,7 @@ class upload */ function local_upload($source_file, $filedata = false) { - global $user, $request, $phpbb_container; + global $user, $request; $upload = array(); @@ -237,7 +237,7 @@ class upload } /** @var \phpbb\files\filespec $file */ - $file = $phpbb_container->get('files.filespec') + $file = $this->factory->get('filespec') ->set_upload_ary($upload) ->set_upload_namespace($this); @@ -299,7 +299,7 @@ class upload */ function remote_upload($upload_url) { - global $user, $phpbb_root_path, $phpbb_container; + global $user, $phpbb_root_path; $upload_ary = array(); $upload_ary['local_mode'] = true; @@ -477,7 +477,7 @@ class upload $upload_ary['tmp_name'] = $filename; /** @var \phpbb\files\filespec $file */ - $file = $phpbb_container->get('files.filespec') + $file = $this->factory->get('filespec') ->set_upload_ary($upload_ary) ->set_upload_namespace($this); $this->common_checks($file); From a53825ad760cc8437d8c26eb1f947622c0fcf229 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Tue, 2 Jun 2015 11:48:55 +0200 Subject: [PATCH 18/66] [ticket/13904] No longer use fileerror class for extending filespec class PHPBB3-13904 --- phpBB/phpbb/files/filespec.php | 14 ++++++++++++++ phpBB/phpbb/files/upload.php | 27 +++++++++------------------ tests/upload/fileupload_test.php | 27 +++++++++++++++++---------- 3 files changed, 40 insertions(+), 28 deletions(-) diff --git a/phpBB/phpbb/files/filespec.php b/phpBB/phpbb/files/filespec.php index 2fdba2d793..d14c9a226d 100644 --- a/phpBB/phpbb/files/filespec.php +++ b/phpBB/phpbb/files/filespec.php @@ -118,6 +118,20 @@ class filespec return !isset($this->filename); } + /** + * Set error in error array + * + * @param mixed $error Content for error array + * + * @return \phpbb\files\filespec This instance of the filespec class + */ + public function set_error($error) + { + $this->error[] = $error; + + return $this; + } + /** * Cleans destination filename * diff --git a/phpBB/phpbb/files/upload.php b/phpBB/phpbb/files/upload.php index 7ca29efe1a..291cdb266c 100644 --- a/phpBB/phpbb/files/upload.php +++ b/phpBB/phpbb/files/upload.php @@ -306,14 +306,12 @@ class upload if (!preg_match('#^(https?://).*?\.(' . implode('|', $this->allowed_extensions) . ')$#i', $upload_url, $match)) { - $file = new fileerror($user->lang[$this->error_prefix . 'URL_INVALID']); - return $file; + return $this->factory->get('filespec')->set_error($user->lang[$this->error_prefix . 'URL_INVALID']); } if (empty($match[2])) { - $file = new fileerror($user->lang[$this->error_prefix . 'URL_INVALID']); - return $file; + return $this->factory->get('filespec')->set_error($user->lang[$this->error_prefix . 'URL_INVALID']);e; } $url = parse_url($upload_url); @@ -362,8 +360,7 @@ class upload if (!($fsock = @fsockopen($host, $port, $errno, $errstr))) { - $file = new fileerror($user->lang[$this->error_prefix . 'NOT_UPLOADED']); - return $file; + return $this->factory->get('filespec')->set_error($user->lang[$this->error_prefix . 'NOT_UPLOADED']); } // Make sure $path not beginning with / @@ -404,8 +401,7 @@ class upload { $max_filesize = get_formatted_filesize($remote_max_filesize, false); - $file = new fileerror(sprintf($user->lang[$this->error_prefix . 'WRONG_FILESIZE'], $max_filesize['value'], $max_filesize['unit'])); - return $file; + return $this->factory->get('filespec')->set_error(sprintf($user->lang[$this->error_prefix . 'WRONG_FILESIZE'], $max_filesize['value'], $max_filesize['unit'])); } $data .= $block; @@ -432,14 +428,12 @@ class upload { $max_filesize = get_formatted_filesize($remote_max_filesize, false); - $file = new fileerror(sprintf($user->lang[$this->error_prefix . 'WRONG_FILESIZE'], $max_filesize['value'], $max_filesize['unit'])); - return $file; + return $this->factory->get('filespec')->set_error(sprintf($user->lang[$this->error_prefix . 'WRONG_FILESIZE'], $max_filesize['value'], $max_filesize['unit'])); } } else if (stripos($line, '404 not found') !== false) { - $file = new fileerror($user->lang[$this->error_prefix . 'URL_NOT_FOUND']); - return $file; + return $this->factory->get('filespec')->set_error($this->error_prefix . 'URL_NOT_FOUND'); } } } @@ -449,16 +443,14 @@ class upload // Cancel upload if we exceed timeout if (!empty($stream_meta_data['timed_out']) || time() >= $timer_stop) { - $file = new fileerror($user->lang[$this->error_prefix . 'REMOTE_UPLOAD_TIMEOUT']); - return $file; + return $this->factory->get('filespec')->set_error($this->error_prefix . 'REMOTE_UPLOAD_TIMEOUT'); } } @fclose($fsock); if (empty($data)) { - $file = new fileerror($user->lang[$this->error_prefix . 'EMPTY_REMOTE_DATA']); - return $file; + return $this->factory->get('filespec')->set_error($this->error_prefix . 'EMPTY_REMOTE_DATA'); } $tmp_path = (!@ini_get('safe_mode') || strtolower(@ini_get('safe_mode')) == 'off') ? false : $phpbb_root_path . 'cache'; @@ -466,8 +458,7 @@ class upload if (!($fp = @fopen($filename, 'wb'))) { - $file = new fileerror($user->lang[$this->error_prefix . 'NOT_UPLOADED']); - return $file; + return $this->factory->get('filespec')->set_error($this->error_prefix . 'NOT_UPLOADED'); } $upload_ary['size'] = fwrite($fp, $data); diff --git a/tests/upload/fileupload_test.php b/tests/upload/fileupload_test.php index fe6096aebf..d4785c782c 100644 --- a/tests/upload/fileupload_test.php +++ b/tests/upload/fileupload_test.php @@ -22,6 +22,12 @@ class phpbb_fileupload_test extends phpbb_test_case private $filesystem; + /** @var \Symfony\Component\DependencyInjection\ContainerInterface */ + protected $container; + + /** @var \phpbb\files\factory */ + protected $factory; + protected function setUp() { // Global $config required by unique_id @@ -44,12 +50,13 @@ class phpbb_fileupload_test extends phpbb_test_case $this->filesystem = $phpbb_filesystem = new \phpbb\filesystem\filesystem(); - $phpbb_container = new phpbb_mock_container_builder($this->phpbb_root_path, $this->phpEx); - $phpbb_container->set('files.filespec', new \phpbb\files\filespec( + $this->container = new phpbb_mock_container_builder($this->phpbb_root_path, $this->phpEx); + $this->container->set('files.filespec', new \phpbb\files\filespec( $this->filesystem, new \phpbb\mimetype\guesser(array( 'mimetype.extension_guesser' => new \phpbb\mimetype\extension_guesser(), )))); + $this->factory = new \phpbb\files\factory($this->container); $this->path = __DIR__ . '/fixture/'; } @@ -76,7 +83,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_common_checks_invalid_extension() { - $upload = new \phpbb\files\upload($this->filesystem); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory); $upload->set_allowed_extensions(array('png')) ->set_max_filesize(100); $file = $this->gen_valid_filespec(); @@ -86,7 +93,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_common_checks_invalid_filename() { - $upload = new \phpbb\files\upload($this->filesystem); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory); $upload->set_allowed_extensions(array('jpg')) ->set_max_filesize(100); $file = $this->gen_valid_filespec(); @@ -97,7 +104,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_common_checks_too_large() { - $upload = new \phpbb\files\upload($this->filesystem); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory); $upload->set_allowed_extensions(array('jpg')) ->set_max_filesize(100); $file = $this->gen_valid_filespec(); @@ -108,7 +115,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_common_checks_valid_file() { - $upload = new \phpbb\files\upload($this->filesystem); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory); $upload->set_allowed_extensions(array('jpg')) ->set_max_filesize(1000); $file = $this->gen_valid_filespec(); @@ -118,7 +125,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_local_upload() { - $upload = new \phpbb\files\upload($this->filesystem); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory); $upload->set_allowed_extensions(array('jpg')) ->set_max_filesize(1000); @@ -130,7 +137,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_move_existent_file() { - $upload = new \phpbb\files\upload($this->filesystem); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory); $upload->set_allowed_extensions(array('jpg')) ->set_max_filesize(1000); @@ -144,7 +151,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_move_existent_file_overwrite() { - $upload = new \phpbb\files\upload($this->filesystem); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory); $upload->set_allowed_extensions(array('jpg')) ->set_max_filesize(1000); @@ -159,7 +166,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_valid_dimensions() { - $upload = new \phpbb\files\upload($this->filesystem); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory); $upload->set_allowed_extensions(false) ->set_max_filesize(false) ->set_allowed_dimensions(1, 1, 100, 100); From c34fd1e7c030860169bcbde9ac3c4f75d610cd60 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Tue, 2 Jun 2015 12:22:14 +0200 Subject: [PATCH 19/66] [ticket/13904] Fix fileupload functional tests PHPBB3-13904 --- tests/functional/fileupload_remote_test.php | 35 +++++++++++++++++---- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/tests/functional/fileupload_remote_test.php b/tests/functional/fileupload_remote_test.php index 4aa1a83b30..34e9ca2b05 100644 --- a/tests/functional/fileupload_remote_test.php +++ b/tests/functional/fileupload_remote_test.php @@ -11,15 +11,17 @@ * */ -require_once __DIR__ . '/../../phpBB/includes/functions_upload.php'; - /** * @group functional */ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case { + /** @var \phpbb\filesystem\filesystem_interface */ protected $filesystem; + /** @var \phpbb\files\factory */ + protected $factory; + public function setUp() { parent::setUp(); @@ -41,6 +43,11 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case $user = new phpbb_mock_user(); $user->lang = new phpbb_mock_lang(); $this->filesystem = new \phpbb\filesystem\filesystem(); + + $container = new phpbb_mock_container_builder(); + $container->set('files.filespec', new \phpbb\files\filespec($this->filesystem)); + $this->factory = new \phpbb\files\factory($container); + $container->set('files.factory', $this->factory); } public function tearDown() @@ -52,21 +59,33 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case public function test_invalid_extension() { - $upload = new fileupload($this->filesystem, '', array('jpg'), 100); + /** @var \phpbb\files\upload $upload */ + $upload = new \phpbb\files\upload($this->filesystem, $this->factory); + $upload->set_error_prefix('') + ->set_allowed_extensions(array('jpg')) + ->set_max_filesize(100); $file = $upload->remote_upload(self::$root_url . 'develop/blank.gif'); $this->assertEquals('URL_INVALID', $file->error[0]); } public function test_empty_file() { - $upload = new fileupload($this->filesystem, '', array('jpg'), 100); + /** @var \phpbb\files\upload $upload */ + $upload = new \phpbb\files\upload($this->filesystem, $this->factory); + $upload->set_error_prefix('') + ->set_allowed_extensions(array('jpg')) + ->set_max_filesize(100); $file = $upload->remote_upload(self::$root_url . 'develop/blank.jpg'); $this->assertEquals('EMPTY_REMOTE_DATA', $file->error[0]); } public function test_successful_upload() { - $upload = new fileupload($this->filesystem, '', array('gif'), 1000); + /** @var \phpbb\files\upload $upload */ + $upload = new \phpbb\files\upload($this->filesystem, $this->factory); + $upload->set_error_prefix('') + ->set_allowed_extensions(array('gif')) + ->set_max_filesize(1000); $file = $upload->remote_upload(self::$root_url . 'styles/prosilver/theme/images/forum_read.gif'); $this->assertEquals(0, sizeof($file->error)); $this->assertTrue(file_exists($file->filename)); @@ -74,7 +93,11 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case public function test_too_large() { - $upload = new fileupload($this->filesystem, '', array('gif'), 100); + /** @var \phpbb\files\upload $upload */ + $upload = new \phpbb\files\upload($this->filesystem, $this->factory); + $upload->set_error_prefix('') + ->set_allowed_extensions(array('gif')) + ->set_max_filesize(100); $file = $upload->remote_upload(self::$root_url . 'styles/prosilver/theme/images/forum_read.gif'); $this->assertEquals(1, sizeof($file->error)); $this->assertEquals('WRONG_FILESIZE', $file->error[0]); From 25df7f814920a67b1d9b1e65e797c44960026585 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Tue, 2 Jun 2015 12:24:31 +0200 Subject: [PATCH 20/66] [ticket/13904] Remove functions_fileupload.php PHPBB3-13904 --- phpBB/includes/functions_upload.php | 31 ----------------------------- tests/upload/filespec_test.php | 1 - tests/upload/fileupload_test.php | 1 - 3 files changed, 33 deletions(-) delete mode 100644 phpBB/includes/functions_upload.php diff --git a/phpBB/includes/functions_upload.php b/phpBB/includes/functions_upload.php deleted file mode 100644 index 398771e92a..0000000000 --- a/phpBB/includes/functions_upload.php +++ /dev/null @@ -1,31 +0,0 @@ - -* @license GNU General Public License, version 2 (GPL-2.0) -* -* For full copyright and license information, please see -* the docs/CREDITS.txt file. -* -*/ - -/** -* @ignore -*/ -if (!defined('IN_PHPBB')) -{ - exit; -} - -/** -* Class for assigning error messages before a real filespec class can be assigned -*/ -class fileerror extends \phpbb\files\filespec -{ - function fileerror($error_msg) - { - $this->error[] = $error_msg; - } -} diff --git a/tests/upload/filespec_test.php b/tests/upload/filespec_test.php index f3260bf1a9..10577ec8f3 100644 --- a/tests/upload/filespec_test.php +++ b/tests/upload/filespec_test.php @@ -13,7 +13,6 @@ require_once __DIR__ . '/../../phpBB/includes/functions.php'; require_once __DIR__ . '/../../phpBB/includes/utf/utf_tools.php'; -require_once __DIR__ . '/../../phpBB/includes/functions_upload.php'; class phpbb_filespec_test extends phpbb_test_case { diff --git a/tests/upload/fileupload_test.php b/tests/upload/fileupload_test.php index d4785c782c..8b7056e9cf 100644 --- a/tests/upload/fileupload_test.php +++ b/tests/upload/fileupload_test.php @@ -13,7 +13,6 @@ require_once __DIR__ . '/../../phpBB/includes/functions.php'; require_once __DIR__ . '/../../phpBB/includes/utf/utf_tools.php'; -require_once __DIR__ . '/../../phpBB/includes/functions_upload.php'; require_once __DIR__ . '/../mock/filespec.php'; class phpbb_fileupload_test extends phpbb_test_case From e4546ad03c0c0130e60d164f3741cc57c33b8980 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Tue, 2 Jun 2015 13:23:51 +0200 Subject: [PATCH 21/66] [ticket/13904] Improve doc blocks in upload class PHPBB3-13904 --- phpBB/phpbb/files/upload.php | 85 +++++++++++++++++++++++++++++++----- 1 file changed, 74 insertions(+), 11 deletions(-) diff --git a/phpBB/phpbb/files/upload.php b/phpBB/phpbb/files/upload.php index 291cdb266c..f253fc762d 100644 --- a/phpBB/phpbb/files/upload.php +++ b/phpBB/phpbb/files/upload.php @@ -19,21 +19,34 @@ namespace phpbb\files; */ class upload { + /** @var array Allowed file extensions */ var $allowed_extensions = array(); + + /** @var array Disallowed content */ var $disallowed_content = array('body', 'head', 'html', 'img', 'plaintext', 'a href', 'pre', 'script', 'table', 'title'); + + /** @var int Maximum filesize */ var $max_filesize = 0; + + /** @var int Minimum width of images */ var $min_width = 0; + + /** @var int Minimum height of images */ var $min_height = 0; + + /** @var int Maximum width of images */ var $max_width = 0; + + /** @var int Maximum height of images */ var $max_height = 0; + + /** @var string Prefix for language variables of errors */ var $error_prefix = ''; /** @var int Timeout for remote upload */ var $upload_timeout = 6; - /** - * @var \phpbb\filesystem\filesystem_interface - */ + /** @var \phpbb\filesystem\filesystem_interface */ protected $filesystem; /** @var \phpbb\files\factory Files factory */ @@ -44,7 +57,6 @@ class upload * * @param \phpbb\filesystem\filesystem_interface $filesystem * @param \phpbb\files\factory $factory Files factory - * */ public function __construct(\phpbb\filesystem\filesystem_interface $filesystem, factory $factory) { @@ -66,6 +78,10 @@ class upload /** * Set allowed extensions + * + * @param array $allowed_extensions Allowed file extensions + * + * @return \phpbb\files\upload This instance of upload */ function set_allowed_extensions($allowed_extensions) { @@ -79,6 +95,13 @@ class upload /** * Set allowed dimensions + * + * @param int $min_width Minimum image width + * @param int $min_height Minimum image height + * @param int $max_width Maximum image width + * @param int $max_height Maximum image height + * + * @return \phpbb\files\upload This instance of upload */ function set_allowed_dimensions($min_width, $min_height, $max_width, $max_height) { @@ -91,7 +114,11 @@ class upload } /** - * Set maximum allowed filesize + * Set maximum allowed file size + * + * @param int $max_filesize Maximum file size + * + * @return \phpbb\files\upload This instance of upload */ function set_max_filesize($max_filesize) { @@ -105,6 +132,10 @@ class upload /** * Set disallowed strings + * + * @param array $disallowed_content Disallowed content + * + * @return \phpbb\files\upload This instance of upload */ function set_disallowed_content($disallowed_content) { @@ -118,6 +149,10 @@ class upload /** * Set error prefix + * + * @param string $error_prefix Prefix for language variables of errors + * + * @return \phpbb\files\upload This instance of upload */ function set_error_prefix($error_prefix) { @@ -133,7 +168,7 @@ class upload * @param string $form_name Form name assigned to the file input field (if it is an array, the key has to be specified) * @param \phpbb\plupload\plupload $plupload The plupload object * - * @return object $file Object "filespec" is returned, all further operations can be done with this object + * @return filespec $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) @@ -152,7 +187,7 @@ class upload } } - /** @var \phpbb\files\filespec $file */ + /** @var filespec $file */ $file = $this->factory->get('filespec') ->set_upload_ary($upload) ->set_upload_namespace($this); @@ -214,6 +249,11 @@ class upload /** * Move file from another location to phpBB + * + * @param string $source_file Filename of source file + * @param array|bool $filedata Array with filedata or false + * + * @return filespec Object "filespec" is returned, all further operations can be done with this object */ function local_upload($source_file, $filedata = false) { @@ -236,7 +276,7 @@ class upload $upload['type'] = $filedata['type']; } - /** @var \phpbb\files\filespec $file */ + /** @var filespec $file */ $file = $this->factory->get('filespec') ->set_upload_ary($upload) ->set_upload_namespace($this); @@ -294,7 +334,7 @@ class upload * Uploads file from given url * * @param string $upload_url URL pointing to file to upload, for example http://www.foobar.com/example.gif - * @return object $file Object "filespec" is returned, all further operations can be done with this object + * @return filespec $file Object "filespec" is returned, all further operations can be done with this object * @access public */ function remote_upload($upload_url) @@ -467,7 +507,7 @@ class upload $upload_ary['tmp_name'] = $filename; - /** @var \phpbb\files\filespec $file */ + /** @var filespec $file */ $file = $this->factory->get('filespec') ->set_upload_ary($upload_ary) ->set_upload_namespace($this); @@ -478,6 +518,10 @@ class upload /** * Assign internal error + * + * @param string $errorcode Error code to assign + * + * @return string Error string * @access private */ function assign_internal_error($errorcode) @@ -528,7 +572,9 @@ class upload } /** - * Perform common checks + * Perform common file checks + * + * @param filespec $file Instance of filespec class */ function common_checks(&$file) { @@ -563,6 +609,10 @@ class upload /** * Check for allowed extension + * + * @param filespec $file Instance of filespec class + * + * @return bool True if extension is allowed, false if not */ function valid_extension(&$file) { @@ -571,6 +621,11 @@ class upload /** * Check for allowed dimension + * + * @param filespec $file Instance of filespec class + * + * @return bool True if dimensions are valid or no constraints set, false + * if not */ function valid_dimensions(&$file) { @@ -592,6 +647,10 @@ class upload /** * Check if form upload is valid + * + * @param string $form_name Name of form + * + * @return bool True if form upload is valid, false if not */ function is_valid($form_name) { @@ -604,6 +663,10 @@ class upload /** * Check for bad content (IE mime-sniffing) + * + * @param filespec $file Instance of filespec class + * + * @return bool True if content is valid, false if not */ function valid_content(&$file) { From 0121e60cd73963043047e1b29b8d94ea9aa684e3 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Tue, 2 Jun 2015 13:33:10 +0200 Subject: [PATCH 22/66] [ticket/13904] Use language class instead of user global in upload PHPBB3-13904 --- .../default/container/services_files.yml | 1 + phpBB/phpbb/files/upload.php | 55 ++++++++++--------- 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/phpBB/config/default/container/services_files.yml b/phpBB/config/default/container/services_files.yml index 8f3357022c..c275b3f205 100644 --- a/phpBB/config/default/container/services_files.yml +++ b/phpBB/config/default/container/services_files.yml @@ -21,3 +21,4 @@ services: arguments: - @filesystem - @files.factory + - @language diff --git a/phpBB/phpbb/files/upload.php b/phpBB/phpbb/files/upload.php index f253fc762d..e37f90e820 100644 --- a/phpBB/phpbb/files/upload.php +++ b/phpBB/phpbb/files/upload.php @@ -13,6 +13,8 @@ namespace phpbb\files; +use \phpbb\language\language; + /** * File upload class * Init class (all parameters optional and able to be set/overwritten separately) - scope is global and valid for all uploads @@ -52,16 +54,21 @@ class upload /** @var \phpbb\files\factory Files factory */ protected $factory; + /** @var \phpbb\language\language Language class */ + protected $language; + /** * Init file upload class. * * @param \phpbb\filesystem\filesystem_interface $filesystem * @param \phpbb\files\factory $factory Files factory + * @param \phpbb\language\language $language Language class */ - public function __construct(\phpbb\filesystem\filesystem_interface $filesystem, factory $factory) + public function __construct(\phpbb\filesystem\filesystem_interface $filesystem, factory $factory, language $language) { $this->filesystem = $filesystem; $this->factory = $factory; + $this->language = $language; } /** @@ -173,7 +180,7 @@ class upload */ function form_upload($form_name, \phpbb\plupload\plupload $plupload = null) { - global $user, $request; + global $request; $upload = $request->file($form_name); unset($upload['local_mode']); @@ -213,7 +220,7 @@ class upload // Check if empty file got uploaded (not catched by is_uploaded_file) if (isset($upload['size']) && $upload['size'] == 0) { - $file->error[] = $user->lang[$this->error_prefix . 'EMPTY_FILEUPLOAD']; + $file->error[] = $this->language->lang($this->error_prefix . 'EMPTY_FILEUPLOAD'); return $file; } @@ -231,14 +238,14 @@ class upload $unit = ($unit == 'k') ? 'KB' : (($unit == 'g') ? 'GB' : 'MB'); } - $file->error[] = (empty($max_filesize)) ? $user->lang[$this->error_prefix . 'PHP_SIZE_NA'] : sprintf($user->lang[$this->error_prefix . 'PHP_SIZE_OVERRUN'], $max_filesize, $user->lang[$unit]); + $file->error[] = (empty($max_filesize)) ? $this->language->lang($this->error_prefix . 'PHP_SIZE_NA') : $this->language->lang($this->error_prefix . 'PHP_SIZE_OVERRUN', $max_filesize, $this->language->lang($unit)); return $file; } // Not correctly uploaded if (!$file->is_uploaded()) { - $file->error[] = $user->lang[$this->error_prefix . 'NOT_UPLOADED']; + $file->error[] = $this->language->lang($this->error_prefix . 'NOT_UPLOADED'); return $file; } @@ -257,7 +264,7 @@ class upload */ function local_upload($source_file, $filedata = false) { - global $user, $request; + global $request; $upload = array(); @@ -312,14 +319,14 @@ class upload $unit = ($unit == 'k') ? 'KB' : (($unit == 'g') ? 'GB' : 'MB'); } - $file->error[] = (empty($max_filesize)) ? $user->lang[$this->error_prefix . 'PHP_SIZE_NA'] : sprintf($user->lang[$this->error_prefix . 'PHP_SIZE_OVERRUN'], $max_filesize, $user->lang[$unit]); + $file->error[] = (empty($max_filesize)) ?$this->language->lang($this->error_prefix . 'PHP_SIZE_NA') : $this->language->lang($this->error_prefix . 'PHP_SIZE_OVERRUN', $max_filesize, $this->language->lang($unit)); return $file; } // Not correctly uploaded if (!$file->is_uploaded()) { - $file->error[] = $user->lang[$this->error_prefix . 'NOT_UPLOADED']; + $file->error[] = $this->language->lang($this->error_prefix . 'NOT_UPLOADED'); return $file; } @@ -339,19 +346,19 @@ class upload */ function remote_upload($upload_url) { - global $user, $phpbb_root_path; + global $phpbb_root_path; $upload_ary = array(); $upload_ary['local_mode'] = true; if (!preg_match('#^(https?://).*?\.(' . implode('|', $this->allowed_extensions) . ')$#i', $upload_url, $match)) { - return $this->factory->get('filespec')->set_error($user->lang[$this->error_prefix . 'URL_INVALID']); + return $this->factory->get('filespec')->set_error($this->language->lang($this->error_prefix . 'URL_INVALID')); } if (empty($match[2])) { - return $this->factory->get('filespec')->set_error($user->lang[$this->error_prefix . 'URL_INVALID']);e; + return $this->factory->get('filespec')->set_error($this->language->lang($this->error_prefix . 'URL_INVALID')); } $url = parse_url($upload_url); @@ -400,7 +407,7 @@ class upload if (!($fsock = @fsockopen($host, $port, $errno, $errstr))) { - return $this->factory->get('filespec')->set_error($user->lang[$this->error_prefix . 'NOT_UPLOADED']); + return $this->factory->get('filespec')->set_error($this->language->lang($this->error_prefix . 'NOT_UPLOADED')); } // Make sure $path not beginning with / @@ -441,7 +448,7 @@ class upload { $max_filesize = get_formatted_filesize($remote_max_filesize, false); - return $this->factory->get('filespec')->set_error(sprintf($user->lang[$this->error_prefix . 'WRONG_FILESIZE'], $max_filesize['value'], $max_filesize['unit'])); + return $this->factory->get('filespec')->set_error($this->language->lang($this->error_prefix . 'WRONG_FILESIZE', $max_filesize['value'], $max_filesize['unit'])); } $data .= $block; @@ -468,7 +475,7 @@ class upload { $max_filesize = get_formatted_filesize($remote_max_filesize, false); - return $this->factory->get('filespec')->set_error(sprintf($user->lang[$this->error_prefix . 'WRONG_FILESIZE'], $max_filesize['value'], $max_filesize['unit'])); + return $this->factory->get('filespec')->set_error($this->language->lang($this->error_prefix . 'WRONG_FILESIZE', $max_filesize['value'], $max_filesize['unit'])); } } else if (stripos($line, '404 not found') !== false) @@ -526,8 +533,6 @@ class upload */ function assign_internal_error($errorcode) { - global $user; - switch ($errorcode) { case 1: @@ -542,21 +547,21 @@ class upload $unit = ($unit == 'k') ? 'KB' : (($unit == 'g') ? 'GB' : 'MB'); } - $error = (empty($max_filesize)) ? $user->lang[$this->error_prefix . 'PHP_SIZE_NA'] : sprintf($user->lang[$this->error_prefix . 'PHP_SIZE_OVERRUN'], $max_filesize, $user->lang[$unit]); + $error = (empty($max_filesize)) ? $this->language->lang($this->error_prefix . 'PHP_SIZE_NA') : $this->language->lang($this->error_prefix . 'PHP_SIZE_OVERRUN', $max_filesize, $this->language->lang($unit)); break; case 2: $max_filesize = get_formatted_filesize($this->max_filesize, false); - $error = sprintf($user->lang[$this->error_prefix . 'WRONG_FILESIZE'], $max_filesize['value'], $max_filesize['unit']); + $error = $this->language->lang($this->error_prefix . 'WRONG_FILESIZE', $max_filesize['value'], $max_filesize['unit']); break; case 3: - $error = $user->lang[$this->error_prefix . 'PARTIAL_UPLOAD']; + $error = $this->language->lang($this->error_prefix . 'PARTIAL_UPLOAD'); break; case 4: - $error = $user->lang[$this->error_prefix . 'NOT_UPLOADED']; + $error = $this->language->lang($this->error_prefix . 'NOT_UPLOADED'); break; case 6: @@ -578,32 +583,30 @@ class upload */ function common_checks(&$file) { - global $user; - // Filesize is too big or it's 0 if it was larger than the maxsize in the upload form if ($this->max_filesize && ($file->get('filesize') > $this->max_filesize || $file->get('filesize') == 0)) { $max_filesize = get_formatted_filesize($this->max_filesize, false); - $file->error[] = sprintf($user->lang[$this->error_prefix . 'WRONG_FILESIZE'], $max_filesize['value'], $max_filesize['unit']); + $file->error[] = $this->language->lang($this->error_prefix . 'WRONG_FILESIZE', $max_filesize['value'], $max_filesize['unit']); } // check Filename if (preg_match("#[\\/:*?\"<>|]#i", $file->get('realname'))) { - $file->error[] = sprintf($user->lang[$this->error_prefix . 'INVALID_FILENAME'], $file->get('realname')); + $file->error[] = $this->language->lang($this->error_prefix . 'INVALID_FILENAME', $file->get('realname')); } // Invalid Extension if (!$this->valid_extension($file)) { - $file->error[] = sprintf($user->lang[$this->error_prefix . 'DISALLOWED_EXTENSION'], $file->get('extension')); + $file->error[] = $this->language->lang($this->error_prefix . 'DISALLOWED_EXTENSION', $file->get('extension')); } // MIME Sniffing if (!$this->valid_content($file)) { - $file->error[] = sprintf($user->lang[$this->error_prefix . 'DISALLOWED_CONTENT']); + $file->error[] = $this->language->lang($this->error_prefix . 'DISALLOWED_CONTENT'); } } From ef59e0228a993bcaf7bf59c7cb24511f3cbe1b78 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Tue, 2 Jun 2015 13:42:58 +0200 Subject: [PATCH 23/66] [ticket/13904] Fix tests again PHPBB3-13904 --- tests/functional/fileupload_remote_test.php | 15 ++++++++----- tests/upload/fileupload_test.php | 25 ++++++++++++--------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/tests/functional/fileupload_remote_test.php b/tests/functional/fileupload_remote_test.php index 34e9ca2b05..7301297096 100644 --- a/tests/functional/fileupload_remote_test.php +++ b/tests/functional/fileupload_remote_test.php @@ -22,6 +22,9 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case /** @var \phpbb\files\factory */ protected $factory; + /** @var \phpbb\language\language */ + protected $language; + public function setUp() { parent::setUp(); @@ -30,7 +33,7 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case // Global $config required by unique_id // Global $user required by fileupload::remote_upload - global $config, $user; + global $config, $user, $phpbb_root_path, $phpEx; if (!is_array($config)) { @@ -48,6 +51,8 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case $container->set('files.filespec', new \phpbb\files\filespec($this->filesystem)); $this->factory = new \phpbb\files\factory($container); $container->set('files.factory', $this->factory); + + $this->language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)); } public function tearDown() @@ -60,7 +65,7 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case public function test_invalid_extension() { /** @var \phpbb\files\upload $upload */ - $upload = new \phpbb\files\upload($this->filesystem, $this->factory); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language); $upload->set_error_prefix('') ->set_allowed_extensions(array('jpg')) ->set_max_filesize(100); @@ -71,7 +76,7 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case public function test_empty_file() { /** @var \phpbb\files\upload $upload */ - $upload = new \phpbb\files\upload($this->filesystem, $this->factory); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language); $upload->set_error_prefix('') ->set_allowed_extensions(array('jpg')) ->set_max_filesize(100); @@ -82,7 +87,7 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case public function test_successful_upload() { /** @var \phpbb\files\upload $upload */ - $upload = new \phpbb\files\upload($this->filesystem, $this->factory); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language); $upload->set_error_prefix('') ->set_allowed_extensions(array('gif')) ->set_max_filesize(1000); @@ -94,7 +99,7 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case public function test_too_large() { /** @var \phpbb\files\upload $upload */ - $upload = new \phpbb\files\upload($this->filesystem, $this->factory); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language); $upload->set_error_prefix('') ->set_allowed_extensions(array('gif')) ->set_max_filesize(100); diff --git a/tests/upload/fileupload_test.php b/tests/upload/fileupload_test.php index 8b7056e9cf..87e10ac954 100644 --- a/tests/upload/fileupload_test.php +++ b/tests/upload/fileupload_test.php @@ -27,12 +27,15 @@ class phpbb_fileupload_test extends phpbb_test_case /** @var \phpbb\files\factory */ protected $factory; + /** @var \phpbb\language\language */ + protected $language; + protected function setUp() { // Global $config required by unique_id // Global $user required by several functions dealing with translations // Global $request required by form_upload, local_upload and is_valid - global $config, $user, $request, $phpbb_filesystem, $phpbb_container; + global $config, $user, $request, $phpbb_filesystem, $phpbb_root_path, $phpEx; if (!is_array($config)) { @@ -49,7 +52,7 @@ class phpbb_fileupload_test extends phpbb_test_case $this->filesystem = $phpbb_filesystem = new \phpbb\filesystem\filesystem(); - $this->container = new phpbb_mock_container_builder($this->phpbb_root_path, $this->phpEx); + $this->container = new phpbb_mock_container_builder($phpbb_root_path, $phpEx); $this->container->set('files.filespec', new \phpbb\files\filespec( $this->filesystem, new \phpbb\mimetype\guesser(array( @@ -57,6 +60,8 @@ class phpbb_fileupload_test extends phpbb_test_case )))); $this->factory = new \phpbb\files\factory($this->container); + $this->language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)); + $this->path = __DIR__ . '/fixture/'; } @@ -82,7 +87,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_common_checks_invalid_extension() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language); $upload->set_allowed_extensions(array('png')) ->set_max_filesize(100); $file = $this->gen_valid_filespec(); @@ -92,7 +97,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_common_checks_invalid_filename() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language); $upload->set_allowed_extensions(array('jpg')) ->set_max_filesize(100); $file = $this->gen_valid_filespec(); @@ -103,7 +108,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_common_checks_too_large() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language); $upload->set_allowed_extensions(array('jpg')) ->set_max_filesize(100); $file = $this->gen_valid_filespec(); @@ -114,7 +119,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_common_checks_valid_file() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language); $upload->set_allowed_extensions(array('jpg')) ->set_max_filesize(1000); $file = $this->gen_valid_filespec(); @@ -124,7 +129,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_local_upload() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language); $upload->set_allowed_extensions(array('jpg')) ->set_max_filesize(1000); @@ -136,7 +141,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_move_existent_file() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language); $upload->set_allowed_extensions(array('jpg')) ->set_max_filesize(1000); @@ -150,7 +155,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_move_existent_file_overwrite() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language); $upload->set_allowed_extensions(array('jpg')) ->set_max_filesize(1000); @@ -165,7 +170,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_valid_dimensions() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language); $upload->set_allowed_extensions(false) ->set_max_filesize(false) ->set_allowed_dimensions(1, 1, 100, 100); From 6541e4cb17d3014151d469b870eac5637ed23071 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Tue, 2 Jun 2015 14:52:09 +0200 Subject: [PATCH 24/66] [ticket/13904] Improve doc blocks in filespec class PHPBB3-13904 --- phpBB/phpbb/files/filespec.php | 94 +++++++++++++++++++++++++++------- 1 file changed, 76 insertions(+), 18 deletions(-) diff --git a/phpBB/phpbb/files/filespec.php b/phpBB/phpbb/files/filespec.php index d14c9a226d..d091e975d5 100644 --- a/phpBB/phpbb/files/filespec.php +++ b/phpBB/phpbb/files/filespec.php @@ -19,25 +19,50 @@ namespace phpbb\files; */ class filespec { + /** @var string File name */ var $filename = ''; + + /** @var string Real name of file */ var $realname = ''; + + /** @var string Upload name of file */ var $uploadname = ''; + + /** @var string Mimetype of file */ var $mimetype = ''; + + /** @var string File extension */ var $extension = ''; + + /** @var int File size */ var $filesize = 0; + + /** @var int Width of file */ var $width = 0; + + /** @var int Height of file */ var $height = 0; + + /** @var array Image info including type and size */ var $image_info = array(); + /** @var string Destination file name */ var $destination_file = ''; + + /** @var string Destination file path */ var $destination_path = ''; + /** @var bool Whether file was moved */ var $file_moved = false; + + /** @var bool Whether file is local */ var $local = false; + /** @var array Error array */ var $error = array(); - var $upload = ''; + /** @var upload Instance of upload class */ + var $upload; /** * @var \phpbb\filesystem\filesystem_interface @@ -57,20 +82,26 @@ class filespec protected $mimetype_guesser; /** - * File Class - * @access private + * File upload class + * + * @param \phpbb\filesystem\filesystem_interface $phpbb_filesystem + * @param \phpbb\mimetype\guesser $mimetype_guesser + * @param \phpbb\plupload\plupload $plupload */ function __construct(\phpbb\filesystem\filesystem_interface $phpbb_filesystem, \phpbb\mimetype\guesser $mimetype_guesser = null, \phpbb\plupload\plupload $plupload = null) { - // @todo call this via files - //$this->set_upload_ary($upload_ary); - //$this->set_upload_namespace($upload_namespace); - $this->plupload = $plupload; $this->mimetype_guesser = $mimetype_guesser; $this->filesystem = $phpbb_filesystem; } + /** + * Set upload ary + * + * @param array $upload_ary Upload ary + * + * @return filespec This instance of the filespec class + */ public function set_upload_ary($upload_ary) { $this->filename = $upload_ary['tmp_name']; @@ -101,6 +132,13 @@ class filespec return $this; } + /** + * Set the upload namespace + * + * @param upload $namespace Instance of upload class + * + * @return filespec This instance of the filespec class + */ public function set_upload_namespace($namespace) { $this->upload = $namespace; @@ -109,7 +147,7 @@ class filespec } /** - * Check if class members were not properly initalised yet + * Check if class members were not properly initialised yet * * @return bool True if there was an init error, false if not */ @@ -135,10 +173,13 @@ class filespec /** * Cleans destination filename * - * @param real|unique|unique_ext $mode real creates a realname, filtering some characters, lowering every character. Unique creates an unique filename + * @param string $mode Either real, unique, or unique_ext. Real creates a + * realname, filtering some characters, lowering every + * character. Unique creates a unique filename. * @param string $prefix Prefix applied to filename * @param string $user_id The user_id is only needed for when cleaning a user's avatar - * @access public + * + *@access public */ function clean_filename($mode = 'unique', $prefix = '', $user_id = '') { @@ -184,6 +225,10 @@ class filespec /** * Get property from file object + * + * @param string $property Name of property + * + * @return mixed Content of property */ function get($property) { @@ -196,9 +241,9 @@ class filespec } /** - * Check if file is an image (mimetype) + * Check if file is an image (mime type) * - * @return true if it is an image, false if not + * @return bool true if it is an image, false if not */ function is_image() { @@ -208,7 +253,7 @@ class filespec /** * Check if the file got correctly uploaded * - * @return true if it is a valid upload, false if not + * @return bool true if it is a valid upload, false if not */ function is_uploaded() { @@ -241,7 +286,8 @@ class filespec /** * Get file extension * - * @param string Filename that needs to be checked + * @param string $filename Filename that needs to be checked + * * @return string Extension of the supplied filename */ static public function get_extension($filename) @@ -258,10 +304,10 @@ class filespec } /** - * Get mimetype + * Get mime type * * @param string $filename Filename that needs to be checked - * @return string Mimetype of supplied filename + * @return string Mime type of supplied filename */ function get_mimetype($filename) { @@ -279,7 +325,11 @@ class filespec } /** - * Get filesize + * Get file size + * + * @param string $filename File name of file to check + * + * @return int File size */ function get_filesize($filename) { @@ -289,6 +339,10 @@ class filespec /** * Check the first 256 bytes for forbidden content + * + * @param array $disallowed_content Array containg disallowed content + * + * @return bool False if disallowed content found, true if not */ function check_content($disallowed_content) { @@ -321,8 +375,10 @@ class filespec * @param string $destination Destination path, for example $config['avatar_path'] * @param bool $overwrite If set to true, an already existing file will be overwritten * @param bool $skip_image_check If set to true, the check for the file to be a valid image is skipped - * @param string $chmod Permission mask for chmodding the file after a successful move. The mode entered here reflects the mode defined by {@link phpbb_chmod()} + * @param string|bool $chmod Permission mask for chmodding the file after a successful move. + * The mode entered here reflects the mode defined by {@link phpbb_chmod()} * + * @return bool True if file was moved, false if not * @access public */ function move_file($destination, $overwrite = false, $skip_image_check = false, $chmod = false) @@ -475,6 +531,8 @@ class filespec /** * Performing additional checks + * + * @return bool False if issue was found, true if not */ function additional_checks() { From 697ac5f4aa151b06ed65f8352652443bf297682a Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Tue, 2 Jun 2015 15:06:24 +0200 Subject: [PATCH 25/66] [ticket/13904] Use language class instead of global user in filespec PHPBB3-13904 --- .../default/container/services_files.yml | 1 + phpBB/phpbb/files/filespec.php | 45 ++++++++++--------- tests/functional/fileupload_remote_test.php | 10 ++--- tests/upload/filespec_test.php | 19 ++++---- tests/upload/fileupload_test.php | 12 ++--- 5 files changed, 41 insertions(+), 46 deletions(-) diff --git a/phpBB/config/default/container/services_files.yml b/phpBB/config/default/container/services_files.yml index c275b3f205..aabb5583d1 100644 --- a/phpBB/config/default/container/services_files.yml +++ b/phpBB/config/default/container/services_files.yml @@ -12,6 +12,7 @@ services: scope: prototype arguments: - @filesystem + - @language - @mimetype.guesser - @plupload diff --git a/phpBB/phpbb/files/filespec.php b/phpBB/phpbb/files/filespec.php index d091e975d5..5e685615d7 100644 --- a/phpBB/phpbb/files/filespec.php +++ b/phpBB/phpbb/files/filespec.php @@ -13,6 +13,8 @@ namespace phpbb\files; +use \phpbb\language\language; + /** * Responsible for holding all file relevant information, as well as doing file-specific operations. * The {@link fileupload fileupload class} can be used to upload several files, each of them being this object to operate further on. @@ -81,18 +83,23 @@ class filespec */ protected $mimetype_guesser; + /** @var \phpbb\language\language Language class */ + protected $language; + /** * File upload class * * @param \phpbb\filesystem\filesystem_interface $phpbb_filesystem + * @param \phpbb\language\language $language * @param \phpbb\mimetype\guesser $mimetype_guesser * @param \phpbb\plupload\plupload $plupload */ - function __construct(\phpbb\filesystem\filesystem_interface $phpbb_filesystem, \phpbb\mimetype\guesser $mimetype_guesser = null, \phpbb\plupload\plupload $plupload = null) + function __construct(\phpbb\filesystem\filesystem_interface $phpbb_filesystem, language $language, \phpbb\mimetype\guesser $mimetype_guesser = null, \phpbb\plupload\plupload $plupload = null) { $this->plupload = $plupload; $this->mimetype_guesser = $mimetype_guesser; $this->filesystem = $phpbb_filesystem; + $this->language = $language; } /** @@ -383,7 +390,7 @@ class filespec */ function move_file($destination, $overwrite = false, $skip_image_check = false, $chmod = false) { - global $user, $phpbb_root_path; + global $phpbb_root_path; if (sizeof($this->error)) { @@ -410,7 +417,7 @@ class filespec if (file_exists($this->destination_file) && !$overwrite) { @unlink($this->filename); - $this->error[] = $user->lang($this->upload->error_prefix . 'GENERAL_UPLOAD_ERROR', $this->destination_file); + $this->error[] = $this->language->lang($this->upload->error_prefix . 'GENERAL_UPLOAD_ERROR', $this->destination_file); $this->file_moved = false; return false; } @@ -429,7 +436,7 @@ class filespec { if (!@move_uploaded_file($this->filename, $this->destination_file)) { - $this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'GENERAL_UPLOAD_ERROR'], $this->destination_file); + $this->error[] = $this->language->lang($this->upload->error_prefix . 'GENERAL_UPLOAD_ERROR', $this->destination_file); } } @@ -441,7 +448,7 @@ class filespec { if (!@copy($this->filename, $this->destination_file)) { - $this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'GENERAL_UPLOAD_ERROR'], $this->destination_file); + $this->error[] = $this->language->lang($this->upload->error_prefix . 'GENERAL_UPLOAD_ERROR', $this->destination_file); } } @@ -451,7 +458,7 @@ class filespec if (!@copy($this->filename, $this->destination_file)) { - $this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'GENERAL_UPLOAD_ERROR'], $this->destination_file); + $this->error[] = $this->language->lang($this->upload->error_prefix . 'GENERAL_UPLOAD_ERROR', $this->destination_file); } break; @@ -502,23 +509,23 @@ class filespec { if (!isset($types[$this->image_info['type']])) { - $this->error[] = $user->lang('IMAGE_FILETYPE_INVALID', $this->image_info['type'], $this->mimetype); + $this->error[] = $this->language->lang('IMAGE_FILETYPE_INVALID', $this->image_info['type'], $this->mimetype); } else { - $this->error[] = $user->lang('IMAGE_FILETYPE_MISMATCH', $types[$this->image_info['type']][0], $this->extension); + $this->error[] = $this->language->lang('IMAGE_FILETYPE_MISMATCH', $types[$this->image_info['type']][0], $this->extension); } } // Make sure the dimensions match a valid image if (empty($this->width) || empty($this->height)) { - $this->error[] = $user->lang['ATTACHED_IMAGE_NOT_IMAGE']; + $this->error[] = $this->language->lang('ATTACHED_IMAGE_NOT_IMAGE'); } } else { - $this->error[] = $user->lang['UNABLE_GET_IMAGE_SIZE']; + $this->error[] = $this->language->lang('UNABLE_GET_IMAGE_SIZE'); } } @@ -536,8 +543,6 @@ class filespec */ function additional_checks() { - global $user; - if (!$this->file_moved) { return false; @@ -548,20 +553,20 @@ class filespec { $max_filesize = get_formatted_filesize($this->upload->max_filesize, false); - $this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'WRONG_FILESIZE'], $max_filesize['value'], $max_filesize['unit']); + $this->error[] = $this->language->lang($this->upload->error_prefix . 'WRONG_FILESIZE', $max_filesize['value'], $max_filesize['unit']); return false; } if (!$this->upload->valid_dimensions($this)) { - $this->error[] = $user->lang($this->upload->error_prefix . 'WRONG_SIZE', - $user->lang('PIXELS', (int) $this->upload->min_width), - $user->lang('PIXELS', (int) $this->upload->min_height), - $user->lang('PIXELS', (int) $this->upload->max_width), - $user->lang('PIXELS', (int) $this->upload->max_height), - $user->lang('PIXELS', (int) $this->width), - $user->lang('PIXELS', (int) $this->height)); + $this->error[] = $this->language->lang($this->upload->error_prefix . 'WRONG_SIZE', + $this->language->lang('PIXELS', (int) $this->upload->min_width), + $this->language->lang('PIXELS', (int) $this->upload->min_height), + $this->language->lang('PIXELS', (int) $this->upload->max_width), + $this->language->lang('PIXELS', (int) $this->upload->max_height), + $this->language->lang('PIXELS', (int) $this->width), + $this->language->lang('PIXELS', (int) $this->height)); return false; } diff --git a/tests/functional/fileupload_remote_test.php b/tests/functional/fileupload_remote_test.php index 7301297096..ae4de26df4 100644 --- a/tests/functional/fileupload_remote_test.php +++ b/tests/functional/fileupload_remote_test.php @@ -32,8 +32,7 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case // URL // Global $config required by unique_id - // Global $user required by fileupload::remote_upload - global $config, $user, $phpbb_root_path, $phpEx; + global $config, $phpbb_root_path, $phpEx; if (!is_array($config)) { @@ -43,16 +42,13 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case $config['rand_seed'] = ''; $config['rand_seed_last_update'] = time() + 600; - $user = new phpbb_mock_user(); - $user->lang = new phpbb_mock_lang(); $this->filesystem = new \phpbb\filesystem\filesystem(); + $this->language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)); $container = new phpbb_mock_container_builder(); - $container->set('files.filespec', new \phpbb\files\filespec($this->filesystem)); + $container->set('files.filespec', new \phpbb\files\filespec($this->filesystem, $this->language)); $this->factory = new \phpbb\files\factory($container); $container->set('files.factory', $this->factory); - - $this->language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)); } public function tearDown() diff --git a/tests/upload/filespec_test.php b/tests/upload/filespec_test.php index 10577ec8f3..b28adc3f28 100644 --- a/tests/upload/filespec_test.php +++ b/tests/upload/filespec_test.php @@ -25,12 +25,13 @@ class phpbb_filespec_test extends phpbb_test_case private $filesystem; public $path; + /** @var \phpbb\language\language */ + protected $language; + protected function setUp() { // Global $config required by unique_id - // Global $user required by filespec::additional_checks and - // filespec::move_file - global $config, $user, $phpbb_filesystem; + global $config, $phpbb_root_path, $phpEx; if (!is_array($config)) { @@ -44,9 +45,6 @@ class phpbb_filespec_test extends phpbb_test_case // See: phpBB/install/schemas/schema_data.sql $config['mime_triggers'] = 'body|head|html|img|plaintext|a href|pre|script|table|title'; - $user = new phpbb_mock_user(); - $user->lang = new phpbb_mock_lang(); - $this->config = &$config; $this->path = __DIR__ . '/fixture/'; @@ -75,8 +73,9 @@ class phpbb_filespec_test extends phpbb_test_case $guessers[2]->set_priority(-2); $guessers[3]->set_priority(-2); $this->mimetype_guesser = new \phpbb\mimetype\guesser($guessers); + $this->language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)); - $this->filesystem = $phpbb_filesystem = new \phpbb\filesystem\filesystem(); + $this->filesystem = new \phpbb\filesystem\filesystem(); } private function get_filespec($override = array()) @@ -90,15 +89,13 @@ class phpbb_filespec_test extends phpbb_test_case 'error' => '', ); - $filespec = new \phpbb\files\filespec($this->filesystem, $this->mimetype_guesser); + $filespec = new \phpbb\files\filespec($this->filesystem, $this->language, $this->mimetype_guesser); return $filespec->set_upload_ary(array_merge($upload_ary, $override)); } protected function tearDown() { - global $user; $this->config = array(); - $user = null; $iterator = new DirectoryIterator($this->path . 'copies'); foreach ($iterator as $fileinfo) @@ -289,7 +286,7 @@ class phpbb_filespec_test extends phpbb_test_case array('txt_copy', 'txt_as_img', 'image/jpg', 'txt', false, true), array('txt_copy_2', 'txt_moved', 'text/plain', 'txt', false, true), array('jpg_copy', 'jpg_moved', 'image/png', 'jpg', false, true), - array('png_copy', 'png_moved', 'image/png', 'jpg', 'IMAGE_FILETYPE_MISMATCH png jpg', true), + array('png_copy', 'png_moved', 'image/png', 'jpg', 'Image file type mismatch: expected extension png but extension jpg given.', true), ); } diff --git a/tests/upload/fileupload_test.php b/tests/upload/fileupload_test.php index 87e10ac954..56e29a3ac2 100644 --- a/tests/upload/fileupload_test.php +++ b/tests/upload/fileupload_test.php @@ -33,9 +33,8 @@ class phpbb_fileupload_test extends phpbb_test_case protected function setUp() { // Global $config required by unique_id - // Global $user required by several functions dealing with translations // Global $request required by form_upload, local_upload and is_valid - global $config, $user, $request, $phpbb_filesystem, $phpbb_root_path, $phpEx; + global $config, $request, $phpbb_root_path, $phpEx; if (!is_array($config)) { @@ -45,23 +44,20 @@ class phpbb_fileupload_test extends phpbb_test_case $config['rand_seed'] = ''; $config['rand_seed_last_update'] = time() + 600; - $user = new phpbb_mock_user(); - $user->lang = new phpbb_mock_lang(); - $request = new phpbb_mock_request(); - $this->filesystem = $phpbb_filesystem = new \phpbb\filesystem\filesystem(); + $this->filesystem = new \phpbb\filesystem\filesystem(); + $this->language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)); $this->container = new phpbb_mock_container_builder($phpbb_root_path, $phpEx); $this->container->set('files.filespec', new \phpbb\files\filespec( $this->filesystem, + $this->language, new \phpbb\mimetype\guesser(array( 'mimetype.extension_guesser' => new \phpbb\mimetype\extension_guesser(), )))); $this->factory = new \phpbb\files\factory($this->container); - $this->language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)); - $this->path = __DIR__ . '/fixture/'; } From 47f8f2cc88bdcd40087c8e391be1d33d36a2d308 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Tue, 2 Jun 2015 15:24:38 +0200 Subject: [PATCH 26/66] [ticket/13904] Pass request service to upload instead of using global PHPBB3-13904 --- .../default/container/services_files.yml | 1 + phpBB/phpbb/files/upload.php | 23 ++++++++++-------- tests/functional/fileupload_remote_test.php | 12 ++++++---- tests/upload/fileupload_test.php | 24 ++++++++++--------- 4 files changed, 35 insertions(+), 25 deletions(-) diff --git a/phpBB/config/default/container/services_files.yml b/phpBB/config/default/container/services_files.yml index aabb5583d1..f780441b65 100644 --- a/phpBB/config/default/container/services_files.yml +++ b/phpBB/config/default/container/services_files.yml @@ -23,3 +23,4 @@ services: - @filesystem - @files.factory - @language + - @request diff --git a/phpBB/phpbb/files/upload.php b/phpBB/phpbb/files/upload.php index e37f90e820..e62c29883a 100644 --- a/phpBB/phpbb/files/upload.php +++ b/phpBB/phpbb/files/upload.php @@ -13,7 +13,10 @@ namespace phpbb\files; +use \phpbb\filesystem\filesystem_interface; use \phpbb\language\language; +use \phpbb\plupload\plupload; +use \phpbb\request\request_interface; /** * File upload class @@ -57,18 +60,23 @@ class upload /** @var \phpbb\language\language Language class */ protected $language; + /** @var \phpbb\request\request_interface Request class */ + protected $request; + /** * Init file upload class. * * @param \phpbb\filesystem\filesystem_interface $filesystem * @param \phpbb\files\factory $factory Files factory * @param \phpbb\language\language $language Language class + * @param \phpbb\request\request_interface $request Request class */ - public function __construct(\phpbb\filesystem\filesystem_interface $filesystem, factory $factory, language $language) + public function __construct(filesystem_interface $filesystem, factory $factory, language $language, request_interface $request) { $this->filesystem = $filesystem; $this->factory = $factory; $this->language = $language; + $this->request = $request; } /** @@ -178,11 +186,9 @@ class upload * @return filespec $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, plupload $plupload = null) { - global $request; - - $upload = $request->file($form_name); + $upload = $this->request->file($form_name); unset($upload['local_mode']); if ($plupload) @@ -264,8 +270,6 @@ class upload */ function local_upload($source_file, $filedata = false) { - global $request; - $upload = array(); $upload['local_mode'] = true; @@ -331,7 +335,7 @@ class upload } $this->common_checks($file); - $request->overwrite('local', $upload, \phpbb\request\request_interface::FILES); + $this->request->overwrite('local', $upload, request_interface::FILES); return $file; } @@ -657,8 +661,7 @@ class upload */ function is_valid($form_name) { - global $request; - $upload = $request->file($form_name); + $upload = $this->request->file($form_name); return (!empty($upload) && $upload['name'] !== 'none'); } diff --git a/tests/functional/fileupload_remote_test.php b/tests/functional/fileupload_remote_test.php index ae4de26df4..4754153cbf 100644 --- a/tests/functional/fileupload_remote_test.php +++ b/tests/functional/fileupload_remote_test.php @@ -25,6 +25,9 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case /** @var \phpbb\language\language */ protected $language; + /** @var \phpbb\request\request_interface */ + protected $request; + public function setUp() { parent::setUp(); @@ -44,6 +47,7 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case $this->filesystem = new \phpbb\filesystem\filesystem(); $this->language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)); + $this->request = $this->getMock('\phpbb\request\request'); $container = new phpbb_mock_container_builder(); $container->set('files.filespec', new \phpbb\files\filespec($this->filesystem, $this->language)); @@ -61,7 +65,7 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case public function test_invalid_extension() { /** @var \phpbb\files\upload $upload */ - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request); $upload->set_error_prefix('') ->set_allowed_extensions(array('jpg')) ->set_max_filesize(100); @@ -72,7 +76,7 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case public function test_empty_file() { /** @var \phpbb\files\upload $upload */ - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request); $upload->set_error_prefix('') ->set_allowed_extensions(array('jpg')) ->set_max_filesize(100); @@ -83,7 +87,7 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case public function test_successful_upload() { /** @var \phpbb\files\upload $upload */ - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request); $upload->set_error_prefix('') ->set_allowed_extensions(array('gif')) ->set_max_filesize(1000); @@ -95,7 +99,7 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case public function test_too_large() { /** @var \phpbb\files\upload $upload */ - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request); $upload->set_error_prefix('') ->set_allowed_extensions(array('gif')) ->set_max_filesize(100); diff --git a/tests/upload/fileupload_test.php b/tests/upload/fileupload_test.php index 56e29a3ac2..deb4404585 100644 --- a/tests/upload/fileupload_test.php +++ b/tests/upload/fileupload_test.php @@ -30,11 +30,13 @@ class phpbb_fileupload_test extends phpbb_test_case /** @var \phpbb\language\language */ protected $language; + /** @var \phpbb\request\request_interface */ + protected $request; + protected function setUp() { // Global $config required by unique_id - // Global $request required by form_upload, local_upload and is_valid - global $config, $request, $phpbb_root_path, $phpEx; + global $config, $phpbb_root_path, $phpEx; if (!is_array($config)) { @@ -44,7 +46,7 @@ class phpbb_fileupload_test extends phpbb_test_case $config['rand_seed'] = ''; $config['rand_seed_last_update'] = time() + 600; - $request = new phpbb_mock_request(); + $this->request = $this->getMock('\phpbb\request\request'); $this->filesystem = new \phpbb\filesystem\filesystem(); $this->language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)); @@ -83,7 +85,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_common_checks_invalid_extension() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request); $upload->set_allowed_extensions(array('png')) ->set_max_filesize(100); $file = $this->gen_valid_filespec(); @@ -93,7 +95,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_common_checks_invalid_filename() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request); $upload->set_allowed_extensions(array('jpg')) ->set_max_filesize(100); $file = $this->gen_valid_filespec(); @@ -104,7 +106,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_common_checks_too_large() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request); $upload->set_allowed_extensions(array('jpg')) ->set_max_filesize(100); $file = $this->gen_valid_filespec(); @@ -115,7 +117,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_common_checks_valid_file() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request); $upload->set_allowed_extensions(array('jpg')) ->set_max_filesize(1000); $file = $this->gen_valid_filespec(); @@ -125,7 +127,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_local_upload() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request); $upload->set_allowed_extensions(array('jpg')) ->set_max_filesize(1000); @@ -137,7 +139,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_move_existent_file() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request); $upload->set_allowed_extensions(array('jpg')) ->set_max_filesize(1000); @@ -151,7 +153,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_move_existent_file_overwrite() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request); $upload->set_allowed_extensions(array('jpg')) ->set_max_filesize(1000); @@ -166,7 +168,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_valid_dimensions() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request); $upload->set_allowed_extensions(false) ->set_max_filesize(false) ->set_allowed_dimensions(1, 1, 100, 100); From 52652ca1824e91ecfe7549167aebd92c314af678 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Tue, 2 Jun 2015 15:29:32 +0200 Subject: [PATCH 27/66] [ticket/13904] Remove phpbb_root_path global from upload class PHPBB3-13904 --- phpBB/phpbb/files/upload.php | 11 +++++++---- tests/functional/fileupload_remote_test.php | 12 ++++++++---- tests/upload/fileupload_test.php | 20 ++++++++++++-------- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/phpBB/phpbb/files/upload.php b/phpBB/phpbb/files/upload.php index e62c29883a..38aad5a3bd 100644 --- a/phpBB/phpbb/files/upload.php +++ b/phpBB/phpbb/files/upload.php @@ -63,6 +63,9 @@ class upload /** @var \phpbb\request\request_interface Request class */ protected $request; + /** @var string phpBB root path */ + protected $phpbb_root_path; + /** * Init file upload class. * @@ -70,13 +73,15 @@ class upload * @param \phpbb\files\factory $factory Files factory * @param \phpbb\language\language $language Language class * @param \phpbb\request\request_interface $request Request class + * @param string $phpbb_root_path phpBB root path */ - public function __construct(filesystem_interface $filesystem, factory $factory, language $language, request_interface $request) + public function __construct(filesystem_interface $filesystem, factory $factory, language $language, request_interface $request, $phpbb_root_path) { $this->filesystem = $filesystem; $this->factory = $factory; $this->language = $language; $this->request = $request; + $this->phpbb_root_path = $phpbb_root_path; } /** @@ -350,8 +355,6 @@ class upload */ function remote_upload($upload_url) { - global $phpbb_root_path; - $upload_ary = array(); $upload_ary['local_mode'] = true; @@ -504,7 +507,7 @@ class upload return $this->factory->get('filespec')->set_error($this->error_prefix . 'EMPTY_REMOTE_DATA'); } - $tmp_path = (!@ini_get('safe_mode') || strtolower(@ini_get('safe_mode')) == 'off') ? false : $phpbb_root_path . 'cache'; + $tmp_path = (!@ini_get('safe_mode') || strtolower(@ini_get('safe_mode')) == 'off') ? false : $this->phpbb_root_path . 'cache'; $filename = tempnam($tmp_path, unique_id() . '-'); if (!($fp = @fopen($filename, 'wb'))) diff --git a/tests/functional/fileupload_remote_test.php b/tests/functional/fileupload_remote_test.php index 4754153cbf..a32c339afa 100644 --- a/tests/functional/fileupload_remote_test.php +++ b/tests/functional/fileupload_remote_test.php @@ -28,6 +28,9 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case /** @var \phpbb\request\request_interface */ protected $request; + /** @var string phpBB root path */ + protected $phpbb_root_path; + public function setUp() { parent::setUp(); @@ -53,6 +56,7 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case $container->set('files.filespec', new \phpbb\files\filespec($this->filesystem, $this->language)); $this->factory = new \phpbb\files\factory($container); $container->set('files.factory', $this->factory); + $this->phpbb_root_path = $phpbb_root_path; } public function tearDown() @@ -65,7 +69,7 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case public function test_invalid_extension() { /** @var \phpbb\files\upload $upload */ - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request, $this->phpbb_root_path); $upload->set_error_prefix('') ->set_allowed_extensions(array('jpg')) ->set_max_filesize(100); @@ -76,7 +80,7 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case public function test_empty_file() { /** @var \phpbb\files\upload $upload */ - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request, $this->phpbb_root_path); $upload->set_error_prefix('') ->set_allowed_extensions(array('jpg')) ->set_max_filesize(100); @@ -87,7 +91,7 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case public function test_successful_upload() { /** @var \phpbb\files\upload $upload */ - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request, $this->phpbb_root_path); $upload->set_error_prefix('') ->set_allowed_extensions(array('gif')) ->set_max_filesize(1000); @@ -99,7 +103,7 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case public function test_too_large() { /** @var \phpbb\files\upload $upload */ - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request, $this->phpbb_root_path); $upload->set_error_prefix('') ->set_allowed_extensions(array('gif')) ->set_max_filesize(100); diff --git a/tests/upload/fileupload_test.php b/tests/upload/fileupload_test.php index deb4404585..0832f269cb 100644 --- a/tests/upload/fileupload_test.php +++ b/tests/upload/fileupload_test.php @@ -33,6 +33,9 @@ class phpbb_fileupload_test extends phpbb_test_case /** @var \phpbb\request\request_interface */ protected $request; + /** @var string phpBB root path */ + protected $phpbb_root_path; + protected function setUp() { // Global $config required by unique_id @@ -61,6 +64,7 @@ class phpbb_fileupload_test extends phpbb_test_case $this->factory = new \phpbb\files\factory($this->container); $this->path = __DIR__ . '/fixture/'; + $this->phpbb_root_path = $phpbb_root_path; } private function gen_valid_filespec() @@ -85,7 +89,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_common_checks_invalid_extension() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request, $this->phpbb_root_path); $upload->set_allowed_extensions(array('png')) ->set_max_filesize(100); $file = $this->gen_valid_filespec(); @@ -95,7 +99,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_common_checks_invalid_filename() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request, $this->phpbb_root_path); $upload->set_allowed_extensions(array('jpg')) ->set_max_filesize(100); $file = $this->gen_valid_filespec(); @@ -106,7 +110,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_common_checks_too_large() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request, $this->phpbb_root_path); $upload->set_allowed_extensions(array('jpg')) ->set_max_filesize(100); $file = $this->gen_valid_filespec(); @@ -117,7 +121,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_common_checks_valid_file() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request, $this->phpbb_root_path); $upload->set_allowed_extensions(array('jpg')) ->set_max_filesize(1000); $file = $this->gen_valid_filespec(); @@ -127,7 +131,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_local_upload() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request, $this->phpbb_root_path); $upload->set_allowed_extensions(array('jpg')) ->set_max_filesize(1000); @@ -139,7 +143,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_move_existent_file() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request, $this->phpbb_root_path); $upload->set_allowed_extensions(array('jpg')) ->set_max_filesize(1000); @@ -153,7 +157,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_move_existent_file_overwrite() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request, $this->phpbb_root_path); $upload->set_allowed_extensions(array('jpg')) ->set_max_filesize(1000); @@ -168,7 +172,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_valid_dimensions() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request, $this->phpbb_root_path); $upload->set_allowed_extensions(false) ->set_max_filesize(false) ->set_allowed_dimensions(1, 1, 100, 100); From b871dbcf1f2d0483cbe19cddf94a5bdc9659ab00 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Tue, 2 Jun 2015 15:46:41 +0200 Subject: [PATCH 28/66] [ticket/13904] Remove phpbb_root_path global from filespec class PHPBB3-13904 --- .../default/container/services_files.yml | 2 ++ phpBB/phpbb/files/filespec.php | 19 +++++++++++-------- tests/functional/fileupload_remote_test.php | 2 +- tests/upload/filespec_test.php | 11 +++++++---- tests/upload/fileupload_test.php | 1 + 5 files changed, 22 insertions(+), 13 deletions(-) diff --git a/phpBB/config/default/container/services_files.yml b/phpBB/config/default/container/services_files.yml index f780441b65..d30037a4e5 100644 --- a/phpBB/config/default/container/services_files.yml +++ b/phpBB/config/default/container/services_files.yml @@ -13,6 +13,7 @@ services: arguments: - @filesystem - @language + - %core.root_path% - @mimetype.guesser - @plupload @@ -24,3 +25,4 @@ services: - @files.factory - @language - @request + - %core.root_path% diff --git a/phpBB/phpbb/files/filespec.php b/phpBB/phpbb/files/filespec.php index 5e685615d7..736610f6c2 100644 --- a/phpBB/phpbb/files/filespec.php +++ b/phpBB/phpbb/files/filespec.php @@ -86,20 +86,25 @@ class filespec /** @var \phpbb\language\language Language class */ protected $language; + /** @var string phpBB root path */ + protected $phpbb_root_path; + /** * File upload class * - * @param \phpbb\filesystem\filesystem_interface $phpbb_filesystem - * @param \phpbb\language\language $language - * @param \phpbb\mimetype\guesser $mimetype_guesser - * @param \phpbb\plupload\plupload $plupload + * @param \phpbb\filesystem\filesystem_interface $phpbb_filesystem Filesystem + * @param \phpbb\language\language $language Language + * @param string $phpbb_root_path phpBB root path + * @param \phpbb\mimetype\guesser $mimetype_guesser Mime type guesser + * @param \phpbb\plupload\plupload $plupload Plupload */ - function __construct(\phpbb\filesystem\filesystem_interface $phpbb_filesystem, language $language, \phpbb\mimetype\guesser $mimetype_guesser = null, \phpbb\plupload\plupload $plupload = null) + function __construct(\phpbb\filesystem\filesystem_interface $phpbb_filesystem, language $language, $phpbb_root_path, \phpbb\mimetype\guesser $mimetype_guesser = null, \phpbb\plupload\plupload $plupload = null) { $this->plupload = $plupload; $this->mimetype_guesser = $mimetype_guesser; $this->filesystem = $phpbb_filesystem; $this->language = $language; + $this->phpbb_root_path = $phpbb_root_path; } /** @@ -390,8 +395,6 @@ class filespec */ function move_file($destination, $overwrite = false, $skip_image_check = false, $chmod = false) { - global $phpbb_root_path; - if (sizeof($this->error)) { return false; @@ -400,7 +403,7 @@ class filespec $chmod = ($chmod === false) ? CHMOD_READ | CHMOD_WRITE : $chmod; // We need to trust the admin in specifying valid upload directories and an attacker not being able to overwrite it... - $this->destination_path = $phpbb_root_path . $destination; + $this->destination_path = $this->phpbb_root_path . $destination; // Check if the destination path exist... if (!file_exists($this->destination_path)) diff --git a/tests/functional/fileupload_remote_test.php b/tests/functional/fileupload_remote_test.php index a32c339afa..f69ba9f122 100644 --- a/tests/functional/fileupload_remote_test.php +++ b/tests/functional/fileupload_remote_test.php @@ -53,7 +53,7 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case $this->request = $this->getMock('\phpbb\request\request'); $container = new phpbb_mock_container_builder(); - $container->set('files.filespec', new \phpbb\files\filespec($this->filesystem, $this->language)); + $container->set('files.filespec', new \phpbb\files\filespec($this->filesystem, $this->language, $this->phpbb_root_path)); $this->factory = new \phpbb\files\factory($container); $container->set('files.factory', $this->factory); $this->phpbb_root_path = $phpbb_root_path; diff --git a/tests/upload/filespec_test.php b/tests/upload/filespec_test.php index b28adc3f28..b0df72a6ff 100644 --- a/tests/upload/filespec_test.php +++ b/tests/upload/filespec_test.php @@ -28,6 +28,9 @@ class phpbb_filespec_test extends phpbb_test_case /** @var \phpbb\language\language */ protected $language; + /** @var string phpBB root path */ + protected $phpbb_root_path; + protected function setUp() { // Global $config required by unique_id @@ -76,6 +79,7 @@ class phpbb_filespec_test extends phpbb_test_case $this->language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)); $this->filesystem = new \phpbb\filesystem\filesystem(); + $this->phpbb_root_path = $phpbb_root_path; } private function get_filespec($override = array()) @@ -89,7 +93,7 @@ class phpbb_filespec_test extends phpbb_test_case 'error' => '', ); - $filespec = new \phpbb\files\filespec($this->filesystem, $this->language, $this->mimetype_guesser); + $filespec = new \phpbb\files\filespec($this->filesystem, $this->language, $this->phpbb_root_path, $this->mimetype_guesser); return $filespec->set_upload_ary(array_merge($upload_ary, $override)); } @@ -297,8 +301,7 @@ class phpbb_filespec_test extends phpbb_test_case { // Global $phpbb_root_path and $phpEx are required by phpbb_chmod global $phpbb_root_path, $phpEx; - $phpbb_root_path = ''; - $phpEx = 'php'; + $this->phpbb_root_path = ''; $upload = new phpbb_mock_fileupload(); $upload->max_filesize = self::UPLOAD_MAX_FILESIZE; @@ -319,7 +322,7 @@ class phpbb_filespec_test extends phpbb_test_case $this->assertEquals($error, $filespec->error[0]); } - $phpEx = ''; + $this->phpbb_root_path = $phpbb_root_path; } /** diff --git a/tests/upload/fileupload_test.php b/tests/upload/fileupload_test.php index 0832f269cb..c62e9a1947 100644 --- a/tests/upload/fileupload_test.php +++ b/tests/upload/fileupload_test.php @@ -58,6 +58,7 @@ class phpbb_fileupload_test extends phpbb_test_case $this->container->set('files.filespec', new \phpbb\files\filespec( $this->filesystem, $this->language, + $phpbb_root_path, new \phpbb\mimetype\guesser(array( 'mimetype.extension_guesser' => new \phpbb\mimetype\extension_guesser(), )))); From c65f0d748ae3cef90e08ce2c700e65734392da45 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Tue, 2 Jun 2015 15:58:57 +0200 Subject: [PATCH 29/66] [ticket/13904] Add more tests for upload class PHPBB3-13904 --- tests/files/upload_test.php | 96 +++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 tests/files/upload_test.php diff --git a/tests/files/upload_test.php b/tests/files/upload_test.php new file mode 100644 index 0000000000..4323f6cc92 --- /dev/null +++ b/tests/files/upload_test.php @@ -0,0 +1,96 @@ + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +class phpbb_files_upload_test extends phpbb_test_case +{ + private $path; + + private $filesystem; + + /** @var \Symfony\Component\DependencyInjection\ContainerInterface */ + protected $container; + + /** @var \phpbb\files\factory */ + protected $factory; + + /** @var \phpbb\language\language */ + protected $language; + + /** @var \phpbb\request\request_interface */ + protected $request; + + /** @var string phpBB root path */ + protected $phpbb_root_path; + + protected function setUp() + { + // Global $config required by unique_id + global $config, $phpbb_root_path, $phpEx; + + if (!is_array($config)) + { + $config = array(); + } + + $config['rand_seed'] = ''; + $config['rand_seed_last_update'] = time() + 600; + + $this->request = $this->getMock('\phpbb\request\request'); + + $this->filesystem = new \phpbb\filesystem\filesystem(); + $this->language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)); + + $this->container = new phpbb_mock_container_builder($phpbb_root_path, $phpEx); + $this->container->set('files.filespec', new \phpbb\files\filespec( + $this->filesystem, + $this->language, + $phpbb_root_path, + new \phpbb\mimetype\guesser(array( + 'mimetype.extension_guesser' => new \phpbb\mimetype\extension_guesser(), + )))); + $this->factory = new \phpbb\files\factory($this->container); + + $this->path = __DIR__ . '/fixture/'; + $this->phpbb_root_path = $phpbb_root_path; + } + + public function test_reset_vars() + { + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request, $this->phpbb_root_path); + $upload->set_max_filesize(500); + $this->assertEquals(500, $upload->max_filesize); + $upload->reset_vars(); + $this->assertEquals(0, $upload->max_filesize); + } + + public function test_set_disallowed_content() + { + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request, $this->phpbb_root_path); + $upload->set_disallowed_content(array('foo')); + $this->assertEquals(array('foo'), $upload->disallowed_content); + $upload->set_disallowed_content(array('foo', 'bar', 'meh')); + $this->assertEquals(array('foo', 'bar', 'meh'), $upload->disallowed_content); + $upload->set_disallowed_content(''); + $this->assertEquals(array('foo', 'bar', 'meh'), $upload->disallowed_content); + $this->assertINstanceOf('\phpbb\files\upload', $upload->set_disallowed_content(array())); + $this->assertEquals(array(), $upload->disallowed_content); + $upload->reset_vars(); + $this->assertEquals(array(), $upload->disallowed_content); + } + + public function test_is_valid() + { + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request, $this->phpbb_root_path); + $this->assertFalse($upload->is_valid('foobar')); + } +} From 11b2c938c6c3a6a14465f04ed356fbd013276143 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Tue, 14 Jul 2015 16:15:39 +0200 Subject: [PATCH 30/66] [ticket/13904] Move form_upload to its own class and define type classes PHPBB3-13904 --- .../default/container/services_files.yml | 7 + phpBB/includes/functions_posting.php | 2 +- phpBB/phpbb/files/factory.php | 2 +- phpBB/phpbb/files/types/form.php | 151 ++++++++++++++++++ phpBB/phpbb/files/types/type_interface.php | 38 +++++ phpBB/phpbb/files/upload.php | 90 ++--------- 6 files changed, 210 insertions(+), 80 deletions(-) create mode 100644 phpBB/phpbb/files/types/form.php create mode 100644 phpBB/phpbb/files/types/type_interface.php diff --git a/phpBB/config/default/container/services_files.yml b/phpBB/config/default/container/services_files.yml index d30037a4e5..d884b76a34 100644 --- a/phpBB/config/default/container/services_files.yml +++ b/phpBB/config/default/container/services_files.yml @@ -26,3 +26,10 @@ services: - @language - @request - %core.root_path% + + files.types.form: + class: phpbb\files\types\form + arguments: + - @files.factory + - @plupload + - @request diff --git a/phpBB/includes/functions_posting.php b/phpBB/includes/functions_posting.php index f625555d49..4627ff0a9c 100644 --- a/phpBB/includes/functions_posting.php +++ b/phpBB/includes/functions_posting.php @@ -433,7 +433,7 @@ function upload_attachment($form_name, $forum_id, $local = false, $local_storage $upload->set_allowed_extensions(array_keys($extensions['_allowed_'])); /** @var \phpbb\files\filespec $file */ - $file = ($local) ? $upload->local_upload($local_storage, $local_filedata) : $upload->form_upload($form_name, $plupload); + $file = ($local) ? $upload->local_upload($local_storage, $local_filedata) : $upload->handle_upload('form', $form_name); if ($file->init_error()) { diff --git a/phpBB/phpbb/files/factory.php b/phpBB/phpbb/files/factory.php index b28a7ab659..508c50c6ce 100644 --- a/phpBB/phpbb/files/factory.php +++ b/phpBB/phpbb/files/factory.php @@ -42,7 +42,7 @@ class factory { $service = false; - $name = (strpos($name, 'files.') === false && strpos($name, '.') === false) ? 'files.' . $name : $name; + $name = (strpos($name, 'files.') === false) ? 'files.' . $name : $name; try { diff --git a/phpBB/phpbb/files/types/form.php b/phpBB/phpbb/files/types/form.php new file mode 100644 index 0000000000..5c5c332906 --- /dev/null +++ b/phpBB/phpbb/files/types/form.php @@ -0,0 +1,151 @@ + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +namespace phpbb\files\types; + +use \phpbb\files\factory; +use \phpbb\files\filespec; +use \phpbb\files\upload; +use \phpbb\plupload\plupload; +use \phpbb\request\request_interface; + +class form implements type_interface +{ + /** @var factory Files factory */ + protected $factory; + + /** @var plupload */ + protected $plupload; + + /** @var request_interface */ + protected $request; + + /** @var upload */ + protected $upload; + + /** + * Construct a form upload type + * + * @param factory $factory + * @param request_interface $request + */ + public function __construct(factory $factory, plupload $plupload, request_interface $request) + { + $this->factory = $factory; + $this->plupload = $plupload; + $this->request = $request; + } + + /** + * {@inheritdoc} + */ + public function upload() + { + $args = func_get_args(); + return $this->form_upload($args[0], $args[1]); + } + + /** + * {@inheritdoc} + */ + public function set_upload(upload $upload) + { + $this->upload = $upload; + + return $this; + } + + /** + * Form upload method + * Upload file from users harddisk + * + * @param string $form_name Form name assigned to the file input field (if it is an array, the key has to be specified) + * @param plupload $plupload The plupload object + * + * @return filespec $file Object "filespec" is returned, all further operations can be done with this object + * @access public + */ + protected function form_upload($form_name, plupload $plupload = null) + { + $upload = $this->request->file($form_name); + unset($upload['local_mode']); + + if ($plupload) + { + $result = $plupload->handle_upload($form_name); + if (is_array($result)) + { + $upload = array_merge($upload, $result); + } + } + + /** @var filespec $file */ + $file = $this->factory->get('filespec') + ->set_upload_ary($upload) + ->set_upload_namespace($this->upload); + + if ($file->init_error()) + { + $file->error[] = ''; + return $file; + } + + // Error array filled? + if (isset($upload['error'])) + { + $error = $this->upload->assign_internal_error($upload['error']); + + if ($error !== false) + { + $file->error[] = $error; + return $file; + } + } + + // Check if empty file got uploaded (not catched by is_uploaded_file) + if (isset($upload['size']) && $upload['size'] == 0) + { + $file->error[] = $this->language->lang($this->error_prefix . 'EMPTY_FILEUPLOAD'); + return $file; + } + + // PHP Upload filesize exceeded + if ($file->get('filename') == 'none') + { + $max_filesize = @ini_get('upload_max_filesize'); + $unit = 'MB'; + + if (!empty($max_filesize)) + { + $unit = strtolower(substr($max_filesize, -1, 1)); + $max_filesize = (int) $max_filesize; + + $unit = ($unit == 'k') ? 'KB' : (($unit == 'g') ? 'GB' : 'MB'); + } + + $file->error[] = (empty($max_filesize)) ? $this->language->lang($this->error_prefix . 'PHP_SIZE_NA') : $this->language->lang($this->error_prefix . 'PHP_SIZE_OVERRUN', $max_filesize, $this->language->lang($unit)); + return $file; + } + + // Not correctly uploaded + if (!$file->is_uploaded()) + { + $file->error[] = $this->language->lang($this->error_prefix . 'NOT_UPLOADED'); + return $file; + } + + $this->upload->common_checks($file); + + return $file; + } +} diff --git a/phpBB/phpbb/files/types/type_interface.php b/phpBB/phpbb/files/types/type_interface.php new file mode 100644 index 0000000000..adfbb75293 --- /dev/null +++ b/phpBB/phpbb/files/types/type_interface.php @@ -0,0 +1,38 @@ + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +namespace phpbb\files\types; + +use \phpbb\files\upload; + +interface type_interface +{ + /** + * Handle upload for upload types. Arguments passed to this method will be + * handled by the upload type classes themselves. + * + * @return \phpbb\files\filespec|bool Filespec instance if upload is + * successful or false if not + */ + public function upload(); + + /** + * Set upload instance + * Needs to be executed before every upload. + * + * @param upload $upload Upload instance + * + * @return type_interface Returns itself + */ + public function set_upload(upload $upload); +} diff --git a/phpBB/phpbb/files/upload.php b/phpBB/phpbb/files/upload.php index 38aad5a3bd..09f2b9408d 100644 --- a/phpBB/phpbb/files/upload.php +++ b/phpBB/phpbb/files/upload.php @@ -182,87 +182,21 @@ class upload } /** - * Form upload method - * Upload file from users harddisk + * Handle upload based on type * - * @param string $form_name Form name assigned to the file input field (if it is an array, the key has to be specified) - * @param \phpbb\plupload\plupload $plupload The plupload object + * @param string $type Upload type * - * @return filespec $file Object "filespec" is returned, all further operations can be done with this object - * @access public + * @return \phpbb\files\filespec|bool A filespec instance if upload was + * successful, false if there were issues or the type is not supported */ - function form_upload($form_name, plupload $plupload = null) + public function handle_upload($type) { - $upload = $this->request->file($form_name); - unset($upload['local_mode']); + $args = func_get_args(); + array_shift($args); + $type_class = $this->factory->get('types.' . $type) + ->set_upload($this); - if ($plupload) - { - $result = $plupload->handle_upload($form_name); - if (is_array($result)) - { - $upload = array_merge($upload, $result); - } - } - - /** @var filespec $file */ - $file = $this->factory->get('filespec') - ->set_upload_ary($upload) - ->set_upload_namespace($this); - - if ($file->init_error()) - { - $file->error[] = ''; - return $file; - } - - // Error array filled? - if (isset($upload['error'])) - { - $error = $this->assign_internal_error($upload['error']); - - if ($error !== false) - { - $file->error[] = $error; - return $file; - } - } - - // Check if empty file got uploaded (not catched by is_uploaded_file) - if (isset($upload['size']) && $upload['size'] == 0) - { - $file->error[] = $this->language->lang($this->error_prefix . 'EMPTY_FILEUPLOAD'); - return $file; - } - - // PHP Upload filesize exceeded - if ($file->get('filename') == 'none') - { - $max_filesize = @ini_get('upload_max_filesize'); - $unit = 'MB'; - - if (!empty($max_filesize)) - { - $unit = strtolower(substr($max_filesize, -1, 1)); - $max_filesize = (int) $max_filesize; - - $unit = ($unit == 'k') ? 'KB' : (($unit == 'g') ? 'GB' : 'MB'); - } - - $file->error[] = (empty($max_filesize)) ? $this->language->lang($this->error_prefix . 'PHP_SIZE_NA') : $this->language->lang($this->error_prefix . 'PHP_SIZE_OVERRUN', $max_filesize, $this->language->lang($unit)); - return $file; - } - - // Not correctly uploaded - if (!$file->is_uploaded()) - { - $file->error[] = $this->language->lang($this->error_prefix . 'NOT_UPLOADED'); - return $file; - } - - $this->common_checks($file); - - return $file; + return (is_object($type_class)) ? call_user_func_array(array($type_class, 'upload'), $args) : false; } /** @@ -536,9 +470,9 @@ class upload * @param string $errorcode Error code to assign * * @return string Error string - * @access private + * @access public */ - function assign_internal_error($errorcode) + public function assign_internal_error($errorcode) { switch ($errorcode) { From f9b69e73d4b754c7c5bab52d16b2df98812e5570 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Wed, 15 Jul 2015 13:35:38 +0200 Subject: [PATCH 31/66] [ticket/13904] Fix minor coding issues and don't use form_upload anymore PHPBB3-13904 --- phpBB/phpbb/avatar/driver/upload.php | 2 +- phpBB/phpbb/files/types/form.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/phpBB/phpbb/avatar/driver/upload.php b/phpBB/phpbb/avatar/driver/upload.php index c1ce18cb12..ab5325f88a 100644 --- a/phpBB/phpbb/avatar/driver/upload.php +++ b/phpBB/phpbb/avatar/driver/upload.php @@ -116,7 +116,7 @@ class upload extends \phpbb\avatar\driver\driver if (!empty($upload_file['name'])) { - $file = $upload->form_upload('avatar_upload_file'); + $file = $upload->handle_upload('form', 'avatar_upload_file'); } else if (!empty($this->config['allow_avatar_remote_upload']) && !empty($url)) { diff --git a/phpBB/phpbb/files/types/form.php b/phpBB/phpbb/files/types/form.php index 5c5c332906..d82df46fac 100644 --- a/phpBB/phpbb/files/types/form.php +++ b/phpBB/phpbb/files/types/form.php @@ -52,7 +52,7 @@ class form implements type_interface public function upload() { $args = func_get_args(); - return $this->form_upload($args[0], $args[1]); + return $this->form_upload($args[0], (isset($args[1])) ? $args[1] : null); } /** From cf9b6ed4742f95b75125963935f4f2d9e9cfa62c Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Wed, 15 Jul 2015 13:57:19 +0200 Subject: [PATCH 32/66] [ticket/13904] Use the class member plupload instead of argument PHPBB3-13904 --- phpBB/phpbb/files/types/form.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/phpBB/phpbb/files/types/form.php b/phpBB/phpbb/files/types/form.php index d82df46fac..130a64445b 100644 --- a/phpBB/phpbb/files/types/form.php +++ b/phpBB/phpbb/files/types/form.php @@ -52,7 +52,7 @@ class form implements type_interface public function upload() { $args = func_get_args(); - return $this->form_upload($args[0], (isset($args[1])) ? $args[1] : null); + return $this->form_upload($args[0]); } /** @@ -70,19 +70,18 @@ class form implements type_interface * Upload file from users harddisk * * @param string $form_name Form name assigned to the file input field (if it is an array, the key has to be specified) - * @param plupload $plupload The plupload object * * @return filespec $file Object "filespec" is returned, all further operations can be done with this object * @access public */ - protected function form_upload($form_name, plupload $plupload = null) + protected function form_upload($form_name) { $upload = $this->request->file($form_name); unset($upload['local_mode']); - if ($plupload) + if ($this->plupload) { - $result = $plupload->handle_upload($form_name); + $result = $this->plupload->handle_upload($form_name); if (is_array($result)) { $upload = array_merge($upload, $result); From adcc901af181b6727dd7af89a3926c9923a58471 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Wed, 15 Jul 2015 16:08:20 +0200 Subject: [PATCH 33/66] [ticket/13904] Fix minor issues and move local_upload to its own class PHPBB3-13904 --- .../default/container/services_files.yml | 10 ++ phpBB/includes/functions_posting.php | 2 +- phpBB/phpbb/files/types/form.php | 39 ++--- phpBB/phpbb/files/types/local.php | 137 ++++++++++++++++++ phpBB/phpbb/files/upload.php | 80 ---------- tests/upload/fileupload_test.php | 29 +++- 6 files changed, 188 insertions(+), 109 deletions(-) create mode 100644 phpBB/phpbb/files/types/local.php diff --git a/phpBB/config/default/container/services_files.yml b/phpBB/config/default/container/services_files.yml index d884b76a34..d043954f93 100644 --- a/phpBB/config/default/container/services_files.yml +++ b/phpBB/config/default/container/services_files.yml @@ -29,7 +29,17 @@ services: files.types.form: class: phpbb\files\types\form + scope: prototype arguments: - @files.factory + - @language - @plupload - @request + + files.types.local: + class: phpbb\files\types\form + scope: prototype + arguments: + - @files.factory + - @language + - @request diff --git a/phpBB/includes/functions_posting.php b/phpBB/includes/functions_posting.php index 4627ff0a9c..378d14d14d 100644 --- a/phpBB/includes/functions_posting.php +++ b/phpBB/includes/functions_posting.php @@ -433,7 +433,7 @@ function upload_attachment($form_name, $forum_id, $local = false, $local_storage $upload->set_allowed_extensions(array_keys($extensions['_allowed_'])); /** @var \phpbb\files\filespec $file */ - $file = ($local) ? $upload->local_upload($local_storage, $local_filedata) : $upload->handle_upload('form', $form_name); + $file = ($local) ? $upload->handle_upload('local', $local_storage, $local_filedata) : $upload->handle_upload('form', $form_name); if ($file->init_error()) { diff --git a/phpBB/phpbb/files/types/form.php b/phpBB/phpbb/files/types/form.php index 130a64445b..98d1c51d5f 100644 --- a/phpBB/phpbb/files/types/form.php +++ b/phpBB/phpbb/files/types/form.php @@ -16,14 +16,18 @@ namespace phpbb\files\types; use \phpbb\files\factory; use \phpbb\files\filespec; use \phpbb\files\upload; +use \phpbb\language\language; use \phpbb\plupload\plupload; use \phpbb\request\request_interface; -class form implements type_interface +class form extends base { /** @var factory Files factory */ protected $factory; + /** @var language */ + protected $language; + /** @var plupload */ protected $plupload; @@ -39,9 +43,10 @@ class form implements type_interface * @param factory $factory * @param request_interface $request */ - public function __construct(factory $factory, plupload $plupload, request_interface $request) + public function __construct(factory $factory, language $language, plupload $plupload, request_interface $request) { $this->factory = $factory; + $this->language = $language; $this->plupload = $plupload; $this->request = $request; } @@ -79,13 +84,10 @@ class form implements type_interface $upload = $this->request->file($form_name); unset($upload['local_mode']); - if ($this->plupload) + $result = $this->plupload->handle_upload($form_name); + if (is_array($result)) { - $result = $this->plupload->handle_upload($form_name); - if (is_array($result)) - { - $upload = array_merge($upload, $result); - } + $upload = array_merge($upload, $result); } /** @var filespec $file */ @@ -114,32 +116,21 @@ class form implements type_interface // Check if empty file got uploaded (not catched by is_uploaded_file) if (isset($upload['size']) && $upload['size'] == 0) { - $file->error[] = $this->language->lang($this->error_prefix . 'EMPTY_FILEUPLOAD'); + $file->error[] = $this->language->lang($this->upload->error_prefix . 'EMPTY_FILEUPLOAD'); return $file; } - // PHP Upload filesize exceeded - if ($file->get('filename') == 'none') + // PHP Upload filesize check + $file = $this->check_upload_size($file); + if (sizeof($file->error)) { - $max_filesize = @ini_get('upload_max_filesize'); - $unit = 'MB'; - - if (!empty($max_filesize)) - { - $unit = strtolower(substr($max_filesize, -1, 1)); - $max_filesize = (int) $max_filesize; - - $unit = ($unit == 'k') ? 'KB' : (($unit == 'g') ? 'GB' : 'MB'); - } - - $file->error[] = (empty($max_filesize)) ? $this->language->lang($this->error_prefix . 'PHP_SIZE_NA') : $this->language->lang($this->error_prefix . 'PHP_SIZE_OVERRUN', $max_filesize, $this->language->lang($unit)); return $file; } // Not correctly uploaded if (!$file->is_uploaded()) { - $file->error[] = $this->language->lang($this->error_prefix . 'NOT_UPLOADED'); + $file->error[] = $this->language->lang($this->upload->error_prefix . 'NOT_UPLOADED'); return $file; } diff --git a/phpBB/phpbb/files/types/local.php b/phpBB/phpbb/files/types/local.php new file mode 100644 index 0000000000..38876d8ab4 --- /dev/null +++ b/phpBB/phpbb/files/types/local.php @@ -0,0 +1,137 @@ + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +namespace phpbb\files\types; + +use \phpbb\files\factory; +use \phpbb\files\filespec; +use \phpbb\files\upload; +use \phpbb\language\language; +use \phpbb\request\request_interface; + +class local extends base +{ + /** @var factory Files factory */ + protected $factory; + + /** @var language */ + protected $language; + + /** @var request_interface */ + protected $request; + + /** @var upload */ + protected $upload; + + /** + * Construct a form upload type + * + * @param factory $factory + * @param request_interface $request + */ + public function __construct(factory $factory, language $language, request_interface $request) + { + $this->factory = $factory; + $this->language = $language; + $this->request = $request; + } + + /** + * {@inheritdoc} + */ + public function upload() + { + $args = func_get_args(); + return $this->local_upload($args[0], isset($args[1]) ? $args[1] : false); + } + + /** + * Move file from another location to phpBB + * + * @param string $source_file Filename of source file + * @param array|bool $filedata Array with filedata or false + * + * @return filespec Object "filespec" is returned, all further operations can be done with this object + */ + protected function local_upload($source_file, $filedata = false) + { + $upload = array(); + + $upload['local_mode'] = true; + $upload['tmp_name'] = $source_file; + + if ($filedata === false) + { + $upload['name'] = utf8_basename($source_file); + $upload['size'] = 0; + } + else + { + $upload['name'] = $filedata['realname']; + $upload['size'] = $filedata['size']; + $upload['type'] = $filedata['type']; + } + + /** @var filespec $file */ + $file = $this->factory->get('filespec') + ->set_upload_ary($upload) + ->set_upload_namespace($this->upload); + + if ($file->init_error()) + { + $file->error[] = ''; + return $file; + } + + if (isset($upload['error'])) + { + $error = $this->upload->assign_internal_error($upload['error']); + + if ($error !== false) + { + $file->error[] = $error; + return $file; + } + } + + // PHP Upload filesize exceeded + if ($file->get('filename') == 'none') + { + $max_filesize = @ini_get('upload_max_filesize'); + $unit = 'MB'; + + if (!empty($max_filesize)) + { + $unit = strtolower(substr($max_filesize, -1, 1)); + $max_filesize = (int) $max_filesize; + + $unit = ($unit == 'k') ? 'KB' : (($unit == 'g') ? 'GB' : 'MB'); + } + + $file->error[] = (empty($max_filesize)) ?$this->language->lang($this->upload->error_prefix . 'PHP_SIZE_NA') : $this->language->lang($this->upload->error_prefix . 'PHP_SIZE_OVERRUN', $max_filesize, $this->language->lang($unit)); + return $file; + } + + // Not correctly uploaded + if (!$file->is_uploaded()) + { + $file->error[] = $this->language->lang($this->upload->error_prefix . 'NOT_UPLOADED'); + return $file; + } + + $this->upload->common_checks($file); + $this->request->overwrite('local', $upload, request_interface::FILES); + + return $file; + } +} diff --git a/phpBB/phpbb/files/upload.php b/phpBB/phpbb/files/upload.php index 09f2b9408d..471c9c378f 100644 --- a/phpBB/phpbb/files/upload.php +++ b/phpBB/phpbb/files/upload.php @@ -199,86 +199,6 @@ class upload return (is_object($type_class)) ? call_user_func_array(array($type_class, 'upload'), $args) : false; } - /** - * Move file from another location to phpBB - * - * @param string $source_file Filename of source file - * @param array|bool $filedata Array with filedata or false - * - * @return filespec Object "filespec" is returned, all further operations can be done with this object - */ - function local_upload($source_file, $filedata = false) - { - $upload = array(); - - $upload['local_mode'] = true; - $upload['tmp_name'] = $source_file; - - if ($filedata === false) - { - $upload['name'] = utf8_basename($source_file); - $upload['size'] = 0; - } - else - { - $upload['name'] = $filedata['realname']; - $upload['size'] = $filedata['size']; - $upload['type'] = $filedata['type']; - } - - /** @var filespec $file */ - $file = $this->factory->get('filespec') - ->set_upload_ary($upload) - ->set_upload_namespace($this); - - if ($file->init_error()) - { - $file->error[] = ''; - return $file; - } - - if (isset($upload['error'])) - { - $error = $this->assign_internal_error($upload['error']); - - if ($error !== false) - { - $file->error[] = $error; - return $file; - } - } - - // PHP Upload filesize exceeded - if ($file->get('filename') == 'none') - { - $max_filesize = @ini_get('upload_max_filesize'); - $unit = 'MB'; - - if (!empty($max_filesize)) - { - $unit = strtolower(substr($max_filesize, -1, 1)); - $max_filesize = (int) $max_filesize; - - $unit = ($unit == 'k') ? 'KB' : (($unit == 'g') ? 'GB' : 'MB'); - } - - $file->error[] = (empty($max_filesize)) ?$this->language->lang($this->error_prefix . 'PHP_SIZE_NA') : $this->language->lang($this->error_prefix . 'PHP_SIZE_OVERRUN', $max_filesize, $this->language->lang($unit)); - return $file; - } - - // Not correctly uploaded - if (!$file->is_uploaded()) - { - $file->error[] = $this->language->lang($this->error_prefix . 'NOT_UPLOADED'); - return $file; - } - - $this->common_checks($file); - $this->request->overwrite('local', $upload, request_interface::FILES); - - return $file; - } - /** * Remote upload method * Uploads file from given url diff --git a/tests/upload/fileupload_test.php b/tests/upload/fileupload_test.php index c62e9a1947..7a84b34473 100644 --- a/tests/upload/fileupload_test.php +++ b/tests/upload/fileupload_test.php @@ -43,7 +43,7 @@ class phpbb_fileupload_test extends phpbb_test_case if (!is_array($config)) { - $config = array(); + $config = new \phpbb\config\config(array()); } $config['rand_seed'] = ''; @@ -53,6 +53,15 @@ class phpbb_fileupload_test extends phpbb_test_case $this->filesystem = new \phpbb\filesystem\filesystem(); $this->language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)); + $guessers = array( + new \Symfony\Component\HttpFoundation\File\MimeType\FileinfoMimeTypeGuesser(), + new \Symfony\Component\HttpFoundation\File\MimeType\FileBinaryMimeTypeGuesser(), + new \phpbb\mimetype\content_guesser(), + new \phpbb\mimetype\extension_guesser(), + ); + $guessers[2]->set_priority(-2); + $guessers[3]->set_priority(-2); + $this->mimetype_guesser = new \phpbb\mimetype\guesser($guessers); $this->container = new phpbb_mock_container_builder($phpbb_root_path, $phpEx); $this->container->set('files.filespec', new \phpbb\files\filespec( @@ -63,6 +72,18 @@ class phpbb_fileupload_test extends phpbb_test_case 'mimetype.extension_guesser' => new \phpbb\mimetype\extension_guesser(), )))); $this->factory = new \phpbb\files\factory($this->container); + $plupload = new \phpbb\plupload\plupload($phpbb_root_path, $config, $this->request, new \phpbb\user($this->language, '\phpbb\datetime'), new \phpbb\php\ini(), $this->mimetype_guesser); + $this->container->set('files.types.form', new \phpbb\files\types\form( + $this->factory, + $this->language, + $plupload, + $this->request + ), phpbb_mock_container_builder::SCOPE_PROTOTYPE); + $this->container->set('files.types.local', new \phpbb\files\types\local( + $this->factory, + $this->language, + $this->request + ), phpbb_mock_container_builder::SCOPE_PROTOTYPE); $this->path = __DIR__ . '/fixture/'; $this->phpbb_root_path = $phpbb_root_path; @@ -137,7 +158,7 @@ class phpbb_fileupload_test extends phpbb_test_case ->set_max_filesize(1000); copy($this->path . 'jpg', $this->path . 'jpg.jpg'); - $file = $upload->local_upload($this->path . 'jpg.jpg'); + $file = $upload->handle_upload('local', $this->path . 'jpg.jpg'); $this->assertEquals(0, sizeof($file->error)); unlink($this->path . 'jpg.jpg'); } @@ -149,7 +170,7 @@ class phpbb_fileupload_test extends phpbb_test_case ->set_max_filesize(1000); copy($this->path . 'jpg', $this->path . 'jpg.jpg'); - $file = $upload->local_upload($this->path . 'jpg.jpg'); + $file = $upload->handle_upload('local', $this->path . 'jpg.jpg'); $this->assertEquals(0, sizeof($file->error)); $this->assertFalse($file->move_file('../tests/upload/fixture')); $this->assertFalse($file->file_moved); @@ -164,7 +185,7 @@ class phpbb_fileupload_test extends phpbb_test_case copy($this->path . 'jpg', $this->path . 'jpg.jpg'); copy($this->path . 'jpg', $this->path . 'copies/jpg.jpg'); - $file = $upload->local_upload($this->path . 'jpg.jpg'); + $file = $upload->handle_upload('local', $this->path . 'jpg.jpg'); $this->assertEquals(0, sizeof($file->error)); $file->move_file('../tests/upload/fixture/copies', true); $this->assertEquals(0, sizeof($file->error)); From 167cc58f27da138e138310dafaeff5b53ccedef0 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Wed, 15 Jul 2015 16:08:49 +0200 Subject: [PATCH 34/66] [ticket/13904] Add types base class PHPBB3-13904 --- phpBB/phpbb/files/types/base.php | 66 ++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 phpBB/phpbb/files/types/base.php diff --git a/phpBB/phpbb/files/types/base.php b/phpBB/phpbb/files/types/base.php new file mode 100644 index 0000000000..686ce6f695 --- /dev/null +++ b/phpBB/phpbb/files/types/base.php @@ -0,0 +1,66 @@ + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +namespace phpbb\files\types; + +use \phpbb\files\filespec; +use \phpbb\files\upload; +use \phpbb\language\language; + +abstract class base implements type_interface +{ + /** @var language */ + protected $language; + + /** @var upload */ + protected $upload; + + /** + * Check if upload exceeds maximum file size + * + * @param filespec $file Filespec object + * + * @return filespec Returns same filespec instance + */ + public function check_upload_size($file) + { + // PHP Upload filesize exceeded + if ($file->get('filename') == 'none') + { + $max_filesize = @ini_get('upload_max_filesize'); + $unit = 'MB'; + + if (!empty($max_filesize)) + { + $unit = strtolower(substr($max_filesize, -1, 1)); + $max_filesize = (int) $max_filesize; + + $unit = ($unit == 'k') ? 'KB' : (($unit == 'g') ? 'GB' : 'MB'); + } + + $file->error[] = (empty($max_filesize)) ? $this->language->lang($this->upload->error_prefix . 'PHP_SIZE_NA') : $this->language->lang($this->upload->error_prefix . 'PHP_SIZE_OVERRUN', $max_filesize, $this->language->lang($unit)); + } + + return $file; + } + + /** + * {@inheritdoc} + */ + public function set_upload(upload $upload) + { + $this->upload = $upload; + + return $this; + } +} From 636a29d589e92765b5746a977b3b53c1bb4e818c Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Wed, 15 Jul 2015 17:12:24 +0200 Subject: [PATCH 35/66] [ticket/13904] Split up local_upload method PHPBB3-13904 --- phpBB/phpbb/files/types/local.php | 47 ++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/phpBB/phpbb/files/types/local.php b/phpBB/phpbb/files/types/local.php index 38876d8ab4..d439f01866 100644 --- a/phpBB/phpbb/files/types/local.php +++ b/phpBB/phpbb/files/types/local.php @@ -65,22 +65,7 @@ class local extends base */ protected function local_upload($source_file, $filedata = false) { - $upload = array(); - - $upload['local_mode'] = true; - $upload['tmp_name'] = $source_file; - - if ($filedata === false) - { - $upload['name'] = utf8_basename($source_file); - $upload['size'] = 0; - } - else - { - $upload['name'] = $filedata['realname']; - $upload['size'] = $filedata['size']; - $upload['type'] = $filedata['type']; - } + $upload = $this->get_upload_ary($source_file, $filedata); /** @var filespec $file */ $file = $this->factory->get('filespec') @@ -134,4 +119,34 @@ class local extends base return $file; } + + /** + * Retrieve upload array + * + * @param string $source_file Source file name + * @param array $filedata File data array + * + * @return array Upload array + */ + protected function get_upload_ary($source_file, $filedata) + { + $upload = array(); + + $upload['local_mode'] = true; + $upload['tmp_name'] = $source_file; + + if ($filedata === false) + { + $upload['name'] = utf8_basename($source_file); + $upload['size'] = 0; + } + else + { + $upload['name'] = $filedata['realname']; + $upload['size'] = $filedata['size']; + $upload['type'] = $filedata['type']; + } + + return $upload; + } } From 5b21830ba81b5512b7c3f945a899da9103c80558 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Wed, 15 Jul 2015 18:00:26 +0200 Subject: [PATCH 36/66] [ticket/13904] Improve docblock PHPBB3-13904 --- phpBB/phpbb/files/filespec.php | 4 ++-- phpBB/phpbb/files/types/local.php | 3 +-- phpBB/phpbb/files/upload.php | 13 ++++++------- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/phpBB/phpbb/files/filespec.php b/phpBB/phpbb/files/filespec.php index 736610f6c2..ed64b7ff5c 100644 --- a/phpBB/phpbb/files/filespec.php +++ b/phpBB/phpbb/files/filespec.php @@ -83,7 +83,7 @@ class filespec */ protected $mimetype_guesser; - /** @var \phpbb\language\language Language class */ + /** @var language Language class */ protected $language; /** @var string phpBB root path */ @@ -93,7 +93,7 @@ class filespec * File upload class * * @param \phpbb\filesystem\filesystem_interface $phpbb_filesystem Filesystem - * @param \phpbb\language\language $language Language + * @param language $language Language * @param string $phpbb_root_path phpBB root path * @param \phpbb\mimetype\guesser $mimetype_guesser Mime type guesser * @param \phpbb\plupload\plupload $plupload Plupload diff --git a/phpBB/phpbb/files/types/local.php b/phpBB/phpbb/files/types/local.php index d439f01866..bb5994841f 100644 --- a/phpBB/phpbb/files/types/local.php +++ b/phpBB/phpbb/files/types/local.php @@ -15,7 +15,6 @@ namespace phpbb\files\types; use \phpbb\files\factory; use \phpbb\files\filespec; -use \phpbb\files\upload; use \phpbb\language\language; use \phpbb\request\request_interface; @@ -30,7 +29,7 @@ class local extends base /** @var request_interface */ protected $request; - /** @var upload */ + /** @var \phpbb\files\upload */ protected $upload; /** diff --git a/phpBB/phpbb/files/upload.php b/phpBB/phpbb/files/upload.php index 471c9c378f..43f06c3503 100644 --- a/phpBB/phpbb/files/upload.php +++ b/phpBB/phpbb/files/upload.php @@ -15,7 +15,6 @@ namespace phpbb\files; use \phpbb\filesystem\filesystem_interface; use \phpbb\language\language; -use \phpbb\plupload\plupload; use \phpbb\request\request_interface; /** @@ -51,7 +50,7 @@ class upload /** @var int Timeout for remote upload */ var $upload_timeout = 6; - /** @var \phpbb\filesystem\filesystem_interface */ + /** @var filesystem_interface */ protected $filesystem; /** @var \phpbb\files\factory Files factory */ @@ -60,7 +59,7 @@ class upload /** @var \phpbb\language\language Language class */ protected $language; - /** @var \phpbb\request\request_interface Request class */ + /** @var request_interface Request class */ protected $request; /** @var string phpBB root path */ @@ -69,10 +68,10 @@ class upload /** * Init file upload class. * - * @param \phpbb\filesystem\filesystem_interface $filesystem - * @param \phpbb\files\factory $factory Files factory - * @param \phpbb\language\language $language Language class - * @param \phpbb\request\request_interface $request Request class + * @param filesystem_interface $filesystem + * @param factory $factory Files factory + * @param language $language Language class + * @param request_interface $request Request class * @param string $phpbb_root_path phpBB root path */ public function __construct(filesystem_interface $filesystem, factory $factory, language $language, request_interface $request, $phpbb_root_path) From 57ccfe0c483254e54b7b40bc1906ef946daf4f55 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Wed, 15 Jul 2015 18:00:52 +0200 Subject: [PATCH 37/66] [ticket/13904] Move remote upload to its own type class PHPBB3-13904 --- .../default/container/services_files.yml | 8 + phpBB/phpbb/avatar/driver/upload.php | 2 +- phpBB/phpbb/files/types/remote.php | 242 ++++++++++++++++++ phpBB/phpbb/files/upload.php | 185 ------------- tests/functional/fileupload_remote_test.php | 8 +- 5 files changed, 255 insertions(+), 190 deletions(-) create mode 100644 phpBB/phpbb/files/types/remote.php diff --git a/phpBB/config/default/container/services_files.yml b/phpBB/config/default/container/services_files.yml index d043954f93..4c353ccfa9 100644 --- a/phpBB/config/default/container/services_files.yml +++ b/phpBB/config/default/container/services_files.yml @@ -43,3 +43,11 @@ services: - @files.factory - @language - @request + + files.types.remote: + class: phpbb\files\types\remote + scope: prototype + arguments: + - @files.factory + - @language + - @request diff --git a/phpBB/phpbb/avatar/driver/upload.php b/phpBB/phpbb/avatar/driver/upload.php index ab5325f88a..2bd23a26a8 100644 --- a/phpBB/phpbb/avatar/driver/upload.php +++ b/phpBB/phpbb/avatar/driver/upload.php @@ -146,7 +146,7 @@ class upload extends \phpbb\avatar\driver\driver return false; } - $file = $upload->remote_upload($url); + $file = $upload->handle_upload('remote', $url); } else { diff --git a/phpBB/phpbb/files/types/remote.php b/phpBB/phpbb/files/types/remote.php new file mode 100644 index 0000000000..c70f1557e7 --- /dev/null +++ b/phpBB/phpbb/files/types/remote.php @@ -0,0 +1,242 @@ + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +namespace phpbb\files\types; + +use \phpbb\files\factory; +use \phpbb\files\filespec; +use \phpbb\files\upload; +use \phpbb\language\language; +use \phpbb\request\request_interface; + +class remote extends base +{ + /** @var factory Files factory */ + protected $factory; + + /** @var language */ + protected $language; + + /** @var request_interface */ + protected $request; + + /** @var upload */ + protected $upload; + + /** + * Construct a form upload type + * + * @param factory $factory + * @param request_interface $request + */ + public function __construct(factory $factory, language $language, request_interface $request) + { + $this->factory = $factory; + $this->language = $language; + $this->request = $request; + } + + /** + * {@inheritdoc} + */ + public function upload() + { + $args = func_get_args(); + return $this->remote_upload($args[0]); + } + + /** + * Remote upload method + * Uploads file from given url + * + * @param string $upload_url URL pointing to file to upload, for example http://www.foobar.com/example.gif + * @return filespec $file Object "filespec" is returned, all further operations can be done with this object + * @access public + */ + protected function remote_upload($upload_url) + { + $upload_ary = array(); + $upload_ary['local_mode'] = true; + + if (!preg_match('#^(https?://).*?\.(' . implode('|', $this->upload->allowed_extensions) . ')$#i', $upload_url, $match)) + { + return $this->factory->get('filespec')->set_error($this->language->lang($this->upload->error_prefix . 'URL_INVALID')); + } + + if (empty($match[2])) + { + return $this->factory->get('filespec')->set_error($this->language->lang($this->upload->error_prefix . 'URL_INVALID')); + } + + $url = parse_url($upload_url); + + $host = $url['host']; + $path = $url['path']; + $port = (!empty($url['port'])) ? (int) $url['port'] : 80; + + $upload_ary['type'] = 'application/octet-stream'; + + $url['path'] = explode('.', $url['path']); + $ext = array_pop($url['path']); + + $url['path'] = implode('', $url['path']); + $upload_ary['name'] = utf8_basename($url['path']) . (($ext) ? '.' . $ext : ''); + $filename = $url['path']; + $filesize = 0; + + $remote_max_filesize = $this->upload->max_filesize; + if (!$remote_max_filesize) + { + $max_filesize = @ini_get('upload_max_filesize'); + + if (!empty($max_filesize)) + { + $unit = strtolower(substr($max_filesize, -1, 1)); + $remote_max_filesize = (int) $max_filesize; + + switch ($unit) + { + case 'g': + $remote_max_filesize *= 1024; + // no break + case 'm': + $remote_max_filesize *= 1024; + // no break + case 'k': + $remote_max_filesize *= 1024; + // no break + } + } + } + + $errno = 0; + $errstr = ''; + + if (!($fsock = @fsockopen($host, $port, $errno, $errstr))) + { + return $this->factory->get('filespec')->set_error($this->language->lang($this->upload->error_prefix . 'NOT_UPLOADED')); + } + + // Make sure $path not beginning with / + if (strpos($path, '/') === 0) + { + $path = substr($path, 1); + } + + fputs($fsock, 'GET /' . $path . " HTTP/1.1\r\n"); + fputs($fsock, "HOST: " . $host . "\r\n"); + fputs($fsock, "Connection: close\r\n\r\n"); + + // Set a proper timeout for the socket + socket_set_timeout($fsock, $this->upload->upload_timeout); + + $get_info = false; + $data = ''; + $length = false; + $timer_stop = time() + $this->upload->upload_timeout; + + while ((!$length || $filesize < $length) && !@feof($fsock)) + { + if ($get_info) + { + if ($length) + { + // Don't attempt to read past end of file if server indicated length + $block = @fread($fsock, min($length - $filesize, 1024)); + } + else + { + $block = @fread($fsock, 1024); + } + + $filesize += strlen($block); + + if ($remote_max_filesize && $filesize > $remote_max_filesize) + { + $max_filesize = get_formatted_filesize($remote_max_filesize, false); + + return $this->factory->get('filespec')->set_error($this->language->lang($this->upload->error_prefix . 'WRONG_FILESIZE', $max_filesize['value'], $max_filesize['unit'])); + } + + $data .= $block; + } + else + { + $line = @fgets($fsock, 1024); + + if ($line == "\r\n") + { + $get_info = true; + } + else + { + if (stripos($line, 'content-type: ') !== false) + { + $upload_ary['type'] = rtrim(str_replace('content-type: ', '', strtolower($line))); + } + else if ($this->upload->max_filesize && stripos($line, 'content-length: ') !== false) + { + $length = (int) str_replace('content-length: ', '', strtolower($line)); + + if ($remote_max_filesize && $length && $length > $remote_max_filesize) + { + $max_filesize = get_formatted_filesize($remote_max_filesize, false); + + return $this->factory->get('filespec')->set_error($this->language->lang($this->upload->error_prefix . 'WRONG_FILESIZE', $max_filesize['value'], $max_filesize['unit'])); + } + } + else if (stripos($line, '404 not found') !== false) + { + return $this->factory->get('filespec')->set_error($this->upload->error_prefix . 'URL_NOT_FOUND'); + } + } + } + + $stream_meta_data = stream_get_meta_data($fsock); + + // Cancel upload if we exceed timeout + if (!empty($stream_meta_data['timed_out']) || time() >= $timer_stop) + { + return $this->factory->get('filespec')->set_error($this->upload->error_prefix . 'REMOTE_UPLOAD_TIMEOUT'); + } + } + @fclose($fsock); + + if (empty($data)) + { + return $this->factory->get('filespec')->set_error($this->upload->error_prefix . 'EMPTY_REMOTE_DATA'); + } + + $tmp_path = (!@ini_get('safe_mode') || strtolower(@ini_get('safe_mode')) == 'off') ? false : $this->phpbb_root_path . 'cache'; + $filename = tempnam($tmp_path, unique_id() . '-'); + + if (!($fp = @fopen($filename, 'wb'))) + { + return $this->factory->get('filespec')->set_error($this->upload->error_prefix . 'NOT_UPLOADED'); + } + + $upload_ary['size'] = fwrite($fp, $data); + fclose($fp); + unset($data); + + $upload_ary['tmp_name'] = $filename; + + /** @var filespec $file */ + $file = $this->factory->get('filespec') + ->set_upload_ary($upload_ary) + ->set_upload_namespace($this); + $this->upload->common_checks($file); + + return $file; + } +} diff --git a/phpBB/phpbb/files/upload.php b/phpBB/phpbb/files/upload.php index 43f06c3503..ceb7e1a741 100644 --- a/phpBB/phpbb/files/upload.php +++ b/phpBB/phpbb/files/upload.php @@ -198,191 +198,6 @@ class upload return (is_object($type_class)) ? call_user_func_array(array($type_class, 'upload'), $args) : false; } - /** - * Remote upload method - * Uploads file from given url - * - * @param string $upload_url URL pointing to file to upload, for example http://www.foobar.com/example.gif - * @return filespec $file Object "filespec" is returned, all further operations can be done with this object - * @access public - */ - function remote_upload($upload_url) - { - $upload_ary = array(); - $upload_ary['local_mode'] = true; - - if (!preg_match('#^(https?://).*?\.(' . implode('|', $this->allowed_extensions) . ')$#i', $upload_url, $match)) - { - return $this->factory->get('filespec')->set_error($this->language->lang($this->error_prefix . 'URL_INVALID')); - } - - if (empty($match[2])) - { - return $this->factory->get('filespec')->set_error($this->language->lang($this->error_prefix . 'URL_INVALID')); - } - - $url = parse_url($upload_url); - - $host = $url['host']; - $path = $url['path']; - $port = (!empty($url['port'])) ? (int) $url['port'] : 80; - - $upload_ary['type'] = 'application/octet-stream'; - - $url['path'] = explode('.', $url['path']); - $ext = array_pop($url['path']); - - $url['path'] = implode('', $url['path']); - $upload_ary['name'] = utf8_basename($url['path']) . (($ext) ? '.' . $ext : ''); - $filename = $url['path']; - $filesize = 0; - - $remote_max_filesize = $this->max_filesize; - if (!$remote_max_filesize) - { - $max_filesize = @ini_get('upload_max_filesize'); - - if (!empty($max_filesize)) - { - $unit = strtolower(substr($max_filesize, -1, 1)); - $remote_max_filesize = (int) $max_filesize; - - switch ($unit) - { - case 'g': - $remote_max_filesize *= 1024; - // no break - case 'm': - $remote_max_filesize *= 1024; - // no break - case 'k': - $remote_max_filesize *= 1024; - // no break - } - } - } - - $errno = 0; - $errstr = ''; - - if (!($fsock = @fsockopen($host, $port, $errno, $errstr))) - { - return $this->factory->get('filespec')->set_error($this->language->lang($this->error_prefix . 'NOT_UPLOADED')); - } - - // Make sure $path not beginning with / - if (strpos($path, '/') === 0) - { - $path = substr($path, 1); - } - - fputs($fsock, 'GET /' . $path . " HTTP/1.1\r\n"); - fputs($fsock, "HOST: " . $host . "\r\n"); - fputs($fsock, "Connection: close\r\n\r\n"); - - // Set a proper timeout for the socket - socket_set_timeout($fsock, $this->upload_timeout); - - $get_info = false; - $data = ''; - $length = false; - $timer_stop = time() + $this->upload_timeout; - - while ((!$length || $filesize < $length) && !@feof($fsock)) - { - if ($get_info) - { - if ($length) - { - // Don't attempt to read past end of file if server indicated length - $block = @fread($fsock, min($length - $filesize, 1024)); - } - else - { - $block = @fread($fsock, 1024); - } - - $filesize += strlen($block); - - if ($remote_max_filesize && $filesize > $remote_max_filesize) - { - $max_filesize = get_formatted_filesize($remote_max_filesize, false); - - return $this->factory->get('filespec')->set_error($this->language->lang($this->error_prefix . 'WRONG_FILESIZE', $max_filesize['value'], $max_filesize['unit'])); - } - - $data .= $block; - } - else - { - $line = @fgets($fsock, 1024); - - if ($line == "\r\n") - { - $get_info = true; - } - else - { - if (stripos($line, 'content-type: ') !== false) - { - $upload_ary['type'] = rtrim(str_replace('content-type: ', '', strtolower($line))); - } - else if ($this->max_filesize && stripos($line, 'content-length: ') !== false) - { - $length = (int) str_replace('content-length: ', '', strtolower($line)); - - if ($remote_max_filesize && $length && $length > $remote_max_filesize) - { - $max_filesize = get_formatted_filesize($remote_max_filesize, false); - - return $this->factory->get('filespec')->set_error($this->language->lang($this->error_prefix . 'WRONG_FILESIZE', $max_filesize['value'], $max_filesize['unit'])); - } - } - else if (stripos($line, '404 not found') !== false) - { - return $this->factory->get('filespec')->set_error($this->error_prefix . 'URL_NOT_FOUND'); - } - } - } - - $stream_meta_data = stream_get_meta_data($fsock); - - // Cancel upload if we exceed timeout - if (!empty($stream_meta_data['timed_out']) || time() >= $timer_stop) - { - return $this->factory->get('filespec')->set_error($this->error_prefix . 'REMOTE_UPLOAD_TIMEOUT'); - } - } - @fclose($fsock); - - if (empty($data)) - { - return $this->factory->get('filespec')->set_error($this->error_prefix . 'EMPTY_REMOTE_DATA'); - } - - $tmp_path = (!@ini_get('safe_mode') || strtolower(@ini_get('safe_mode')) == 'off') ? false : $this->phpbb_root_path . 'cache'; - $filename = tempnam($tmp_path, unique_id() . '-'); - - if (!($fp = @fopen($filename, 'wb'))) - { - return $this->factory->get('filespec')->set_error($this->error_prefix . 'NOT_UPLOADED'); - } - - $upload_ary['size'] = fwrite($fp, $data); - fclose($fp); - unset($data); - - $upload_ary['tmp_name'] = $filename; - - /** @var filespec $file */ - $file = $this->factory->get('filespec') - ->set_upload_ary($upload_ary) - ->set_upload_namespace($this); - $this->common_checks($file); - - return $file; - } - /** * Assign internal error * diff --git a/tests/functional/fileupload_remote_test.php b/tests/functional/fileupload_remote_test.php index f69ba9f122..0f1e4d6e59 100644 --- a/tests/functional/fileupload_remote_test.php +++ b/tests/functional/fileupload_remote_test.php @@ -73,7 +73,7 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case $upload->set_error_prefix('') ->set_allowed_extensions(array('jpg')) ->set_max_filesize(100); - $file = $upload->remote_upload(self::$root_url . 'develop/blank.gif'); + $file = $upload->handle_upload('remote', self::$root_url . 'develop/blank.gif'); $this->assertEquals('URL_INVALID', $file->error[0]); } @@ -84,7 +84,7 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case $upload->set_error_prefix('') ->set_allowed_extensions(array('jpg')) ->set_max_filesize(100); - $file = $upload->remote_upload(self::$root_url . 'develop/blank.jpg'); + $file = $upload->handle_upload('remote', self::$root_url . 'develop/blank.jpg'); $this->assertEquals('EMPTY_REMOTE_DATA', $file->error[0]); } @@ -95,7 +95,7 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case $upload->set_error_prefix('') ->set_allowed_extensions(array('gif')) ->set_max_filesize(1000); - $file = $upload->remote_upload(self::$root_url . 'styles/prosilver/theme/images/forum_read.gif'); + $file = $upload->handle_upload('remote', self::$root_url . 'styles/prosilver/theme/images/forum_read.gif'); $this->assertEquals(0, sizeof($file->error)); $this->assertTrue(file_exists($file->filename)); } @@ -107,7 +107,7 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case $upload->set_error_prefix('') ->set_allowed_extensions(array('gif')) ->set_max_filesize(100); - $file = $upload->remote_upload(self::$root_url . 'styles/prosilver/theme/images/forum_read.gif'); + $file = $upload->handle_upload('remote', self::$root_url . 'styles/prosilver/theme/images/forum_read.gif'); $this->assertEquals(1, sizeof($file->error)); $this->assertEquals('WRONG_FILESIZE', $file->error[0]); } From 759a1a09fae5147afe346729764f050ecc67076b Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Wed, 15 Jul 2015 21:30:42 +0200 Subject: [PATCH 38/66] [ticket/13904] Fix remote upload functional tests PHPBB3-13904 --- phpBB/phpbb/files/types/remote.php | 2 +- tests/functional/fileupload_remote_test.php | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/phpBB/phpbb/files/types/remote.php b/phpBB/phpbb/files/types/remote.php index c70f1557e7..04263f9cba 100644 --- a/phpBB/phpbb/files/types/remote.php +++ b/phpBB/phpbb/files/types/remote.php @@ -234,7 +234,7 @@ class remote extends base /** @var filespec $file */ $file = $this->factory->get('filespec') ->set_upload_ary($upload_ary) - ->set_upload_namespace($this); + ->set_upload_namespace($this->upload); $this->upload->common_checks($file); return $file; diff --git a/tests/functional/fileupload_remote_test.php b/tests/functional/fileupload_remote_test.php index 0f1e4d6e59..1bb63719c6 100644 --- a/tests/functional/fileupload_remote_test.php +++ b/tests/functional/fileupload_remote_test.php @@ -56,6 +56,7 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case $container->set('files.filespec', new \phpbb\files\filespec($this->filesystem, $this->language, $this->phpbb_root_path)); $this->factory = new \phpbb\files\factory($container); $container->set('files.factory', $this->factory); + $container->set('files.types.remote', new \phpbb\files\types\remote($this->factory, $this->language, $this->request)); $this->phpbb_root_path = $phpbb_root_path; } From 0a6f54d522f7799819969fcd588a354b7beb3fa9 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Wed, 15 Jul 2015 22:40:17 +0200 Subject: [PATCH 39/66] [ticket/13904] Modify doc blocks PHPBB3-13904 --- phpBB/phpbb/files/types/base.php | 10 ++++------ phpBB/phpbb/files/types/remote.php | 3 +-- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/phpBB/phpbb/files/types/base.php b/phpBB/phpbb/files/types/base.php index 686ce6f695..77ebdb4f32 100644 --- a/phpBB/phpbb/files/types/base.php +++ b/phpBB/phpbb/files/types/base.php @@ -13,8 +13,6 @@ namespace phpbb\files\types; -use \phpbb\files\filespec; -use \phpbb\files\upload; use \phpbb\language\language; abstract class base implements type_interface @@ -22,15 +20,15 @@ abstract class base implements type_interface /** @var language */ protected $language; - /** @var upload */ + /** @var \phpbb\files\upload */ protected $upload; /** * Check if upload exceeds maximum file size * - * @param filespec $file Filespec object + * @param \phpbb\files\filespec $file Filespec object * - * @return filespec Returns same filespec instance + * @return \phpbb\files\filespec Returns same filespec instance */ public function check_upload_size($file) { @@ -57,7 +55,7 @@ abstract class base implements type_interface /** * {@inheritdoc} */ - public function set_upload(upload $upload) + public function set_upload(\phpbb\files\upload $upload) { $this->upload = $upload; diff --git a/phpBB/phpbb/files/types/remote.php b/phpBB/phpbb/files/types/remote.php index 04263f9cba..f2db6c798c 100644 --- a/phpBB/phpbb/files/types/remote.php +++ b/phpBB/phpbb/files/types/remote.php @@ -15,7 +15,6 @@ namespace phpbb\files\types; use \phpbb\files\factory; use \phpbb\files\filespec; -use \phpbb\files\upload; use \phpbb\language\language; use \phpbb\request\request_interface; @@ -30,7 +29,7 @@ class remote extends base /** @var request_interface */ protected $request; - /** @var upload */ + /** @var \phpbb\files\upload */ protected $upload; /** From a09c6d1fb760151b1a6c654b597b4578c3136be1 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Wed, 15 Jul 2015 23:10:23 +0200 Subject: [PATCH 40/66] [ticket/13904] Split code up and pass root path to remote upload type PHPBB3-13904 --- .../default/container/services_files.yml | 1 + phpBB/phpbb/files/types/remote.php | 66 ++++++++++++------- tests/functional/fileupload_remote_test.php | 2 +- 3 files changed, 43 insertions(+), 26 deletions(-) diff --git a/phpBB/config/default/container/services_files.yml b/phpBB/config/default/container/services_files.yml index 4c353ccfa9..e193dad4ea 100644 --- a/phpBB/config/default/container/services_files.yml +++ b/phpBB/config/default/container/services_files.yml @@ -51,3 +51,4 @@ services: - @files.factory - @language - @request + - %core.root_path% diff --git a/phpBB/phpbb/files/types/remote.php b/phpBB/phpbb/files/types/remote.php index f2db6c798c..65cff8ccc7 100644 --- a/phpBB/phpbb/files/types/remote.php +++ b/phpBB/phpbb/files/types/remote.php @@ -32,17 +32,21 @@ class remote extends base /** @var \phpbb\files\upload */ protected $upload; + /** @var string phpBB root path */ + protected $phpbb_root_path; + /** * Construct a form upload type * * @param factory $factory * @param request_interface $request */ - public function __construct(factory $factory, language $language, request_interface $request) + public function __construct(factory $factory, language $language, request_interface $request, $phpbb_root_path) { $this->factory = $factory; $this->language = $language; $this->request = $request; + $this->phpbb_root_path = $phpbb_root_path; } /** @@ -93,30 +97,7 @@ class remote extends base $filename = $url['path']; $filesize = 0; - $remote_max_filesize = $this->upload->max_filesize; - if (!$remote_max_filesize) - { - $max_filesize = @ini_get('upload_max_filesize'); - - if (!empty($max_filesize)) - { - $unit = strtolower(substr($max_filesize, -1, 1)); - $remote_max_filesize = (int) $max_filesize; - - switch ($unit) - { - case 'g': - $remote_max_filesize *= 1024; - // no break - case 'm': - $remote_max_filesize *= 1024; - // no break - case 'k': - $remote_max_filesize *= 1024; - // no break - } - } - } + $remote_max_filesize = $this->get_max_file_size(); $errno = 0; $errstr = ''; @@ -238,4 +219,39 @@ class remote extends base return $file; } + + /** + * Get maximum file size for remote uploads + * + * @return int Maximum file size + */ + protected function get_max_file_size() + { + $max_file_size = $this->upload->max_filesize; + if (!$max_file_size) + { + $max_file_size = @ini_get('upload_max_filesize'); + + if (!empty($max_filesize)) + { + $unit = strtolower(substr($max_file_size, -1, 1)); + $max_file_size = (int) $max_filesize; + + switch ($unit) + { + case 'g': + $max_file_size *= 1024; + // no break + case 'm': + $max_file_size *= 1024; + // no break + case 'k': + $max_file_size *= 1024; + // no break + } + } + } + + return $max_file_size; + } } diff --git a/tests/functional/fileupload_remote_test.php b/tests/functional/fileupload_remote_test.php index 1bb63719c6..a754d8c91d 100644 --- a/tests/functional/fileupload_remote_test.php +++ b/tests/functional/fileupload_remote_test.php @@ -56,7 +56,7 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case $container->set('files.filespec', new \phpbb\files\filespec($this->filesystem, $this->language, $this->phpbb_root_path)); $this->factory = new \phpbb\files\factory($container); $container->set('files.factory', $this->factory); - $container->set('files.types.remote', new \phpbb\files\types\remote($this->factory, $this->language, $this->request)); + $container->set('files.types.remote', new \phpbb\files\types\remote($this->factory, $this->language, $this->request, $phpbb_root_path)); $this->phpbb_root_path = $phpbb_root_path; } From 845233fc626b0d5e6d9e61039fde8e31b4dd28aa Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Thu, 16 Jul 2015 00:21:23 +0200 Subject: [PATCH 41/66] [ticket/13904] Improve test coverage and use constants instead of magic numbers PHPBB3-13904 --- phpBB/phpbb/files/upload.php | 30 +++++++++++++++++++----------- tests/files/upload_test.php | 23 +++++++++++++++++++++++ tests/upload/fileupload_test.php | 19 +++++++++++++++++++ 3 files changed, 61 insertions(+), 11 deletions(-) diff --git a/phpBB/phpbb/files/upload.php b/phpBB/phpbb/files/upload.php index ceb7e1a741..234eb69735 100644 --- a/phpBB/phpbb/files/upload.php +++ b/phpBB/phpbb/files/upload.php @@ -210,7 +210,7 @@ class upload { switch ($errorcode) { - case 1: + case UPLOAD_ERR_INI_SIZE: $max_filesize = @ini_get('upload_max_filesize'); $unit = 'MB'; @@ -223,29 +223,37 @@ class upload } $error = (empty($max_filesize)) ? $this->language->lang($this->error_prefix . 'PHP_SIZE_NA') : $this->language->lang($this->error_prefix . 'PHP_SIZE_OVERRUN', $max_filesize, $this->language->lang($unit)); - break; + break; - case 2: + case UPLOAD_ERR_FORM_SIZE: $max_filesize = get_formatted_filesize($this->max_filesize, false); $error = $this->language->lang($this->error_prefix . 'WRONG_FILESIZE', $max_filesize['value'], $max_filesize['unit']); - break; + break; - case 3: + case UPLOAD_ERR_PARTIAL: $error = $this->language->lang($this->error_prefix . 'PARTIAL_UPLOAD'); - break; + break; - case 4: + case UPLOAD_ERR_NO_FILE: $error = $this->language->lang($this->error_prefix . 'NOT_UPLOADED'); - break; + break; - case 6: + case UPLOAD_ERR_NO_TMP_DIR: $error = 'Temporary folder could not be found. Please check your PHP installation.'; - break; + break; + + case UPLOAD_ERR_CANT_WRITE: + $error = 'Can’t write to temporary folder.'; + break; + + case UPLOAD_ERR_EXTENSION: + $error = 'A PHP extension has stopped the file upload.'; + break; default: $error = false; - break; + break; } return $error; diff --git a/tests/files/upload_test.php b/tests/files/upload_test.php index 4323f6cc92..057bb8a4f2 100644 --- a/tests/files/upload_test.php +++ b/tests/files/upload_test.php @@ -93,4 +93,27 @@ class phpbb_files_upload_test extends phpbb_test_case $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request, $this->phpbb_root_path); $this->assertFalse($upload->is_valid('foobar')); } + + public function data_internal_error() + { + return array( + array(UPLOAD_ERR_INI_SIZE, 'PHP_SIZE_OVERRUN'), + array(UPLOAD_ERR_FORM_SIZE, 'WRONG_FILESIZE'), + array(UPLOAD_ERR_PARTIAL, 'PARTIAL_UPLOAD'), + array(UPLOAD_ERR_NO_FILE, 'NOT_UPLOADED'), + array(UPLOAD_ERR_NO_TMP_DIR, 'Temporary folder could not be found. Please check your PHP installation.'), + array(UPLOAD_ERR_CANT_WRITE, 'Can’t write to temporary folder.'), + array(UPLOAD_ERR_EXTENSION, 'A PHP extension has stopped the file upload.'), + array(9, false), + ); + } + + /** + * @dataProvider data_internal_error + */ + public function test_assign_internal_error($error_code, $expected) + { + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request, $this->phpbb_root_path); + $this->assertSame($expected, $upload->assign_internal_error($error_code)); + } } diff --git a/tests/upload/fileupload_test.php b/tests/upload/fileupload_test.php index 7a84b34473..ab42a4a153 100644 --- a/tests/upload/fileupload_test.php +++ b/tests/upload/fileupload_test.php @@ -119,6 +119,25 @@ class phpbb_fileupload_test extends phpbb_test_case $this->assertEquals('DISALLOWED_EXTENSION', $file->error[0]); } + public function test_common_checks_disallowed_content() + { + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request, $this->phpbb_root_path); + $upload->set_allowed_extensions(array('jpg')) + ->set_max_filesize(1000); + $file = new \phpbb\files\filespec($this->filesystem, $this->language, $this->phpbb_root_path); + $file->set_upload_ary(array( + 'size' => 50, + 'tmp_name' => dirname(__FILE__) . '/fixture/disallowed', + 'name' => 'disallowed.jpg', + 'type' => 'image/jpg' + )) + ->set_upload_namespace($upload); + file_put_contents(dirname(__FILE__) . '/fixture/disallowed', '' . file_get_contents(dirname(__FILE__) . '/fixture/jpg')); + $upload->common_checks($file); + $this->assertEquals('DISALLOWED_CONTENT', $file->error[0]); + unlink(dirname(__FILE__) . '/fixture/disallowed'); + } + public function test_common_checks_invalid_filename() { $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request, $this->phpbb_root_path); From 9e87e5a3437f3d88f7dac4c576a53fed53ff4bae Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Thu, 16 Jul 2015 00:23:10 +0200 Subject: [PATCH 42/66] [ticket/13904] Remove unused use statement PHPBB3-13904 --- phpBB/phpbb/files/types/base.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/phpBB/phpbb/files/types/base.php b/phpBB/phpbb/files/types/base.php index 77ebdb4f32..1ed06053a4 100644 --- a/phpBB/phpbb/files/types/base.php +++ b/phpBB/phpbb/files/types/base.php @@ -13,11 +13,9 @@ namespace phpbb\files\types; -use \phpbb\language\language; - abstract class base implements type_interface { - /** @var language */ + /** @var \phpbb\language\language */ protected $language; /** @var \phpbb\files\upload */ From 3e99816fa2f184b859d47308254aa8f07d68f1dd Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Thu, 16 Jul 2015 12:06:23 +0200 Subject: [PATCH 43/66] [ticket/13904] Set visibility in files and improve test coverage PHPBB3-13904 --- phpBB/phpbb/files/filespec.php | 78 ++++++++++++--------- phpBB/phpbb/files/upload.php | 40 +++++------ tests/files/upload_test.php | 13 ++-- tests/functional/fileupload_remote_test.php | 2 +- tests/upload/filespec_test.php | 60 ++++++++++++++-- 5 files changed, 127 insertions(+), 66 deletions(-) diff --git a/phpBB/phpbb/files/filespec.php b/phpBB/phpbb/files/filespec.php index ed64b7ff5c..e07aef9892 100644 --- a/phpBB/phpbb/files/filespec.php +++ b/phpBB/phpbb/files/filespec.php @@ -22,49 +22,52 @@ use \phpbb\language\language; class filespec { /** @var string File name */ - var $filename = ''; + protected $filename = ''; /** @var string Real name of file */ - var $realname = ''; + protected $realname = ''; /** @var string Upload name of file */ - var $uploadname = ''; + protected $uploadname = ''; /** @var string Mimetype of file */ - var $mimetype = ''; + protected $mimetype = ''; /** @var string File extension */ - var $extension = ''; + public $extension = ''; /** @var int File size */ - var $filesize = 0; + public $filesize = 0; /** @var int Width of file */ - var $width = 0; + protected $width = 0; /** @var int Height of file */ - var $height = 0; + protected $height = 0; /** @var array Image info including type and size */ - var $image_info = array(); + protected $image_info = array(); /** @var string Destination file name */ - var $destination_file = ''; + protected $destination_file = ''; /** @var string Destination file path */ - var $destination_path = ''; + protected $destination_path = ''; /** @var bool Whether file was moved */ - var $file_moved = false; + public $file_moved = false; - /** @var bool Whether file is local */ - var $local = false; + /** @var bool Whether file is local */ + public $local = false; + + /** @var bool Class initialization flag */ + protected $class_initialized = false; /** @var array Error array */ - var $error = array(); + public $error = array(); /** @var upload Instance of upload class */ - var $upload; + public $upload; /** * @var \phpbb\filesystem\filesystem_interface @@ -98,7 +101,7 @@ class filespec * @param \phpbb\mimetype\guesser $mimetype_guesser Mime type guesser * @param \phpbb\plupload\plupload $plupload Plupload */ - function __construct(\phpbb\filesystem\filesystem_interface $phpbb_filesystem, language $language, $phpbb_root_path, \phpbb\mimetype\guesser $mimetype_guesser = null, \phpbb\plupload\plupload $plupload = null) + public function __construct(\phpbb\filesystem\filesystem_interface $phpbb_filesystem, language $language, $phpbb_root_path, \phpbb\mimetype\guesser $mimetype_guesser = null, \phpbb\plupload\plupload $plupload = null) { $this->plupload = $plupload; $this->mimetype_guesser = $mimetype_guesser; @@ -116,6 +119,12 @@ class filespec */ public function set_upload_ary($upload_ary) { + if (!isset($upload_ary) || !sizeof($upload_ary)) + { + return $this; + } + + $this->class_initialized = true; $this->filename = $upload_ary['tmp_name']; $this->filesize = $upload_ary['size']; $name = (STRIP) ? stripslashes($upload_ary['name']) : $upload_ary['name']; @@ -165,7 +174,7 @@ class filespec */ public function init_error() { - return !isset($this->filename); + return !$this->class_initialized; } /** @@ -193,7 +202,7 @@ class filespec * *@access public */ - function clean_filename($mode = 'unique', $prefix = '', $user_id = '') + public function clean_filename($mode = 'unique', $prefix = '', $user_id = '') { if ($this->init_error()) { @@ -216,22 +225,21 @@ class filespec $this->realname = preg_replace("/%(\w{2})/", '_', $this->realname); $this->realname = $prefix . $this->realname . '.' . $this->extension; - break; + break; case 'unique': $this->realname = $prefix . md5(unique_id()); - break; + break; case 'avatar': $this->extension = strtolower($this->extension); $this->realname = $prefix . $user_id . '.' . $this->extension; - break; + break; case 'unique_ext': default: $this->realname = $prefix . md5(unique_id()) . '.' . $this->extension; - break; } } @@ -242,7 +250,7 @@ class filespec * * @return mixed Content of property */ - function get($property) + public function get($property) { if ($this->init_error() || !isset($this->$property)) { @@ -257,7 +265,7 @@ class filespec * * @return bool true if it is an image, false if not */ - function is_image() + public function is_image() { return (strpos($this->mimetype, 'image/') === 0); } @@ -267,7 +275,7 @@ class filespec * * @return bool true if it is a valid upload, false if not */ - function is_uploaded() + public function is_uploaded() { $is_plupload = $this->plupload && $this->plupload->is_active(); @@ -287,7 +295,7 @@ class filespec /** * Remove file */ - function remove() + public function remove() { if ($this->file_moved) { @@ -321,7 +329,7 @@ class filespec * @param string $filename Filename that needs to be checked * @return string Mime type of supplied filename */ - function get_mimetype($filename) + public function get_mimetype($filename) { if ($this->mimetype_guesser !== null) { @@ -343,7 +351,7 @@ class filespec * * @return int File size */ - function get_filesize($filename) + public function get_filesize($filename) { return @filesize($filename); } @@ -356,7 +364,7 @@ class filespec * * @return bool False if disallowed content found, true if not */ - function check_content($disallowed_content) + public function check_content($disallowed_content) { if (empty($disallowed_content)) { @@ -393,7 +401,7 @@ class filespec * @return bool True if file was moved, false if not * @access public */ - function move_file($destination, $overwrite = false, $skip_image_check = false, $chmod = false) + public function move_file($destination, $overwrite = false, $skip_image_check = false, $chmod = false) { if (sizeof($this->error)) { @@ -443,7 +451,7 @@ class filespec } } - break; + break; case 'move': @@ -455,7 +463,7 @@ class filespec } } - break; + break; case 'local': @@ -464,7 +472,7 @@ class filespec $this->error[] = $this->language->lang($this->upload->error_prefix . 'GENERAL_UPLOAD_ERROR', $this->destination_file); } - break; + break; } // Remove temporary filename @@ -544,7 +552,7 @@ class filespec * * @return bool False if issue was found, true if not */ - function additional_checks() + public function additional_checks() { if (!$this->file_moved) { diff --git a/phpBB/phpbb/files/upload.php b/phpBB/phpbb/files/upload.php index 234eb69735..397eb5af36 100644 --- a/phpBB/phpbb/files/upload.php +++ b/phpBB/phpbb/files/upload.php @@ -24,31 +24,31 @@ use \phpbb\request\request_interface; class upload { /** @var array Allowed file extensions */ - var $allowed_extensions = array(); + public $allowed_extensions = array(); /** @var array Disallowed content */ - var $disallowed_content = array('body', 'head', 'html', 'img', 'plaintext', 'a href', 'pre', 'script', 'table', 'title'); + protected $disallowed_content = array('body', 'head', 'html', 'img', 'plaintext', 'a href', 'pre', 'script', 'table', 'title'); /** @var int Maximum filesize */ - var $max_filesize = 0; + public $max_filesize = 0; /** @var int Minimum width of images */ - var $min_width = 0; + public $min_width = 0; /** @var int Minimum height of images */ - var $min_height = 0; + public $min_height = 0; /** @var int Maximum width of images */ - var $max_width = 0; + public $max_width = 0; /** @var int Maximum height of images */ - var $max_height = 0; + public $max_height = 0; /** @var string Prefix for language variables of errors */ - var $error_prefix = ''; + public $error_prefix = ''; /** @var int Timeout for remote upload */ - var $upload_timeout = 6; + public $upload_timeout = 6; /** @var filesystem_interface */ protected $filesystem; @@ -86,7 +86,7 @@ class upload /** * Reset vars */ - function reset_vars() + public function reset_vars() { $this->max_filesize = 0; $this->min_width = $this->min_height = $this->max_width = $this->max_height = 0; @@ -102,7 +102,7 @@ class upload * * @return \phpbb\files\upload This instance of upload */ - function set_allowed_extensions($allowed_extensions) + public function set_allowed_extensions($allowed_extensions) { if ($allowed_extensions !== false && is_array($allowed_extensions)) { @@ -122,7 +122,7 @@ class upload * * @return \phpbb\files\upload This instance of upload */ - function set_allowed_dimensions($min_width, $min_height, $max_width, $max_height) + public function set_allowed_dimensions($min_width, $min_height, $max_width, $max_height) { $this->min_width = (int) $min_width; $this->min_height = (int) $min_height; @@ -139,7 +139,7 @@ class upload * * @return \phpbb\files\upload This instance of upload */ - function set_max_filesize($max_filesize) + public function set_max_filesize($max_filesize) { if ($max_filesize !== false && (int) $max_filesize) { @@ -156,7 +156,7 @@ class upload * * @return \phpbb\files\upload This instance of upload */ - function set_disallowed_content($disallowed_content) + public function set_disallowed_content($disallowed_content) { if ($disallowed_content !== false && is_array($disallowed_content)) { @@ -173,7 +173,7 @@ class upload * * @return \phpbb\files\upload This instance of upload */ - function set_error_prefix($error_prefix) + public function set_error_prefix($error_prefix) { $this->error_prefix = $error_prefix; @@ -264,7 +264,7 @@ class upload * * @param filespec $file Instance of filespec class */ - function common_checks(&$file) + public function common_checks(&$file) { // Filesize is too big or it's 0 if it was larger than the maxsize in the upload form if ($this->max_filesize && ($file->get('filesize') > $this->max_filesize || $file->get('filesize') == 0)) @@ -300,7 +300,7 @@ class upload * * @return bool True if extension is allowed, false if not */ - function valid_extension(&$file) + public function valid_extension(&$file) { return (in_array($file->get('extension'), $this->allowed_extensions)) ? true : false; } @@ -313,7 +313,7 @@ class upload * @return bool True if dimensions are valid or no constraints set, false * if not */ - function valid_dimensions(&$file) + public function valid_dimensions(&$file) { if (!$this->max_width && !$this->max_height && !$this->min_width && !$this->min_height) { @@ -338,7 +338,7 @@ class upload * * @return bool True if form upload is valid, false if not */ - function is_valid($form_name) + public function is_valid($form_name) { $upload = $this->request->file($form_name); @@ -353,7 +353,7 @@ class upload * * @return bool True if content is valid, false if not */ - function valid_content(&$file) + public function valid_content(&$file) { return ($file->check_content($this->disallowed_content)); } diff --git a/tests/files/upload_test.php b/tests/files/upload_test.php index 057bb8a4f2..dc0080d25c 100644 --- a/tests/files/upload_test.php +++ b/tests/files/upload_test.php @@ -76,16 +76,19 @@ class phpbb_files_upload_test extends phpbb_test_case public function test_set_disallowed_content() { $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request, $this->phpbb_root_path); + $disallowed_content = new ReflectionProperty($upload, 'disallowed_content'); + $disallowed_content->setAccessible(true); + $upload->set_disallowed_content(array('foo')); - $this->assertEquals(array('foo'), $upload->disallowed_content); + $this->assertEquals(array('foo'), $disallowed_content->getValue($upload)); $upload->set_disallowed_content(array('foo', 'bar', 'meh')); - $this->assertEquals(array('foo', 'bar', 'meh'), $upload->disallowed_content); + $this->assertEquals(array('foo', 'bar', 'meh'), $disallowed_content->getValue($upload)); $upload->set_disallowed_content(''); - $this->assertEquals(array('foo', 'bar', 'meh'), $upload->disallowed_content); + $this->assertEquals(array('foo', 'bar', 'meh'), $disallowed_content->getValue($upload)); $this->assertINstanceOf('\phpbb\files\upload', $upload->set_disallowed_content(array())); - $this->assertEquals(array(), $upload->disallowed_content); + $this->assertEquals(array(), $disallowed_content->getValue($upload)); $upload->reset_vars(); - $this->assertEquals(array(), $upload->disallowed_content); + $this->assertEquals(array(), $disallowed_content->getValue($upload)); } public function test_is_valid() diff --git a/tests/functional/fileupload_remote_test.php b/tests/functional/fileupload_remote_test.php index a754d8c91d..a6aa233aaf 100644 --- a/tests/functional/fileupload_remote_test.php +++ b/tests/functional/fileupload_remote_test.php @@ -98,7 +98,7 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case ->set_max_filesize(1000); $file = $upload->handle_upload('remote', self::$root_url . 'styles/prosilver/theme/images/forum_read.gif'); $this->assertEquals(0, sizeof($file->error)); - $this->assertTrue(file_exists($file->filename)); + $this->assertTrue(file_exists($file->get('filename'))); } public function test_too_large() diff --git a/tests/upload/filespec_test.php b/tests/upload/filespec_test.php index b0df72a6ff..f885b1acfc 100644 --- a/tests/upload/filespec_test.php +++ b/tests/upload/filespec_test.php @@ -133,7 +133,7 @@ class phpbb_filespec_test extends phpbb_test_case { $upload = new phpbb_mock_fileupload(); $filespec = $this->get_filespec(); - $filespec->upload = $upload; + $filespec->set_upload_namespace($upload); $filespec->file_moved = true; $filespec->filesize = $filespec->get_filesize($this->path . $filename); @@ -174,6 +174,7 @@ class phpbb_filespec_test extends phpbb_test_case array($chunks[2] . $chunks[9]), array($chunks[3] . $chunks[4]), array($chunks[5] . $chunks[6]), + array('foobar.png'), ); } @@ -185,7 +186,7 @@ class phpbb_filespec_test extends phpbb_test_case $bad_chars = array("'", "\\", ' ', '/', ':', '*', '?', '"', '<', '>', '|'); $filespec = $this->get_filespec(array('name' => $filename)); $filespec->clean_filename('real', self::PREFIX); - $name = $filespec->realname; + $name = $filespec->get('realname'); $this->assertEquals(0, preg_match('/%(\w{2})/', $name)); foreach ($bad_chars as $char) @@ -201,7 +202,7 @@ class phpbb_filespec_test extends phpbb_test_case { $filespec = $this->get_filespec(); $filespec->clean_filename('unique', self::PREFIX); - $name = $filespec->realname; + $name = $filespec->get('realname'); $this->assertEquals(strlen($name), 32 + strlen(self::PREFIX)); $this->assertRegExp('#^[A-Za-z0-9]+$#', substr($name, strlen(self::PREFIX))); @@ -210,6 +211,55 @@ class phpbb_filespec_test extends phpbb_test_case } } + public function test_clean_filename_unique_ext() + { + $filenames = array(); + for ($tests = 0; $tests < self::TEST_COUNT; $tests++) + { + $filespec = $this->get_filespec(array('name' => 'foobar.jpg')); + $filespec->clean_filename('unique_ext', self::PREFIX); + $name = $filespec->get('realname'); + + $this->assertEquals(strlen($name), 32 + strlen(self::PREFIX) + strlen('.jpg')); + $this->assertRegExp('#^[A-Za-z0-9]+\.jpg$#', substr($name, strlen(self::PREFIX))); + $this->assertFalse(isset($filenames[$name])); + $filenames[$name] = true; + } + } + + public function data_clean_filename_avatar() + { + return array( + array(false, false, ''), + array('foobar.png', 'u5.png', 'avatar', 'u', 5), + array('foobar.png', 'g9.png', 'avatar', 'g', 9), + + ); + } + + /** + * @dataProvider data_clean_filename_avatar + */ + public function test_clean_filename_avatar($filename, $expected, $mode, $prefix = '', $user_id = '') + { + $filespec = new \phpbb\files\filespec($this->filesystem, $this->language, $this->phpbb_root_path, $this->mimetype_guesser); + + if ($filename) + { + $upload_ary = array( + 'name' => $filename, + 'type' => '', + 'size' => '', + 'tmp_name' => '', + 'error' => '', + ); + $filespec->set_upload_ary($upload_ary); + } + $filespec->clean_filename($mode, $prefix, $user_id); + + $this->assertSame($expected, $filespec->get('realname')); + } + public function get_extension_variables() { return array( @@ -312,7 +362,7 @@ class phpbb_filespec_test extends phpbb_test_case 'type' => $mime_type, )); $filespec->extension = $extension; - $filespec->upload = $upload; + $filespec->set_upload_namespace($upload); $filespec->local = true; $this->assertEquals($expected, $filespec->move_file($this->path . 'copies')); @@ -336,6 +386,6 @@ class phpbb_filespec_test extends phpbb_test_case $type_cast_helper->set_var($upload_name, $filename, 'string', true, true); $filespec = $this->get_filespec(array('name'=> $upload_name)); - $this->assertSame(trim(utf8_basename(htmlspecialchars($filename))), $filespec->uploadname); + $this->assertSame(trim(utf8_basename(htmlspecialchars($filename))), $filespec->get('uploadname')); } } From 46e3d8219671e996677be1943a7e3c80f1039693 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Thu, 16 Jul 2015 16:30:59 +0200 Subject: [PATCH 44/66] [ticket/13904] Add more tests and test cases PHPBB3-13904 --- tests/functional/fileupload_remote_test.php | 1 + tests/mock/fileupload.php | 3 +- tests/upload/filespec_test.php | 43 +++++++++++++++++++++ tests/upload/fileupload_test.php | 4 +- 4 files changed, 49 insertions(+), 2 deletions(-) diff --git a/tests/functional/fileupload_remote_test.php b/tests/functional/fileupload_remote_test.php index a6aa233aaf..1a74b3b498 100644 --- a/tests/functional/fileupload_remote_test.php +++ b/tests/functional/fileupload_remote_test.php @@ -99,6 +99,7 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case $file = $upload->handle_upload('remote', self::$root_url . 'styles/prosilver/theme/images/forum_read.gif'); $this->assertEquals(0, sizeof($file->error)); $this->assertTrue(file_exists($file->get('filename'))); + $this->assertTrue($file->is_uploaded()); } public function test_too_large() diff --git a/tests/mock/fileupload.php b/tests/mock/fileupload.php index 8cc4d77ea1..5a0afc6cd3 100644 --- a/tests/mock/fileupload.php +++ b/tests/mock/fileupload.php @@ -19,9 +19,10 @@ class phpbb_mock_fileupload { public $max_filesize = 100; public $error_prefix = ''; + public $valid_dimensions = true; public function valid_dimensions($filespec) { - return true; + return $this->valid_dimensions; } } diff --git a/tests/upload/filespec_test.php b/tests/upload/filespec_test.php index f885b1acfc..a6ea850763 100644 --- a/tests/upload/filespec_test.php +++ b/tests/upload/filespec_test.php @@ -112,6 +112,13 @@ class phpbb_filespec_test extends phpbb_test_case } } + public function test_empty_upload_ary() + { + $filespec = new \phpbb\files\filespec($this->filesystem, $this->language, $this->phpbb_root_path, $this->mimetype_guesser); + $this->assertInstanceOf('\phpbb\files\filespec', $filespec->set_upload_ary(array())); + $this->assertTrue($filespec->init_error()); + } + public function additional_checks_variables() { // False here just indicates the file is too large and fails the @@ -140,6 +147,19 @@ class phpbb_filespec_test extends phpbb_test_case $this->assertEquals($expected, $filespec->additional_checks()); } + public function test_additional_checks_dimensions() + { + $upload = new phpbb_mock_fileupload(); + $filespec = $this->get_filespec(); + $filespec->set_upload_namespace($upload); + $upload->valid_dimensions = false; + $filespec->file_moved = true; + $upload->max_filesize = 0; + + $this->assertEquals(false, $filespec->additional_checks()); + $this->assertSame(array('WRONG_SIZE'), $filespec->error); + } + public function check_content_variables() { // False here indicates that a file is non-binary and contains @@ -388,4 +408,27 @@ class phpbb_filespec_test extends phpbb_test_case $this->assertSame(trim(utf8_basename(htmlspecialchars($filename))), $filespec->get('uploadname')); } + + public function test_is_uploaded() + { + $filespec = new \phpbb\files\filespec($this->filesystem, $this->language, $this->phpbb_root_path, null); + $reflection_filespec = new ReflectionClass($filespec); + $plupload_property = $reflection_filespec->getProperty('plupload'); + $plupload_property->setAccessible(true); + $plupload_mock = $this->getMockBuilder('\phpbb\plupload\plupload') + ->disableOriginalConstructor() + ->getMock(); + $plupload_mock->expects($this->any()) + ->method('is_active') + ->will($this->returnValue(true)); + $plupload_property->setValue($filespec, $plupload_mock); + $is_uploaded = $reflection_filespec->getMethod('is_uploaded'); + + // Plupload is active and file does not exist + $this->assertFalse($is_uploaded->invoke($filespec)); + + // Plupload is not active and file was not uploaded + $plupload_property->setValue($filespec, null); + $this->assertFalse($is_uploaded->invoke($filespec)); + } } diff --git a/tests/upload/fileupload_test.php b/tests/upload/fileupload_test.php index ab42a4a153..d1d8b55ff3 100644 --- a/tests/upload/fileupload_test.php +++ b/tests/upload/fileupload_test.php @@ -179,7 +179,9 @@ class phpbb_fileupload_test extends phpbb_test_case copy($this->path . 'jpg', $this->path . 'jpg.jpg'); $file = $upload->handle_upload('local', $this->path . 'jpg.jpg'); $this->assertEquals(0, sizeof($file->error)); - unlink($this->path . 'jpg.jpg'); + $this->assertFalse($file->additional_checks()); + $this->assertTrue($file->move_file('../tests/upload/fixture/copies', true)); + $file->remove(); } public function test_move_existent_file() From 7ba0fe47a8610b1bd882f00cd1e158c7b29d248c Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Fri, 17 Jul 2015 09:29:09 +0200 Subject: [PATCH 45/66] [ticket/13904] Test move file on existing error PHPBB3-13904 --- tests/upload/filespec_test.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/upload/filespec_test.php b/tests/upload/filespec_test.php index a6ea850763..1ba473555f 100644 --- a/tests/upload/filespec_test.php +++ b/tests/upload/filespec_test.php @@ -395,6 +395,14 @@ class phpbb_filespec_test extends phpbb_test_case $this->phpbb_root_path = $phpbb_root_path; } + public function test_move_file_error() + { + $filespec = $this->get_filespec(); + $this->assertFalse($filespec->move_file('foobar')); + $filespec->error[] = 'foo'; + $this->assertFalse($filespec->move_file('foo')); + } + /** * @dataProvider clean_filename_variables */ From 02f94b7527298aad3bd15738676da872aa498039 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Fri, 14 Aug 2015 10:31:26 +0200 Subject: [PATCH 46/66] [ticket/13904] Update doc blocks PHPBB3-13904 --- phpBB/phpbb/files/types/form.php | 6 ++++-- phpBB/phpbb/files/types/local.php | 5 +++-- phpBB/phpbb/files/types/remote.php | 6 ++++-- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/phpBB/phpbb/files/types/form.php b/phpBB/phpbb/files/types/form.php index 98d1c51d5f..be0a425d57 100644 --- a/phpBB/phpbb/files/types/form.php +++ b/phpBB/phpbb/files/types/form.php @@ -40,8 +40,10 @@ class form extends base /** * Construct a form upload type * - * @param factory $factory - * @param request_interface $request + * @param factory $factory Files factory + * @param language $language Language class + * @param plupload $plupload Plupload + * @param request_interface $request Request object */ public function __construct(factory $factory, language $language, plupload $plupload, request_interface $request) { diff --git a/phpBB/phpbb/files/types/local.php b/phpBB/phpbb/files/types/local.php index bb5994841f..dfaabded9b 100644 --- a/phpBB/phpbb/files/types/local.php +++ b/phpBB/phpbb/files/types/local.php @@ -35,8 +35,9 @@ class local extends base /** * Construct a form upload type * - * @param factory $factory - * @param request_interface $request + * @param factory $factory Files factory + * @param language $language Language class + * @param request_interface $request Request object */ public function __construct(factory $factory, language $language, request_interface $request) { diff --git a/phpBB/phpbb/files/types/remote.php b/phpBB/phpbb/files/types/remote.php index 65cff8ccc7..94bba8ad46 100644 --- a/phpBB/phpbb/files/types/remote.php +++ b/phpBB/phpbb/files/types/remote.php @@ -38,8 +38,10 @@ class remote extends base /** * Construct a form upload type * - * @param factory $factory - * @param request_interface $request + * @param factory $factory Files factory + * @param language $language Language class + * @param request_interface $request Request object + * @param string $phpbb_root_path phpBB root path */ public function __construct(factory $factory, language $language, request_interface $request, $phpbb_root_path) { From cdde86ce7e0c594fad5992789b3fae466bd526cc Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Wed, 26 Aug 2015 13:57:42 +0200 Subject: [PATCH 47/66] [ticket/13904] Use \phpbb\php\ini class for ini_get() PHPBB3-13904 --- .../default/container/services_files.yml | 2 ++ phpBB/phpbb/files/filespec.php | 31 +++++++++++-------- phpBB/phpbb/files/upload.php | 9 ++++-- tests/files/upload_test.php | 13 +++++--- tests/functional/fileupload_remote_test.php | 14 ++++++--- tests/upload/filespec_test.php | 8 ++--- tests/upload/fileupload_test.php | 27 +++++++++------- 7 files changed, 65 insertions(+), 39 deletions(-) diff --git a/phpBB/config/default/container/services_files.yml b/phpBB/config/default/container/services_files.yml index e193dad4ea..7237c5d318 100644 --- a/phpBB/config/default/container/services_files.yml +++ b/phpBB/config/default/container/services_files.yml @@ -12,6 +12,7 @@ services: scope: prototype arguments: - @filesystem + - @php_ini - @language - %core.root_path% - @mimetype.guesser @@ -23,6 +24,7 @@ services: arguments: - @filesystem - @files.factory + - @php_ini - @language - @request - %core.root_path% diff --git a/phpBB/phpbb/files/filespec.php b/phpBB/phpbb/files/filespec.php index e07aef9892..34d86116c2 100644 --- a/phpBB/phpbb/files/filespec.php +++ b/phpBB/phpbb/files/filespec.php @@ -74,6 +74,15 @@ class filespec */ protected $filesystem; + /** @var \phpbb\php\ini ini_get() wrapper class */ + protected $php_ini; + + /** @var language Language class */ + protected $language; + + /** @var string phpBB root path */ + protected $phpbb_root_path; + /** * The plupload object * @var \phpbb\plupload\plupload @@ -86,28 +95,24 @@ class filespec */ protected $mimetype_guesser; - /** @var language Language class */ - protected $language; - - /** @var string phpBB root path */ - protected $phpbb_root_path; - /** * File upload class * * @param \phpbb\filesystem\filesystem_interface $phpbb_filesystem Filesystem + * @param \phpbb\php\ini $php_ini ini_get() wrapper * @param language $language Language - * @param string $phpbb_root_path phpBB root path - * @param \phpbb\mimetype\guesser $mimetype_guesser Mime type guesser - * @param \phpbb\plupload\plupload $plupload Plupload + * @param string $phpbb_root_path phpBB root path + * @param \phpbb\mimetype\guesser $mimetype_guesser Mime type guesser + * @param \phpbb\plupload\plupload $plupload Plupload */ - public function __construct(\phpbb\filesystem\filesystem_interface $phpbb_filesystem, language $language, $phpbb_root_path, \phpbb\mimetype\guesser $mimetype_guesser = null, \phpbb\plupload\plupload $plupload = null) + public function __construct(\phpbb\filesystem\filesystem_interface $phpbb_filesystem, \phpbb\php\ini $php_ini, language $language, $phpbb_root_path, \phpbb\mimetype\guesser $mimetype_guesser = null, \phpbb\plupload\plupload $plupload = null) { - $this->plupload = $plupload; - $this->mimetype_guesser = $mimetype_guesser; $this->filesystem = $phpbb_filesystem; + $this->php_ini = $php_ini; $this->language = $language; $this->phpbb_root_path = $phpbb_root_path; + $this->plupload = $plupload; + $this->mimetype_guesser = $mimetype_guesser; } /** @@ -420,7 +425,7 @@ class filespec return false; } - $upload_mode = (@ini_get('open_basedir') || @ini_get('safe_mode') || strtolower(@ini_get('safe_mode')) == 'on') ? 'move' : 'copy'; + $upload_mode = ($this->php_ini->get_bool('open_basedir') || $this->php_ini->get_bool('safe_mode')) ? 'move' : 'copy'; $upload_mode = ($this->local) ? 'local' : $upload_mode; $this->destination_file = $this->destination_path . '/' . utf8_basename($this->realname); diff --git a/phpBB/phpbb/files/upload.php b/phpBB/phpbb/files/upload.php index 397eb5af36..35107ccb45 100644 --- a/phpBB/phpbb/files/upload.php +++ b/phpBB/phpbb/files/upload.php @@ -56,6 +56,9 @@ class upload /** @var \phpbb\files\factory Files factory */ protected $factory; + /** @var \phpbb\php\ini ini_get() wrapper */ + protected $php_ini; + /** @var \phpbb\language\language Language class */ protected $language; @@ -70,14 +73,16 @@ class upload * * @param filesystem_interface $filesystem * @param factory $factory Files factory + * @param \phpbb\php\ini $php_ini ini_get() wrapper * @param language $language Language class * @param request_interface $request Request class * @param string $phpbb_root_path phpBB root path */ - public function __construct(filesystem_interface $filesystem, factory $factory, language $language, request_interface $request, $phpbb_root_path) + public function __construct(filesystem_interface $filesystem, factory $factory, \phpbb\php\ini $php_ini, language $language, request_interface $request, $phpbb_root_path) { $this->filesystem = $filesystem; $this->factory = $factory; + $this->php_ini = $php_ini; $this->language = $language; $this->request = $request; $this->phpbb_root_path = $phpbb_root_path; @@ -211,7 +216,7 @@ class upload switch ($errorcode) { case UPLOAD_ERR_INI_SIZE: - $max_filesize = @ini_get('upload_max_filesize'); + $max_filesize = $this->php_ini->get_string('upload_max_filesize'); $unit = 'MB'; if (!empty($max_filesize)) diff --git a/tests/files/upload_test.php b/tests/files/upload_test.php index dc0080d25c..d11ebc742b 100644 --- a/tests/files/upload_test.php +++ b/tests/files/upload_test.php @@ -23,6 +23,9 @@ class phpbb_files_upload_test extends phpbb_test_case /** @var \phpbb\files\factory */ protected $factory; + /** @var \phpbb\php\ini */ + protected $php_ini; + /** @var \phpbb\language\language */ protected $language; @@ -49,10 +52,12 @@ class phpbb_files_upload_test extends phpbb_test_case $this->filesystem = new \phpbb\filesystem\filesystem(); $this->language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)); + $this->php_ini = new \phpbb\php\ini; $this->container = new phpbb_mock_container_builder($phpbb_root_path, $phpEx); $this->container->set('files.filespec', new \phpbb\files\filespec( $this->filesystem, + $this->php_ini, $this->language, $phpbb_root_path, new \phpbb\mimetype\guesser(array( @@ -66,7 +71,7 @@ class phpbb_files_upload_test extends phpbb_test_case public function test_reset_vars() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request, $this->phpbb_root_path); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->php_ini, $this->language, $this->request, $this->phpbb_root_path); $upload->set_max_filesize(500); $this->assertEquals(500, $upload->max_filesize); $upload->reset_vars(); @@ -75,7 +80,7 @@ class phpbb_files_upload_test extends phpbb_test_case public function test_set_disallowed_content() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request, $this->phpbb_root_path); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->php_ini, $this->language, $this->request, $this->phpbb_root_path); $disallowed_content = new ReflectionProperty($upload, 'disallowed_content'); $disallowed_content->setAccessible(true); @@ -93,7 +98,7 @@ class phpbb_files_upload_test extends phpbb_test_case public function test_is_valid() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request, $this->phpbb_root_path); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->php_ini, $this->language, $this->request, $this->phpbb_root_path); $this->assertFalse($upload->is_valid('foobar')); } @@ -116,7 +121,7 @@ class phpbb_files_upload_test extends phpbb_test_case */ public function test_assign_internal_error($error_code, $expected) { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request, $this->phpbb_root_path); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->php_ini, $this->language, $this->request, $this->phpbb_root_path); $this->assertSame($expected, $upload->assign_internal_error($error_code)); } } diff --git a/tests/functional/fileupload_remote_test.php b/tests/functional/fileupload_remote_test.php index 1a74b3b498..45c1e914c2 100644 --- a/tests/functional/fileupload_remote_test.php +++ b/tests/functional/fileupload_remote_test.php @@ -22,6 +22,9 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case /** @var \phpbb\files\factory */ protected $factory; + /** @var \phpbb\php\ini */ + protected $php_ini; + /** @var \phpbb\language\language */ protected $language; @@ -51,9 +54,10 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case $this->filesystem = new \phpbb\filesystem\filesystem(); $this->language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)); $this->request = $this->getMock('\phpbb\request\request'); + $this->php_ini = new \phpbb\php\ini; $container = new phpbb_mock_container_builder(); - $container->set('files.filespec', new \phpbb\files\filespec($this->filesystem, $this->language, $this->phpbb_root_path)); + $container->set('files.filespec', new \phpbb\files\filespec($this->filesystem, $this->php_ini, $this->language, $this->phpbb_root_path)); $this->factory = new \phpbb\files\factory($container); $container->set('files.factory', $this->factory); $container->set('files.types.remote', new \phpbb\files\types\remote($this->factory, $this->language, $this->request, $phpbb_root_path)); @@ -70,7 +74,7 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case public function test_invalid_extension() { /** @var \phpbb\files\upload $upload */ - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request, $this->phpbb_root_path); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->php_ini, $this->language, $this->request, $this->phpbb_root_path); $upload->set_error_prefix('') ->set_allowed_extensions(array('jpg')) ->set_max_filesize(100); @@ -81,7 +85,7 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case public function test_empty_file() { /** @var \phpbb\files\upload $upload */ - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request, $this->phpbb_root_path); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->php_ini, $this->language, $this->request, $this->phpbb_root_path); $upload->set_error_prefix('') ->set_allowed_extensions(array('jpg')) ->set_max_filesize(100); @@ -92,7 +96,7 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case public function test_successful_upload() { /** @var \phpbb\files\upload $upload */ - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request, $this->phpbb_root_path); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->php_ini, $this->language, $this->request, $this->phpbb_root_path); $upload->set_error_prefix('') ->set_allowed_extensions(array('gif')) ->set_max_filesize(1000); @@ -105,7 +109,7 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case public function test_too_large() { /** @var \phpbb\files\upload $upload */ - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request, $this->phpbb_root_path); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->php_ini, $this->language, $this->request, $this->phpbb_root_path); $upload->set_error_prefix('') ->set_allowed_extensions(array('gif')) ->set_max_filesize(100); diff --git a/tests/upload/filespec_test.php b/tests/upload/filespec_test.php index 1ba473555f..8a9e53f30b 100644 --- a/tests/upload/filespec_test.php +++ b/tests/upload/filespec_test.php @@ -93,7 +93,7 @@ class phpbb_filespec_test extends phpbb_test_case 'error' => '', ); - $filespec = new \phpbb\files\filespec($this->filesystem, $this->language, $this->phpbb_root_path, $this->mimetype_guesser); + $filespec = new \phpbb\files\filespec($this->filesystem, new \phpbb\php\ini, $this->language, $this->phpbb_root_path, $this->mimetype_guesser); return $filespec->set_upload_ary(array_merge($upload_ary, $override)); } @@ -114,7 +114,7 @@ class phpbb_filespec_test extends phpbb_test_case public function test_empty_upload_ary() { - $filespec = new \phpbb\files\filespec($this->filesystem, $this->language, $this->phpbb_root_path, $this->mimetype_guesser); + $filespec = new \phpbb\files\filespec($this->filesystem, new \phpbb\php\ini, $this->language, $this->phpbb_root_path, $this->mimetype_guesser); $this->assertInstanceOf('\phpbb\files\filespec', $filespec->set_upload_ary(array())); $this->assertTrue($filespec->init_error()); } @@ -262,7 +262,7 @@ class phpbb_filespec_test extends phpbb_test_case */ public function test_clean_filename_avatar($filename, $expected, $mode, $prefix = '', $user_id = '') { - $filespec = new \phpbb\files\filespec($this->filesystem, $this->language, $this->phpbb_root_path, $this->mimetype_guesser); + $filespec = new \phpbb\files\filespec($this->filesystem, new \phpbb\php\ini, $this->language, $this->phpbb_root_path, $this->mimetype_guesser); if ($filename) { @@ -419,7 +419,7 @@ class phpbb_filespec_test extends phpbb_test_case public function test_is_uploaded() { - $filespec = new \phpbb\files\filespec($this->filesystem, $this->language, $this->phpbb_root_path, null); + $filespec = new \phpbb\files\filespec($this->filesystem, new \phpbb\php\ini, $this->language, $this->phpbb_root_path, null); $reflection_filespec = new ReflectionClass($filespec); $plupload_property = $reflection_filespec->getProperty('plupload'); $plupload_property->setAccessible(true); diff --git a/tests/upload/fileupload_test.php b/tests/upload/fileupload_test.php index d1d8b55ff3..957fb47755 100644 --- a/tests/upload/fileupload_test.php +++ b/tests/upload/fileupload_test.php @@ -27,6 +27,9 @@ class phpbb_fileupload_test extends phpbb_test_case /** @var \phpbb\files\factory */ protected $factory; + /** @var \phpbb\php\ini */ + protected $php_ini; + /** @var \phpbb\language\language */ protected $language; @@ -50,6 +53,7 @@ class phpbb_fileupload_test extends phpbb_test_case $config['rand_seed_last_update'] = time() + 600; $this->request = $this->getMock('\phpbb\request\request'); + $this->php_ini = new \phpbb\php\ini; $this->filesystem = new \phpbb\filesystem\filesystem(); $this->language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)); @@ -66,13 +70,14 @@ class phpbb_fileupload_test extends phpbb_test_case $this->container = new phpbb_mock_container_builder($phpbb_root_path, $phpEx); $this->container->set('files.filespec', new \phpbb\files\filespec( $this->filesystem, + $this->php_ini, $this->language, $phpbb_root_path, new \phpbb\mimetype\guesser(array( 'mimetype.extension_guesser' => new \phpbb\mimetype\extension_guesser(), )))); $this->factory = new \phpbb\files\factory($this->container); - $plupload = new \phpbb\plupload\plupload($phpbb_root_path, $config, $this->request, new \phpbb\user($this->language, '\phpbb\datetime'), new \phpbb\php\ini(), $this->mimetype_guesser); + $plupload = new \phpbb\plupload\plupload($phpbb_root_path, $config, $this->request, new \phpbb\user($this->language, '\phpbb\datetime'), $this->php_ini, $this->mimetype_guesser); $this->container->set('files.types.form', new \phpbb\files\types\form( $this->factory, $this->language, @@ -111,7 +116,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_common_checks_invalid_extension() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request, $this->phpbb_root_path); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->php_ini, $this->language, $this->request, $this->phpbb_root_path); $upload->set_allowed_extensions(array('png')) ->set_max_filesize(100); $file = $this->gen_valid_filespec(); @@ -121,10 +126,10 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_common_checks_disallowed_content() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request, $this->phpbb_root_path); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->php_ini, $this->language, $this->request, $this->phpbb_root_path); $upload->set_allowed_extensions(array('jpg')) ->set_max_filesize(1000); - $file = new \phpbb\files\filespec($this->filesystem, $this->language, $this->phpbb_root_path); + $file = new \phpbb\files\filespec($this->filesystem, $this->php_ini, $this->language, $this->phpbb_root_path); $file->set_upload_ary(array( 'size' => 50, 'tmp_name' => dirname(__FILE__) . '/fixture/disallowed', @@ -140,7 +145,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_common_checks_invalid_filename() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request, $this->phpbb_root_path); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->php_ini, $this->language, $this->request, $this->phpbb_root_path); $upload->set_allowed_extensions(array('jpg')) ->set_max_filesize(100); $file = $this->gen_valid_filespec(); @@ -151,7 +156,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_common_checks_too_large() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request, $this->phpbb_root_path); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->php_ini, $this->language, $this->request, $this->phpbb_root_path); $upload->set_allowed_extensions(array('jpg')) ->set_max_filesize(100); $file = $this->gen_valid_filespec(); @@ -162,7 +167,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_common_checks_valid_file() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request, $this->phpbb_root_path); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->php_ini, $this->language, $this->request, $this->phpbb_root_path); $upload->set_allowed_extensions(array('jpg')) ->set_max_filesize(1000); $file = $this->gen_valid_filespec(); @@ -172,7 +177,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_local_upload() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request, $this->phpbb_root_path); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->php_ini, $this->language, $this->request, $this->phpbb_root_path); $upload->set_allowed_extensions(array('jpg')) ->set_max_filesize(1000); @@ -186,7 +191,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_move_existent_file() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request, $this->phpbb_root_path); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->php_ini, $this->language, $this->request, $this->phpbb_root_path); $upload->set_allowed_extensions(array('jpg')) ->set_max_filesize(1000); @@ -200,7 +205,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_move_existent_file_overwrite() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request, $this->phpbb_root_path); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->php_ini, $this->language, $this->request, $this->phpbb_root_path); $upload->set_allowed_extensions(array('jpg')) ->set_max_filesize(1000); @@ -215,7 +220,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_valid_dimensions() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request, $this->phpbb_root_path); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->php_ini, $this->language, $this->request, $this->phpbb_root_path); $upload->set_allowed_extensions(false) ->set_max_filesize(false) ->set_allowed_dimensions(1, 1, 100, 100); From 36545d5cbe7188efbedf2e1f44b1a7b9617b50c1 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Wed, 26 Aug 2015 16:18:10 +0200 Subject: [PATCH 48/66] [ticket/13904] Switch around constructor arguments PHPBB3-13904 --- .../default/container/services_files.yml | 4 ++-- phpBB/phpbb/files/filespec.php | 20 +++++------------ phpBB/phpbb/files/upload.php | 6 ++--- tests/files/upload_test.php | 10 ++++----- tests/functional/fileupload_remote_test.php | 10 ++++----- tests/upload/filespec_test.php | 8 +++---- tests/upload/fileupload_test.php | 22 +++++++++---------- 7 files changed, 36 insertions(+), 44 deletions(-) diff --git a/phpBB/config/default/container/services_files.yml b/phpBB/config/default/container/services_files.yml index 7237c5d318..25cd532ae3 100644 --- a/phpBB/config/default/container/services_files.yml +++ b/phpBB/config/default/container/services_files.yml @@ -12,8 +12,8 @@ services: scope: prototype arguments: - @filesystem - - @php_ini - @language + - @php_ini - %core.root_path% - @mimetype.guesser - @plupload @@ -24,8 +24,8 @@ services: arguments: - @filesystem - @files.factory - - @php_ini - @language + - @php_ini - @request - %core.root_path% diff --git a/phpBB/phpbb/files/filespec.php b/phpBB/phpbb/files/filespec.php index 34d86116c2..48e12f23ef 100644 --- a/phpBB/phpbb/files/filespec.php +++ b/phpBB/phpbb/files/filespec.php @@ -69,9 +69,7 @@ class filespec /** @var upload Instance of upload class */ public $upload; - /** - * @var \phpbb\filesystem\filesystem_interface - */ + /** @var \phpbb\filesystem\filesystem_interface */ protected $filesystem; /** @var \phpbb\php\ini ini_get() wrapper class */ @@ -83,33 +81,27 @@ class filespec /** @var string phpBB root path */ protected $phpbb_root_path; - /** - * The plupload object - * @var \phpbb\plupload\plupload - */ + /** @var \phpbb\plupload\plupload The plupload object */ protected $plupload; - /** - * phpBB Mimetype guesser - * @var \phpbb\mimetype\guesser - */ + /** @var \phpbb\mimetype\guesser phpBB Mimetype guesser */ protected $mimetype_guesser; /** * File upload class * * @param \phpbb\filesystem\filesystem_interface $phpbb_filesystem Filesystem - * @param \phpbb\php\ini $php_ini ini_get() wrapper * @param language $language Language + * @param \phpbb\php\ini $php_ini ini_get() wrapper * @param string $phpbb_root_path phpBB root path * @param \phpbb\mimetype\guesser $mimetype_guesser Mime type guesser * @param \phpbb\plupload\plupload $plupload Plupload */ - public function __construct(\phpbb\filesystem\filesystem_interface $phpbb_filesystem, \phpbb\php\ini $php_ini, language $language, $phpbb_root_path, \phpbb\mimetype\guesser $mimetype_guesser = null, \phpbb\plupload\plupload $plupload = null) + public function __construct(\phpbb\filesystem\filesystem_interface $phpbb_filesystem, language $language, \phpbb\php\ini $php_ini, $phpbb_root_path, \phpbb\mimetype\guesser $mimetype_guesser = null, \phpbb\plupload\plupload $plupload = null) { $this->filesystem = $phpbb_filesystem; - $this->php_ini = $php_ini; $this->language = $language; + $this->php_ini = $php_ini; $this->phpbb_root_path = $phpbb_root_path; $this->plupload = $plupload; $this->mimetype_guesser = $mimetype_guesser; diff --git a/phpBB/phpbb/files/upload.php b/phpBB/phpbb/files/upload.php index 35107ccb45..036f216d22 100644 --- a/phpBB/phpbb/files/upload.php +++ b/phpBB/phpbb/files/upload.php @@ -73,17 +73,17 @@ class upload * * @param filesystem_interface $filesystem * @param factory $factory Files factory - * @param \phpbb\php\ini $php_ini ini_get() wrapper * @param language $language Language class + * @param \phpbb\php\ini $php_ini ini_get() wrapper * @param request_interface $request Request class * @param string $phpbb_root_path phpBB root path */ - public function __construct(filesystem_interface $filesystem, factory $factory, \phpbb\php\ini $php_ini, language $language, request_interface $request, $phpbb_root_path) + public function __construct(filesystem_interface $filesystem, factory $factory, language $language, \phpbb\php\ini $php_ini, request_interface $request, $phpbb_root_path) { $this->filesystem = $filesystem; $this->factory = $factory; - $this->php_ini = $php_ini; $this->language = $language; + $this->php_ini = $php_ini; $this->request = $request; $this->phpbb_root_path = $phpbb_root_path; } diff --git a/tests/files/upload_test.php b/tests/files/upload_test.php index d11ebc742b..6f4a10e6aa 100644 --- a/tests/files/upload_test.php +++ b/tests/files/upload_test.php @@ -57,8 +57,8 @@ class phpbb_files_upload_test extends phpbb_test_case $this->container = new phpbb_mock_container_builder($phpbb_root_path, $phpEx); $this->container->set('files.filespec', new \phpbb\files\filespec( $this->filesystem, - $this->php_ini, $this->language, + $this->php_ini, $phpbb_root_path, new \phpbb\mimetype\guesser(array( 'mimetype.extension_guesser' => new \phpbb\mimetype\extension_guesser(), @@ -71,7 +71,7 @@ class phpbb_files_upload_test extends phpbb_test_case public function test_reset_vars() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->php_ini, $this->language, $this->request, $this->phpbb_root_path); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); $upload->set_max_filesize(500); $this->assertEquals(500, $upload->max_filesize); $upload->reset_vars(); @@ -80,7 +80,7 @@ class phpbb_files_upload_test extends phpbb_test_case public function test_set_disallowed_content() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->php_ini, $this->language, $this->request, $this->phpbb_root_path); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); $disallowed_content = new ReflectionProperty($upload, 'disallowed_content'); $disallowed_content->setAccessible(true); @@ -98,7 +98,7 @@ class phpbb_files_upload_test extends phpbb_test_case public function test_is_valid() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->php_ini, $this->language, $this->request, $this->phpbb_root_path); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); $this->assertFalse($upload->is_valid('foobar')); } @@ -121,7 +121,7 @@ class phpbb_files_upload_test extends phpbb_test_case */ public function test_assign_internal_error($error_code, $expected) { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->php_ini, $this->language, $this->request, $this->phpbb_root_path); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); $this->assertSame($expected, $upload->assign_internal_error($error_code)); } } diff --git a/tests/functional/fileupload_remote_test.php b/tests/functional/fileupload_remote_test.php index 45c1e914c2..4fd8ae1f19 100644 --- a/tests/functional/fileupload_remote_test.php +++ b/tests/functional/fileupload_remote_test.php @@ -57,7 +57,7 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case $this->php_ini = new \phpbb\php\ini; $container = new phpbb_mock_container_builder(); - $container->set('files.filespec', new \phpbb\files\filespec($this->filesystem, $this->php_ini, $this->language, $this->phpbb_root_path)); + $container->set('files.filespec', new \phpbb\files\filespec($this->filesystem, $this->language, $this->php_ini, $this->phpbb_root_path)); $this->factory = new \phpbb\files\factory($container); $container->set('files.factory', $this->factory); $container->set('files.types.remote', new \phpbb\files\types\remote($this->factory, $this->language, $this->request, $phpbb_root_path)); @@ -74,7 +74,7 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case public function test_invalid_extension() { /** @var \phpbb\files\upload $upload */ - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->php_ini, $this->language, $this->request, $this->phpbb_root_path); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); $upload->set_error_prefix('') ->set_allowed_extensions(array('jpg')) ->set_max_filesize(100); @@ -85,7 +85,7 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case public function test_empty_file() { /** @var \phpbb\files\upload $upload */ - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->php_ini, $this->language, $this->request, $this->phpbb_root_path); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); $upload->set_error_prefix('') ->set_allowed_extensions(array('jpg')) ->set_max_filesize(100); @@ -96,7 +96,7 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case public function test_successful_upload() { /** @var \phpbb\files\upload $upload */ - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->php_ini, $this->language, $this->request, $this->phpbb_root_path); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); $upload->set_error_prefix('') ->set_allowed_extensions(array('gif')) ->set_max_filesize(1000); @@ -109,7 +109,7 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case public function test_too_large() { /** @var \phpbb\files\upload $upload */ - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->php_ini, $this->language, $this->request, $this->phpbb_root_path); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); $upload->set_error_prefix('') ->set_allowed_extensions(array('gif')) ->set_max_filesize(100); diff --git a/tests/upload/filespec_test.php b/tests/upload/filespec_test.php index 8a9e53f30b..595439c917 100644 --- a/tests/upload/filespec_test.php +++ b/tests/upload/filespec_test.php @@ -93,7 +93,7 @@ class phpbb_filespec_test extends phpbb_test_case 'error' => '', ); - $filespec = new \phpbb\files\filespec($this->filesystem, new \phpbb\php\ini, $this->language, $this->phpbb_root_path, $this->mimetype_guesser); + $filespec = new \phpbb\files\filespec($this->filesystem, $this->language, new \phpbb\php\ini, $this->phpbb_root_path, $this->mimetype_guesser); return $filespec->set_upload_ary(array_merge($upload_ary, $override)); } @@ -114,7 +114,7 @@ class phpbb_filespec_test extends phpbb_test_case public function test_empty_upload_ary() { - $filespec = new \phpbb\files\filespec($this->filesystem, new \phpbb\php\ini, $this->language, $this->phpbb_root_path, $this->mimetype_guesser); + $filespec = new \phpbb\files\filespec($this->filesystem, $this->language, new \phpbb\php\ini, $this->phpbb_root_path, $this->mimetype_guesser); $this->assertInstanceOf('\phpbb\files\filespec', $filespec->set_upload_ary(array())); $this->assertTrue($filespec->init_error()); } @@ -262,7 +262,7 @@ class phpbb_filespec_test extends phpbb_test_case */ public function test_clean_filename_avatar($filename, $expected, $mode, $prefix = '', $user_id = '') { - $filespec = new \phpbb\files\filespec($this->filesystem, new \phpbb\php\ini, $this->language, $this->phpbb_root_path, $this->mimetype_guesser); + $filespec = new \phpbb\files\filespec($this->filesystem, $this->language, new \phpbb\php\ini, $this->phpbb_root_path, $this->mimetype_guesser); if ($filename) { @@ -419,7 +419,7 @@ class phpbb_filespec_test extends phpbb_test_case public function test_is_uploaded() { - $filespec = new \phpbb\files\filespec($this->filesystem, new \phpbb\php\ini, $this->language, $this->phpbb_root_path, null); + $filespec = new \phpbb\files\filespec($this->filesystem, $this->language, new \phpbb\php\ini, $this->phpbb_root_path, null); $reflection_filespec = new ReflectionClass($filespec); $plupload_property = $reflection_filespec->getProperty('plupload'); $plupload_property->setAccessible(true); diff --git a/tests/upload/fileupload_test.php b/tests/upload/fileupload_test.php index 957fb47755..40f8704271 100644 --- a/tests/upload/fileupload_test.php +++ b/tests/upload/fileupload_test.php @@ -70,8 +70,8 @@ class phpbb_fileupload_test extends phpbb_test_case $this->container = new phpbb_mock_container_builder($phpbb_root_path, $phpEx); $this->container->set('files.filespec', new \phpbb\files\filespec( $this->filesystem, - $this->php_ini, $this->language, + $this->php_ini, $phpbb_root_path, new \phpbb\mimetype\guesser(array( 'mimetype.extension_guesser' => new \phpbb\mimetype\extension_guesser(), @@ -116,7 +116,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_common_checks_invalid_extension() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->php_ini, $this->language, $this->request, $this->phpbb_root_path); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); $upload->set_allowed_extensions(array('png')) ->set_max_filesize(100); $file = $this->gen_valid_filespec(); @@ -126,10 +126,10 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_common_checks_disallowed_content() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->php_ini, $this->language, $this->request, $this->phpbb_root_path); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); $upload->set_allowed_extensions(array('jpg')) ->set_max_filesize(1000); - $file = new \phpbb\files\filespec($this->filesystem, $this->php_ini, $this->language, $this->phpbb_root_path); + $file = new \phpbb\files\filespec($this->filesystem, $this->language, $this->php_ini, $this->phpbb_root_path); $file->set_upload_ary(array( 'size' => 50, 'tmp_name' => dirname(__FILE__) . '/fixture/disallowed', @@ -145,7 +145,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_common_checks_invalid_filename() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->php_ini, $this->language, $this->request, $this->phpbb_root_path); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); $upload->set_allowed_extensions(array('jpg')) ->set_max_filesize(100); $file = $this->gen_valid_filespec(); @@ -156,7 +156,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_common_checks_too_large() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->php_ini, $this->language, $this->request, $this->phpbb_root_path); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); $upload->set_allowed_extensions(array('jpg')) ->set_max_filesize(100); $file = $this->gen_valid_filespec(); @@ -167,7 +167,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_common_checks_valid_file() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->php_ini, $this->language, $this->request, $this->phpbb_root_path); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); $upload->set_allowed_extensions(array('jpg')) ->set_max_filesize(1000); $file = $this->gen_valid_filespec(); @@ -177,7 +177,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_local_upload() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->php_ini, $this->language, $this->request, $this->phpbb_root_path); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); $upload->set_allowed_extensions(array('jpg')) ->set_max_filesize(1000); @@ -191,7 +191,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_move_existent_file() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->php_ini, $this->language, $this->request, $this->phpbb_root_path); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); $upload->set_allowed_extensions(array('jpg')) ->set_max_filesize(1000); @@ -205,7 +205,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_move_existent_file_overwrite() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->php_ini, $this->language, $this->request, $this->phpbb_root_path); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); $upload->set_allowed_extensions(array('jpg')) ->set_max_filesize(1000); @@ -220,7 +220,7 @@ class phpbb_fileupload_test extends phpbb_test_case public function test_valid_dimensions() { - $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->php_ini, $this->language, $this->request, $this->phpbb_root_path); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); $upload->set_allowed_extensions(false) ->set_max_filesize(false) ->set_allowed_dimensions(1, 1, 100, 100); From b2957b9d6543737076d96209ba0f12e2fc145a9a Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Wed, 9 Sep 2015 08:28:49 +0200 Subject: [PATCH 49/66] [ticket/13904] Add bantu/ini-get-wrapper to composer.json PHPBB3-13904 --- phpBB/composer.json | 1 + phpBB/composer.lock | 32 +++++++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/phpBB/composer.json b/phpBB/composer.json index 70319fe3fb..bcf359a53b 100644 --- a/phpBB/composer.json +++ b/phpBB/composer.json @@ -25,6 +25,7 @@ "phpbb/phpbb-core": "self.version" }, "require": { + "bantu/ini-get-wrapper": "1.0.*", "lusitanian/oauth": "0.2.*", "marc1706/fast-image-size": "1.1.*", "patchwork/utf8": "1.1.*", diff --git a/phpBB/composer.lock b/phpBB/composer.lock index 7d45abc6e3..cbd7292aed 100644 --- a/phpBB/composer.lock +++ b/phpBB/composer.lock @@ -4,8 +4,38 @@ "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "da690ad4ab4884661792b4b9d648934a", + "hash": "ba4a5aa23baa2fffaf9843b0be42e189", "packages": [ + { + "name": "bantu/ini-get-wrapper", + "version": "v1.0.1", + "source": { + "type": "git", + "url": "https://github.com/bantuXorg/php-ini-get-wrapper.git", + "reference": "4770c7feab370c62e23db4f31c112b7c6d90aee2" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/bantuXorg/php-ini-get-wrapper/zipball/4770c7feab370c62e23db4f31c112b7c6d90aee2", + "reference": "4770c7feab370c62e23db4f31c112b7c6d90aee2", + "shasum": "" + }, + "require-dev": { + "phpunit/phpunit": "3.7.*" + }, + "type": "library", + "autoload": { + "psr-4": { + "bantu\\IniGetWrapper\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Convenience wrapper around ini_get()", + "time": "2014-09-15 13:12:35" + }, { "name": "lusitanian/oauth", "version": "v0.2.1", From 16f3b8c2b9de388223cbe8ace9e1d9bcf0ba5e11 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Thu, 27 Aug 2015 10:51:10 +0200 Subject: [PATCH 50/66] [ticket/13904] Modify files for changes in ini wrapper PHPBB3-13904 --- .../config/default/container/services_php.yml | 2 +- phpBB/install/database_update.php | 4 +- phpBB/phpbb/files/filespec.php | 8 ++-- phpBB/phpbb/files/upload.php | 8 ++-- phpBB/phpbb/install/helper/config.php | 8 ++-- phpBB/phpbb/plupload/plupload.php | 12 ++--- tests/cache/apc_driver_test.php | 6 +-- tests/files/upload_test.php | 4 +- tests/functional/fileupload_remote_test.php | 4 +- tests/installer/installer_config_test.php | 6 +-- tests/installer/module_base_test.php | 2 +- tests/plupload/plupload_test.php | 2 +- tests/upload/filespec_test.php | 8 ++-- tests/upload/fileupload_test.php | 4 +- tests/wrapper/phpbb_php_ini_fake.php | 2 +- tests/wrapper/phpbb_php_ini_test.php | 46 ++++++++----------- 16 files changed, 60 insertions(+), 66 deletions(-) diff --git a/phpBB/config/default/container/services_php.yml b/phpBB/config/default/container/services_php.yml index 8aabc7341f..29349960f3 100644 --- a/phpBB/config/default/container/services_php.yml +++ b/phpBB/config/default/container/services_php.yml @@ -1,3 +1,3 @@ services: php_ini: - class: phpbb\php\ini + class: bantu\IniGetWrapper\IniGetWrapper diff --git a/phpBB/install/database_update.php b/phpBB/install/database_update.php index 853848d637..299467b5f3 100644 --- a/phpBB/install/database_update.php +++ b/phpBB/install/database_update.php @@ -205,8 +205,8 @@ $migrator->set_migrations($migrations); // What is a safe limit of execution time? Half the max execution time should be safe. // No more than 15 seconds so the user isn't sitting and waiting for a very long time -$phpbb_ini = new \phpbb\php\ini(); -$safe_time_limit = min(15, ($phpbb_ini->get_int('max_execution_time') / 2)); +$phpbb_ini = new \bantu\IniGetWrapper\IniGetWrapper(); +$safe_time_limit = min(15, ($phpbb_ini->getNumeric('max_execution_time') / 2)); // While we're going to try limit this to half the max execution time, // we want to try and take additional measures to prevent hitting the diff --git a/phpBB/phpbb/files/filespec.php b/phpBB/phpbb/files/filespec.php index 48e12f23ef..580016b281 100644 --- a/phpBB/phpbb/files/filespec.php +++ b/phpBB/phpbb/files/filespec.php @@ -72,7 +72,7 @@ class filespec /** @var \phpbb\filesystem\filesystem_interface */ protected $filesystem; - /** @var \phpbb\php\ini ini_get() wrapper class */ + /** @var \bantu\IniGetWrapper\IniGetWrapper ini_get() wrapper class */ protected $php_ini; /** @var language Language class */ @@ -92,12 +92,12 @@ class filespec * * @param \phpbb\filesystem\filesystem_interface $phpbb_filesystem Filesystem * @param language $language Language - * @param \phpbb\php\ini $php_ini ini_get() wrapper + * @param \bantu\IniGetWrapper\IniGetWrapper $php_ini ini_get() wrapper * @param string $phpbb_root_path phpBB root path * @param \phpbb\mimetype\guesser $mimetype_guesser Mime type guesser * @param \phpbb\plupload\plupload $plupload Plupload */ - public function __construct(\phpbb\filesystem\filesystem_interface $phpbb_filesystem, language $language, \phpbb\php\ini $php_ini, $phpbb_root_path, \phpbb\mimetype\guesser $mimetype_guesser = null, \phpbb\plupload\plupload $plupload = null) + public function __construct(\phpbb\filesystem\filesystem_interface $phpbb_filesystem, language $language, \bantu\IniGetWrapper\IniGetWrapper $php_ini, $phpbb_root_path, \phpbb\mimetype\guesser $mimetype_guesser = null, \phpbb\plupload\plupload $plupload = null) { $this->filesystem = $phpbb_filesystem; $this->language = $language; @@ -417,7 +417,7 @@ class filespec return false; } - $upload_mode = ($this->php_ini->get_bool('open_basedir') || $this->php_ini->get_bool('safe_mode')) ? 'move' : 'copy'; + $upload_mode = ($this->php_ini->getBool('open_basedir') || $this->php_ini->getBool('safe_mode')) ? 'move' : 'copy'; $upload_mode = ($this->local) ? 'local' : $upload_mode; $this->destination_file = $this->destination_path . '/' . utf8_basename($this->realname); diff --git a/phpBB/phpbb/files/upload.php b/phpBB/phpbb/files/upload.php index 036f216d22..328dd49a06 100644 --- a/phpBB/phpbb/files/upload.php +++ b/phpBB/phpbb/files/upload.php @@ -56,7 +56,7 @@ class upload /** @var \phpbb\files\factory Files factory */ protected $factory; - /** @var \phpbb\php\ini ini_get() wrapper */ + /** @var \bantu\IniGetWrapper\IniGetWrapper ini_get() wrapper */ protected $php_ini; /** @var \phpbb\language\language Language class */ @@ -74,11 +74,11 @@ class upload * @param filesystem_interface $filesystem * @param factory $factory Files factory * @param language $language Language class - * @param \phpbb\php\ini $php_ini ini_get() wrapper + * @param \bantu\IniGetWrapper\IniGetWrapper $php_ini ini_get() wrapper * @param request_interface $request Request class * @param string $phpbb_root_path phpBB root path */ - public function __construct(filesystem_interface $filesystem, factory $factory, language $language, \phpbb\php\ini $php_ini, request_interface $request, $phpbb_root_path) + public function __construct(filesystem_interface $filesystem, factory $factory, language $language, \bantu\IniGetWrapper\IniGetWrapper $php_ini, request_interface $request, $phpbb_root_path) { $this->filesystem = $filesystem; $this->factory = $factory; @@ -216,7 +216,7 @@ class upload switch ($errorcode) { case UPLOAD_ERR_INI_SIZE: - $max_filesize = $this->php_ini->get_string('upload_max_filesize'); + $max_filesize = $this->php_ini->getString('upload_max_filesize'); $unit = 'MB'; if (!empty($max_filesize)) diff --git a/phpBB/phpbb/install/helper/config.php b/phpBB/phpbb/install/helper/config.php index b0480e7e5b..d5653f1924 100644 --- a/phpBB/phpbb/install/helper/config.php +++ b/phpBB/phpbb/install/helper/config.php @@ -41,7 +41,7 @@ class config protected $install_config_file; /** - * @var \phpbb\php\ini + * @var \bantu\IniGetWrapper\IniGetWrapper */ protected $php_ini; @@ -83,7 +83,7 @@ class config /** * Constructor */ - public function __construct(\phpbb\filesystem\filesystem_interface $filesystem, \phpbb\php\ini $php_ini, $phpbb_root_path) + public function __construct(\phpbb\filesystem\filesystem_interface $filesystem, \bantu\IniGetWrapper\IniGetWrapper $php_ini, $phpbb_root_path) { $this->filesystem = $filesystem; $this->php_ini = $php_ini; @@ -373,7 +373,7 @@ class config protected function setup_system_data() { // Query maximum runtime from php.ini - $execution_time = $this->php_ini->get_int('max_execution_time'); + $execution_time = $this->php_ini->getNumeric('max_execution_time'); $execution_time = min(15, $execution_time / 2); $this->system_data['max_execution_time'] = $execution_time; @@ -381,6 +381,6 @@ class config $this->system_data['start_time'] = time(); // Get memory limit - $this->system_data['memory_limit'] = $this->php_ini->get_bytes('memory_limit'); + $this->system_data['memory_limit'] = $this->php_ini->getBytes('memory_limit'); } } diff --git a/phpBB/phpbb/plupload/plupload.php b/phpBB/phpbb/plupload/plupload.php index 35f3a0071e..0e67ee209b 100644 --- a/phpBB/phpbb/plupload/plupload.php +++ b/phpBB/phpbb/plupload/plupload.php @@ -39,7 +39,7 @@ class plupload protected $user; /** - * @var \phpbb\php\ini + * @var \bantu\IniGetWrapper\IniGetWrapper */ protected $php_ini; @@ -67,10 +67,10 @@ class plupload * @param \phpbb\config\config $config * @param \phpbb\request\request_interface $request * @param \phpbb\user $user - * @param \phpbb\php\ini $php_ini + * @param \bantu\IniGetWrapper\IniGetWrapper $php_ini * @param \phpbb\mimetype\guesser $mimetype_guesser */ - public function __construct($phpbb_root_path, \phpbb\config\config $config, \phpbb\request\request_interface $request, \phpbb\user $user, \phpbb\php\ini $php_ini, \phpbb\mimetype\guesser $mimetype_guesser) + public function __construct($phpbb_root_path, \phpbb\config\config $config, \phpbb\request\request_interface $request, \phpbb\user $user, \bantu\IniGetWrapper\IniGetWrapper $php_ini, \phpbb\mimetype\guesser $mimetype_guesser) { $this->phpbb_root_path = $phpbb_root_path; $this->config = $config; @@ -284,9 +284,9 @@ class plupload public function get_chunk_size() { $max = min( - $this->php_ini->get_bytes('upload_max_filesize'), - $this->php_ini->get_bytes('post_max_size'), - max(1, $this->php_ini->get_bytes('memory_limit')), + $this->php_ini->getBytes('upload_max_filesize'), + $this->php_ini->getBytes('post_max_size'), + max(1, $this->php_ini->getBytes('memory_limit')), $this->config['max_filesize'] ); diff --git a/tests/cache/apc_driver_test.php b/tests/cache/apc_driver_test.php index 10130b465b..706f274448 100644 --- a/tests/cache/apc_driver_test.php +++ b/tests/cache/apc_driver_test.php @@ -34,14 +34,14 @@ class phpbb_cache_apc_driver_test extends phpbb_cache_common_test_case self::markTestSkipped('APC extension is not loaded'); } - $php_ini = new \phpbb\php\ini; + $php_ini = new \bantu\IniGetWrapper\IniGetWrapper; - if (!$php_ini->get_bool('apc.enabled')) + if (!$php_ini->getBool('apc.enabled')) { self::markTestSkipped('APC is not enabled. Make sure apc.enabled=1 in php.ini'); } - if (PHP_SAPI == 'cli' && !$php_ini->get_bool('apc.enable_cli')) + if (PHP_SAPI == 'cli' && !$php_ini->getBool('apc.enable_cli')) { self::markTestSkipped('APC is not enabled for CLI. Set apc.enable_cli=1 in php.ini'); } diff --git a/tests/files/upload_test.php b/tests/files/upload_test.php index 6f4a10e6aa..da9d78afbc 100644 --- a/tests/files/upload_test.php +++ b/tests/files/upload_test.php @@ -23,7 +23,7 @@ class phpbb_files_upload_test extends phpbb_test_case /** @var \phpbb\files\factory */ protected $factory; - /** @var \phpbb\php\ini */ + /** @var \bantu\IniGetWrapper\IniGetWrapper */ protected $php_ini; /** @var \phpbb\language\language */ @@ -52,7 +52,7 @@ class phpbb_files_upload_test extends phpbb_test_case $this->filesystem = new \phpbb\filesystem\filesystem(); $this->language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)); - $this->php_ini = new \phpbb\php\ini; + $this->php_ini = new \bantu\IniGetWrapper\IniGetWrapper; $this->container = new phpbb_mock_container_builder($phpbb_root_path, $phpEx); $this->container->set('files.filespec', new \phpbb\files\filespec( diff --git a/tests/functional/fileupload_remote_test.php b/tests/functional/fileupload_remote_test.php index 4fd8ae1f19..6752ff01f4 100644 --- a/tests/functional/fileupload_remote_test.php +++ b/tests/functional/fileupload_remote_test.php @@ -22,7 +22,7 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case /** @var \phpbb\files\factory */ protected $factory; - /** @var \phpbb\php\ini */ + /** @var \bantu\IniGetWrapper\IniGetWrapper */ protected $php_ini; /** @var \phpbb\language\language */ @@ -54,7 +54,7 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case $this->filesystem = new \phpbb\filesystem\filesystem(); $this->language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)); $this->request = $this->getMock('\phpbb\request\request'); - $this->php_ini = new \phpbb\php\ini; + $this->php_ini = new \bantu\IniGetWrapper\IniGetWrapper; $container = new phpbb_mock_container_builder(); $container->set('files.filespec', new \phpbb\files\filespec($this->filesystem, $this->language, $this->php_ini, $this->phpbb_root_path)); diff --git a/tests/installer/installer_config_test.php b/tests/installer/installer_config_test.php index c7334ebd93..7de23aea36 100644 --- a/tests/installer/installer_config_test.php +++ b/tests/installer/installer_config_test.php @@ -24,11 +24,11 @@ class phpbb_installer_config_test extends phpbb_test_case { $phpbb_root_path = __DIR__ . './../../phpBB/'; $filesystem = $this->getMock('\phpbb\filesystem\filesystem'); - $php_ini = $this->getMockBuilder('\phpbb\php\ini') + $php_ini = $this->getMockBuilder('\bantu\IniGetWrapper\IniGetWrapper') ->getMock(); - $php_ini->method('get_int') + $php_ini->method('getInt') ->willReturn(-1); - $php_ini->method('get_bytes') + $php_ini->method('getBytes') ->willReturn(-1); $this->config = new config($filesystem, $php_ini, $phpbb_root_path); diff --git a/tests/installer/module_base_test.php b/tests/installer/module_base_test.php index fd92c9b674..9578010047 100644 --- a/tests/installer/module_base_test.php +++ b/tests/installer/module_base_test.php @@ -43,7 +43,7 @@ class module_base_test extends phpbb_test_case $this->module = new test_installer_module($module_collection, true, false); $iohandler = $this->getMock('\phpbb\install\helper\iohandler\iohandler_interface'); - $config = new \phpbb\install\helper\config(new \phpbb\filesystem\filesystem(), new \phpbb\php\ini(), '', 'php'); + $config = new \phpbb\install\helper\config(new \phpbb\filesystem\filesystem(), new \bantu\IniGetWrapper\IniGetWrapper(), '', 'php'); $this->module->setup($config, $iohandler); } diff --git a/tests/plupload/plupload_test.php b/tests/plupload/plupload_test.php index aa7793567d..3312f4d0a0 100644 --- a/tests/plupload/plupload_test.php +++ b/tests/plupload/plupload_test.php @@ -48,7 +48,7 @@ class phpbb_plupload_test extends phpbb_test_case $config, new phpbb_mock_request, new \phpbb\user($lang, '\phpbb\datetime'), - new \phpbb\php\ini, + new \bantu\IniGetWrapper\IniGetWrapper, new \phpbb\mimetype\guesser(array(new \phpbb\mimetype\extension_guesser)) ); diff --git a/tests/upload/filespec_test.php b/tests/upload/filespec_test.php index 595439c917..a53c27f045 100644 --- a/tests/upload/filespec_test.php +++ b/tests/upload/filespec_test.php @@ -93,7 +93,7 @@ class phpbb_filespec_test extends phpbb_test_case 'error' => '', ); - $filespec = new \phpbb\files\filespec($this->filesystem, $this->language, new \phpbb\php\ini, $this->phpbb_root_path, $this->mimetype_guesser); + $filespec = new \phpbb\files\filespec($this->filesystem, $this->language, new \bantu\IniGetWrapper\IniGetWrapper, $this->phpbb_root_path, $this->mimetype_guesser); return $filespec->set_upload_ary(array_merge($upload_ary, $override)); } @@ -114,7 +114,7 @@ class phpbb_filespec_test extends phpbb_test_case public function test_empty_upload_ary() { - $filespec = new \phpbb\files\filespec($this->filesystem, $this->language, new \phpbb\php\ini, $this->phpbb_root_path, $this->mimetype_guesser); + $filespec = new \phpbb\files\filespec($this->filesystem, $this->language, new \bantu\IniGetWrapper\IniGetWrapper, $this->phpbb_root_path, $this->mimetype_guesser); $this->assertInstanceOf('\phpbb\files\filespec', $filespec->set_upload_ary(array())); $this->assertTrue($filespec->init_error()); } @@ -262,7 +262,7 @@ class phpbb_filespec_test extends phpbb_test_case */ public function test_clean_filename_avatar($filename, $expected, $mode, $prefix = '', $user_id = '') { - $filespec = new \phpbb\files\filespec($this->filesystem, $this->language, new \phpbb\php\ini, $this->phpbb_root_path, $this->mimetype_guesser); + $filespec = new \phpbb\files\filespec($this->filesystem, $this->language, new \bantu\IniGetWrapper\IniGetWrapper, $this->phpbb_root_path, $this->mimetype_guesser); if ($filename) { @@ -419,7 +419,7 @@ class phpbb_filespec_test extends phpbb_test_case public function test_is_uploaded() { - $filespec = new \phpbb\files\filespec($this->filesystem, $this->language, new \phpbb\php\ini, $this->phpbb_root_path, null); + $filespec = new \phpbb\files\filespec($this->filesystem, $this->language, new \bantu\IniGetWrapper\IniGetWrapper, $this->phpbb_root_path, null); $reflection_filespec = new ReflectionClass($filespec); $plupload_property = $reflection_filespec->getProperty('plupload'); $plupload_property->setAccessible(true); diff --git a/tests/upload/fileupload_test.php b/tests/upload/fileupload_test.php index 40f8704271..43106599e4 100644 --- a/tests/upload/fileupload_test.php +++ b/tests/upload/fileupload_test.php @@ -27,7 +27,7 @@ class phpbb_fileupload_test extends phpbb_test_case /** @var \phpbb\files\factory */ protected $factory; - /** @var \phpbb\php\ini */ + /** @var \bantu\IniGetWrapper\IniGetWrapper */ protected $php_ini; /** @var \phpbb\language\language */ @@ -53,7 +53,7 @@ class phpbb_fileupload_test extends phpbb_test_case $config['rand_seed_last_update'] = time() + 600; $this->request = $this->getMock('\phpbb\request\request'); - $this->php_ini = new \phpbb\php\ini; + $this->php_ini = new \bantu\IniGetWrapper\IniGetWrapper; $this->filesystem = new \phpbb\filesystem\filesystem(); $this->language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)); diff --git a/tests/wrapper/phpbb_php_ini_fake.php b/tests/wrapper/phpbb_php_ini_fake.php index f218ff6d03..300ce30cfe 100644 --- a/tests/wrapper/phpbb_php_ini_fake.php +++ b/tests/wrapper/phpbb_php_ini_fake.php @@ -11,7 +11,7 @@ * */ -class phpbb_php_ini_fake extends \phpbb\php\ini +class phpbb_php_ini_fake extends \bantu\IniGetWrapper\IniGetWrapper { function get($varname) { diff --git a/tests/wrapper/phpbb_php_ini_test.php b/tests/wrapper/phpbb_php_ini_test.php index 27c2487f0a..8ef63757a5 100644 --- a/tests/wrapper/phpbb_php_ini_test.php +++ b/tests/wrapper/phpbb_php_ini_test.php @@ -16,6 +16,7 @@ require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; class phpbb_wrapper_phpbb_php_ini_test extends phpbb_test_case { + /** @var \phpbb_php_ini_fake php_ini */ protected $php_ini; public function setUp() @@ -25,44 +26,37 @@ class phpbb_wrapper_phpbb_php_ini_test extends phpbb_test_case public function test_get_string() { - $this->assertSame(false, $this->php_ini->get_string(false)); - $this->assertSame('phpbb', $this->php_ini->get_string(' phpbb ')); + $this->assertSame('', $this->php_ini->getString(false)); + $this->assertSame('phpbb', $this->php_ini->getString(' phpbb ')); } public function test_get_bool() { - $this->assertSame(true, $this->php_ini->get_bool('ON')); - $this->assertSame(true, $this->php_ini->get_bool('on')); - $this->assertSame(true, $this->php_ini->get_bool('1')); + $this->assertSame(true, $this->php_ini->getBool('ON')); + $this->assertSame(true, $this->php_ini->getBool('on')); + $this->assertSame(true, $this->php_ini->getBool('1')); - $this->assertSame(false, $this->php_ini->get_bool('OFF')); - $this->assertSame(false, $this->php_ini->get_bool('off')); - $this->assertSame(false, $this->php_ini->get_bool('0')); - $this->assertSame(false, $this->php_ini->get_bool('')); + $this->assertSame(false, $this->php_ini->getBool('OFF')); + $this->assertSame(false, $this->php_ini->getBool('off')); + $this->assertSame(false, $this->php_ini->getBool('0')); + $this->assertSame(false, $this->php_ini->getBool('')); } public function test_get_int() { - $this->assertSame(1234, $this->php_ini->get_int('1234')); - $this->assertSame(-12345, $this->php_ini->get_int('-12345')); - $this->assertSame(false, $this->php_ini->get_int('phpBB')); - } - - public function test_get_float() - { - $this->assertSame(1234.0, $this->php_ini->get_float('1234')); - $this->assertSame(-12345.0, $this->php_ini->get_float('-12345')); - $this->assertSame(false, $this->php_ini->get_float('phpBB')); + $this->assertSame(1234, $this->php_ini->getNumeric('1234')); + $this->assertSame(-12345, $this->php_ini->getNumeric('-12345')); + $this->assertSame(null, $this->php_ini->getNumeric('phpBB')); } public function test_get_bytes_invalid() { - $this->assertSame(false, $this->php_ini->get_bytes(false)); - $this->assertSame(false, $this->php_ini->get_bytes('phpBB')); - $this->assertSame(false, $this->php_ini->get_bytes('k')); - $this->assertSame(false, $this->php_ini->get_bytes('-k')); - $this->assertSame(false, $this->php_ini->get_bytes('M')); - $this->assertSame(false, $this->php_ini->get_bytes('-M')); + $this->assertSame(null, $this->php_ini->getBytes(false)); + $this->assertSame(null, $this->php_ini->getBytes('phpBB')); + $this->assertSame(null, $this->php_ini->getBytes('k')); + $this->assertSame(null, $this->php_ini->getBytes('-k')); + $this->assertSame(null, $this->php_ini->getBytes('M')); + $this->assertSame(null, $this->php_ini->getBytes('-M')); } /** @@ -70,7 +64,7 @@ class phpbb_wrapper_phpbb_php_ini_test extends phpbb_test_case */ public function test_get_bytes($expected, $value) { - $actual = $this->php_ini->get_bytes($value); + $actual = $this->php_ini->getBytes($value); $this->assertTrue(is_float($actual) || is_int($actual)); $this->assertEquals($expected, $actual); From b29b62debe4077ec688ebee9e9363db2c8614dd5 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Thu, 27 Aug 2015 12:48:06 +0200 Subject: [PATCH 51/66] [ticket/13904] Use ini_get() wrapper in file upload types PHPBB3-13904 --- .../default/container/services_files.yml | 3 +++ phpBB/phpbb/files/types/base.php | 5 +++- phpBB/phpbb/files/types/form.php | 10 +++++-- phpBB/phpbb/files/types/local.php | 26 ++++++++----------- phpBB/phpbb/files/types/remote.php | 12 ++++++--- tests/functional/fileupload_remote_test.php | 2 +- tests/upload/fileupload_test.php | 2 ++ 7 files changed, 38 insertions(+), 22 deletions(-) diff --git a/phpBB/config/default/container/services_files.yml b/phpBB/config/default/container/services_files.yml index 25cd532ae3..a4136a0573 100644 --- a/phpBB/config/default/container/services_files.yml +++ b/phpBB/config/default/container/services_files.yml @@ -35,6 +35,7 @@ services: arguments: - @files.factory - @language + - @php_ini - @plupload - @request @@ -44,6 +45,7 @@ services: arguments: - @files.factory - @language + - @php_ini - @request files.types.remote: @@ -52,5 +54,6 @@ services: arguments: - @files.factory - @language + - @php_ini - @request - %core.root_path% diff --git a/phpBB/phpbb/files/types/base.php b/phpBB/phpbb/files/types/base.php index 1ed06053a4..3313ad040b 100644 --- a/phpBB/phpbb/files/types/base.php +++ b/phpBB/phpbb/files/types/base.php @@ -18,6 +18,9 @@ abstract class base implements type_interface /** @var \phpbb\language\language */ protected $language; + /** @var \bantu\IniGetWrapper\IniGetWrapper */ + protected $php_ini; + /** @var \phpbb\files\upload */ protected $upload; @@ -33,7 +36,7 @@ abstract class base implements type_interface // PHP Upload filesize exceeded if ($file->get('filename') == 'none') { - $max_filesize = @ini_get('upload_max_filesize'); + $max_filesize = $this->php_ini->getString('upload_max_filesize'); $unit = 'MB'; if (!empty($max_filesize)) diff --git a/phpBB/phpbb/files/types/form.php b/phpBB/phpbb/files/types/form.php index be0a425d57..b8ad62f58b 100644 --- a/phpBB/phpbb/files/types/form.php +++ b/phpBB/phpbb/files/types/form.php @@ -13,6 +13,7 @@ namespace phpbb\files\types; +use \bantu\IniGetWrapper\IniGetWrapper; use \phpbb\files\factory; use \phpbb\files\filespec; use \phpbb\files\upload; @@ -28,6 +29,9 @@ class form extends base /** @var language */ protected $language; + /** @var IniGetWrapper */ + protected $php_ini; + /** @var plupload */ protected $plupload; @@ -42,13 +46,15 @@ class form extends base * * @param factory $factory Files factory * @param language $language Language class + * @param IniGetWrapper $php_ini ini_get() wrapper * @param plupload $plupload Plupload * @param request_interface $request Request object */ - public function __construct(factory $factory, language $language, plupload $plupload, request_interface $request) + public function __construct(factory $factory, language $language, IniGetWrapper $php_ini, plupload $plupload, request_interface $request) { $this->factory = $factory; $this->language = $language; + $this->php_ini = $php_ini; $this->plupload = $plupload; $this->request = $request; } @@ -122,7 +128,7 @@ class form extends base return $file; } - // PHP Upload filesize check + // PHP Upload file size check $file = $this->check_upload_size($file); if (sizeof($file->error)) { diff --git a/phpBB/phpbb/files/types/local.php b/phpBB/phpbb/files/types/local.php index dfaabded9b..96bfdaca7f 100644 --- a/phpBB/phpbb/files/types/local.php +++ b/phpBB/phpbb/files/types/local.php @@ -13,6 +13,7 @@ namespace phpbb\files\types; +use \bantu\IniGetWrapper\IniGetWrapper; use \phpbb\files\factory; use \phpbb\files\filespec; use \phpbb\language\language; @@ -26,6 +27,9 @@ class local extends base /** @var language */ protected $language; + /** @var IniGetWrapper */ + protected $php_ini; + /** @var request_interface */ protected $request; @@ -37,12 +41,14 @@ class local extends base * * @param factory $factory Files factory * @param language $language Language class + * @param IniGetWrapper $php_ini ini_get() wrapper * @param request_interface $request Request object */ - public function __construct(factory $factory, language $language, request_interface $request) + public function __construct(factory $factory, language $language, IniGetWrapper $php_ini, request_interface $request) { $this->factory = $factory; $this->language = $language; + $this->php_ini = $php_ini; $this->request = $request; } @@ -89,21 +95,11 @@ class local extends base } } - // PHP Upload filesize exceeded - if ($file->get('filename') == 'none') + // PHP Upload file size check + $this->check_upload_size($file); + $file = $this->check_upload_size($file); + if (sizeof($file->error)) { - $max_filesize = @ini_get('upload_max_filesize'); - $unit = 'MB'; - - if (!empty($max_filesize)) - { - $unit = strtolower(substr($max_filesize, -1, 1)); - $max_filesize = (int) $max_filesize; - - $unit = ($unit == 'k') ? 'KB' : (($unit == 'g') ? 'GB' : 'MB'); - } - - $file->error[] = (empty($max_filesize)) ?$this->language->lang($this->upload->error_prefix . 'PHP_SIZE_NA') : $this->language->lang($this->upload->error_prefix . 'PHP_SIZE_OVERRUN', $max_filesize, $this->language->lang($unit)); return $file; } diff --git a/phpBB/phpbb/files/types/remote.php b/phpBB/phpbb/files/types/remote.php index 94bba8ad46..bad87243a5 100644 --- a/phpBB/phpbb/files/types/remote.php +++ b/phpBB/phpbb/files/types/remote.php @@ -13,6 +13,7 @@ namespace phpbb\files\types; +use \bantu\IniGetWrapper\IniGetWrapper; use \phpbb\files\factory; use \phpbb\files\filespec; use \phpbb\language\language; @@ -26,6 +27,9 @@ class remote extends base /** @var language */ protected $language; + /** @var IniGetWrapper */ + protected $php_ini; + /** @var request_interface */ protected $request; @@ -40,13 +44,15 @@ class remote extends base * * @param factory $factory Files factory * @param language $language Language class + * @param IniGetWrapper $php_ini ini_get() wrapper * @param request_interface $request Request object * @param string $phpbb_root_path phpBB root path */ - public function __construct(factory $factory, language $language, request_interface $request, $phpbb_root_path) + public function __construct(factory $factory, language $language, IniGetWrapper $php_ini, request_interface $request, $phpbb_root_path) { $this->factory = $factory; $this->language = $language; + $this->php_ini = $php_ini; $this->request = $request; $this->phpbb_root_path = $phpbb_root_path; } @@ -199,7 +205,7 @@ class remote extends base return $this->factory->get('filespec')->set_error($this->upload->error_prefix . 'EMPTY_REMOTE_DATA'); } - $tmp_path = (!@ini_get('safe_mode') || strtolower(@ini_get('safe_mode')) == 'off') ? false : $this->phpbb_root_path . 'cache'; + $tmp_path = (!$this->php_ini->getBool('safe_mode')) ? false : $this->phpbb_root_path . 'cache'; $filename = tempnam($tmp_path, unique_id() . '-'); if (!($fp = @fopen($filename, 'wb'))) @@ -232,7 +238,7 @@ class remote extends base $max_file_size = $this->upload->max_filesize; if (!$max_file_size) { - $max_file_size = @ini_get('upload_max_filesize'); + $max_file_size = $this->php_ini->getString('upload_max_filesize'); if (!empty($max_filesize)) { diff --git a/tests/functional/fileupload_remote_test.php b/tests/functional/fileupload_remote_test.php index 6752ff01f4..29701e072a 100644 --- a/tests/functional/fileupload_remote_test.php +++ b/tests/functional/fileupload_remote_test.php @@ -60,7 +60,7 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case $container->set('files.filespec', new \phpbb\files\filespec($this->filesystem, $this->language, $this->php_ini, $this->phpbb_root_path)); $this->factory = new \phpbb\files\factory($container); $container->set('files.factory', $this->factory); - $container->set('files.types.remote', new \phpbb\files\types\remote($this->factory, $this->language, $this->request, $phpbb_root_path)); + $container->set('files.types.remote', new \phpbb\files\types\remote($this->factory, $this->language, $this->php_ini, $this->request, $phpbb_root_path)); $this->phpbb_root_path = $phpbb_root_path; } diff --git a/tests/upload/fileupload_test.php b/tests/upload/fileupload_test.php index 43106599e4..8675567e22 100644 --- a/tests/upload/fileupload_test.php +++ b/tests/upload/fileupload_test.php @@ -81,12 +81,14 @@ class phpbb_fileupload_test extends phpbb_test_case $this->container->set('files.types.form', new \phpbb\files\types\form( $this->factory, $this->language, + $this->php_ini, $plupload, $this->request ), phpbb_mock_container_builder::SCOPE_PROTOTYPE); $this->container->set('files.types.local', new \phpbb\files\types\local( $this->factory, $this->language, + $this->php_ini, $this->request ), phpbb_mock_container_builder::SCOPE_PROTOTYPE); From c3ccd423d2a6f3aab1183b22177df6f605f33371 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Tue, 1 Sep 2015 10:58:35 +0200 Subject: [PATCH 52/66] [ticket/13904] Add back tests for retrieving floats PHPBB3-13904 --- tests/wrapper/phpbb_php_ini_test.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/wrapper/phpbb_php_ini_test.php b/tests/wrapper/phpbb_php_ini_test.php index 8ef63757a5..9f46a78d5b 100644 --- a/tests/wrapper/phpbb_php_ini_test.php +++ b/tests/wrapper/phpbb_php_ini_test.php @@ -49,6 +49,13 @@ class phpbb_wrapper_phpbb_php_ini_test extends phpbb_test_case $this->assertSame(null, $this->php_ini->getNumeric('phpBB')); } + public function test_get_float() + { + $this->assertSame(1234.0, $this->php_ini->getNumeric('1234.0')); + $this->assertSame(-12345.0, $this->php_ini->getNumeric('-12345.0')); + $this->assertSame(null, $this->php_ini->getNumeric('phpBB')); + } + public function test_get_bytes_invalid() { $this->assertSame(null, $this->php_ini->getBytes(false)); From 591995267a3f1931131fa630bd3ff120476f881f Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Thu, 3 Sep 2015 17:20:54 +0200 Subject: [PATCH 53/66] [ticket/13904] Improve test coverage of filespec class PHPBB3-13904 --- .../default/container/services_files.yml | 1 + phpBB/phpbb/files/filespec.php | 11 +- tests/files/upload_test.php | 1 + tests/functional/fileupload_remote_test.php | 2 +- tests/upload/filespec_test.php | 119 +++++++++++++++++- tests/upload/fileupload_test.php | 3 +- 6 files changed, 126 insertions(+), 11 deletions(-) diff --git a/phpBB/config/default/container/services_files.yml b/phpBB/config/default/container/services_files.yml index a4136a0573..cfdade37df 100644 --- a/phpBB/config/default/container/services_files.yml +++ b/phpBB/config/default/container/services_files.yml @@ -14,6 +14,7 @@ services: - @filesystem - @language - @php_ini + - @upload_imagesize - %core.root_path% - @mimetype.guesser - @plupload diff --git a/phpBB/phpbb/files/filespec.php b/phpBB/phpbb/files/filespec.php index 580016b281..6ce54a4789 100644 --- a/phpBB/phpbb/files/filespec.php +++ b/phpBB/phpbb/files/filespec.php @@ -75,6 +75,9 @@ class filespec /** @var \bantu\IniGetWrapper\IniGetWrapper ini_get() wrapper class */ protected $php_ini; + /** @var \fastImageSize\fastImageSize */ + protected $imagesize; + /** @var language Language class */ protected $language; @@ -97,11 +100,12 @@ class filespec * @param \phpbb\mimetype\guesser $mimetype_guesser Mime type guesser * @param \phpbb\plupload\plupload $plupload Plupload */ - public function __construct(\phpbb\filesystem\filesystem_interface $phpbb_filesystem, language $language, \bantu\IniGetWrapper\IniGetWrapper $php_ini, $phpbb_root_path, \phpbb\mimetype\guesser $mimetype_guesser = null, \phpbb\plupload\plupload $plupload = null) + public function __construct(\phpbb\filesystem\filesystem_interface $phpbb_filesystem, language $language, \bantu\IniGetWrapper\IniGetWrapper $php_ini, \fastImageSize\fastImageSize $imagesize, $phpbb_root_path, \phpbb\mimetype\guesser $mimetype_guesser = null, \phpbb\plupload\plupload $plupload = null) { $this->filesystem = $phpbb_filesystem; $this->language = $language; $this->php_ini = $php_ini; + $this->imagesize = $imagesize; $this->phpbb_root_path = $phpbb_root_path; $this->plupload = $plupload; $this->mimetype_guesser = $mimetype_guesser; @@ -500,10 +504,7 @@ class filespec { $this->width = $this->height = 0; - // Get imagesize class - $imagesize = new \fastImageSize\fastImageSize(); - - $this->image_info = $imagesize->getImageSize($this->destination_file, $this->mimetype); + $this->image_info = $this->imagesize->getImageSize($this->destination_file, $this->mimetype); if ($this->image_info !== false) { diff --git a/tests/files/upload_test.php b/tests/files/upload_test.php index da9d78afbc..0d02926702 100644 --- a/tests/files/upload_test.php +++ b/tests/files/upload_test.php @@ -59,6 +59,7 @@ class phpbb_files_upload_test extends phpbb_test_case $this->filesystem, $this->language, $this->php_ini, + new \fastImageSize\fastImageSize(), $phpbb_root_path, new \phpbb\mimetype\guesser(array( 'mimetype.extension_guesser' => new \phpbb\mimetype\extension_guesser(), diff --git a/tests/functional/fileupload_remote_test.php b/tests/functional/fileupload_remote_test.php index 29701e072a..1484acc4d5 100644 --- a/tests/functional/fileupload_remote_test.php +++ b/tests/functional/fileupload_remote_test.php @@ -57,7 +57,7 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case $this->php_ini = new \bantu\IniGetWrapper\IniGetWrapper; $container = new phpbb_mock_container_builder(); - $container->set('files.filespec', new \phpbb\files\filespec($this->filesystem, $this->language, $this->php_ini, $this->phpbb_root_path)); + $container->set('files.filespec', new \phpbb\files\filespec($this->filesystem, $this->language, $this->php_ini, new \fastImageSize\fastImageSize(), $this->phpbb_root_path)); $this->factory = new \phpbb\files\factory($container); $container->set('files.factory', $this->factory); $container->set('files.types.remote', new \phpbb\files\types\remote($this->factory, $this->language, $this->php_ini, $this->request, $phpbb_root_path)); diff --git a/tests/upload/filespec_test.php b/tests/upload/filespec_test.php index a53c27f045..10a442cd1d 100644 --- a/tests/upload/filespec_test.php +++ b/tests/upload/filespec_test.php @@ -93,7 +93,7 @@ class phpbb_filespec_test extends phpbb_test_case 'error' => '', ); - $filespec = new \phpbb\files\filespec($this->filesystem, $this->language, new \bantu\IniGetWrapper\IniGetWrapper, $this->phpbb_root_path, $this->mimetype_guesser); + $filespec = new \phpbb\files\filespec($this->filesystem, $this->language, new \bantu\IniGetWrapper\IniGetWrapper, new \fastImageSize\fastImageSize(), $this->phpbb_root_path, $this->mimetype_guesser); return $filespec->set_upload_ary(array_merge($upload_ary, $override)); } @@ -114,7 +114,7 @@ class phpbb_filespec_test extends phpbb_test_case public function test_empty_upload_ary() { - $filespec = new \phpbb\files\filespec($this->filesystem, $this->language, new \bantu\IniGetWrapper\IniGetWrapper, $this->phpbb_root_path, $this->mimetype_guesser); + $filespec = new \phpbb\files\filespec($this->filesystem, $this->language, new \bantu\IniGetWrapper\IniGetWrapper, new \fastImageSize\fastImageSize(), $this->phpbb_root_path, $this->mimetype_guesser); $this->assertInstanceOf('\phpbb\files\filespec', $filespec->set_upload_ary(array())); $this->assertTrue($filespec->init_error()); } @@ -262,7 +262,7 @@ class phpbb_filespec_test extends phpbb_test_case */ public function test_clean_filename_avatar($filename, $expected, $mode, $prefix = '', $user_id = '') { - $filespec = new \phpbb\files\filespec($this->filesystem, $this->language, new \bantu\IniGetWrapper\IniGetWrapper, $this->phpbb_root_path, $this->mimetype_guesser); + $filespec = new \phpbb\files\filespec($this->filesystem, $this->language, new \bantu\IniGetWrapper\IniGetWrapper, new \fastImageSize\fastImageSize(), $this->phpbb_root_path, $this->mimetype_guesser); if ($filename) { @@ -403,6 +403,117 @@ class phpbb_filespec_test extends phpbb_test_case $this->assertFalse($filespec->move_file('foo')); } + public function data_move_file_copy() + { + return array( + array('gif_copy', true, false, array()), + array('gif_copy', true, true, array()), + array('foo_bar', false, false, array('GENERAL_UPLOAD_ERROR')), + array('foo_bar', false, true, array('GENERAL_UPLOAD_ERROR')), + ); + } + + /** + * @dataProvider data_move_file_copy + */ + public function test_move_file_copy($tmp_name, $move_success, $safe_mode_on, $expected_error) + { + // Initialise a blank filespec object for use with trivial methods + $upload_ary = array( + 'name' => 'gif_moved', + 'type' => 'image/gif', + 'size' => '', + 'tmp_name' => $this->path . 'copies/' . $tmp_name, + 'error' => '', + ); + + $php_ini = $this->getMockBuilder('\bantu\IniGetWrapper\IniGetWrapper') + ->getMock(); + $php_ini->expects($this->any()) + ->method('getBool') + ->with($this->anything()) + ->willReturn($safe_mode_on); + $upload = new phpbb_mock_fileupload(); + $upload->max_filesize = self::UPLOAD_MAX_FILESIZE; + $filespec = new \phpbb\files\filespec($this->filesystem, $this->language, $php_ini, new \fastImageSize\fastImagesize, '', $this->mimetype_guesser); + $filespec->set_upload_ary($upload_ary); + $filespec->local = false; + $filespec->extension = 'gif'; + $filespec->set_upload_namespace($upload); + + $this->assertEquals($move_success, $filespec->move_file($this->path . 'copies')); + $this->assertEquals($filespec->file_moved, file_exists($this->path . 'copies/gif_moved')); + $this->assertSame($expected_error, $filespec->error); + } + + public function data_move_file_imagesize() + { + return array( + array( + array( + 'width' => 2, + 'height' => 2, + 'type' => IMAGETYPE_GIF, + ), + array() + ), + array( + array( + 'width' => 2, + 'height' => 2, + 'type' => -1, + ), + array('Image file type -1 for mimetype image/gif not supported.') + ), + array( + array( + 'width' => 0, + 'height' => 0, + 'type' => IMAGETYPE_GIF, + ), + array('The image file you tried to attach is invalid.') + ), + array( + false, + array('It was not possible to determine the dimensions of the image. Please verify that the URL you entered is correct.') + ) + ); + } + + /** + * @dataProvider data_move_file_imagesize + */ + public function test_move_file_imagesize($imagesize_return, $expected_error) + { + // Initialise a blank filespec object for use with trivial methods + $upload_ary = array( + 'name' => 'gif_moved', + 'type' => 'image/gif', + 'size' => '', + 'tmp_name' => $this->path . 'copies/gif_copy', + 'error' => '', + ); + + $imagesize = $this->getMockBuilder('\fastImageSize\fastImageSize') + ->getMock(); + $imagesize->expects($this->any()) + ->method('getImageSize') + ->with($this->anything()) + ->willReturn($imagesize_return); + + $upload = new phpbb_mock_fileupload(); + $upload->max_filesize = self::UPLOAD_MAX_FILESIZE; + $filespec = new \phpbb\files\filespec($this->filesystem, $this->language, new \bantu\IniGetWrapper\IniGetWrapper, $imagesize, '', $this->mimetype_guesser); + $filespec->set_upload_ary($upload_ary); + $filespec->local = false; + $filespec->extension = 'gif'; + $filespec->set_upload_namespace($upload); + + $this->assertEquals(true, $filespec->move_file($this->path . 'copies')); + $this->assertEquals($filespec->file_moved, file_exists($this->path . 'copies/gif_moved')); + $this->assertSame($expected_error, $filespec->error); + } + /** * @dataProvider clean_filename_variables */ @@ -419,7 +530,7 @@ class phpbb_filespec_test extends phpbb_test_case public function test_is_uploaded() { - $filespec = new \phpbb\files\filespec($this->filesystem, $this->language, new \bantu\IniGetWrapper\IniGetWrapper, $this->phpbb_root_path, null); + $filespec = new \phpbb\files\filespec($this->filesystem, $this->language, new \bantu\IniGetWrapper\IniGetWrapper, new \fastImageSize\fastImageSize(), $this->phpbb_root_path, null); $reflection_filespec = new ReflectionClass($filespec); $plupload_property = $reflection_filespec->getProperty('plupload'); $plupload_property->setAccessible(true); diff --git a/tests/upload/fileupload_test.php b/tests/upload/fileupload_test.php index 8675567e22..bb3092528b 100644 --- a/tests/upload/fileupload_test.php +++ b/tests/upload/fileupload_test.php @@ -72,6 +72,7 @@ class phpbb_fileupload_test extends phpbb_test_case $this->filesystem, $this->language, $this->php_ini, + new \fastImageSize\fastImageSize(), $phpbb_root_path, new \phpbb\mimetype\guesser(array( 'mimetype.extension_guesser' => new \phpbb\mimetype\extension_guesser(), @@ -131,7 +132,7 @@ class phpbb_fileupload_test extends phpbb_test_case $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); $upload->set_allowed_extensions(array('jpg')) ->set_max_filesize(1000); - $file = new \phpbb\files\filespec($this->filesystem, $this->language, $this->php_ini, $this->phpbb_root_path); + $file = new \phpbb\files\filespec($this->filesystem, $this->language, $this->php_ini, new \fastImageSize\fastImageSize(), $this->phpbb_root_path); $file->set_upload_ary(array( 'size' => 50, 'tmp_name' => dirname(__FILE__) . '/fixture/disallowed', From 1b9e6e352fd4a78e195d22a7dad6cd4f8f11d97c Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Mon, 7 Sep 2015 11:34:42 +0200 Subject: [PATCH 54/66] [ticket/13904] Improve test coverage of form upload type PHPBB3-13904 --- phpBB/phpbb/files/types/form.php | 10 -- tests/files/types_form_test.php | 174 +++++++++++++++++++++++++++++++ 2 files changed, 174 insertions(+), 10 deletions(-) create mode 100644 tests/files/types_form_test.php diff --git a/phpBB/phpbb/files/types/form.php b/phpBB/phpbb/files/types/form.php index b8ad62f58b..1662e0e920 100644 --- a/phpBB/phpbb/files/types/form.php +++ b/phpBB/phpbb/files/types/form.php @@ -68,16 +68,6 @@ class form extends base return $this->form_upload($args[0]); } - /** - * {@inheritdoc} - */ - public function set_upload(upload $upload) - { - $this->upload = $upload; - - return $this; - } - /** * Form upload method * Upload file from users harddisk diff --git a/tests/files/types_form_test.php b/tests/files/types_form_test.php new file mode 100644 index 0000000000..51219f7b4e --- /dev/null +++ b/tests/files/types_form_test.php @@ -0,0 +1,174 @@ + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; + +class phpbb_files_types_form_test extends phpbb_test_case +{ + private $path; + + private $filesystem; + + /** @var \Symfony\Component\DependencyInjection\ContainerInterface */ + protected $container; + + /** @var \phpbb\files\factory */ + protected $factory; + + /** @var \bantu\IniGetWrapper\IniGetWrapper */ + protected $php_ini; + + /** @var \phpbb\language\language */ + protected $language; + + /** @var \phpbb\request\request_interface */ + protected $request; + + /** @var \phpbb\plupload\plupload */ + protected $plupload; + + /** @var string phpBB root path */ + protected $phpbb_root_path; + + protected function setUp() + { + global $phpbb_root_path, $phpEx; + + $this->request = $this->getMock('\phpbb\request\request'); + $this->request->expects($this->any()) + ->method('file') + ->willReturn(array()); + + $this->filesystem = new \phpbb\filesystem\filesystem(); + $this->language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)); + $this->php_ini = new \bantu\IniGetWrapper\IniGetWrapper; + + $this->container = new phpbb_mock_container_builder($phpbb_root_path, $phpEx); + $this->container->set('files.filespec', new \phpbb\files\filespec( + $this->filesystem, + $this->language, + $this->php_ini, + new \fastImageSize\fastImageSize(), + $phpbb_root_path, + new \phpbb\mimetype\guesser(array( + 'mimetype.extension_guesser' => new \phpbb\mimetype\extension_guesser(), + )))); + $this->factory = new \phpbb\files\factory($this->container); + $this->plupload = $this->getMockBuilder('\phpbb\plupload\plupload') + ->disableOriginalConstructor() + ->getMock(); + $this->plupload->expects($this->any()) + ->method('handle_upload') + ->willReturn(array()); + + $this->path = __DIR__ . '/fixture/'; + $this->phpbb_root_path = $phpbb_root_path; + } + + public function data_upload_form() + { + return array( + array( + array(), + array(''), + ), + array( + array( + 'tmp_name' => 'foo', + 'name' => 'foo', + 'size' => 500, + 'type' => 'image/png', + 'error' => UPLOAD_ERR_PARTIAL, + ), + array('PARTIAL_UPLOAD'), + ), + array( + array( + 'tmp_name' => 'foo', + 'name' => 'foo', + 'size' => 500, + 'type' => 'image/png', + 'error' => -9, + ), + array('NOT_UPLOADED'), + ), + array( + array( + 'tmp_name' => 'foo', + 'name' => 'foo', + 'size' => 0, + 'type' => 'image/png', + ), + array('EMPTY_FILEUPLOAD'), + ), + array( + array( + 'tmp_name' => 'none', + 'name' => 'none', + 'size' => 50, + 'type' => 'image/png', + ), + array('PHP_SIZE_OVERRUN'), + ), + array( + array( + 'tmp_name' => 'tests/upload/fixture/png', + 'name' => 'foo.png', + 'size' => 500, + 'type' => 'image/png', + 'local_mode' => true, + ), + array(), + array('local_mode' => true), + ), + ); + } + + /** + * @dataProvider data_upload_form + */ + public function test_upload_form($upload, $expected, $plupload = array()) + { + $this->request = $this->getMock('\phpbb\request\request'); + $this->request->expects($this->any()) + ->method('file') + ->willReturn($upload); + $filespec = new \phpbb\files\filespec( + $this->filesystem, + $this->language, + $this->php_ini, + new \fastImageSize\fastImageSize(), + $this->phpbb_root_path, + new \phpbb\mimetype\guesser(array( + 'mimetype.extension_guesser' => new \phpbb\mimetype\extension_guesser(), + ))); + $this->container->set('files.filespec', $filespec); + $this->factory = new \phpbb\files\factory($this->container); + $this->plupload = $this->getMockBuilder('\phpbb\plupload\plupload') + ->disableOriginalConstructor() + ->getMock(); + $this->plupload->expects($this->any()) + ->method('handle_upload') + ->willReturn($plupload); + + $type_form = new \phpbb\files\types\form($this->factory, $this->language, $this->php_ini, $this->plupload, $this->request); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); + $upload->set_allowed_extensions(array('png')); + $type_form->set_upload($upload); + + + $file = $type_form->upload('foobar'); + $this->assertSame($expected, $file->error); + $this->assertInstanceOf('\phpbb\files\filespec', $file); + } +} From 82bca32015b94b140b7c810fc5d7e721e8ee341f Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Mon, 7 Sep 2015 11:35:12 +0200 Subject: [PATCH 55/66] [ticket/13904] Improve test coverage of remote upload type PHPBB3-13904 --- phpBB/phpbb/files/types/remote.php | 5 -- tests/files/types_remote_test.php | 77 ++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 tests/files/types_remote_test.php diff --git a/phpBB/phpbb/files/types/remote.php b/phpBB/phpbb/files/types/remote.php index bad87243a5..b5a0dade11 100644 --- a/phpBB/phpbb/files/types/remote.php +++ b/phpBB/phpbb/files/types/remote.php @@ -84,11 +84,6 @@ class remote extends base return $this->factory->get('filespec')->set_error($this->language->lang($this->upload->error_prefix . 'URL_INVALID')); } - if (empty($match[2])) - { - return $this->factory->get('filespec')->set_error($this->language->lang($this->upload->error_prefix . 'URL_INVALID')); - } - $url = parse_url($upload_url); $host = $url['host']; diff --git a/tests/files/types_remote_test.php b/tests/files/types_remote_test.php new file mode 100644 index 0000000000..405a8c4104 --- /dev/null +++ b/tests/files/types_remote_test.php @@ -0,0 +1,77 @@ + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; + +class phpbb_files_types_remote_test extends phpbb_test_case +{ + private $path; + + private $filesystem; + + /** @var \Symfony\Component\DependencyInjection\ContainerInterface */ + protected $container; + + /** @var \phpbb\files\factory */ + protected $factory; + + /** @var \bantu\IniGetWrapper\IniGetWrapper */ + protected $php_ini; + + /** @var \phpbb\language\language */ + protected $language; + + /** @var \phpbb\request\request_interface */ + protected $request; + + /** @var string phpBB root path */ + protected $phpbb_root_path; + + protected function setUp() + { + global $phpbb_root_path, $phpEx; + + $this->request = $this->getMock('\phpbb\request\request'); + + $this->filesystem = new \phpbb\filesystem\filesystem(); + $this->language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)); + $this->php_ini = new \bantu\IniGetWrapper\IniGetWrapper; + + $this->container = new phpbb_mock_container_builder($phpbb_root_path, $phpEx); + $this->container->set('files.filespec', new \phpbb\files\filespec( + $this->filesystem, + $this->language, + $this->php_ini, + new \fastImageSize\fastImageSize(), + $phpbb_root_path, + new \phpbb\mimetype\guesser(array( + 'mimetype.extension_guesser' => new \phpbb\mimetype\extension_guesser(), + )))); + $this->factory = new \phpbb\files\factory($this->container); + + $this->path = __DIR__ . '/fixture/'; + $this->phpbb_root_path = $phpbb_root_path; + } + + public function test_upload_fsock_fail() + { + $type_remote = new \phpbb\files\types\remote($this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); + $upload->set_allowed_extensions(array('png')); + $type_remote->set_upload($upload); + + $file = $type_remote->upload('https://bärföö.com/foo.png'); + + $this->assertSame(array('NOT_UPLOADED'), $file->error); + } +} From e5bffbf40ff59614cea1b5f3f662d91cf8e63323 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Mon, 7 Sep 2015 11:35:31 +0200 Subject: [PATCH 56/66] [ticket/13904] Improve test coverage of base upload type class PHPBB3-13904 --- tests/files/types_base_test.php | 93 +++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 tests/files/types_base_test.php diff --git a/tests/files/types_base_test.php b/tests/files/types_base_test.php new file mode 100644 index 0000000000..dc82ee2639 --- /dev/null +++ b/tests/files/types_base_test.php @@ -0,0 +1,93 @@ + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +class phpbb_files_types_base_test extends phpbb_test_case +{ + private $path; + + private $filesystem; + + /** @var \Symfony\Component\DependencyInjection\ContainerInterface */ + protected $container; + + /** @var \phpbb\files\factory */ + protected $factory; + + /** @var \bantu\IniGetWrapper\IniGetWrapper */ + protected $php_ini; + + /** @var \phpbb\language\language */ + protected $language; + + /** @var \phpbb\request\request_interface */ + protected $request; + + /** @var string phpBB root path */ + protected $phpbb_root_path; + + protected function setUp() + { + global $phpbb_root_path, $phpEx; + + $this->request = $this->getMock('\phpbb\request\request'); + + $this->filesystem = new \phpbb\filesystem\filesystem(); + $this->language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)); + $this->php_ini = new \bantu\IniGetWrapper\IniGetWrapper; + + $this->container = new phpbb_mock_container_builder($phpbb_root_path, $phpEx); + $this->container->set('files.filespec', new \phpbb\files\filespec( + $this->filesystem, + $this->language, + $this->php_ini, + new \fastImageSize\fastImageSize(), + $phpbb_root_path, + new \phpbb\mimetype\guesser(array( + 'mimetype.extension_guesser' => new \phpbb\mimetype\extension_guesser(), + )))); + $this->factory = new \phpbb\files\factory($this->container); + + $this->path = __DIR__ . '/fixture/'; + $this->phpbb_root_path = $phpbb_root_path; + } + + public function data_check_upload_size() + { + return array( + array('foo', '500KB', array()), + array('none', '500KB', array('PHP_SIZE_OVERRUN')), + array('none', '', array('PHP_SIZE_NA')), + ); + } + + /** + * @dataProvider data_check_upload_size + */ + public function test_check_upload_size($filename, $max_filesize, $expected) + { + $php_ini = $this->getMock('\bantu\IniGetWrapper\IniGetWrapper'); + $php_ini->expects($this->any()) + ->method('getString') + ->willReturn($max_filesize); + $type_form = new \phpbb\files\types\local($this->factory, $this->language, $php_ini, $this->request); + $file = $this->getMockBuilder('\phpbb\files\filespec') + ->disableOriginalConstructor() + ->getMock(); + $file->expects($this->any()) + ->method('get') + ->willReturn($filename); + $type_form->check_upload_size($file); + + $this->assertSame($expected, $file->error); + } +} From 7a92ad596c56c25728fd6a22c3a817504e8cb347 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Mon, 7 Sep 2015 12:19:48 +0200 Subject: [PATCH 57/66] [ticket/13904] Minor coding style fixes PHPBB3-13904 --- phpBB/phpbb/files/filespec.php | 1 + phpBB/phpbb/files/types/form.php | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/phpBB/phpbb/files/filespec.php b/phpBB/phpbb/files/filespec.php index 6ce54a4789..83007f11c0 100644 --- a/phpBB/phpbb/files/filespec.php +++ b/phpBB/phpbb/files/filespec.php @@ -96,6 +96,7 @@ class filespec * @param \phpbb\filesystem\filesystem_interface $phpbb_filesystem Filesystem * @param language $language Language * @param \bantu\IniGetWrapper\IniGetWrapper $php_ini ini_get() wrapper + * @param \fastImageSize\fastImageSize $imagesize Imagesize class * @param string $phpbb_root_path phpBB root path * @param \phpbb\mimetype\guesser $mimetype_guesser Mime type guesser * @param \phpbb\plupload\plupload $plupload Plupload diff --git a/phpBB/phpbb/files/types/form.php b/phpBB/phpbb/files/types/form.php index 1662e0e920..4cf340ee7d 100644 --- a/phpBB/phpbb/files/types/form.php +++ b/phpBB/phpbb/files/types/form.php @@ -16,7 +16,6 @@ namespace phpbb\files\types; use \bantu\IniGetWrapper\IniGetWrapper; use \phpbb\files\factory; use \phpbb\files\filespec; -use \phpbb\files\upload; use \phpbb\language\language; use \phpbb\plupload\plupload; use \phpbb\request\request_interface; @@ -38,7 +37,7 @@ class form extends base /** @var request_interface */ protected $request; - /** @var upload */ + /** @var \phpbb\files\upload */ protected $upload; /** From 00e5ff9e2e9c0dd7325f33a22888d6888af4b197 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Mon, 7 Sep 2015 22:51:44 +0200 Subject: [PATCH 58/66] [ticket/13904] Add unit tests for local upload type PHPBB3-13904 --- phpBB/phpbb/files/types/local.php | 12 --- tests/files/types_local_test.php | 161 ++++++++++++++++++++++++++++++ 2 files changed, 161 insertions(+), 12 deletions(-) create mode 100644 tests/files/types_local_test.php diff --git a/phpBB/phpbb/files/types/local.php b/phpBB/phpbb/files/types/local.php index 96bfdaca7f..ff11890856 100644 --- a/phpBB/phpbb/files/types/local.php +++ b/phpBB/phpbb/files/types/local.php @@ -84,19 +84,7 @@ class local extends base return $file; } - if (isset($upload['error'])) - { - $error = $this->upload->assign_internal_error($upload['error']); - - if ($error !== false) - { - $file->error[] = $error; - return $file; - } - } - // PHP Upload file size check - $this->check_upload_size($file); $file = $this->check_upload_size($file); if (sizeof($file->error)) { diff --git a/tests/files/types_local_test.php b/tests/files/types_local_test.php new file mode 100644 index 0000000000..8712c9cd5a --- /dev/null +++ b/tests/files/types_local_test.php @@ -0,0 +1,161 @@ + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; + +class phpbb_files_types_local_test extends phpbb_test_case +{ + private $path; + + private $filesystem; + + /** @var \Symfony\Component\DependencyInjection\ContainerInterface */ + protected $container; + + /** @var \phpbb\files\factory */ + protected $factory; + + /** @var \bantu\IniGetWrapper\IniGetWrapper */ + protected $php_ini; + + /** @var \phpbb\language\language */ + protected $language; + + /** @var \phpbb\request\request_interface */ + protected $request; + + /** @var \phpbb\plupload\plupload */ + protected $plupload; + + /** @var string phpBB root path */ + protected $phpbb_root_path; + + protected function setUp() + { + global $phpbb_root_path, $phpEx; + + $this->request = $this->getMock('\phpbb\request\request'); + $this->request->expects($this->any()) + ->method('file') + ->willReturn(array()); + + $this->filesystem = new \phpbb\filesystem\filesystem(); + $this->language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)); + $this->php_ini = new \bantu\IniGetWrapper\IniGetWrapper; + + $this->container = new phpbb_mock_container_builder($phpbb_root_path, $phpEx); + $this->container->set('files.filespec', new \phpbb\files\filespec( + $this->filesystem, + $this->language, + $this->php_ini, + new \fastImageSize\fastImageSize(), + $phpbb_root_path, + new \phpbb\mimetype\guesser(array( + 'mimetype.extension_guesser' => new \phpbb\mimetype\extension_guesser(), + )))); + $this->factory = new \phpbb\files\factory($this->container); + $this->plupload = $this->getMockBuilder('\phpbb\plupload\plupload') + ->disableOriginalConstructor() + ->getMock(); + $this->plupload->expects($this->any()) + ->method('handle_upload') + ->willReturn(array()); + + $this->path = __DIR__ . '/fixture/'; + $this->phpbb_root_path = $phpbb_root_path; + } + + public function test_upload_init_error() + { + $filespec = $this->getMockBuilder('\phpbb\files\filespec') + ->disableOriginalConstructor() + ->getMock(); + $filespec->expects($this->any()) + ->method('init_error') + ->willReturn(true); + $filespec->expects($this->any()) + ->method('set_upload_ary') + ->willReturnSelf(); + $filespec->expects($this->any()) + ->method('set_upload_namespace') + ->willReturnSelf(); + $this->container->set('files.filespec', $filespec); + $this->factory = new \phpbb\files\factory($this->container); + + $type_local = new \phpbb\files\types\local($this->factory, $this->language, $this->php_ini, $this->request); + + + $file = $type_local->upload('foo', false); + $this->assertSame(array(''), $file->error); + $this->assertInstanceOf('\phpbb\files\filespec', $file); + } + + public function data_upload_form() + { + return array( + array( + 'foo', + array( + 'tmp_name' => 'foo', + 'size' => 500, + 'type' => 'image/png', + ), + array('NOT_UPLOADED'), + ), + array( + 'none', + false, + array('PHP_SIZE_OVERRUN'), + ), + array( + 'tests/upload/fixture/png', + array( + 'realname' => 'foo.png', + 'size' => 500, + 'type' => 'image/png', + 'local_mode' => true, + ), + array(), + ), + ); + } + + /** + * @dataProvider data_upload_form + */ + public function test_upload_form($filename, $upload_ary, $expected) + { + $filespec = new \phpbb\files\filespec( + $this->filesystem, + $this->language, + $this->php_ini, + new \fastImageSize\fastImageSize(), + $this->phpbb_root_path, + new \phpbb\mimetype\guesser(array( + 'mimetype.extension_guesser' => new \phpbb\mimetype\extension_guesser(), + ))); + $filespec->local = true; + $this->container->set('files.filespec', $filespec); + $this->factory = new \phpbb\files\factory($this->container); + + $type_local = new \phpbb\files\types\local($this->factory, $this->language, $this->php_ini, $this->request); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); + $upload->set_allowed_extensions(array('png')); + $type_local->set_upload($upload); + + + $file = $type_local->upload($filename, $upload_ary); + $this->assertSame($expected, $file->error); + $this->assertInstanceOf('\phpbb\files\filespec', $file); + } +} From e60c8a5a8bfa1aa500c096ea90cd32fda9465f9a Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Mon, 7 Sep 2015 22:52:20 +0200 Subject: [PATCH 59/66] [ticket/13904] Improve code coverage PHPBB3-13904 --- phpBB/phpbb/files/types/remote.php | 4 +- tests/files/type_foo.php | 31 ++++++++++++++ tests/files/types_remote_test.php | 66 +++++++++++++++++++++++++++++- 3 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 tests/files/type_foo.php diff --git a/phpBB/phpbb/files/types/remote.php b/phpBB/phpbb/files/types/remote.php index b5a0dade11..34ee97f1c9 100644 --- a/phpBB/phpbb/files/types/remote.php +++ b/phpBB/phpbb/files/types/remote.php @@ -235,10 +235,10 @@ class remote extends base { $max_file_size = $this->php_ini->getString('upload_max_filesize'); - if (!empty($max_filesize)) + if (!empty($max_file_size)) { $unit = strtolower(substr($max_file_size, -1, 1)); - $max_file_size = (int) $max_filesize; + $max_file_size = (int) $max_file_size; switch ($unit) { diff --git a/tests/files/type_foo.php b/tests/files/type_foo.php new file mode 100644 index 0000000000..20eddfcbc4 --- /dev/null +++ b/tests/files/type_foo.php @@ -0,0 +1,31 @@ + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +namespace phpbb\files\types; + +class foo extends \phpbb\files\types\remote +{ + static public $tempnam_path; +} + +function tempnam($one, $two) +{ + if (empty(foo::$tempnam_path)) + { + return \tempnam($one, $two); + } + else + { + return foo::$tempnam_path; + } +} diff --git a/tests/files/types_remote_test.php b/tests/files/types_remote_test.php index 405a8c4104..6d513ac5c0 100644 --- a/tests/files/types_remote_test.php +++ b/tests/files/types_remote_test.php @@ -12,6 +12,7 @@ */ require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; +require_once dirname(__FILE__) . '/type_foo.php'; class phpbb_files_types_remote_test extends phpbb_test_case { @@ -39,8 +40,9 @@ class phpbb_files_types_remote_test extends phpbb_test_case protected function setUp() { - global $phpbb_root_path, $phpEx; + global $config, $phpbb_root_path, $phpEx; + $config = new \phpbb\config\config(array()); $this->request = $this->getMock('\phpbb\request\request'); $this->filesystem = new \phpbb\filesystem\filesystem(); @@ -74,4 +76,66 @@ class phpbb_files_types_remote_test extends phpbb_test_case $this->assertSame(array('NOT_UPLOADED'), $file->error); } + + public function data_get_max_file_size() + { + return array( + array('', 'http://example.com/foo/bar.png'), + array('2k', 'http://example.com/foo/bar.png'), + array('500k', 'http://example.com/foo/bar.png'), + array('500M', 'http://example.com/foo/bar.png'), + array('500m', 'http://example.com/foo/bar.png'), + array('500k', 'http://google.com/.png', 'DISALLOWED_CONTENT'), + array('1', 'http://google.com/.png', 'WRONG_FILESIZE'), + array('500g', 'http://example.com/foo/bar.png'), + array('foobar', 'http://example.com/foo/bar.png'), + array('-5k', 'http://example.com/foo/bar.png'), + ); + } + + /** + * @dataProvider data_get_max_file_size + */ + public function test_get_max_file_size($max_file_size, $link, $expected = 'URL_NOT_FOUND') + { + $php_ini = $this->getMock('\bantu\IniGetWrapper\IniGetWrapper', array('getString')); + $php_ini->expects($this->any()) + ->method('getString') + ->willReturn($max_file_size); + $type_remote = new \phpbb\files\types\remote($this->factory, $this->language, $php_ini, $this->request, $this->phpbb_root_path); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); + $upload->set_allowed_extensions(array('png')); + $type_remote->set_upload($upload); + + $file = $type_remote->upload($link); + + $this->assertSame(array($expected), $file->error); + } + + public function test_upload_timeout() + { + $type_remote = new \phpbb\files\types\remote($this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); + $upload->set_allowed_extensions(array('png')); + $type_remote->set_upload($upload); + $upload->upload_timeout = -5; + + $file = $type_remote->upload('http://google.com/.png'); + + $this->assertSame(array('REMOTE_UPLOAD_TIMEOUT'), $file->error); + } + + public function test_upload_wrong_path() + { + $type_remote = new \phpbb\files\types\foo($this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); + $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); + $upload->set_allowed_extensions(array('png')); + $type_remote->set_upload($upload); + $type_remote::$tempnam_path = $this->phpbb_root_path . 'cache/wrong/path'; + + $file = $type_remote->upload('http://google.com/.png'); + + $this->assertSame(array('NOT_UPLOADED'), $file->error); + $type_remote::$tempnam_path = ''; + } } From 223fa3aed5df1c2885c2f7fe837ac8cb47cd44f2 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Wed, 9 Sep 2015 08:31:40 +0200 Subject: [PATCH 60/66] [ticket/13904] Update composer.lock PHPBB3-13904 --- phpBB/composer.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/composer.lock b/phpBB/composer.lock index cbd7292aed..accd4a55f6 100644 --- a/phpBB/composer.lock +++ b/phpBB/composer.lock @@ -4,7 +4,7 @@ "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "ba4a5aa23baa2fffaf9843b0be42e189", + "hash": "983770c26a0dcd7399ebd488846ade9a", "packages": [ { "name": "bantu/ini-get-wrapper", From 327e36a4d68ff9607967af52ef8f6a00c60343ff Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Wed, 9 Sep 2015 09:41:40 +0200 Subject: [PATCH 61/66] [ticket/13904] Modify files for updated fast-image-size library PHPBB3-13904 --- phpBB/phpbb/files/filespec.php | 6 +++--- tests/files/types_base_test.php | 2 +- tests/files/types_form_test.php | 4 ++-- tests/files/types_local_test.php | 4 ++-- tests/files/types_remote_test.php | 2 +- tests/files/upload_test.php | 2 +- tests/functional/fileupload_remote_test.php | 2 +- tests/upload/filespec_test.php | 12 ++++++------ tests/upload/fileupload_test.php | 4 ++-- 9 files changed, 19 insertions(+), 19 deletions(-) diff --git a/phpBB/phpbb/files/filespec.php b/phpBB/phpbb/files/filespec.php index 83007f11c0..e171e7e68f 100644 --- a/phpBB/phpbb/files/filespec.php +++ b/phpBB/phpbb/files/filespec.php @@ -75,7 +75,7 @@ class filespec /** @var \bantu\IniGetWrapper\IniGetWrapper ini_get() wrapper class */ protected $php_ini; - /** @var \fastImageSize\fastImageSize */ + /** @var \FastImageSize\FastImageSize */ protected $imagesize; /** @var language Language class */ @@ -96,12 +96,12 @@ class filespec * @param \phpbb\filesystem\filesystem_interface $phpbb_filesystem Filesystem * @param language $language Language * @param \bantu\IniGetWrapper\IniGetWrapper $php_ini ini_get() wrapper - * @param \fastImageSize\fastImageSize $imagesize Imagesize class + * @param \FastImageSize\FastImageSize $imagesize Imagesize class * @param string $phpbb_root_path phpBB root path * @param \phpbb\mimetype\guesser $mimetype_guesser Mime type guesser * @param \phpbb\plupload\plupload $plupload Plupload */ - public function __construct(\phpbb\filesystem\filesystem_interface $phpbb_filesystem, language $language, \bantu\IniGetWrapper\IniGetWrapper $php_ini, \fastImageSize\fastImageSize $imagesize, $phpbb_root_path, \phpbb\mimetype\guesser $mimetype_guesser = null, \phpbb\plupload\plupload $plupload = null) + public function __construct(\phpbb\filesystem\filesystem_interface $phpbb_filesystem, language $language, \bantu\IniGetWrapper\IniGetWrapper $php_ini, \FastImageSize\FastImageSize $imagesize, $phpbb_root_path, \phpbb\mimetype\guesser $mimetype_guesser = null, \phpbb\plupload\plupload $plupload = null) { $this->filesystem = $phpbb_filesystem; $this->language = $language; diff --git a/tests/files/types_base_test.php b/tests/files/types_base_test.php index dc82ee2639..e630bf8c48 100644 --- a/tests/files/types_base_test.php +++ b/tests/files/types_base_test.php @@ -50,7 +50,7 @@ class phpbb_files_types_base_test extends phpbb_test_case $this->filesystem, $this->language, $this->php_ini, - new \fastImageSize\fastImageSize(), + new \FastImageSize\FastImageSize(), $phpbb_root_path, new \phpbb\mimetype\guesser(array( 'mimetype.extension_guesser' => new \phpbb\mimetype\extension_guesser(), diff --git a/tests/files/types_form_test.php b/tests/files/types_form_test.php index 51219f7b4e..efcece49d1 100644 --- a/tests/files/types_form_test.php +++ b/tests/files/types_form_test.php @@ -58,7 +58,7 @@ class phpbb_files_types_form_test extends phpbb_test_case $this->filesystem, $this->language, $this->php_ini, - new \fastImageSize\fastImageSize(), + new \FastImageSize\FastImageSize(), $phpbb_root_path, new \phpbb\mimetype\guesser(array( 'mimetype.extension_guesser' => new \phpbb\mimetype\extension_guesser(), @@ -147,7 +147,7 @@ class phpbb_files_types_form_test extends phpbb_test_case $this->filesystem, $this->language, $this->php_ini, - new \fastImageSize\fastImageSize(), + new \FastImageSize\FastImageSize(), $this->phpbb_root_path, new \phpbb\mimetype\guesser(array( 'mimetype.extension_guesser' => new \phpbb\mimetype\extension_guesser(), diff --git a/tests/files/types_local_test.php b/tests/files/types_local_test.php index 8712c9cd5a..1367bbfcf8 100644 --- a/tests/files/types_local_test.php +++ b/tests/files/types_local_test.php @@ -58,7 +58,7 @@ class phpbb_files_types_local_test extends phpbb_test_case $this->filesystem, $this->language, $this->php_ini, - new \fastImageSize\fastImageSize(), + new \FastImageSize\FastImageSize(), $phpbb_root_path, new \phpbb\mimetype\guesser(array( 'mimetype.extension_guesser' => new \phpbb\mimetype\extension_guesser(), @@ -139,7 +139,7 @@ class phpbb_files_types_local_test extends phpbb_test_case $this->filesystem, $this->language, $this->php_ini, - new \fastImageSize\fastImageSize(), + new \FastImageSize\FastImageSize(), $this->phpbb_root_path, new \phpbb\mimetype\guesser(array( 'mimetype.extension_guesser' => new \phpbb\mimetype\extension_guesser(), diff --git a/tests/files/types_remote_test.php b/tests/files/types_remote_test.php index 6d513ac5c0..a85844ee78 100644 --- a/tests/files/types_remote_test.php +++ b/tests/files/types_remote_test.php @@ -54,7 +54,7 @@ class phpbb_files_types_remote_test extends phpbb_test_case $this->filesystem, $this->language, $this->php_ini, - new \fastImageSize\fastImageSize(), + new \FastImageSize\FastImageSize(), $phpbb_root_path, new \phpbb\mimetype\guesser(array( 'mimetype.extension_guesser' => new \phpbb\mimetype\extension_guesser(), diff --git a/tests/files/upload_test.php b/tests/files/upload_test.php index 0d02926702..1716eca6c9 100644 --- a/tests/files/upload_test.php +++ b/tests/files/upload_test.php @@ -59,7 +59,7 @@ class phpbb_files_upload_test extends phpbb_test_case $this->filesystem, $this->language, $this->php_ini, - new \fastImageSize\fastImageSize(), + new \FastImageSize\FastImageSize(), $phpbb_root_path, new \phpbb\mimetype\guesser(array( 'mimetype.extension_guesser' => new \phpbb\mimetype\extension_guesser(), diff --git a/tests/functional/fileupload_remote_test.php b/tests/functional/fileupload_remote_test.php index 1484acc4d5..3349ffc576 100644 --- a/tests/functional/fileupload_remote_test.php +++ b/tests/functional/fileupload_remote_test.php @@ -57,7 +57,7 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case $this->php_ini = new \bantu\IniGetWrapper\IniGetWrapper; $container = new phpbb_mock_container_builder(); - $container->set('files.filespec', new \phpbb\files\filespec($this->filesystem, $this->language, $this->php_ini, new \fastImageSize\fastImageSize(), $this->phpbb_root_path)); + $container->set('files.filespec', new \phpbb\files\filespec($this->filesystem, $this->language, $this->php_ini, new \FastImageSize\FastImageSize(), $this->phpbb_root_path)); $this->factory = new \phpbb\files\factory($container); $container->set('files.factory', $this->factory); $container->set('files.types.remote', new \phpbb\files\types\remote($this->factory, $this->language, $this->php_ini, $this->request, $phpbb_root_path)); diff --git a/tests/upload/filespec_test.php b/tests/upload/filespec_test.php index 10a442cd1d..08c064698c 100644 --- a/tests/upload/filespec_test.php +++ b/tests/upload/filespec_test.php @@ -93,7 +93,7 @@ class phpbb_filespec_test extends phpbb_test_case 'error' => '', ); - $filespec = new \phpbb\files\filespec($this->filesystem, $this->language, new \bantu\IniGetWrapper\IniGetWrapper, new \fastImageSize\fastImageSize(), $this->phpbb_root_path, $this->mimetype_guesser); + $filespec = new \phpbb\files\filespec($this->filesystem, $this->language, new \bantu\IniGetWrapper\IniGetWrapper, new \FastImageSize\FastImageSize(), $this->phpbb_root_path, $this->mimetype_guesser); return $filespec->set_upload_ary(array_merge($upload_ary, $override)); } @@ -114,7 +114,7 @@ class phpbb_filespec_test extends phpbb_test_case public function test_empty_upload_ary() { - $filespec = new \phpbb\files\filespec($this->filesystem, $this->language, new \bantu\IniGetWrapper\IniGetWrapper, new \fastImageSize\fastImageSize(), $this->phpbb_root_path, $this->mimetype_guesser); + $filespec = new \phpbb\files\filespec($this->filesystem, $this->language, new \bantu\IniGetWrapper\IniGetWrapper, new \FastImageSize\FastImageSize(), $this->phpbb_root_path, $this->mimetype_guesser); $this->assertInstanceOf('\phpbb\files\filespec', $filespec->set_upload_ary(array())); $this->assertTrue($filespec->init_error()); } @@ -262,7 +262,7 @@ class phpbb_filespec_test extends phpbb_test_case */ public function test_clean_filename_avatar($filename, $expected, $mode, $prefix = '', $user_id = '') { - $filespec = new \phpbb\files\filespec($this->filesystem, $this->language, new \bantu\IniGetWrapper\IniGetWrapper, new \fastImageSize\fastImageSize(), $this->phpbb_root_path, $this->mimetype_guesser); + $filespec = new \phpbb\files\filespec($this->filesystem, $this->language, new \bantu\IniGetWrapper\IniGetWrapper, new \FastImageSize\FastImageSize(), $this->phpbb_root_path, $this->mimetype_guesser); if ($filename) { @@ -435,7 +435,7 @@ class phpbb_filespec_test extends phpbb_test_case ->willReturn($safe_mode_on); $upload = new phpbb_mock_fileupload(); $upload->max_filesize = self::UPLOAD_MAX_FILESIZE; - $filespec = new \phpbb\files\filespec($this->filesystem, $this->language, $php_ini, new \fastImageSize\fastImagesize, '', $this->mimetype_guesser); + $filespec = new \phpbb\files\filespec($this->filesystem, $this->language, $php_ini, new \FastImageSize\FastImagesize, '', $this->mimetype_guesser); $filespec->set_upload_ary($upload_ary); $filespec->local = false; $filespec->extension = 'gif'; @@ -494,7 +494,7 @@ class phpbb_filespec_test extends phpbb_test_case 'error' => '', ); - $imagesize = $this->getMockBuilder('\fastImageSize\fastImageSize') + $imagesize = $this->getMockBuilder('\FastImageSize\FastImageSize') ->getMock(); $imagesize->expects($this->any()) ->method('getImageSize') @@ -530,7 +530,7 @@ class phpbb_filespec_test extends phpbb_test_case public function test_is_uploaded() { - $filespec = new \phpbb\files\filespec($this->filesystem, $this->language, new \bantu\IniGetWrapper\IniGetWrapper, new \fastImageSize\fastImageSize(), $this->phpbb_root_path, null); + $filespec = new \phpbb\files\filespec($this->filesystem, $this->language, new \bantu\IniGetWrapper\IniGetWrapper, new \FastImageSize\FastImageSize(), $this->phpbb_root_path, null); $reflection_filespec = new ReflectionClass($filespec); $plupload_property = $reflection_filespec->getProperty('plupload'); $plupload_property->setAccessible(true); diff --git a/tests/upload/fileupload_test.php b/tests/upload/fileupload_test.php index bb3092528b..01b625efbb 100644 --- a/tests/upload/fileupload_test.php +++ b/tests/upload/fileupload_test.php @@ -72,7 +72,7 @@ class phpbb_fileupload_test extends phpbb_test_case $this->filesystem, $this->language, $this->php_ini, - new \fastImageSize\fastImageSize(), + new \FastImageSize\FastImageSize(), $phpbb_root_path, new \phpbb\mimetype\guesser(array( 'mimetype.extension_guesser' => new \phpbb\mimetype\extension_guesser(), @@ -132,7 +132,7 @@ class phpbb_fileupload_test extends phpbb_test_case $upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); $upload->set_allowed_extensions(array('jpg')) ->set_max_filesize(1000); - $file = new \phpbb\files\filespec($this->filesystem, $this->language, $this->php_ini, new \fastImageSize\fastImageSize(), $this->phpbb_root_path); + $file = new \phpbb\files\filespec($this->filesystem, $this->language, $this->php_ini, new \FastImageSize\FastImageSize(), $this->phpbb_root_path); $file->set_upload_ary(array( 'size' => 50, 'tmp_name' => dirname(__FILE__) . '/fixture/disallowed', From 70ad0c6a8f7d16b767aa78cde2acc9a3b3512e6f Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Wed, 9 Sep 2015 10:43:12 +0200 Subject: [PATCH 62/66] [ticket/13904] Add language entries for error messages in upload class PHPBB3-13904 --- phpBB/language/en/common.php | 2 ++ phpBB/language/en/posting.php | 2 ++ phpBB/phpbb/files/upload.php | 7 ++----- tests/files/upload_test.php | 6 +++--- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/phpBB/language/en/common.php b/phpBB/language/en/common.php index 292d30dd1c..8e5ab53b0e 100644 --- a/phpBB/language/en/common.php +++ b/phpBB/language/en/common.php @@ -105,11 +105,13 @@ $lang = array_merge($lang, array( 'AVATAR_EMPTY_FILEUPLOAD' => 'The uploaded avatar file is empty.', 'AVATAR_INVALID_FILENAME' => '%s is an invalid filename.', 'AVATAR_NOT_UPLOADED' => 'Avatar could not be uploaded.', + 'AVATAR_NO_TEMP_DIR' => 'Temporary folder could not be found or is not writable.', 'AVATAR_NO_SIZE' => 'The width or height of the linked avatar could not be determined. Please enter them manually.', 'AVATAR_PARTIAL_UPLOAD' => 'The specified file was only partially uploaded.', 'AVATAR_PHP_SIZE_NA' => 'The avatar’s filesize is too large.
The maximum allowed filesize set in php.ini could not be determined.', 'AVATAR_PHP_SIZE_OVERRUN' => 'The avatar’s filesize is too large. The maximum allowed upload size is %1$d %2$s.
Please note this is set in php.ini and cannot be overridden.', 'AVATAR_REMOTE_UPLOAD_TIMEOUT' => 'The specified avatar could not be uploaded because the request timed out.', + 'AVATAR_PHP_UPLOAD_STOPPED' => 'A PHP extension has stopped the file upload.', 'AVATAR_URL_INVALID' => 'The URL you specified is invalid.', 'AVATAR_URL_NOT_FOUND' => 'The file specified could not be found.', 'AVATAR_WRONG_FILESIZE' => 'The avatar’s filesize must be between 0 and %1$d %2$s.', diff --git a/phpBB/language/en/posting.php b/phpBB/language/en/posting.php index 924395ed44..0e2d706f19 100644 --- a/phpBB/language/en/posting.php +++ b/phpBB/language/en/posting.php @@ -176,8 +176,10 @@ $lang = array_merge($lang, array( 'NO_POLL_TITLE' => 'You have to enter a poll title.', 'NO_POST' => 'The requested post does not exist.', 'NO_POST_MODE' => 'No post mode specified.', + 'NO_TEMP_DIR' => 'Temporary folder could not be found or is not writable.', 'PARTIAL_UPLOAD' => 'The uploaded file was only partially uploaded.', + 'PHP_UPLOAD_STOPPED' => 'A PHP extension has stopped the file upload.', 'PHP_SIZE_NA' => 'The attachment’s file size is too large.
Could not determine the maximum size defined by PHP in php.ini.', 'PHP_SIZE_OVERRUN' => 'The attachment’s file size is too large, the maximum upload size is %1$d %2$s.
Please note this is set in php.ini and cannot be overridden.', 'PLACE_INLINE' => 'Place inline', diff --git a/phpBB/phpbb/files/upload.php b/phpBB/phpbb/files/upload.php index 328dd49a06..bd379924e7 100644 --- a/phpBB/phpbb/files/upload.php +++ b/phpBB/phpbb/files/upload.php @@ -245,15 +245,12 @@ class upload break; case UPLOAD_ERR_NO_TMP_DIR: - $error = 'Temporary folder could not be found. Please check your PHP installation.'; - break; - case UPLOAD_ERR_CANT_WRITE: - $error = 'Can’t write to temporary folder.'; + $error = $this->language->lang($this->error_prefix . 'NO_TEMP_DIR'); break; case UPLOAD_ERR_EXTENSION: - $error = 'A PHP extension has stopped the file upload.'; + $error = $this->language->lang($this->error_prefix . 'PHP_UPLOAD_STOPPED'); break; default: diff --git a/tests/files/upload_test.php b/tests/files/upload_test.php index 1716eca6c9..c41204a0d5 100644 --- a/tests/files/upload_test.php +++ b/tests/files/upload_test.php @@ -110,9 +110,9 @@ class phpbb_files_upload_test extends phpbb_test_case array(UPLOAD_ERR_FORM_SIZE, 'WRONG_FILESIZE'), array(UPLOAD_ERR_PARTIAL, 'PARTIAL_UPLOAD'), array(UPLOAD_ERR_NO_FILE, 'NOT_UPLOADED'), - array(UPLOAD_ERR_NO_TMP_DIR, 'Temporary folder could not be found. Please check your PHP installation.'), - array(UPLOAD_ERR_CANT_WRITE, 'Can’t write to temporary folder.'), - array(UPLOAD_ERR_EXTENSION, 'A PHP extension has stopped the file upload.'), + array(UPLOAD_ERR_NO_TMP_DIR, 'NO_TEMP_DIR'), + array(UPLOAD_ERR_CANT_WRITE, 'NO_TEMP_DIR'), + array(UPLOAD_ERR_EXTENSION, 'PHP_UPLOAD_STOPPED'), array(9, false), ); } From 5f91f1cad85eaf7f8dc62a1a140605a46431496f Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Wed, 9 Sep 2015 10:46:14 +0200 Subject: [PATCH 63/66] [ticket/13904] Minor coding style fixes PHPBB3-13904 --- phpBB/phpbb/files/factory.php | 2 +- phpBB/phpbb/files/filespec.php | 4 +--- phpBB/phpbb/files/types/form.php | 12 ++++++------ phpBB/phpbb/files/types/local.php | 10 +++++----- phpBB/phpbb/files/types/remote.php | 10 +++++----- phpBB/phpbb/files/types/type_interface.php | 2 +- phpBB/phpbb/files/upload.php | 6 +++--- 7 files changed, 22 insertions(+), 24 deletions(-) diff --git a/phpBB/phpbb/files/factory.php b/phpBB/phpbb/files/factory.php index 508c50c6ce..84b7cc9449 100644 --- a/phpBB/phpbb/files/factory.php +++ b/phpBB/phpbb/files/factory.php @@ -42,7 +42,7 @@ class factory { $service = false; - $name = (strpos($name, 'files.') === false) ? 'files.' . $name : $name; + $name = (strpos($name, '.') === false) ? 'files.' . $name : $name; try { diff --git a/phpBB/phpbb/files/filespec.php b/phpBB/phpbb/files/filespec.php index e171e7e68f..7a42e6bd50 100644 --- a/phpBB/phpbb/files/filespec.php +++ b/phpBB/phpbb/files/filespec.php @@ -13,7 +13,7 @@ namespace phpbb\files; -use \phpbb\language\language; +use phpbb\language\language; /** * Responsible for holding all file relevant information, as well as doing file-specific operations. @@ -201,8 +201,6 @@ class filespec * character. Unique creates a unique filename. * @param string $prefix Prefix applied to filename * @param string $user_id The user_id is only needed for when cleaning a user's avatar - * - *@access public */ public function clean_filename($mode = 'unique', $prefix = '', $user_id = '') { diff --git a/phpBB/phpbb/files/types/form.php b/phpBB/phpbb/files/types/form.php index 4cf340ee7d..832f090c47 100644 --- a/phpBB/phpbb/files/types/form.php +++ b/phpBB/phpbb/files/types/form.php @@ -13,12 +13,12 @@ namespace phpbb\files\types; -use \bantu\IniGetWrapper\IniGetWrapper; -use \phpbb\files\factory; -use \phpbb\files\filespec; -use \phpbb\language\language; -use \phpbb\plupload\plupload; -use \phpbb\request\request_interface; +use bantu\IniGetWrapper\IniGetWrapper; +use phpbb\files\factory; +use phpbb\files\filespec; +use phpbb\language\language; +use phpbb\plupload\plupload; +use phpbb\request\request_interface; class form extends base { diff --git a/phpBB/phpbb/files/types/local.php b/phpBB/phpbb/files/types/local.php index ff11890856..7e9210b196 100644 --- a/phpBB/phpbb/files/types/local.php +++ b/phpBB/phpbb/files/types/local.php @@ -13,11 +13,11 @@ namespace phpbb\files\types; -use \bantu\IniGetWrapper\IniGetWrapper; -use \phpbb\files\factory; -use \phpbb\files\filespec; -use \phpbb\language\language; -use \phpbb\request\request_interface; +use bantu\IniGetWrapper\IniGetWrapper; +use phpbb\files\factory; +use phpbb\files\filespec; +use phpbb\language\language; +use phpbb\request\request_interface; class local extends base { diff --git a/phpBB/phpbb/files/types/remote.php b/phpBB/phpbb/files/types/remote.php index 34ee97f1c9..44feab0ece 100644 --- a/phpBB/phpbb/files/types/remote.php +++ b/phpBB/phpbb/files/types/remote.php @@ -13,11 +13,11 @@ namespace phpbb\files\types; -use \bantu\IniGetWrapper\IniGetWrapper; -use \phpbb\files\factory; -use \phpbb\files\filespec; -use \phpbb\language\language; -use \phpbb\request\request_interface; +use bantu\IniGetWrapper\IniGetWrapper; +use phpbb\files\factory; +use phpbb\files\filespec; +use phpbb\language\language; +use phpbb\request\request_interface; class remote extends base { diff --git a/phpBB/phpbb/files/types/type_interface.php b/phpBB/phpbb/files/types/type_interface.php index adfbb75293..e07078349a 100644 --- a/phpBB/phpbb/files/types/type_interface.php +++ b/phpBB/phpbb/files/types/type_interface.php @@ -13,7 +13,7 @@ namespace phpbb\files\types; -use \phpbb\files\upload; +use phpbb\files\upload; interface type_interface { diff --git a/phpBB/phpbb/files/upload.php b/phpBB/phpbb/files/upload.php index bd379924e7..45ff1c372f 100644 --- a/phpBB/phpbb/files/upload.php +++ b/phpBB/phpbb/files/upload.php @@ -13,9 +13,9 @@ namespace phpbb\files; -use \phpbb\filesystem\filesystem_interface; -use \phpbb\language\language; -use \phpbb\request\request_interface; +use phpbb\filesystem\filesystem_interface; +use phpbb\language\language; +use phpbb\request\request_interface; /** * File upload class From 40e614f56436447dffd272351e23b79c2da9fa3f Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Wed, 9 Sep 2015 12:58:22 +0200 Subject: [PATCH 64/66] [ticket/13904] Fix tests after changes to factory PHPBB3-13904 --- phpBB/includes/functions_posting.php | 2 +- phpBB/phpbb/avatar/driver/upload.php | 4 ++-- phpBB/phpbb/files/upload.php | 2 +- tests/functional/fileupload_remote_test.php | 8 ++++---- tests/upload/fileupload_test.php | 6 +++--- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/phpBB/includes/functions_posting.php b/phpBB/includes/functions_posting.php index 378d14d14d..cd7c9a82f7 100644 --- a/phpBB/includes/functions_posting.php +++ b/phpBB/includes/functions_posting.php @@ -433,7 +433,7 @@ function upload_attachment($form_name, $forum_id, $local = false, $local_storage $upload->set_allowed_extensions(array_keys($extensions['_allowed_'])); /** @var \phpbb\files\filespec $file */ - $file = ($local) ? $upload->handle_upload('local', $local_storage, $local_filedata) : $upload->handle_upload('form', $form_name); + $file = ($local) ? $upload->handle_upload('files.types.local', $local_storage, $local_filedata) : $upload->handle_upload('files.types.form', $form_name); if ($file->init_error()) { diff --git a/phpBB/phpbb/avatar/driver/upload.php b/phpBB/phpbb/avatar/driver/upload.php index 2bd23a26a8..a0c23cb624 100644 --- a/phpBB/phpbb/avatar/driver/upload.php +++ b/phpBB/phpbb/avatar/driver/upload.php @@ -116,7 +116,7 @@ class upload extends \phpbb\avatar\driver\driver if (!empty($upload_file['name'])) { - $file = $upload->handle_upload('form', 'avatar_upload_file'); + $file = $upload->handle_upload('files.types.form', 'avatar_upload_file'); } else if (!empty($this->config['allow_avatar_remote_upload']) && !empty($url)) { @@ -146,7 +146,7 @@ class upload extends \phpbb\avatar\driver\driver return false; } - $file = $upload->handle_upload('remote', $url); + $file = $upload->handle_upload('files.types.remote', $url); } else { diff --git a/phpBB/phpbb/files/upload.php b/phpBB/phpbb/files/upload.php index 45ff1c372f..e011e714e5 100644 --- a/phpBB/phpbb/files/upload.php +++ b/phpBB/phpbb/files/upload.php @@ -197,7 +197,7 @@ class upload { $args = func_get_args(); array_shift($args); - $type_class = $this->factory->get('types.' . $type) + $type_class = $this->factory->get($type) ->set_upload($this); return (is_object($type_class)) ? call_user_func_array(array($type_class, 'upload'), $args) : false; diff --git a/tests/functional/fileupload_remote_test.php b/tests/functional/fileupload_remote_test.php index 3349ffc576..7e0f192b40 100644 --- a/tests/functional/fileupload_remote_test.php +++ b/tests/functional/fileupload_remote_test.php @@ -78,7 +78,7 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case $upload->set_error_prefix('') ->set_allowed_extensions(array('jpg')) ->set_max_filesize(100); - $file = $upload->handle_upload('remote', self::$root_url . 'develop/blank.gif'); + $file = $upload->handle_upload('files.types.remote', self::$root_url . 'develop/blank.gif'); $this->assertEquals('URL_INVALID', $file->error[0]); } @@ -89,7 +89,7 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case $upload->set_error_prefix('') ->set_allowed_extensions(array('jpg')) ->set_max_filesize(100); - $file = $upload->handle_upload('remote', self::$root_url . 'develop/blank.jpg'); + $file = $upload->handle_upload('files.types.remote', self::$root_url . 'develop/blank.jpg'); $this->assertEquals('EMPTY_REMOTE_DATA', $file->error[0]); } @@ -100,7 +100,7 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case $upload->set_error_prefix('') ->set_allowed_extensions(array('gif')) ->set_max_filesize(1000); - $file = $upload->handle_upload('remote', self::$root_url . 'styles/prosilver/theme/images/forum_read.gif'); + $file = $upload->handle_upload('files.types.remote', self::$root_url . 'styles/prosilver/theme/images/forum_read.gif'); $this->assertEquals(0, sizeof($file->error)); $this->assertTrue(file_exists($file->get('filename'))); $this->assertTrue($file->is_uploaded()); @@ -113,7 +113,7 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case $upload->set_error_prefix('') ->set_allowed_extensions(array('gif')) ->set_max_filesize(100); - $file = $upload->handle_upload('remote', self::$root_url . 'styles/prosilver/theme/images/forum_read.gif'); + $file = $upload->handle_upload('files.types.remote', self::$root_url . 'styles/prosilver/theme/images/forum_read.gif'); $this->assertEquals(1, sizeof($file->error)); $this->assertEquals('WRONG_FILESIZE', $file->error[0]); } diff --git a/tests/upload/fileupload_test.php b/tests/upload/fileupload_test.php index 01b625efbb..211e72a6e9 100644 --- a/tests/upload/fileupload_test.php +++ b/tests/upload/fileupload_test.php @@ -185,7 +185,7 @@ class phpbb_fileupload_test extends phpbb_test_case ->set_max_filesize(1000); copy($this->path . 'jpg', $this->path . 'jpg.jpg'); - $file = $upload->handle_upload('local', $this->path . 'jpg.jpg'); + $file = $upload->handle_upload('files.types.local', $this->path . 'jpg.jpg'); $this->assertEquals(0, sizeof($file->error)); $this->assertFalse($file->additional_checks()); $this->assertTrue($file->move_file('../tests/upload/fixture/copies', true)); @@ -199,7 +199,7 @@ class phpbb_fileupload_test extends phpbb_test_case ->set_max_filesize(1000); copy($this->path . 'jpg', $this->path . 'jpg.jpg'); - $file = $upload->handle_upload('local', $this->path . 'jpg.jpg'); + $file = $upload->handle_upload('files.types.local', $this->path . 'jpg.jpg'); $this->assertEquals(0, sizeof($file->error)); $this->assertFalse($file->move_file('../tests/upload/fixture')); $this->assertFalse($file->file_moved); @@ -214,7 +214,7 @@ class phpbb_fileupload_test extends phpbb_test_case copy($this->path . 'jpg', $this->path . 'jpg.jpg'); copy($this->path . 'jpg', $this->path . 'copies/jpg.jpg'); - $file = $upload->handle_upload('local', $this->path . 'jpg.jpg'); + $file = $upload->handle_upload('files.types.local', $this->path . 'jpg.jpg'); $this->assertEquals(0, sizeof($file->error)); $file->move_file('../tests/upload/fixture/copies', true); $this->assertEquals(0, sizeof($file->error)); From 759dc9bb84d712c11148a9689d294c09aa0f81d4 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Sun, 13 Sep 2015 09:30:56 +0200 Subject: [PATCH 65/66] [ticket/13904] Set properties to protected where possible in filespec PHPBB3-13904 --- phpBB/phpbb/files/filespec.php | 8 ++++---- tests/files/types_local_test.php | 4 +++- tests/upload/filespec_test.php | 31 +++++++++++++++++++------------ tests/upload/fileupload_test.php | 2 +- 4 files changed, 27 insertions(+), 18 deletions(-) diff --git a/phpBB/phpbb/files/filespec.php b/phpBB/phpbb/files/filespec.php index 7a42e6bd50..7fc9e923ea 100644 --- a/phpBB/phpbb/files/filespec.php +++ b/phpBB/phpbb/files/filespec.php @@ -34,10 +34,10 @@ class filespec protected $mimetype = ''; /** @var string File extension */ - public $extension = ''; + protected $extension = ''; /** @var int File size */ - public $filesize = 0; + protected $filesize = 0; /** @var int Width of file */ protected $width = 0; @@ -55,10 +55,10 @@ class filespec protected $destination_path = ''; /** @var bool Whether file was moved */ - public $file_moved = false; + protected $file_moved = false; /** @var bool Whether file is local */ - public $local = false; + protected $local = false; /** @var bool Class initialization flag */ protected $class_initialized = false; diff --git a/tests/files/types_local_test.php b/tests/files/types_local_test.php index 1367bbfcf8..f4fa7fad3f 100644 --- a/tests/files/types_local_test.php +++ b/tests/files/types_local_test.php @@ -144,7 +144,9 @@ class phpbb_files_types_local_test extends phpbb_test_case new \phpbb\mimetype\guesser(array( 'mimetype.extension_guesser' => new \phpbb\mimetype\extension_guesser(), ))); - $filespec->local = true; + $filespec_local = new ReflectionProperty($filespec, 'local'); + $filespec_local->setAccessible(true); + $filespec_local->setValue($filespec, true); $this->container->set('files.filespec', $filespec); $this->factory = new \phpbb\files\factory($this->container); diff --git a/tests/upload/filespec_test.php b/tests/upload/filespec_test.php index 08c064698c..1351b46002 100644 --- a/tests/upload/filespec_test.php +++ b/tests/upload/filespec_test.php @@ -82,6 +82,13 @@ class phpbb_filespec_test extends phpbb_test_case $this->phpbb_root_path = $phpbb_root_path; } + private function set_reflection_property(&$class, $property_name, $value) + { + $property = new ReflectionProperty($class, $property_name); + $property->setAccessible(true); + $property->setValue($class, $value); + } + private function get_filespec($override = array()) { // Initialise a blank filespec object for use with trivial methods @@ -141,8 +148,8 @@ class phpbb_filespec_test extends phpbb_test_case $upload = new phpbb_mock_fileupload(); $filespec = $this->get_filespec(); $filespec->set_upload_namespace($upload); - $filespec->file_moved = true; - $filespec->filesize = $filespec->get_filesize($this->path . $filename); + $this->set_reflection_property($filespec, 'file_moved', true); + $this->set_reflection_property($filespec, 'filesize', $filespec->get_filesize($this->path . $filename)); $this->assertEquals($expected, $filespec->additional_checks()); } @@ -153,7 +160,7 @@ class phpbb_filespec_test extends phpbb_test_case $filespec = $this->get_filespec(); $filespec->set_upload_namespace($upload); $upload->valid_dimensions = false; - $filespec->file_moved = true; + $this->set_reflection_property($filespec, 'file_moved', true); $upload->max_filesize = 0; $this->assertEquals(false, $filespec->additional_checks()); @@ -381,12 +388,12 @@ class phpbb_filespec_test extends phpbb_test_case 'name' => $realname, 'type' => $mime_type, )); - $filespec->extension = $extension; + $this->set_reflection_property($filespec, 'extension', $extension); $filespec->set_upload_namespace($upload); - $filespec->local = true; + $this->set_reflection_property($filespec, 'local', true); $this->assertEquals($expected, $filespec->move_file($this->path . 'copies')); - $this->assertEquals($filespec->file_moved, file_exists($this->path . 'copies/' . $realname)); + $this->assertEquals($filespec->get('file_moved'), file_exists($this->path . 'copies/' . $realname)); if ($error) { $this->assertEquals($error, $filespec->error[0]); @@ -437,12 +444,12 @@ class phpbb_filespec_test extends phpbb_test_case $upload->max_filesize = self::UPLOAD_MAX_FILESIZE; $filespec = new \phpbb\files\filespec($this->filesystem, $this->language, $php_ini, new \FastImageSize\FastImagesize, '', $this->mimetype_guesser); $filespec->set_upload_ary($upload_ary); - $filespec->local = false; - $filespec->extension = 'gif'; + $this->set_reflection_property($filespec, 'local', false); + $this->set_reflection_property($filespec, 'extension', 'gif'); $filespec->set_upload_namespace($upload); $this->assertEquals($move_success, $filespec->move_file($this->path . 'copies')); - $this->assertEquals($filespec->file_moved, file_exists($this->path . 'copies/gif_moved')); + $this->assertEquals($filespec->get('file_moved'), file_exists($this->path . 'copies/gif_moved')); $this->assertSame($expected_error, $filespec->error); } @@ -505,12 +512,12 @@ class phpbb_filespec_test extends phpbb_test_case $upload->max_filesize = self::UPLOAD_MAX_FILESIZE; $filespec = new \phpbb\files\filespec($this->filesystem, $this->language, new \bantu\IniGetWrapper\IniGetWrapper, $imagesize, '', $this->mimetype_guesser); $filespec->set_upload_ary($upload_ary); - $filespec->local = false; - $filespec->extension = 'gif'; + $this->set_reflection_property($filespec, 'local', false); + $this->set_reflection_property($filespec, 'extension', 'gif'); $filespec->set_upload_namespace($upload); $this->assertEquals(true, $filespec->move_file($this->path . 'copies')); - $this->assertEquals($filespec->file_moved, file_exists($this->path . 'copies/gif_moved')); + $this->assertEquals($filespec->get('file_moved'), file_exists($this->path . 'copies/gif_moved')); $this->assertSame($expected_error, $filespec->error); } diff --git a/tests/upload/fileupload_test.php b/tests/upload/fileupload_test.php index 211e72a6e9..05cb8dcf93 100644 --- a/tests/upload/fileupload_test.php +++ b/tests/upload/fileupload_test.php @@ -202,7 +202,7 @@ class phpbb_fileupload_test extends phpbb_test_case $file = $upload->handle_upload('files.types.local', $this->path . 'jpg.jpg'); $this->assertEquals(0, sizeof($file->error)); $this->assertFalse($file->move_file('../tests/upload/fixture')); - $this->assertFalse($file->file_moved); + $this->assertFalse($file->get('file_moved')); $this->assertEquals(1, sizeof($file->error)); } From 6651c1d8f4aead15f4750989670f75721390ee21 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Sun, 13 Sep 2015 09:31:23 +0200 Subject: [PATCH 66/66] [ticket/13904] Use filespec's get_filesize instead of calling filesize() PHPBB3-13904 --- phpBB/phpbb/files/filespec.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phpBB/phpbb/files/filespec.php b/phpBB/phpbb/files/filespec.php index 7fc9e923ea..2ff2a92c83 100644 --- a/phpBB/phpbb/files/filespec.php +++ b/phpBB/phpbb/files/filespec.php @@ -145,7 +145,7 @@ class filespec $this->extension = strtolower(self::get_extension($this->realname)); // Try to get real filesize from temporary folder (not always working) ;) - $this->filesize = (@filesize($this->filename)) ? @filesize($this->filename) : $this->filesize; + $this->filesize = ($this->get_filesize($this->filename)) ?: $this->filesize; $this->width = $this->height = 0; $this->file_moved = false; @@ -494,7 +494,7 @@ class filespec } // Try to get real filesize from destination folder - $this->filesize = (@filesize($this->destination_file)) ? @filesize($this->destination_file) : $this->filesize; + $this->filesize = ($this->get_filesize($this->destination_file)) ?: $this->filesize; // Get mimetype of supplied file $this->mimetype = $this->get_mimetype($this->destination_file);