blob: 7c48b4294be0befe39ac6735c39a54398988203b [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev3a459572011-12-21 11:23:11 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev3a459572011-12-21 11:23:11 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * File Uploading Class
31 *
32 * @package CodeIgniter
33 * @subpackage Libraries
34 * @category Uploads
Derek Jonesf4a4bd82011-10-20 12:18:42 -050035 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @link http://codeigniter.com/user_guide/libraries/file_uploading.html
37 */
38class CI_Upload {
Barry Mienydd671972010-10-04 16:33:58 +020039
Andrey Andreevf5ccd122012-11-02 00:17:39 +020040 /**
41 * Maximum file size
42 *
43 * @var int
44 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +030045 public $max_size = 0;
Andrey Andreevf5ccd122012-11-02 00:17:39 +020046
47 /**
48 * Maximum image width
49 *
50 * @var int
51 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +030052 public $max_width = 0;
Andrey Andreevf5ccd122012-11-02 00:17:39 +020053
54 /**
55 * Maximum image height
56 *
57 * @var int
58 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +030059 public $max_height = 0;
Andrey Andreevf5ccd122012-11-02 00:17:39 +020060
61 /**
Andrey Andreev05aa2d62012-12-03 16:06:55 +020062 * Minimum image width
63 *
64 * @var int
65 */
66 public $min_width = 0;
67
68 /**
69 * Minimum image height
70 *
71 * @var int
72 */
73 public $min_height = 0;
74
75 /**
Andrey Andreevf5ccd122012-11-02 00:17:39 +020076 * Maximum filename length
77 *
78 * @var int
79 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +030080 public $max_filename = 0;
Andrey Andreevf5ccd122012-11-02 00:17:39 +020081
82 /**
83 * Maximum duplicate filename increment ID
84 *
85 * @var int
86 */
Adam Jackettccbbea12011-08-21 16:19:11 -040087 public $max_filename_increment = 100;
Andrey Andreevf5ccd122012-11-02 00:17:39 +020088
89 /**
90 * Allowed file types
91 *
92 * @var string
93 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +030094 public $allowed_types = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +020095
96 /**
97 * Temporary filename
98 *
99 * @var string
100 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300101 public $file_temp = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200102
103 /**
104 * Filename
105 *
106 * @var string
107 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300108 public $file_name = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200109
110 /**
111 * Original filename
112 *
113 * @var string
114 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300115 public $orig_name = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200116
117 /**
118 * File type
119 *
120 * @var string
121 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300122 public $file_type = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200123
124 /**
125 * File size
126 *
127 * @var int
128 */
129 public $file_size = NULL;
130
131 /**
132 * Filename extension
133 *
134 * @var string
135 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300136 public $file_ext = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200137
138 /**
139 * Upload path
140 *
141 * @var string
142 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300143 public $upload_path = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200144
145 /**
146 * Overwrite flag
147 *
148 * @var bool
149 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300150 public $overwrite = FALSE;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200151
152 /**
153 * Obfuscate filename flag
154 *
155 * @var bool
156 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300157 public $encrypt_name = FALSE;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200158
159 /**
160 * Is image flag
161 *
162 * @var bool
163 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300164 public $is_image = FALSE;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200165
166 /**
167 * Image width
168 *
169 * @var int
170 */
171 public $image_width = NULL;
172
173 /**
174 * Image height
175 *
176 * @var int
177 */
178 public $image_height = NULL;
179
180 /**
181 * Image type
182 *
183 * @var string
184 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300185 public $image_type = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200186
187 /**
188 * Image size string
189 *
190 * @var string
191 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300192 public $image_size_str = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200193
194 /**
195 * Error messages list
196 *
197 * @var array
198 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300199 public $error_msg = array();
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200200
201 /**
202 * MIME types list
203 *
204 * @var array
205 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300206 public $mimes = array();
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200207
208 /**
209 * Remove spaces flag
210 *
211 * @var bool
212 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300213 public $remove_spaces = TRUE;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200214
215 /**
216 * MIME detection flag
217 *
218 * @var bool
219 */
Andrey Andreevd60e7002012-06-17 00:03:03 +0300220 public $detect_mime = TRUE;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200221
222 /**
223 * XSS filter flag
224 *
225 * @var bool
226 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300227 public $xss_clean = FALSE;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200228
229 /**
230 * Temporary filename prefix
231 *
232 * @var string
233 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300234 public $temp_prefix = 'temp_file_';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200235
236 /**
237 * Filename sent by the client
238 *
239 * @var bool
240 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300241 public $client_name = '';
Barry Mienydd671972010-10-04 16:33:58 +0200242
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200243 // --------------------------------------------------------------------
244
245 /**
246 * Filename override
247 *
248 * @var string
249 */
Greg Aker58fdee82010-11-10 15:07:09 -0600250 protected $_file_name_override = '';
Barry Mienydd671972010-10-04 16:33:58 +0200251
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200252 // --------------------------------------------------------------------
253
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 /**
255 * Constructor
256 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200257 * @param array $props
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300258 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 */
Greg Aker58fdee82010-11-10 15:07:09 -0600260 public function __construct($props = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 {
262 if (count($props) > 0)
263 {
264 $this->initialize($props);
265 }
Barry Mienydd671972010-10-04 16:33:58 +0200266
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300267 $this->mimes =& get_mimes();
268
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300269 log_message('debug', 'Upload Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 }
Barry Mienydd671972010-10-04 16:33:58 +0200271
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200273
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 /**
275 * Initialize preferences
276 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200277 * @param array $config
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200279 */
Greg Aker58fdee82010-11-10 15:07:09 -0600280 public function initialize($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 {
282 $defaults = array(
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300283 'max_size' => 0,
284 'max_width' => 0,
285 'max_height' => 0,
Andrey Andreev05aa2d62012-12-03 16:06:55 +0200286 'min_width' => 0,
287 'min_height' => 0,
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300288 'max_filename' => 0,
289 'max_filename_increment' => 100,
290 'allowed_types' => '',
291 'file_temp' => '',
292 'file_name' => '',
293 'orig_name' => '',
294 'file_type' => '',
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200295 'file_size' => NULL,
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300296 'file_ext' => '',
297 'upload_path' => '',
298 'overwrite' => FALSE,
299 'encrypt_name' => FALSE,
300 'is_image' => FALSE,
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200301 'image_width' => NULL,
302 'image_height' => NULL,
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300303 'image_type' => '',
304 'image_size_str' => '',
305 'error_msg' => array(),
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300306 'remove_spaces' => TRUE,
Andrey Andreevd60e7002012-06-17 00:03:03 +0300307 'detect_mime' => TRUE,
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300308 'xss_clean' => FALSE,
309 'temp_prefix' => 'temp_file_',
310 'client_name' => ''
311 );
Barry Mienydd671972010-10-04 16:33:58 +0200312
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 foreach ($defaults as $key => $val)
314 {
315 if (isset($config[$key]))
316 {
317 $method = 'set_'.$key;
318 if (method_exists($this, $method))
319 {
320 $this->$method($config[$key]);
321 }
322 else
323 {
324 $this->$key = $config[$key];
Barry Mienydd671972010-10-04 16:33:58 +0200325 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 }
327 else
328 {
329 $this->$key = $val;
330 }
331 }
Barry Mienydd671972010-10-04 16:33:58 +0200332
Derek Jonese9d723f2010-07-12 10:10:59 -0500333 // if a file_name was provided in the config, use it instead of the user input
334 // supplied file name for all uploads until initialized again
335 $this->_file_name_override = $this->file_name;
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 }
Barry Mienydd671972010-10-04 16:33:58 +0200337
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200339
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 /**
341 * Perform the file upload
342 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200343 * @param string $field
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200345 */
Greg Aker58fdee82010-11-10 15:07:09 -0600346 public function do_upload($field = 'userfile')
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300348 // Is $_FILES[$field] set? If not, no reason to continue.
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 if ( ! isset($_FILES[$field]))
350 {
351 $this->set_error('upload_no_file_selected');
352 return FALSE;
353 }
Barry Mienydd671972010-10-04 16:33:58 +0200354
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 // Is the upload path valid?
356 if ( ! $this->validate_upload_path())
357 {
358 // errors will already be set by validate_upload_path() so just return FALSE
359 return FALSE;
360 }
361
362 // Was the file able to be uploaded? If not, determine the reason why.
363 if ( ! is_uploaded_file($_FILES[$field]['tmp_name']))
364 {
365 $error = ( ! isset($_FILES[$field]['error'])) ? 4 : $_FILES[$field]['error'];
366
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300367 switch ($error)
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 {
Darren Benneye123a602013-03-30 18:17:54 +0000369 case UPLOAD_ERR_INI_SIZE:
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 $this->set_error('upload_file_exceeds_limit');
371 break;
Darren Benneye123a602013-03-30 18:17:54 +0000372 case UPLOAD_ERR_FORM_SIZE:
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 $this->set_error('upload_file_exceeds_form_limit');
374 break;
Darren Benneye123a602013-03-30 18:17:54 +0000375 case UPLOAD_ERR_PARTIAL:
Barry Mienydd671972010-10-04 16:33:58 +0200376 $this->set_error('upload_file_partial');
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 break;
Darren Benneye123a602013-03-30 18:17:54 +0000378 case UPLOAD_ERR_NO_FILE:
Barry Mienydd671972010-10-04 16:33:58 +0200379 $this->set_error('upload_no_file_selected');
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 break;
Darren Benneye123a602013-03-30 18:17:54 +0000381 case UPLOAD_ERR_NO_TMP_DIR:
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 $this->set_error('upload_no_temp_directory');
383 break;
Darren Benneye123a602013-03-30 18:17:54 +0000384 case UPLOAD_ERR_CANT_WRITE:
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 $this->set_error('upload_unable_to_write_file');
386 break;
Darren Benneye123a602013-03-30 18:17:54 +0000387 case UPLOAD_ERR_EXTENSION:
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 $this->set_error('upload_stopped_by_extension');
389 break;
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300390 default:
391 $this->set_error('upload_no_file_selected');
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 break;
393 }
394
395 return FALSE;
396 }
397
398 // Set the uploaded data as class variables
Barry Mienydd671972010-10-04 16:33:58 +0200399 $this->file_temp = $_FILES[$field]['tmp_name'];
400 $this->file_size = $_FILES[$field]['size'];
Andrey Andreevd60e7002012-06-17 00:03:03 +0300401
402 // Skip MIME type detection?
403 if ($this->detect_mime !== FALSE)
404 {
405 $this->_file_mime_type($_FILES[$field]);
406 }
407
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300408 $this->file_type = preg_replace('/^(.+?);.*$/', '\\1', $this->file_type);
Derek Jones616fb022010-04-22 16:52:18 -0500409 $this->file_type = strtolower(trim(stripslashes($this->file_type), '"'));
Derek Jonese9d723f2010-07-12 10:10:59 -0500410 $this->file_name = $this->_prep_filename($_FILES[$field]['name']);
411 $this->file_ext = $this->get_extension($this->file_name);
412 $this->client_name = $this->file_name;
Barry Mienydd671972010-10-04 16:33:58 +0200413
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 // Is the file type allowed to be uploaded?
415 if ( ! $this->is_allowed_filetype())
416 {
417 $this->set_error('upload_invalid_filetype');
418 return FALSE;
419 }
420
Derek Jonese9d723f2010-07-12 10:10:59 -0500421 // if we're overriding, let's now make sure the new name and type is allowed
Alex Bilbied261b1e2012-06-02 11:12:16 +0100422 if ($this->_file_name_override !== '')
Derek Jonese9d723f2010-07-12 10:10:59 -0500423 {
424 $this->file_name = $this->_prep_filename($this->_file_name_override);
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000425
426 // If no extension was provided in the file_name config item, use the uploaded one
Pascal Kriete14287f32011-02-14 13:39:34 -0500427 if (strpos($this->_file_name_override, '.') === FALSE)
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000428 {
429 $this->file_name .= $this->file_ext;
430 }
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000431 else
432 {
vlakoff35672462013-02-15 01:36:04 +0100433 // An extension was provided, let's have it!
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300434 $this->file_ext = $this->get_extension($this->_file_name_override);
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000435 }
Derek Jonese9d723f2010-07-12 10:10:59 -0500436
437 if ( ! $this->is_allowed_filetype(TRUE))
438 {
439 $this->set_error('upload_invalid_filetype');
Barry Mienydd671972010-10-04 16:33:58 +0200440 return FALSE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500441 }
442 }
Barry Mienydd671972010-10-04 16:33:58 +0200443
Derek Jonese9d723f2010-07-12 10:10:59 -0500444 // Convert the file size to kilobytes
445 if ($this->file_size > 0)
446 {
447 $this->file_size = round($this->file_size/1024, 2);
448 }
449
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 // Is the file size within the allowed maximum?
451 if ( ! $this->is_allowed_filesize())
452 {
453 $this->set_error('upload_invalid_filesize');
454 return FALSE;
455 }
456
457 // Are the image dimensions within the allowed size?
458 // Note: This can fail if the server has an open_basdir restriction.
459 if ( ! $this->is_allowed_dimensions())
460 {
461 $this->set_error('upload_invalid_dimensions');
462 return FALSE;
463 }
464
465 // Sanitize the file name for security
Andrey Andreev7e559772013-01-29 15:38:33 +0200466 $CI =& get_instance();
467 $this->file_name = $CI->security->sanitize_filename($this->file_name);
Barry Mienydd671972010-10-04 16:33:58 +0200468
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 // Truncate the file name if it's too long
470 if ($this->max_filename > 0)
471 {
472 $this->file_name = $this->limit_filename_length($this->file_name, $this->max_filename);
473 }
474
475 // Remove white spaces in the name
Alex Bilbied261b1e2012-06-02 11:12:16 +0100476 if ($this->remove_spaces === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300478 $this->file_name = preg_replace('/\s+/', '_', $this->file_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 }
480
481 /*
482 * Validate the file name
483 * This function appends an number onto the end of
484 * the file if one with the same name already exists.
485 * If it returns false there was a problem.
486 */
487 $this->orig_name = $this->file_name;
488
Alex Bilbied261b1e2012-06-02 11:12:16 +0100489 if ($this->overwrite === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 {
491 $this->file_name = $this->set_filename($this->upload_path, $this->file_name);
Barry Mienydd671972010-10-04 16:33:58 +0200492
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 if ($this->file_name === FALSE)
494 {
495 return FALSE;
496 }
497 }
498
499 /*
Derek Jonese9d723f2010-07-12 10:10:59 -0500500 * Run the file through the XSS hacking filter
501 * This helps prevent malicious code from being
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300502 * embedded within a file. Scripts can easily
Derek Jonese9d723f2010-07-12 10:10:59 -0500503 * be disguised as images or other file types.
504 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300505 if ($this->xss_clean && $this->do_xss_clean() === FALSE)
Derek Jonese9d723f2010-07-12 10:10:59 -0500506 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300507 $this->set_error('upload_unable_to_write_file');
508 return FALSE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500509 }
510
511 /*
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 * Move the file to the final destination
513 * To deal with different server configurations
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300514 * we'll attempt to use copy() first. If that fails
515 * we'll use move_uploaded_file(). One of the two should
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 * reliably work in most environments
517 */
518 if ( ! @copy($this->file_temp, $this->upload_path.$this->file_name))
519 {
520 if ( ! @move_uploaded_file($this->file_temp, $this->upload_path.$this->file_name))
521 {
Barry Mienydd671972010-10-04 16:33:58 +0200522 $this->set_error('upload_destination_error');
523 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 }
525 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000526
527 /*
528 * Set the finalized image dimensions
529 * This sets the image width/height (assuming the
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300530 * file was an image). We use this information
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 * in the "data" function.
532 */
533 $this->set_image_properties($this->upload_path.$this->file_name);
534
535 return TRUE;
536 }
Barry Mienydd671972010-10-04 16:33:58 +0200537
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200539
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 /**
541 * Finalized Data Array
Barry Mienydd671972010-10-04 16:33:58 +0200542 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 * Returns an associative array containing all of the information
544 * related to the upload, allowing the developer easy access in one array.
545 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200546 * @param string $index
Michiel Vugteveen0ee287e2012-06-11 10:38:14 +0200547 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200548 */
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200549 public function data($index = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 {
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200551 $data = array(
Michiel Vugteveen3a7fb042012-06-11 09:29:16 +0200552 'file_name' => $this->file_name,
553 'file_type' => $this->file_type,
554 'file_path' => $this->upload_path,
555 'full_path' => $this->upload_path.$this->file_name,
556 'raw_name' => str_replace($this->file_ext, '', $this->file_name),
557 'orig_name' => $this->orig_name,
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300558 'client_name' => $this->client_name,
Michiel Vugteveen3a7fb042012-06-11 09:29:16 +0200559 'file_ext' => $this->file_ext,
560 'file_size' => $this->file_size,
561 'is_image' => $this->is_image(),
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300562 'image_width' => $this->image_width,
563 'image_height' => $this->image_height,
564 'image_type' => $this->image_type,
565 'image_size_str' => $this->image_size_str,
566 );
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200567
Michiel Vugteveen46a04292012-06-11 10:37:52 +0200568 if ( ! empty($index))
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200569 {
Michiel Vugteveen46a04292012-06-11 10:37:52 +0200570 return isset($data[$index]) ? $data[$index] : NULL;
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200571 }
572
Michiel Vugteveen46a04292012-06-11 10:37:52 +0200573 return $data;
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 }
Barry Mienydd671972010-10-04 16:33:58 +0200575
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200577
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 /**
579 * Set Upload Path
580 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200581 * @param string $path
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200583 */
Greg Aker58fdee82010-11-10 15:07:09 -0600584 public function set_upload_path($path)
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 {
586 // Make sure it has a trailing slash
587 $this->upload_path = rtrim($path, '/').'/';
588 }
Barry Mienydd671972010-10-04 16:33:58 +0200589
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200591
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 /**
593 * Set the file name
594 *
595 * This function takes a filename/path as input and looks for the
596 * existence of a file with the same name. If found, it will append a
597 * number to the end of the filename to avoid overwriting a pre-existing file.
598 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200599 * @param string $path
600 * @param string $filename
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200602 */
Greg Aker58fdee82010-11-10 15:07:09 -0600603 public function set_filename($path, $filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100605 if ($this->encrypt_name === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200606 {
Barry Mienydd671972010-10-04 16:33:58 +0200607 $filename = md5(uniqid(mt_rand())).$this->file_ext;
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 }
Barry Mienydd671972010-10-04 16:33:58 +0200609
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 if ( ! file_exists($path.$filename))
611 {
612 return $filename;
613 }
Barry Mienydd671972010-10-04 16:33:58 +0200614
Derek Allard2067d1a2008-11-13 22:59:24 +0000615 $filename = str_replace($this->file_ext, '', $filename);
Barry Mienydd671972010-10-04 16:33:58 +0200616
Derek Allard2067d1a2008-11-13 22:59:24 +0000617 $new_filename = '';
Adam Jackettccbbea12011-08-21 16:19:11 -0400618 for ($i = 1; $i < $this->max_filename_increment; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200619 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 if ( ! file_exists($path.$filename.$i.$this->file_ext))
621 {
622 $new_filename = $filename.$i.$this->file_ext;
623 break;
624 }
625 }
626
Alex Bilbied261b1e2012-06-02 11:12:16 +0100627 if ($new_filename === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 {
629 $this->set_error('upload_bad_filename');
630 return FALSE;
631 }
632 else
633 {
634 return $new_filename;
635 }
636 }
Barry Mienydd671972010-10-04 16:33:58 +0200637
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200639
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 /**
641 * Set Maximum File Size
642 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200643 * @param int $n
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200645 */
Greg Aker58fdee82010-11-10 15:07:09 -0600646 public function set_max_filesize($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300648 $this->max_size = ((int) $n < 0) ? 0 : (int) $n;
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 }
Barry Mienydd671972010-10-04 16:33:58 +0200650
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200652
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 /**
654 * Set Maximum File Name Length
655 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200656 * @param int $n
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200658 */
Greg Aker58fdee82010-11-10 15:07:09 -0600659 public function set_max_filename($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300661 $this->max_filename = ((int) $n < 0) ? 0 : (int) $n;
Derek Allard2067d1a2008-11-13 22:59:24 +0000662 }
663
664 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200665
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 /**
667 * Set Maximum Image Width
668 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200669 * @param int $n
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200671 */
Greg Aker58fdee82010-11-10 15:07:09 -0600672 public function set_max_width($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300674 $this->max_width = ((int) $n < 0) ? 0 : (int) $n;
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 }
Barry Mienydd671972010-10-04 16:33:58 +0200676
Derek Allard2067d1a2008-11-13 22:59:24 +0000677 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200678
Derek Allard2067d1a2008-11-13 22:59:24 +0000679 /**
680 * Set Maximum Image Height
681 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200682 * @param int $n
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200684 */
Greg Aker58fdee82010-11-10 15:07:09 -0600685 public function set_max_height($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300687 $this->max_height = ((int) $n < 0) ? 0 : (int) $n;
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 }
Barry Mienydd671972010-10-04 16:33:58 +0200689
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200691
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 /**
Andrey Andreev05aa2d62012-12-03 16:06:55 +0200693 * Set minimum image width
694 *
695 * @param int $n
696 * @return void
697 */
698 public function set_min_width($n)
699 {
700 $this->min_width = ((int) $n < 0) ? 0 : (int) $n;
701 }
702
703 // --------------------------------------------------------------------
704
705 /**
706 * Set minimum image height
707 *
708 * @param int $n
709 * @return void
710 */
711 public function set_min_height($n)
712 {
713 $this->min_height = ((int) $n < 0) ? 0 : (int) $n;
714 }
715
716 // --------------------------------------------------------------------
717
718 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000719 * Set Allowed File Types
720 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200721 * @param string $types
Derek Allard2067d1a2008-11-13 22:59:24 +0000722 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200723 */
Greg Aker58fdee82010-11-10 15:07:09 -0600724 public function set_allowed_types($types)
Derek Allard2067d1a2008-11-13 22:59:24 +0000725 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300726 if ( ! is_array($types) && $types === '*')
Derek Jonese12f64e2010-03-02 22:55:08 -0600727 {
728 $this->allowed_types = '*';
729 return;
730 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000731 $this->allowed_types = explode('|', $types);
732 }
Barry Mienydd671972010-10-04 16:33:58 +0200733
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200735
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 /**
737 * Set Image Properties
738 *
739 * Uses GD to determine the width/height/type of image
740 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200741 * @param string $path
Derek Allard2067d1a2008-11-13 22:59:24 +0000742 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200743 */
Greg Aker58fdee82010-11-10 15:07:09 -0600744 public function set_image_properties($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000745 {
746 if ( ! $this->is_image())
747 {
748 return;
749 }
750
751 if (function_exists('getimagesize'))
752 {
753 if (FALSE !== ($D = @getimagesize($path)))
Barry Mienydd671972010-10-04 16:33:58 +0200754 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000755 $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
756
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300757 $this->image_width = $D[0];
758 $this->image_height = $D[1];
759 $this->image_type = isset($types[$D[2]]) ? $types[$D[2]] : 'unknown';
760 $this->image_size_str = $D[3]; // string containing height and width
Derek Allard2067d1a2008-11-13 22:59:24 +0000761 }
762 }
763 }
Barry Mienydd671972010-10-04 16:33:58 +0200764
Derek Allard2067d1a2008-11-13 22:59:24 +0000765 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200766
Derek Allard2067d1a2008-11-13 22:59:24 +0000767 /**
768 * Set XSS Clean
769 *
770 * Enables the XSS flag so that the file that was uploaded
771 * will be run through the XSS filter.
772 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200773 * @param bool $flag
Derek Allard2067d1a2008-11-13 22:59:24 +0000774 * @return void
775 */
Greg Aker58fdee82010-11-10 15:07:09 -0600776 public function set_xss_clean($flag = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000777 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100778 $this->xss_clean = ($flag === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 }
Barry Mienydd671972010-10-04 16:33:58 +0200780
Derek Allard2067d1a2008-11-13 22:59:24 +0000781 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200782
Derek Allard2067d1a2008-11-13 22:59:24 +0000783 /**
784 * Validate the image
785 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200787 */
Greg Aker58fdee82010-11-10 15:07:09 -0600788 public function is_image()
Derek Allard2067d1a2008-11-13 22:59:24 +0000789 {
790 // IE will sometimes return odd mime-types during upload, so here we just standardize all
791 // jpegs or pngs to the same file type.
792
Derek Jones4b9c6292011-07-01 17:40:48 -0500793 $png_mimes = array('image/x-png');
Derek Allard2067d1a2008-11-13 22:59:24 +0000794 $jpeg_mimes = array('image/jpg', 'image/jpe', 'image/jpeg', 'image/pjpeg');
Barry Mienydd671972010-10-04 16:33:58 +0200795
Derek Allard2067d1a2008-11-13 22:59:24 +0000796 if (in_array($this->file_type, $png_mimes))
797 {
798 $this->file_type = 'image/png';
799 }
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300800 elseif (in_array($this->file_type, $jpeg_mimes))
Derek Allard2067d1a2008-11-13 22:59:24 +0000801 {
802 $this->file_type = 'image/jpeg';
803 }
804
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300805 $img_mimes = array('image/gif', 'image/jpeg', 'image/png');
Derek Allard2067d1a2008-11-13 22:59:24 +0000806
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300807 return in_array($this->file_type, $img_mimes, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000808 }
Barry Mienydd671972010-10-04 16:33:58 +0200809
Derek Allard2067d1a2008-11-13 22:59:24 +0000810 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200811
Derek Allard2067d1a2008-11-13 22:59:24 +0000812 /**
813 * Verify that the filetype is allowed
814 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200815 * @param bool $ignore_mime
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200817 */
Greg Aker58fdee82010-11-10 15:07:09 -0600818 public function is_allowed_filetype($ignore_mime = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000819 {
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200820 if ($this->allowed_types === '*')
Derek Jonese12f64e2010-03-02 22:55:08 -0600821 {
822 return TRUE;
823 }
Barry Mienydd671972010-10-04 16:33:58 +0200824
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200825 if ( ! is_array($this->allowed_types) OR count($this->allowed_types) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 {
827 $this->set_error('upload_no_file_types');
828 return FALSE;
829 }
Barry Mienydd671972010-10-04 16:33:58 +0200830
Derek Jonese9d723f2010-07-12 10:10:59 -0500831 $ext = strtolower(ltrim($this->file_ext, '.'));
Barry Mienydd671972010-10-04 16:33:58 +0200832
Derek Jonese9d723f2010-07-12 10:10:59 -0500833 if ( ! in_array($ext, $this->allowed_types))
Derek Allard2067d1a2008-11-13 22:59:24 +0000834 {
Derek Jonese9d723f2010-07-12 10:10:59 -0500835 return FALSE;
836 }
Derek Jonesafa282f2009-02-10 17:11:52 +0000837
Barry Mienydd671972010-10-04 16:33:58 +0200838 // Images get some additional checks
Derek Jonese9d723f2010-07-12 10:10:59 -0500839 $image_types = array('gif', 'jpg', 'jpeg', 'png', 'jpe');
Barry Mienydd671972010-10-04 16:33:58 +0200840
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200841 if (in_array($ext, $image_types) && @getimagesize($this->file_temp) === FALSE)
Derek Jonese9d723f2010-07-12 10:10:59 -0500842 {
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200843 return FALSE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500844 }
Barry Mienydd671972010-10-04 16:33:58 +0200845
Derek Jonese9d723f2010-07-12 10:10:59 -0500846 if ($ignore_mime === TRUE)
847 {
848 return TRUE;
849 }
Barry Mienydd671972010-10-04 16:33:58 +0200850
Derek Jonese9d723f2010-07-12 10:10:59 -0500851 $mime = $this->mimes_types($ext);
Barry Mienydd671972010-10-04 16:33:58 +0200852
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300853 if (is_array($mime) && in_array($this->file_type, $mime, TRUE))
Derek Jonese9d723f2010-07-12 10:10:59 -0500854 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300855 return TRUE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500856 }
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200857 elseif ($mime === $this->file_type)
Derek Jonese9d723f2010-07-12 10:10:59 -0500858 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300859 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000860 }
Barry Mienydd671972010-10-04 16:33:58 +0200861
Derek Allard2067d1a2008-11-13 22:59:24 +0000862 return FALSE;
863 }
Barry Mienydd671972010-10-04 16:33:58 +0200864
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200866
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 /**
868 * Verify that the file is within the allowed size
869 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200871 */
Greg Aker58fdee82010-11-10 15:07:09 -0600872 public function is_allowed_filesize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000873 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100874 return ($this->max_size === 0 OR $this->max_size > $this->file_size);
Derek Allard2067d1a2008-11-13 22:59:24 +0000875 }
Barry Mienydd671972010-10-04 16:33:58 +0200876
Derek Allard2067d1a2008-11-13 22:59:24 +0000877 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200878
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 /**
880 * Verify that the image is within the allowed width/height
881 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000882 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200883 */
Greg Aker58fdee82010-11-10 15:07:09 -0600884 public function is_allowed_dimensions()
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 {
886 if ( ! $this->is_image())
887 {
888 return TRUE;
889 }
890
891 if (function_exists('getimagesize'))
892 {
893 $D = @getimagesize($this->file_temp);
894
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300895 if ($this->max_width > 0 && $D[0] > $this->max_width)
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 {
897 return FALSE;
898 }
899
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300900 if ($this->max_height > 0 && $D[1] > $this->max_height)
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 {
902 return FALSE;
903 }
Andrey Andreev05aa2d62012-12-03 16:06:55 +0200904
905 if ($this->min_width > 0 && $D[0] < $this->min_width)
906 {
907 return FALSE;
908 }
909
910 if ($this->min_height > 0 && $D[1] < $this->min_height)
911 {
912 return FALSE;
913 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000914 }
915
916 return TRUE;
917 }
Barry Mienydd671972010-10-04 16:33:58 +0200918
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 * Validate Upload Path
923 *
924 * Verifies that it is a valid upload path with proper permissions.
925 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200927 */
Greg Aker58fdee82010-11-10 15:07:09 -0600928 public function validate_upload_path()
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100930 if ($this->upload_path === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 {
932 $this->set_error('upload_no_filepath');
933 return FALSE;
934 }
Barry Mienydd671972010-10-04 16:33:58 +0200935
Andrey Andreevc839d282012-06-07 14:35:27 +0300936 if (@realpath($this->upload_path) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000937 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300938 $this->upload_path = str_replace('\\', '/', realpath($this->upload_path));
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 }
940
941 if ( ! @is_dir($this->upload_path))
942 {
943 $this->set_error('upload_no_filepath');
944 return FALSE;
945 }
946
947 if ( ! is_really_writable($this->upload_path))
948 {
949 $this->set_error('upload_not_writable');
950 return FALSE;
951 }
952
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300953 $this->upload_path = preg_replace('/(.+?)\/*$/', '\\1/', $this->upload_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 return TRUE;
955 }
Barry Mienydd671972010-10-04 16:33:58 +0200956
Derek Allard2067d1a2008-11-13 22:59:24 +0000957 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200958
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 /**
960 * Extract the file extension
961 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200962 * @param string $filename
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200964 */
Greg Aker58fdee82010-11-10 15:07:09 -0600965 public function get_extension($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000966 {
967 $x = explode('.', $filename);
Andrey Andreev46d53fb2012-05-11 13:42:24 +0300968 return (count($x) !== 1) ? '.'.end($x) : '';
Barry Mienydd671972010-10-04 16:33:58 +0200969 }
970
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200972
Derek Allard2067d1a2008-11-13 22:59:24 +0000973 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000974 * Limit the File Name Length
975 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300976 * @param string $filename
977 * @param int $length
Derek Allard2067d1a2008-11-13 22:59:24 +0000978 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200979 */
Greg Aker58fdee82010-11-10 15:07:09 -0600980 public function limit_filename_length($filename, $length)
Derek Allard2067d1a2008-11-13 22:59:24 +0000981 {
982 if (strlen($filename) < $length)
983 {
984 return $filename;
985 }
Barry Mienydd671972010-10-04 16:33:58 +0200986
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 $ext = '';
988 if (strpos($filename, '.') !== FALSE)
989 {
990 $parts = explode('.', $filename);
991 $ext = '.'.array_pop($parts);
992 $filename = implode('.', $parts);
993 }
Barry Mienydd671972010-10-04 16:33:58 +0200994
Derek Allard2067d1a2008-11-13 22:59:24 +0000995 return substr($filename, 0, ($length - strlen($ext))).$ext;
996 }
997
998 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200999
Derek Allard2067d1a2008-11-13 22:59:24 +00001000 /**
1001 * Runs the file through the XSS clean function
1002 *
1003 * This prevents people from embedding malicious code in their files.
1004 * I'm not sure that it won't negatively affect certain files in unexpected ways,
1005 * but so far I haven't found that it causes trouble.
1006 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001007 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001008 */
Greg Aker58fdee82010-11-10 15:07:09 -06001009 public function do_xss_clean()
Barry Mienydd671972010-10-04 16:33:58 +02001010 {
Derek Jonese9d723f2010-07-12 10:10:59 -05001011 $file = $this->file_temp;
Barry Mienydd671972010-10-04 16:33:58 +02001012
Andrey Andreev5036c9c2012-06-04 15:34:56 +03001013 if (filesize($file) == 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001014 {
1015 return FALSE;
1016 }
Barry Mienydd671972010-10-04 16:33:58 +02001017
Andrey Andreevc839d282012-06-07 14:35:27 +03001018 if (memory_get_usage() && ($memory_limit = ini_get('memory_limit')))
Greg Akerf82e51c2010-04-14 19:33:50 -05001019 {
Andrey Andreevc839d282012-06-07 14:35:27 +03001020 $memory_limit *= 1024 * 1024;
Barry Mienydd671972010-10-04 16:33:58 +02001021
Greg Akerc78a2592010-06-09 11:45:32 -05001022 // There was a bug/behavioural change in PHP 5.2, where numbers over one million get output
Andrey Andreev8e6f7a92012-03-26 20:13:00 +03001023 // into scientific notation. number_format() ensures this number is an integer
Greg Akerc78a2592010-06-09 11:45:32 -05001024 // http://bugs.php.net/bug.php?id=43053
Barry Mienydd671972010-10-04 16:33:58 +02001025
Andrey Andreevc839d282012-06-07 14:35:27 +03001026 $memory_limit = number_format(ceil(filesize($file) + $memory_limit), 0, '.', '');
Barry Mienydd671972010-10-04 16:33:58 +02001027
Andrey Andreevc839d282012-06-07 14:35:27 +03001028 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 -05001029 }
1030
1031 // If the file being uploaded is an image, then we should have no problem with XSS attacks (in theory), but
1032 // 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 +03001033 // using IE who looks at the image. It does this by inspecting the first 255 bytes of an image. To get around this
1034 // 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 +02001035 // 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 -05001036 // attempted XSS attack.
1037
1038 if (function_exists('getimagesize') && @getimagesize($file) !== FALSE)
1039 {
Barry Mienydd671972010-10-04 16:33:58 +02001040 if (($file = @fopen($file, 'rb')) === FALSE) // "b" to force binary
1041 {
Greg Akerf82e51c2010-04-14 19:33:50 -05001042 return FALSE; // Couldn't open the file, return FALSE
Barry Mienydd671972010-10-04 16:33:58 +02001043 }
Greg Akerf82e51c2010-04-14 19:33:50 -05001044
Barry Mienydd671972010-10-04 16:33:58 +02001045 $opening_bytes = fread($file, 256);
1046 fclose($file);
Greg Akerf82e51c2010-04-14 19:33:50 -05001047
1048 // These are known to throw IE into mime-type detection chaos
1049 // <a, <body, <head, <html, <img, <plaintext, <pre, <script, <table, <title
1050 // title is basically just in SVG, but we filter it anyhow
1051
vlakoff35672462013-02-15 01:36:04 +01001052 // if it's an image or no "triggers" detected in the first 256 bytes - we're good
Andrey Andreevc839d282012-06-07 14:35:27 +03001053 return ! preg_match('/<(a|body|head|html|img|plaintext|pre|script|table|title)[\s>]/i', $opening_bytes);
Greg Akerf82e51c2010-04-14 19:33:50 -05001054 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001055
1056 if (($data = @file_get_contents($file)) === FALSE)
1057 {
1058 return FALSE;
1059 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001060
Greg Akerf82e51c2010-04-14 19:33:50 -05001061 $CI =& get_instance();
Greg Akerf82e51c2010-04-14 19:33:50 -05001062 return $CI->security->xss_clean($data, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001063 }
Barry Mienydd671972010-10-04 16:33:58 +02001064
Derek Allard2067d1a2008-11-13 22:59:24 +00001065 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001066
Derek Allard2067d1a2008-11-13 22:59:24 +00001067 /**
1068 * Set an error message
1069 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001070 * @param string $msg
Derek Allard2067d1a2008-11-13 22:59:24 +00001071 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001072 */
Greg Aker58fdee82010-11-10 15:07:09 -06001073 public function set_error($msg)
Derek Allard2067d1a2008-11-13 22:59:24 +00001074 {
Barry Mienydd671972010-10-04 16:33:58 +02001075 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +00001076 $CI->lang->load('upload');
Barry Mienydd671972010-10-04 16:33:58 +02001077
Derek Allard2067d1a2008-11-13 22:59:24 +00001078 if (is_array($msg))
1079 {
1080 foreach ($msg as $val)
1081 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001082 $msg = ($CI->lang->line($val) === FALSE) ? $val : $CI->lang->line($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001083 $this->error_msg[] = $msg;
1084 log_message('error', $msg);
Barry Mienydd671972010-10-04 16:33:58 +02001085 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001086 }
1087 else
1088 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001089 $msg = ($CI->lang->line($msg) === FALSE) ? $msg : $CI->lang->line($msg);
Derek Allard2067d1a2008-11-13 22:59:24 +00001090 $this->error_msg[] = $msg;
1091 log_message('error', $msg);
1092 }
1093 }
Barry Mienydd671972010-10-04 16:33:58 +02001094
Derek Allard2067d1a2008-11-13 22:59:24 +00001095 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001096
Derek Allard2067d1a2008-11-13 22:59:24 +00001097 /**
1098 * Display the error message
1099 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001100 * @param string $open
1101 * @param string $close
Derek Allard2067d1a2008-11-13 22:59:24 +00001102 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001103 */
Greg Aker58fdee82010-11-10 15:07:09 -06001104 public function display_errors($open = '<p>', $close = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +00001105 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +03001106 return (count($this->error_msg) > 0) ? $open.implode($close.$open, $this->error_msg).$close : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001107 }
Barry Mienydd671972010-10-04 16:33:58 +02001108
Derek Allard2067d1a2008-11-13 22:59:24 +00001109 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001110
Derek Allard2067d1a2008-11-13 22:59:24 +00001111 /**
1112 * List of Mime Types
1113 *
Andrey Andreev8e6f7a92012-03-26 20:13:00 +03001114 * This is a list of mime types. We use it to validate
Derek Allard2067d1a2008-11-13 22:59:24 +00001115 * the "allowed types" set by the developer
1116 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001117 * @param string $mime
Derek Allard2067d1a2008-11-13 22:59:24 +00001118 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001119 */
Greg Aker58fdee82010-11-10 15:07:09 -06001120 public function mimes_types($mime)
Derek Allard2067d1a2008-11-13 22:59:24 +00001121 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +03001122 return isset($this->mimes[$mime]) ? $this->mimes[$mime] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001123 }
1124
1125 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001126
Derek Allard2067d1a2008-11-13 22:59:24 +00001127 /**
1128 * Prep Filename
1129 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001130 * Prevents possible script execution from Apache's handling
1131 * of files' multiple extensions.
Derek Allard2067d1a2008-11-13 22:59:24 +00001132 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001133 * @link http://httpd.apache.org/docs/1.3/mod/mod_mime.html#multipleext
1134 *
1135 * @param string $filename
Derek Allard2067d1a2008-11-13 22:59:24 +00001136 * @return string
1137 */
Greg Aker58fdee82010-11-10 15:07:09 -06001138 protected function _prep_filename($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +00001139 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001140 if (strpos($filename, '.') === FALSE OR $this->allowed_types === '*')
Derek Allard2067d1a2008-11-13 22:59:24 +00001141 {
1142 return $filename;
1143 }
Derek Allard616dab82009-02-16 15:44:32 +00001144
Derek Allard2067d1a2008-11-13 22:59:24 +00001145 $parts = explode('.', $filename);
1146 $ext = array_pop($parts);
1147 $filename = array_shift($parts);
Derek Allard616dab82009-02-16 15:44:32 +00001148
Derek Allard2067d1a2008-11-13 22:59:24 +00001149 foreach ($parts as $part)
1150 {
Derek Jonese9d723f2010-07-12 10:10:59 -05001151 if ( ! in_array(strtolower($part), $this->allowed_types) OR $this->mimes_types(strtolower($part)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001152 {
1153 $filename .= '.'.$part.'_';
1154 }
1155 else
1156 {
1157 $filename .= '.'.$part;
1158 }
1159 }
Derek Allardd70b0642009-02-16 13:51:42 +00001160
Andrey Andreev8e6f7a92012-03-26 20:13:00 +03001161 return $filename.'.'.$ext;
Derek Allard2067d1a2008-11-13 22:59:24 +00001162 }
1163
1164 // --------------------------------------------------------------------
1165
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001166 /**
1167 * File MIME type
1168 *
1169 * Detects the (actual) MIME type of the uploaded file, if possible.
1170 * The input array is expected to be $_FILES[$field]
1171 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001172 * @param array $file
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001173 * @return void
1174 */
1175 protected function _file_mime_type($file)
1176 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001177 // We'll need this to validate the MIME info string (e.g. text/plain; charset=us-ascii)
Andrey Andreevf7aed122011-12-13 11:01:06 +02001178 $regexp = '/^([a-z\-]+\/[a-z0-9\-\.\+]+)(;\s.+)?$/';
Andrey Andreeva49e3812011-12-09 13:05:22 +02001179
1180 /* Fileinfo extension - most reliable method
1181 *
1182 * Unfortunately, prior to PHP 5.3 - it's only available as a PECL extension and the
1183 * more convenient FILEINFO_MIME_TYPE flag doesn't exist.
1184 */
1185 if (function_exists('finfo_file'))
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001186 {
Andrey Andreevd60e7002012-06-17 00:03:03 +03001187 $finfo = @finfo_open(FILEINFO_MIME);
Andrey Andreeva49e3812011-12-09 13:05:22 +02001188 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 +03001189 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001190 $mime = @finfo_file($finfo, $file['tmp_name']);
1191 finfo_close($finfo);
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001192
1193 /* According to the comments section of the PHP manual page,
1194 * it is possible that this function returns an empty string
Andrey Andreev2cec39f2011-09-24 22:59:37 +03001195 * for some files (e.g. if they don't exist in the magic MIME database)
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001196 */
Andrey Andreeva49e3812011-12-09 13:05:22 +02001197 if (is_string($mime) && preg_match($regexp, $mime, $matches))
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001198 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001199 $this->file_type = $matches[1];
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001200 return;
1201 }
1202 }
1203 }
1204
Andrey Andreeva49e3812011-12-09 13:05:22 +02001205 /* This is an ugly hack, but UNIX-type systems provide a "native" way to detect the file type,
1206 * which is still more secure than depending on the value of $_FILES[$field]['type'], and as it
1207 * was reported in issue #750 (https://github.com/EllisLab/CodeIgniter/issues/750) - it's better
1208 * than mime_content_type() as well, hence the attempts to try calling the command line with
1209 * three different functions.
Andrey Andreev6700b932011-09-24 14:25:33 +03001210 *
1211 * Notes:
Andrey Andreev59654312011-12-02 14:28:54 +02001212 * - the DIRECTORY_SEPARATOR comparison ensures that we're not on a Windows system
Andrey Andreeva49e3812011-12-09 13:05:22 +02001213 * - many system admins would disable the exec(), shell_exec(), popen() and similar functions
vlakofff3994252013-02-19 01:45:23 +01001214 * due to security concerns, hence the function_usable() checks
Andrey Andreev6700b932011-09-24 14:25:33 +03001215 */
Andrey Andreeva49e3812011-12-09 13:05:22 +02001216 if (DIRECTORY_SEPARATOR !== '\\')
Andrey Andreev6700b932011-09-24 14:25:33 +03001217 {
Andrey Andreevd60e7002012-06-17 00:03:03 +03001218 $cmd = function_exists('escapeshellarg')
1219 ? 'file --brief --mime '.escapeshellarg($file['tmp_name']).' 2>&1'
1220 : 'file --brief --mime '.$file['tmp_name'].' 2>&1';
Andrey Andreeva49e3812011-12-09 13:05:22 +02001221
Andrey Andreeve9d2dc82012-11-07 14:23:29 +02001222 if (function_usable('exec'))
Andrey Andreev6700b932011-09-24 14:25:33 +03001223 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001224 /* This might look confusing, as $mime is being populated with all of the output when set in the second parameter.
vlakofff3994252013-02-19 01:45:23 +01001225 * However, we only need the last line, which is the actual return value of exec(), and as such - it overwrites
Andrey Andreeva49e3812011-12-09 13:05:22 +02001226 * anything that could already be set for $mime previously. This effectively makes the second parameter a dummy
1227 * value, which is only put to allow us to get the return status code.
1228 */
1229 $mime = @exec($cmd, $mime, $return_status);
1230 if ($return_status === 0 && is_string($mime) && preg_match($regexp, $mime, $matches))
1231 {
1232 $this->file_type = $matches[1];
1233 return;
1234 }
1235 }
1236
Andrey Andreeve9d2dc82012-11-07 14:23:29 +02001237 if ( (bool) @ini_get('safe_mode') === FALSE && function_usable('shell_exec'))
Andrey Andreeva49e3812011-12-09 13:05:22 +02001238 {
1239 $mime = @shell_exec($cmd);
1240 if (strlen($mime) > 0)
1241 {
1242 $mime = explode("\n", trim($mime));
1243 if (preg_match($regexp, $mime[(count($mime) - 1)], $matches))
1244 {
1245 $this->file_type = $matches[1];
1246 return;
1247 }
1248 }
1249 }
1250
Andrey Andreeve9d2dc82012-11-07 14:23:29 +02001251 if (function_usable('popen'))
Andrey Andreeva49e3812011-12-09 13:05:22 +02001252 {
1253 $proc = @popen($cmd, 'r');
1254 if (is_resource($proc))
1255 {
tubalmartin010f1f42012-03-03 22:24:31 +01001256 $mime = @fread($proc, 512);
Andrey Andreeva49e3812011-12-09 13:05:22 +02001257 @pclose($proc);
1258 if ($mime !== FALSE)
1259 {
1260 $mime = explode("\n", trim($mime));
1261 if (preg_match($regexp, $mime[(count($mime) - 1)], $matches))
1262 {
1263 $this->file_type = $matches[1];
1264 return;
1265 }
1266 }
1267 }
1268 }
1269 }
1270
1271 // Fall back to the deprecated mime_content_type(), if available (still better than $_FILES[$field]['type'])
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001272 if (function_exists('mime_content_type'))
1273 {
1274 $this->file_type = @mime_content_type($file['tmp_name']);
Andrey Andreeva49e3812011-12-09 13:05:22 +02001275 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 +03001276 {
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001277 return;
1278 }
1279 }
1280
1281 $this->file_type = $file['type'];
1282 }
1283
Derek Allard2067d1a2008-11-13 22:59:24 +00001284}
Derek Allard2067d1a2008-11-13 22:59:24 +00001285
1286/* End of file Upload.php */
Andrey Andreev56454792012-05-17 14:32:19 +03001287/* Location: ./system/libraries/Upload.php */