phpBB
Statistics
| Revision:

root / trunk / phpBB / includes / functions_upload.php

History | View | Annotate | Download (24.8 kB)

1 5110 acydburn
<?php
2 8146 acydburn
/**
3 5114 acydburn
*
4 5114 acydburn
* @package phpBB3
5 8146 acydburn
* @copyright (c) 2005 phpBB Group
6 11653 git-gate
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7 5114 acydburn
*
8 5114 acydburn
*/
9 5110 acydburn
10 5114 acydburn
/**
11 8146 acydburn
* @ignore
12 8146 acydburn
*/
13 8146 acydburn
if (!defined('IN_PHPBB'))
14 8146 acydburn
{
15 8146 acydburn
        exit;
16 8146 acydburn
}
17 8146 acydburn
18 8146 acydburn
/**
19 6650 acydburn
* Responsible for holding all file relevant information, as well as doing file-specific operations.
20 5114 acydburn
* The {@link fileupload fileupload class} can be used to upload several files, each of them being this object to operate further on.
21 6058 acydburn
* @package phpBB3
22 5114 acydburn
*/
23 5110 acydburn
class filespec
24 5110 acydburn
{
25 5110 acydburn
        var $filename = '';
26 5110 acydburn
        var $realname = '';
27 5110 acydburn
        var $uploadname = '';
28 5110 acydburn
        var $mimetype = '';
29 5110 acydburn
        var $extension = '';
30 5110 acydburn
        var $filesize = 0;
31 5110 acydburn
        var $width = 0;
32 5110 acydburn
        var $height = 0;
33 6058 acydburn
        var $image_info = array();
34 5110 acydburn
35 5110 acydburn
        var $destination_file = '';
36 5110 acydburn
        var $destination_path = '';
37 5110 acydburn
38 5110 acydburn
        var $file_moved = false;
39 5110 acydburn
        var $init_error = false;
40 5110 acydburn
        var $local = false;
41 5110 acydburn
42 5110 acydburn
        var $error = array();
43 5110 acydburn
44 5110 acydburn
        var $upload = '';
45 5110 acydburn
46 5110 acydburn
        /**
47 5110 acydburn
        * File Class
48 6312 acydburn
        * @access private
49 5110 acydburn
        */
50 5110 acydburn
        function filespec($upload_ary, $upload_namespace)
51 5110 acydburn
        {
52 5110 acydburn
                if (!isset($upload_ary))
53 5110 acydburn
                {
54 5110 acydburn
                        $this->init_error = true;
55 5110 acydburn
                        return;
56 5110 acydburn
                }
57 5110 acydburn
58 5110 acydburn
                $this->filename = $upload_ary['tmp_name'];
59 5110 acydburn
                $this->filesize = $upload_ary['size'];
60 10766 git-gate
                $name = (STRIP) ? stripslashes($upload_ary['name']) : $upload_ary['name'];
61 10766 git-gate
                $name = trim(utf8_htmlspecialchars(utf8_basename($name)));
62 10766 git-gate
                $this->realname = $this->uploadname = $name;
63 6945 acydburn
                $this->mimetype = $upload_ary['type'];
64 5110 acydburn
65 5110 acydburn
                // Opera adds the name to the mime type
66 5110 acydburn
                $this->mimetype        = (strpos($this->mimetype, '; name') !== false) ? str_replace(strstr($this->mimetype, '; name'), '', $this->mimetype) : $this->mimetype;
67 5140 acydburn
68 5140 acydburn
                if (!$this->mimetype)
69 5140 acydburn
                {
70 5140 acydburn
                        $this->mimetype = 'application/octetstream';
71 5140 acydburn
                }
72 6058 acydburn
73 5307 acydburn
                $this->extension = strtolower($this->get_extension($this->realname));
74 5110 acydburn
75 5110 acydburn
                // Try to get real filesize from temporary folder (not always working) ;)
76 5110 acydburn
                $this->filesize = (@filesize($this->filename)) ? @filesize($this->filename) : $this->filesize;
77 5110 acydburn
78 5110 acydburn
                $this->width = $this->height = 0;
79 5110 acydburn
                $this->file_moved = false;
80 5110 acydburn
81 5110 acydburn
                $this->local = (isset($upload_ary['local_mode'])) ? true : false;
82 5110 acydburn
                $this->upload = $upload_namespace;
83 5110 acydburn
        }
84 5110 acydburn
85 5110 acydburn
        /**
86 5110 acydburn
        * Cleans destination filename
87 8146 acydburn
        *
88 6831 acydburn
        * @param real|unique|unique_ext $mode real creates a realname, filtering some characters, lowering every character. Unique creates an unique filename
89 5110 acydburn
        * @param string $prefix Prefix applied to filename
90 6058 acydburn
        * @access public
91 5110 acydburn
        */
92 7429 kellanved
        function clean_filename($mode = 'unique', $prefix = '', $user_id = '')
93 5110 acydburn
        {
94 5110 acydburn
                if ($this->init_error)
95 5110 acydburn
                {
96 5110 acydburn
                        return;
97 5110 acydburn
                }
98 6058 acydburn
99 5110 acydburn
                switch ($mode)
100 5110 acydburn
                {
101 5110 acydburn
                        case 'real':
102 5622 acydburn
                                // Remove every extension from filename (to not let the mime bug being exposed)
103 5622 acydburn
                                if (strpos($this->realname, '.') !== false)
104 5622 acydburn
                                {
105 5622 acydburn
                                        $this->realname = substr($this->realname, 0, strpos($this->realname, '.'));
106 5622 acydburn
                                }
107 5622 acydburn
108 5110 acydburn
                                // Replace any chars which may cause us problems with _
109 5110 acydburn
                                $bad_chars = array("'", "\\", ' ', '/', ':', '*', '?', '"', '<', '>', '|');
110 5135 acydburn
111 5135 acydburn
                                $this->realname = rawurlencode(str_replace($bad_chars, '_', strtolower($this->realname)));
112 5135 acydburn
                                $this->realname = preg_replace("/%(\w{2})/", '_', $this->realname);
113 5135 acydburn
114 5643 acydburn
                                $this->realname = $prefix . $this->realname . '.' . $this->extension;
115 6058 acydburn
                        break;
116 5110 acydburn
117 5110 acydburn
                        case 'unique':
118 6831 acydburn
                                $this->realname = $prefix . md5(unique_id());
119 6831 acydburn
                        break;
120 6831 acydburn
121 7429 kellanved
                        case 'avatar':
122 7453 kellanved
                                $this->extension = strtolower($this->extension);
123 7429 kellanved
                                $this->realname = $prefix . $user_id . '.' . $this->extension;
124 8763 acydburn
125 7429 kellanved
                        break;
126 8763 acydburn
127 6831 acydburn
                        case 'unique_ext':
128 5110 acydburn
                        default:
129 5135 acydburn
                                $this->realname = $prefix . md5(unique_id()) . '.' . $this->extension;
130 6058 acydburn
                        break;
131 5110 acydburn
                }
132 5110 acydburn
        }
133 5110 acydburn
134 6058 acydburn
        /**
135 6058 acydburn
        * Get property from file object
136 6058 acydburn
        */
137 5110 acydburn
        function get($property)
138 5110 acydburn
        {
139 5967 acydburn
                if ($this->init_error || !isset($this->$property))
140 5110 acydburn
                {
141 5110 acydburn
                        return false;
142 5110 acydburn
                }
143 5967 acydburn
144 5110 acydburn
                return $this->$property;
145 5110 acydburn
        }
146 5110 acydburn
147 6058 acydburn
        /**
148 6058 acydburn
        * Check if file is an image (mimetype)
149 6058 acydburn
        *
150 6058 acydburn
        * @return true if it is an image, false if not
151 6058 acydburn
        */
152 5110 acydburn
        function is_image()
153 5110 acydburn
        {
154 5110 acydburn
                return (strpos($this->mimetype, 'image/') !== false) ? true : false;
155 5110 acydburn
        }
156 5110 acydburn
157 6058 acydburn
        /**
158 6058 acydburn
        * Check if the file got correctly uploaded
159 6058 acydburn
        *
160 6414 acydburn
        * @return true if it is a valid upload, false if not
161 6058 acydburn
        */
162 5110 acydburn
        function is_uploaded()
163 5110 acydburn
        {
164 5140 acydburn
                if (!$this->local && !is_uploaded_file($this->filename))
165 5140 acydburn
                {
166 5140 acydburn
                        return false;
167 5140 acydburn
                }
168 5140 acydburn
169 6414 acydburn
                if ($this->local && !file_exists($this->filename))
170 6414 acydburn
                {
171 6414 acydburn
                        return false;
172 6414 acydburn
                }
173 6414 acydburn
174 6414 acydburn
                return true;
175 5110 acydburn
        }
176 5110 acydburn
177 6058 acydburn
        /**
178 6058 acydburn
        * Remove file
179 6058 acydburn
        */
180 5110 acydburn
        function remove()
181 5110 acydburn
        {
182 5110 acydburn
                if ($this->file_moved)
183 5110 acydburn
                {
184 5110 acydburn
                        @unlink($this->destination_file);
185 5110 acydburn
                }
186 5110 acydburn
        }
187 5110 acydburn
188 5110 acydburn
        /**
189 5307 acydburn
        * Get file extension
190 5307 acydburn
        */
191 5307 acydburn
        function get_extension($filename)
192 5307 acydburn
        {
193 5307 acydburn
                if (strpos($filename, '.') === false)
194 5307 acydburn
                {
195 5307 acydburn
                        return '';
196 5307 acydburn
                }
197 5307 acydburn
198 5307 acydburn
                $filename = explode('.', $filename);
199 5307 acydburn
                return array_pop($filename);
200 5307 acydburn
        }
201 5307 acydburn
202 5307 acydburn
        /**
203 6058 acydburn
        * Get mimetype. Utilize mime_content_type if the function exist.
204 6945 acydburn
        * Not used at the moment...
205 5790 acydburn
        */
206 5790 acydburn
        function get_mimetype($filename)
207 5790 acydburn
        {
208 5824 acydburn
                $mimetype = '';
209 5824 acydburn
210 5790 acydburn
                if (function_exists('mime_content_type'))
211 5790 acydburn
                {
212 5790 acydburn
                        $mimetype = mime_content_type($filename);
213 5790 acydburn
                }
214 5790 acydburn
215 5824 acydburn
                // Some browsers choke on a mimetype of application/octet-stream
216 5824 acydburn
                if (!$mimetype || $mimetype == 'application/octet-stream')
217 5790 acydburn
                {
218 5790 acydburn
                        $mimetype = 'application/octetstream';
219 5790 acydburn
                }
220 5790 acydburn
221 5790 acydburn
                return $mimetype;
222 5790 acydburn
        }
223 5790 acydburn
224 5790 acydburn
        /**
225 5790 acydburn
        * Get filesize
226 5790 acydburn
        */
227 5790 acydburn
        function get_filesize($filename)
228 5790 acydburn
        {
229 5790 acydburn
                return @filesize($filename);
230 5790 acydburn
        }
231 8763 acydburn
232 8763 acydburn
233 8555 Kellanved
        /**
234 8555 Kellanved
        * Check the first 256 bytes for forbidden content
235 8555 Kellanved
        */
236 8555 Kellanved
        function check_content($disallowed_content)
237 8555 Kellanved
        {
238 8555 Kellanved
                if (empty($disallowed_content))
239 8555 Kellanved
                {
240 8555 Kellanved
                        return true;
241 8555 Kellanved
                }
242 8763 acydburn
243 8555 Kellanved
                $fp = @fopen($this->filename, 'rb');
244 5790 acydburn
245 8555 Kellanved
                if ($fp !== false)
246 8555 Kellanved
                {
247 8555 Kellanved
                        $ie_mime_relevant = fread($fp, 256);
248 8555 Kellanved
                        fclose($fp);
249 8555 Kellanved
                        foreach ($disallowed_content as $forbidden)
250 8555 Kellanved
                        {
251 8555 Kellanved
                                if (stripos($ie_mime_relevant, '<' . $forbidden) !== false)
252 8555 Kellanved
                                {
253 8555 Kellanved
                                        return false;
254 8555 Kellanved
                                }
255 8555 Kellanved
                        }
256 8555 Kellanved
                }
257 8555 Kellanved
                return true;
258 8555 Kellanved
        }
259 8555 Kellanved
260 5790 acydburn
        /**
261 5110 acydburn
        * Move file to destination folder
262 5110 acydburn
        * The phpbb_root_path variable will be applied to the destination path
263 5110 acydburn
        *
264 5110 acydburn
        * @param string $destination_path Destination path, for example $config['avatar_path']
265 6787 acydburn
        * @param bool $overwrite If set to true, an already existing file will be overwritten
266 8783 acydburn
        * @param string $chmod Permission mask for chmodding the file after a successful move. The mode entered here reflects the mode defined by {@link phpbb_chmod()}
267 8783 acydburn
        *
268 6058 acydburn
        * @access public
269 5110 acydburn
        */
270 8780 acydburn
        function move_file($destination, $overwrite = false, $skip_image_check = false, $chmod = false)
271 5110 acydburn
        {
272 5110 acydburn
                global $user, $phpbb_root_path;
273 5110 acydburn
274 5110 acydburn
                if (sizeof($this->error))
275 5110 acydburn
                {
276 5110 acydburn
                        return false;
277 5110 acydburn
                }
278 5110 acydburn
279 8780 acydburn
                $chmod = ($chmod === false) ? CHMOD_READ | CHMOD_WRITE : $chmod;
280 8780 acydburn
281 6364 acydburn
                // We need to trust the admin in specifying valid upload directories and an attacker not being able to overwrite it...
282 5110 acydburn
                $this->destination_path = $phpbb_root_path . $destination;
283 5110 acydburn
284 6364 acydburn
                // Check if the destination path exist...
285 6364 acydburn
                if (!file_exists($this->destination_path))
286 6364 acydburn
                {
287 6364 acydburn
                        @unlink($this->filename);
288 6364 acydburn
                        return false;
289 6364 acydburn
                }
290 6364 acydburn
291 8522 acydburn
                $upload_mode = (@ini_get('open_basedir') || @ini_get('safe_mode') || strtolower(@ini_get('safe_mode')) == 'on') ? 'move' : 'copy';
292 5110 acydburn
                $upload_mode = ($this->local) ? 'local' : $upload_mode;
293 9905 acydburn
                $this->destination_file = $this->destination_path . '/' . utf8_basename($this->realname);
294 5110 acydburn
295 6364 acydburn
                // Check if the file already exist, else there is something wrong...
296 6787 acydburn
                if (file_exists($this->destination_file) && !$overwrite)
297 6364 acydburn
                {
298 6364 acydburn
                        @unlink($this->filename);
299 6364 acydburn
                }
300 6787 acydburn
                else
301 5110 acydburn
                {
302 6787 acydburn
                        if (file_exists($this->destination_file))
303 6787 acydburn
                        {
304 6787 acydburn
                                @unlink($this->destination_file);
305 6787 acydburn
                        }
306 6058 acydburn
307 6787 acydburn
                        switch ($upload_mode)
308 6787 acydburn
                        {
309 6787 acydburn
                                case 'copy':
310 6787 acydburn
311 8146 acydburn
                                        if (!@copy($this->filename, $this->destination_file))
312 6787 acydburn
                                        {
313 8146 acydburn
                                                if (!@move_uploaded_file($this->filename, $this->destination_file))
314 6787 acydburn
                                                {
315 6787 acydburn
                                                        $this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'GENERAL_UPLOAD_ERROR'], $this->destination_file);
316 6787 acydburn
                                                }
317 6787 acydburn
                                        }
318 6787 acydburn
319 6787 acydburn
                                break;
320 6787 acydburn
321 6787 acydburn
                                case 'move':
322 6787 acydburn
323 8146 acydburn
                                        if (!@move_uploaded_file($this->filename, $this->destination_file))
324 5110 acydburn
                                        {
325 8146 acydburn
                                                if (!@copy($this->filename, $this->destination_file))
326 6787 acydburn
                                                {
327 6787 acydburn
                                                        $this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'GENERAL_UPLOAD_ERROR'], $this->destination_file);
328 6787 acydburn
                                                }
329 5110 acydburn
                                        }
