mirror of
https://github.com/phpbb/phpbb.git
synced 2025-08-01 14:30:32 +02:00
Merge pull request #4894 from rubencm/ticket/15276
[ticket/15276] Use storage in avatars * github.com:phpbb/phpbb: (34 commits) [ticket/15276] Changed annotation [ticket/15276] Remove unused code [ticket/15276] Revert some changes [ticket/15276] Use IniGetWrapper [ticket/15276] Add missing dependency [ticket/15276] Remove unused dependency [ticket/15276] Add missing properties [ticket/15276] Use InitGetWrapper [ticket/15276] Fix comments [ticket/15276] Fix code and add phpdoc [ticket/15276] Use stream_copy_to_stream [ticket/15276] Fix typo [ticket/15276] Use mimetype guesser [ticket/15276] Update file_info to get size of images [ticket/15276] Update [ticket/15276] Remove avatar_path [ticket/15276] Remove avatar_path from acp [ticket/15276] Use finfo to get mimetype [ticket/15276] Update file_info [ticket/15276] Fix code style ...
This commit is contained in:
@@ -13,15 +13,18 @@
|
||||
|
||||
namespace phpbb\avatar\driver;
|
||||
|
||||
use bantu\IniGetWrapper\IniGetWrapper;
|
||||
use \phpbb\storage\exception\exception as storage_exception;
|
||||
|
||||
/**
|
||||
* Handles avatars uploaded to the board
|
||||
*/
|
||||
class upload extends \phpbb\avatar\driver\driver
|
||||
{
|
||||
/**
|
||||
* @var \phpbb\filesystem\filesystem_interface
|
||||
* @var \phpbb\storage\storage
|
||||
*/
|
||||
protected $filesystem;
|
||||
protected $storage;
|
||||
|
||||
/**
|
||||
* @var \phpbb\event\dispatcher_interface
|
||||
@@ -33,28 +36,33 @@ class upload extends \phpbb\avatar\driver\driver
|
||||
*/
|
||||
protected $files_factory;
|
||||
|
||||
/**
|
||||
* @var IniGetWrapper
|
||||
*/
|
||||
protected $php_ini;
|
||||
|
||||
/**
|
||||
* Construct a driver object
|
||||
*
|
||||
* @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\storage\storage phpBB avatar storage
|
||||
* @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
|
||||
* @param IniGetWrapper $php_ini ini_get() wrapper
|
||||
*/
|
||||
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)
|
||||
public function __construct(\phpbb\config\config $config, $phpbb_root_path, $php_ext, \phpbb\storage\storage $storage, \phpbb\path_helper $path_helper, \phpbb\event\dispatcher_interface $dispatcher, \phpbb\files\factory $files_factory, IniGetWrapper $php_ini)
|
||||
{
|
||||
$this->config = $config;
|
||||
$this->phpbb_root_path = $phpbb_root_path;
|
||||
$this->php_ext = $php_ext;
|
||||
$this->filesystem = $filesystem;
|
||||
$this->storage = $storage;
|
||||
$this->path_helper = $path_helper;
|
||||
$this->dispatcher = $dispatcher;
|
||||
$this->files_factory = $files_factory;
|
||||
$this->cache = $cache;
|
||||
$this->php_ini = $php_ini;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -116,7 +124,7 @@ class upload extends \phpbb\avatar\driver\driver
|
||||
|
||||
if (!empty($upload_file['name']))
|
||||
{
|
||||
$file = $upload->handle_upload('files.types.form', 'avatar_upload_file');
|
||||
$file = $upload->handle_upload('files.types.form_storage', 'avatar_upload_file');
|
||||
}
|
||||
else if (!empty($this->config['allow_avatar_remote_upload']) && !empty($url))
|
||||
{
|
||||
@@ -156,7 +164,7 @@ class upload extends \phpbb\avatar\driver\driver
|
||||
return false;
|
||||
}
|
||||
|
||||
$file = $upload->handle_upload('files.types.remote', $url);
|
||||
$file = $upload->handle_upload('files.types.remote_storage', $url);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -169,26 +177,11 @@ class upload extends \phpbb\avatar\driver\driver
|
||||
// If there was an error during upload, then abort operation
|
||||
if (count($file->error))
|
||||
{
|
||||
$file->remove();
|
||||
$file->remove($this->storage);
|
||||
$error = $file->error;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Calculate new destination
|
||||
$destination = $this->config['avatar_path'];
|
||||
|
||||
// Adjust destination path (no trailing slash)
|
||||
if (substr($destination, -1, 1) == '/' || substr($destination, -1, 1) == '\\')
|
||||
{
|
||||
$destination = substr($destination, 0, -1);
|
||||
}
|
||||
|
||||
$destination = str_replace(array('../', '..\\', './', '.\\'), '', $destination);
|
||||
if ($destination && ($destination[0] == '/' || $destination[0] == "\\"))
|
||||
{
|
||||
$destination = '';
|
||||
}
|
||||
|
||||
$filedata = array(
|
||||
'filename' => $file->get('filename'),
|
||||
'filesize' => $file->get('filesize'),
|
||||
@@ -203,7 +196,6 @@ class upload extends \phpbb\avatar\driver\driver
|
||||
*
|
||||
* @event core.avatar_driver_upload_move_file_before
|
||||
* @var array filedata Array containing uploaded file data
|
||||
* @var string destination Destination directory where the file is going to be moved
|
||||
* @var string prefix Prefix for the avatar filename
|
||||
* @var array row Array with avatar row data
|
||||
* @var array error Array of errors, if filled in by this event file will not be moved
|
||||
@@ -212,7 +204,6 @@ class upload extends \phpbb\avatar\driver\driver
|
||||
*/
|
||||
$vars = array(
|
||||
'filedata',
|
||||
'destination',
|
||||
'prefix',
|
||||
'row',
|
||||
'error',
|
||||
@@ -224,14 +215,14 @@ class upload extends \phpbb\avatar\driver\driver
|
||||
if (!count($error))
|
||||
{
|
||||
// Move file and overwrite any existing image
|
||||
$file->move_file($destination, true);
|
||||
$file->move_file($this->storage, true);
|
||||
}
|
||||
|
||||
// If there was an error during move, then clean up leftovers
|
||||
$error = array_merge($error, $file->error);
|
||||
if (count($error))
|
||||
{
|
||||
$file->remove();
|
||||
$file->remove($this->storage);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -257,7 +248,6 @@ class upload extends \phpbb\avatar\driver\driver
|
||||
return array(
|
||||
'allow_avatar_remote_upload'=> array('lang' => 'ALLOW_REMOTE_UPLOAD', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => true),
|
||||
'avatar_filesize' => array('lang' => 'MAX_FILESIZE', 'validate' => 'int:0', 'type' => 'number:0', 'explain' => true, 'append' => ' ' . $user->lang['BYTES']),
|
||||
'avatar_path' => array('lang' => 'AVATAR_STORAGE_PATH', 'validate' => 'rpath', 'type' => 'text:20:255', 'explain' => true),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -268,37 +258,35 @@ class upload extends \phpbb\avatar\driver\driver
|
||||
{
|
||||
|
||||
$error = array();
|
||||
$destination = $this->config['avatar_path'];
|
||||
$prefix = $this->config['avatar_salt'] . '_';
|
||||
$ext = substr(strrchr($row['avatar'], '.'), 1);
|
||||
$filename = $this->phpbb_root_path . $destination . '/' . $prefix . $row['id'] . '.' . $ext;
|
||||
$filename = $prefix . $row['id'] . '.' . $ext;
|
||||
|
||||
/**
|
||||
* Before deleting an existing avatar
|
||||
*
|
||||
* @event core.avatar_driver_upload_delete_before
|
||||
* @var string destination Destination directory where the file is going to be deleted
|
||||
* @var string prefix Prefix for the avatar filename
|
||||
* @var array row Array with avatar row data
|
||||
* @var array error Array of errors, if filled in by this event file will not be deleted
|
||||
* @since 3.1.6-RC1
|
||||
* @changed 3.3.0-a1 Remove destination
|
||||
*/
|
||||
$vars = array(
|
||||
'destination',
|
||||
'prefix',
|
||||
'row',
|
||||
'error',
|
||||
);
|
||||
extract($this->dispatcher->trigger_event('core.avatar_driver_upload_delete_before', compact($vars)));
|
||||
|
||||
if (!count($error) && $this->filesystem->exists($filename))
|
||||
if (!count($error) && $this->storage->exists($filename))
|
||||
{
|
||||
try
|
||||
{
|
||||
$this->filesystem->remove($filename);
|
||||
$this->storage->delete($filename);
|
||||
return true;
|
||||
}
|
||||
catch (\phpbb\filesystem\exception\filesystem_exception $e)
|
||||
catch (storage_exception $e)
|
||||
{
|
||||
// Fail is covered by return statement below
|
||||
}
|
||||
@@ -316,12 +304,12 @@ class upload extends \phpbb\avatar\driver\driver
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if user is able to upload an avatar
|
||||
* Check if user is able to upload an avatar to a temporary folder
|
||||
*
|
||||
* @return bool True if user can upload, false if not
|
||||
*/
|
||||
protected function can_upload()
|
||||
{
|
||||
return ($this->filesystem->exists($this->phpbb_root_path . $this->config['avatar_path']) && $this->filesystem->is_writable($this->phpbb_root_path . $this->config['avatar_path']) && (@ini_get('file_uploads') || strtolower(@ini_get('file_uploads')) == 'on'));
|
||||
return $this->php_ini->getBool('file_uploads');
|
||||
}
|
||||
}
|
||||
|
26
phpBB/phpbb/db/migration/data/v330/storage_avatar.php
Normal file
26
phpBB/phpbb/db/migration/data/v330/storage_avatar.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?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\db\migration\data\v330;
|
||||
|
||||
class storage_avatar extends \phpbb\db\migration\migration
|
||||
{
|
||||
public function update_data()
|
||||
{
|
||||
return array(
|
||||
array('config.add', array('storage\\avatar\\provider', \phpbb\storage\provider\local::class)),
|
||||
array('config.add', array('storage\\avatar\\config\\path', $this->config['avatar_path'])),
|
||||
array('config.remove', array('avatar_path')),
|
||||
);
|
||||
}
|
||||
}
|
@@ -25,21 +25,12 @@ class form extends base
|
||||
/** @var factory Files factory */
|
||||
protected $factory;
|
||||
|
||||
/** @var language */
|
||||
protected $language;
|
||||
|
||||
/** @var IniGetWrapper */
|
||||
protected $php_ini;
|
||||
|
||||
/** @var plupload */
|
||||
protected $plupload;
|
||||
|
||||
/** @var request_interface */
|
||||
protected $request;
|
||||
|
||||
/** @var \phpbb\files\upload */
|
||||
protected $upload;
|
||||
|
||||
/**
|
||||
* Construct a form upload type
|
||||
*
|
||||
|
128
phpBB/phpbb/files/types/form_storage.php
Normal file
128
phpBB/phpbb/files/types/form_storage.php
Normal file
@@ -0,0 +1,128 @@
|
||||
<?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 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_storage extends base
|
||||
{
|
||||
/** @var factory Files factory */
|
||||
protected $factory;
|
||||
|
||||
/** @var plupload */
|
||||
protected $plupload;
|
||||
|
||||
/** @var request_interface */
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* Construct a form upload type
|
||||
*
|
||||
* @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, 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function upload()
|
||||
{
|
||||
$args = func_get_args();
|
||||
return $this->form_upload($args[0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
*
|
||||
* @return filespec $file Object "filespec" is returned, all further operations can be done with this object
|
||||
*/
|
||||
protected function form_upload($form_name)
|
||||
{
|
||||
$upload = $this->request->file($form_name);
|
||||
unset($upload['local_mode']);
|
||||
|
||||
$result = $this->plupload->handle_upload($form_name);
|
||||
if (is_array($result))
|
||||
{
|
||||
$upload = array_merge($upload, $result);
|
||||
}
|
||||
|
||||
/** @var filespec $file */
|
||||
$file = $this->factory->get('filespec_storage')
|
||||
->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->upload->error_prefix . 'EMPTY_FILEUPLOAD');
|
||||
return $file;
|
||||
}
|
||||
|
||||
// PHP Upload file size check
|
||||
$file = $this->check_upload_size($file);
|
||||
if (sizeof($file->error))
|
||||
{
|
||||
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);
|
||||
|
||||
return $file;
|
||||
}
|
||||
}
|
@@ -24,18 +24,9 @@ class local extends base
|
||||
/** @var factory Files factory */
|
||||
protected $factory;
|
||||
|
||||
/** @var language */
|
||||
protected $language;
|
||||
|
||||
/** @var IniGetWrapper */
|
||||
protected $php_ini;
|
||||
|
||||
/** @var request_interface */
|
||||
protected $request;
|
||||
|
||||
/** @var \phpbb\files\upload */
|
||||
protected $upload;
|
||||
|
||||
/**
|
||||
* Construct a form upload type
|
||||
*
|
||||
|
@@ -28,18 +28,9 @@ class remote extends base
|
||||
/** @var factory Files factory */
|
||||
protected $factory;
|
||||
|
||||
/** @var language */
|
||||
protected $language;
|
||||
|
||||
/** @var IniGetWrapper */
|
||||
protected $php_ini;
|
||||
|
||||
/** @var request_interface */
|
||||
protected $request;
|
||||
|
||||
/** @var \phpbb\files\upload */
|
||||
protected $upload;
|
||||
|
||||
/** @var string phpBB root path */
|
||||
protected $phpbb_root_path;
|
||||
|
||||
|
197
phpBB/phpbb/files/types/remote_storage.php
Normal file
197
phpBB/phpbb/files/types/remote_storage.php
Normal file
@@ -0,0 +1,197 @@
|
||||
<?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 bantu\IniGetWrapper\IniGetWrapper;
|
||||
use phpbb\config\config;
|
||||
use phpbb\files\factory;
|
||||
use phpbb\files\filespec;
|
||||
use phpbb\language\language;
|
||||
use phpbb\request\request_interface;
|
||||
|
||||
class remote_storage extends base
|
||||
{
|
||||
/** @var config phpBB config */
|
||||
protected $config;
|
||||
|
||||
/** @var factory Files factory */
|
||||
protected $factory;
|
||||
|
||||
/** @var request_interface */
|
||||
protected $request;
|
||||
|
||||
/** @var string phpBB root path */
|
||||
protected $phpbb_root_path;
|
||||
|
||||
/**
|
||||
* Construct a form upload type
|
||||
*
|
||||
* @param config $config phpBB config
|
||||
* @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(config $config, factory $factory, language $language, IniGetWrapper $php_ini, request_interface $request, $phpbb_root_path)
|
||||
{
|
||||
$this->config = $config;
|
||||
$this->factory = $factory;
|
||||
$this->language = $language;
|
||||
$this->php_ini = $php_ini;
|
||||
$this->request = $request;
|
||||
$this->phpbb_root_path = $phpbb_root_path;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@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
|
||||
*/
|
||||
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'));
|
||||
}
|
||||
|
||||
$url = parse_url($upload_url);
|
||||
|
||||
$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 : '');
|
||||
|
||||
$remote_max_filesize = $this->get_max_file_size();
|
||||
|
||||
$guzzle_options = [
|
||||
'timeout' => $this->upload->upload_timeout,
|
||||
'connect_timeout' => $this->upload->upload_timeout,
|
||||
'verify' => !empty($this->config['remote_upload_verify']) ? (bool) $this->config['remote_upload_verify'] : false,
|
||||
];
|
||||
$client = new \GuzzleHttp\Client($guzzle_options);
|
||||
|
||||
try
|
||||
{
|
||||
$response = $client->get($upload_url, $guzzle_options);
|
||||
}
|
||||
catch (\GuzzleHttp\Exception\ClientException $clientException)
|
||||
{
|
||||
return $this->factory->get('filespec')->set_error($this->upload->error_prefix . 'URL_NOT_FOUND');
|
||||
}
|
||||
catch (\GuzzleHttp\Exception\RequestException $requestException)
|
||||
{
|
||||
if (strpos($requestException->getMessage(), 'cURL error 28') !== false || preg_match('/408|504/', $requestException->getCode()))
|
||||
{
|
||||
return $this->factory->get('filespec')->set_error($this->upload->error_prefix . 'REMOTE_UPLOAD_TIMEOUT');
|
||||
}
|
||||
else
|
||||
{
|
||||
return $this->factory->get('filespec')->set_error($this->language->lang($this->upload->error_prefix . 'NOT_UPLOADED'));
|
||||
}
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
return $this->factory->get('filespec')->set_error($this->language->lang($this->upload->error_prefix . 'NOT_UPLOADED'));
|
||||
}
|
||||
|
||||
$content_length = $response->getBody()->getSize();
|
||||
if ($remote_max_filesize && $content_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']));
|
||||
}
|
||||
|
||||
if ($content_length === 0)
|
||||
{
|
||||
return $this->factory->get('filespec')->set_error($this->upload->error_prefix . 'EMPTY_REMOTE_DATA');
|
||||
}
|
||||
|
||||
$data = $response->getBody();
|
||||
|
||||
$filename = tempnam(sys_get_temp_dir(), 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_storage')
|
||||
->set_upload_ary($upload_ary)
|
||||
->set_upload_namespace($this->upload);
|
||||
$this->upload->common_checks($file);
|
||||
|
||||
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 = $this->php_ini->getString('upload_max_filesize');
|
||||
|
||||
if (!empty($max_file_size))
|
||||
{
|
||||
$unit = strtolower(substr($max_file_size, -1, 1));
|
||||
$max_file_size = (int) $max_file_size;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
@@ -18,6 +18,8 @@ use phpbb\storage\exception\exception;
|
||||
use phpbb\filesystem\exception\filesystem_exception;
|
||||
use phpbb\filesystem\filesystem;
|
||||
use phpbb\filesystem\helper as filesystem_helper;
|
||||
use phpbb\mimetype\guesser;
|
||||
use FastImageSize\FastImageSize;
|
||||
|
||||
/**
|
||||
* @internal Experimental
|
||||
@@ -31,6 +33,20 @@ class local implements adapter_interface, stream_interface
|
||||
*/
|
||||
protected $filesystem;
|
||||
|
||||
/**
|
||||
* FastImageSize
|
||||
*
|
||||
* @var \FastImageSize\FastImageSize
|
||||
*/
|
||||
protected $imagesize;
|
||||
|
||||
/**
|
||||
* Mimetype Guesser component
|
||||
*
|
||||
* @var \phpbb\mimetype\guesser
|
||||
*/
|
||||
protected $mimetype_guesser;
|
||||
|
||||
/**
|
||||
* @var string path
|
||||
*/
|
||||
@@ -44,9 +60,11 @@ class local implements adapter_interface, stream_interface
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public function __construct(filesystem $filesystem, $phpbb_root_path)
|
||||
public function __construct(filesystem $filesystem, FastImageSize $imagesize, guesser $mimetype_guesser, $phpbb_root_path)
|
||||
{
|
||||
$this->filesystem = $filesystem;
|
||||
$this->imagesize = $imagesize;
|
||||
$this->mimetype_guesser = $mimetype_guesser;
|
||||
$this->phpbb_root_path = $phpbb_root_path;
|
||||
}
|
||||
|
||||
@@ -235,4 +253,82 @@ class local implements adapter_interface, stream_interface
|
||||
throw new exception('STORAGE_CANNOT_COPY_RESOURCE');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get file size.
|
||||
*
|
||||
* @param string $path The file
|
||||
*
|
||||
* @throws \phpbb\storage\exception\exception When cannot get size
|
||||
*
|
||||
* @return array Properties
|
||||
*/
|
||||
public function file_size($path)
|
||||
{
|
||||
$size = filesize($this->root_path . $path);
|
||||
|
||||
if ($size === null)
|
||||
{
|
||||
throw new exception('STORAGE_CANNOT_GET_FILESIZE');
|
||||
}
|
||||
|
||||
return ['size' => $size];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get file mimetype.
|
||||
*
|
||||
* @param string $path The file
|
||||
*
|
||||
* @return array Properties
|
||||
*/
|
||||
public function file_mimetype($path)
|
||||
{
|
||||
return ['mimetype' => $this->mimetype_guesser->guess($this->root_path . $path)];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get image dimensions.
|
||||
*
|
||||
* @param string $path The file
|
||||
*
|
||||
* @return array Properties
|
||||
*/
|
||||
protected function image_dimensions($path)
|
||||
{
|
||||
$size = $this->imagesize->getImageSize($this->root_path . $path);
|
||||
|
||||
// For not supported types like swf
|
||||
if ($size === false)
|
||||
{
|
||||
$imsize = getimagesize($this->root_path . $path);
|
||||
$size = ['width' => $imsize[0], 'height' => $imsize[1]];
|
||||
}
|
||||
|
||||
return ['image_width' => $size['width'], 'image_height' => $size['height']];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get image width.
|
||||
*
|
||||
* @param string $path The file
|
||||
*
|
||||
* @return array Properties
|
||||
*/
|
||||
public function file_image_width($path)
|
||||
{
|
||||
return $this->image_dimensions($path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get image height.
|
||||
*
|
||||
* @param string $path The file
|
||||
*
|
||||
* @return array Properties
|
||||
*/
|
||||
public function file_image_height($path)
|
||||
{
|
||||
return $this->image_dimensions($path);
|
||||
}
|
||||
}
|
||||
|
85
phpBB/phpbb/storage/file_info.php
Normal file
85
phpBB/phpbb/storage/file_info.php
Normal file
@@ -0,0 +1,85 @@
|
||||
<?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\storage;
|
||||
|
||||
use phpbb\storage\exception\exception;
|
||||
use phpbb\storage\adapter\adapter_interface;
|
||||
|
||||
class file_info
|
||||
{
|
||||
/**
|
||||
* @var \phpbb\storage\adapter\adapter_interface
|
||||
*/
|
||||
protected $adapter;
|
||||
|
||||
/**
|
||||
* Path of the file
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $path;
|
||||
|
||||
/**
|
||||
* Stores the properties of $path file, so dont have to be consulted multiple times.
|
||||
* For example, when you need the width of an image, using getimagesize() you get
|
||||
* both dimensions, so you store both here, and when you get the height, you dont have
|
||||
* to call getimagesize() again.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $properties;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param \Symfony\Component\DependencyInjection\ContainerInterface $adapter
|
||||
* @param string $path
|
||||
*/
|
||||
public function __construct(adapter_interface $adapter, $path)
|
||||
{
|
||||
$this->adapter = $adapter;
|
||||
$this->path = $path;
|
||||
$this->properties = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Load propertys lazily.
|
||||
*
|
||||
* @param string name The property name.
|
||||
*
|
||||
* @return string Returns the property value
|
||||
*/
|
||||
public function get($name)
|
||||
{
|
||||
if (!isset($this->properties[$name]))
|
||||
{
|
||||
if (!method_exists($this->adapter, 'file_' . $name))
|
||||
{
|
||||
throw new exception('STORAGE_METHOD_NOT_IMPLEMENTED');
|
||||
}
|
||||
|
||||
$this->properties = array_merge($this->properties, call_user_func([$this->adapter, 'file_' . $name], $this->path));
|
||||
}
|
||||
|
||||
return $this->properties[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of \phpbb\storage\file_info->get()
|
||||
*/
|
||||
public function __get($name)
|
||||
{
|
||||
return $this->get($name);
|
||||
}
|
||||
}
|
@@ -192,4 +192,18 @@ class storage
|
||||
$adapter->put_contents($path, stream_get_contents($resource));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get file info.
|
||||
*
|
||||
* @param string $path The file
|
||||
*
|
||||
* @throws \phpbb\storage\exception\not_implemented When the adapter doesnt implement the method
|
||||
*
|
||||
* @return \phpbb\storage\file_info Returns file_info object
|
||||
*/
|
||||
public function file_info($path)
|
||||
{
|
||||
return new file_info($this->adapter, $path);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user