blob: 7946111cc042ba000c0baa2be67d1b4c026f162d [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
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, 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 Andreev2cf4c9b2014-02-21 15:01:48 +020045 public $max_size = 0;
Andrey Andreevf5ccd122012-11-02 00:17:39 +020046
47 /**
48 * Maximum image width
49 *
50 * @var int
51 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +020052 public $max_width = 0;
Andrey Andreevf5ccd122012-11-02 00:17:39 +020053
54 /**
55 * Maximum image height
56 *
57 * @var int
58 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +020059 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 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +020066 public $min_width = 0;
Andrey Andreev05aa2d62012-12-03 16:06:55 +020067
68 /**
69 * Minimum image height
70 *
71 * @var int
72 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +020073 public $min_height = 0;
Andrey Andreev05aa2d62012-12-03 16:06:55 +020074
75 /**
Andrey Andreevf5ccd122012-11-02 00:17:39 +020076 * Maximum filename length
77 *
78 * @var int
79 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +020080 public $max_filename = 0;
Andrey Andreevf5ccd122012-11-02 00:17:39 +020081
82 /**
83 * Maximum duplicate filename increment ID
84 *
85 * @var int
86 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +020087 public $max_filename_increment = 100;
Andrey Andreevf5ccd122012-11-02 00:17:39 +020088
89 /**
90 * Allowed file types
91 *
92 * @var string
93 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +020094 public $allowed_types = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +020095
96 /**
97 * Temporary filename
98 *
99 * @var string
100 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200101 public $file_temp = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200102
103 /**
104 * Filename
105 *
106 * @var string
107 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200108 public $file_name = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200109
110 /**
111 * Original filename
112 *
113 * @var string
114 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200115 public $orig_name = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200116
117 /**
118 * File type
119 *
120 * @var string
121 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200122 public $file_type = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200123
124 /**
125 * File size
126 *
127 * @var int
128 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200129 public $file_size = NULL;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200130
131 /**
132 * Filename extension
133 *
134 * @var string
135 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200136 public $file_ext = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200137
138 /**
Adrianf496d132013-06-24 15:56:45 +0200139 * Force filename extension to lowercase
140 *
141 * @var string
142 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200143 public $file_ext_tolower = FALSE;
Adrianf496d132013-06-24 15:56:45 +0200144
145 /**
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200146 * Upload path
147 *
148 * @var string
149 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200150 public $upload_path = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200151
152 /**
153 * Overwrite flag
154 *
155 * @var bool
156 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200157 public $overwrite = FALSE;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200158
159 /**
160 * Obfuscate filename flag
161 *
162 * @var bool
163 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200164 public $encrypt_name = FALSE;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200165
166 /**
167 * Is image flag
168 *
169 * @var bool
170 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200171 public $is_image = FALSE;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200172
173 /**
174 * Image width
175 *
176 * @var int
177 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200178 public $image_width = NULL;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200179
180 /**
181 * Image height
182 *
183 * @var int
184 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200185 public $image_height = NULL;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200186
187 /**
188 * Image type
189 *
190 * @var string
191 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200192 public $image_type = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200193
194 /**
195 * Image size string
196 *
197 * @var string
198 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200199 public $image_size_str = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200200
201 /**
202 * Error messages list
203 *
204 * @var array
205 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200206 public $error_msg = array();
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200207
208 /**
209 * Remove spaces flag
210 *
211 * @var bool
212 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200213 public $remove_spaces = TRUE;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200214
215 /**
216 * MIME detection flag
217 *
218 * @var bool
219 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200220 public $detect_mime = TRUE;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200221
222 /**
223 * XSS filter flag
224 *
225 * @var bool
226 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200227 public $xss_clean = FALSE;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200228
229 /**
Andrey Andreev32c72122013-10-21 15:35:05 +0300230 * Apache mod_mime fix flag
231 *
232 * @var bool
233 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200234 public $mod_mime_fix = TRUE;
Andrey Andreev32c72122013-10-21 15:35:05 +0300235
236 /**
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200237 * Temporary filename prefix
238 *
239 * @var string
240 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200241 public $temp_prefix = 'temp_file_';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200242
243 /**
244 * Filename sent by the client
245 *
246 * @var bool
247 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200248 public $client_name = '';
Barry Mienydd671972010-10-04 16:33:58 +0200249
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200250 // --------------------------------------------------------------------
251
252 /**
253 * Filename override
254 *
255 * @var string
256 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200257 protected $_file_name_override = '';
258
259 /**
260 * MIME types list
261 *
262 * @var array
263 */
264 protected $_mimes = array();
Barry Mienydd671972010-10-04 16:33:58 +0200265
Andrey Andreev119d8a72014-01-08 15:27:53 +0200266 /**
267 * CI Singleton
268 *
269 * @var object
270 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200271 protected $_CI;
Andrey Andreev119d8a72014-01-08 15:27:53 +0200272
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200273 // --------------------------------------------------------------------
274
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 /**
276 * Constructor
277 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200278 * @param array $props
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300279 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200281 public function __construct($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200283 empty($config) OR $this->initialize($config, FALSE);
Barry Mienydd671972010-10-04 16:33:58 +0200284
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200285 $this->_mimes =& get_mimes();
286 $this->_CI =& get_instance();
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300287
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300288 log_message('debug', 'Upload Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 }
Barry Mienydd671972010-10-04 16:33:58 +0200290
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200292
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 /**
294 * Initialize preferences
295 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200296 * @param array $config
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200297 * @param bool $reset
298 * @return CI_Upload
Barry Mienydd671972010-10-04 16:33:58 +0200299 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200300 public function initialize(array $config = array(), $reset = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200302 $reflection = new ReflectionClass($this);
Barry Mienydd671972010-10-04 16:33:58 +0200303
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200304 if ($reset === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200306 $defaults = $reflection->getDefaultProperties();
307 foreach (array_keys($defaults) as $key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200309 if ($key[0] === '_')
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200311 continue;
312 }
313
314 if (isset($config[$key]))
315 {
316 if ($reflection->hasMethod('set_'.$key))
317 {
318 $this->{'set_'.$key}($config[$key]);
319 }
320 else
321 {
322 $this->$key = $config[$key];
323 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 }
325 else
326 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200327 $this->$key = $defaults[$key];
Barry Mienydd671972010-10-04 16:33:58 +0200328 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 }
Joseba Juániza8027ff2014-08-06 20:03:25 +0200330
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200331 }
Joseba Juániza8027ff2014-08-06 20:03:25 +0200332 else
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200333 {
Joseba Juániza8027ff2014-08-06 20:03:25 +0200334
335 foreach ($config as $key => &$value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 {
Joseba Juániza8027ff2014-08-06 20:03:25 +0200337 if ($key[0] !== '_' && $reflection->hasProperty($key))
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200338 {
Joseba Juániza8027ff2014-08-06 20:03:25 +0200339 if ($reflection->hasMethod('set_'.$key))
340 {
341 $this->{'set_'.$key}($value);
342 }
343 else
344 {
345 $this->$key = $value;
346 }
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200347 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 }
Joseba Juániza8027ff2014-08-06 20:03:25 +0200349
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 }
Barry Mienydd671972010-10-04 16:33:58 +0200351
Derek Jonese9d723f2010-07-12 10:10:59 -0500352 // if a file_name was provided in the config, use it instead of the user input
353 // supplied file name for all uploads until initialized again
354 $this->_file_name_override = $this->file_name;
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200355 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 }
Barry Mienydd671972010-10-04 16:33:58 +0200357
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200359
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 /**
361 * Perform the file upload
362 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200363 * @param string $field
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200365 */
Greg Aker58fdee82010-11-10 15:07:09 -0600366 public function do_upload($field = 'userfile')
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300368 // Is $_FILES[$field] set? If not, no reason to continue.
Andrey Andreeve7d017b2014-02-25 12:23:34 +0200369 if (isset($_FILES[$field]))
370 {
371 $_file = $_FILES[$field];
372 }
373 // Does the field name contain array notation?
374 elseif (($c = preg_match_all('/(?:^[^\[]+)|\[[^]]*\]/', $field, $matches)) > 1)
375 {
376 $_file = $_FILES;
377 for ($i = 0; $i < $c; $i++)
378 {
379 // We can't track numeric iterations, only full field names are accepted
380 if (($field = trim($matches[0][$i], '[]')) === '' OR ! isset($_file[$field]))
381 {
382 $_file = NULL;
383 break;
384 }
385
386 $_file = $_file[$field];
387 }
388 }
389
390 if ( ! isset($_file))
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 {
392 $this->set_error('upload_no_file_selected');
393 return FALSE;
394 }
Barry Mienydd671972010-10-04 16:33:58 +0200395
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 // Is the upload path valid?
397 if ( ! $this->validate_upload_path())
398 {
399 // errors will already be set by validate_upload_path() so just return FALSE
400 return FALSE;
401 }
402
403 // Was the file able to be uploaded? If not, determine the reason why.
Andrey Andreeve7d017b2014-02-25 12:23:34 +0200404 if ( ! is_uploaded_file($_file['tmp_name']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 {
Andrey Andreeve7d017b2014-02-25 12:23:34 +0200406 $error = isset($_file['error']) ? $_file['error'] : 4;
Derek Allard2067d1a2008-11-13 22:59:24 +0000407
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300408 switch ($error)
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 {
Darren Benneye123a602013-03-30 18:17:54 +0000410 case UPLOAD_ERR_INI_SIZE:
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 $this->set_error('upload_file_exceeds_limit');
412 break;
Darren Benneye123a602013-03-30 18:17:54 +0000413 case UPLOAD_ERR_FORM_SIZE:
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 $this->set_error('upload_file_exceeds_form_limit');
415 break;
Darren Benneye123a602013-03-30 18:17:54 +0000416 case UPLOAD_ERR_PARTIAL:
Barry Mienydd671972010-10-04 16:33:58 +0200417 $this->set_error('upload_file_partial');
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 break;
Darren Benneye123a602013-03-30 18:17:54 +0000419 case UPLOAD_ERR_NO_FILE:
Barry Mienydd671972010-10-04 16:33:58 +0200420 $this->set_error('upload_no_file_selected');
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 break;
Darren Benneye123a602013-03-30 18:17:54 +0000422 case UPLOAD_ERR_NO_TMP_DIR:
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 $this->set_error('upload_no_temp_directory');
424 break;
Darren Benneye123a602013-03-30 18:17:54 +0000425 case UPLOAD_ERR_CANT_WRITE:
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 $this->set_error('upload_unable_to_write_file');
427 break;
Darren Benneye123a602013-03-30 18:17:54 +0000428 case UPLOAD_ERR_EXTENSION:
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 $this->set_error('upload_stopped_by_extension');
430 break;
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300431 default:
432 $this->set_error('upload_no_file_selected');
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 break;
434 }
435
436 return FALSE;
437 }
438
439 // Set the uploaded data as class variables
Andrey Andreeve7d017b2014-02-25 12:23:34 +0200440 $this->file_temp = $_file['tmp_name'];
441 $this->file_size = $_file['size'];
Andrey Andreevd60e7002012-06-17 00:03:03 +0300442
443 // Skip MIME type detection?
444 if ($this->detect_mime !== FALSE)
445 {
Andrey Andreeve7d017b2014-02-25 12:23:34 +0200446 $this->_file_mime_type($_file);
Andrey Andreevd60e7002012-06-17 00:03:03 +0300447 }
448
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300449 $this->file_type = preg_replace('/^(.+?);.*$/', '\\1', $this->file_type);
Derek Jones616fb022010-04-22 16:52:18 -0500450 $this->file_type = strtolower(trim(stripslashes($this->file_type), '"'));
Andrey Andreeve7d017b2014-02-25 12:23:34 +0200451 $this->file_name = $this->_prep_filename($_file['name']);
Derek Jonese9d723f2010-07-12 10:10:59 -0500452 $this->file_ext = $this->get_extension($this->file_name);
453 $this->client_name = $this->file_name;
Barry Mienydd671972010-10-04 16:33:58 +0200454
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 // Is the file type allowed to be uploaded?
456 if ( ! $this->is_allowed_filetype())
457 {
458 $this->set_error('upload_invalid_filetype');
459 return FALSE;
460 }
461
Derek Jonese9d723f2010-07-12 10:10:59 -0500462 // if we're overriding, let's now make sure the new name and type is allowed
Alex Bilbied261b1e2012-06-02 11:12:16 +0100463 if ($this->_file_name_override !== '')
Derek Jonese9d723f2010-07-12 10:10:59 -0500464 {
465 $this->file_name = $this->_prep_filename($this->_file_name_override);
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000466
467 // If no extension was provided in the file_name config item, use the uploaded one
Pascal Kriete14287f32011-02-14 13:39:34 -0500468 if (strpos($this->_file_name_override, '.') === FALSE)
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000469 {
470 $this->file_name .= $this->file_ext;
471 }
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000472 else
473 {
vlakoff35672462013-02-15 01:36:04 +0100474 // An extension was provided, let's have it!
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300475 $this->file_ext = $this->get_extension($this->_file_name_override);
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000476 }
Derek Jonese9d723f2010-07-12 10:10:59 -0500477
478 if ( ! $this->is_allowed_filetype(TRUE))
479 {
480 $this->set_error('upload_invalid_filetype');
Barry Mienydd671972010-10-04 16:33:58 +0200481 return FALSE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500482 }
483 }
Barry Mienydd671972010-10-04 16:33:58 +0200484
Derek Jonese9d723f2010-07-12 10:10:59 -0500485 // Convert the file size to kilobytes
486 if ($this->file_size > 0)
487 {
488 $this->file_size = round($this->file_size/1024, 2);
489 }
490
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 // Is the file size within the allowed maximum?
492 if ( ! $this->is_allowed_filesize())
493 {
494 $this->set_error('upload_invalid_filesize');
495 return FALSE;
496 }
497
498 // Are the image dimensions within the allowed size?
vlakoffc941d852013-08-06 14:44:40 +0200499 // Note: This can fail if the server has an open_basedir restriction.
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 if ( ! $this->is_allowed_dimensions())
501 {
502 $this->set_error('upload_invalid_dimensions');
503 return FALSE;
504 }
505
506 // Sanitize the file name for security
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200507 $this->file_name = $this->_CI->security->sanitize_filename($this->file_name);
Barry Mienydd671972010-10-04 16:33:58 +0200508
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 // Truncate the file name if it's too long
510 if ($this->max_filename > 0)
511 {
512 $this->file_name = $this->limit_filename_length($this->file_name, $this->max_filename);
513 }
514
515 // Remove white spaces in the name
Alex Bilbied261b1e2012-06-02 11:12:16 +0100516 if ($this->remove_spaces === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300518 $this->file_name = preg_replace('/\s+/', '_', $this->file_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 }
520
521 /*
522 * Validate the file name
523 * This function appends an number onto the end of
524 * the file if one with the same name already exists.
525 * If it returns false there was a problem.
526 */
527 $this->orig_name = $this->file_name;
528
Alex Bilbied261b1e2012-06-02 11:12:16 +0100529 if ($this->overwrite === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 {
531 $this->file_name = $this->set_filename($this->upload_path, $this->file_name);
Barry Mienydd671972010-10-04 16:33:58 +0200532
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 if ($this->file_name === FALSE)
534 {
535 return FALSE;
536 }
537 }
538
539 /*
Derek Jonese9d723f2010-07-12 10:10:59 -0500540 * Run the file through the XSS hacking filter
541 * This helps prevent malicious code from being
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300542 * embedded within a file. Scripts can easily
Derek Jonese9d723f2010-07-12 10:10:59 -0500543 * be disguised as images or other file types.
544 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300545 if ($this->xss_clean && $this->do_xss_clean() === FALSE)
Derek Jonese9d723f2010-07-12 10:10:59 -0500546 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300547 $this->set_error('upload_unable_to_write_file');
548 return FALSE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500549 }
550
551 /*
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 * Move the file to the final destination
553 * To deal with different server configurations
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300554 * we'll attempt to use copy() first. If that fails
555 * we'll use move_uploaded_file(). One of the two should
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 * reliably work in most environments
557 */
558 if ( ! @copy($this->file_temp, $this->upload_path.$this->file_name))
559 {
560 if ( ! @move_uploaded_file($this->file_temp, $this->upload_path.$this->file_name))
561 {
Barry Mienydd671972010-10-04 16:33:58 +0200562 $this->set_error('upload_destination_error');
563 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 }
565 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000566
567 /*
568 * Set the finalized image dimensions
569 * This sets the image width/height (assuming the
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300570 * file was an image). We use this information
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 * in the "data" function.
572 */
573 $this->set_image_properties($this->upload_path.$this->file_name);
574
575 return TRUE;
576 }
Barry Mienydd671972010-10-04 16:33:58 +0200577
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200579
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 /**
581 * Finalized Data Array
Barry Mienydd671972010-10-04 16:33:58 +0200582 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 * Returns an associative array containing all of the information
584 * related to the upload, allowing the developer easy access in one array.
585 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200586 * @param string $index
Michiel Vugteveen0ee287e2012-06-11 10:38:14 +0200587 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200588 */
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200589 public function data($index = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 {
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200591 $data = array(
Michiel Vugteveen3a7fb042012-06-11 09:29:16 +0200592 'file_name' => $this->file_name,
593 'file_type' => $this->file_type,
594 'file_path' => $this->upload_path,
595 'full_path' => $this->upload_path.$this->file_name,
596 'raw_name' => str_replace($this->file_ext, '', $this->file_name),
597 'orig_name' => $this->orig_name,
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300598 'client_name' => $this->client_name,
Michiel Vugteveen3a7fb042012-06-11 09:29:16 +0200599 'file_ext' => $this->file_ext,
600 'file_size' => $this->file_size,
601 'is_image' => $this->is_image(),
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300602 'image_width' => $this->image_width,
603 'image_height' => $this->image_height,
604 'image_type' => $this->image_type,
605 'image_size_str' => $this->image_size_str,
606 );
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200607
Michiel Vugteveen46a04292012-06-11 10:37:52 +0200608 if ( ! empty($index))
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200609 {
Michiel Vugteveen46a04292012-06-11 10:37:52 +0200610 return isset($data[$index]) ? $data[$index] : NULL;
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200611 }
612
Michiel Vugteveen46a04292012-06-11 10:37:52 +0200613 return $data;
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 }
Barry Mienydd671972010-10-04 16:33:58 +0200615
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200617
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 /**
619 * Set Upload Path
620 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200621 * @param string $path
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200622 * @return CI_Upload
Barry Mienydd671972010-10-04 16:33:58 +0200623 */
Greg Aker58fdee82010-11-10 15:07:09 -0600624 public function set_upload_path($path)
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 {
626 // Make sure it has a trailing slash
627 $this->upload_path = rtrim($path, '/').'/';
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200628 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 }
Barry Mienydd671972010-10-04 16:33:58 +0200630
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200632
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 /**
634 * Set the file name
635 *
636 * This function takes a filename/path as input and looks for the
637 * existence of a file with the same name. If found, it will append a
638 * number to the end of the filename to avoid overwriting a pre-existing file.
639 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200640 * @param string $path
641 * @param string $filename
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200643 */
Greg Aker58fdee82010-11-10 15:07:09 -0600644 public function set_filename($path, $filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000645 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100646 if ($this->encrypt_name === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200647 {
Barry Mienydd671972010-10-04 16:33:58 +0200648 $filename = md5(uniqid(mt_rand())).$this->file_ext;
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 }
Barry Mienydd671972010-10-04 16:33:58 +0200650
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 if ( ! file_exists($path.$filename))
652 {
653 return $filename;
654 }
Barry Mienydd671972010-10-04 16:33:58 +0200655
Derek Allard2067d1a2008-11-13 22:59:24 +0000656 $filename = str_replace($this->file_ext, '', $filename);
Barry Mienydd671972010-10-04 16:33:58 +0200657
Derek Allard2067d1a2008-11-13 22:59:24 +0000658 $new_filename = '';
Adam Jackettccbbea12011-08-21 16:19:11 -0400659 for ($i = 1; $i < $this->max_filename_increment; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200660 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 if ( ! file_exists($path.$filename.$i.$this->file_ext))
662 {
663 $new_filename = $filename.$i.$this->file_ext;
664 break;
665 }
666 }
667
Alex Bilbied261b1e2012-06-02 11:12:16 +0100668 if ($new_filename === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000669 {
670 $this->set_error('upload_bad_filename');
671 return FALSE;
672 }
673 else
674 {
675 return $new_filename;
676 }
677 }
Barry Mienydd671972010-10-04 16:33:58 +0200678
Derek Allard2067d1a2008-11-13 22:59:24 +0000679 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200680
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 /**
682 * Set Maximum File Size
683 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200684 * @param int $n
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200685 * @return CI_Upload
Barry Mienydd671972010-10-04 16:33:58 +0200686 */
Greg Aker58fdee82010-11-10 15:07:09 -0600687 public function set_max_filesize($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200689 $this->max_size = ($n < 0) ? 0 : (int) $n;
690 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000691 }
Barry Mienydd671972010-10-04 16:33:58 +0200692
Derek Allard2067d1a2008-11-13 22:59:24 +0000693 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200694
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 /**
696 * Set Maximum File Name Length
697 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200698 * @param int $n
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200699 * @return CI_Upload
Barry Mienydd671972010-10-04 16:33:58 +0200700 */
Greg Aker58fdee82010-11-10 15:07:09 -0600701 public function set_max_filename($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200703 $this->max_filename = ($n < 0) ? 0 : (int) $n;
704 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000705 }
706
707 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200708
Derek Allard2067d1a2008-11-13 22:59:24 +0000709 /**
710 * Set Maximum Image Width
711 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200712 * @param int $n
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200713 * @return CI_Upload
Barry Mienydd671972010-10-04 16:33:58 +0200714 */
Greg Aker58fdee82010-11-10 15:07:09 -0600715 public function set_max_width($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000716 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200717 $this->max_width = ($n < 0) ? 0 : (int) $n;
718 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000719 }
Barry Mienydd671972010-10-04 16:33:58 +0200720
Derek Allard2067d1a2008-11-13 22:59:24 +0000721 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200722
Derek Allard2067d1a2008-11-13 22:59:24 +0000723 /**
724 * Set Maximum Image Height
725 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200726 * @param int $n
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200727 * @return CI_Upload
Barry Mienydd671972010-10-04 16:33:58 +0200728 */
Greg Aker58fdee82010-11-10 15:07:09 -0600729 public function set_max_height($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000730 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200731 $this->max_height = ($n < 0) ? 0 : (int) $n;
732 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000733 }
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 /**
Andrey Andreev05aa2d62012-12-03 16:06:55 +0200738 * Set minimum image width
739 *
740 * @param int $n
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200741 * @return CI_Upload
Andrey Andreev05aa2d62012-12-03 16:06:55 +0200742 */
743 public function set_min_width($n)
744 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200745 $this->min_width = ($n < 0) ? 0 : (int) $n;
746 return $this;
Andrey Andreev05aa2d62012-12-03 16:06:55 +0200747 }
748
749 // --------------------------------------------------------------------
750
751 /**
752 * Set minimum image height
753 *
754 * @param int $n
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200755 * @return CI_Upload
Andrey Andreev05aa2d62012-12-03 16:06:55 +0200756 */
757 public function set_min_height($n)
758 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200759 $this->min_height = ($n < 0) ? 0 : (int) $n;
760 return $this;
Andrey Andreev05aa2d62012-12-03 16:06:55 +0200761 }
762
763 // --------------------------------------------------------------------
764
765 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000766 * Set Allowed File Types
767 *
Andrey Andreev82179bf2014-02-22 00:29:53 +0200768 * @param mixed $types
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200769 * @return CI_Upload
Barry Mienydd671972010-10-04 16:33:58 +0200770 */
Greg Aker58fdee82010-11-10 15:07:09 -0600771 public function set_allowed_types($types)
Derek Allard2067d1a2008-11-13 22:59:24 +0000772 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200773 $this->allowed_types = (is_array($types) OR $types === '*')
774 ? $types
775 : explode('|', $types);
776 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000777 }
Barry Mienydd671972010-10-04 16:33:58 +0200778
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200780
Derek Allard2067d1a2008-11-13 22:59:24 +0000781 /**
782 * Set Image Properties
783 *
784 * Uses GD to determine the width/height/type of image
785 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200786 * @param string $path
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200787 * @return CI_Upload
Barry Mienydd671972010-10-04 16:33:58 +0200788 */
Greg Aker58fdee82010-11-10 15:07:09 -0600789 public function set_image_properties($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000790 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200791 if ($this->is_image() && function_exists('getimagesize'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 {
793 if (FALSE !== ($D = @getimagesize($path)))
Barry Mienydd671972010-10-04 16:33:58 +0200794 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
796
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300797 $this->image_width = $D[0];
798 $this->image_height = $D[1];
799 $this->image_type = isset($types[$D[2]]) ? $types[$D[2]] : 'unknown';
800 $this->image_size_str = $D[3]; // string containing height and width
Derek Allard2067d1a2008-11-13 22:59:24 +0000801 }
802 }
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200803
804 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000805 }
Barry Mienydd671972010-10-04 16:33:58 +0200806
Derek Allard2067d1a2008-11-13 22:59:24 +0000807 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200808
Derek Allard2067d1a2008-11-13 22:59:24 +0000809 /**
810 * Set XSS Clean
811 *
812 * Enables the XSS flag so that the file that was uploaded
813 * will be run through the XSS filter.
814 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200815 * @param bool $flag
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200816 * @return CI_Upload
Derek Allard2067d1a2008-11-13 22:59:24 +0000817 */
Greg Aker58fdee82010-11-10 15:07:09 -0600818 public function set_xss_clean($flag = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000819 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100820 $this->xss_clean = ($flag === TRUE);
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200821 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000822 }
Barry Mienydd671972010-10-04 16:33:58 +0200823
Derek Allard2067d1a2008-11-13 22:59:24 +0000824 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200825
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 /**
827 * Validate the image
828 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000829 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200830 */
Greg Aker58fdee82010-11-10 15:07:09 -0600831 public function is_image()
Derek Allard2067d1a2008-11-13 22:59:24 +0000832 {
833 // IE will sometimes return odd mime-types during upload, so here we just standardize all
834 // jpegs or pngs to the same file type.
835
Derek Jones4b9c6292011-07-01 17:40:48 -0500836 $png_mimes = array('image/x-png');
Derek Allard2067d1a2008-11-13 22:59:24 +0000837 $jpeg_mimes = array('image/jpg', 'image/jpe', 'image/jpeg', 'image/pjpeg');
Barry Mienydd671972010-10-04 16:33:58 +0200838
Derek Allard2067d1a2008-11-13 22:59:24 +0000839 if (in_array($this->file_type, $png_mimes))
840 {
841 $this->file_type = 'image/png';
842 }
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300843 elseif (in_array($this->file_type, $jpeg_mimes))
Derek Allard2067d1a2008-11-13 22:59:24 +0000844 {
845 $this->file_type = 'image/jpeg';
846 }
847
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300848 $img_mimes = array('image/gif', 'image/jpeg', 'image/png');
Derek Allard2067d1a2008-11-13 22:59:24 +0000849
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300850 return in_array($this->file_type, $img_mimes, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 }
Barry Mienydd671972010-10-04 16:33:58 +0200852
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200854
Derek Allard2067d1a2008-11-13 22:59:24 +0000855 /**
856 * Verify that the filetype is allowed
857 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200858 * @param bool $ignore_mime
Derek Allard2067d1a2008-11-13 22:59:24 +0000859 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200860 */
Greg Aker58fdee82010-11-10 15:07:09 -0600861 public function is_allowed_filetype($ignore_mime = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000862 {
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200863 if ($this->allowed_types === '*')
Derek Jonese12f64e2010-03-02 22:55:08 -0600864 {
865 return TRUE;
866 }
Barry Mienydd671972010-10-04 16:33:58 +0200867
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200868 if (empty($this->allowed_types) OR ! is_array($this->allowed_types))
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 {
870 $this->set_error('upload_no_file_types');
871 return FALSE;
872 }
Barry Mienydd671972010-10-04 16:33:58 +0200873
Derek Jonese9d723f2010-07-12 10:10:59 -0500874 $ext = strtolower(ltrim($this->file_ext, '.'));
Barry Mienydd671972010-10-04 16:33:58 +0200875
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200876 if ( ! in_array($ext, $this->allowed_types, TRUE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000877 {
Derek Jonese9d723f2010-07-12 10:10:59 -0500878 return FALSE;
879 }
Derek Jonesafa282f2009-02-10 17:11:52 +0000880
Barry Mienydd671972010-10-04 16:33:58 +0200881 // Images get some additional checks
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200882 if (in_array($ext, array('gif', 'jpg', 'jpeg', 'jpe', 'png'), TRUE) && @getimagesize($this->file_temp) === FALSE)
Derek Jonese9d723f2010-07-12 10:10:59 -0500883 {
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200884 return FALSE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500885 }
Barry Mienydd671972010-10-04 16:33:58 +0200886
Derek Jonese9d723f2010-07-12 10:10:59 -0500887 if ($ignore_mime === TRUE)
888 {
889 return TRUE;
890 }
Barry Mienydd671972010-10-04 16:33:58 +0200891
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200892 if (isset($this->_mimes[$ext]))
Derek Jonese9d723f2010-07-12 10:10:59 -0500893 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200894 return is_array($this->_mimes[$ext])
895 ? in_array($this->file_type, $this->_mimes[$ext], TRUE)
896 : ($this->_mimes[$ext] === $this->file_type);
Derek Allard2067d1a2008-11-13 22:59:24 +0000897 }
Barry Mienydd671972010-10-04 16:33:58 +0200898
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 return FALSE;
900 }
Barry Mienydd671972010-10-04 16:33:58 +0200901
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200903
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 /**
905 * Verify that the file is within the allowed size
906 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000907 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200908 */
Greg Aker58fdee82010-11-10 15:07:09 -0600909 public function is_allowed_filesize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000910 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100911 return ($this->max_size === 0 OR $this->max_size > $this->file_size);
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 }
Barry Mienydd671972010-10-04 16:33:58 +0200913
Derek Allard2067d1a2008-11-13 22:59:24 +0000914 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200915
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 /**
917 * Verify that the image is within the allowed width/height
918 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000919 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200920 */
Greg Aker58fdee82010-11-10 15:07:09 -0600921 public function is_allowed_dimensions()
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 {
923 if ( ! $this->is_image())
924 {
925 return TRUE;
926 }
927
928 if (function_exists('getimagesize'))
929 {
930 $D = @getimagesize($this->file_temp);
931
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300932 if ($this->max_width > 0 && $D[0] > $this->max_width)
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 {
934 return FALSE;
935 }
936
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300937 if ($this->max_height > 0 && $D[1] > $this->max_height)
Derek Allard2067d1a2008-11-13 22:59:24 +0000938 {
939 return FALSE;
940 }
Andrey Andreev05aa2d62012-12-03 16:06:55 +0200941
942 if ($this->min_width > 0 && $D[0] < $this->min_width)
943 {
944 return FALSE;
945 }
946
947 if ($this->min_height > 0 && $D[1] < $this->min_height)
948 {
949 return FALSE;
950 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 }
952
953 return TRUE;
954 }
Barry Mienydd671972010-10-04 16:33:58 +0200955
Derek Allard2067d1a2008-11-13 22:59:24 +0000956 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200957
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 /**
959 * Validate Upload Path
960 *
961 * Verifies that it is a valid upload path with proper permissions.
962 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200964 */
Greg Aker58fdee82010-11-10 15:07:09 -0600965 public function validate_upload_path()
Derek Allard2067d1a2008-11-13 22:59:24 +0000966 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100967 if ($this->upload_path === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000968 {
969 $this->set_error('upload_no_filepath');
970 return FALSE;
971 }
Barry Mienydd671972010-10-04 16:33:58 +0200972
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200973 if (realpath($this->upload_path) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000974 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300975 $this->upload_path = str_replace('\\', '/', realpath($this->upload_path));
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 }
977
Andrey Andreev382b5132014-02-26 18:41:59 +0200978 if ( ! is_dir($this->upload_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000979 {
980 $this->set_error('upload_no_filepath');
981 return FALSE;
982 }
983
984 if ( ! is_really_writable($this->upload_path))
985 {
986 $this->set_error('upload_not_writable');
987 return FALSE;
988 }
989
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300990 $this->upload_path = preg_replace('/(.+?)\/*$/', '\\1/', $this->upload_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000991 return TRUE;
992 }
Barry Mienydd671972010-10-04 16:33:58 +0200993
Derek Allard2067d1a2008-11-13 22:59:24 +0000994 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200995
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 /**
997 * Extract the file extension
998 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200999 * @param string $filename
Derek Allard2067d1a2008-11-13 22:59:24 +00001000 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001001 */
Greg Aker58fdee82010-11-10 15:07:09 -06001002 public function get_extension($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +00001003 {
1004 $x = explode('.', $filename);
Adrianf496d132013-06-24 15:56:45 +02001005
Adrian74a228b2013-06-25 12:09:22 +02001006 if (count($x) === 1)
1007 {
1008 return '';
1009 }
1010
1011 $ext = ($this->file_ext_tolower) ? strtolower(end($x)) : end($x);
1012 return '.'.$ext;
Barry Mienydd671972010-10-04 16:33:58 +02001013 }
1014
Derek Allard2067d1a2008-11-13 22:59:24 +00001015 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001016
Derek Allard2067d1a2008-11-13 22:59:24 +00001017 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001018 * Limit the File Name Length
1019 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001020 * @param string $filename
1021 * @param int $length
Derek Allard2067d1a2008-11-13 22:59:24 +00001022 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001023 */
Greg Aker58fdee82010-11-10 15:07:09 -06001024 public function limit_filename_length($filename, $length)
Derek Allard2067d1a2008-11-13 22:59:24 +00001025 {
1026 if (strlen($filename) < $length)
1027 {
1028 return $filename;
1029 }
Barry Mienydd671972010-10-04 16:33:58 +02001030
Derek Allard2067d1a2008-11-13 22:59:24 +00001031 $ext = '';
1032 if (strpos($filename, '.') !== FALSE)
1033 {
1034 $parts = explode('.', $filename);
1035 $ext = '.'.array_pop($parts);
1036 $filename = implode('.', $parts);
1037 }
Barry Mienydd671972010-10-04 16:33:58 +02001038
Derek Allard2067d1a2008-11-13 22:59:24 +00001039 return substr($filename, 0, ($length - strlen($ext))).$ext;
1040 }
1041
1042 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001043
Derek Allard2067d1a2008-11-13 22:59:24 +00001044 /**
1045 * Runs the file through the XSS clean function
1046 *
1047 * This prevents people from embedding malicious code in their files.
1048 * I'm not sure that it won't negatively affect certain files in unexpected ways,
1049 * but so far I haven't found that it causes trouble.
1050 *
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +02001051 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001052 */
Greg Aker58fdee82010-11-10 15:07:09 -06001053 public function do_xss_clean()
Barry Mienydd671972010-10-04 16:33:58 +02001054 {
Derek Jonese9d723f2010-07-12 10:10:59 -05001055 $file = $this->file_temp;
Barry Mienydd671972010-10-04 16:33:58 +02001056
Andrey Andreev5036c9c2012-06-04 15:34:56 +03001057 if (filesize($file) == 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001058 {
1059 return FALSE;
1060 }
Barry Mienydd671972010-10-04 16:33:58 +02001061
Andrey Andreevc839d282012-06-07 14:35:27 +03001062 if (memory_get_usage() && ($memory_limit = ini_get('memory_limit')))
Greg Akerf82e51c2010-04-14 19:33:50 -05001063 {
Andrey Andreevc839d282012-06-07 14:35:27 +03001064 $memory_limit *= 1024 * 1024;
Barry Mienydd671972010-10-04 16:33:58 +02001065
Greg Akerc78a2592010-06-09 11:45:32 -05001066 // There was a bug/behavioural change in PHP 5.2, where numbers over one million get output
Andrey Andreev8e6f7a92012-03-26 20:13:00 +03001067 // into scientific notation. number_format() ensures this number is an integer
Greg Akerc78a2592010-06-09 11:45:32 -05001068 // http://bugs.php.net/bug.php?id=43053
Barry Mienydd671972010-10-04 16:33:58 +02001069
Andrey Andreevc839d282012-06-07 14:35:27 +03001070 $memory_limit = number_format(ceil(filesize($file) + $memory_limit), 0, '.', '');
Barry Mienydd671972010-10-04 16:33:58 +02001071
Andrey Andreevc839d282012-06-07 14:35:27 +03001072 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 -05001073 }
1074
1075 // If the file being uploaded is an image, then we should have no problem with XSS attacks (in theory), but
1076 // 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 +03001077 // using IE who looks at the image. It does this by inspecting the first 255 bytes of an image. To get around this
1078 // 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 +02001079 // 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 -05001080 // attempted XSS attack.
1081
1082 if (function_exists('getimagesize') && @getimagesize($file) !== FALSE)
1083 {
Barry Mienydd671972010-10-04 16:33:58 +02001084 if (($file = @fopen($file, 'rb')) === FALSE) // "b" to force binary
1085 {
Greg Akerf82e51c2010-04-14 19:33:50 -05001086 return FALSE; // Couldn't open the file, return FALSE
Barry Mienydd671972010-10-04 16:33:58 +02001087 }
Greg Akerf82e51c2010-04-14 19:33:50 -05001088
Barry Mienydd671972010-10-04 16:33:58 +02001089 $opening_bytes = fread($file, 256);
1090 fclose($file);
Greg Akerf82e51c2010-04-14 19:33:50 -05001091
1092 // These are known to throw IE into mime-type detection chaos
1093 // <a, <body, <head, <html, <img, <plaintext, <pre, <script, <table, <title
1094 // title is basically just in SVG, but we filter it anyhow
1095
vlakoff35672462013-02-15 01:36:04 +01001096 // if it's an image or no "triggers" detected in the first 256 bytes - we're good
Andrey Andreevc839d282012-06-07 14:35:27 +03001097 return ! preg_match('/<(a|body|head|html|img|plaintext|pre|script|table|title)[\s>]/i', $opening_bytes);
Greg Akerf82e51c2010-04-14 19:33:50 -05001098 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001099
1100 if (($data = @file_get_contents($file)) === FALSE)
1101 {
1102 return FALSE;
1103 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001104
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +02001105 return $this->_CI->security->xss_clean($data, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001106 }
Barry Mienydd671972010-10-04 16:33:58 +02001107
Derek Allard2067d1a2008-11-13 22:59:24 +00001108 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001109
Derek Allard2067d1a2008-11-13 22:59:24 +00001110 /**
1111 * Set an error message
1112 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001113 * @param string $msg
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +02001114 * @return CI_Upload
Barry Mienydd671972010-10-04 16:33:58 +02001115 */
Greg Aker58fdee82010-11-10 15:07:09 -06001116 public function set_error($msg)
Derek Allard2067d1a2008-11-13 22:59:24 +00001117 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +02001118 $this->_CI->lang->load('upload');
Barry Mienydd671972010-10-04 16:33:58 +02001119
Andrey Andreev119d8a72014-01-08 15:27:53 +02001120 is_array($msg) OR $msg = array($msg);
Darren Benney4a8d1902013-03-30 20:07:49 +00001121
Darren Benney38d5f4b2013-03-30 21:00:38 +00001122 foreach ($msg as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001123 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +02001124 $msg = ($this->_CI->lang->line($val) === FALSE) ? $val : $this->_CI->lang->line($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001125 $this->error_msg[] = $msg;
1126 log_message('error', $msg);
1127 }
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +02001128
1129 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +00001130 }
Barry Mienydd671972010-10-04 16:33:58 +02001131
Derek Allard2067d1a2008-11-13 22:59:24 +00001132 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001133
Derek Allard2067d1a2008-11-13 22:59:24 +00001134 /**
1135 * Display the error message
1136 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001137 * @param string $open
1138 * @param string $close
Derek Allard2067d1a2008-11-13 22:59:24 +00001139 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001140 */
Greg Aker58fdee82010-11-10 15:07:09 -06001141 public function display_errors($open = '<p>', $close = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +00001142 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +03001143 return (count($this->error_msg) > 0) ? $open.implode($close.$open, $this->error_msg).$close : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001144 }
Barry Mienydd671972010-10-04 16:33:58 +02001145
Derek Allard2067d1a2008-11-13 22:59:24 +00001146 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001147
Derek Allard2067d1a2008-11-13 22:59:24 +00001148 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001149 * Prep Filename
1150 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001151 * Prevents possible script execution from Apache's handling
1152 * of files' multiple extensions.
Derek Allard2067d1a2008-11-13 22:59:24 +00001153 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001154 * @link http://httpd.apache.org/docs/1.3/mod/mod_mime.html#multipleext
1155 *
1156 * @param string $filename
Derek Allard2067d1a2008-11-13 22:59:24 +00001157 * @return string
1158 */
Greg Aker58fdee82010-11-10 15:07:09 -06001159 protected function _prep_filename($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +00001160 {
Andrey Andreev32c72122013-10-21 15:35:05 +03001161 if ($this->mod_mime_fix === FALSE OR $this->allowed_types === '*' OR strpos($filename, '.') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001162 {
1163 return $filename;
1164 }
Derek Allard616dab82009-02-16 15:44:32 +00001165
Derek Allard2067d1a2008-11-13 22:59:24 +00001166 $parts = explode('.', $filename);
1167 $ext = array_pop($parts);
1168 $filename = array_shift($parts);
Derek Allard616dab82009-02-16 15:44:32 +00001169
Derek Allard2067d1a2008-11-13 22:59:24 +00001170 foreach ($parts as $part)
1171 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +02001172 if ( ! in_array(strtolower($part), $this->allowed_types) OR ! isset($this->_mimes[strtolower($part)]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001173 {
1174 $filename .= '.'.$part.'_';
1175 }
1176 else
1177 {
1178 $filename .= '.'.$part;
1179 }
1180 }
Derek Allardd70b0642009-02-16 13:51:42 +00001181
Andrey Andreev8e6f7a92012-03-26 20:13:00 +03001182 return $filename.'.'.$ext;
Derek Allard2067d1a2008-11-13 22:59:24 +00001183 }
1184
1185 // --------------------------------------------------------------------
1186
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001187 /**
1188 * File MIME type
1189 *
1190 * Detects the (actual) MIME type of the uploaded file, if possible.
1191 * The input array is expected to be $_FILES[$field]
1192 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001193 * @param array $file
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001194 * @return void
1195 */
1196 protected function _file_mime_type($file)
1197 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001198 // We'll need this to validate the MIME info string (e.g. text/plain; charset=us-ascii)
Andrey Andreevf7aed122011-12-13 11:01:06 +02001199 $regexp = '/^([a-z\-]+\/[a-z0-9\-\.\+]+)(;\s.+)?$/';
Andrey Andreeva49e3812011-12-09 13:05:22 +02001200
1201 /* Fileinfo extension - most reliable method
1202 *
1203 * Unfortunately, prior to PHP 5.3 - it's only available as a PECL extension and the
1204 * more convenient FILEINFO_MIME_TYPE flag doesn't exist.
1205 */
1206 if (function_exists('finfo_file'))
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001207 {
Andrey Andreevd60e7002012-06-17 00:03:03 +03001208 $finfo = @finfo_open(FILEINFO_MIME);
Andrey Andreeva49e3812011-12-09 13:05:22 +02001209 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 +03001210 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001211 $mime = @finfo_file($finfo, $file['tmp_name']);
1212 finfo_close($finfo);
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001213
1214 /* According to the comments section of the PHP manual page,
1215 * it is possible that this function returns an empty string
Andrey Andreev2cec39f2011-09-24 22:59:37 +03001216 * for some files (e.g. if they don't exist in the magic MIME database)
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001217 */
Andrey Andreeva49e3812011-12-09 13:05:22 +02001218 if (is_string($mime) && preg_match($regexp, $mime, $matches))
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001219 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001220 $this->file_type = $matches[1];
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001221 return;
1222 }
1223 }
1224 }
1225
Andrey Andreeva49e3812011-12-09 13:05:22 +02001226 /* This is an ugly hack, but UNIX-type systems provide a "native" way to detect the file type,
1227 * which is still more secure than depending on the value of $_FILES[$field]['type'], and as it
1228 * was reported in issue #750 (https://github.com/EllisLab/CodeIgniter/issues/750) - it's better
1229 * than mime_content_type() as well, hence the attempts to try calling the command line with
1230 * three different functions.
Andrey Andreev6700b932011-09-24 14:25:33 +03001231 *
1232 * Notes:
Andrey Andreev59654312011-12-02 14:28:54 +02001233 * - the DIRECTORY_SEPARATOR comparison ensures that we're not on a Windows system
Andrey Andreeva49e3812011-12-09 13:05:22 +02001234 * - many system admins would disable the exec(), shell_exec(), popen() and similar functions
vlakofff3994252013-02-19 01:45:23 +01001235 * due to security concerns, hence the function_usable() checks
Andrey Andreev6700b932011-09-24 14:25:33 +03001236 */
Andrey Andreeva49e3812011-12-09 13:05:22 +02001237 if (DIRECTORY_SEPARATOR !== '\\')
Andrey Andreev6700b932011-09-24 14:25:33 +03001238 {
Andrey Andreevd60e7002012-06-17 00:03:03 +03001239 $cmd = function_exists('escapeshellarg')
1240 ? 'file --brief --mime '.escapeshellarg($file['tmp_name']).' 2>&1'
1241 : 'file --brief --mime '.$file['tmp_name'].' 2>&1';
Andrey Andreeva49e3812011-12-09 13:05:22 +02001242
Andrey Andreeve9d2dc82012-11-07 14:23:29 +02001243 if (function_usable('exec'))
Andrey Andreev6700b932011-09-24 14:25:33 +03001244 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001245 /* 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 +01001246 * 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 +02001247 * anything that could already be set for $mime previously. This effectively makes the second parameter a dummy
1248 * value, which is only put to allow us to get the return status code.
1249 */
1250 $mime = @exec($cmd, $mime, $return_status);
1251 if ($return_status === 0 && is_string($mime) && preg_match($regexp, $mime, $matches))
1252 {
1253 $this->file_type = $matches[1];
1254 return;
1255 }
1256 }
1257
Andrey Andreevf6274742014-02-20 18:05:58 +02001258 if ( ! ini_get('safe_mode') && function_usable('shell_exec'))
Andrey Andreeva49e3812011-12-09 13:05:22 +02001259 {
1260 $mime = @shell_exec($cmd);
1261 if (strlen($mime) > 0)
1262 {
1263 $mime = explode("\n", trim($mime));
1264 if (preg_match($regexp, $mime[(count($mime) - 1)], $matches))
1265 {
1266 $this->file_type = $matches[1];
1267 return;
1268 }
1269 }
1270 }
1271
Andrey Andreeve9d2dc82012-11-07 14:23:29 +02001272 if (function_usable('popen'))
Andrey Andreeva49e3812011-12-09 13:05:22 +02001273 {
1274 $proc = @popen($cmd, 'r');
1275 if (is_resource($proc))
1276 {
tubalmartin010f1f42012-03-03 22:24:31 +01001277 $mime = @fread($proc, 512);
Andrey Andreeva49e3812011-12-09 13:05:22 +02001278 @pclose($proc);
1279 if ($mime !== FALSE)
1280 {
1281 $mime = explode("\n", trim($mime));
1282 if (preg_match($regexp, $mime[(count($mime) - 1)], $matches))
1283 {
1284 $this->file_type = $matches[1];
1285 return;
1286 }
1287 }
1288 }
1289 }
1290 }
1291
1292 // Fall back to the deprecated mime_content_type(), if available (still better than $_FILES[$field]['type'])
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001293 if (function_exists('mime_content_type'))
1294 {
1295 $this->file_type = @mime_content_type($file['tmp_name']);
Andrey Andreeva49e3812011-12-09 13:05:22 +02001296 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 +03001297 {
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001298 return;
1299 }
1300 }
1301
1302 $this->file_type = $file['type'];
1303 }
1304
Derek Allard2067d1a2008-11-13 22:59:24 +00001305}
Derek Allard2067d1a2008-11-13 22:59:24 +00001306
1307/* End of file Upload.php */
Adrian74a228b2013-06-25 12:09:22 +02001308/* Location: ./system/libraries/Upload.php */