aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/files/upload.php
blob: 234eb697355dd7f01ea48c84928ca44e6000fd80 (plain)
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
<?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;

use \phpbb\filesystem\filesystem_interface;
use \phpbb\language\language;
use \phpbb\request\request_interface;

/**
 * File upload class
 * Init class (all parameters optional and able to be set/overwritten separately) - scope is global and valid for all uploads
 */
class upload
{
	/** @var array Allowed file extensions */
	var $allowed_extensions = array();

	/** @var array Disallowed content */
	var $disallowed_content = array('body', 'head', 'html', 'img', 'plaintext', 'a href', 'pre', 'script', 'table', 'title');

	/** @var int Maximum filesize */
	var $max_filesize = 0;

	/** @var int Minimum width of images */
	var $min_width = 0;

	/** @var int Minimum height of images */
	var $min_height = 0;

	/** @var int Maximum width of images */
	var $max_width = 0;

	/** @var int Maximum height of images */
	var $max_height = 0;

	/** @var string Prefix for language variables of errors */
	var $error_prefix = '';

	/** @var int Timeout for remote upload */
	var $upload_timeout = 6;

	/** @var filesystem_interface */
	protected $filesystem;

	/** @var \phpbb\files\factory Files factory */
	protected $factory;

	/** @var \phpbb\language\language Language class */
	protected $language;

	/** @var request_interface Request class */
	protected $request;

	/** @var string phpBB root path */
	protected $phpbb_root_path;

	/**
	 * Init file upload class.
	 *
	 * @param filesystem_interface $filesystem
	 * @param factory $factory Files factory
	 * @param language $language Language class
	 * @param request_interface $request Request class
	 * @param string $phpbb_root_path phpBB root path
	 */
	public function __construct(filesystem_interface $filesystem, factory $factory, language $language, request_interface $request, $phpbb_root_path)
	{
		$this->filesystem = $filesystem;
		$this->factory = $factory;
		$this->language = $language;
		$this->request = $request;
		$this->phpbb_root_path = $phpbb_root_path;
	}

	/**
	 * Reset vars
	 */
	function reset_vars()
	{
		$this->max_filesize = 0;
		$this->min_width = $this->min_height = $this->max_width = $this->max_height = 0;
		$this->error_prefix = '';
		$this->allowed_extensions = array();
		$this->disallowed_content = array();
	}

	/**
	 * Set allowed extensions
	 *
	 * @param array $allowed_extensions Allowed file extensions
	 *
	 * @return \phpbb\files\upload This instance of upload
	 */
	function set_allowed_extensions($allowed_extensions)
	{
		if ($allowed_extensions !== false && is_array($allowed_extensions))
		{
			$this->allowed_extensions = $allowed_extensions;
		}

		return $this;
	}

	/**
	 * Set allowed dimensions
	 *
	 * @param int $min_width Minimum image width
	 * @param int $min_height Minimum image height
	 * @param int $max_width Maximum image width
	 * @param int $max_height Maximum image height
	 *
	 * @return \phpbb\files\upload This instance of upload
	 */
	function set_allowed_dimensions($min_width, $min_height, $max_width, $max_height)
	{
		$this->min_width = (int) $min_width;
		$this->min_height = (int) $min_height;
		$this->max_width = (int) $max_width;
		$this->max_height = (int) $max_height;

		return $this;
	}

	/**
	 * Set maximum allowed file size
	 *
	 * @param int $max_filesize Maximum file size
	 *
	 * @return \phpbb\files\upload This instance of upload
	 */
	function set_max_filesize($max_filesize)
	{
		if ($max_filesize !== false && (int) $max_filesize)
		{
			$this->max_filesize = (int) $max_filesize;
		}

		return $this;
	}

	/**
	 * Set disallowed strings
	 *
	 * @param array $disallowed_content Disallowed content
	 *
	 * @return \phpbb\files\upload This instance of upload
	 */
	function set_disallowed_content($disallowed_content)
	{
		if ($disallowed_content !== false && is_array($disallowed_content))
		{
			$this->disallowed_content = array_diff($disallowed_content, array(''));
		}

		return $this;
	}

	/**
	 * Set error prefix
	 *
	 * @param string $error_prefix Prefix for language variables of errors
	 *
	 * @return \phpbb\files\upload This instance of upload
	 */
	function set_error_prefix($error_prefix)
	{
		$this->error_prefix = $error_prefix;

		return $this;
	}

	/**
	 * Handle upload based on type
	 *
	 * @param string $type Upload type
	 *
	 * @return \phpbb\files\filespec|bool A filespec instance if upload was
	 *		successful, false if there were issues or the type is not supported
	 */
	public function handle_upload($type)
	{
		$args = func_get_args();
		array_shift($args);
		$type_class = $this->factory->get('types.' . $type)
			->set_upload($this);

		return (is_object($type_class)) ? call_user_func_array(array($type_class, 'upload'), $args) : false;
	}