330 6787 acydburn
331 6787 acydburn
                                break;
332 6058 acydburn
333 6787 acydburn
                                case 'local':
334 6058 acydburn
335 8146 acydburn
                                        if (!@copy($this->filename, $this->destination_file))
336 5110 acydburn
                                        {
337 5110 acydburn
                                                $this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'GENERAL_UPLOAD_ERROR'], $this->destination_file);
338 5110 acydburn
                                        }
339 5110 acydburn
340 6787 acydburn
                                break;
341 6787 acydburn
                        }
342 6058 acydburn
343 10122 acydburn
                        // Remove temporary filename
344 10122 acydburn
                        @unlink($this->filename);
345 10122 acydburn
346 10122 acydburn
                        if (sizeof($this->error))
347 10122 acydburn
                        {
348 10122 acydburn
                                return false;
349 10122 acydburn
                        }
350 10122 acydburn
351 8780 acydburn
                        phpbb_chmod($this->destination_file, $chmod);
352 5110 acydburn
                }
353 5110 acydburn
354 5110 acydburn
                // Try to get real filesize from destination folder
355 5110 acydburn
                $this->filesize = (@filesize($this->destination_file)) ? @filesize($this->destination_file) : $this->filesize;
356 5110 acydburn
357 7616 acydburn
                if ($this->is_image() && !$skip_image_check)
