blob: b3e9f751543c4847da1eded4265183b02567f694 [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
297
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 foreach ($defaults as $key => $val)
299 {
300 if (isset($config[$key]))
301 {
302 $method = 'set_'.$key;
303 if (method_exists($this, $method))
304 {
305 $this->$method($config[$key]);
306 }
307 else
308 {
309 $this->$key = $config[$key];
Barry Mienydd671972010-10-04 16:33:58 +0200310 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 }
312 else
313 {
314 $this->$key = $val;
315 }
316 }
Barry Mienydd671972010-10-04 16:33:58 +0200317
Derek Jonese9d723f2010-07-12 10:10:59 -0500318 // if a file_name was provided in the config, use it instead of the user input
319 // supplied file name for all uploads until initialized again
320 $this->_file_name_override = $this->file_name;
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 }
Barry Mienydd671972010-10-04 16:33:58 +0200322
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200324
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 /**
326 * Perform the file upload
327 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200328 * @param string $field
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200330 */
Greg Aker58fdee82010-11-10 15:07:09 -0600331 public function do_upload($field = 'userfile')
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300333 // Is $_FILES[$field] set? If not, no reason to continue.
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 if ( ! isset($_FILES[$field]))
335 {
336 $this->set_error('upload_no_file_selected');
337 return FALSE;
338 }
Barry Mienydd671972010-10-04 16:33:58 +0200339
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 // Is the upload path valid?
341 if ( ! $this->validate_upload_path())
342 {
343 // errors will already be set by validate_upload_path() so just return FALSE
344 return FALSE;
345 }
346
347 // Was the file able to be uploaded? If not, determine the reason why.
348 if ( ! is_uploaded_file($_FILES[$field]['tmp_name']))
349 {
350 $error = ( ! isset($_FILES[$field]['error'])) ? 4 : $_FILES[$field]['error'];
351
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300352 switch ($error)
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 {
354 case 1: // UPLOAD_ERR_INI_SIZE
355 $this->set_error('upload_file_exceeds_limit');
356 break;
357 case 2: // UPLOAD_ERR_FORM_SIZE
358 $this->set_error('upload_file_exceeds_form_limit');
359 break;
360 case 3: // UPLOAD_ERR_PARTIAL
Barry Mienydd671972010-10-04 16:33:58 +0200361 $this->set_error('upload_file_partial');
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 break;
363 case 4: // UPLOAD_ERR_NO_FILE
Barry Mienydd671972010-10-04 16:33:58 +0200364 $this->set_error('upload_no_file_selected');
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 break;
366 case 6: // UPLOAD_ERR_NO_TMP_DIR
367 $this->set_error('upload_no_temp_directory');
368 break;
369 case 7: // UPLOAD_ERR_CANT_WRITE
370 $this->set_error('upload_unable_to_write_file');
371 break;
372 case 8: // UPLOAD_ERR_EXTENSION
373 $this->set_error('upload_stopped_by_extension');
374 break;
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300375 default:
376 $this->set_error('upload_no_file_selected');
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 break;
378 }
379
380 return FALSE;
381 }
382
383 // Set the uploaded data as class variables
Barry Mienydd671972010-10-04 16:33:58 +0200384 $this->file_temp = $_FILES[$field]['tmp_name'];
385 $this->file_size = $_FILES[$field]['size'];
Andrey Andreevd60e7002012-06-17 00:03:03 +0300386
387 // Skip MIME type detection?
388 if ($this->detect_mime !== FALSE)
389 {
390 $this->_file_mime_type($_FILES[$field]);
391 }
392
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300393 $this->file_type = preg_replace('/^(.+?);.*$/', '\\1', $this->file_type);
Derek Jones616fb022010-04-22 16:52:18 -0500394 $this->file_type = strtolower(trim(stripslashes($this->file_type), '"'));
Derek Jonese9d723f2010-07-12 10:10:59 -0500395 $this->file_name = $this->_prep_filename($_FILES[$field]['name']);
396 $this->file_ext = $this->get_extension($this->file_name);
397 $this->client_name = $this->file_name;
Barry Mienydd671972010-10-04 16:33:58 +0200398
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 // Is the file type allowed to be uploaded?
400 if ( ! $this->is_allowed_filetype())
401 {
402 $this->set_error('upload_invalid_filetype');
403 return FALSE;
404 }
405
Derek Jonese9d723f2010-07-12 10:10:59 -0500406 // if we're overriding, let's now make sure the new name and type is allowed
Alex Bilbied261b1e2012-06-02 11:12:16 +0100407 if ($this->_file_name_override !== '')
Derek Jonese9d723f2010-07-12 10:10:59 -0500408 {
409 $this->file_name = $this->_prep_filename($this->_file_name_override);
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000410
411 // If no extension was provided in the file_name config item, use the uploaded one
Pascal Kriete14287f32011-02-14 13:39:34 -0500412 if (strpos($this->_file_name_override, '.') === FALSE)
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000413 {
414 $this->file_name .= $this->file_ext;
415 }
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000416 else
417 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300418 // An extension was provided, lets have it!
419 $this->file_ext = $this->get_extension($this->_file_name_override);
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000420 }
Derek Jonese9d723f2010-07-12 10:10:59 -0500421
422 if ( ! $this->is_allowed_filetype(TRUE))
423 {
424 $this->set_error('upload_invalid_filetype');
Barry Mienydd671972010-10-04 16:33:58 +0200425 return FALSE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500426 }
427 }
Barry Mienydd671972010-10-04 16:33:58 +0200428
Derek Jonese9d723f2010-07-12 10:10:59 -0500429 // Convert the file size to kilobytes
430 if ($this->file_size > 0)
431 {
432 $this->file_size = round($this->file_size/1024, 2);
433 }
434
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 // Is the file size within the allowed maximum?
436 if ( ! $this->is_allowed_filesize())
437 {
438 $this->set_error('upload_invalid_filesize');
439 return FALSE;
440 }
441
442 // Are the image dimensions within the allowed size?
443 // Note: This can fail if the server has an open_basdir restriction.
444 if ( ! $this->is_allowed_dimensions())
445 {
446 $this->set_error('upload_invalid_dimensions');
447 return FALSE;
448 }
449
450 // Sanitize the file name for security
451 $this->file_name = $this->clean_file_name($this->file_name);
Barry Mienydd671972010-10-04 16:33:58 +0200452
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 // Truncate the file name if it's too long
454 if ($this->max_filename > 0)
455 {
456 $this->file_name = $this->limit_filename_length($this->file_name, $this->max_filename);
457 }
458
459 // Remove white spaces in the name
Alex Bilbied261b1e2012-06-02 11:12:16 +0100460 if ($this->remove_spaces === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300462 $this->file_name = preg_replace('/\s+/', '_', $this->file_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 }
464
465 /*
466 * Validate the file name
467 * This function appends an number onto the end of
468 * the file if one with the same name already exists.
469 * If it returns false there was a problem.
470 */
471 $this->orig_name = $this->file_name;
472
Alex Bilbied261b1e2012-06-02 11:12:16 +0100473 if ($this->overwrite === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 {
475 $this->file_name = $this->set_filename($this->upload_path, $this->file_name);
Barry Mienydd671972010-10-04 16:33:58 +0200476
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 if ($this->file_name === FALSE)
478 {
479 return FALSE;
480 }
481 }
482
483 /*
Derek Jonese9d723f2010-07-12 10:10:59 -0500484 * Run the file through the XSS hacking filter
485 * This helps prevent malicious code from being
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300486 * embedded within a file. Scripts can easily
Derek Jonese9d723f2010-07-12 10:10:59 -0500487 * be disguised as images or other file types.
488 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300489 if ($this->xss_clean && $this->do_xss_clean() === FALSE)
Derek Jonese9d723f2010-07-12 10:10:59 -0500490 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300491 $this->set_error('upload_unable_to_write_file');
492 return FALSE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500493 }
494
495 /*
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 * Move the file to the final destination
497 * To deal with different server configurations
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300498 * we'll attempt to use copy() first. If that fails
499 * we'll use move_uploaded_file(). One of the two should
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 * reliably work in most environments
501 */
502 if ( ! @copy($this->file_temp, $this->upload_path.$this->file_name))
503 {
504 if ( ! @move_uploaded_file($this->file_temp, $this->upload_path.$this->file_name))
505 {
Barry Mienydd671972010-10-04 16:33:58 +0200506 $this->set_error('upload_destination_error');
507 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 }
509 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000510
511 /*
512 * Set the finalized image dimensions
513 * This sets the image width/height (assuming the
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300514 * file was an image). We use this information
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 * in the "data" function.
516 */
517 $this->set_image_properties($this->upload_path.$this->file_name);
518
519 return TRUE;
520 }
Barry Mienydd671972010-10-04 16:33:58 +0200521
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200523
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 /**
525 * Finalized Data Array
Barry Mienydd671972010-10-04 16:33:58 +0200526 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 * Returns an associative array containing all of the information
528 * related to the upload, allowing the developer easy access in one array.
529 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200530 * @param string $index
Michiel Vugteveen0ee287e2012-06-11 10:38:14 +0200531 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200532 */
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200533 public function data($index = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 {
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200535 $data = array(
Michiel Vugteveen3a7fb042012-06-11 09:29:16 +0200536 'file_name' => $this->file_name,
537 'file_type' => $this->file_type,
538 'file_path' => $this->upload_path,
539 'full_path' => $this->upload_path.$this->file_name,
540 'raw_name' => str_replace($this->file_ext, '', $this->file_name),
541 'orig_name' => $this->orig_name,
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300542 'client_name' => $this->client_name,
Michiel Vugteveen3a7fb042012-06-11 09:29:16 +0200543 'file_ext' => $this->file_ext,
544 'file_size' => $this->file_size,
545 'is_image' => $this->is_image(),
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300546 'image_width' => $this->image_width,
547 'image_height' => $this->image_height,
548 'image_type' => $this->image_type,
549 'image_size_str' => $this->image_size_str,
550 );
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200551
Michiel Vugteveen46a04292012-06-11 10:37:52 +0200552 if ( ! empty($index))
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200553 {
Michiel Vugteveen46a04292012-06-11 10:37:52 +0200554 return isset($data[$index]) ? $data[$index] : NULL;
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200555 }
556
Michiel Vugteveen46a04292012-06-11 10:37:52 +0200557 return $data;
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 }
Barry Mienydd671972010-10-04 16:33:58 +0200559
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200561
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 /**
563 * Set Upload Path
564 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200565 * @param string $path
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200567 */
Greg Aker58fdee82010-11-10 15:07:09 -0600568 public function set_upload_path($path)
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 {
570 // Make sure it has a trailing slash
571 $this->upload_path = rtrim($path, '/').'/';
572 }
Barry Mienydd671972010-10-04 16:33:58 +0200573
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200575
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 /**
577 * Set the file name
578 *
579 * This function takes a filename/path as input and looks for the
580 * existence of a file with the same name. If found, it will append a
581 * number to the end of the filename to avoid overwriting a pre-existing file.
582 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200583 * @param string $path
584 * @param string $filename
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200586 */
Greg Aker58fdee82010-11-10 15:07:09 -0600587 public function set_filename($path, $filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100589 if ($this->encrypt_name === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200590 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 mt_srand();
Barry Mienydd671972010-10-04 16:33:58 +0200592 $filename = md5(uniqid(mt_rand())).$this->file_ext;
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 }
Barry Mienydd671972010-10-04 16:33:58 +0200594
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 if ( ! file_exists($path.$filename))
596 {
597 return $filename;
598 }
Barry Mienydd671972010-10-04 16:33:58 +0200599
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 $filename = str_replace($this->file_ext, '', $filename);
Barry Mienydd671972010-10-04 16:33:58 +0200601
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 $new_filename = '';
Adam Jackettccbbea12011-08-21 16:19:11 -0400603 for ($i = 1; $i < $this->max_filename_increment; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200604 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 if ( ! file_exists($path.$filename.$i.$this->file_ext))
606 {
607 $new_filename = $filename.$i.$this->file_ext;
608 break;
609 }
610 }
611
Alex Bilbied261b1e2012-06-02 11:12:16 +0100612 if ($new_filename === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 {
614 $this->set_error('upload_bad_filename');
615 return FALSE;
616 }
617 else
618 {
619 return $new_filename;
620 }
621 }
Barry Mienydd671972010-10-04 16:33:58 +0200622
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200624
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 /**
626 * Set Maximum File Size
627 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200628 * @param int $n
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200630 */
Greg Aker58fdee82010-11-10 15:07:09 -0600631 public function set_max_filesize($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300633 $this->max_size = ((int) $n < 0) ? 0 : (int) $n;
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 }
Barry Mienydd671972010-10-04 16:33:58 +0200635
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200637
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 /**
639 * Set Maximum File Name Length
640 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200641 * @param int $n
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200643 */
Greg Aker58fdee82010-11-10 15:07:09 -0600644 public function set_max_filename($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000645 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300646 $this->max_filename = ((int) $n < 0) ? 0 : (int) $n;
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 }
648
649 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200650
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 /**
652 * Set Maximum Image Width
653 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200654 * @param int $n
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200656 */
Greg Aker58fdee82010-11-10 15:07:09 -0600657 public function set_max_width($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000658 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300659 $this->max_width = ((int) $n < 0) ? 0 : (int) $n;
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 }
Barry Mienydd671972010-10-04 16:33:58 +0200661
Derek Allard2067d1a2008-11-13 22:59:24 +0000662 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200663
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 /**
665 * Set Maximum Image Height
666 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200667 * @param int $n
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200669 */
Greg Aker58fdee82010-11-10 15:07:09 -0600670 public function set_max_height($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300672 $this->max_height = ((int) $n < 0) ? 0 : (int) $n;
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 }
Barry Mienydd671972010-10-04 16:33:58 +0200674
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200676
Derek Allard2067d1a2008-11-13 22:59:24 +0000677 /**
678 * Set Allowed File Types
679 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200680 * @param string $types
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200682 */
Greg Aker58fdee82010-11-10 15:07:09 -0600683 public function set_allowed_types($types)
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300685 if ( ! is_array($types) && $types === '*')
Derek Jonese12f64e2010-03-02 22:55:08 -0600686 {
687 $this->allowed_types = '*';
688 return;
689 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 $this->allowed_types = explode('|', $types);
691 }
Barry Mienydd671972010-10-04 16:33:58 +0200692
Derek Allard2067d1a2008-11-13 22:59:24 +0000693 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200694
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 /**
696 * Set Image Properties
697 *
698 * Uses GD to determine the width/height/type of image
699 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200700 * @param string $path
Derek Allard2067d1a2008-11-13 22:59:24 +0000701 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200702 */
Greg Aker58fdee82010-11-10 15:07:09 -0600703 public function set_image_properties($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 {
705 if ( ! $this->is_image())
706 {
707 return;
708 }
709
710 if (function_exists('getimagesize'))
711 {
712 if (FALSE !== ($D = @getimagesize($path)))
Barry Mienydd671972010-10-04 16:33:58 +0200713 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000714 $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
715
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300716 $this->image_width = $D[0];
717 $this->image_height = $D[1];
718 $this->image_type = isset($types[$D[2]]) ? $types[$D[2]] : 'unknown';
719 $this->image_size_str = $D[3]; // string containing height and width
Derek Allard2067d1a2008-11-13 22:59:24 +0000720 }
721 }
722 }
Barry Mienydd671972010-10-04 16:33:58 +0200723
Derek Allard2067d1a2008-11-13 22:59:24 +0000724 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200725
Derek Allard2067d1a2008-11-13 22:59:24 +0000726 /**
727 * Set XSS Clean
728 *
729 * Enables the XSS flag so that the file that was uploaded
730 * will be run through the XSS filter.
731 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200732 * @param bool $flag
Derek Allard2067d1a2008-11-13 22:59:24 +0000733 * @return void
734 */
Greg Aker58fdee82010-11-10 15:07:09 -0600735 public function set_xss_clean($flag = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100737 $this->xss_clean = ($flag === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000738 }
Barry Mienydd671972010-10-04 16:33:58 +0200739
Derek Allard2067d1a2008-11-13 22:59:24 +0000740 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200741
Derek Allard2067d1a2008-11-13 22:59:24 +0000742 /**
743 * Validate the image
744 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000745 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200746 */
Greg Aker58fdee82010-11-10 15:07:09 -0600747 public function is_image()
Derek Allard2067d1a2008-11-13 22:59:24 +0000748 {
749 // IE will sometimes return odd mime-types during upload, so here we just standardize all
750 // jpegs or pngs to the same file type.
751
Derek Jones4b9c6292011-07-01 17:40:48 -0500752 $png_mimes = array('image/x-png');
Derek Allard2067d1a2008-11-13 22:59:24 +0000753 $jpeg_mimes = array('image/jpg', 'image/jpe', 'image/jpeg', 'image/pjpeg');
Barry Mienydd671972010-10-04 16:33:58 +0200754
Derek Allard2067d1a2008-11-13 22:59:24 +0000755 if (in_array($this->file_type, $png_mimes))
756 {
757 $this->file_type = 'image/png';
758 }
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300759 elseif (in_array($this->file_type, $jpeg_mimes))
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 {
761 $this->file_type = 'image/jpeg';
762 }
763
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300764 $img_mimes = array('image/gif', 'image/jpeg', 'image/png');
Derek Allard2067d1a2008-11-13 22:59:24 +0000765
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300766 return in_array($this->file_type, $img_mimes, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000767 }
Barry Mienydd671972010-10-04 16:33:58 +0200768
Derek Allard2067d1a2008-11-13 22:59:24 +0000769 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200770
Derek Allard2067d1a2008-11-13 22:59:24 +0000771 /**
772 * Verify that the filetype is allowed
773 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200774 * @param bool $ignore_mime
Derek Allard2067d1a2008-11-13 22:59:24 +0000775 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200776 */
Greg Aker58fdee82010-11-10 15:07:09 -0600777 public function is_allowed_filetype($ignore_mime = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000778 {
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200779 if ($this->allowed_types === '*')
Derek Jonese12f64e2010-03-02 22:55:08 -0600780 {
781 return TRUE;
782 }
Barry Mienydd671972010-10-04 16:33:58 +0200783
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200784 if ( ! is_array($this->allowed_types) OR count($this->allowed_types) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000785 {
786 $this->set_error('upload_no_file_types');
787 return FALSE;
788 }
Barry Mienydd671972010-10-04 16:33:58 +0200789
Derek Jonese9d723f2010-07-12 10:10:59 -0500790 $ext = strtolower(ltrim($this->file_ext, '.'));
Barry Mienydd671972010-10-04 16:33:58 +0200791
Derek Jonese9d723f2010-07-12 10:10:59 -0500792 if ( ! in_array($ext, $this->allowed_types))
Derek Allard2067d1a2008-11-13 22:59:24 +0000793 {
Derek Jonese9d723f2010-07-12 10:10:59 -0500794 return FALSE;
795 }
Derek Jonesafa282f2009-02-10 17:11:52 +0000796
Barry Mienydd671972010-10-04 16:33:58 +0200797 // Images get some additional checks
Derek Jonese9d723f2010-07-12 10:10:59 -0500798 $image_types = array('gif', 'jpg', 'jpeg', 'png', 'jpe');
Barry Mienydd671972010-10-04 16:33:58 +0200799
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200800 if (in_array($ext, $image_types) && @getimagesize($this->file_temp) === FALSE)
Derek Jonese9d723f2010-07-12 10:10:59 -0500801 {
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200802 return FALSE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500803 }
Barry Mienydd671972010-10-04 16:33:58 +0200804
Derek Jonese9d723f2010-07-12 10:10:59 -0500805 if ($ignore_mime === TRUE)
806 {
807 return TRUE;
808 }
Barry Mienydd671972010-10-04 16:33:58 +0200809
Derek Jonese9d723f2010-07-12 10:10:59 -0500810 $mime = $this->mimes_types($ext);
Barry Mienydd671972010-10-04 16:33:58 +0200811
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300812 if (is_array($mime) && in_array($this->file_type, $mime, TRUE))
Derek Jonese9d723f2010-07-12 10:10:59 -0500813 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300814 return TRUE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500815 }
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200816 elseif ($mime === $this->file_type)
Derek Jonese9d723f2010-07-12 10:10:59 -0500817 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300818 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000819 }
Barry Mienydd671972010-10-04 16:33:58 +0200820
Derek Allard2067d1a2008-11-13 22:59:24 +0000821 return FALSE;
822 }
Barry Mienydd671972010-10-04 16:33:58 +0200823
Derek Allard2067d1a2008-11-13 22:59:24 +0000824 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200825
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 /**
827 * Verify that the file is within the allowed size
828 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000829 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200830 */
Greg Aker58fdee82010-11-10 15:07:09 -0600831 public function is_allowed_filesize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000832 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100833 return ($this->max_size === 0 OR $this->max_size > $this->file_size);
Derek Allard2067d1a2008-11-13 22:59:24 +0000834 }
Barry Mienydd671972010-10-04 16:33:58 +0200835
Derek Allard2067d1a2008-11-13 22:59:24 +0000836 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200837
Derek Allard2067d1a2008-11-13 22:59:24 +0000838 /**
839 * Verify that the image is within the allowed width/height
840 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000841 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200842 */
Greg Aker58fdee82010-11-10 15:07:09 -0600843 public function is_allowed_dimensions()
Derek Allard2067d1a2008-11-13 22:59:24 +0000844 {
845 if ( ! $this->is_image())
846 {
847 return TRUE;
848 }
849
850 if (function_exists('getimagesize'))
851 {
852 $D = @getimagesize($this->file_temp);
853
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300854 if ($this->max_width > 0 && $D[0] > $this->max_width)
Derek Allard2067d1a2008-11-13 22:59:24 +0000855 {
856 return FALSE;
857 }
858
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300859 if ($this->max_height > 0 && $D[1] > $this->max_height)
Derek Allard2067d1a2008-11-13 22:59:24 +0000860 {
861 return FALSE;
862 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 }
864
865 return TRUE;
866 }
Barry Mienydd671972010-10-04 16:33:58 +0200867
Derek Allard2067d1a2008-11-13 22:59:24 +0000868 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200869
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 /**
871 * Validate Upload Path
872 *
873 * Verifies that it is a valid upload path with proper permissions.
874 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000875 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200876 */
Greg Aker58fdee82010-11-10 15:07:09 -0600877 public function validate_upload_path()
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100879 if ($this->upload_path === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 {
881 $this->set_error('upload_no_filepath');
882 return FALSE;
883 }
Barry Mienydd671972010-10-04 16:33:58 +0200884
Andrey Andreevc839d282012-06-07 14:35:27 +0300885 if (@realpath($this->upload_path) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300887 $this->upload_path = str_replace('\\', '/', realpath($this->upload_path));
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 }
889
890 if ( ! @is_dir($this->upload_path))
891 {
892 $this->set_error('upload_no_filepath');
893 return FALSE;
894 }
895
896 if ( ! is_really_writable($this->upload_path))
897 {
898 $this->set_error('upload_not_writable');
899 return FALSE;
900 }
901
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300902 $this->upload_path = preg_replace('/(.+?)\/*$/', '\\1/', $this->upload_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000903 return TRUE;
904 }
Barry Mienydd671972010-10-04 16:33:58 +0200905
Derek Allard2067d1a2008-11-13 22:59:24 +0000906 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200907
Derek Allard2067d1a2008-11-13 22:59:24 +0000908 /**
909 * Extract the file extension
910 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200911 * @param string $filename
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200913 */
Greg Aker58fdee82010-11-10 15:07:09 -0600914 public function get_extension($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000915 {
916 $x = explode('.', $filename);
Andrey Andreev46d53fb2012-05-11 13:42:24 +0300917 return (count($x) !== 1) ? '.'.end($x) : '';
Barry Mienydd671972010-10-04 16:33:58 +0200918 }
919
Derek Allard2067d1a2008-11-13 22:59:24 +0000920 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200921
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 /**
923 * Clean the file name for security
924 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200925 * @param string $filename
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200927 */
Greg Aker58fdee82010-11-10 15:07:09 -0600928 public function clean_file_name($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 {
930 $bad = array(
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300931 '<!--', '-->',
932 "'", '"',
933 '<', '>',
934 '&', '$',
935 '=',
936 ';',
937 '?',
938 '/',
Andrey Andreev470805b2012-05-24 21:57:21 +0300939 '!',
940 '#',
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300941 '%20',
942 '%22',
943 '%3c', // <
944 '%253c', // <
945 '%3e', // >
946 '%0e', // >
947 '%28', // (
948 '%29', // )
949 '%2528', // (
950 '%26', // &
951 '%24', // $
952 '%3f', // ?
953 '%3b', // ;
954 '%3d' // =
955 );
Barry Mienydd671972010-10-04 16:33:58 +0200956
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300957 return stripslashes(str_replace($bad, '', $filename));
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 }
959
960 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200961
Derek Allard2067d1a2008-11-13 22:59:24 +0000962 /**
963 * Limit the File Name Length
964 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300965 * @param string $filename
966 * @param int $length
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200968 */
Greg Aker58fdee82010-11-10 15:07:09 -0600969 public function limit_filename_length($filename, $length)
Derek Allard2067d1a2008-11-13 22:59:24 +0000970 {
971 if (strlen($filename) < $length)
972 {
973 return $filename;
974 }
Barry Mienydd671972010-10-04 16:33:58 +0200975
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 $ext = '';
977 if (strpos($filename, '.') !== FALSE)
978 {
979 $parts = explode('.', $filename);
980 $ext = '.'.array_pop($parts);
981 $filename = implode('.', $parts);
982 }
Barry Mienydd671972010-10-04 16:33:58 +0200983
Derek Allard2067d1a2008-11-13 22:59:24 +0000984 return substr($filename, 0, ($length - strlen($ext))).$ext;
985 }
986
987 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200988
Derek Allard2067d1a2008-11-13 22:59:24 +0000989 /**
990 * Runs the file through the XSS clean function
991 *
992 * This prevents people from embedding malicious code in their files.
993 * I'm not sure that it won't negatively affect certain files in unexpected ways,
994 * but so far I haven't found that it causes trouble.
995 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200997 */
Greg Aker58fdee82010-11-10 15:07:09 -0600998 public function do_xss_clean()
Barry Mienydd671972010-10-04 16:33:58 +0200999 {
Derek Jonese9d723f2010-07-12 10:10:59 -05001000 $file = $this->file_temp;
Barry Mienydd671972010-10-04 16:33:58 +02001001
Andrey Andreev5036c9c2012-06-04 15:34:56 +03001002 if (filesize($file) == 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001003 {
1004 return FALSE;
1005 }
Barry Mienydd671972010-10-04 16:33:58 +02001006
Andrey Andreevc839d282012-06-07 14:35:27 +03001007 if (memory_get_usage() && ($memory_limit = ini_get('memory_limit')))
Greg Akerf82e51c2010-04-14 19:33:50 -05001008 {
Andrey Andreevc839d282012-06-07 14:35:27 +03001009 $memory_limit *= 1024 * 1024;
Barry Mienydd671972010-10-04 16:33:58 +02001010
Greg Akerc78a2592010-06-09 11:45:32 -05001011 // There was a bug/behavioural change in PHP 5.2, where numbers over one million get output
Andrey Andreev8e6f7a92012-03-26 20:13:00 +03001012 // into scientific notation. number_format() ensures this number is an integer
Greg Akerc78a2592010-06-09 11:45:32 -05001013 // http://bugs.php.net/bug.php?id=43053
Barry Mienydd671972010-10-04 16:33:58 +02001014
Andrey Andreevc839d282012-06-07 14:35:27 +03001015 $memory_limit = number_format(ceil(filesize($file) + $memory_limit), 0, '.', '');
Barry Mienydd671972010-10-04 16:33:58 +02001016
Andrey Andreevc839d282012-06-07 14:35:27 +03001017 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 -05001018 }
1019
1020 // If the file being uploaded is an image, then we should have no problem with XSS attacks (in theory), but
1021 // 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 +03001022 // using IE who looks at the image. It does this by inspecting the first 255 bytes of an image. To get around this
1023 // 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 +02001024 // 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 -05001025 // attempted XSS attack.
1026
1027 if (function_exists('getimagesize') && @getimagesize($file) !== FALSE)
1028 {
Barry Mienydd671972010-10-04 16:33:58 +02001029 if (($file = @fopen($file, 'rb')) === FALSE) // "b" to force binary
1030 {
Greg Akerf82e51c2010-04-14 19:33:50 -05001031 return FALSE; // Couldn't open the file, return FALSE
Barry Mienydd671972010-10-04 16:33:58 +02001032 }
Greg Akerf82e51c2010-04-14 19:33:50 -05001033
Barry Mienydd671972010-10-04 16:33:58 +02001034 $opening_bytes = fread($file, 256);
1035 fclose($file);
Greg Akerf82e51c2010-04-14 19:33:50 -05001036
1037 // These are known to throw IE into mime-type detection chaos
1038 // <a, <body, <head, <html, <img, <plaintext, <pre, <script, <table, <title
1039 // title is basically just in SVG, but we filter it anyhow
1040
Andrey Andreevc839d282012-06-07 14:35:27 +03001041 // if its an image or no "triggers" detected in the first 256 bytes - we're good
1042 return ! preg_match('/<(a|body|head|html|img|plaintext|pre|script|table|title)[\s>]/i', $opening_bytes);
Greg Akerf82e51c2010-04-14 19:33:50 -05001043 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001044
1045 if (($data = @file_get_contents($file)) === FALSE)
1046 {
1047 return FALSE;
1048 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001049
Greg Akerf82e51c2010-04-14 19:33:50 -05001050 $CI =& get_instance();
Greg Akerf82e51c2010-04-14 19:33:50 -05001051 return $CI->security->xss_clean($data, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001052 }
Barry Mienydd671972010-10-04 16:33:58 +02001053
Derek Allard2067d1a2008-11-13 22:59:24 +00001054 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001055
Derek Allard2067d1a2008-11-13 22:59:24 +00001056 /**
1057 * Set an error message
1058 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001059 * @param string $msg
Derek Allard2067d1a2008-11-13 22:59:24 +00001060 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001061 */
Greg Aker58fdee82010-11-10 15:07:09 -06001062 public function set_error($msg)
Derek Allard2067d1a2008-11-13 22:59:24 +00001063 {
Barry Mienydd671972010-10-04 16:33:58 +02001064 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +00001065 $CI->lang->load('upload');
Barry Mienydd671972010-10-04 16:33:58 +02001066
Derek Allard2067d1a2008-11-13 22:59:24 +00001067 if (is_array($msg))
1068 {
1069 foreach ($msg as $val)
1070 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001071 $msg = ($CI->lang->line($val) === FALSE) ? $val : $CI->lang->line($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001072 $this->error_msg[] = $msg;
1073 log_message('error', $msg);
Barry Mienydd671972010-10-04 16:33:58 +02001074 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001075 }
1076 else
1077 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001078 $msg = ($CI->lang->line($msg) === FALSE) ? $msg : $CI->lang->line($msg);
Derek Allard2067d1a2008-11-13 22:59:24 +00001079 $this->error_msg[] = $msg;
1080 log_message('error', $msg);
1081 }
1082 }
Barry Mienydd671972010-10-04 16:33:58 +02001083
Derek Allard2067d1a2008-11-13 22:59:24 +00001084 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001085
Derek Allard2067d1a2008-11-13 22:59:24 +00001086 /**
1087 * Display the error message
1088 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001089 * @param string $open
1090 * @param string $close
Derek Allard2067d1a2008-11-13 22:59:24 +00001091 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001092 */
Greg Aker58fdee82010-11-10 15:07:09 -06001093 public function display_errors($open = '<p>', $close = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +00001094 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +03001095 return (count($this->error_msg) > 0) ? $open.implode($close.$open, $this->error_msg).$close : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001096 }
Barry Mienydd671972010-10-04 16:33:58 +02001097
Derek Allard2067d1a2008-11-13 22:59:24 +00001098 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001099
Derek Allard2067d1a2008-11-13 22:59:24 +00001100 /**
1101 * List of Mime Types
1102 *
Andrey Andreev8e6f7a92012-03-26 20:13:00 +03001103 * This is a list of mime types. We use it to validate
Derek Allard2067d1a2008-11-13 22:59:24 +00001104 * the "allowed types" set by the developer
1105 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001106 * @param string $mime
Derek Allard2067d1a2008-11-13 22:59:24 +00001107 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001108 */
Greg Aker58fdee82010-11-10 15:07:09 -06001109 public function mimes_types($mime)
Derek Allard2067d1a2008-11-13 22:59:24 +00001110 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +03001111 return isset($this->mimes[$mime]) ? $this->mimes[$mime] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001112 }
1113
1114 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001115
Derek Allard2067d1a2008-11-13 22:59:24 +00001116 /**
1117 * Prep Filename
1118 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001119 * Prevents possible script execution from Apache's handling
1120 * of files' multiple extensions.
Derek Allard2067d1a2008-11-13 22:59:24 +00001121 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001122 * @link http://httpd.apache.org/docs/1.3/mod/mod_mime.html#multipleext
1123 *
1124 * @param string $filename
Derek Allard2067d1a2008-11-13 22:59:24 +00001125 * @return string
1126 */
Greg Aker58fdee82010-11-10 15:07:09 -06001127 protected function _prep_filename($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +00001128 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001129 if (strpos($filename, '.') === FALSE OR $this->allowed_types === '*')
Derek Allard2067d1a2008-11-13 22:59:24 +00001130 {
1131 return $filename;
1132 }
Derek Allard616dab82009-02-16 15:44:32 +00001133
Derek Allard2067d1a2008-11-13 22:59:24 +00001134 $parts = explode('.', $filename);
1135 $ext = array_pop($parts);
1136 $filename = array_shift($parts);
Derek Allard616dab82009-02-16 15:44:32 +00001137
Derek Allard2067d1a2008-11-13 22:59:24 +00001138 foreach ($parts as $part)
1139 {
Derek Jonese9d723f2010-07-12 10:10:59 -05001140 if ( ! in_array(strtolower($part), $this->allowed_types) OR $this->mimes_types(strtolower($part)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001141 {
1142 $filename .= '.'.$part.'_';
1143 }
1144 else
1145 {
1146 $filename .= '.'.$part;
1147 }
1148 }
Derek Allardd70b0642009-02-16 13:51:42 +00001149
Andrey Andreev8e6f7a92012-03-26 20:13:00 +03001150 return $filename.'.'.$ext;
Derek Allard2067d1a2008-11-13 22:59:24 +00001151 }
1152
1153 // --------------------------------------------------------------------
1154
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001155 /**
1156 * File MIME type
1157 *
1158 * Detects the (actual) MIME type of the uploaded file, if possible.
1159 * The input array is expected to be $_FILES[$field]
1160 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001161 * @param array $file
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001162 * @return void
1163 */
1164 protected function _file_mime_type($file)
1165 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001166 // We'll need this to validate the MIME info string (e.g. text/plain; charset=us-ascii)
Andrey Andreevf7aed122011-12-13 11:01:06 +02001167 $regexp = '/^([a-z\-]+\/[a-z0-9\-\.\+]+)(;\s.+)?$/';
Andrey Andreeva49e3812011-12-09 13:05:22 +02001168
1169 /* Fileinfo extension - most reliable method
1170 *
1171 * Unfortunately, prior to PHP 5.3 - it's only available as a PECL extension and the
1172 * more convenient FILEINFO_MIME_TYPE flag doesn't exist.
1173 */
1174 if (function_exists('finfo_file'))
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001175 {
Andrey Andreevd60e7002012-06-17 00:03:03 +03001176 $finfo = @finfo_open(FILEINFO_MIME);
Andrey Andreeva49e3812011-12-09 13:05:22 +02001177 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 +03001178 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001179 $mime = @finfo_file($finfo, $file['tmp_name']);
1180 finfo_close($finfo);
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001181
1182 /* According to the comments section of the PHP manual page,
1183 * it is possible that this function returns an empty string
Andrey Andreev2cec39f2011-09-24 22:59:37 +03001184 * for some files (e.g. if they don't exist in the magic MIME database)
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001185 */
Andrey Andreeva49e3812011-12-09 13:05:22 +02001186 if (is_string($mime) && preg_match($regexp, $mime, $matches))
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001187 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001188 $this->file_type = $matches[1];
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001189 return;
1190 }
1191 }
1192 }
1193
Andrey Andreeva49e3812011-12-09 13:05:22 +02001194 /* This is an ugly hack, but UNIX-type systems provide a "native" way to detect the file type,
1195 * which is still more secure than depending on the value of $_FILES[$field]['type'], and as it
1196 * was reported in issue #750 (https://github.com/EllisLab/CodeIgniter/issues/750) - it's better
1197 * than mime_content_type() as well, hence the attempts to try calling the command line with
1198 * three different functions.
Andrey Andreev6700b932011-09-24 14:25:33 +03001199 *
1200 * Notes:
Andrey Andreev59654312011-12-02 14:28:54 +02001201 * - the DIRECTORY_SEPARATOR comparison ensures that we're not on a Windows system
Andrey Andreeva49e3812011-12-09 13:05:22 +02001202 * - many system admins would disable the exec(), shell_exec(), popen() and similar functions
1203 * due to security concerns, hence the function_exists() checks
Andrey Andreev6700b932011-09-24 14:25:33 +03001204 */
Andrey Andreeva49e3812011-12-09 13:05:22 +02001205 if (DIRECTORY_SEPARATOR !== '\\')
Andrey Andreev6700b932011-09-24 14:25:33 +03001206 {
Andrey Andreevd60e7002012-06-17 00:03:03 +03001207 $cmd = function_exists('escapeshellarg')
1208 ? 'file --brief --mime '.escapeshellarg($file['tmp_name']).' 2>&1'
1209 : 'file --brief --mime '.$file['tmp_name'].' 2>&1';
Andrey Andreeva49e3812011-12-09 13:05:22 +02001210
Andrey Andreeve9d2dc82012-11-07 14:23:29 +02001211 if (function_usable('exec'))
Andrey Andreev6700b932011-09-24 14:25:33 +03001212 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001213 /* This might look confusing, as $mime is being populated with all of the output when set in the second parameter.
1214 * However, we only neeed the last line, which is the actual return value of exec(), and as such - it overwrites
1215 * anything that could already be set for $mime previously. This effectively makes the second parameter a dummy
1216 * value, which is only put to allow us to get the return status code.
1217 */
1218 $mime = @exec($cmd, $mime, $return_status);
1219 if ($return_status === 0 && is_string($mime) && preg_match($regexp, $mime, $matches))
1220 {
1221 $this->file_type = $matches[1];
1222 return;
1223 }
1224 }
1225
Andrey Andreeve9d2dc82012-11-07 14:23:29 +02001226 if ( (bool) @ini_get('safe_mode') === FALSE && function_usable('shell_exec'))
Andrey Andreeva49e3812011-12-09 13:05:22 +02001227 {
1228 $mime = @shell_exec($cmd);
1229 if (strlen($mime) > 0)
1230 {
1231 $mime = explode("\n", trim($mime));
1232 if (preg_match($regexp, $mime[(count($mime) - 1)], $matches))
1233 {
1234 $this->file_type = $matches[1];
1235 return;
1236 }
1237 }
1238 }
1239
Andrey Andreeve9d2dc82012-11-07 14:23:29 +02001240 if (function_usable('popen'))
Andrey Andreeva49e3812011-12-09 13:05:22 +02001241 {
1242 $proc = @popen($cmd, 'r');
1243 if (is_resource($proc))
1244 {
tubalmartin010f1f42012-03-03 22:24:31 +01001245 $mime = @fread($proc, 512);
Andrey Andreeva49e3812011-12-09 13:05:22 +02001246 @pclose($proc);
1247 if ($mime !== FALSE)
1248 {
1249 $mime = explode("\n", trim($mime));
1250 if (preg_match($regexp, $mime[(count($mime) - 1)], $matches))
1251 {
1252 $this->file_type = $matches[1];
1253 return;
1254 }
1255 }
1256 }
1257 }
1258 }
1259
1260 // Fall back to the deprecated mime_content_type(), if available (still better than $_FILES[$field]['type'])
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001261 if (function_exists('mime_content_type'))
1262 {
1263 $this->file_type = @mime_content_type($file['tmp_name']);
Andrey Andreeva49e3812011-12-09 13:05:22 +02001264 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 +03001265 {
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001266 return;
1267 }
1268 }
1269
1270 $this->file_type = $file['type'];
1271 }
1272
Derek Allard2067d1a2008-11-13 22:59:24 +00001273}
Derek Allard2067d1a2008-11-13 22:59:24 +00001274
1275/* End of file Upload.php */
Andrey Andreev56454792012-05-17 14:32:19 +03001276/* Location: ./system/libraries/Upload.php */