blob: 15eb74bd57423243d9517d6d664076fb8aa07367 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev3a459572011-12-21 11:23:11 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev3a459572011-12-21 11:23:11 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * File Uploading Class
31 *
32 * @package CodeIgniter
33 * @subpackage Libraries
34 * @category Uploads
Derek Jonesf4a4bd82011-10-20 12:18:42 -050035 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @link http://codeigniter.com/user_guide/libraries/file_uploading.html
37 */
38class CI_Upload {
Barry Mienydd671972010-10-04 16:33:58 +020039
Andrey Andreevf5ccd122012-11-02 00:17:39 +020040 /**
41 * Maximum file size
42 *
43 * @var int
44 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +030045 public $max_size = 0;
Andrey Andreevf5ccd122012-11-02 00:17:39 +020046
47 /**
48 * Maximum image width
49 *
50 * @var int
51 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +030052 public $max_width = 0;
Andrey Andreevf5ccd122012-11-02 00:17:39 +020053
54 /**
55 * Maximum image height
56 *
57 * @var int
58 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +030059 public $max_height = 0;
Andrey Andreevf5ccd122012-11-02 00:17:39 +020060
61 /**
Andrey Andreev05aa2d62012-12-03 16:06:55 +020062 * Minimum image width
63 *
64 * @var int
65 */
66 public $min_width = 0;
67
68 /**
69 * Minimum image height
70 *
71 * @var int
72 */
73 public $min_height = 0;
74
75 /**
Andrey Andreevf5ccd122012-11-02 00:17:39 +020076 * Maximum filename length
77 *
78 * @var int
79 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +030080 public $max_filename = 0;
Andrey Andreevf5ccd122012-11-02 00:17:39 +020081
82 /**
83 * Maximum duplicate filename increment ID
84 *
85 * @var int
86 */
Adam Jackettccbbea12011-08-21 16:19:11 -040087 public $max_filename_increment = 100;
Andrey Andreevf5ccd122012-11-02 00:17:39 +020088
89 /**
90 * Allowed file types
91 *
92 * @var string
93 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +030094 public $allowed_types = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +020095
96 /**
97 * Temporary filename
98 *
99 * @var string
100 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300101 public $file_temp = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200102
103 /**
104 * Filename
105 *
106 * @var string
107 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300108 public $file_name = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200109
110 /**
111 * Original filename
112 *
113 * @var string
114 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300115 public $orig_name = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200116
117 /**
118 * File type
119 *
120 * @var string
121 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300122 public $file_type = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200123
124 /**
125 * File size
126 *
127 * @var int
128 */
129 public $file_size = NULL;
130
131 /**
132 * Filename extension
133 *
134 * @var string
135 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300136 public $file_ext = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200137
138 /**
Adrianf496d132013-06-24 15:56:45 +0200139 * Force filename extension to lowercase
140 *
141 * @var string
142 */
Adrian74a228b2013-06-25 12:09:22 +0200143 public $file_ext_tolower = FALSE;
Adrianf496d132013-06-24 15:56:45 +0200144
145 /**
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200146 * Upload path
147 *
148 * @var string
149 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300150 public $upload_path = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200151
152 /**
153 * Overwrite flag
154 *
155 * @var bool
156 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300157 public $overwrite = FALSE;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200158
159 /**
160 * Obfuscate filename flag
161 *
162 * @var bool
163 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300164 public $encrypt_name = FALSE;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200165
166 /**
167 * Is image flag
168 *
169 * @var bool
170 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300171 public $is_image = FALSE;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200172
173 /**
174 * Image width
175 *
176 * @var int
177 */
178 public $image_width = NULL;
179
180 /**
181 * Image height
182 *
183 * @var int
184 */
185 public $image_height = NULL;
186
187 /**
188 * Image type
189 *
190 * @var string
191 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300192 public $image_type = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200193
194 /**
195 * Image size string
196 *
197 * @var string
198 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300199 public $image_size_str = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200200
201 /**
202 * Error messages list
203 *
204 * @var array
205 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300206 public $error_msg = array();
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200207
208 /**
209 * MIME types list
210 *
211 * @var array
212 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300213 public $mimes = array();
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200214
215 /**
216 * Remove spaces flag
217 *
218 * @var bool
219 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300220 public $remove_spaces = TRUE;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200221
222 /**
223 * MIME detection flag
224 *
225 * @var bool
226 */
Andrey Andreevd60e7002012-06-17 00:03:03 +0300227 public $detect_mime = TRUE;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200228
229 /**
230 * XSS filter flag
231 *
232 * @var bool
233 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300234 public $xss_clean = FALSE;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200235
236 /**
237 * Temporary filename prefix
238 *
239 * @var string
240 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300241 public $temp_prefix = 'temp_file_';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200242
243 /**
244 * Filename sent by the client
245 *
246 * @var bool
247 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300248 public $client_name = '';
Barry Mienydd671972010-10-04 16:33:58 +0200249
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200250 // --------------------------------------------------------------------
251
252 /**
253 * Filename override
254 *
255 * @var string
256 */
Greg Aker58fdee82010-11-10 15:07:09 -0600257 protected $_file_name_override = '';
Barry Mienydd671972010-10-04 16:33:58 +0200258
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200259 // --------------------------------------------------------------------
260
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 /**
262 * Constructor
263 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200264 * @param array $props
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300265 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 */
Greg Aker58fdee82010-11-10 15:07:09 -0600267 public function __construct($props = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 {
269 if (count($props) > 0)
270 {
271 $this->initialize($props);
272 }
Barry Mienydd671972010-10-04 16:33:58 +0200273
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300274 $this->mimes =& get_mimes();
275
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300276 log_message('debug', 'Upload Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 }
Barry Mienydd671972010-10-04 16:33:58 +0200278
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200280
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 /**
282 * Initialize preferences
283 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200284 * @param array $config
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200286 */
Greg Aker58fdee82010-11-10 15:07:09 -0600287 public function initialize($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 {
289 $defaults = array(
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300290 'max_size' => 0,
291 'max_width' => 0,
292 'max_height' => 0,
Andrey Andreev05aa2d62012-12-03 16:06:55 +0200293 'min_width' => 0,
294 'min_height' => 0,
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300295 'max_filename' => 0,
296 'max_filename_increment' => 100,
297 'allowed_types' => '',
298 'file_temp' => '',
299 'file_name' => '',
300 'orig_name' => '',
301 'file_type' => '',
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200302 'file_size' => NULL,
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300303 'file_ext' => '',
Adrian74a228b2013-06-25 12:09:22 +0200304 'file_ext_tolower' => FALSE,
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300305 'upload_path' => '',
306 'overwrite' => FALSE,
307 'encrypt_name' => FALSE,
308 'is_image' => FALSE,
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200309 'image_width' => NULL,
310 'image_height' => NULL,
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300311 'image_type' => '',
312 'image_size_str' => '',
313 'error_msg' => array(),
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300314 'remove_spaces' => TRUE,
Andrey Andreevd60e7002012-06-17 00:03:03 +0300315 'detect_mime' => TRUE,
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300316 'xss_clean' => FALSE,
317 'temp_prefix' => 'temp_file_',
318 'client_name' => ''
319 );
Barry Mienydd671972010-10-04 16:33:58 +0200320
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 foreach ($defaults as $key => $val)
322 {
323 if (isset($config[$key]))
324 {
325 $method = 'set_'.$key;
326 if (method_exists($this, $method))
327 {
328 $this->$method($config[$key]);
329 }
330 else
331 {
332 $this->$key = $config[$key];
Barry Mienydd671972010-10-04 16:33:58 +0200333 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 }
335 else
336 {
337 $this->$key = $val;
338 }
339 }
Barry Mienydd671972010-10-04 16:33:58 +0200340
Derek Jonese9d723f2010-07-12 10:10:59 -0500341 // if a file_name was provided in the config, use it instead of the user input
342 // supplied file name for all uploads until initialized again
343 $this->_file_name_override = $this->file_name;
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 }
Barry Mienydd671972010-10-04 16:33:58 +0200345
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200347
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 /**
349 * Perform the file upload
350 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200351 * @param string $field
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200353 */
Greg Aker58fdee82010-11-10 15:07:09 -0600354 public function do_upload($field = 'userfile')
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300356 // Is $_FILES[$field] set? If not, no reason to continue.
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 if ( ! isset($_FILES[$field]))
358 {
359 $this->set_error('upload_no_file_selected');
360 return FALSE;
361 }
Barry Mienydd671972010-10-04 16:33:58 +0200362
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 // Is the upload path valid?
364 if ( ! $this->validate_upload_path())
365 {
366 // errors will already be set by validate_upload_path() so just return FALSE
367 return FALSE;
368 }
369
370 // Was the file able to be uploaded? If not, determine the reason why.
371 if ( ! is_uploaded_file($_FILES[$field]['tmp_name']))
372 {
373 $error = ( ! isset($_FILES[$field]['error'])) ? 4 : $_FILES[$field]['error'];
374
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300375 switch ($error)
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 {
Darren Benneye123a602013-03-30 18:17:54 +0000377 case UPLOAD_ERR_INI_SIZE:
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 $this->set_error('upload_file_exceeds_limit');
379 break;
Darren Benneye123a602013-03-30 18:17:54 +0000380 case UPLOAD_ERR_FORM_SIZE:
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 $this->set_error('upload_file_exceeds_form_limit');
382 break;
Darren Benneye123a602013-03-30 18:17:54 +0000383 case UPLOAD_ERR_PARTIAL:
Barry Mienydd671972010-10-04 16:33:58 +0200384 $this->set_error('upload_file_partial');
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 break;
Darren Benneye123a602013-03-30 18:17:54 +0000386 case UPLOAD_ERR_NO_FILE:
Barry Mienydd671972010-10-04 16:33:58 +0200387 $this->set_error('upload_no_file_selected');
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 break;
Darren Benneye123a602013-03-30 18:17:54 +0000389 case UPLOAD_ERR_NO_TMP_DIR:
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 $this->set_error('upload_no_temp_directory');
391 break;
Darren Benneye123a602013-03-30 18:17:54 +0000392 case UPLOAD_ERR_CANT_WRITE:
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 $this->set_error('upload_unable_to_write_file');
394 break;
Darren Benneye123a602013-03-30 18:17:54 +0000395 case UPLOAD_ERR_EXTENSION:
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 $this->set_error('upload_stopped_by_extension');
397 break;
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300398 default:
399 $this->set_error('upload_no_file_selected');
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 break;
401 }
402
403 return FALSE;
404 }
405
406 // Set the uploaded data as class variables
Barry Mienydd671972010-10-04 16:33:58 +0200407 $this->file_temp = $_FILES[$field]['tmp_name'];
408 $this->file_size = $_FILES[$field]['size'];
Andrey Andreevd60e7002012-06-17 00:03:03 +0300409
410 // Skip MIME type detection?
411 if ($this->detect_mime !== FALSE)
412 {
413 $this->_file_mime_type($_FILES[$field]);
414 }
415
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300416 $this->file_type = preg_replace('/^(.+?);.*$/', '\\1', $this->file_type);
Derek Jones616fb022010-04-22 16:52:18 -0500417 $this->file_type = strtolower(trim(stripslashes($this->file_type), '"'));
Derek Jonese9d723f2010-07-12 10:10:59 -0500418 $this->file_name = $this->_prep_filename($_FILES[$field]['name']);
419 $this->file_ext = $this->get_extension($this->file_name);
420 $this->client_name = $this->file_name;
Barry Mienydd671972010-10-04 16:33:58 +0200421
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 // Is the file type allowed to be uploaded?
423 if ( ! $this->is_allowed_filetype())
424 {
425 $this->set_error('upload_invalid_filetype');
426 return FALSE;
427 }
428
Derek Jonese9d723f2010-07-12 10:10:59 -0500429 // if we're overriding, let's now make sure the new name and type is allowed
Alex Bilbied261b1e2012-06-02 11:12:16 +0100430 if ($this->_file_name_override !== '')
Derek Jonese9d723f2010-07-12 10:10:59 -0500431 {
432 $this->file_name = $this->_prep_filename($this->_file_name_override);
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000433
434 // If no extension was provided in the file_name config item, use the uploaded one
Pascal Kriete14287f32011-02-14 13:39:34 -0500435 if (strpos($this->_file_name_override, '.') === FALSE)
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000436 {
437 $this->file_name .= $this->file_ext;
438 }
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000439 else
440 {
vlakoff35672462013-02-15 01:36:04 +0100441 // An extension was provided, let's have it!
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300442 $this->file_ext = $this->get_extension($this->_file_name_override);
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000443 }
Derek Jonese9d723f2010-07-12 10:10:59 -0500444
445 if ( ! $this->is_allowed_filetype(TRUE))
446 {
447 $this->set_error('upload_invalid_filetype');
Barry Mienydd671972010-10-04 16:33:58 +0200448 return FALSE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500449 }
450 }
Barry Mienydd671972010-10-04 16:33:58 +0200451
Derek Jonese9d723f2010-07-12 10:10:59 -0500452 // Convert the file size to kilobytes
453 if ($this->file_size > 0)
454 {
455 $this->file_size = round($this->file_size/1024, 2);
456 }
457
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 // Is the file size within the allowed maximum?
459 if ( ! $this->is_allowed_filesize())
460 {
461 $this->set_error('upload_invalid_filesize');
462 return FALSE;
463 }
464
465 // Are the image dimensions within the allowed size?
vlakoffc941d852013-08-06 14:44:40 +0200466 // Note: This can fail if the server has an open_basedir restriction.
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 if ( ! $this->is_allowed_dimensions())
468 {
469 $this->set_error('upload_invalid_dimensions');
470 return FALSE;
471 }
472
473 // Sanitize the file name for security
Andrey Andreev7e559772013-01-29 15:38:33 +0200474 $CI =& get_instance();
475 $this->file_name = $CI->security->sanitize_filename($this->file_name);
Barry Mienydd671972010-10-04 16:33:58 +0200476
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 // Truncate the file name if it's too long
478 if ($this->max_filename > 0)
479 {
480 $this->file_name = $this->limit_filename_length($this->file_name, $this->max_filename);
481 }
482
483 // Remove white spaces in the name
Alex Bilbied261b1e2012-06-02 11:12:16 +0100484 if ($this->remove_spaces === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300486 $this->file_name = preg_replace('/\s+/', '_', $this->file_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 }
488
489 /*
490 * Validate the file name
491 * This function appends an number onto the end of
492 * the file if one with the same name already exists.
493 * If it returns false there was a problem.
494 */
495 $this->orig_name = $this->file_name;
496
Alex Bilbied261b1e2012-06-02 11:12:16 +0100497 if ($this->overwrite === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 {
499 $this->file_name = $this->set_filename($this->upload_path, $this->file_name);
Barry Mienydd671972010-10-04 16:33:58 +0200500
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 if ($this->file_name === FALSE)
502 {
503 return FALSE;
504 }
505 }
506
507 /*
Derek Jonese9d723f2010-07-12 10:10:59 -0500508 * Run the file through the XSS hacking filter
509 * This helps prevent malicious code from being
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300510 * embedded within a file. Scripts can easily
Derek Jonese9d723f2010-07-12 10:10:59 -0500511 * be disguised as images or other file types.
512 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300513 if ($this->xss_clean && $this->do_xss_clean() === FALSE)
Derek Jonese9d723f2010-07-12 10:10:59 -0500514 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300515 $this->set_error('upload_unable_to_write_file');
516 return FALSE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500517 }
518
519 /*
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 * Move the file to the final destination
521 * To deal with different server configurations
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300522 * we'll attempt to use copy() first. If that fails
523 * we'll use move_uploaded_file(). One of the two should
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 * reliably work in most environments
525 */
526 if ( ! @copy($this->file_temp, $this->upload_path.$this->file_name))
527 {
528 if ( ! @move_uploaded_file($this->file_temp, $this->upload_path.$this->file_name))
529 {
Barry Mienydd671972010-10-04 16:33:58 +0200530 $this->set_error('upload_destination_error');
531 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 }
533 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000534
535 /*
536 * Set the finalized image dimensions
537 * This sets the image width/height (assuming the
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300538 * file was an image). We use this information
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 * in the "data" function.
540 */
541 $this->set_image_properties($this->upload_path.$this->file_name);
542
543 return TRUE;
544 }
Barry Mienydd671972010-10-04 16:33:58 +0200545
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200547
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 /**
549 * Finalized Data Array
Barry Mienydd671972010-10-04 16:33:58 +0200550 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 * Returns an associative array containing all of the information
552 * related to the upload, allowing the developer easy access in one array.
553 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200554 * @param string $index
Michiel Vugteveen0ee287e2012-06-11 10:38:14 +0200555 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200556 */
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200557 public function data($index = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 {
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200559 $data = array(
Michiel Vugteveen3a7fb042012-06-11 09:29:16 +0200560 'file_name' => $this->file_name,
561 'file_type' => $this->file_type,
562 'file_path' => $this->upload_path,
563 'full_path' => $this->upload_path.$this->file_name,
564 'raw_name' => str_replace($this->file_ext, '', $this->file_name),
565 'orig_name' => $this->orig_name,
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300566 'client_name' => $this->client_name,
Michiel Vugteveen3a7fb042012-06-11 09:29:16 +0200567 'file_ext' => $this->file_ext,
568 'file_size' => $this->file_size,
569 'is_image' => $this->is_image(),
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300570 'image_width' => $this->image_width,
571 'image_height' => $this->image_height,
572 'image_type' => $this->image_type,
573 'image_size_str' => $this->image_size_str,
574 );
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200575
Michiel Vugteveen46a04292012-06-11 10:37:52 +0200576 if ( ! empty($index))
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200577 {
Michiel Vugteveen46a04292012-06-11 10:37:52 +0200578 return isset($data[$index]) ? $data[$index] : NULL;
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200579 }
580
Michiel Vugteveen46a04292012-06-11 10:37:52 +0200581 return $data;
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 }
Barry Mienydd671972010-10-04 16:33:58 +0200583
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200585
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 /**
587 * Set Upload Path
588 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200589 * @param string $path
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200591 */
Greg Aker58fdee82010-11-10 15:07:09 -0600592 public function set_upload_path($path)
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 {
594 // Make sure it has a trailing slash
595 $this->upload_path = rtrim($path, '/').'/';
596 }
Barry Mienydd671972010-10-04 16:33:58 +0200597
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200599
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 /**
601 * Set the file name
602 *
603 * This function takes a filename/path as input and looks for the
604 * existence of a file with the same name. If found, it will append a
605 * number to the end of the filename to avoid overwriting a pre-existing file.
606 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200607 * @param string $path
608 * @param string $filename
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200610 */
Greg Aker58fdee82010-11-10 15:07:09 -0600611 public function set_filename($path, $filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100613 if ($this->encrypt_name === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200614 {
Barry Mienydd671972010-10-04 16:33:58 +0200615 $filename = md5(uniqid(mt_rand())).$this->file_ext;
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 }
Barry Mienydd671972010-10-04 16:33:58 +0200617
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 if ( ! file_exists($path.$filename))
619 {
620 return $filename;
621 }
Barry Mienydd671972010-10-04 16:33:58 +0200622
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 $filename = str_replace($this->file_ext, '', $filename);
Barry Mienydd671972010-10-04 16:33:58 +0200624
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 $new_filename = '';
Adam Jackettccbbea12011-08-21 16:19:11 -0400626 for ($i = 1; $i < $this->max_filename_increment; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200627 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 if ( ! file_exists($path.$filename.$i.$this->file_ext))
629 {
630 $new_filename = $filename.$i.$this->file_ext;
631 break;
632 }
633 }
634
Alex Bilbied261b1e2012-06-02 11:12:16 +0100635 if ($new_filename === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 {
637 $this->set_error('upload_bad_filename');
638 return FALSE;
639 }
640 else
641 {
642 return $new_filename;
643 }
644 }
Barry Mienydd671972010-10-04 16:33:58 +0200645
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200647
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 /**
649 * Set Maximum File Size
650 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200651 * @param int $n
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200653 */
Greg Aker58fdee82010-11-10 15:07:09 -0600654 public function set_max_filesize($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300656 $this->max_size = ((int) $n < 0) ? 0 : (int) $n;
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 }
Barry Mienydd671972010-10-04 16:33:58 +0200658
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200660
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 /**
662 * Set Maximum File Name Length
663 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200664 * @param int $n
Derek Allard2067d1a2008-11-13 22:59:24 +0000665 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200666 */
Greg Aker58fdee82010-11-10 15:07:09 -0600667 public function set_max_filename($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300669 $this->max_filename = ((int) $n < 0) ? 0 : (int) $n;
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 }
671
672 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200673
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 /**
675 * Set Maximum Image Width
676 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200677 * @param int $n
Derek Allard2067d1a2008-11-13 22:59:24 +0000678 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200679 */
Greg Aker58fdee82010-11-10 15:07:09 -0600680 public function set_max_width($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300682 $this->max_width = ((int) $n < 0) ? 0 : (int) $n;
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 }
Barry Mienydd671972010-10-04 16:33:58 +0200684
Derek Allard2067d1a2008-11-13 22:59:24 +0000685 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200686
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 /**
688 * Set Maximum Image Height
689 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200690 * @param int $n
Derek Allard2067d1a2008-11-13 22:59:24 +0000691 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200692 */
Greg Aker58fdee82010-11-10 15:07:09 -0600693 public function set_max_height($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000694 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300695 $this->max_height = ((int) $n < 0) ? 0 : (int) $n;
Derek Allard2067d1a2008-11-13 22:59:24 +0000696 }
Barry Mienydd671972010-10-04 16:33:58 +0200697
Derek Allard2067d1a2008-11-13 22:59:24 +0000698 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200699
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 /**
Andrey Andreev05aa2d62012-12-03 16:06:55 +0200701 * Set minimum image width
702 *
703 * @param int $n
704 * @return void
705 */
706 public function set_min_width($n)
707 {
708 $this->min_width = ((int) $n < 0) ? 0 : (int) $n;
709 }
710
711 // --------------------------------------------------------------------
712
713 /**
714 * Set minimum image height
715 *
716 * @param int $n
717 * @return void
718 */
719 public function set_min_height($n)
720 {
721 $this->min_height = ((int) $n < 0) ? 0 : (int) $n;
722 }
723
724 // --------------------------------------------------------------------
725
726 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000727 * Set Allowed File Types
728 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200729 * @param string $types
Derek Allard2067d1a2008-11-13 22:59:24 +0000730 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200731 */
Greg Aker58fdee82010-11-10 15:07:09 -0600732 public function set_allowed_types($types)
Derek Allard2067d1a2008-11-13 22:59:24 +0000733 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300734 if ( ! is_array($types) && $types === '*')
Derek Jonese12f64e2010-03-02 22:55:08 -0600735 {
736 $this->allowed_types = '*';
737 return;
738 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000739 $this->allowed_types = explode('|', $types);
740 }
Barry Mienydd671972010-10-04 16:33:58 +0200741
Derek Allard2067d1a2008-11-13 22:59:24 +0000742 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200743
Derek Allard2067d1a2008-11-13 22:59:24 +0000744 /**
745 * Set Image Properties
746 *
747 * Uses GD to determine the width/height/type of image
748 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200749 * @param string $path
Derek Allard2067d1a2008-11-13 22:59:24 +0000750 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200751 */
Greg Aker58fdee82010-11-10 15:07:09 -0600752 public function set_image_properties($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000753 {
754 if ( ! $this->is_image())
755 {
756 return;
757 }
758
759 if (function_exists('getimagesize'))
760 {
761 if (FALSE !== ($D = @getimagesize($path)))
Barry Mienydd671972010-10-04 16:33:58 +0200762 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000763 $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
764
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300765 $this->image_width = $D[0];
766 $this->image_height = $D[1];
767 $this->image_type = isset($types[$D[2]]) ? $types[$D[2]] : 'unknown';
768 $this->image_size_str = $D[3]; // string containing height and width
Derek Allard2067d1a2008-11-13 22:59:24 +0000769 }
770 }
771 }
Barry Mienydd671972010-10-04 16:33:58 +0200772
Derek Allard2067d1a2008-11-13 22:59:24 +0000773 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200774
Derek Allard2067d1a2008-11-13 22:59:24 +0000775 /**
776 * Set XSS Clean
777 *
778 * Enables the XSS flag so that the file that was uploaded
779 * will be run through the XSS filter.
780 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200781 * @param bool $flag
Derek Allard2067d1a2008-11-13 22:59:24 +0000782 * @return void
783 */
Greg Aker58fdee82010-11-10 15:07:09 -0600784 public function set_xss_clean($flag = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000785 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100786 $this->xss_clean = ($flag === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000787 }
Barry Mienydd671972010-10-04 16:33:58 +0200788
Derek Allard2067d1a2008-11-13 22:59:24 +0000789 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200790
Derek Allard2067d1a2008-11-13 22:59:24 +0000791 /**
792 * Validate the image
793 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000794 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200795 */
Greg Aker58fdee82010-11-10 15:07:09 -0600796 public function is_image()
Derek Allard2067d1a2008-11-13 22:59:24 +0000797 {
798 // IE will sometimes return odd mime-types during upload, so here we just standardize all
799 // jpegs or pngs to the same file type.
800
Derek Jones4b9c6292011-07-01 17:40:48 -0500801 $png_mimes = array('image/x-png');
Derek Allard2067d1a2008-11-13 22:59:24 +0000802 $jpeg_mimes = array('image/jpg', 'image/jpe', 'image/jpeg', 'image/pjpeg');
Barry Mienydd671972010-10-04 16:33:58 +0200803
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 if (in_array($this->file_type, $png_mimes))
805 {
806 $this->file_type = 'image/png';
807 }
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300808 elseif (in_array($this->file_type, $jpeg_mimes))
Derek Allard2067d1a2008-11-13 22:59:24 +0000809 {
810 $this->file_type = 'image/jpeg';
811 }
812
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300813 $img_mimes = array('image/gif', 'image/jpeg', 'image/png');
Derek Allard2067d1a2008-11-13 22:59:24 +0000814
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300815 return in_array($this->file_type, $img_mimes, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 }
Barry Mienydd671972010-10-04 16:33:58 +0200817
Derek Allard2067d1a2008-11-13 22:59:24 +0000818 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200819
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 /**
821 * Verify that the filetype is allowed
822 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200823 * @param bool $ignore_mime
Derek Allard2067d1a2008-11-13 22:59:24 +0000824 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200825 */
Greg Aker58fdee82010-11-10 15:07:09 -0600826 public function is_allowed_filetype($ignore_mime = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000827 {
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200828 if ($this->allowed_types === '*')
Derek Jonese12f64e2010-03-02 22:55:08 -0600829 {
830 return TRUE;
831 }
Barry Mienydd671972010-10-04 16:33:58 +0200832
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200833 if ( ! is_array($this->allowed_types) OR count($this->allowed_types) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000834 {
835 $this->set_error('upload_no_file_types');
836 return FALSE;
837 }
Barry Mienydd671972010-10-04 16:33:58 +0200838
Derek Jonese9d723f2010-07-12 10:10:59 -0500839 $ext = strtolower(ltrim($this->file_ext, '.'));
Barry Mienydd671972010-10-04 16:33:58 +0200840
Derek Jonese9d723f2010-07-12 10:10:59 -0500841 if ( ! in_array($ext, $this->allowed_types))
Derek Allard2067d1a2008-11-13 22:59:24 +0000842 {
Derek Jonese9d723f2010-07-12 10:10:59 -0500843 return FALSE;
844 }
Derek Jonesafa282f2009-02-10 17:11:52 +0000845
Barry Mienydd671972010-10-04 16:33:58 +0200846 // Images get some additional checks
Derek Jonese9d723f2010-07-12 10:10:59 -0500847 $image_types = array('gif', 'jpg', 'jpeg', 'png', 'jpe');
Barry Mienydd671972010-10-04 16:33:58 +0200848
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200849 if (in_array($ext, $image_types) && @getimagesize($this->file_temp) === FALSE)
Derek Jonese9d723f2010-07-12 10:10:59 -0500850 {
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200851 return FALSE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500852 }
Barry Mienydd671972010-10-04 16:33:58 +0200853
Derek Jonese9d723f2010-07-12 10:10:59 -0500854 if ($ignore_mime === TRUE)
855 {
856 return TRUE;
857 }
Barry Mienydd671972010-10-04 16:33:58 +0200858
Derek Jonese9d723f2010-07-12 10:10:59 -0500859 $mime = $this->mimes_types($ext);
Barry Mienydd671972010-10-04 16:33:58 +0200860
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300861 if (is_array($mime) && in_array($this->file_type, $mime, TRUE))
Derek Jonese9d723f2010-07-12 10:10:59 -0500862 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300863 return TRUE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500864 }
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200865 elseif ($mime === $this->file_type)
Derek Jonese9d723f2010-07-12 10:10:59 -0500866 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300867 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000868 }
Barry Mienydd671972010-10-04 16:33:58 +0200869
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 return FALSE;
871 }
Barry Mienydd671972010-10-04 16:33:58 +0200872
Derek Allard2067d1a2008-11-13 22:59:24 +0000873 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200874
Derek Allard2067d1a2008-11-13 22:59:24 +0000875 /**
876 * Verify that the file is within the allowed size
877 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200879 */
Greg Aker58fdee82010-11-10 15:07:09 -0600880 public function is_allowed_filesize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100882 return ($this->max_size === 0 OR $this->max_size > $this->file_size);
Derek Allard2067d1a2008-11-13 22:59:24 +0000883 }
Barry Mienydd671972010-10-04 16:33:58 +0200884
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200886
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 /**
888 * Verify that the image is within the allowed width/height
889 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000890 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200891 */
Greg Aker58fdee82010-11-10 15:07:09 -0600892 public function is_allowed_dimensions()
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 {
894 if ( ! $this->is_image())
895 {
896 return TRUE;
897 }
898
899 if (function_exists('getimagesize'))
900 {
901 $D = @getimagesize($this->file_temp);
902
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300903 if ($this->max_width > 0 && $D[0] > $this->max_width)
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 {
905 return FALSE;
906 }
907
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300908 if ($this->max_height > 0 && $D[1] > $this->max_height)
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 {
910 return FALSE;
911 }
Andrey Andreev05aa2d62012-12-03 16:06:55 +0200912
913 if ($this->min_width > 0 && $D[0] < $this->min_width)
914 {
915 return FALSE;
916 }
917
918 if ($this->min_height > 0 && $D[1] < $this->min_height)
919 {
920 return FALSE;
921 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 }
923
924 return TRUE;
925 }
Barry Mienydd671972010-10-04 16:33:58 +0200926
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200928
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 /**
930 * Validate Upload Path
931 *
932 * Verifies that it is a valid upload path with proper permissions.
933 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200935 */
Greg Aker58fdee82010-11-10 15:07:09 -0600936 public function validate_upload_path()
Derek Allard2067d1a2008-11-13 22:59:24 +0000937 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100938 if ($this->upload_path === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 {
940 $this->set_error('upload_no_filepath');
941 return FALSE;
942 }
Barry Mienydd671972010-10-04 16:33:58 +0200943
Andrey Andreevc839d282012-06-07 14:35:27 +0300944 if (@realpath($this->upload_path) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300946 $this->upload_path = str_replace('\\', '/', realpath($this->upload_path));
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 }
948
949 if ( ! @is_dir($this->upload_path))
950 {
951 $this->set_error('upload_no_filepath');
952 return FALSE;
953 }
954
955 if ( ! is_really_writable($this->upload_path))
956 {
957 $this->set_error('upload_not_writable');
958 return FALSE;
959 }
960
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300961 $this->upload_path = preg_replace('/(.+?)\/*$/', '\\1/', $this->upload_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000962 return TRUE;
963 }
Barry Mienydd671972010-10-04 16:33:58 +0200964
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200966
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 /**
968 * Extract the file extension
969 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200970 * @param string $filename
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200972 */
Greg Aker58fdee82010-11-10 15:07:09 -0600973 public function get_extension($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000974 {
975 $x = explode('.', $filename);
Adrianf496d132013-06-24 15:56:45 +0200976
Adrian74a228b2013-06-25 12:09:22 +0200977 if (count($x) === 1)
978 {
979 return '';
980 }
981
982 $ext = ($this->file_ext_tolower) ? strtolower(end($x)) : end($x);
983 return '.'.$ext;
Barry Mienydd671972010-10-04 16:33:58 +0200984 }
985
Derek Allard2067d1a2008-11-13 22:59:24 +0000986 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200987
Derek Allard2067d1a2008-11-13 22:59:24 +0000988 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000989 * Limit the File Name Length
990 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300991 * @param string $filename
992 * @param int $length
Derek Allard2067d1a2008-11-13 22:59:24 +0000993 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200994 */
Greg Aker58fdee82010-11-10 15:07:09 -0600995 public function limit_filename_length($filename, $length)
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 {
997 if (strlen($filename) < $length)
998 {
999 return $filename;
1000 }
Barry Mienydd671972010-10-04 16:33:58 +02001001
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 $ext = '';
1003 if (strpos($filename, '.') !== FALSE)
1004 {
1005 $parts = explode('.', $filename);
1006 $ext = '.'.array_pop($parts);
1007 $filename = implode('.', $parts);
1008 }
Barry Mienydd671972010-10-04 16:33:58 +02001009
Derek Allard2067d1a2008-11-13 22:59:24 +00001010 return substr($filename, 0, ($length - strlen($ext))).$ext;
1011 }
1012
1013 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001014
Derek Allard2067d1a2008-11-13 22:59:24 +00001015 /**
1016 * Runs the file through the XSS clean function
1017 *
1018 * This prevents people from embedding malicious code in their files.
1019 * I'm not sure that it won't negatively affect certain files in unexpected ways,
1020 * but so far I haven't found that it causes trouble.
1021 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001022 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001023 */
Greg Aker58fdee82010-11-10 15:07:09 -06001024 public function do_xss_clean()
Barry Mienydd671972010-10-04 16:33:58 +02001025 {
Derek Jonese9d723f2010-07-12 10:10:59 -05001026 $file = $this->file_temp;
Barry Mienydd671972010-10-04 16:33:58 +02001027
Andrey Andreev5036c9c2012-06-04 15:34:56 +03001028 if (filesize($file) == 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001029 {
1030 return FALSE;
1031 }
Barry Mienydd671972010-10-04 16:33:58 +02001032
Andrey Andreevc839d282012-06-07 14:35:27 +03001033 if (memory_get_usage() && ($memory_limit = ini_get('memory_limit')))
Greg Akerf82e51c2010-04-14 19:33:50 -05001034 {
Andrey Andreevc839d282012-06-07 14:35:27 +03001035 $memory_limit *= 1024 * 1024;
Barry Mienydd671972010-10-04 16:33:58 +02001036
Greg Akerc78a2592010-06-09 11:45:32 -05001037 // There was a bug/behavioural change in PHP 5.2, where numbers over one million get output
Andrey Andreev8e6f7a92012-03-26 20:13:00 +03001038 // into scientific notation. number_format() ensures this number is an integer
Greg Akerc78a2592010-06-09 11:45:32 -05001039 // http://bugs.php.net/bug.php?id=43053
Barry Mienydd671972010-10-04 16:33:58 +02001040
Andrey Andreevc839d282012-06-07 14:35:27 +03001041 $memory_limit = number_format(ceil(filesize($file) + $memory_limit), 0, '.', '');
Barry Mienydd671972010-10-04 16:33:58 +02001042
Andrey Andreevc839d282012-06-07 14:35:27 +03001043 ini_set('memory_limit', $memory_limit); // When an integer is used, the value is measured in bytes. - PHP.net
Greg Akerf82e51c2010-04-14 19:33:50 -05001044 }
1045
1046 // If the file being uploaded is an image, then we should have no problem with XSS attacks (in theory), but
1047 // IE can be fooled into mime-type detecting a malformed image as an html file, thus executing an XSS attack on anyone
Andrey Andreev8e6f7a92012-03-26 20:13:00 +03001048 // using IE who looks at the image. It does this by inspecting the first 255 bytes of an image. To get around this
1049 // CI will itself look at the first 255 bytes of an image to determine its relative safety. This can save a lot of
Barry Mienydd671972010-10-04 16:33:58 +02001050 // processor power and time if it is actually a clean image, as it will be in nearly all instances _except_ an
Greg Akerf82e51c2010-04-14 19:33:50 -05001051 // attempted XSS attack.
1052
1053 if (function_exists('getimagesize') && @getimagesize($file) !== FALSE)
1054 {
Barry Mienydd671972010-10-04 16:33:58 +02001055 if (($file = @fopen($file, 'rb')) === FALSE) // "b" to force binary
1056 {
Greg Akerf82e51c2010-04-14 19:33:50 -05001057 return FALSE; // Couldn't open the file, return FALSE
Barry Mienydd671972010-10-04 16:33:58 +02001058 }
Greg Akerf82e51c2010-04-14 19:33:50 -05001059
Barry Mienydd671972010-10-04 16:33:58 +02001060 $opening_bytes = fread($file, 256);
1061 fclose($file);
Greg Akerf82e51c2010-04-14 19:33:50 -05001062
1063 // These are known to throw IE into mime-type detection chaos
1064 // <a, <body, <head, <html, <img, <plaintext, <pre, <script, <table, <title
1065 // title is basically just in SVG, but we filter it anyhow
1066
vlakoff35672462013-02-15 01:36:04 +01001067 // if it's an image or no "triggers" detected in the first 256 bytes - we're good
Andrey Andreevc839d282012-06-07 14:35:27 +03001068 return ! preg_match('/<(a|body|head|html|img|plaintext|pre|script|table|title)[\s>]/i', $opening_bytes);
Greg Akerf82e51c2010-04-14 19:33:50 -05001069 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001070
1071 if (($data = @file_get_contents($file)) === FALSE)
1072 {
1073 return FALSE;
1074 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001075
Greg Akerf82e51c2010-04-14 19:33:50 -05001076 $CI =& get_instance();
Greg Akerf82e51c2010-04-14 19:33:50 -05001077 return $CI->security->xss_clean($data, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001078 }
Barry Mienydd671972010-10-04 16:33:58 +02001079
Derek Allard2067d1a2008-11-13 22:59:24 +00001080 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001081
Derek Allard2067d1a2008-11-13 22:59:24 +00001082 /**
1083 * Set an error message
1084 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001085 * @param string $msg
Derek Allard2067d1a2008-11-13 22:59:24 +00001086 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001087 */
Greg Aker58fdee82010-11-10 15:07:09 -06001088 public function set_error($msg)
Derek Allard2067d1a2008-11-13 22:59:24 +00001089 {
Barry Mienydd671972010-10-04 16:33:58 +02001090 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +00001091 $CI->lang->load('upload');
Barry Mienydd671972010-10-04 16:33:58 +02001092
Darren Benney4a8d1902013-03-30 20:07:49 +00001093 if ( ! is_array($msg))
Derek Allard2067d1a2008-11-13 22:59:24 +00001094 {
Darren Benney38d5f4b2013-03-30 21:00:38 +00001095 $msg = array($msg);
Derek Allard2067d1a2008-11-13 22:59:24 +00001096 }
Darren Benney4a8d1902013-03-30 20:07:49 +00001097
Darren Benney38d5f4b2013-03-30 21:00:38 +00001098 foreach ($msg as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001099 {
Darren Benney38d5f4b2013-03-30 21:00:38 +00001100 $msg = ($CI->lang->line($val) === FALSE) ? $val : $CI->lang->line($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001101 $this->error_msg[] = $msg;
1102 log_message('error', $msg);
1103 }
1104 }
Barry Mienydd671972010-10-04 16:33:58 +02001105
Derek Allard2067d1a2008-11-13 22:59:24 +00001106 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001107
Derek Allard2067d1a2008-11-13 22:59:24 +00001108 /**
1109 * Display the error message
1110 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001111 * @param string $open
1112 * @param string $close
Derek Allard2067d1a2008-11-13 22:59:24 +00001113 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001114 */
Greg Aker58fdee82010-11-10 15:07:09 -06001115 public function display_errors($open = '<p>', $close = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +00001116 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +03001117 return (count($this->error_msg) > 0) ? $open.implode($close.$open, $this->error_msg).$close : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001118 }
Barry Mienydd671972010-10-04 16:33:58 +02001119
Derek Allard2067d1a2008-11-13 22:59:24 +00001120 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001121
Derek Allard2067d1a2008-11-13 22:59:24 +00001122 /**
1123 * List of Mime Types
1124 *
Andrey Andreev8e6f7a92012-03-26 20:13:00 +03001125 * This is a list of mime types. We use it to validate
Derek Allard2067d1a2008-11-13 22:59:24 +00001126 * the "allowed types" set by the developer
1127 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001128 * @param string $mime
Derek Allard2067d1a2008-11-13 22:59:24 +00001129 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001130 */
Greg Aker58fdee82010-11-10 15:07:09 -06001131 public function mimes_types($mime)
Derek Allard2067d1a2008-11-13 22:59:24 +00001132 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +03001133 return isset($this->mimes[$mime]) ? $this->mimes[$mime] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001134 }
1135
1136 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001137
Derek Allard2067d1a2008-11-13 22:59:24 +00001138 /**
1139 * Prep Filename
1140 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001141 * Prevents possible script execution from Apache's handling
1142 * of files' multiple extensions.
Derek Allard2067d1a2008-11-13 22:59:24 +00001143 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001144 * @link http://httpd.apache.org/docs/1.3/mod/mod_mime.html#multipleext
1145 *
1146 * @param string $filename
Derek Allard2067d1a2008-11-13 22:59:24 +00001147 * @return string
1148 */
Greg Aker58fdee82010-11-10 15:07:09 -06001149 protected function _prep_filename($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +00001150 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001151 if (strpos($filename, '.') === FALSE OR $this->allowed_types === '*')
Derek Allard2067d1a2008-11-13 22:59:24 +00001152 {
1153 return $filename;
1154 }
Derek Allard616dab82009-02-16 15:44:32 +00001155
Derek Allard2067d1a2008-11-13 22:59:24 +00001156 $parts = explode('.', $filename);
1157 $ext = array_pop($parts);
1158 $filename = array_shift($parts);
Derek Allard616dab82009-02-16 15:44:32 +00001159
Derek Allard2067d1a2008-11-13 22:59:24 +00001160 foreach ($parts as $part)
1161 {
Derek Jonese9d723f2010-07-12 10:10:59 -05001162 if ( ! in_array(strtolower($part), $this->allowed_types) OR $this->mimes_types(strtolower($part)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001163 {
1164 $filename .= '.'.$part.'_';
1165 }
1166 else
1167 {
1168 $filename .= '.'.$part;
1169 }
1170 }
Derek Allardd70b0642009-02-16 13:51:42 +00001171
Andrey Andreev8e6f7a92012-03-26 20:13:00 +03001172 return $filename.'.'.$ext;
Derek Allard2067d1a2008-11-13 22:59:24 +00001173 }
1174
1175 // --------------------------------------------------------------------
1176
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001177 /**
1178 * File MIME type
1179 *
1180 * Detects the (actual) MIME type of the uploaded file, if possible.
1181 * The input array is expected to be $_FILES[$field]
1182 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001183 * @param array $file
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001184 * @return void
1185 */
1186 protected function _file_mime_type($file)
1187 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001188 // We'll need this to validate the MIME info string (e.g. text/plain; charset=us-ascii)
Andrey Andreevf7aed122011-12-13 11:01:06 +02001189 $regexp = '/^([a-z\-]+\/[a-z0-9\-\.\+]+)(;\s.+)?$/';
Andrey Andreeva49e3812011-12-09 13:05:22 +02001190
1191 /* Fileinfo extension - most reliable method
1192 *
1193 * Unfortunately, prior to PHP 5.3 - it's only available as a PECL extension and the
1194 * more convenient FILEINFO_MIME_TYPE flag doesn't exist.
1195 */
1196 if (function_exists('finfo_file'))
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001197 {
Andrey Andreevd60e7002012-06-17 00:03:03 +03001198 $finfo = @finfo_open(FILEINFO_MIME);
Andrey Andreeva49e3812011-12-09 13:05:22 +02001199 if (is_resource($finfo)) // It is possible that a FALSE value is returned, if there is no magic MIME database file found on the system
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001200 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001201 $mime = @finfo_file($finfo, $file['tmp_name']);
1202 finfo_close($finfo);
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001203
1204 /* According to the comments section of the PHP manual page,
1205 * it is possible that this function returns an empty string
Andrey Andreev2cec39f2011-09-24 22:59:37 +03001206 * for some files (e.g. if they don't exist in the magic MIME database)
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001207 */
Andrey Andreeva49e3812011-12-09 13:05:22 +02001208 if (is_string($mime) && preg_match($regexp, $mime, $matches))
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001209 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001210 $this->file_type = $matches[1];
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001211 return;
1212 }
1213 }
1214 }
1215
Andrey Andreeva49e3812011-12-09 13:05:22 +02001216 /* This is an ugly hack, but UNIX-type systems provide a "native" way to detect the file type,
1217 * which is still more secure than depending on the value of $_FILES[$field]['type'], and as it
1218 * was reported in issue #750 (https://github.com/EllisLab/CodeIgniter/issues/750) - it's better
1219 * than mime_content_type() as well, hence the attempts to try calling the command line with
1220 * three different functions.
Andrey Andreev6700b932011-09-24 14:25:33 +03001221 *
1222 * Notes:
Andrey Andreev59654312011-12-02 14:28:54 +02001223 * - the DIRECTORY_SEPARATOR comparison ensures that we're not on a Windows system
Andrey Andreeva49e3812011-12-09 13:05:22 +02001224 * - many system admins would disable the exec(), shell_exec(), popen() and similar functions
vlakofff3994252013-02-19 01:45:23 +01001225 * due to security concerns, hence the function_usable() checks
Andrey Andreev6700b932011-09-24 14:25:33 +03001226 */
Andrey Andreeva49e3812011-12-09 13:05:22 +02001227 if (DIRECTORY_SEPARATOR !== '\\')
Andrey Andreev6700b932011-09-24 14:25:33 +03001228 {
Andrey Andreevd60e7002012-06-17 00:03:03 +03001229 $cmd = function_exists('escapeshellarg')
1230 ? 'file --brief --mime '.escapeshellarg($file['tmp_name']).' 2>&1'
1231 : 'file --brief --mime '.$file['tmp_name'].' 2>&1';
Andrey Andreeva49e3812011-12-09 13:05:22 +02001232
Andrey Andreeve9d2dc82012-11-07 14:23:29 +02001233 if (function_usable('exec'))
Andrey Andreev6700b932011-09-24 14:25:33 +03001234 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001235 /* This might look confusing, as $mime is being populated with all of the output when set in the second parameter.
vlakofff3994252013-02-19 01:45:23 +01001236 * However, we only need the last line, which is the actual return value of exec(), and as such - it overwrites
Andrey Andreeva49e3812011-12-09 13:05:22 +02001237 * anything that could already be set for $mime previously. This effectively makes the second parameter a dummy
1238 * value, which is only put to allow us to get the return status code.
1239 */
1240 $mime = @exec($cmd, $mime, $return_status);
1241 if ($return_status === 0 && is_string($mime) && preg_match($regexp, $mime, $matches))
1242 {
1243 $this->file_type = $matches[1];
1244 return;
1245 }
1246 }
1247
Andrey Andreeve9d2dc82012-11-07 14:23:29 +02001248 if ( (bool) @ini_get('safe_mode') === FALSE && function_usable('shell_exec'))
Andrey Andreeva49e3812011-12-09 13:05:22 +02001249 {
1250 $mime = @shell_exec($cmd);
1251 if (strlen($mime) > 0)
1252 {
1253 $mime = explode("\n", trim($mime));
1254 if (preg_match($regexp, $mime[(count($mime) - 1)], $matches))
1255 {
1256 $this->file_type = $matches[1];
1257 return;
1258 }
1259 }
1260 }
1261
Andrey Andreeve9d2dc82012-11-07 14:23:29 +02001262 if (function_usable('popen'))
Andrey Andreeva49e3812011-12-09 13:05:22 +02001263 {
1264 $proc = @popen($cmd, 'r');
1265 if (is_resource($proc))
1266 {
tubalmartin010f1f42012-03-03 22:24:31 +01001267 $mime = @fread($proc, 512);
Andrey Andreeva49e3812011-12-09 13:05:22 +02001268 @pclose($proc);
1269 if ($mime !== FALSE)
1270 {
1271 $mime = explode("\n", trim($mime));
1272 if (preg_match($regexp, $mime[(count($mime) - 1)], $matches))
1273 {
1274 $this->file_type = $matches[1];
1275 return;
1276 }
1277 }
1278 }
1279 }
1280 }
1281
1282 // Fall back to the deprecated mime_content_type(), if available (still better than $_FILES[$field]['type'])
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001283 if (function_exists('mime_content_type'))
1284 {
1285 $this->file_type = @mime_content_type($file['tmp_name']);
Andrey Andreeva49e3812011-12-09 13:05:22 +02001286 if (strlen($this->file_type) > 0) // It's possible that mime_content_type() returns FALSE or an empty string
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001287 {
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001288 return;
1289 }
1290 }
1291
1292 $this->file_type = $file['type'];
1293 }
1294
Derek Allard2067d1a2008-11-13 22:59:24 +00001295}
Derek Allard2067d1a2008-11-13 22:59:24 +00001296
1297/* End of file Upload.php */
Adrian74a228b2013-06-25 12:09:22 +02001298/* Location: ./system/libraries/Upload.php */