358 5110 acydburn
                {
359 6058 acydburn
                        $this->width = $this->height = 0;
360 6058 acydburn
361 7646 acydburn
                        if (($this->image_info = @getimagesize($this->destination_file)) !== false)
362 6058 acydburn
                        {
363 6058 acydburn
                                $this->width = $this->image_info[0];
364 6058 acydburn
                                $this->height = $this->image_info[1];
365 6058 acydburn
366 6058 acydburn
                                if (!empty($this->image_info['mime']))
367 6058 acydburn
                                {
368 6058 acydburn
                                        $this->mimetype = $this->image_info['mime'];
369 6058 acydburn
                                }
370 6354 acydburn
371 6354 acydburn
                                // Check image type
372 6354 acydburn
                                $types = $this->upload->image_types();
373 6354 acydburn
374 6354 acydburn
                                if (!isset($types[$this->image_info[2]]) || !in_array($this->extension, $types[$this->image_info[2]]))
375 6354 acydburn
                                {
376 6354 acydburn
                                        if (!isset($types[$this->image_info[2]]))
377 6354 acydburn
                                        {
378 6354 acydburn
                                                $this->error[] = sprintf($user->lang['IMAGE_FILETYPE_INVALID'], $this->image_info[2], $this->mimetype);
379 6354 acydburn
                                        }
380 6354 acydburn
                                        else
381 6354 acydburn
                                        {
382 6354 acydburn
                                                $this->error[] = sprintf($user->lang['IMAGE_FILETYPE_MISMATCH'], $types[$this->image_info[2]][0], $this->extension);
383 6354 acydburn
                                        }
384 6354 acydburn
                                }
385 7150 acydburn
386 7150 acydburn
                                // Make sure the dimensions match a valid image
387 7501 kellanved
                                if (empty($this->width) || empty($this->height))
388 7150 acydburn
                                {
389 7150 acydburn
                                        $this->error[] = $user->lang['ATTACHED_IMAGE_NOT_IMAGE'];
390 7150 acydburn
                                }
391 6058 acydburn
                        }
392 6354 acydburn
                        else
393 6354 acydburn
                        {
394 6354 acydburn
                                $this->error[] = $user->lang['UNABLE_GET_IMAGE_SIZE'];
395 6354 acydburn
                        }
396 5110 acydburn
                }
397 5110 acydburn
398 5110 acydburn
                $this->file_moved = true;
399 5110 acydburn
                $this->additional_checks();
400 5110 acydburn
                unset($this->upload);
401 5967 acydburn
402 5967 acydburn
                return true;
