Skip to content

Commit 1af6f05

Browse files
committed
[ticket/13904] Load upload class using factory
PHPBB3-13904
1 parent d2be8e1 commit 1af6f05

6 files changed

Lines changed: 47 additions & 32 deletions

File tree

phpBB/config/default/container/services_files.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,9 @@ services:
99
- @filesystem
1010
- @mimetype.guesser
1111
- @plupload
12+
13+
files.upload:
14+
class: phpbb\files\upload
15+
scope: prototype
16+
arguments:
17+
- @filesystem

phpBB/includes/functions_posting.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,14 +404,14 @@ function posting_gen_topic_types($forum_id, $cur_topic_type = POST_NORMAL)
404404
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)
405405
{
406406
global $auth, $user, $config, $db, $cache;
407-
global $phpbb_root_path, $phpEx, $phpbb_dispatcher, $phpbb_filesystem;
407+
global $phpbb_root_path, $phpEx, $phpbb_dispatcher, $phpbb_container;
408408

409409
$filedata = array(
410410
'error' => array()
411411
);
412412

413413
include_once($phpbb_root_path . 'includes/functions_upload.' . $phpEx);
414-
$upload = new fileupload($phpbb_filesystem);
414+
$upload = $phpbb_container->get('files.upload');
415415

416416
if ($config['check_attachment_content'] && isset($config['mime_triggers']))
417417
{
@@ -433,6 +433,7 @@ function upload_attachment($form_name, $forum_id, $local = false, $local_storage
433433
$extensions = $cache->obtain_attach_extensions((($is_message) ? false : (int) $forum_id));
434434
$upload->set_allowed_extensions(array_keys($extensions['_allowed_']));
435435

436+
/** @var \phpbb\files\filespec $file */
436437
$file = ($local) ? $upload->local_upload($local_storage, $local_filedata, $mimetype_guesser) : $upload->form_upload($form_name, $mimetype_guesser, $plupload);
437438

438439
if ($file->init_error())

phpBB/phpbb/avatar/driver/remote.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,8 @@ public function process_form($request, $template, $user, $row, &$error)
114114
return false;
115115
}
116116

117-
if (!class_exists('fileupload'))
118-
{
119-
include($this->phpbb_root_path . 'includes/functions_upload.' . $this->php_ext);
120-
}
121-
122-
$types = \fileupload::image_types();
123-
$extension = strtolower(\filespec::get_extension($url));
117+
$types = \phpbb\files\upload::image_types();
118+
$extension = strtolower(\phpbb\files\filespec::get_extension($url));
124119

125120
// Check if this is actually an image
126121
if ($file_stream = @fopen($url, 'r'))

phpBB/phpbb/avatar/driver/upload.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ class upload extends \phpbb\avatar\driver\driver
3333
*/
3434
protected $dispatcher;
3535

36+
/**
37+
* @var \phpbb\files\factory
38+
*/
39+
protected $files_factory;
40+
3641
/**
3742
* Construct a driver object
3843
*
@@ -43,9 +48,10 @@ class upload extends \phpbb\avatar\driver\driver
4348
* @param \phpbb\path_helper $path_helper phpBB path helper
4449
* @param \phpbb\mimetype\guesser $mimetype_guesser Mimetype guesser
4550
* @param \phpbb\event\dispatcher_interface $dispatcher phpBB Event dispatcher object
51+
* @param \phpbb\files\factory $files_factory File classes factory
4652
* @param \phpbb\cache\driver\driver_interface $cache Cache driver
4753
*/
48-
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)
54+
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)
4955
{
5056
$this->config = $config;
5157
$this->phpbb_root_path = $phpbb_root_path;
@@ -54,6 +60,7 @@ public function __construct(\phpbb\config\config $config, $phpbb_root_path, $php
5460
$this->path_helper = $path_helper;
5561
$this->mimetype_guesser = $mimetype_guesser;
5662
$this->dispatcher = $dispatcher;
63+
$this->files_factory = $files_factory;
5764
$this->cache = $cache;
5865
}
5966

@@ -99,12 +106,17 @@ public function process_form($request, $template, $user, $row, &$error)
99106
return false;
100107
}
101108

