blob: 5d163fb7757f6f0468b7a86b2a6ff585c4c9f387 [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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, 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 /**
62 * Maximum filename length
63 *
64 * @var int
65 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +030066 public $max_filename = 0;
Andrey Andreevf5ccd122012-11-02 00:17:39 +020067
68 /**
69 * Maximum duplicate filename increment ID
70 *
71 * @var int
72 */
Adam Jackettccbbea12011-08-21 16:19:11 -040073 public $max_filename_increment = 100;
Andrey Andreevf5ccd122012-11-02 00:17:39 +020074
75 /**
76 * Allowed file types
77 *
78 * @var string
79 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +030080 public $allowed_types = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +020081
82 /**
83 * Temporary filename
84 *
85 * @var string
86 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +030087 public $file_temp = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +020088
89 /**
90 * Filename
91 *
92 * @var string
93 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +030094 public $file_name = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +020095
96 /**
97 * Original filename
98 *
99 * @var string
100 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300101 public $orig_name = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200102
103 /**
104 * File type
105 *
106 * @var string
107 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300108 public $file_type = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200109
110 /**
111 * File size
112 *
113 * @var int
114 */
115 public $file_size = NULL;
116
117 /**
118 * Filename extension
119 *
120 * @var string
121 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300122 public $file_ext = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200123
124 /**
125 * Upload path
126 *
127 * @var string
128 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300129 public $upload_path = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200130
131 /**
132 * Overwrite flag
133 *
134 * @var bool
135 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300136 public $overwrite = FALSE;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200137
138 /**
139 * Obfuscate filename flag
140 *
141 * @var bool
142 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300143 public $encrypt_name = FALSE;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200144
145 /**
146 * Is image flag
147 *
148 * @var bool
149 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300150 public $is_image = FALSE;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200151
152 /**
153 * Image width
154 *
155 * @var int
156 */
157 public $image_width = NULL;
158
159 /**
160 * Image height
161 *
162 * @var int
163 */
164 public $image_height = NULL;
165
166 /**
167 * Image type
168 *
169 * @var string
170 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300171 public $image_type = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200172
173 /**
174 * Image size string
175 *
176 * @var string
177 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300178 public $image_size_str = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200179
180 /**
181 * Error messages list
182 *
183 * @var array
184 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300185 public $error_msg = array();
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200186
187 /**
188 * MIME types list
189 *
190 * @var array
191 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300192 public $mimes = array();
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200193
194 /**
195 * Remove spaces flag
196 *
197 * @var bool
198 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300199 public $remove_spaces = TRUE;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200200
201 /**
202 * MIME detection flag
203 *
204 * @var bool
205 */
Andrey Andreevd60e7002012-06-17 00:03:03 +0300206 public $detect_mime = TRUE;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200207
208 /**
209 * XSS filter flag
210 *
211 * @var bool
212 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300213 public $xss_clean = FALSE;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200214
215 /**
216 * Temporary filename prefix
217 *
218 * @var string
219 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300220 public $temp_prefix = 'temp_file_';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200221
222 /**
223 * Filename sent by the client
224 *
225 * @var bool
226 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300227 public $client_name = '';
Barry Mienydd671972010-10-04 16:33:58 +0200228
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200229 // --------------------------------------------------------------------
230
231 /**
232 * Filename override
233 *
234 * @var string
235 */
Greg Aker58fdee82010-11-10 15:07:09 -0600236 protected $_file_name_override = '';
Barry Mienydd671972010-10-04 16:33:58 +0200237
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200238 // --------------------------------------------------------------------
239
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 /**
241 * Constructor
242 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200243 * @param array $props
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300244 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 */
Greg Aker58fdee82010-11-10 15:07:09 -0600246 public function __construct($props = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 {
248 if (count($props) > 0)
249 {
250 $this->initialize($props);
251 }
Barry Mienydd671972010-10-04 16:33:58 +0200252
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300253 $this->mimes =& get_mimes();
254
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300255 log_message('debug', 'Upload Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 }
Barry Mienydd671972010-10-04 16:33:58 +0200257
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200259
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 /**
261 * Initialize preferences
262 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200263 * @param array $config
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200265 */
Greg Aker58fdee82010-11-10 15:07:09 -0600266 public function initialize($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 {
268 $defaults = array(
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300269 'max_size' => 0,
270 'max_width' => 0,
271 'max_height' => 0,
272 'max_filename' => 0,
273 'max_filename_increment' => 100,
274 'allowed_types' => '',
275 'file_temp' => '',
276 'file_name' => '',
277 'orig_name' => '',
278 'file_type' => '',
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200279 'file_size' => NULL,
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300280 'file_ext' => '',
281 'upload_path' => '',
282 'overwrite' => FALSE,
283 'encrypt_name' => FALSE,
284 'is_image' => FALSE,
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200285 'image_width' => NULL,
286 'image_height' => NULL,
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300287 'image_type' => '',
288 'image_size_str' => '',
289 'error_msg' => array(),
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300290 'remove_spaces' => TRUE,
Andrey Andreevd60e7002012-06-17 00:03:03 +0300291 'detect_mime' => TRUE,
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300292 'xss_clean' => FALSE,
293 'temp_prefix' => 'temp_file_',
294 'client_name' => ''
295 );
Barry Mienydd671972010-10-04 16:33:58 +0200296
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 foreach ($defaults as $key => $val)
298 {
299 if (isset($config[$key]))
300 {
301 $method = 'set_'.$key;
302 if (method_exists($this, $method))
303 {
304 $this->$method($config[$key]);
305 }
306 else
307 {
308 $this->$key = $config[$key];
Barry Mienydd671972010-10-04 16:33:58 +0200309 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 }
311 else
312 {
313 $this->$key = $val;
314 }
315 }
Barry Mienydd671972010-10-04 16:33:58 +0200316
Derek Jonese9d723f2010-07-12 10:10:59 -0500317 // if a file_name was provided in the config, use it instead of the user input
318 // supplied file name for all uploads until initialized again
319 $this->_file_name_override = $this->file_name;
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 }
Barry Mienydd671972010-10-04 16:33:58 +0200321
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200323
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 /**
325 * Perform the file upload
326 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200327 * @param string $field
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200329 */
Greg Aker58fdee82010-11-10 15:07:09 -0600330 public function do_upload($field = 'userfile')
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300332 // Is $_FILES[$field] set? If not, no reason to continue.
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 if ( ! isset($_FILES[$field]))
334 {
335 $this->set_error('upload_no_file_selected');
336 return FALSE;
337 }
Barry Mienydd671972010-10-04 16:33:58 +0200338
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 // Is the upload path valid?
340 if ( ! $this->validate_upload_path())
341 {
342 // errors will already be set by validate_upload_path() so just return FALSE
343 return FALSE;
344 }
345
346 // Was the file able to be uploaded? If not, determine the reason why.
347 if ( ! is_uploaded_file($_FILES[$field]['tmp_name']))
348 {
349 $error = ( ! isset($_FILES[$field]['error'])) ? 4 : $_FILES[$field]['error'];
350
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300351 switch ($error)
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 {
353 case 1: // UPLOAD_ERR_INI_SIZE
354 $this->set_error('upload_file_exceeds_limit');
355 break;
356 case 2: // UPLOAD_ERR_FORM_SIZE
357 $this->set_error('upload_file_exceeds_form_limit');
358 break;
359 case 3: // UPLOAD_ERR_PARTIAL
Barry Mienydd671972010-10-04 16:33:58 +0200360 $this->set_error('upload_file_partial');
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 break;
362 case 4: // UPLOAD_ERR_NO_FILE
Barry Mienydd671972010-10-04 16:33:58 +0200363 $this->set_error('upload_no_file_selected');
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 break;
365 case 6: // UPLOAD_ERR_NO_TMP_DIR
366 $this->set_error('upload_no_temp_directory');
367 break;
368 case 7: // UPLOAD_ERR_CANT_WRITE
369 $this->set_error('upload_unable_to_write_file');
370 break;
371 case 8: // UPLOAD_ERR_EXTENSION
372 $this->set_error('upload_stopped_by_extension');
373 break;
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300374 default:
375 $this->set_error('upload_no_file_selected');
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 break;
377 }
378
379 return FALSE;
380 }
381
382 // Set the uploaded data as class variables
Barry Mienydd671972010-10-04 16:33:58 +0200383 $this->file_temp = $_FILES[$field]['tmp_name'];
384 $this->file_size = $_FILES[$field]['size'];
Andrey Andreevd60e7002012-06-17 00:03:03 +0300385
386 // Skip MIME type detection?
387 if ($this->detect_mime !== FALSE)
388 {
389 $this->_file_mime_type($_FILES[$field]);
390 }
391
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300392 $this->file_type = preg_replace('/^(.+?);.*$/', '\\1', $this->file_type);
Derek Jones616fb022010-04-22 16:52:18 -0500393 $this->file_type = strtolower(trim(stripslashes($this->file_type), '"'));
Derek Jonese9d723f2010-07-12 10:10:59 -0500394 $this->file_name = $this->_prep_filename($_FILES[$field]['name']);
395 $this->file_ext = $this->get_extension($this->file_name);
396 $this->client_name = $this->file_name;
Barry Mienydd671972010-10-04 16:33:58 +0200397
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 // Is the file type allowed to be uploaded?
399 if ( ! $this->is_allowed_filetype())
400 {
401 $this->set_error('upload_invalid_filetype');
402 return FALSE;
403 }
404
Derek Jonese9d723f2010-07-12 10:10:59 -0500405 // if we're overriding, let's now make sure the new name and type is allowed
Alex Bilbied261b1e2012-06-02 11:12:16 +0100406 if ($this->_file_name_override !== '')
Derek Jonese9d723f2010-07-12 10:10:59 -0500407 {
408 $this->file_name = $this->_prep_filename($this->_file_name_override);
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000409
410 // If no extension was provided in the file_name config item, use the uploaded one
Pascal Kriete14287f32011-02-14 13:39:34 -0500411 if (strpos($this->_file_name_override, '.') === FALSE)
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000412 {
413 $this->file_name .= $this->file_ext;
414 }
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000415 else
416 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300417 // An extension was provided, lets have it!
418 $this->file_ext = $this->get_extension($this->_file_name_override);
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000419 }
Derek Jonese9d723f2010-07-12 10:10:59 -0500420
421 if ( ! $this->is_allowed_filetype(TRUE))
422 {
423 $this->set_error('upload_invalid_filetype');
Barry Mienydd671972010-10-04 16:33:58 +0200424 return FALSE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500425 }
426 }
Barry Mienydd671972010-10-04 16:33:58 +0200427
Derek Jonese9d723f2010-07-12 10:10:59 -0500428 // Convert the file size to kilobytes
429 if ($this->file_size > 0)
430 {
431 $this->file_size = round($this->file_size/1024, 2);
432 }
433
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 // Is the file size within the allowed maximum?
435 if ( ! $this->is_allowed_filesize())
436 {
437 $this->set_error('upload_invalid_filesize');
438 return FALSE;
439 }
440
441 // Are the image dimensions within the allowed size?
442 // Note: This can fail if the server has an open_basdir restriction.
443 if ( ! $this->is_allowed_dimensions())
444 {
445 $this->set_error('upload_invalid_dimensions');
446 return FALSE;
447 }
448
449 // Sanitize the file name for security
450 $this->file_name = $this->clean_file_name($this->file_name);
Barry Mienydd671972010-10-04 16:33:58 +0200451
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 // Truncate the file name if it's too long
453 if ($this->max_filename > 0)
454 {
455 $this->file_name = $this->limit_filename_length($this->file_name, $this->max_filename);
456 }
457
458 // Remove white spaces in the name
Alex Bilbied261b1e2012-06-02 11:12:16 +0100459 if ($this->remove_spaces === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300461 $this->file_name = preg_replace('/\s+/', '_', $this->file_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 }
463
464 /*
465 * Validate the file name
466 * This function appends an number onto the end of
467 * the file if one with the same name already exists.
468 * If it returns false there was a problem.
469 */
470 $this->orig_name = $this->file_name;
471
Alex Bilbied261b1e2012-06-02 11:12:16 +0100472 if ($this->overwrite === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 {
474 $this->file_name = $this->set_filename($this->upload_path, $this->file_name);
Barry Mienydd671972010-10-04 16:33:58 +0200475
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 if ($this->file_name === FALSE)
477 {
478 return FALSE;
479 }
480 }
481
482 /*
Derek Jonese9d723f2010-07-12 10:10:59 -0500483 * Run the file through the XSS hacking filter
484 * This helps prevent malicious code from being
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300485 * embedded within a file. Scripts can easily
Derek Jonese9d723f2010-07-12 10:10:59 -0500486 * be disguised as images or other file types.
487 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300488 if ($this->xss_clean && $this->do_xss_clean() === FALSE)
Derek Jonese9d723f2010-07-12 10:10:59 -0500489 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300490 $this->set_error('upload_unable_to_write_file');
491 return FALSE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500492 }
493
494 /*
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 * Move the file to the final destination
496 * To deal with different server configurations
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300497 * we'll attempt to use copy() first. If that fails
498 * we'll use move_uploaded_file(). One of the two should
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 * reliably work in most environments
500 */
501 if ( ! @copy($this->file_temp, $this->upload_path.$this->file_name))
502 {
503 if ( ! @move_uploaded_file($this->file_temp, $this->upload_path.$this->file_name))
504 {
Barry Mienydd671972010-10-04 16:33:58 +0200505 $this->set_error('upload_destination_error');
506 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 }
508 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000509
510 /*
511 * Set the finalized image dimensions
512 * This sets the image width/height (assuming the
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300513 * file was an image). We use this information
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 * in the "data" function.
515 */
516 $this->set_image_properties($this->upload_path.$this->file_name);
517
518 return TRUE;
519 }
Barry Mienydd671972010-10-04 16:33:58 +0200520
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200522
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 /**
524 * Finalized Data Array
Barry Mienydd671972010-10-04 16:33:58 +0200525 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 * Returns an associative array containing all of the information
527 * related to the upload, allowing the developer easy access in one array.
528 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200529 * @param string $index
Michiel Vugteveen0ee287e2012-06-11 10:38:14 +0200530 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200531 */
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200532 public function data($index = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 {
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200534 $data = array(
Michiel Vugteveen3a7fb042012-06-11 09:29:16 +0200535 'file_name' => $this->file_name,
536 'file_type' => $this->file_type,
537 'file_path' => $this->upload_path,
538 'full_path' => $this->upload_path.$this->file_name,
539 'raw_name' => str_replace($this->file_ext, '', $this->file_name),
540 'orig_name' => $this->orig_name,
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300541 'client_name' => $this->client_name,
Michiel Vugteveen3a7fb042012-06-11 09:29:16 +0200542 'file_ext' => $this->file_ext,
543 'file_size' => $this->file_size,
544 'is_image' => $this->is_image(),
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300545 'image_width' => $this->image_width,
546 'image_height' => $this->image_height,
547 'image_type' => $this->image_type,
548 'image_size_str' => $this->image_size_str,
549 );
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200550
Michiel Vugteveen46a04292012-06-11 10:37:52 +0200551 if ( ! empty($index))
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200552 {
Michiel Vugteveen46a04292012-06-11 10:37:52 +0200553 return isset($data[$index]) ? $data[$index] : NULL;
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200554 }
555
Michiel Vugteveen46a04292012-06-11 10:37:52 +0200556 return $data;
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 }
Barry Mienydd671972010-10-04 16:33:58 +0200558
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200560
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 /**
562 * Set Upload Path
563 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200564 * @param string $path
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200566 */
Greg Aker58fdee82010-11-10 15:07:09 -0600567 public function set_upload_path($path)
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 {
569 // Make sure it has a trailing slash
570 $this->upload_path = rtrim($path, '/').'/';
571 }
Barry Mienydd671972010-10-04 16:33:58 +0200572
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200574
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 /**
576 * Set the file name
577 *
578 * This function takes a filename/path as input and looks for the
579 * existence of a file with the same name. If found, it will append a
580 * number to the end of the filename to avoid overwriting a pre-existing file.
581 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200582 * @param string $path
583 * @param string $filename
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200585 */
Greg Aker58fdee82010-11-10 15:07:09 -0600586 public function set_filename($path, $filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100588 if ($this->encrypt_name === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200589 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 mt_srand();
Barry Mienydd671972010-10-04 16:33:58 +0200591 $filename = md5(uniqid(mt_rand())).$this->file_ext;
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 }
Barry Mienydd671972010-10-04 16:33:58 +0200593
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 if ( ! file_exists($path.$filename))
595 {
596 return $filename;
597 }
Barry Mienydd671972010-10-04 16:33:58 +0200598
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 $filename = str_replace($this->file_ext, '', $filename);
Barry Mienydd671972010-10-04 16:33:58 +0200600
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 $new_filename = '';
Adam Jackettccbbea12011-08-21 16:19:11 -0400602 for ($i = 1; $i < $this->max_filename_increment; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200603 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 if ( ! file_exists($path.$filename.$i.$this->file_ext))
605 {
606 $new_filename = $filename.$i.$this->file_ext;
607 break;
608 }
609 }
610
Alex Bilbied261b1e2012-06-02 11:12:16 +0100611 if ($new_filename === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 {
613 $this->set_error('upload_bad_filename');
614 return FALSE;
615 }
616 else
617 {
618 return $new_filename;
619 }
620 }
Barry Mienydd671972010-10-04 16:33:58 +0200621
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200623
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 /**
625 * Set Maximum File Size
626 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200627 * @param int $n
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200629 */
Greg Aker58fdee82010-11-10 15:07:09 -0600630 public function set_max_filesize($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300632 $this->max_size = ((int) $n < 0) ? 0 : (int) $n;
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 }
Barry Mienydd671972010-10-04 16:33:58 +0200634
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200636
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 /**
638 * Set Maximum File Name Length
639 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200640 * @param int $n
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200642 */
Greg Aker58fdee82010-11-10 15:07:09 -0600643 public function set_max_filename($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300645 $this->max_filename = ((int) $n < 0) ? 0 : (int) $n;
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 }
647
648 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200649
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 /**
651 * Set Maximum Image Width
652 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200653 * @param int $n
Derek Allard2067d1a2008-11-13 22:59:24 +0000654 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200655 */
Greg Aker58fdee82010-11-10 15:07:09 -0600656 public function set_max_width($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300658 $this->max_width = ((int) $n < 0) ? 0 : (int) $n;
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 }
Barry Mienydd671972010-10-04 16:33:58 +0200660
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200662
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 /**
664 * Set Maximum Image Height
665 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200666 * @param int $n
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200668 */
Greg Aker58fdee82010-11-10 15:07:09 -0600669 public function set_max_height($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300671 $this->max_height = ((int) $n < 0) ? 0 : (int) $n;
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 }
Barry Mienydd671972010-10-04 16:33:58 +0200673
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200675
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 /**
677 * Set Allowed File Types
678 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200679 * @param string $types
Derek Allard2067d1a2008-11-13 22:59:24 +0000680 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200681 */
Greg Aker58fdee82010-11-10 15:07:09 -0600682 public function set_allowed_types($types)
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300684 if ( ! is_array($types) && $types === '*')
Derek Jonese12f64e2010-03-02 22:55:08 -0600685 {
686 $this->allowed_types = '*';
687 return;
688 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 $this->allowed_types = explode('|', $types);
690 }
Barry Mienydd671972010-10-04 16:33:58 +0200691
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200693
Derek Allard2067d1a2008-11-13 22:59:24 +0000694 /**
695 * Set Image Properties
696 *
697 * Uses GD to determine the width/height/type of image
698 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200699 * @param string $path
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200701 */
Greg Aker58fdee82010-11-10 15:07:09 -0600702 public function set_image_properties($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000703 {
704 if ( ! $this->is_image())
705 {
706 return;
707 }
708
709 if (function_exists('getimagesize'))
710 {
711 if (FALSE !== ($D = @getimagesize($path)))
Barry Mienydd671972010-10-04 16:33:58 +0200712 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000713 $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
714
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300715 $this->image_width = $D[0];
716 $this->image_height = $D[1];
717 $this->image_type = isset($types[$D[2]]) ? $types[$D[2]] : 'unknown';
718 $this->image_size_str = $D[3]; // string containing height and width
Derek Allard2067d1a2008-11-13 22:59:24 +0000719 }
720 }
721 }
Barry Mienydd671972010-10-04 16:33:58 +0200722
Derek Allard2067d1a2008-11-13 22:59:24 +0000723 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200724
Derek Allard2067d1a2008-11-13 22:59:24 +0000725 /**
726 * Set XSS Clean
727 *
728 * Enables the XSS flag so that the file that was uploaded
729 * will be run through the XSS filter.
730 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200731 * @param bool $flag
Derek Allard2067d1a2008-11-13 22:59:24 +0000732 * @return void
733 */
Greg Aker58fdee82010-11-10 15:07:09 -0600734 public function set_xss_clean($flag = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000735 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100736 $this->xss_clean = ($flag === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000737 }
Barry Mienydd671972010-10-04 16:33:58 +0200738
Derek Allard2067d1a2008-11-13 22:59:24 +0000739 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200740
Derek Allard2067d1a2008-11-13 22:59:24 +0000741 /**
742 * Validate the image
743 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000744 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200745 */
Greg Aker58fdee82010-11-10 15:07:09 -0600746 public function is_image()
Derek Allard2067d1a2008-11-13 22:59:24 +0000747 {
748 // IE will sometimes return odd mime-types during upload, so here we just standardize all
749 // jpegs or pngs to the same file type.
750
Derek Jones4b9c6292011-07-01 17:40:48 -0500751 $png_mimes = array('image/x-png');
Derek Allard2067d1a2008-11-13 22:59:24 +0000752 $jpeg_mimes = array('image/jpg', 'image/jpe', 'image/jpeg', 'image/pjpeg');
Barry Mienydd671972010-10-04 16:33:58 +0200753
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 if (in_array($this->file_type, $png_mimes))
755 {
756 $this->file_type = 'image/png';
757 }
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300758 elseif (in_array($this->file_type, $jpeg_mimes))
Derek Allard2067d1a2008-11-13 22:59:24 +0000759 {
760 $this->file_type = 'image/jpeg';
761 }
762
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300763 $img_mimes = array('image/gif', 'image/jpeg', 'image/png');
Derek Allard2067d1a2008-11-13 22:59:24 +0000764
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300765 return in_array($this->file_type, $img_mimes, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000766 }
Barry Mienydd671972010-10-04 16:33:58 +0200767
Derek Allard2067d1a2008-11-13 22:59:24 +0000768 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200769
Derek Allard2067d1a2008-11-13 22:59:24 +0000770 /**
771 * Verify that the filetype is allowed
772 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200773 * @param bool $ignore_mime
Derek Allard2067d1a2008-11-13 22:59:24 +0000774 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200775 */
Greg Aker58fdee82010-11-10 15:07:09 -0600776 public function is_allowed_filetype($ignore_mime = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000777 {
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200778 if ($this->allowed_types === '*')
Derek Jonese12f64e2010-03-02 22:55:08 -0600779 {
780 return TRUE;
781 }
Barry Mienydd671972010-10-04 16:33:58 +0200782
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200783 if ( ! is_array($this->allowed_types) OR count($this->allowed_types) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000784 {
785 $this->set_error('upload_no_file_types');
786 return FALSE;
787 }
Barry Mienydd671972010-10-04 16:33:58 +0200788
Derek Jonese9d723f2010-07-12 10:10:59 -0500789 $ext = strtolower(ltrim($this->file_ext, '.'));
Barry Mienydd671972010-10-04 16:33:58 +0200790
Derek Jonese9d723f2010-07-12 10:10:59 -0500791 if ( ! in_array($ext, $this->allowed_types))
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 {
Derek Jonese9d723f2010-07-12 10:10:59 -0500793 return FALSE;
794 }
Derek Jonesafa282f2009-02-10 17:11:52 +0000795
Barry Mienydd671972010-10-04 16:33:58 +0200796 // Images get some additional checks
Derek Jonese9d723f2010-07-12 10:10:59 -0500797 $image_types = array('gif', 'jpg', 'jpeg', 'png', 'jpe');
Barry Mienydd671972010-10-04 16:33:58 +0200798
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200799 if (in_array($ext, $image_types) && @getimagesize($this->file_temp) === FALSE)
Derek Jonese9d723f2010-07-12 10:10:59 -0500800 {
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200801 return FALSE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500802 }
Barry Mienydd671972010-10-04 16:33:58 +0200803
Derek Jonese9d723f2010-07-12 10:10:59 -0500804 if ($ignore_mime === TRUE)
805 {
806 return TRUE;
807 }
Barry Mienydd671972010-10-04 16:33:58 +0200808
Derek Jonese9d723f2010-07-12 10:10:59 -0500809 $mime = $this->mimes_types($ext);
Barry Mienydd671972010-10-04 16:33:58 +0200810
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300811 if (is_array($mime) && in_array($this->file_type, $mime, TRUE))
Derek Jonese9d723f2010-07-12 10:10:59 -0500812 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300813 return TRUE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500814 }
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200815 elseif ($mime === $this->file_type)
Derek Jonese9d723f2010-07-12 10:10:59 -0500816 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300817 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000818 }
Barry Mienydd671972010-10-04 16:33:58 +0200819
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 return FALSE;
821 }
Barry Mienydd671972010-10-04 16:33:58 +0200822
Derek Allard2067d1a2008-11-13 22:59:24 +0000823 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200824
Derek Allard2067d1a2008-11-13 22:59:24 +0000825 /**
826 * Verify that the file is within the allowed size
827 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000828 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200829 */
Greg Aker58fdee82010-11-10 15:07:09 -0600830 public function is_allowed_filesize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000831 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100832 return ($this->max_size === 0 OR $this->max_size > $this->file_size);
Derek Allard2067d1a2008-11-13 22:59:24 +0000833 }
Barry Mienydd671972010-10-04 16:33:58 +0200834
Derek Allard2067d1a2008-11-13 22:59:24 +0000835 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200836
Derek Allard2067d1a2008-11-13 22:59:24 +0000837 /**
838 * Verify that the image is within the allowed width/height
839 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000840 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200841 */
Greg Aker58fdee82010-11-10 15:07:09 -0600842 public function is_allowed_dimensions()
Derek Allard2067d1a2008-11-13 22:59:24 +0000843 {
844 if ( ! $this->is_image())
845 {
846 return TRUE;
847 }
848
849 if (function_exists('getimagesize'))
850 {
851 $D = @getimagesize($this->file_temp);
852
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300853 if ($this->max_width > 0 && $D[0] > $this->max_width)
Derek Allard2067d1a2008-11-13 22:59:24 +0000854 {
855 return FALSE;
856 }
857
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300858 if ($this->max_height > 0 && $D[1] > $this->max_height)
Derek Allard2067d1a2008-11-13 22:59:24 +0000859 {
860 return FALSE;
861 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000862 }
863
864 return TRUE;
865 }
Barry Mienydd671972010-10-04 16:33:58 +0200866
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200868
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 /**
870 * Validate Upload Path
871 *
872 * Verifies that it is a valid upload path with proper permissions.
873 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200875 */
Greg Aker58fdee82010-11-10 15:07:09 -0600876 public function validate_upload_path()
Derek Allard2067d1a2008-11-13 22:59:24 +0000877 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100878 if ($this->upload_path === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 {
880 $this->set_error('upload_no_filepath');
881 return FALSE;
882 }
Barry Mienydd671972010-10-04 16:33:58 +0200883
Andrey Andreevc839d282012-06-07 14:35:27 +0300884 if (@realpath($this->upload_path) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300886 $this->upload_path = str_replace('\\', '/', realpath($this->upload_path));
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 }
888
889 if ( ! @is_dir($this->upload_path))
890 {
891 $this->set_error('upload_no_filepath');
892 return FALSE;
893 }
894
895 if ( ! is_really_writable($this->upload_path))
896 {
897 $this->set_error('upload_not_writable');
898 return FALSE;
899 }
900
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300901 $this->upload_path = preg_replace('/(.+?)\/*$/', '\\1/', $this->upload_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 return TRUE;
903 }
Barry Mienydd671972010-10-04 16:33:58 +0200904
Derek Allard2067d1a2008-11-13 22:59:24 +0000905 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200906
Derek Allard2067d1a2008-11-13 22:59:24 +0000907 /**
908 * Extract the file extension
909 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200910 * @param string $filename
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200912 */
Greg Aker58fdee82010-11-10 15:07:09 -0600913 public function get_extension($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000914 {
915 $x = explode('.', $filename);
Andrey Andreev46d53fb2012-05-11 13:42:24 +0300916 return (count($x) !== 1) ? '.'.end($x) : '';
Barry Mienydd671972010-10-04 16:33:58 +0200917 }
918
Derek Allard2067d1a2008-11-13 22:59:24 +0000919 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200920
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 /**
922 * Clean the file name for security
923 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200924 * @param string $filename
Derek Allard2067d1a2008-11-13 22:59:24 +0000925 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200926 */
Greg Aker58fdee82010-11-10 15:07:09 -0600927 public function clean_file_name($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000928 {
929 $bad = array(
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300930 '<!--', '-->',
931 "'", '"',
932 '<', '>',
933 '&', '$',
934 '=',
935 ';',
936 '?',
937 '/',
Andrey Andreev470805b2012-05-24 21:57:21 +0300938 '!',
939 '#',
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300940 '%20',
941 '%22',
942 '%3c', // <
943 '%253c', // <
944 '%3e', // >
945 '%0e', // >
946 '%28', // (
947 '%29', // )
948 '%2528', // (
949 '%26', // &
950 '%24', // $
951 '%3f', // ?
952 '%3b', // ;
953 '%3d' // =
954 );
Barry Mienydd671972010-10-04 16:33:58 +0200955
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300956 return stripslashes(str_replace($bad, '', $filename));
Derek Allard2067d1a2008-11-13 22:59:24 +0000957 }
958
959 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200960
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 /**
962 * Limit the File Name Length
963 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300964 * @param string $filename
965 * @param int $length
Derek Allard2067d1a2008-11-13 22:59:24 +0000966 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200967 */
Greg Aker58fdee82010-11-10 15:07:09 -0600968 public function limit_filename_length($filename, $length)
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 {
970 if (strlen($filename) < $length)
971 {
972 return $filename;
973 }
Barry Mienydd671972010-10-04 16:33:58 +0200974
Derek Allard2067d1a2008-11-13 22:59:24 +0000975 $ext = '';
976 if (strpos($filename, '.') !== FALSE)
977 {
978 $parts = explode('.', $filename);
979 $ext = '.'.array_pop($parts);
980 $filename = implode('.', $parts);
981 }
Barry Mienydd671972010-10-04 16:33:58 +0200982
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 return substr($filename, 0, ($length - strlen($ext))).$ext;
984 }
985
986 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200987
Derek Allard2067d1a2008-11-13 22:59:24 +0000988 /**
989 * Runs the file through the XSS clean function
990 *
991 * This prevents people from embedding malicious code in their files.
992 * I'm not sure that it won't negatively affect certain files in unexpected ways,
993 * but so far I haven't found that it causes trouble.
994 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000995 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200996 */
Greg Aker58fdee82010-11-10 15:07:09 -0600997 public function do_xss_clean()
Barry Mienydd671972010-10-04 16:33:58 +0200998 {
Derek Jonese9d723f2010-07-12 10:10:59 -0500999 $file = $this->file_temp;
Barry Mienydd671972010-10-04 16:33:58 +02001000
Andrey Andreev5036c9c2012-06-04 15:34:56 +03001001 if (filesize($file) == 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 {
1003 return FALSE;
1004 }
Barry Mienydd671972010-10-04 16:33:58 +02001005
Andrey Andreevc839d282012-06-07 14:35:27 +03001006 if (memory_get_usage() && ($memory_limit = ini_get('memory_limit')))
Greg Akerf82e51c2010-04-14 19:33:50 -05001007 {
Andrey Andreevc839d282012-06-07 14:35:27 +03001008 $memory_limit *= 1024 * 1024;
Barry Mienydd671972010-10-04 16:33:58 +02001009
Greg Akerc78a2592010-06-09 11:45:32 -05001010 // There was a bug/behavioural change in PHP 5.2, where numbers over one million get output
Andrey Andreev8e6f7a92012-03-26 20:13:00 +03001011 // into scientific notation. number_format() ensures this number is an integer
Greg Akerc78a2592010-06-09 11:45:32 -05001012 // http://bugs.php.net/bug.php?id=43053
Barry Mienydd671972010-10-04 16:33:58 +02001013
Andrey Andreevc839d282012-06-07 14:35:27 +03001014 $memory_limit = number_format(ceil(filesize($file) + $memory_limit), 0, '.', '');
Barry Mienydd671972010-10-04 16:33:58 +02001015
Andrey Andreevc839d282012-06-07 14:35:27 +03001016 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 -05001017 }
1018
1019 // If the file being uploaded is an image, then we should have no problem with XSS attacks (in theory), but
1020 // 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 +03001021 // using IE who looks at the image. It does this by inspecting the first 255 bytes of an image. To get around this
1022 // 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 +02001023 // 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 -05001024 // attempted XSS attack.
1025
1026 if (function_exists('getimagesize') && @getimagesize($file) !== FALSE)
1027 {
Barry Mienydd671972010-10-04 16:33:58 +02001028 if (($file = @fopen($file, 'rb')) === FALSE) // "b" to force binary
1029 {
Greg Akerf82e51c2010-04-14 19:33:50 -05001030 return FALSE; // Couldn't open the file, return FALSE
Barry Mienydd671972010-10-04 16:33:58 +02001031 }
Greg Akerf82e51c2010-04-14 19:33:50 -05001032
Barry Mienydd671972010-10-04 16:33:58 +02001033 $opening_bytes = fread($file, 256);
1034 fclose($file);
Greg Akerf82e51c2010-04-14 19:33:50 -05001035
1036 // These are known to throw IE into mime-type detection chaos
1037 // <a, <body, <head, <html, <img, <plaintext, <pre, <script, <table, <title
1038 // title is basically just in SVG, but we filter it anyhow
1039
Andrey Andreevc839d282012-06-07 14:35:27 +03001040 // if its an image or no "triggers" detected in the first 256 bytes - we're good
1041 return ! preg_match('/<(a|body|head|html|img|plaintext|pre|script|table|title)[\s>]/i', $opening_bytes);
Greg Akerf82e51c2010-04-14 19:33:50 -05001042 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001043
1044 if (($data = @file_get_contents($file)) === FALSE)
1045 {
1046 return FALSE;
1047 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001048
Greg Akerf82e51c2010-04-14 19:33:50 -05001049 $CI =& get_instance();
Greg Akerf82e51c2010-04-14 19:33:50 -05001050 return $CI->security->xss_clean($data, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001051 }
Barry Mienydd671972010-10-04 16:33:58 +02001052
Derek Allard2067d1a2008-11-13 22:59:24 +00001053 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001054
Derek Allard2067d1a2008-11-13 22:59:24 +00001055 /**
1056 * Set an error message
1057 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001058 * @param string $msg
Derek Allard2067d1a2008-11-13 22:59:24 +00001059 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001060 */
Greg Aker58fdee82010-11-10 15:07:09 -06001061 public function set_error($msg)
Derek Allard2067d1a2008-11-13 22:59:24 +00001062 {
Barry Mienydd671972010-10-04 16:33:58 +02001063 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +00001064 $CI->lang->load('upload');
Barry Mienydd671972010-10-04 16:33:58 +02001065
Derek Allard2067d1a2008-11-13 22:59:24 +00001066 if (is_array($msg))
1067 {
1068 foreach ($msg as $val)
1069 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001070 $msg = ($CI->lang->line($val) === FALSE) ? $val : $CI->lang->line($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001071 $this->error_msg[] = $msg;
1072 log_message('error', $msg);
Barry Mienydd671972010-10-04 16:33:58 +02001073 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001074 }
1075 else
1076 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001077 $msg = ($CI->lang->line($msg) === FALSE) ? $msg : $CI->lang->line($msg);
Derek Allard2067d1a2008-11-13 22:59:24 +00001078 $this->error_msg[] = $msg;
1079 log_message('error', $msg);
1080 }
1081 }
Barry Mienydd671972010-10-04 16:33:58 +02001082
Derek Allard2067d1a2008-11-13 22:59:24 +00001083 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001084
Derek Allard2067d1a2008-11-13 22:59:24 +00001085 /**
1086 * Display the error message
1087 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001088 * @param string $open
1089 * @param string $close
Derek Allard2067d1a2008-11-13 22:59:24 +00001090 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001091 */
Greg Aker58fdee82010-11-10 15:07:09 -06001092 public function display_errors($open = '<p>', $close = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +00001093 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +03001094 return (count($this->error_msg) > 0) ? $open.implode($close.$open, $this->error_msg).$close : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001095 }
Barry Mienydd671972010-10-04 16:33:58 +02001096
Derek Allard2067d1a2008-11-13 22:59:24 +00001097 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001098
Derek Allard2067d1a2008-11-13 22:59:24 +00001099 /**
1100 * List of Mime Types
1101 *
Andrey Andreev8e6f7a92012-03-26 20:13:00 +03001102 * This is a list of mime types. We use it to validate
Derek Allard2067d1a2008-11-13 22:59:24 +00001103 * the "allowed types" set by the developer
1104 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001105 * @param string $mime
Derek Allard2067d1a2008-11-13 22:59:24 +00001106 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001107 */
Greg Aker58fdee82010-11-10 15:07:09 -06001108 public function mimes_types($mime)
Derek Allard2067d1a2008-11-13 22:59:24 +00001109 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +03001110 return isset($this->mimes[$mime]) ? $this->mimes[$mime] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001111 }
1112
1113 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001114
Derek Allard2067d1a2008-11-13 22:59:24 +00001115 /**
1116 * Prep Filename
1117 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001118 * Prevents possible script execution from Apache's handling
1119 * of files' multiple extensions.
Derek Allard2067d1a2008-11-13 22:59:24 +00001120 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001121 * @link http://httpd.apache.org/docs/1.3/mod/mod_mime.html#multipleext
1122 *
1123 * @param string $filename
Derek Allard2067d1a2008-11-13 22:59:24 +00001124 * @return string
1125 */
Greg Aker58fdee82010-11-10 15:07:09 -06001126 protected function _prep_filename($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +00001127 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001128 if (strpos($filename, '.') === FALSE OR $this->allowed_types === '*')
Derek Allard2067d1a2008-11-13 22:59:24 +00001129 {
1130 return $filename;
1131 }
Derek Allard616dab82009-02-16 15:44:32 +00001132
Derek Allard2067d1a2008-11-13 22:59:24 +00001133 $parts = explode('.', $filename);
1134 $ext = array_pop($parts);
1135 $filename = array_shift($parts);
Derek Allard616dab82009-02-16 15:44:32 +00001136
Derek Allard2067d1a2008-11-13 22:59:24 +00001137 foreach ($parts as $part)
1138 {
Derek Jonese9d723f2010-07-12 10:10:59 -05001139 if ( ! in_array(strtolower($part), $this->allowed_types) OR $this->mimes_types(strtolower($part)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001140 {
1141 $filename .= '.'.$part.'_';
1142 }
1143 else
1144 {
1145 $filename .= '.'.$part;
1146 }
1147 }
Derek Allardd70b0642009-02-16 13:51:42 +00001148
Andrey Andreev8e6f7a92012-03-26 20:13:00 +03001149 return $filename.'.'.$ext;
Derek Allard2067d1a2008-11-13 22:59:24 +00001150 }
1151
1152 // --------------------------------------------------------------------
1153
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001154 /**
1155 * File MIME type
1156 *
1157 * Detects the (actual) MIME type of the uploaded file, if possible.
1158 * The input array is expected to be $_FILES[$field]
1159 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001160 * @param array $file
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001161 * @return void
1162 */
1163 protected function _file_mime_type($file)
1164 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001165 // We'll need this to validate the MIME info string (e.g. text/plain; charset=us-ascii)
Andrey Andreevf7aed122011-12-13 11:01:06 +02001166 $regexp = '/^([a-z\-]+\/[a-z0-9\-\.\+]+)(;\s.+)?$/';
Andrey Andreeva49e3812011-12-09 13:05:22 +02001167
1168 /* Fileinfo extension - most reliable method
1169 *
1170 * Unfortunately, prior to PHP 5.3 - it's only available as a PECL extension and the
1171 * more convenient FILEINFO_MIME_TYPE flag doesn't exist.
1172 */
1173 if (function_exists('finfo_file'))
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001174 {
Andrey Andreevd60e7002012-06-17 00:03:03 +03001175 $finfo = @finfo_open(FILEINFO_MIME);
Andrey Andreeva49e3812011-12-09 13:05:22 +02001176 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 +03001177 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001178 $mime = @finfo_file($finfo, $file['tmp_name']);
1179 finfo_close($finfo);
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001180
1181 /* According to the comments section of the PHP manual page,
1182 * it is possible that this function returns an empty string
Andrey Andreev2cec39f2011-09-24 22:59:37 +03001183 * for some files (e.g. if they don't exist in the magic MIME database)
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001184 */
Andrey Andreeva49e3812011-12-09 13:05:22 +02001185 if (is_string($mime) && preg_match($regexp, $mime, $matches))
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001186 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001187 $this->file_type = $matches[1];
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001188 return;
1189 }
1190 }
1191 }
1192
Andrey Andreeva49e3812011-12-09 13:05:22 +02001193 /* This is an ugly hack, but UNIX-type systems provide a "native" way to detect the file type,
1194 * which is still more secure than depending on the value of $_FILES[$field]['type'], and as it
1195 * was reported in issue #750 (https://github.com/EllisLab/CodeIgniter/issues/750) - it's better
1196 * than mime_content_type() as well, hence the attempts to try calling the command line with
1197 * three different functions.
Andrey Andreev6700b932011-09-24 14:25:33 +03001198 *
1199 * Notes:
Andrey Andreev59654312011-12-02 14:28:54 +02001200 * - the DIRECTORY_SEPARATOR comparison ensures that we're not on a Windows system
Andrey Andreeva49e3812011-12-09 13:05:22 +02001201 * - many system admins would disable the exec(), shell_exec(), popen() and similar functions
1202 * due to security concerns, hence the function_exists() checks
Andrey Andreev6700b932011-09-24 14:25:33 +03001203 */
Andrey Andreeva49e3812011-12-09 13:05:22 +02001204 if (DIRECTORY_SEPARATOR !== '\\')
Andrey Andreev6700b932011-09-24 14:25:33 +03001205 {
Andrey Andreevd60e7002012-06-17 00:03:03 +03001206 $cmd = function_exists('escapeshellarg')
1207 ? 'file --brief --mime '.escapeshellarg($file['tmp_name']).' 2>&1'
1208 : 'file --brief --mime '.$file['tmp_name'].' 2>&1';
Andrey Andreeva49e3812011-12-09 13:05:22 +02001209
Andrey Andreeve9d2dc82012-11-07 14:23:29 +02001210 if (function_usable('exec'))
Andrey Andreev6700b932011-09-24 14:25:33 +03001211 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001212 /* This might look confusing, as $mime is being populated with all of the output when set in the second parameter.
1213 * However, we only neeed the last line, which is the actual return value of exec(), and as such - it overwrites
1214 * anything that could already be set for $mime previously. This effectively makes the second parameter a dummy
1215 * value, which is only put to allow us to get the return status code.
1216 */
1217 $mime = @exec($cmd, $mime, $return_status);
1218 if ($return_status === 0 && is_string($mime) && preg_match($regexp, $mime, $matches))
1219 {
1220 $this->file_type = $matches[1];
1221 return;
1222 }
1223 }
1224
Andrey Andreeve9d2dc82012-11-07 14:23:29 +02001225 if ( (bool) @ini_get('safe_mode') === FALSE && function_usable('shell_exec'))
Andrey Andreeva49e3812011-12-09 13:05:22 +02001226 {
1227 $mime = @shell_exec($cmd);
1228 if (strlen($mime) > 0)
1229 {
1230 $mime = explode("\n", trim($mime));
1231 if (preg_match($regexp, $mime[(count($mime) - 1)], $matches))
1232 {
1233 $this->file_type = $matches[1];
1234 return;
1235 }
1236 }
1237 }
1238
Andrey Andreeve9d2dc82012-11-07 14:23:29 +02001239 if (function_usable('popen'))
Andrey Andreeva49e3812011-12-09 13:05:22 +02001240 {
1241 $proc = @popen($cmd, 'r');
1242 if (is_resource($proc))
1243 {
tubalmartin010f1f42012-03-03 22:24:31 +01001244 $mime = @fread($proc, 512);
Andrey Andreeva49e3812011-12-09 13:05:22 +02001245 @pclose($proc);
1246 if ($mime !== FALSE)
1247 {
1248 $mime = explode("\n", trim($mime));
1249 if (preg_match($regexp, $mime[(count($mime) - 1)], $matches))
1250 {
1251 $this->file_type = $matches[1];
1252 return;
1253 }
1254 }
1255 }
1256 }
1257 }
1258
1259 // Fall back to the deprecated mime_content_type(), if available (still better than $_FILES[$field]['type'])
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001260 if (function_exists('mime_content_type'))
1261 {
1262 $this->file_type = @mime_content_type($file['tmp_name']);
Andrey Andreeva49e3812011-12-09 13:05:22 +02001263 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 +03001264 {
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001265 return;
1266 }
1267 }
1268
1269 $this->file_type = $file['type'];
1270 }
1271
Derek Allard2067d1a2008-11-13 22:59:24 +00001272}
Derek Allard2067d1a2008-11-13 22:59:24 +00001273
1274/* End of file Upload.php */
Andrey Andreev56454792012-05-17 14:32:19 +03001275/* Location: ./system/libraries/Upload.php */