403 5110 acydburn
        }
404 5110 acydburn
405 6058 acydburn
        /**
406 6058 acydburn
        * Performing additional checks
407 6058 acydburn
        */
408 5110 acydburn
        function additional_checks()
409 5110 acydburn
        {
410 5110 acydburn
                global $user;
411 5110 acydburn
412 5110 acydburn
                if (!$this->file_moved)
413 5110 acydburn
                {
414 5110 acydburn
                        return false;
415 5110 acydburn
                }
416 6058 acydburn
417 5110 acydburn
                // Filesize is too big or it's 0 if it was larger than the maxsize in the upload form
418 5110 acydburn
                if ($this->upload->max_filesize && ($this->get('filesize') > $this->upload->max_filesize || $this->filesize == 0))
419 5110 acydburn
                {
420 8389 acydburn
                        $max_filesize = get_formatted_filesize($this->upload->max_filesize, false);
421 8763 acydburn
422 9748 bantu
                        $this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'WRONG_FILESIZE'], $max_filesize['value'], $max_filesize['unit']);
423 5967 acydburn
424 5967 acydburn
                        return false;
425 5110 acydburn
                }
426 5110 acydburn
427 5110 acydburn
                if (!$this->upload->valid_dimensions($this))
428 5110 acydburn
                {
429 11603 git-gate
                        $this->error[] = $user->lang($this->upload->error_prefix . 'WRONG_SIZE',
430 11603 git-gate
                                $user->lang('PIXELS', (int) $this->upload->min_width),
431 11603 git-gate
                                $user->lang('PIXELS', (int) $this->upload->min_height),
432 11603 git-gate
                                $user->lang('PIXELS', (int) $this->upload->max_width),
433 11603 git-gate
                                $user->lang('PIXELS', (int) $this->upload->max_height),
434 11603 git-gate
                                $user->lang('PIXELS', (int) $this->width),
435 11603 git-gate
                                $user->lang('PIXELS', (int) $this->height));
436 5967 acydburn
437 5967 acydburn
                        return false;
438 5110 acydburn
                }
439 5967 acydburn
440 5967 acydburn
                return true;
441 5110 acydburn
        }
