mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-08 04:18:52 +00:00
[ticket/13904] Fix minor issues and move local_upload to its own class
PHPBB3-13904
This commit is contained in:
parent
cf9b6ed474
commit
adcc901af1
6 changed files with 188 additions and 109 deletions
|
@ -29,7 +29,17 @@ services:
|
||||||
|
|
||||||
files.types.form:
|
files.types.form:
|
||||||
class: phpbb\files\types\form
|
class: phpbb\files\types\form
|
||||||
|
scope: prototype
|
||||||
arguments:
|
arguments:
|
||||||
- @files.factory
|
- @files.factory
|
||||||
|
- @language
|
||||||
- @plupload
|
- @plupload
|
||||||
- @request
|
- @request
|
||||||
|
|
||||||
|
files.types.local:
|
||||||
|
class: phpbb\files\types\form
|
||||||
|
scope: prototype
|
||||||
|
arguments:
|
||||||
|
- @files.factory
|
||||||
|
- @language
|
||||||
|
- @request
|
||||||
|
|
|
@ -433,7 +433,7 @@ function upload_attachment($form_name, $forum_id, $local = false, $local_storage
|
||||||
$upload->set_allowed_extensions(array_keys($extensions['_allowed_']));
|
$upload->set_allowed_extensions(array_keys($extensions['_allowed_']));
|
||||||
|
|
||||||
/** @var \phpbb\files\filespec $file */
|
/** @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())
|
if ($file->init_error())
|
||||||
{
|
{
|
||||||
|
|
|
@ -16,14 +16,18 @@ namespace phpbb\files\types;
|
||||||
use \phpbb\files\factory;
|
use \phpbb\files\factory;
|
||||||
use \phpbb\files\filespec;
|
use \phpbb\files\filespec;
|
||||||
use \phpbb\files\upload;
|
use \phpbb\files\upload;
|
||||||
|
use \phpbb\language\language;
|
||||||
use \phpbb\plupload\plupload;
|
use \phpbb\plupload\plupload;
|
||||||
use \phpbb\request\request_interface;
|
use \phpbb\request\request_interface;
|
||||||
|
|
||||||
class form implements type_interface
|
class form extends base
|
||||||
{
|
{
|
||||||
/** @var factory Files factory */
|
/** @var factory Files factory */
|
||||||
protected $factory;
|
protected $factory;
|
||||||
|
|
||||||
|
/** @var language */
|
||||||
|
protected $language;
|
||||||
|
|
||||||
/** @var plupload */
|
/** @var plupload */
|
||||||
protected $plupload;
|
protected $plupload;
|
||||||
|
|
||||||
|
@ -39,9 +43,10 @@ class form implements type_interface
|
||||||
* @param factory $factory
|
* @param factory $factory
|
||||||
* @param request_interface $request
|
* @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->factory = $factory;
|
||||||
|
$this->language = $language;
|
||||||
$this->plupload = $plupload;
|
$this->plupload = $plupload;
|
||||||
$this->request = $request;
|
$this->request = $request;
|
||||||
}
|
}
|
||||||
|
@ -79,13 +84,10 @@ class form implements type_interface
|
||||||
$upload = $this->request->file($form_name);
|
$upload = $this->request->file($form_name);
|
||||||
unset($upload['local_mode']);
|
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);
|
$upload = array_merge($upload, $result);
|
||||||
if (is_array($result))
|
|
||||||
{
|
|
||||||
$upload = array_merge($upload, $result);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @var filespec $file */
|
/** @var filespec $file */
|
||||||
|
@ -114,32 +116,21 @@ class form implements type_interface
|
||||||
// Check if empty file got uploaded (not catched by is_uploaded_file)
|
// Check if empty file got uploaded (not catched by is_uploaded_file)
|
||||||
if (isset($upload['size']) && $upload['size'] == 0)
|
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;
|
return $file;
|
||||||
}
|
}
|
||||||
|
|
||||||
// PHP Upload filesize exceeded
|
// PHP Upload filesize check
|
||||||
if ($file->get('filename') == 'none')
|
$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;
|
return $file;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Not correctly uploaded
|
// Not correctly uploaded
|
||||||
if (!$file->is_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;
|
return $file;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
137
phpBB/phpbb/files/types/local.php
Normal file
137
phpBB/phpbb/files/types/local.php
Normal file
|
@ -0,0 +1,137 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* This file is part of the phpBB Forum Software package.
|
||||||
|
*
|
||||||
|
* @copyright (c) phpBB Limited <https://www.phpbb.com>
|
||||||
|
* @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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -199,86 +199,6 @@ class upload
|
||||||
return (is_object($type_class)) ? call_user_func_array(array($type_class, 'upload'), $args) : false;
|
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
|
* Remote upload method
|
||||||
* Uploads file from given url
|
* Uploads file from given url
|
||||||
|
|
|
@ -43,7 +43,7 @@ class phpbb_fileupload_test extends phpbb_test_case
|
||||||
|
|
||||||
if (!is_array($config))
|
if (!is_array($config))
|
||||||
{
|
{
|
||||||
$config = array();
|
$config = new \phpbb\config\config(array());
|
||||||
}
|
}
|
||||||
|
|
||||||
$config['rand_seed'] = '';
|
$config['rand_seed'] = '';
|
||||||
|
@ -53,6 +53,15 @@ class phpbb_fileupload_test extends phpbb_test_case
|
||||||
|
|
||||||
$this->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->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 = new phpbb_mock_container_builder($phpbb_root_path, $phpEx);
|
||||||
$this->container->set('files.filespec', new \phpbb\files\filespec(
|
$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(),
|
'mimetype.extension_guesser' => new \phpbb\mimetype\extension_guesser(),
|
||||||
))));
|
))));
|
||||||
$this->factory = new \phpbb\files\factory($this->container);
|
$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->path = __DIR__ . '/fixture/';
|
||||||
$this->phpbb_root_path = $phpbb_root_path;
|
$this->phpbb_root_path = $phpbb_root_path;
|
||||||
|
@ -137,7 +158,7 @@ class phpbb_fileupload_test extends phpbb_test_case
|
||||||
->set_max_filesize(1000);
|
->set_max_filesize(1000);
|
||||||
|
|
||||||
copy($this->path . 'jpg', $this->path . 'jpg.jpg');
|
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->assertEquals(0, sizeof($file->error));
|
||||||
unlink($this->path . 'jpg.jpg');
|
unlink($this->path . 'jpg.jpg');
|
||||||
}
|
}
|
||||||
|
@ -149,7 +170,7 @@ class phpbb_fileupload_test extends phpbb_test_case
|
||||||
->set_max_filesize(1000);
|
->set_max_filesize(1000);
|
||||||
|
|
||||||
copy($this->path . 'jpg', $this->path . 'jpg.jpg');
|
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->assertEquals(0, sizeof($file->error));
|
||||||
$this->assertFalse($file->move_file('../tests/upload/fixture'));
|
$this->assertFalse($file->move_file('../tests/upload/fixture'));
|
||||||
$this->assertFalse($file->file_moved);
|
$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 . 'jpg.jpg');
|
||||||
copy($this->path . 'jpg', $this->path . 'copies/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));
|
$this->assertEquals(0, sizeof($file->error));
|
||||||
$file->move_file('../tests/upload/fixture/copies', true);
|
$file->move_file('../tests/upload/fixture/copies', true);
|
||||||
$this->assertEquals(0, sizeof($file->error));
|
$this->assertEquals(0, sizeof($file->error));
|
||||||
|
|
Loading…
Add table
Reference in a new issue