[ticket/13904] No longer use fileerror class for extending filespec class

PHPBB3-13904
This commit is contained in:
Marc Alexander 2015-06-02 11:48:55 +02:00
parent eb11973ea8
commit a53825ad76
3 changed files with 40 additions and 28 deletions

View file

@ -118,6 +118,20 @@ class filespec
return !isset($this->filename); 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 * Cleans destination filename
* *

View file

@ -306,14 +306,12 @@ class upload
if (!preg_match('#^(https?://).*?\.(' . implode('|', $this->allowed_extensions) . ')$#i', $upload_url, $match)) if (!preg_match('#^(https?://).*?\.(' . implode('|', $this->allowed_extensions) . ')$#i', $upload_url, $match))
{ {
$file = new fileerror($user->lang[$this->error_prefix . 'URL_INVALID']); return $this->factory->get('filespec')->set_error($user->lang[$this->error_prefix . 'URL_INVALID']);
return $file;
} }
if (empty($match[2])) if (empty($match[2]))
{ {
$file = new fileerror($user->lang[$this->error_prefix . 'URL_INVALID']); return $this->factory->get('filespec')->set_error($user->lang[$this->error_prefix . 'URL_INVALID']);e;
return $file;
} }
$url = parse_url($upload_url); $url = parse_url($upload_url);
@ -362,8 +360,7 @@ class upload
if (!($fsock = @fsockopen($host, $port, $errno, $errstr))) if (!($fsock = @fsockopen($host, $port, $errno, $errstr)))
{ {
$file = new fileerror($user->lang[$this->error_prefix . 'NOT_UPLOADED']); return $this->factory->get('filespec')->set_error($user->lang[$this->error_prefix . 'NOT_UPLOADED']);
return $file;
} }
// Make sure $path not beginning with / // Make sure $path not beginning with /
@ -404,8 +401,7 @@ class upload
{ {
$max_filesize = get_formatted_filesize($remote_max_filesize, false); $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 $this->factory->get('filespec')->set_error(sprintf($user->lang[$this->error_prefix . 'WRONG_FILESIZE'], $max_filesize['value'], $max_filesize['unit']));
return $file;
} }
$data .= $block; $data .= $block;
@ -432,14 +428,12 @@ class upload
{ {
$max_filesize = get_formatted_filesize($remote_max_filesize, false); $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 $this->factory->get('filespec')->set_error(sprintf($user->lang[$this->error_prefix . 'WRONG_FILESIZE'], $max_filesize['value'], $max_filesize['unit']));
return $file;
} }
} }
else if (stripos($line, '404 not found') !== false) else if (stripos($line, '404 not found') !== false)
{ {
$file = new fileerror($user->lang[$this->error_prefix . 'URL_NOT_FOUND']); return $this->factory->get('filespec')->set_error($this->error_prefix . 'URL_NOT_FOUND');
return $file;
} }
} }
} }
@ -449,16 +443,14 @@ class upload
// Cancel upload if we exceed timeout // Cancel upload if we exceed timeout
if (!empty($stream_meta_data['timed_out']) || time() >= $timer_stop) if (!empty($stream_meta_data['timed_out']) || time() >= $timer_stop)
{ {
$file = new fileerror($user->lang[$this->error_prefix . 'REMOTE_UPLOAD_TIMEOUT']); return $this->factory->get('filespec')->set_error($this->error_prefix . 'REMOTE_UPLOAD_TIMEOUT');
return $file;
} }
} }
@fclose($fsock); @fclose($fsock);
if (empty($data)) if (empty($data))
{ {
$file = new fileerror($user->lang[$this->error_prefix . 'EMPTY_REMOTE_DATA']); return $this->factory->get('filespec')->set_error($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'; $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'))) if (!($fp = @fopen($filename, 'wb')))
{ {
$file = new fileerror($user->lang[$this->error_prefix . 'NOT_UPLOADED']); return $this->factory->get('filespec')->set_error($this->error_prefix . 'NOT_UPLOADED');
return $file;
} }
$upload_ary['size'] = fwrite($fp, $data); $upload_ary['size'] = fwrite($fp, $data);