442 5110 acydburn
}
443 5110 acydburn
444 5114 acydburn
/**
445 6058 acydburn
* Class for assigning error messages before a real filespec class can be assigned
446 6058 acydburn
*
447 5114 acydburn
* @package phpBB3
448 5114 acydburn
*/
449 5110 acydburn
class fileerror extends filespec
450 5110 acydburn
{
451 5110 acydburn
        function fileerror($error_msg)
452 5110 acydburn
        {
453 5110 acydburn
                $this->error[] = $error_msg;
454 5110 acydburn
        }
455 5110 acydburn
}
456 5110 acydburn
457 5114 acydburn
/**
458 5114 acydburn
* File upload class
459 7266 acydburn
* Init class (all parameters optional and able to be set/overwritten separately) - scope is global and valid for all uploads
460 5114 acydburn
*
461 6058 acydburn
* @package phpBB3
462 5114 acydburn
*/
463 5110 acydburn
class fileupload
464 5110 acydburn
{
465 5110 acydburn
        var $allowed_extensions = array();
466 10922 git-gate
        var $disallowed_content = array('body', 'head', 'html', 'img', 'plaintext', 'a href', 'pre', 'script', 'table', 'title');
467 5110 acydburn
        var $max_filesize = 0;
468 5110 acydburn
        var $min_width = 0;
469 5110 acydburn
        var $min_height = 0;
470 5110 acydburn
        var $max_width = 0;
471 5110 acydburn
        var $max_height = 0;
472 5110 acydburn
        var $error_prefix = '';
473 5110 acydburn
474 5110 acydburn
        /**
475 6058 acydburn
        * Init file upload class.
476 5110 acydburn
        *
477 5110 acydburn
        * @param string $error_prefix Used error messages will get prefixed by this string
478 5110 acydburn
        * @param array $allowed_extensions Array of allowed extensions, for example array('jpg', 'jpeg', 'gif', 'png')
479 5110 acydburn
        * @param int $max_filesize Maximum filesize
480 5110 acydburn
        * @param int $min_width Minimum image width (only checked for images)
481 5110 acydburn
        * @param int $min_height Minimum image height (only checked for images)
482 5110 acydburn
        * @param int $max_width Maximum image width (only checked for images)
483 5110 acydburn
        * @param int $max_height Maximum image height (only checked for images)
484 5110 acydburn
        *
485 5110 acydburn
        */
486 8555 Kellanved
        function fileupload($error_prefix = '', $allowed_extensions = false, $max_filesize = false, $min_width = false, $min_height = false, $max_width = false, $max_height = false, $disallowed_content = false)
487 5110 acydburn
        {
488 5110 acydburn
                $this->set_allowed_extensions($allowed_extensions);
489 5110 acydburn
                $this->set_max_filesize($max_filesize);
490 5110 acydburn
                $this->set_allowed_dimensions($min_width, $min_height, $max_width, $max_height);
491 5110 acydburn
                $this->set_error_prefix($error_prefix);
492 8555 Kellanved
                $this->set_disallowed_content($disallowed_content);
493 5110 acydburn
        }
494 5110 acydburn
495 6058 acydburn
        /**
496 6058 acydburn
        * Reset vars
497 6058 acydburn
        */
498 5110 acydburn
        function reset_vars()
499 5110 acydburn
        {
500 5110 acydburn
                $this->max_filesize = 0;
501 5110 acydburn
                $this->min_width = $this->min_height = $this->max_width = $this->max_height = 0;
502 5110 acydburn
                $this->error_prefix = '';
503 5110 acydburn
                $this->allowed_extensions = array();
504 8555 Kellanved
                $this->disallowed_content = array();
505 5110 acydburn
        }
506 5110 acydburn
507 6058 acydburn
        /**
508 6058 acydburn
        * Set allowed extensions
509 6058 acydburn
        */
510 5110 acydburn
        function set_allowed_extensions($allowed_extensions)
511 5110 acydburn
        {
512 5110 acydburn
                if ($allowed_extensions !== false && is_array($allowed_extensions))
513 5110 acydburn
                {
514 5110 acydburn
                        $this->allowed_extensions = $allowed_extensions;
515 5110 acydburn
                }
516 5110 acydburn
        }
517 5110 acydburn
518 6058 acydburn
        /**
519 6058 acydburn
        * Set allowed dimensions
520 6058 acydburn
        */
521 5110 acydburn
        function set_allowed_dimensions($min_width, $min_height, $max_width, $max_height)
522 5110 acydburn
        {
523 5110 acydburn
                $this->min_width = (int) $min_width;
524 5110 acydburn
                $this->min_height = (int) $min_height;
525 5110 acydburn
                $this->max_width = (int) $max_width;
526 5110 acydburn
                $this->max_height = (int) $max_height;
527 5110 acydburn
        }
528 5110 acydburn
529 6058 acydburn
        /**
530 6058 acydburn
        * Set maximum allowed filesize
531 6058 acydburn
        */
532 5110 acydburn
        function set_max_filesize($max_filesize)
533 5110 acydburn
        {
534 5110 acydburn
                if ($max_filesize !== false && (int) $max_filesize)
535 5110 acydburn
                {
536 5110 acydburn
                        $this->max_filesize = (int) $max_filesize;
537 5110 acydburn
                }
538 5110 acydburn
        }
539 8763 acydburn
540 8555 Kellanved
        /**
541 8555 Kellanved
        * Set disallowed strings
542 8555 Kellanved
        */
543 8555 Kellanved
        function set_disallowed_content($disallowed_content)
544 8555 Kellanved
        {
545 8555 Kellanved
                if ($disallowed_content !== false && is_array($disallowed_content))
546 8555 Kellanved
                {
547 10922 git-gate
                        $this->disallowed_content = array_diff($disallowed_content, array(''));
548 8555 Kellanved
                }
549 8555 Kellanved
        }
550 5110 acydburn
551 6058 acydburn
        /**
552 6058 acydburn
        * Set error prefix
553 6058 acydburn
        */
554 5110 acydburn
        function set_error_prefix($error_prefix)
555 5110 acydburn
        {
556 5110 acydburn
                $this->error_prefix = $error_prefix;
557 5110 acydburn
        }
558 5110 acydburn
559 5110 acydburn
        /**
560 5110 acydburn
        * Form upload method
561 5110 acydburn
        * Upload file from users harddisk
562 5110 acydburn
        *
563 5110 acydburn
        * @param string $form_name Form name assigned to the file input field (if it is an array, the key has to be specified)
564 5110 acydburn
        * @return object $file Object "filespec" is returned, all further operations can be done with this object
565 6058 acydburn
        * @access public
566 5110 acydburn
        */
567 5110 acydburn
        function form_upload($form_name)
568 5110 acydburn
        {
569 5110 acydburn
                global $user;
570 5110 acydburn
571 5110 acydburn
                unset($_FILES[$form_name]['local_mode']);
572 5110 acydburn
                $file = new filespec($_FILES[$form_name], $this);
573 5110 acydburn
574 5110 acydburn
                if ($file->init_error)
575 5110 acydburn
                {
576 5110 acydburn
                        $file->error[] = '';
577 5110 acydburn
                        return $file;
578 5110 acydburn
                }
579 6058 acydburn
580 5147 acydburn
                // Error array filled?
581 5110 acydburn
                if (isset($_FILES[$form_name]['error']))
582 5110 acydburn
                {
583 5110 acydburn
                        $error = $this->assign_internal_error($_FILES[$form_name]['error']);
584 5110 acydburn
585 5110 acydburn
                        if ($error !== false)
586 5110 acydburn
                        {
587 5110 acydburn
                                $file->error[] = $error;
588 5110 acydburn
                                return $file;
589 5110 acydburn
                        }
590 5110 acydburn
                }
591 5110 acydburn
592 5147 acydburn
                // Check if empty file got uploaded (not catched by is_uploaded_file)
593 5147 acydburn
                if (isset($_FILES[$form_name]['size']) && $_FILES[$form_name]['size'] == 0)
594 5147 acydburn
                {
595 5147 acydburn
                        $file->error[] = $user->lang[$this->error_prefix . 'EMPTY_FILEUPLOAD'];
596 5147 acydburn
                        return $file;
597 5147 acydburn
                }
598 5147 acydburn
599 5110 acydburn
                // PHP Upload filesize exceeded
600 5110 acydburn
                if ($file->get('filename') == 'none')
601 5110 acydburn
                {
602 9464 acydburn
                        $max_filesize = @ini_get('upload_max_filesize');
603 9464 acydburn
                        $unit = 'MB';
604 9464 acydburn
605 9464 acydburn
                        if (!empty($max_filesize))
606 9464 acydburn
                        {
607 9464 acydburn
                                $unit = strtolower(substr($max_filesize, -1, 1));
608 9464 acydburn
                                $max_filesize = (int) $max_filesize;
609 9464 acydburn
610 9464 acydburn
                                $unit = ($unit == 'k') ? 'KB' : (($unit == 'g') ? 'GB' : 'MB');
611 9464 acydburn
                        }
612 9464 acydburn
613 9464 acydburn
                        $file->error[] = (empty($max_filesize)) ? $user->lang[$this->error_prefix . 'PHP_SIZE_NA'] : sprintf($user->lang[$this->error_prefix . 'PHP_SIZE_OVERRUN'], $max_filesize, $user->lang[$unit]);
614 5110 acydburn
                        return $file;
615 5110 acydburn
                }
616 5110 acydburn
617 5110 acydburn
                // Not correctly uploaded
618 5110 acydburn
                if (!$file->is_uploaded())
619 5110 acydburn
                {
620 5110 acydburn
                        $file->error[] = $user->lang[$this->error_prefix . 'NOT_UPLOADED'];
621 5110 acydburn
                        return $file;
622 5110 acydburn
                }
623 5110 acydburn
624 5110 acydburn
                $this->common_checks($file);
625 5110 acydburn
626 5110 acydburn
                return $file;
627 5110 acydburn
        }
628 5110 acydburn
629 6058 acydburn
        /**
630 6058 acydburn
        * Move file from another location to phpBB
631 6058 acydburn
        */
632 5140 acydburn
        function local_upload($source_file, $filedata = false)
633 5110 acydburn
        {
634 5140 acydburn
                global $user;
635 5140 acydburn
636 5140 acydburn
                $form_name = 'local';
637 5140 acydburn
638 5140 acydburn
                $_FILES[$form_name]['local_mode'] = true;
639 5140 acydburn
                $_FILES[$form_name]['tmp_name'] = $source_file;
640 5140 acydburn
641 5140 acydburn
                if ($filedata === false)
642 5140 acydburn
                {
643 9905 acydburn
                        $_FILES[$form_name]['name'] = utf8_basename($source_file);
644 5140 acydburn
                        $_FILES[$form_name]['size'] = 0;
645 6975 acydburn
                        $mimetype = '';
646 6975 acydburn
647 6975 acydburn
                        if (function_exists('mime_content_type'))
648 6975 acydburn
                        {
649 8087 acydburn
                                $mimetype = mime_content_type($source_file);
650 6975 acydburn
                        }
651 6975 acydburn
652 6975 acydburn
                        // Some browsers choke on a mimetype of application/octet-stream
653 6975 acydburn
                        if (!$mimetype || $mimetype == 'application/octet-stream')
654 6975 acydburn
                        {
655 6975 acydburn
                                $mimetype = 'application/octetstream';
656 6975 acydburn
                        }
657 6975 acydburn
658 6975 acydburn
                        $_FILES[$form_name]['type'] = $mimetype;
659 5140 acydburn
                }
660 5140 acydburn
                else
661 5140 acydburn
                {
662 5140 acydburn
                        $_FILES[$form_name]['name'] = $filedata['realname'];
663 5140 acydburn
                        $_FILES[$form_name]['size'] = $filedata['size'];
664 5140 acydburn
                        $_FILES[$form_name]['type'] = $filedata['type'];
665 6058 acydburn
                }
666 5140 acydburn
667 5140 acydburn
                $file = new filespec($_FILES[$form_name], $this);
668 5140 acydburn
669 5140 acydburn
                if ($file->init_error)
670 5140 acydburn
                {
671 5140 acydburn
                        $file->error[] = '';
672 5140 acydburn
                        return $file;
673 5140 acydburn
                }
674 6058 acydburn
675 5140 acydburn
                if (isset($_FILES[$form_name]['error']))
676 5140 acydburn
                {
677 5140 acydburn
                        $error = $this->assign_internal_error($_FILES[$form_name]['error']);
678 5140 acydburn
679 5140 acydburn
                        if ($error !== false)
680 5140 acydburn
                        {
681 5140 acydburn
                                $file->error[] = $error;
682 5140 acydburn
                                return $file;
683 5140 acydburn
                        }
684 5140 acydburn
                }
685 5140 acydburn
686 5140 acydburn
                // PHP Upload filesize exceeded
687 5140 acydburn
                if ($file->get('filename') == 'none')
688 5140 acydburn
                {
689 9464 acydburn
                        $max_filesize = @ini_get('upload_max_filesize');
690 9464 acydburn
                        $unit = 'MB';
691 9464 acydburn
692 9464 acydburn
                        if (!empty($max_filesize))
693 9464 acydburn
                        {
694 9464 acydburn
                                $unit = strtolower(substr($max_filesize, -1, 1));
695 9464 acydburn
                                $max_filesize = (int) $max_filesize;
696 9464 acydburn
697 9464 acydburn
                                $unit = ($unit == 'k') ? 'KB' : (($unit == 'g') ? 'GB' : 'MB');
698 9464 acydburn
                        }
699 9464 acydburn
700 9464 acydburn
                        $file->error[] = (empty($max_filesize)) ? $user->lang[$this->error_prefix . 'PHP_SIZE_NA'] : sprintf($user->lang[$this->error_prefix . 'PHP_SIZE_OVERRUN'], $max_filesize, $user->lang[$unit]);
701 5140 acydburn
                        return $file;
702 5140 acydburn
                }
703 5140 acydburn
704 5140 acydburn
                // Not correctly uploaded
705 5140 acydburn
                if (!$file->is_uploaded())
706 5140 acydburn
                {
707 5140 acydburn
                        $file->error[] = $user->lang[$this->error_prefix . 'NOT_UPLOADED'];
708 5140 acydburn
                        return $file;
709 5140 acydburn
                }
710 5140 acydburn
711 5140 acydburn
                $this->common_checks($file);
712 5140 acydburn
713 5140 acydburn
                return $file;
714 5110 acydburn
        }
715 5110 acydburn
716 5110 acydburn
        /**
717 5110 acydburn
        * Remote upload method
718 5110 acydburn
        * Uploads file from given url
719 5110 acydburn
        *
720 5110 acydburn
        * @param string $upload_url URL pointing to file to upload, for example http://www.foobar.com/example.gif
721 5110 acydburn
        * @return object $file Object "filespec" is returned, all further operations can be done with this object
722 6058 acydburn
        * @access public
723 5110 acydburn
        */
724 5110 acydburn
        function remote_upload($upload_url)
725 5110 acydburn
        {
726 5110 acydburn
                global $user, $phpbb_root_path;
727 6058 acydburn
728 5110 acydburn
                $upload_ary = array();
729 5110 acydburn
                $upload_ary['local_mode'] = true;
730 5110 acydburn
731 6058 acydburn
                if (!preg_match('#^(https?://).*?\.(' . implode('|', $this->allowed_extensions) . ')$#i', $upload_url, $match))
732 5110 acydburn
                {
733 5110 acydburn
                        $file = new fileerror($user->lang[$this->error_prefix . 'URL_INVALID']);
734 5110 acydburn
                        return $file;
735 5110 acydburn
                }
736 8146 acydburn
737 5110 acydburn
                if (empty($match[2]))
738 5110 acydburn
                {
739 5110 acydburn
                        $file = new fileerror($user->lang[$this->error_prefix . 'URL_INVALID']);
740 5110 acydburn
                        return $file;
741 5110 acydburn
                }
742 5110 acydburn
743 5110 acydburn
                $url = parse_url($upload_url);
744 5110 acydburn
745 5110 acydburn
                $host = $url['host'];
746 5765 acydburn
                $path = $url['path'];
747 5189 acydburn
                $port = (!empty($url['port'])) ? (int) $url['port'] : 80;
748 6058 acydburn
749 5110 acydburn
                $upload_ary['type'] = 'application/octet-stream';
750 6058 acydburn
751 5307 acydburn
                $url['path'] = explode('.', $url['path']);
752 5307 acydburn
                $ext = array_pop($url['path']);
753 6058 acydburn
754 5749 acydburn
                $url['path'] = implode('', $url['path']);
755 9905 acydburn
                $upload_ary['name'] = utf8_basename($url['path']) . (($ext) ? '.' . $ext : '');
756 5110 acydburn
                $filename = $url['path'];
757 5110 acydburn
                $filesize = 0;
758 5110 acydburn
759 6846 acydburn
                $errno = 0;
760 6846 acydburn
                $errstr = '';
761 6846 acydburn
762 5110 acydburn
                if (!($fsock = @fsockopen($host, $port, $errno, $errstr)))
763 5110 acydburn
                {
764 5110 acydburn
                        $file = new fileerror($user->lang[$this->error_prefix . 'NOT_UPLOADED']);
765 5110 acydburn
                        return $file;
766 5110 acydburn
                }
767 5110 acydburn
768 7608 acydburn
                // Make sure $path not beginning with /
769 7608 acydburn
                if (strpos($path, '/') === 0)
770 7608 acydburn
                {
771 7608 acydburn
                        $path = substr($path, 1);
772 7608 acydburn
                }
773 7608 acydburn
774 5765 acydburn
                fputs($fsock, 'GET /' . $path . " HTTP/1.1\r\n");
775 5110 acydburn
                fputs($fsock, "HOST: " . $host . "\r\n");
776 5110 acydburn
                fputs($fsock, "Connection: close\r\n\r\n");
777 5110 acydburn
778 5110 acydburn
                $get_info = false;
779 5110 acydburn
                $data = '';
780 5110 acydburn
                while (!@feof($fsock))
781 5110 acydburn
                {
782 5110 acydburn
                        if ($get_info)
783 5110 acydburn
                        {
784 10636 git-gate
                                $block = @fread($fsock, 1024);
785 10636 git-gate
                                $filesize += strlen($block);
786 10636 git-gate
787 10636 git-gate
                                if ($this->max_filesize && $filesize > $this->max_filesize)
788 10636 git-gate
                                {
789 10636 git-gate
                                        $max_filesize = get_formatted_filesize($this->max_filesize, false);
790 10636 git-gate
791 10636 git-gate
                                        $file = new fileerror(sprintf($user->lang[$this->error_prefix . 'WRONG_FILESIZE'], $max_filesize['value'], $max_filesize['unit']));
792 10636 git-gate
                                        return $file;
793 10636 git-gate
                                }
794 10636 git-gate
795 10636 git-gate
                                $data .= $block;
796 5110 acydburn
                        }
797 5110 acydburn
                        else
798 5110 acydburn
                        {
799 5110 acydburn
                                $line = @fgets($fsock, 1024);
800 5110 acydburn
801 5110 acydburn
                                if ($line == "\r\n")
802 5110 acydburn
                                {
803 5110 acydburn
                                        $get_info = true;
804 5110 acydburn
                                }
805 5110 acydburn
                                else
806 5110 acydburn
                                {
807 6846 acydburn
                                        if (stripos($line, 'content-type: ') !== false)
808 5110 acydburn
                                        {
809 6846 acydburn
                                                $upload_ary['type'] = rtrim(str_replace('content-type: ', '', strtolower($line)));
810 5110 acydburn
                                        }
811 10636 git-gate
                                        else if ($this->max_filesize && stripos($line, 'content-length: ') !== false)
812 10636 git-gate
                                        {
813 10636 git-gate
                                                $length = (int) str_replace('content-length: ', '', strtolower($line));
814 10636 git-gate
815 10636 git-gate
                                                if ($length && $length > $this->max_filesize)
816 10636 git-gate
                                                {
817 10636 git-gate
                                                        $max_filesize = get_formatted_filesize($this->max_filesize, false);
818 10636 git-gate
819 10636 git-gate
                                                        $file = new fileerror(sprintf($user->lang[$this->error_prefix . 'WRONG_FILESIZE'], $max_filesize['value'], $max_filesize['unit']));
820 10636 git-gate
                                                        return $file;
821 10636 git-gate
                                                }
822 10636 git-gate
                                        }
823 6846 acydburn
                                        else if (stripos($line, '404 not found') !== false)
824 5765 acydburn
                                        {
825 5765 acydburn
                                                $file = new fileerror($user->lang[$this->error_prefix . 'URL_NOT_FOUND']);
826 5765 acydburn
                                                return $file;
827 5765 acydburn
                                        }
828 5110 acydburn
                                }
829 5110 acydburn
                        }
830 5110 acydburn
                }
831 5110 acydburn
                @fclose($fsock);
832 5110 acydburn
833 5110 acydburn
                if (empty($data))
834 5110 acydburn
                {
835 5110 acydburn
                        $file = new fileerror($user->lang[$this->error_prefix . 'EMPTY_REMOTE_DATA']);
836 5110 acydburn
                        return $file;
837 5110 acydburn
                }
838 5110 acydburn
839 8522 acydburn
                $tmp_path = (!@ini_get('safe_mode') || strtolower(@ini_get('safe_mode')) == 'off') ? false : $phpbb_root_path . 'cache';
840 5622 acydburn
                $filename = tempnam($tmp_path, unique_id() . '-');
841 5110 acydburn
842 5110 acydburn
                if (!($fp = @fopen($filename, 'wb')))
843 5110 acydburn
                {
844 5110 acydburn
                        $file = new fileerror($user->lang[$this->error_prefix . 'NOT_UPLOADED']);
845 5110 acydburn
                        return $file;
846 5110 acydburn
                }
847 6058 acydburn
848 5110 acydburn
                $upload_ary['size'] = fwrite($fp, $data);
849 5110 acydburn
                fclose($fp);
850 5110 acydburn
                unset($data);
851 5110 acydburn
852 5110 acydburn
                $upload_ary['tmp_name'] = $filename;
853 5110 acydburn
854 5110 acydburn
                $file = new filespec($upload_ary, $this);
855 5110 acydburn
                $this->common_checks($file);
856 5110 acydburn
857 5110 acydburn
                return $file;
858 5110 acydburn
        }
859 5110 acydburn
860 6058 acydburn
        /**
861 6058 acydburn
        * Assign internal error
862 6058 acydburn
        * @access private
863 6058 acydburn
        */
864 5110 acydburn
        function assign_internal_error($errorcode)
865 5110 acydburn
        {
866 5110 acydburn
                global $user;
867 5110 acydburn
868 5110 acydburn
                switch ($errorcode)
869 5110 acydburn
                {
870 5110 acydburn
                        case 1:
871 9464 acydburn
                                $max_filesize = @ini_get('upload_max_filesize');
872 9464 acydburn
                                $unit = 'MB';
873 9464 acydburn
874 9464 acydburn
                                if (!empty($max_filesize))
875 9464 acydburn
                                {
876 9464 acydburn
                                        $unit = strtolower(substr($max_filesize, -1, 1));
877 9464 acydburn
                                        $max_filesize = (int) $max_filesize;
878 9464 acydburn
879 9464 acydburn
                                        $unit = ($unit == 'k') ? 'KB' : (($unit == 'g') ? 'GB' : 'MB');
880 9464 acydburn
                                }
881 9464 acydburn
882 9464 acydburn
                                $error = (empty($max_filesize)) ? $user->lang[$this->error_prefix . 'PHP_SIZE_NA'] : sprintf($user->lang[$this->error_prefix . 'PHP_SIZE_OVERRUN'], $max_filesize, $user->lang[$unit]);
883 5765 acydburn
                        break;
884 5765 acydburn
885 5110 acydburn
                        case 2:
886 8389 acydburn
                                $max_filesize = get_formatted_filesize($this->max_filesize, false);
887 5765 acydburn
888 9748 bantu
                                $error = sprintf($user->lang[$this->error_prefix . 'WRONG_FILESIZE'], $max_filesize['value'], $max_filesize['unit']);
889 5765 acydburn
                        break;
890 5765 acydburn
891 5110 acydburn
                        case 3:
892 6058 acydburn
                                $error = $user->lang[$this->error_prefix . 'PARTIAL_UPLOAD'];
893 6058 acydburn
                        break;
894 6058 acydburn
895 5110 acydburn
                        case 4:
896 5110 acydburn
                                $error = $user->lang[$this->error_prefix . 'NOT_UPLOADED'];
897 6058 acydburn
                        break;
898 6058 acydburn
899 5110 acydburn
                        case 6:
900 5110 acydburn
                                $error = 'Temporary folder could not be found. Please check your PHP installation.';
901 6058 acydburn
                        break;
902 6058 acydburn
903 5110 acydburn
                        default:
904 5110 acydburn
                                $error = false;
905 6058 acydburn
                        break;
906 5110 acydburn
                }
907 5110 acydburn
908 5110 acydburn
                return $error;
909 5110 acydburn
        }
910 6058 acydburn
911 6058 acydburn
        /**
912 6058 acydburn
        * Perform common checks
913 6058 acydburn
        */
914 5110 acydburn
        function common_checks(&$file)
915 5110 acydburn
        {
916 5110 acydburn
                global $user;
917 5110 acydburn
918 5110 acydburn
                // Filesize is too big or it's 0 if it was larger than the maxsize in the upload form
919 5110 acydburn
                if ($this->max_filesize && ($file->get('filesize') > $this->max_filesize || $file->get('filesize') == 0))
920 5110 acydburn
                {
921 8389 acydburn
                        $max_filesize = get_formatted_filesize($this->max_filesize, false);
922 5765 acydburn
923 9748 bantu
                        $file->error[] = sprintf($user->lang[$this->error_prefix . 'WRONG_FILESIZE'], $max_filesize['value'], $max_filesize['unit']);
924 5110 acydburn
                }
925 5110 acydburn
926 5110 acydburn
                // check Filename
927 5110 acydburn
                if (preg_match("#[\\/:*?\"<>|]#i", $file->get('realname')))
928 8146 acydburn
                {
929 5110 acydburn
                        $file->error[] = sprintf($user->lang[$this->error_prefix . 'INVALID_FILENAME'], $file->get('realname'));
930 5110 acydburn
                }
931 5110 acydburn
932 5110 acydburn
                // Invalid Extension
933 5110 acydburn
                if (!$this->valid_extension($file))
934 5110 acydburn
                {
935 5110 acydburn
                        $file->error[] = sprintf($user->lang[$this->error_prefix . 'DISALLOWED_EXTENSION'], $file->get('extension'));
936 5110 acydburn
                }
937 8763 acydburn
938 8555 Kellanved
                // MIME Sniffing
939 8555 Kellanved
                if (!$this->valid_content($file))
940 8555 Kellanved
                {
941 8555 Kellanved
                        $file->error[] = sprintf($user->lang[$this->error_prefix . 'DISALLOWED_CONTENT']);
942 8555 Kellanved
                }
943 5110 acydburn
        }
944 5110 acydburn
945 6058 acydburn
        /**
946 6058 acydburn
        * Check for allowed extension
947 6058 acydburn
        */
948 5110 acydburn
        function valid_extension(&$file)
949 5110 acydburn
        {
950 5110 acydburn
                return (in_array($file->get('extension'), $this->allowed_extensions)) ? true : false;
951 5110 acydburn
        }
952 5110 acydburn
953 6058 acydburn
        /**
954 6058 acydburn
        * Check for allowed dimension
955 6058 acydburn
        */
956 5110 acydburn
        function valid_dimensions(&$file)
957 5110 acydburn
        {
958 5135 acydburn
                if (!$this->max_width && !$this->max_height && !$this->min_width && !$this->min_height)
959 5135 acydburn
                {
960 5135 acydburn
                        return true;
961 5135 acydburn
                }
962 6058 acydburn
963 8146 acydburn
                if (($file->get('width') > $this->max_width && $this->max_width) ||
964 8146 acydburn
                        ($file->get('height') > $this->max_height && $this->max_height) ||
965 5110 acydburn
                        ($file->get('width') < $this->min_width && $this->min_width) ||
966 5135 acydburn
                        ($file->get('height') < $this->min_height && $this->min_height))
967 5110 acydburn
                {
968 5110 acydburn
                        return false;
969 5110 acydburn
                }
970 5110 acydburn
971 5110 acydburn
                return true;
972 5110 acydburn
        }
973 5110 acydburn
974 6058 acydburn
        /**
975 6058 acydburn
        * Check if form upload is valid
976 6058 acydburn
        */
977 5110 acydburn
        function is_valid($form_name)
978 5110 acydburn
        {
979 5110 acydburn
                return (isset($_FILES[$form_name]) && $_FILES[$form_name]['name'] != 'none') ? true : false;
980 5110 acydburn
        }
981 6354 acydburn
982 8555 Kellanved
983 6354 acydburn
        /**
984 10775 git-gate
        * Check for bad content (IE mime-sniffing)
985 8555 Kellanved
        */
986 8555 Kellanved
        function valid_content(&$file)
987 8555 Kellanved
        {
988 8555 Kellanved
                return ($file->check_content($this->disallowed_content));
989 8555 Kellanved
        }
990 8555 Kellanved
991 8555 Kellanved
        /**
992 8146 acydburn
        * Return image type/extension mapping
993 6354 acydburn
        */
994 6354 acydburn
        function image_types()
995 6354 acydburn
        {
996 6354 acydburn
                return array(
997 10778 git-gate
                        IMAGETYPE_GIF                => array('gif'),
998 10778 git-gate
                        IMAGETYPE_JPEG                => array('jpg', 'jpeg'),
999 10778 git-gate
                        IMAGETYPE_PNG                => array('png'),
1000 10778 git-gate
                        IMAGETYPE_SWF                => array('swf'),
1001 10778 git-gate
                        IMAGETYPE_PSD                => array('psd'),
1002 10778 git-gate
                        IMAGETYPE_BMP                => array('bmp'),
1003 10778 git-gate
                        IMAGETYPE_TIFF_II        => array('tif', 'tiff'),
1004 10778 git-gate
                        IMAGETYPE_TIFF_MM        => array('tif', 'tiff'),
1005 10778 git-gate
                        IMAGETYPE_JPC                => array('jpg', 'jpeg'),
1006 10778 git-gate
                        IMAGETYPE_JP2                => array('jpg', 'jpeg'),
1007 10778 git-gate
                        IMAGETYPE_JPX                => array('jpg', 'jpeg'),
1008 10778 git-gate
                        IMAGETYPE_JB2                => array('jpg', 'jpeg'),
1009 10778 git-gate
                        IMAGETYPE_SWC                => array('swc'),
1010 10778 git-gate
                        IMAGETYPE_IFF                => array('iff'),
1011 10778 git-gate
                        IMAGETYPE_WBMP                => array('wbmp'),
1012 10778 git-gate
                        IMAGETYPE_XBM                => array('xbm'),
1013 6354 acydburn
                );
1014 6354 acydburn
        }
1015 5110 acydburn
}