102-
if (!class_exists('fileupload'))
103-
{
104-
include($this->phpbb_root_path . 'includes/functions_upload.' . $this->php_ext);
105-
}
106-
107-
$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));
109+
/** @var \phpbb\files\upload $upload */
110+
$upload = $this->files_factory->get('upload')
111+
->set_error_prefix('AVATAR_')
112+
->set_allowed_extensions($this->allowed_extensions)
113+
->set_max_filesize($this->config['avatar_filesize'])
114+
->set_allowed_dimensions(
115+
$this->config['avatar_min_width'],
116+
$this->config['avatar_min_height'],
117+
$this->config['avatar_max_width'],
118+
$this->config['avatar_max_height'])
119+
->set_disallowed_content((isset($this->config['mime_triggers']) ? explode('|', $this->config['mime_triggers']) : false));
108120

109121
$url = $request->variable('avatar_upload_url', '');
110122
$upload_file = $request->file('avatar_upload_file');

phpBB/phpbb/files/upload.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,15 @@ class upload
4040
* Init file upload class.
4141
*
4242
* @param \phpbb\filesystem\filesystem_interface $filesystem
43-
* @param string $error_prefix Used error messages will get prefixed by this string
44-
* @param array $allowed_extensions Array of allowed extensions, for example array('jpg', 'jpeg', 'gif', 'png')
45-
* @param int $max_filesize Maximum filesize
46-
* @param int $min_width Minimum image width (only checked for images)
47-
* @param int $min_height Minimum image height (only checked for images)
48-
* @param int $max_width Maximum image width (only checked for images)
49-
* @param int $max_height Maximum image height (only checked for images)
50-
* @param bool|array $disallowed_content If enabled, the first 256 bytes of the file must not
51-
* contain any of its values. Defaults to false.
5243
*
5344
*/
54-
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)
45+
public function __construct(\phpbb\filesystem\filesystem_interface $filesystem)
5546
{
56-
$this->set_allowed_extensions($allowed_extensions);
57-
$this->set_max_filesize($max_filesize);
58-
$this->set_allowed_dimensions($min_width, $min_height, $max_width, $max_height);
59-
$this->set_error_prefix($error_prefix);
60-
$this->set_disallowed_content($disallowed_content);
47+
// $this->set_allowed_extensions($allowed_extensions);
48+
// $this->set_max_filesize($max_filesize);
49+
// $this->set_allowed_dimensions($min_width, $min_height, $max_width, $max_height);
50+
// $this->set_error_prefix($error_prefix);
51+
// $this->set_disallowed_content($disallowed_content);
6152
$this->filesystem = $filesystem;
6253
}
6354

@@ -82,6 +73,8 @@ function set_allowed_extensions($allowed_extensions)
8273
{
8374
$this->allowed_extensions = $allowed_extensions;
8475
}
76+
77+
return $this;
8578
}
8679

8780
/**
@@ -93,6 +86,8 @@ function set_allowed_dimensions($min_width, $min_height, $max_width, $max_height
9386
$this->min_height = (int) $min_height;
9487
$this->max_width = (int) $max_width;
9588
$this->max_height = (int) $max_height;
89+
90+
return $this;
9691
}
9792

9893
/**
@@ -104,6 +99,8 @@ function set_max_filesize($max_filesize)
10499
{
105100
$this->max_filesize = (int) $max_filesize;
106101
}
102+
103+
return $this;
107104
}
108105

109106
/**
@@ -115,6 +112,8 @@ function set_disallowed_content($disallowed_content)
115112
{
116113
$this->disallowed_content = array_diff($disallowed_content, array(''));
117114
}
115+
116+
return $this;
118117
}
119118

120119
/**
@@ -123,6 +122,8 @@ function set_disallowed_content($disallowed_content)
123122
function set_error_prefix($error_prefix)
124123
{
125124
$this->error_prefix = $error_prefix;
125+
126+
return $this;
126127
}
127128

128129
/**

phpBB/phpbb/plupload/plupload.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ protected function temporary_filepath($file_name)
303303
$this->temporary_directory,
304304
$this->config['plupload_salt'],
305305
md5($file_name),
306-
\filespec::get_extension($file_name)
306+
\phpbb\files\filespec::get_extension($file_name)
307307
);
308308
}
309309

0 commit comments

Comments
 (0)