blob: 82b46f0941a1ed57d8bada3a340de027b66bd43a [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 {
369 case 1: // UPLOAD_ERR_INI_SIZE
370 $this->set_error('upload_file_exceeds_limit');
371 break;
372 case 2: // UPLOAD_ERR_FORM_SIZE
373 $this->set_error('upload_file_exceeds_form_limit');
374 break;
375 case 3: // 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;
378 case 4: // 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;
381 case 6: // UPLOAD_ERR_NO_TMP_DIR
382 $this->set_error('upload_no_temp_directory');
383 break;
384 case 7: // UPLOAD_ERR_CANT_WRITE
385 $this->set_error('upload_unable_to_write_file');
386 break;
387 case 8: // UPLOAD_ERR_EXTENSION
388 $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 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 mt_srand();
Barry Mienydd671972010-10-04 16:33:58 +0200608 $filename = md5(uniqid(mt_rand())).$this->file_ext;
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 }
Barry Mienydd671972010-10-04 16:33:58 +0200610
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 if ( ! file_exists($path.$filename))
612 {
613 return $filename;
614 }
Barry Mienydd671972010-10-04 16:33:58 +0200615
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 $filename = str_replace($this->file_ext, '', $filename);
Barry Mienydd671972010-10-04 16:33:58 +0200617
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 $new_filename = '';
Adam Jackettccbbea12011-08-21 16:19:11 -0400619 for ($i = 1; $i < $this->max_filename_increment; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200620 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 if ( ! file_exists($path.$filename.$i.$this->file_ext))
622 {
623 $new_filename = $filename.$i.$this->file_ext;
624 break;
625 }
626 }
627
Alex Bilbied261b1e2012-06-02 11:12:16 +0100628 if ($new_filename === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 {
630 $this->set_error('upload_bad_filename');
631 return FALSE;
632 }
633 else
634 {
635 return $new_filename;
636 }
637 }
Barry Mienydd671972010-10-04 16:33:58 +0200638
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200640
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 /**
642 * Set Maximum File Size
643 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200644 * @param int $n
Derek Allard2067d1a2008-11-13 22:59:24 +0000645 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200646 */
Greg Aker58fdee82010-11-10 15:07:09 -0600647 public function set_max_filesize($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300649 $this->max_size = ((int) $n < 0) ? 0 : (int) $n;
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 }
Barry Mienydd671972010-10-04 16:33:58 +0200651
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200653
Derek Allard2067d1a2008-11-13 22:59:24 +0000654 /**
655 * Set Maximum File Name Length
656 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200657 * @param int $n
Derek Allard2067d1a2008-11-13 22:59:24 +0000658 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200659 */
Greg Aker58fdee82010-11-10 15:07:09 -0600660 public function set_max_filename($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300662 $this->max_filename = ((int) $n < 0) ? 0 : (int) $n;
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 }
664
665 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200666
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 /**
668 * Set Maximum Image Width
669 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200670 * @param int $n
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200672 */
Greg Aker58fdee82010-11-10 15:07:09 -0600673 public function set_max_width($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300675 $this->max_width = ((int) $n < 0) ? 0 : (int) $n;
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 }
Barry Mienydd671972010-10-04 16:33:58 +0200677
Derek Allard2067d1a2008-11-13 22:59:24 +0000678 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200679
Derek Allard2067d1a2008-11-13 22:59:24 +0000680 /**
681 * Set Maximum Image Height
682 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200683 * @param int $n
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200685 */
Greg Aker58fdee82010-11-10 15:07:09 -0600686 public function set_max_height($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300688 $this->max_height = ((int) $n < 0) ? 0 : (int) $n;
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 }
Barry Mienydd671972010-10-04 16:33:58 +0200690
Derek Allard2067d1a2008-11-13 22:59:24 +0000691 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200692
Derek Allard2067d1a2008-11-13 22:59:24 +0000693 /**
Andrey Andreev05aa2d62012-12-03 16:06:55 +0200694 * Set minimum image width
695 *
696 * @param int $n
697 * @return void
698 */
699 public function set_min_width($n)
700 {
701 $this->min_width = ((int) $n < 0) ? 0 : (int) $n;
702 }
703
704 // --------------------------------------------------------------------
705
706 /**
707 * Set minimum image height
708 *
709 * @param int $n
710 * @return void
711 */
712 public function set_min_height($n)
713 {
714 $this->min_height = ((int) $n < 0) ? 0 : (int) $n;
715 }
716
717 // --------------------------------------------------------------------
718
719 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000720 * Set Allowed File Types
721 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200722 * @param string $types
Derek Allard2067d1a2008-11-13 22:59:24 +0000723 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200724 */
Greg Aker58fdee82010-11-10 15:07:09 -0600725 public function set_allowed_types($types)
Derek Allard2067d1a2008-11-13 22:59:24 +0000726 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300727 if ( ! is_array($types) && $types === '*')
Derek Jonese12f64e2010-03-02 22:55:08 -0600728 {
729 $this->allowed_types = '*';
730 return;
731 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000732 $this->allowed_types = explode('|', $types);
733 }
Barry Mienydd671972010-10-04 16:33:58 +0200734
Derek Allard2067d1a2008-11-13 22:59:24 +0000735 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200736
Derek Allard2067d1a2008-11-13 22:59:24 +0000737 /**
738 * Set Image Properties
739 *
740 * Uses GD to determine the width/height/type of image
741 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200742 * @param string $path
Derek Allard2067d1a2008-11-13 22:59:24 +0000743 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200744 */
Greg Aker58fdee82010-11-10 15:07:09 -0600745 public function set_image_properties($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 {
747 if ( ! $this->is_image())
748 {
749 return;
750 }
751
752 if (function_exists('getimagesize'))
753 {
754 if (FALSE !== ($D = @getimagesize($path)))
Barry Mienydd671972010-10-04 16:33:58 +0200755 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000756 $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
757
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300758 $this->image_width = $D[0];
759 $this->image_height = $D[1];
760 $this->image_type = isset($types[$D[2]]) ? $types[$D[2]] : 'unknown';
761 $this->image_size_str = $D[3]; // string containing height and width
Derek Allard2067d1a2008-11-13 22:59:24 +0000762 }
763 }
764 }
Barry Mienydd671972010-10-04 16:33:58 +0200765
Derek Allard2067d1a2008-11-13 22:59:24 +0000766 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200767
Derek Allard2067d1a2008-11-13 22:59:24 +0000768 /**
769 * Set XSS Clean
770 *
771 * Enables the XSS flag so that the file that was uploaded
772 * will be run through the XSS filter.
773 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200774 * @param bool $flag
Derek Allard2067d1a2008-11-13 22:59:24 +0000775 * @return void
776 */
Greg Aker58fdee82010-11-10 15:07:09 -0600777 public function set_xss_clean($flag = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000778 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100779 $this->xss_clean = ($flag === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000780 }
Barry Mienydd671972010-10-04 16:33:58 +0200781
Derek Allard2067d1a2008-11-13 22:59:24 +0000782 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200783
Derek Allard2067d1a2008-11-13 22:59:24 +0000784 /**
785 * Validate the image
786 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000787 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200788 */
Greg Aker58fdee82010-11-10 15:07:09 -0600789 public function is_image()
Derek Allard2067d1a2008-11-13 22:59:24 +0000790 {
791 // IE will sometimes return odd mime-types during upload, so here we just standardize all
792 // jpegs or pngs to the same file type.
793
Derek Jones4b9c6292011-07-01 17:40:48 -0500794 $png_mimes = array('image/x-png');
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 $jpeg_mimes = array('image/jpg', 'image/jpe', 'image/jpeg', 'image/pjpeg');
Barry Mienydd671972010-10-04 16:33:58 +0200796
Derek Allard2067d1a2008-11-13 22:59:24 +0000797 if (in_array($this->file_type, $png_mimes))
798 {
799 $this->file_type = 'image/png';
800 }
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300801 elseif (in_array($this->file_type, $jpeg_mimes))
Derek Allard2067d1a2008-11-13 22:59:24 +0000802 {
803 $this->file_type = 'image/jpeg';
804 }
805
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300806 $img_mimes = array('image/gif', 'image/jpeg', 'image/png');
Derek Allard2067d1a2008-11-13 22:59:24 +0000807
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300808 return in_array($this->file_type, $img_mimes, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000809 }
Barry Mienydd671972010-10-04 16:33:58 +0200810
Derek Allard2067d1a2008-11-13 22:59:24 +0000811 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200812
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 /**
814 * Verify that the filetype is allowed
815 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200816 * @param bool $ignore_mime
Derek Allard2067d1a2008-11-13 22:59:24 +0000817 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200818 */
Greg Aker58fdee82010-11-10 15:07:09 -0600819 public function is_allowed_filetype($ignore_mime = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 {
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200821 if ($this->allowed_types === '*')
Derek Jonese12f64e2010-03-02 22:55:08 -0600822 {
823 return TRUE;
824 }
Barry Mienydd671972010-10-04 16:33:58 +0200825
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200826 if ( ! is_array($this->allowed_types) OR count($this->allowed_types) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000827 {
828 $this->set_error('upload_no_file_types');
829 return FALSE;
830 }
Barry Mienydd671972010-10-04 16:33:58 +0200831
Derek Jonese9d723f2010-07-12 10:10:59 -0500832 $ext = strtolower(ltrim($this->file_ext, '.'));
Barry Mienydd671972010-10-04 16:33:58 +0200833
Derek Jonese9d723f2010-07-12 10:10:59 -0500834 if ( ! in_array($ext, $this->allowed_types))
Derek Allard2067d1a2008-11-13 22:59:24 +0000835 {
Derek Jonese9d723f2010-07-12 10:10:59 -0500836 return FALSE;
837 }
Derek Jonesafa282f2009-02-10 17:11:52 +0000838
Barry Mienydd671972010-10-04 16:33:58 +0200839 // Images get some additional checks
Derek Jonese9d723f2010-07-12 10:10:59 -0500840 $image_types = array('gif', 'jpg', 'jpeg', 'png', 'jpe');
Barry Mienydd671972010-10-04 16:33:58 +0200841
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200842 if (in_array($ext, $image_types) && @getimagesize($this->file_temp) === FALSE)
Derek Jonese9d723f2010-07-12 10:10:59 -0500843 {
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200844 return FALSE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500845 }
Barry Mienydd671972010-10-04 16:33:58 +0200846
Derek Jonese9d723f2010-07-12 10:10:59 -0500847 if ($ignore_mime === TRUE)
848 {
849 return TRUE;
850 }
Barry Mienydd671972010-10-04 16:33:58 +0200851
Derek Jonese9d723f2010-07-12 10:10:59 -0500852 $mime = $this->mimes_types($ext);
Barry Mienydd671972010-10-04 16:33:58 +0200853
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300854 if (is_array($mime) && in_array($this->file_type, $mime, TRUE))
Derek Jonese9d723f2010-07-12 10:10:59 -0500855 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300856 return TRUE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500857 }
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200858 elseif ($mime === $this->file_type)
Derek Jonese9d723f2010-07-12 10:10:59 -0500859 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300860 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 }
Barry Mienydd671972010-10-04 16:33:58 +0200862
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 return FALSE;
864 }
Barry Mienydd671972010-10-04 16:33:58 +0200865
Derek Allard2067d1a2008-11-13 22:59:24 +0000866 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200867
Derek Allard2067d1a2008-11-13 22:59:24 +0000868 /**
869 * Verify that the file is within the allowed size
870 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000871 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200872 */
Greg Aker58fdee82010-11-10 15:07:09 -0600873 public function is_allowed_filesize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100875 return ($this->max_size === 0 OR $this->max_size > $this->file_size);
Derek Allard2067d1a2008-11-13 22:59:24 +0000876 }
Barry Mienydd671972010-10-04 16:33:58 +0200877
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200879
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 /**
881 * Verify that the image is within the allowed width/height
882 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000883 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200884 */
Greg Aker58fdee82010-11-10 15:07:09 -0600885 public function is_allowed_dimensions()
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 {
887 if ( ! $this->is_image())
888 {
889 return TRUE;
890 }
891
892 if (function_exists('getimagesize'))
893 {
894 $D = @getimagesize($this->file_temp);
895
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300896 if ($this->max_width > 0 && $D[0] > $this->max_width)
Derek Allard2067d1a2008-11-13 22:59:24 +0000897 {
898 return FALSE;
899 }
900
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300901 if ($this->max_height > 0 && $D[1] > $this->max_height)
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 {
903 return FALSE;
904 }
Andrey Andreev05aa2d62012-12-03 16:06:55 +0200905
906 if ($this->min_width > 0 && $D[0] < $this->min_width)
907 {
908 return FALSE;
909 }
910
911 if ($this->min_height > 0 && $D[1] < $this->min_height)
912 {
913 return FALSE;
914 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000915 }
916
917 return TRUE;
918 }
Barry Mienydd671972010-10-04 16:33:58 +0200919
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 * Validate Upload Path
924 *
925 * Verifies that it is a valid upload path with proper permissions.
926 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200928 */
Greg Aker58fdee82010-11-10 15:07:09 -0600929 public function validate_upload_path()
Derek Allard2067d1a2008-11-13 22:59:24 +0000930 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100931 if ($this->upload_path === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000932 {
933 $this->set_error('upload_no_filepath');
934 return FALSE;
935 }
Barry Mienydd671972010-10-04 16:33:58 +0200936
Andrey Andreevc839d282012-06-07 14:35:27 +0300937 if (@realpath($this->upload_path) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000938 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300939 $this->upload_path = str_replace('\\', '/', realpath($this->upload_path));
Derek Allard2067d1a2008-11-13 22:59:24 +0000940 }
941
942 if ( ! @is_dir($this->upload_path))
943 {
944 $this->set_error('upload_no_filepath');
945 return FALSE;
946 }
947
948 if ( ! is_really_writable($this->upload_path))
949 {
950 $this->set_error('upload_not_writable');
951 return FALSE;
952 }
953
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300954 $this->upload_path = preg_replace('/(.+?)\/*$/', '\\1/', $this->upload_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000955 return TRUE;
956 }
Barry Mienydd671972010-10-04 16:33:58 +0200957
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200959
Derek Allard2067d1a2008-11-13 22:59:24 +0000960 /**
961 * Extract the file extension
962 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200963 * @param string $filename
Derek Allard2067d1a2008-11-13 22:59:24 +0000964 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200965 */
Greg Aker58fdee82010-11-10 15:07:09 -0600966 public function get_extension($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 {
968 $x = explode('.', $filename);
Andrey Andreev46d53fb2012-05-11 13:42:24 +0300969 return (count($x) !== 1) ? '.'.end($x) : '';
Barry Mienydd671972010-10-04 16:33:58 +0200970 }
971
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200973
Derek Allard2067d1a2008-11-13 22:59:24 +0000974 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000975 * Limit the File Name Length
976 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300977 * @param string $filename
978 * @param int $length
Derek Allard2067d1a2008-11-13 22:59:24 +0000979 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200980 */
Greg Aker58fdee82010-11-10 15:07:09 -0600981 public function limit_filename_length($filename, $length)
Derek Allard2067d1a2008-11-13 22:59:24 +0000982 {
983 if (strlen($filename) < $length)
984 {
985 return $filename;
986 }
Barry Mienydd671972010-10-04 16:33:58 +0200987
Derek Allard2067d1a2008-11-13 22:59:24 +0000988 $ext = '';
989 if (strpos($filename, '.') !== FALSE)
990 {
991 $parts = explode('.', $filename);
992 $ext = '.'.array_pop($parts);
993 $filename = implode('.', $parts);
994 }
Barry Mienydd671972010-10-04 16:33:58 +0200995
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 return substr($filename, 0, ($length - strlen($ext))).$ext;
997 }
998
999 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001000
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 /**
1002 * Runs the file through the XSS clean function
1003 *
1004 * This prevents people from embedding malicious code in their files.
1005 * I'm not sure that it won't negatively affect certain files in unexpected ways,
1006 * but so far I haven't found that it causes trouble.
1007 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001008 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001009 */
Greg Aker58fdee82010-11-10 15:07:09 -06001010 public function do_xss_clean()
Barry Mienydd671972010-10-04 16:33:58 +02001011 {
Derek Jonese9d723f2010-07-12 10:10:59 -05001012 $file = $this->file_temp;
Barry Mienydd671972010-10-04 16:33:58 +02001013
Andrey Andreev5036c9c2012-06-04 15:34:56 +03001014 if (filesize($file) == 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001015 {
1016 return FALSE;
1017 }
Barry Mienydd671972010-10-04 16:33:58 +02001018
Andrey Andreevc839d282012-06-07 14:35:27 +03001019 if (memory_get_usage() && ($memory_limit = ini_get('memory_limit')))
Greg Akerf82e51c2010-04-14 19:33:50 -05001020 {
Andrey Andreevc839d282012-06-07 14:35:27 +03001021 $memory_limit *= 1024 * 1024;
Barry Mienydd671972010-10-04 16:33:58 +02001022
Greg Akerc78a2592010-06-09 11:45:32 -05001023 // There was a bug/behavioural change in PHP 5.2, where numbers over one million get output
Andrey Andreev8e6f7a92012-03-26 20:13:00 +03001024 // into scientific notation. number_format() ensures this number is an integer
Greg Akerc78a2592010-06-09 11:45:32 -05001025 // http://bugs.php.net/bug.php?id=43053
Barry Mienydd671972010-10-04 16:33:58 +02001026
Andrey Andreevc839d282012-06-07 14:35:27 +03001027 $memory_limit = number_format(ceil(filesize($file) + $memory_limit), 0, '.', '');
Barry Mienydd671972010-10-04 16:33:58 +02001028
Andrey Andreevc839d282012-06-07 14:35:27 +03001029 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 -05001030 }
1031
1032 // If the file being uploaded is an image, then we should have no problem with XSS attacks (in theory), but
1033 // 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 +03001034 // using IE who looks at the image. It does this by inspecting the first 255 bytes of an image. To get around this
1035 // 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 +02001036 // 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 -05001037 // attempted XSS attack.
1038
1039 if (function_exists('getimagesize') && @getimagesize($file) !== FALSE)
1040 {
Barry Mienydd671972010-10-04 16:33:58 +02001041 if (($file = @fopen($file, 'rb')) === FALSE) // "b" to force binary
1042 {
Greg Akerf82e51c2010-04-14 19:33:50 -05001043 return FALSE; // Couldn't open the file, return FALSE
Barry Mienydd671972010-10-04 16:33:58 +02001044 }
Greg Akerf82e51c2010-04-14 19:33:50 -05001045
Barry Mienydd671972010-10-04 16:33:58 +02001046 $opening_bytes = fread($file, 256);
1047 fclose($file);
Greg Akerf82e51c2010-04-14 19:33:50 -05001048
1049 // These are known to throw IE into mime-type detection chaos
1050 // <a, <body, <head, <html, <img, <plaintext, <pre, <script, <table, <title
1051 // title is basically just in SVG, but we filter it anyhow
1052
vlakoff35672462013-02-15 01:36:04 +01001053 // if it's an image or no "triggers" detected in the first 256 bytes - we're good
Andrey Andreevc839d282012-06-07 14:35:27 +03001054 return ! preg_match('/<(a|body|head|html|img|plaintext|pre|script|table|title)[\s>]/i', $opening_bytes);
Greg Akerf82e51c2010-04-14 19:33:50 -05001055 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001056
1057 if (($data = @file_get_contents($file)) === FALSE)
1058 {
1059 return FALSE;
1060 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001061
Greg Akerf82e51c2010-04-14 19:33:50 -05001062 $CI =& get_instance();
Greg Akerf82e51c2010-04-14 19:33:50 -05001063 return $CI->security->xss_clean($data, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001064 }
Barry Mienydd671972010-10-04 16:33:58 +02001065
Derek Allard2067d1a2008-11-13 22:59:24 +00001066 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001067
Derek Allard2067d1a2008-11-13 22:59:24 +00001068 /**
1069 * Set an error message
1070 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001071 * @param string $msg
Derek Allard2067d1a2008-11-13 22:59:24 +00001072 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001073 */
Greg Aker58fdee82010-11-10 15:07:09 -06001074 public function set_error($msg)
Derek Allard2067d1a2008-11-13 22:59:24 +00001075 {
Barry Mienydd671972010-10-04 16:33:58 +02001076 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +00001077 $CI->lang->load('upload');
Barry Mienydd671972010-10-04 16:33:58 +02001078
Darren Benney4a8d1902013-03-30 20:07:49 +00001079 if ( ! is_array($msg))
Derek Allard2067d1a2008-11-13 22:59:24 +00001080 {
Darren Benney38d5f4b2013-03-30 21:00:38 +00001081 $msg = array($msg);
1082 }
Darren Benney4a8d1902013-03-30 20:07:49 +00001083
Darren Benney38d5f4b2013-03-30 21:00:38 +00001084 foreach ($msg as $val)
1085 {
1086 $msg = ($CI->lang->line($val) === FALSE) ? $val : $CI->lang->line($val);
1087 $this->error_msg[] = $msg;
1088 log_message('error', $msg);
1089 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001090 }
Barry Mienydd671972010-10-04 16:33:58 +02001091
Derek Allard2067d1a2008-11-13 22:59:24 +00001092 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001093
Derek Allard2067d1a2008-11-13 22:59:24 +00001094 /**
1095 * Display the error message
1096 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001097 * @param string $open
1098 * @param string $close
Derek Allard2067d1a2008-11-13 22:59:24 +00001099 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001100 */
Greg Aker58fdee82010-11-10 15:07:09 -06001101 public function display_errors($open = '<p>', $close = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +00001102 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +03001103 return (count($this->error_msg) > 0) ? $open.implode($close.$open, $this->error_msg).$close : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001104 }
Barry Mienydd671972010-10-04 16:33:58 +02001105
Derek Allard2067d1a2008-11-13 22:59:24 +00001106 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001107
Derek Allard2067d1a2008-11-13 22:59:24 +00001108 /**
1109 * List of Mime Types
1110 *
Andrey Andreev8e6f7a92012-03-26 20:13:00 +03001111 * This is a list of mime types. We use it to validate
Derek Allard2067d1a2008-11-13 22:59:24 +00001112 * the "allowed types" set by the developer
1113 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001114 * @param string $mime
Derek Allard2067d1a2008-11-13 22:59:24 +00001115 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001116 */
Greg Aker58fdee82010-11-10 15:07:09 -06001117 public function mimes_types($mime)
Derek Allard2067d1a2008-11-13 22:59:24 +00001118 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +03001119 return isset($this->mimes[$mime]) ? $this->mimes[$mime] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001120 }
1121
1122 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001123
Derek Allard2067d1a2008-11-13 22:59:24 +00001124 /**
1125 * Prep Filename
1126 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001127 * Prevents possible script execution from Apache's handling
1128 * of files' multiple extensions.
Derek Allard2067d1a2008-11-13 22:59:24 +00001129 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001130 * @link http://httpd.apache.org/docs/1.3/mod/mod_mime.html#multipleext
1131 *
1132 * @param string $filename
Derek Allard2067d1a2008-11-13 22:59:24 +00001133 * @return string
1134 */
Greg Aker58fdee82010-11-10 15:07:09 -06001135 protected function _prep_filename($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +00001136 {
Alex Bilbied261b1e2012-06-02 11:12:16 +01001137 if (strpos($filename, '.') === FALSE OR $this->allowed_types === '*')
Derek Allard2067d1a2008-11-13 22:59:24 +00001138 {
1139 return $filename;
1140 }
Derek Allard616dab82009-02-16 15:44:32 +00001141
Derek Allard2067d1a2008-11-13 22:59:24 +00001142 $parts = explode('.', $filename);
1143 $ext = array_pop($parts);
1144 $filename = array_shift($parts);
Derek Allard616dab82009-02-16 15:44:32 +00001145
Derek Allard2067d1a2008-11-13 22:59:24 +00001146 foreach ($parts as $part)
1147 {
Derek Jonese9d723f2010-07-12 10:10:59 -05001148 if ( ! in_array(strtolower($part), $this->allowed_types) OR $this->mimes_types(strtolower($part)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001149 {
1150 $filename .= '.'.$part.'_';
1151 }
1152 else
1153 {
1154 $filename .= '.'.$part;
1155 }
1156 }
Derek Allardd70b0642009-02-16 13:51:42 +00001157
Andrey Andreev8e6f7a92012-03-26 20:13:00 +03001158 return $filename.'.'.$ext;
Derek Allard2067d1a2008-11-13 22:59:24 +00001159 }
1160
1161 // --------------------------------------------------------------------
1162
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001163 /**
1164 * File MIME type
1165 *
1166 * Detects the (actual) MIME type of the uploaded file, if possible.
1167 * The input array is expected to be $_FILES[$field]
1168 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001169 * @param array $file
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001170 * @return void
1171 */
1172 protected function _file_mime_type($file)
1173 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001174 // We'll need this to validate the MIME info string (e.g. text/plain; charset=us-ascii)
Andrey Andreevf7aed122011-12-13 11:01:06 +02001175 $regexp = '/^([a-z\-]+\/[a-z0-9\-\.\+]+)(;\s.+)?$/';
Andrey Andreeva49e3812011-12-09 13:05:22 +02001176
1177 /* Fileinfo extension - most reliable method
1178 *
1179 * Unfortunately, prior to PHP 5.3 - it's only available as a PECL extension and the
1180 * more convenient FILEINFO_MIME_TYPE flag doesn't exist.
1181 */
1182 if (function_exists('finfo_file'))
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001183 {
Andrey Andreevd60e7002012-06-17 00:03:03 +03001184 $finfo = @finfo_open(FILEINFO_MIME);
Andrey Andreeva49e3812011-12-09 13:05:22 +02001185 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 +03001186 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001187 $mime = @finfo_file($finfo, $file['tmp_name']);
1188 finfo_close($finfo);
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001189
1190 /* According to the comments section of the PHP manual page,
1191 * it is possible that this function returns an empty string
Andrey Andreev2cec39f2011-09-24 22:59:37 +03001192 * for some files (e.g. if they don't exist in the magic MIME database)
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001193 */
Andrey Andreeva49e3812011-12-09 13:05:22 +02001194 if (is_string($mime) && preg_match($regexp, $mime, $matches))
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001195 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001196 $this->file_type = $matches[1];
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001197 return;
1198 }
1199 }
1200 }
1201
Andrey Andreeva49e3812011-12-09 13:05:22 +02001202 /* This is an ugly hack, but UNIX-type systems provide a "native" way to detect the file type,
1203 * which is still more secure than depending on the value of $_FILES[$field]['type'], and as it
1204 * was reported in issue #750 (https://github.com/EllisLab/CodeIgniter/issues/750) - it's better
1205 * than mime_content_type() as well, hence the attempts to try calling the command line with
1206 * three different functions.
Andrey Andreev6700b932011-09-24 14:25:33 +03001207 *
1208 * Notes:
Andrey Andreev59654312011-12-02 14:28:54 +02001209 * - the DIRECTORY_SEPARATOR comparison ensures that we're not on a Windows system
Andrey Andreeva49e3812011-12-09 13:05:22 +02001210 * - many system admins would disable the exec(), shell_exec(), popen() and similar functions
vlakofff3994252013-02-19 01:45:23 +01001211 * due to security concerns, hence the function_usable() checks
Andrey Andreev6700b932011-09-24 14:25:33 +03001212 */
Andrey Andreeva49e3812011-12-09 13:05:22 +02001213 if (DIRECTORY_SEPARATOR !== '\\')
Andrey Andreev6700b932011-09-24 14:25:33 +03001214 {
Andrey Andreevd60e7002012-06-17 00:03:03 +03001215 $cmd = function_exists('escapeshellarg')
1216 ? 'file --brief --mime '.escapeshellarg($file['tmp_name']).' 2>&1'
1217 : 'file --brief --mime '.$file['tmp_name'].' 2>&1';
Andrey Andreeva49e3812011-12-09 13:05:22 +02001218
Andrey Andreeve9d2dc82012-11-07 14:23:29 +02001219 if (function_usable('exec'))
Andrey Andreev6700b932011-09-24 14:25:33 +03001220 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001221 /* 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 +01001222 * 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 +02001223 * anything that could already be set for $mime previously. This effectively makes the second parameter a dummy
1224 * value, which is only put to allow us to get the return status code.
1225 */
1226 $mime = @exec($cmd, $mime, $return_status);
1227 if ($return_status === 0 && is_string($mime) && preg_match($regexp, $mime, $matches))
1228 {
1229 $this->file_type = $matches[1];
1230 return;
1231 }
1232 }
1233
Andrey Andreeve9d2dc82012-11-07 14:23:29 +02001234 if ( (bool) @ini_get('safe_mode') === FALSE && function_usable('shell_exec'))
Andrey Andreeva49e3812011-12-09 13:05:22 +02001235 {
1236 $mime = @shell_exec($cmd);
1237 if (strlen($mime) > 0)
1238 {
1239 $mime = explode("\n", trim($mime));
1240 if (preg_match($regexp, $mime[(count($mime) - 1)], $matches))
1241 {
1242 $this->file_type = $matches[1];
1243 return;
1244 }
1245 }
1246 }
1247
Andrey Andreeve9d2dc82012-11-07 14:23:29 +02001248 if (function_usable('popen'))
Andrey Andreeva49e3812011-12-09 13:05:22 +02001249 {
1250 $proc = @popen($cmd, 'r');
1251 if (is_resource($proc))
1252 {
tubalmartin010f1f42012-03-03 22:24:31 +01001253 $mime = @fread($proc, 512);
Andrey Andreeva49e3812011-12-09 13:05:22 +02001254 @pclose($proc);
1255 if ($mime !== FALSE)
1256 {
1257 $mime = explode("\n", trim($mime));
1258 if (preg_match($regexp, $mime[(count($mime) - 1)], $matches))
1259 {
1260 $this->file_type = $matches[1];
1261 return;
1262 }
1263 }
1264 }
1265 }
1266 }
1267
1268 // Fall back to the deprecated mime_content_type(), if available (still better than $_FILES[$field]['type'])
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001269 if (function_exists('mime_content_type'))
1270 {
1271 $this->file_type = @mime_content_type($file['tmp_name']);
Andrey Andreeva49e3812011-12-09 13:05:22 +02001272 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 +03001273 {
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001274 return;
1275 }
1276 }
1277
1278 $this->file_type = $file['type'];
1279 }
1280
Derek Allard2067d1a2008-11-13 22:59:24 +00001281}
Derek Allard2067d1a2008-11-13 22:59:24 +00001282
1283/* End of file Upload.php */
Andrey Andreev56454792012-05-17 14:32:19 +03001284/* Location: ./system/libraries/Upload.php */