	/**
	 * Assign internal error
	 *
	 * @param string $errorcode Error code to assign
	 *
	 * @return string Error string
	 * @access public
	 */
	public function assign_internal_error($errorcode)
	{
		switch ($errorcode)
		{
			case UPLOAD_ERR_INI_SIZE:
				$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');
				}

				$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));
			break;

			case UPLOAD_ERR_FORM_SIZE:
				$max_filesize = get_formatted_filesize($this->max_filesize, false);

				$error = $this->language->lang($this->error_prefix . 'WRONG_FILESIZE', $max_filesize['value'], $max_filesize['unit']);
			break;

			case UPLOAD_ERR_PARTIAL:
				$error = $this->language->lang($this->error_prefix . 'PARTIAL_UPLOAD');
			break;

			case UPLOAD_ERR_NO_FILE:
				$error = $this->language->lang($this->error_prefix . 'NOT_UPLOADED');
			break;

			case UPLOAD_ERR_NO_TMP_DIR:
				$error = 'Temporary folder could not be found. Please check your PHP installation.';
			break;

			case UPLOAD_ERR_CANT_WRITE:
				$error = 'Can’t write to temporary folder.';
			break;

			case UPLOAD_ERR_EXTENSION:
				$error = 'A PHP extension has stopped the file upload.';
			break;

			default:
				$error = false;
			break;
		}

		return $error;
	}

	/**
	 * Perform common file checks
	 *
	 * @param filespec $file Instance of filespec class
	 */
	function common_checks(&$file)
	{
		// Filesize is too big or it's 0 if it was larger than the maxsize in the upload form
		if ($this->max_filesize && ($file->get('filesize') > $this->max_filesize || $file->get('filesize') == 0))
		{
			$max_filesize = get_formatted_filesize($this->max_filesize, false);

			$file->error[] = $this->language->lang($this->error_prefix . 'WRONG_FILESIZE', $max_filesize['value'], $max_filesize['unit']);
		}

		// check Filename
		if (preg_match("#[\\/:*?\"<>|]#i", $file->get('realname')))
		{
			$file->error[] = $this->language->lang($this->error_prefix . 'INVALID_FILENAME', $file->get('realname'));
		}

		// Invalid Extension
		if (!$this->valid_extension($file))
		{
			$file->error[] = $this->language->lang($this->error_prefix . 'DISALLOWED_EXTENSION', $file->get('extension'));
		}

		// MIME Sniffing
		if (!$this->valid_content($file))
		{
			$file->error[] = $this->language->lang($this->error_prefix . 'DISALLOWED_CONTENT');
		}
	}

	/**
	 * Check for allowed extension
	 *
	 * @param filespec $file Instance of filespec class
	 *
	 * @return bool True if extension is allowed, false if not
	 */
	function valid_extension(&$file)
	{
		return (in_array($file->get('extension'), $this->allowed_extensions)) ? true : false;
	}

	/**
	 * Check for allowed dimension
	 *
	 * @param filespec $file Instance of filespec class
	 *
	 * @return bool True if dimensions are valid or no constraints set, false
	 *			if not
	 */
	function valid_dimensions(&$file)
	{
		if (!$this->max_width && !$this->max_height && !$this->min_width && !$this->min_height)
		{
			return true;
		}

		if (($file->get('width') > $this->max_width && $this->max_width) ||
			($file->get('height') > $this->max_height && $this->max_height) ||
			($file->get('width') < $this->min_width && $this->min_width) ||
			($file->get('height') < $this->min_height && $this->min_height))
		{
			return false;
		}

		return true;
	}

	/**
	 * Check if form upload is valid
	 *
	 * @param string $form_name Name of form
	 *
	 * @return bool True if form upload is valid, false if not
	 */
	function is_valid($form_name)
	{
		$upload = $this->request->file($form_name);

		return (!empty($upload) && $upload['name'] !== 'none');
	}


	/**
	 * Check for bad content (IE mime-sniffing)
	 *
	 * @param filespec $file Instance of filespec class
	 *
	 * @return bool True if content is valid, false if not
	 */
	function valid_content(&$file)
	{
		return ($file->check_content($this->disallowed_content));
	}

	/**
	 * Get image type/extension mapping
	 *
	 * @return array Array containing the image types and their extensions
	 */
	static public function image_types()
	{
		$result = array(
			IMAGETYPE_GIF		=> array('gif'),
			IMAGETYPE_JPEG		=> array('jpg', 'jpeg'),
			IMAGETYPE_PNG		=> array('png'),
			IMAGETYPE_SWF		=> array('swf'),
			IMAGETYPE_PSD		=> array('psd'),
			IMAGETYPE_BMP		=> array('bmp'),
			IMAGETYPE_TIFF_II	=> array('tif', 'tiff'),
			IMAGETYPE_TIFF_MM	=> array('tif', 'tiff'),
			IMAGETYPE_JPC		=> array('jpg', 'jpeg'),
			IMAGETYPE_JP2		=> array('jpg', 'jpeg'),
			IMAGETYPE_JPX		=> array('jpg', 'jpeg'),
			IMAGETYPE_JB2		=> array('jpg', 'jpeg'),
			IMAGETYPE_IFF		=> array('iff'),
			IMAGETYPE_WBMP		=> array('wbmp'),
			IMAGETYPE_XBM		=> array('xbm'),
		);

		if (defined('IMAGETYPE_SWC'))
		{
			$result[IMAGETYPE_SWC] = array('swc');
		}

		return $result;
	}
}