View file

@ -22,6 +22,12 @@ class phpbb_fileupload_test extends phpbb_test_case
private $filesystem; private $filesystem;
/** @var \Symfony\Component\DependencyInjection\ContainerInterface */
protected $container;
/** @var \phpbb\files\factory */
protected $factory;
protected function setUp() protected function setUp()
{ {
// Global $config required by unique_id // 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(); $this->filesystem = $phpbb_filesystem = new \phpbb\filesystem\filesystem();
$phpbb_container = new phpbb_mock_container_builder($this->phpbb_root_path, $this->phpEx); $this->container = new phpbb_mock_container_builder($this->phpbb_root_path, $this->phpEx);
$phpbb_container->set('files.filespec', new \phpbb\files\filespec( $this->container->set('files.filespec', new \phpbb\files\filespec(
$this->filesystem, $this->filesystem,
new \phpbb\mimetype\guesser(array( new \phpbb\mimetype\guesser(array(
'mimetype.extension_guesser' => new \phpbb\mimetype\extension_guesser(), 'mimetype.extension_guesser' => new \phpbb\mimetype\extension_guesser(),
)))); ))));
$this->factory = new \phpbb\files\factory($this->container);
$this->path = __DIR__ . '/fixture/'; $this->path = __DIR__ . '/fixture/';
} }
@ -76,7 +83,7 @@ class phpbb_fileupload_test extends phpbb_test_case
public function test_common_checks_invalid_extension() 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')) $upload->set_allowed_extensions(array('png'))
->set_max_filesize(100); ->set_max_filesize(100);
$file = $this->gen_valid_filespec(); $file = $this->gen_valid_filespec();
@ -86,7 +93,7 @@ class phpbb_fileupload_test extends phpbb_test_case
public function test_common_checks_invalid_filename() 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')) $upload->set_allowed_extensions(array('jpg'))
->set_max_filesize(100); ->set_max_filesize(100);
$file = $this->gen_valid_filespec(); $file = $this->gen_valid_filespec();
@ -97,7 +104,7 @@ class phpbb_fileupload_test extends phpbb_test_case
public function test_common_checks_too_large() 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')) $upload->set_allowed_extensions(array('jpg'))
->set_max_filesize(100); ->set_max_filesize(100);
$file = $this->gen_valid_filespec(); $file = $this->gen_valid_filespec();
@ -108,7 +115,7 @@ class phpbb_fileupload_test extends phpbb_test_case
public function test_common_checks_valid_file() 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')) $upload->set_allowed_extensions(array('jpg'))
->set_max_filesize(1000); ->set_max_filesize(1000);
$file = $this->gen_valid_filespec(); $file = $this->gen_valid_filespec();
@ -118,7 +125,7 @@ class phpbb_fileupload_test extends phpbb_test_case
public function test_local_upload() 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')) $upload->set_allowed_extensions(array('jpg'))
->set_max_filesize(1000); ->set_max_filesize(1000);
@ -130,7 +137,7 @@ class phpbb_fileupload_test extends phpbb_test_case
public function test_move_existent_file() 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')) $upload->set_allowed_extensions(array('jpg'))
->set_max_filesize(1000); ->set_max_filesize(1000);
@ -144,7 +151,7 @@ class phpbb_fileupload_test extends phpbb_test_case
public function test_move_existent_file_overwrite() 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')) $upload->set_allowed_extensions(array('jpg'))
->set_max_filesize(1000); ->set_max_filesize(1000);
@ -159,7 +166,7 @@ class phpbb_fileupload_test extends phpbb_test_case
public function test_valid_dimensions() 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) $upload->set_allowed_extensions(false)
->set_max_filesize(false) ->set_max_filesize(false)
->set_allowed_dimensions(1, 1, 100, 100); ->set_allowed_dimensions(1, 1, 100, 100);