1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
<?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\attachment;
use phpbb\auth\auth;
use \phpbb\cache\service;
use \phpbb\config\config;
use \phpbb\event\dispatcher;
use \phpbb\language\language;
use \phpbb\mimetype\guesser;
use \phpbb\plupload\plupload;
use \phpbb\user;
/**
* Attachment upload class
*/
class upload
{
/** @var auth */
protected $auth;
/** @var service */
protected $cache;
/** @var config */
protected $config;
/** @var \phpbb\files\upload Upload class */
protected $files_upload;
/** @var \phpbb\language\language */
protected $language;
/** @var guesser Mimetype guesser */
protected $mimetype_guesser;
/** @var dispatcher */
protected $phpbb_dispatcher;
/** @var plupload Plupload */
protected $plupload;
/** @var user */
protected $user;
/** @var \phpbb\files\filespec Current filespec instance */
private $file;
/** @var array File data */
private $file_data = array(
'error' => array()
);
/** @var array Extensions array */
private $extensions;
/**
* Constructor for attachments upload class
*
* @param auth $auth
* @param service $cache
* @param config $config
* @param \phpbb\files\upload $files_upload
* @param language $language
* @param guesser $mimetype_guesser
* @param dispatcher $phpbb_dispatcher
* @param plupload $plupload
* @param user $user
* @param $phpbb_root_path
*/
public function __construct(auth $auth, service $cache, config $config, \phpbb\files\upload $files_upload, language $language, guesser $mimetype_guesser, dispatcher $phpbb_dispatcher, plupload $plupload, user $user, $phpbb_root_path)
{
$this->auth = $auth;
$this->cache = $cache;
$this->config = $config;
$this->files_upload = $files_upload;
$this->language = $language;
$this->mimetype_guesser = $mimetype_guesser;
$this->phpbb_dispatcher = $phpbb_dispatcher;
$this->plupload = $plupload;
$this->user = $user;
$this->phpbb_root_path = $phpbb_root_path;
}
/**
* Upload Attachment - filedata is generated here
* Uses upload class
*
* @param string $form_name The form name of the file upload input
* @param int $forum_id The id of the forum
* @param bool $local Whether the file is local or not
* @param string $local_storage The path to the local file
* @param bool $is_message Whether it is a PM or not
* @param array $local_filedata An file data object created for the local file
*
* @return array File data array
*/
public function upload($form_name, $forum_id, $local = false, $local_storage = '', $is_message = false, $local_filedata = array())
{
$this->init_files_upload($forum_id, $is_message);
$this->file_data['post_attach'] = $local || $this->files_upload->is_valid($form_name);
if (!$this->file_data['post_attach'])
{
$this->file_data['error'][] = $this->language->lang('NO_UPLOAD_FORM_FOUND');
return $this->file_data;
}
$this->file = ($local) ? $this->files_upload->handle_upload('files.types.local', $local_storage, $local_filedata) : $this->files_upload->handle_upload('files.types.form', $form_name);
if ($this->file->init_error())
{
$this->file_data['post_attach'] = false;
return $this->file_data;
}
// Whether the uploaded file is in the image category
$is_image = (isset($this->extensions[$this->file->get('extension')]['display_cat'])) ? $this->extensions[$this->file->get('extension')]['display_cat'] == ATTACHMENT_CATEGORY_IMAGE : false;
if (!$this->auth->acl_get('a_') && !$this->auth->acl_get('m_', $forum_id))
{
// Check Image Size, if it is an image
if ($is_image)
{
$this->file->upload->set_allowed_dimensions(0, 0, $this->config['img_max_width'], $this->config['img_max_height']);
}
// Admins and mods are allowed to exceed the allowed filesize
if (!empty($this->extensions[$this->file->get('extension')]['max_filesize']))
{
$allowed_filesize = $this->extensions[$this->file->get('extension')]['max_filesize'];
}
else
{
$allowed_filesize = ($is_message) ? $this->config['max_filesize_pm'] : $this->config['max_filesize'];
}
$this->file->upload->set_max_filesize($allowed_filesize);
}
$this->file->clean_filename('unique', $this->user->data['user_id'] . '_');
// Are we uploading an image *and* this image being within the image category?
// Only then perform additional image checks.
$this->file->move_file($this->config['upload_path'], false, !$is_image);
// Do we have to create a thumbnail?
$this->file_data['thumbnail'] = ($is_image && $this->config['img_create_thumbnail']) ? 1 : 0;
// Make sure the image category only holds valid images...
$this->check_image($is_image);
if (sizeof($this->file->error))
{
$this->file->remove();
$this->file_data['error'] = array_merge($this->file_data['error'], $this->file->error);
$this->file_data['post_attach'] = false;
return $this->file_data;
}
$this->fill_file_data();
$filedata = $this->file_data;
/**
* Event to modify uploaded file before submit to the post
*
* @event core.modify_uploaded_file
* @var array filedata Array containing uploaded file data
* @var bool is_image Flag indicating if the file is an image
* @since 3.1.0-RC3
*/
$vars = array(
'filedata',
'is_image',
);
extract($this->phpbb_dispatcher->trigger_event('core.modify_uploaded_file', compact($vars)));
$this->file_data = $filedata;
unset($filedata);
// Check for attachment quota and free space
if (!$this->check_attach_quota() || !$this->check_disk_space())
{
return $this->file_data;
}
// Create Thumbnail
$this->create_thumbnail();
return $this->file_data;
}
/**
* Create thumbnail for file if necessary
*
* @return array Updated $filedata
*/
protected function create_thumbnail()
{
if ($this->file_data['thumbnail'])
{
$source = $this->file->get('destination_file');
$destination = $this->file->get('destination_path') . '/thumb_' . $this->file->get('realname');
if (!create_thumbnail($source, $destination, $this->file->get('mimetype')))
{
$this->file_data['thumbnail'] = 0;
}
}
}
/**
* Init files upload class
*
* @param int $forum_id Forum ID
* @param bool $is_message Whether attachment is inside PM or not
*/
protected function init_files_upload($forum_id, $is_message)
{
if ($this->config['check_attachment_content'] && isset($this->config['mime_triggers']))
{
$this->files_upload->set_disallowed_content(explode('|', $this->config['mime_triggers']));
}
else if (!$this->config['check_attachment_content'])
{
$this->files_upload->set_disallowed_content(array());
}
$this->extensions = $this->cache->obtain_attach_extensions((($is_message) ? false : (int) $forum_id));
$this->files_upload->set_allowed_extensions(array_keys($this->extensions['_allowed_']));
}
/**
* Check if uploaded file is really an image
*
* @param bool $is_image Whether file is image
*/
protected function check_image($is_image)
{
// Make sure the image category only holds valid images...
if ($is_image && !$this->file->is_image())
{
$this->file->remove();
if ($this->plupload && $this->plupload->is_active())
{
$this->plupload->emit_error(104, 'ATTACHED_IMAGE_NOT_IMAGE');
}
// If this error occurs a user tried to exploit an IE Bug by renaming extensions
// Since the image category is displaying content inline we need to catch this.
$this->file->set_error($this->language->lang('ATTACHED_IMAGE_NOT_IMAGE'));
}
}
/**
* Check if attachment quota was reached
*
* @return bool False if attachment quota was reached, true if not
*/
protected function check_attach_quota()
{
if ($this->config['attachment_quota'])
{
if (intval($this->config['upload_dir_size']) + $this->file->get('filesize') > $this->config['attachment_quota'])
{
$this->file_data['error'][] = $this->language->lang('ATTACH_QUOTA_REACHED');
$this->file_data['post_attach'] = false;
$this->file->remove();
return false;
}
}
return true;
}
/**
* Check if there is enough free space available on disk
*
* @return bool True if disk space is available, false if not
*/
protected function check_disk_space()
{
if ($free_space = @disk_free_space($this->phpbb_root_path . $this->config['upload_path']))
{
if ($free_space <= $this->file->get('filesize'))
{
if ($this->auth->acl_get('a_'))
{
$this->file_data['error'][] = $this->language->lang('ATTACH_DISK_FULL');
}
else
{
$this->file_data['error'][] = $this->language->lang('ATTACH_QUOTA_REACHED');
}
$this->file_data['post_attach'] = false;
$this->file->remove();
return false;
}
}
return true;
}
/**
* Fills file data with file information and current time as filetime
*/
protected function fill_file_data()
{
$this->file_data['filesize'] = $this->file->get('filesize');
$this->file_data['mimetype'] = $this->file->get('mimetype');
$this->file_data['extension'] = $this->file->get('extension');
$this->file_data['physical_filename'] = $this->file->get('realname');
$this->file_data['real_filename'] = $this->file->get('uploadname');
$this->file_data['filetime'] = time();
}
}
|