blob: 51232f8a76d5c1757b191be41804f87dde40af2b [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev3a459572011-12-21 11:23:11 +02008 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02009 * Copyright (c) 2014 - 2015, British Columbia Institute of Technology
Andrey Andreev3a459572011-12-21 11:23:11 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevfe9309d2015-01-09 17:48:58 +020032 * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @since Version 1.0.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * File Uploading Class
42 *
43 * @package CodeIgniter
44 * @subpackage Libraries
45 * @category Uploads
Derek Jonesf4a4bd82011-10-20 12:18:42 -050046 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000047 * @link http://codeigniter.com/user_guide/libraries/file_uploading.html
48 */
49class CI_Upload {
Barry Mienydd671972010-10-04 16:33:58 +020050
Andrey Andreevf5ccd122012-11-02 00:17:39 +020051 /**
52 * Maximum file size
53 *
54 * @var int
55 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +020056 public $max_size = 0;
Andrey Andreevf5ccd122012-11-02 00:17:39 +020057
58 /**
59 * Maximum image width
60 *
61 * @var int
62 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +020063 public $max_width = 0;
Andrey Andreevf5ccd122012-11-02 00:17:39 +020064
65 /**
66 * Maximum image height
67 *
68 * @var int
69 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +020070 public $max_height = 0;
Andrey Andreevf5ccd122012-11-02 00:17:39 +020071
72 /**
Andrey Andreev05aa2d62012-12-03 16:06:55 +020073 * Minimum image width
74 *
75 * @var int
76 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +020077 public $min_width = 0;
Andrey Andreev05aa2d62012-12-03 16:06:55 +020078
79 /**
80 * Minimum image height
81 *
82 * @var int
83 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +020084 public $min_height = 0;
Andrey Andreev05aa2d62012-12-03 16:06:55 +020085
86 /**
Andrey Andreevf5ccd122012-11-02 00:17:39 +020087 * Maximum filename length
88 *
89 * @var int
90 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +020091 public $max_filename = 0;
Andrey Andreevf5ccd122012-11-02 00:17:39 +020092
93 /**
94 * Maximum duplicate filename increment ID
95 *
96 * @var int
97 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +020098 public $max_filename_increment = 100;
Andrey Andreevf5ccd122012-11-02 00:17:39 +020099
100 /**
101 * Allowed file types
102 *
103 * @var string
104 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200105 public $allowed_types = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200106
107 /**
108 * Temporary filename
109 *
110 * @var string
111 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200112 public $file_temp = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200113
114 /**
115 * Filename
116 *
117 * @var string
118 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200119 public $file_name = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200120
121 /**
122 * Original filename
123 *
124 * @var string
125 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200126 public $orig_name = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200127
128 /**
129 * File type
130 *
131 * @var string
132 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200133 public $file_type = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200134
135 /**
136 * File size
137 *
138 * @var int
139 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200140 public $file_size = NULL;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200141
142 /**
143 * Filename extension
144 *
145 * @var string
146 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200147 public $file_ext = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200148
149 /**
Adrianf496d132013-06-24 15:56:45 +0200150 * Force filename extension to lowercase
151 *
152 * @var string
153 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200154 public $file_ext_tolower = FALSE;
Adrianf496d132013-06-24 15:56:45 +0200155
156 /**
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200157 * Upload path
158 *
159 * @var string
160 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200161 public $upload_path = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200162
163 /**
164 * Overwrite flag
165 *
166 * @var bool
167 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200168 public $overwrite = FALSE;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200169
170 /**
171 * Obfuscate filename flag
172 *
173 * @var bool
174 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200175 public $encrypt_name = FALSE;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200176
177 /**
178 * Is image flag
179 *
180 * @var bool
181 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200182 public $is_image = FALSE;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200183
184 /**
185 * Image width
186 *
187 * @var int
188 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200189 public $image_width = NULL;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200190
191 /**
192 * Image height
193 *
194 * @var int
195 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200196 public $image_height = NULL;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200197
198 /**
199 * Image type
200 *
201 * @var string
202 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200203 public $image_type = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200204
205 /**
206 * Image size string
207 *
208 * @var string
209 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200210 public $image_size_str = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200211
212 /**
213 * Error messages list
214 *
215 * @var array
216 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200217 public $error_msg = array();
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200218
219 /**
220 * Remove spaces flag
221 *
222 * @var bool
223 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200224 public $remove_spaces = TRUE;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200225
226 /**
227 * MIME detection flag
228 *
229 * @var bool
230 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200231 public $detect_mime = TRUE;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200232
233 /**
234 * XSS filter flag
235 *
236 * @var bool
237 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200238 public $xss_clean = FALSE;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200239
240 /**
Andrey Andreev32c72122013-10-21 15:35:05 +0300241 * Apache mod_mime fix flag
242 *
243 * @var bool
244 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200245 public $mod_mime_fix = TRUE;
Andrey Andreev32c72122013-10-21 15:35:05 +0300246
247 /**
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200248 * Temporary filename prefix
249 *
250 * @var string
251 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200252 public $temp_prefix = 'temp_file_';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200253
254 /**
255 * Filename sent by the client
256 *
257 * @var bool
258 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200259 public $client_name = '';
Barry Mienydd671972010-10-04 16:33:58 +0200260
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200261 // --------------------------------------------------------------------
262
263 /**
264 * Filename override
265 *
266 * @var string
267 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200268 protected $_file_name_override = '';
269
270 /**
271 * MIME types list
272 *
273 * @var array
274 */
275 protected $_mimes = array();
Barry Mienydd671972010-10-04 16:33:58 +0200276
Andrey Andreev119d8a72014-01-08 15:27:53 +0200277 /**
278 * CI Singleton
279 *
280 * @var object
281 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200282 protected $_CI;
Andrey Andreev119d8a72014-01-08 15:27:53 +0200283
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200284 // --------------------------------------------------------------------
285
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 /**
287 * Constructor
288 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200289 * @param array $props
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300290 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200292 public function __construct($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200294 empty($config) OR $this->initialize($config, FALSE);
Barry Mienydd671972010-10-04 16:33:58 +0200295
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200296 $this->_mimes =& get_mimes();
297 $this->_CI =& get_instance();
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300298
Andrey Andreev90726b82015-01-20 12:39:22 +0200299 log_message('info', 'Upload Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 }
Barry Mienydd671972010-10-04 16:33:58 +0200301
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200303
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 /**
305 * Initialize preferences
306 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200307 * @param array $config
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200308 * @param bool $reset
309 * @return CI_Upload
Barry Mienydd671972010-10-04 16:33:58 +0200310 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200311 public function initialize(array $config = array(), $reset = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200313 $reflection = new ReflectionClass($this);
Barry Mienydd671972010-10-04 16:33:58 +0200314
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200315 if ($reset === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200317 $defaults = $reflection->getDefaultProperties();
318 foreach (array_keys($defaults) as $key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200320 if ($key[0] === '_')
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200322 continue;
323 }
324
325 if (isset($config[$key]))
326 {
327 if ($reflection->hasMethod('set_'.$key))
328 {
329 $this->{'set_'.$key}($config[$key]);
330 }
331 else
332 {
333 $this->$key = $config[$key];
334 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 }
336 else
337 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200338 $this->$key = $defaults[$key];
Barry Mienydd671972010-10-04 16:33:58 +0200339 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 }
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200341 }
Joseba Juániza8027ff2014-08-06 20:03:25 +0200342 else
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200343 {
Joseba Juániza8027ff2014-08-06 20:03:25 +0200344 foreach ($config as $key => &$value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 {
Joseba Juániza8027ff2014-08-06 20:03:25 +0200346 if ($key[0] !== '_' && $reflection->hasProperty($key))
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200347 {
Joseba Juániza8027ff2014-08-06 20:03:25 +0200348 if ($reflection->hasMethod('set_'.$key))
349 {
350 $this->{'set_'.$key}($value);
351 }
352 else
353 {
354 $this->$key = $value;
355 }
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200356 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 }
358 }
Barry Mienydd671972010-10-04 16:33:58 +0200359
Derek Jonese9d723f2010-07-12 10:10:59 -0500360 // if a file_name was provided in the config, use it instead of the user input
361 // supplied file name for all uploads until initialized again
362 $this->_file_name_override = $this->file_name;
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200363 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 }
Barry Mienydd671972010-10-04 16:33:58 +0200365
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200367
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 /**
369 * Perform the file upload
370 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200371 * @param string $field
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200373 */
Greg Aker58fdee82010-11-10 15:07:09 -0600374 public function do_upload($field = 'userfile')
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300376 // Is $_FILES[$field] set? If not, no reason to continue.
Andrey Andreeve7d017b2014-02-25 12:23:34 +0200377 if (isset($_FILES[$field]))
378 {
379 $_file = $_FILES[$field];
380 }
381 // Does the field name contain array notation?
382 elseif (($c = preg_match_all('/(?:^[^\[]+)|\[[^]]*\]/', $field, $matches)) > 1)
383 {
384 $_file = $_FILES;
385 for ($i = 0; $i < $c; $i++)
386 {
387 // We can't track numeric iterations, only full field names are accepted
388 if (($field = trim($matches[0][$i], '[]')) === '' OR ! isset($_file[$field]))
389 {
390 $_file = NULL;
391 break;
392 }
393
394 $_file = $_file[$field];
395 }
396 }
397
398 if ( ! isset($_file))
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 {
Andrey Andreevd5784082015-06-22 11:50:04 +0300400 $this->set_error('upload_no_file_selected', 'debug');
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 return FALSE;
402 }
Barry Mienydd671972010-10-04 16:33:58 +0200403
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 // Is the upload path valid?
405 if ( ! $this->validate_upload_path())
406 {
407 // errors will already be set by validate_upload_path() so just return FALSE
408 return FALSE;
409 }
410
411 // Was the file able to be uploaded? If not, determine the reason why.
Andrey Andreeve7d017b2014-02-25 12:23:34 +0200412 if ( ! is_uploaded_file($_file['tmp_name']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 {
Andrey Andreeve7d017b2014-02-25 12:23:34 +0200414 $error = isset($_file['error']) ? $_file['error'] : 4;
Derek Allard2067d1a2008-11-13 22:59:24 +0000415
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300416 switch ($error)
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 {
Darren Benneye123a602013-03-30 18:17:54 +0000418 case UPLOAD_ERR_INI_SIZE:
Andrey Andreevd5784082015-06-22 11:50:04 +0300419 $this->set_error('upload_file_exceeds_limit', 'info');
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 break;
Darren Benneye123a602013-03-30 18:17:54 +0000421 case UPLOAD_ERR_FORM_SIZE:
Andrey Andreevd5784082015-06-22 11:50:04 +0300422 $this->set_error('upload_file_exceeds_form_limit', 'info');
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 break;
Darren Benneye123a602013-03-30 18:17:54 +0000424 case UPLOAD_ERR_PARTIAL:
Andrey Andreevd5784082015-06-22 11:50:04 +0300425 $this->set_error('upload_file_partial', 'debug');
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 break;
Darren Benneye123a602013-03-30 18:17:54 +0000427 case UPLOAD_ERR_NO_FILE:
Andrey Andreevd5784082015-06-22 11:50:04 +0300428 $this->set_error('upload_no_file_selected', 'debug');
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 break;
Darren Benneye123a602013-03-30 18:17:54 +0000430 case UPLOAD_ERR_NO_TMP_DIR:
Andrey Andreevd5784082015-06-22 11:50:04 +0300431 $this->set_error('upload_no_temp_directory', 'error');
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 break;
Darren Benneye123a602013-03-30 18:17:54 +0000433 case UPLOAD_ERR_CANT_WRITE:
Andrey Andreevd5784082015-06-22 11:50:04 +0300434 $this->set_error('upload_unable_to_write_file', 'error');
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 break;
Darren Benneye123a602013-03-30 18:17:54 +0000436 case UPLOAD_ERR_EXTENSION:
Andrey Andreevd5784082015-06-22 11:50:04 +0300437 $this->set_error('upload_stopped_by_extension', 'debug');
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 break;
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300439 default:
Andrey Andreevd5784082015-06-22 11:50:04 +0300440 $this->set_error('upload_no_file_selected', 'debug');
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 break;
442 }
443
444 return FALSE;
445 }
446
447 // Set the uploaded data as class variables
Andrey Andreeve7d017b2014-02-25 12:23:34 +0200448 $this->file_temp = $_file['tmp_name'];
449 $this->file_size = $_file['size'];
Andrey Andreevd60e7002012-06-17 00:03:03 +0300450
451 // Skip MIME type detection?
452 if ($this->detect_mime !== FALSE)
453 {
Andrey Andreeve7d017b2014-02-25 12:23:34 +0200454 $this->_file_mime_type($_file);
Andrey Andreevd60e7002012-06-17 00:03:03 +0300455 }
456
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300457 $this->file_type = preg_replace('/^(.+?);.*$/', '\\1', $this->file_type);
Derek Jones616fb022010-04-22 16:52:18 -0500458 $this->file_type = strtolower(trim(stripslashes($this->file_type), '"'));
Andrey Andreeve7d017b2014-02-25 12:23:34 +0200459 $this->file_name = $this->_prep_filename($_file['name']);
Derek Jonese9d723f2010-07-12 10:10:59 -0500460 $this->file_ext = $this->get_extension($this->file_name);
461 $this->client_name = $this->file_name;
Barry Mienydd671972010-10-04 16:33:58 +0200462
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 // Is the file type allowed to be uploaded?
464 if ( ! $this->is_allowed_filetype())
465 {
Andrey Andreevd5784082015-06-22 11:50:04 +0300466 $this->set_error('upload_invalid_filetype', 'debug');
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 return FALSE;
468 }
469
Derek Jonese9d723f2010-07-12 10:10:59 -0500470 // if we're overriding, let's now make sure the new name and type is allowed
Alex Bilbied261b1e2012-06-02 11:12:16 +0100471 if ($this->_file_name_override !== '')
Derek Jonese9d723f2010-07-12 10:10:59 -0500472 {
473 $this->file_name = $this->_prep_filename($this->_file_name_override);
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000474
475 // If no extension was provided in the file_name config item, use the uploaded one
Pascal Kriete14287f32011-02-14 13:39:34 -0500476 if (strpos($this->_file_name_override, '.') === FALSE)
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000477 {
478 $this->file_name .= $this->file_ext;
479 }
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000480 else
481 {
vlakoff35672462013-02-15 01:36:04 +0100482 // An extension was provided, let's have it!
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300483 $this->file_ext = $this->get_extension($this->_file_name_override);
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000484 }
Derek Jonese9d723f2010-07-12 10:10:59 -0500485
486 if ( ! $this->is_allowed_filetype(TRUE))
487 {
Andrey Andreevd5784082015-06-22 11:50:04 +0300488 $this->set_error('upload_invalid_filetype', 'debug');
Barry Mienydd671972010-10-04 16:33:58 +0200489 return FALSE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500490 }
491 }
Barry Mienydd671972010-10-04 16:33:58 +0200492
Derek Jonese9d723f2010-07-12 10:10:59 -0500493 // Convert the file size to kilobytes
494 if ($this->file_size > 0)
495 {
496 $this->file_size = round($this->file_size/1024, 2);
497 }
498
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 // Is the file size within the allowed maximum?
500 if ( ! $this->is_allowed_filesize())
501 {
Andrey Andreevd5784082015-06-22 11:50:04 +0300502 $this->set_error('upload_invalid_filesize', 'info');
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 return FALSE;
504 }
505
506 // Are the image dimensions within the allowed size?
vlakoffc941d852013-08-06 14:44:40 +0200507 // Note: This can fail if the server has an open_basedir restriction.
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 if ( ! $this->is_allowed_dimensions())
509 {
Andrey Andreevd5784082015-06-22 11:50:04 +0300510 $this->set_error('upload_invalid_dimensions', 'info');
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 return FALSE;
512 }
513
514 // Sanitize the file name for security
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200515 $this->file_name = $this->_CI->security->sanitize_filename($this->file_name);
Barry Mienydd671972010-10-04 16:33:58 +0200516
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 // Truncate the file name if it's too long
518 if ($this->max_filename > 0)
519 {
520 $this->file_name = $this->limit_filename_length($this->file_name, $this->max_filename);
521 }
522
523 // Remove white spaces in the name
Alex Bilbied261b1e2012-06-02 11:12:16 +0100524 if ($this->remove_spaces === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300526 $this->file_name = preg_replace('/\s+/', '_', $this->file_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 }
528
529 /*
530 * Validate the file name
531 * This function appends an number onto the end of
532 * the file if one with the same name already exists.
533 * If it returns false there was a problem.
534 */
535 $this->orig_name = $this->file_name;
Andrey Andreev91da5d12015-07-09 15:14:35 +0300536 if (FALSE === ($this->file_name = $this->set_filename($this->upload_path, $this->file_name)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 {
Andrey Andreev91da5d12015-07-09 15:14:35 +0300538 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 }
540
541 /*
Derek Jonese9d723f2010-07-12 10:10:59 -0500542 * Run the file through the XSS hacking filter
543 * This helps prevent malicious code from being
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300544 * embedded within a file. Scripts can easily
Derek Jonese9d723f2010-07-12 10:10:59 -0500545 * be disguised as images or other file types.
546 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300547 if ($this->xss_clean && $this->do_xss_clean() === FALSE)
Derek Jonese9d723f2010-07-12 10:10:59 -0500548 {
Andrey Andreevd5784082015-06-22 11:50:04 +0300549 $this->set_error('upload_unable_to_write_file', 'error');
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300550 return FALSE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500551 }
552
553 /*
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 * Move the file to the final destination
555 * To deal with different server configurations
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300556 * we'll attempt to use copy() first. If that fails
557 * we'll use move_uploaded_file(). One of the two should
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 * reliably work in most environments
559 */
560 if ( ! @copy($this->file_temp, $this->upload_path.$this->file_name))
561 {
562 if ( ! @move_uploaded_file($this->file_temp, $this->upload_path.$this->file_name))
563 {
Andrey Andreevd5784082015-06-22 11:50:04 +0300564 $this->set_error('upload_destination_error', 'error');
Barry Mienydd671972010-10-04 16:33:58 +0200565 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 }
567 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000568
569 /*
570 * Set the finalized image dimensions
571 * This sets the image width/height (assuming the
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300572 * file was an image). We use this information
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 * in the "data" function.
574 */
575 $this->set_image_properties($this->upload_path.$this->file_name);
576
577 return TRUE;
578 }
Barry Mienydd671972010-10-04 16:33:58 +0200579
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200581
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 /**
583 * Finalized Data Array
Barry Mienydd671972010-10-04 16:33:58 +0200584 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 * Returns an associative array containing all of the information
586 * related to the upload, allowing the developer easy access in one array.
587 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200588 * @param string $index
Michiel Vugteveen0ee287e2012-06-11 10:38:14 +0200589 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200590 */
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200591 public function data($index = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 {
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200593 $data = array(
Michiel Vugteveen3a7fb042012-06-11 09:29:16 +0200594 'file_name' => $this->file_name,
595 'file_type' => $this->file_type,
596 'file_path' => $this->upload_path,
597 'full_path' => $this->upload_path.$this->file_name,
598 'raw_name' => str_replace($this->file_ext, '', $this->file_name),
599 'orig_name' => $this->orig_name,
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300600 'client_name' => $this->client_name,
Michiel Vugteveen3a7fb042012-06-11 09:29:16 +0200601 'file_ext' => $this->file_ext,
602 'file_size' => $this->file_size,
603 'is_image' => $this->is_image(),
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300604 'image_width' => $this->image_width,
605 'image_height' => $this->image_height,
606 'image_type' => $this->image_type,
607 'image_size_str' => $this->image_size_str,
608 );
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200609
Michiel Vugteveen46a04292012-06-11 10:37:52 +0200610 if ( ! empty($index))
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200611 {
Michiel Vugteveen46a04292012-06-11 10:37:52 +0200612 return isset($data[$index]) ? $data[$index] : NULL;
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200613 }
614
Michiel Vugteveen46a04292012-06-11 10:37:52 +0200615 return $data;
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 }
Barry Mienydd671972010-10-04 16:33:58 +0200617
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200619
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 /**
621 * Set Upload Path
622 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200623 * @param string $path
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200624 * @return CI_Upload
Barry Mienydd671972010-10-04 16:33:58 +0200625 */
Greg Aker58fdee82010-11-10 15:07:09 -0600626 public function set_upload_path($path)
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 {
628 // Make sure it has a trailing slash
629 $this->upload_path = rtrim($path, '/').'/';
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200630 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 }
Barry Mienydd671972010-10-04 16:33:58 +0200632
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200634
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 /**
636 * Set the file name
637 *
638 * This function takes a filename/path as input and looks for the
639 * existence of a file with the same name. If found, it will append a
640 * number to the end of the filename to avoid overwriting a pre-existing file.
641 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200642 * @param string $path
643 * @param string $filename
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200645 */
Greg Aker58fdee82010-11-10 15:07:09 -0600646 public function set_filename($path, $filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100648 if ($this->encrypt_name === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200649 {
Barry Mienydd671972010-10-04 16:33:58 +0200650 $filename = md5(uniqid(mt_rand())).$this->file_ext;
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 }
Barry Mienydd671972010-10-04 16:33:58 +0200652
Andrey Andreev91da5d12015-07-09 15:14:35 +0300653 if ($this->overwrite === TRUE OR ! file_exists($path.$filename))
Derek Allard2067d1a2008-11-13 22:59:24 +0000654 {
655 return $filename;
656 }
Barry Mienydd671972010-10-04 16:33:58 +0200657
Derek Allard2067d1a2008-11-13 22:59:24 +0000658 $filename = str_replace($this->file_ext, '', $filename);
Barry Mienydd671972010-10-04 16:33:58 +0200659
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 $new_filename = '';
Adam Jackettccbbea12011-08-21 16:19:11 -0400661 for ($i = 1; $i < $this->max_filename_increment; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200662 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 if ( ! file_exists($path.$filename.$i.$this->file_ext))
664 {
665 $new_filename = $filename.$i.$this->file_ext;
666 break;
667 }
668 }
669
Alex Bilbied261b1e2012-06-02 11:12:16 +0100670 if ($new_filename === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 {
Andrey Andreevd5784082015-06-22 11:50:04 +0300672 $this->set_error('upload_bad_filename', 'debug');
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 return FALSE;
674 }
675 else
676 {
677 return $new_filename;
678 }
679 }
Barry Mienydd671972010-10-04 16:33:58 +0200680
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200682
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 /**
684 * Set Maximum File Size
685 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200686 * @param int $n
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200687 * @return CI_Upload
Barry Mienydd671972010-10-04 16:33:58 +0200688 */
Greg Aker58fdee82010-11-10 15:07:09 -0600689 public function set_max_filesize($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200691 $this->max_size = ($n < 0) ? 0 : (int) $n;
692 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000693 }
Barry Mienydd671972010-10-04 16:33:58 +0200694
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200696
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 /**
698 * Set Maximum File Name Length
699 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200700 * @param int $n
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200701 * @return CI_Upload
Barry Mienydd671972010-10-04 16:33:58 +0200702 */
Greg Aker58fdee82010-11-10 15:07:09 -0600703 public function set_max_filename($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200705 $this->max_filename = ($n < 0) ? 0 : (int) $n;
706 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 }
708
709 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200710
Derek Allard2067d1a2008-11-13 22:59:24 +0000711 /**
712 * Set Maximum Image Width
713 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200714 * @param int $n
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200715 * @return CI_Upload
Barry Mienydd671972010-10-04 16:33:58 +0200716 */
Greg Aker58fdee82010-11-10 15:07:09 -0600717 public function set_max_width($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000718 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200719 $this->max_width = ($n < 0) ? 0 : (int) $n;
720 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000721 }
Barry Mienydd671972010-10-04 16:33:58 +0200722
Derek Allard2067d1a2008-11-13 22:59:24 +0000723 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200724
Derek Allard2067d1a2008-11-13 22:59:24 +0000725 /**
726 * Set Maximum Image Height
727 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200728 * @param int $n
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200729 * @return CI_Upload
Barry Mienydd671972010-10-04 16:33:58 +0200730 */
Greg Aker58fdee82010-11-10 15:07:09 -0600731 public function set_max_height($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000732 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200733 $this->max_height = ($n < 0) ? 0 : (int) $n;
734 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000735 }
Barry Mienydd671972010-10-04 16:33:58 +0200736
Derek Allard2067d1a2008-11-13 22:59:24 +0000737 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200738
Derek Allard2067d1a2008-11-13 22:59:24 +0000739 /**
Andrey Andreev05aa2d62012-12-03 16:06:55 +0200740 * Set minimum image width
741 *
742 * @param int $n
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200743 * @return CI_Upload
Andrey Andreev05aa2d62012-12-03 16:06:55 +0200744 */
745 public function set_min_width($n)
746 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200747 $this->min_width = ($n < 0) ? 0 : (int) $n;
748 return $this;
Andrey Andreev05aa2d62012-12-03 16:06:55 +0200749 }
750
751 // --------------------------------------------------------------------
752
753 /**
754 * Set minimum image height
755 *
756 * @param int $n
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200757 * @return CI_Upload
Andrey Andreev05aa2d62012-12-03 16:06:55 +0200758 */
759 public function set_min_height($n)
760 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200761 $this->min_height = ($n < 0) ? 0 : (int) $n;
762 return $this;
Andrey Andreev05aa2d62012-12-03 16:06:55 +0200763 }
764
765 // --------------------------------------------------------------------
766
767 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000768 * Set Allowed File Types
769 *
Andrey Andreev82179bf2014-02-22 00:29:53 +0200770 * @param mixed $types
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200771 * @return CI_Upload
Barry Mienydd671972010-10-04 16:33:58 +0200772 */
Greg Aker58fdee82010-11-10 15:07:09 -0600773 public function set_allowed_types($types)
Derek Allard2067d1a2008-11-13 22:59:24 +0000774 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200775 $this->allowed_types = (is_array($types) OR $types === '*')
776 ? $types
777 : explode('|', $types);
778 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 }
Barry Mienydd671972010-10-04 16:33:58 +0200780
Derek Allard2067d1a2008-11-13 22:59:24 +0000781 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200782
Derek Allard2067d1a2008-11-13 22:59:24 +0000783 /**
784 * Set Image Properties
785 *
786 * Uses GD to determine the width/height/type of image
787 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200788 * @param string $path
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200789 * @return CI_Upload
Barry Mienydd671972010-10-04 16:33:58 +0200790 */
Greg Aker58fdee82010-11-10 15:07:09 -0600791 public function set_image_properties($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200793 if ($this->is_image() && function_exists('getimagesize'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000794 {
795 if (FALSE !== ($D = @getimagesize($path)))
Barry Mienydd671972010-10-04 16:33:58 +0200796 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000797 $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
798
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300799 $this->image_width = $D[0];
800 $this->image_height = $D[1];
801 $this->image_type = isset($types[$D[2]]) ? $types[$D[2]] : 'unknown';
802 $this->image_size_str = $D[3]; // string containing height and width
Derek Allard2067d1a2008-11-13 22:59:24 +0000803 }
804 }
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200805
806 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000807 }
Barry Mienydd671972010-10-04 16:33:58 +0200808
Derek Allard2067d1a2008-11-13 22:59:24 +0000809 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200810
Derek Allard2067d1a2008-11-13 22:59:24 +0000811 /**
812 * Set XSS Clean
813 *
814 * Enables the XSS flag so that the file that was uploaded
815 * will be run through the XSS filter.
816 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200817 * @param bool $flag
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200818 * @return CI_Upload
Derek Allard2067d1a2008-11-13 22:59:24 +0000819 */
Greg Aker58fdee82010-11-10 15:07:09 -0600820 public function set_xss_clean($flag = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000821 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100822 $this->xss_clean = ($flag === TRUE);
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200823 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000824 }
Barry Mienydd671972010-10-04 16:33:58 +0200825
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200827
Derek Allard2067d1a2008-11-13 22:59:24 +0000828 /**
829 * Validate the image
830 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000831 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200832 */
Greg Aker58fdee82010-11-10 15:07:09 -0600833 public function is_image()
Derek Allard2067d1a2008-11-13 22:59:24 +0000834 {
835 // IE will sometimes return odd mime-types during upload, so here we just standardize all
836 // jpegs or pngs to the same file type.
837
Derek Jones4b9c6292011-07-01 17:40:48 -0500838 $png_mimes = array('image/x-png');
Derek Allard2067d1a2008-11-13 22:59:24 +0000839 $jpeg_mimes = array('image/jpg', 'image/jpe', 'image/jpeg', 'image/pjpeg');
Barry Mienydd671972010-10-04 16:33:58 +0200840
Derek Allard2067d1a2008-11-13 22:59:24 +0000841 if (in_array($this->file_type, $png_mimes))
842 {
843 $this->file_type = 'image/png';
844 }
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300845 elseif (in_array($this->file_type, $jpeg_mimes))
Derek Allard2067d1a2008-11-13 22:59:24 +0000846 {
847 $this->file_type = 'image/jpeg';
848 }
849
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300850 $img_mimes = array('image/gif', 'image/jpeg', 'image/png');
Derek Allard2067d1a2008-11-13 22:59:24 +0000851
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300852 return in_array($this->file_type, $img_mimes, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 }
Barry Mienydd671972010-10-04 16:33:58 +0200854
Derek Allard2067d1a2008-11-13 22:59:24 +0000855 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200856
Derek Allard2067d1a2008-11-13 22:59:24 +0000857 /**
858 * Verify that the filetype is allowed
859 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200860 * @param bool $ignore_mime
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200862 */
Greg Aker58fdee82010-11-10 15:07:09 -0600863 public function is_allowed_filetype($ignore_mime = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000864 {
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200865 if ($this->allowed_types === '*')
Derek Jonese12f64e2010-03-02 22:55:08 -0600866 {
867 return TRUE;
868 }
Barry Mienydd671972010-10-04 16:33:58 +0200869
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200870 if (empty($this->allowed_types) OR ! is_array($this->allowed_types))
Derek Allard2067d1a2008-11-13 22:59:24 +0000871 {
Andrey Andreevd5784082015-06-22 11:50:04 +0300872 $this->set_error('upload_no_file_types', 'debug');
Derek Allard2067d1a2008-11-13 22:59:24 +0000873 return FALSE;
874 }
Barry Mienydd671972010-10-04 16:33:58 +0200875
Derek Jonese9d723f2010-07-12 10:10:59 -0500876 $ext = strtolower(ltrim($this->file_ext, '.'));
Barry Mienydd671972010-10-04 16:33:58 +0200877
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200878 if ( ! in_array($ext, $this->allowed_types, TRUE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 {
Derek Jonese9d723f2010-07-12 10:10:59 -0500880 return FALSE;
881 }
Derek Jonesafa282f2009-02-10 17:11:52 +0000882
Barry Mienydd671972010-10-04 16:33:58 +0200883 // Images get some additional checks
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200884 if (in_array($ext, array('gif', 'jpg', 'jpeg', 'jpe', 'png'), TRUE) && @getimagesize($this->file_temp) === FALSE)
Derek Jonese9d723f2010-07-12 10:10:59 -0500885 {
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200886 return FALSE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500887 }
Barry Mienydd671972010-10-04 16:33:58 +0200888
Derek Jonese9d723f2010-07-12 10:10:59 -0500889 if ($ignore_mime === TRUE)
890 {
891 return TRUE;
892 }
Barry Mienydd671972010-10-04 16:33:58 +0200893
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200894 if (isset($this->_mimes[$ext]))
Derek Jonese9d723f2010-07-12 10:10:59 -0500895 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200896 return is_array($this->_mimes[$ext])
897 ? in_array($this->file_type, $this->_mimes[$ext], TRUE)
898 : ($this->_mimes[$ext] === $this->file_type);
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 }
Barry Mienydd671972010-10-04 16:33:58 +0200900
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 return FALSE;
902 }
Barry Mienydd671972010-10-04 16:33:58 +0200903
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200905
Derek Allard2067d1a2008-11-13 22:59:24 +0000906 /**
907 * Verify that the file is within the allowed size
908 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200910 */
Greg Aker58fdee82010-11-10 15:07:09 -0600911 public function is_allowed_filesize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100913 return ($this->max_size === 0 OR $this->max_size > $this->file_size);
Derek Allard2067d1a2008-11-13 22:59:24 +0000914 }
Barry Mienydd671972010-10-04 16:33:58 +0200915
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200917
Derek Allard2067d1a2008-11-13 22:59:24 +0000918 /**
919 * Verify that the image is within the allowed width/height
920 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200922 */
Greg Aker58fdee82010-11-10 15:07:09 -0600923 public function is_allowed_dimensions()
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 {
925 if ( ! $this->is_image())
926 {
927 return TRUE;
928 }
929
930 if (function_exists('getimagesize'))
931 {
932 $D = @getimagesize($this->file_temp);
933
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300934 if ($this->max_width > 0 && $D[0] > $this->max_width)
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 {
936 return FALSE;
937 }
938
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300939 if ($this->max_height > 0 && $D[1] > $this->max_height)
Derek Allard2067d1a2008-11-13 22:59:24 +0000940 {
941 return FALSE;
942 }
Andrey Andreev05aa2d62012-12-03 16:06:55 +0200943
944 if ($this->min_width > 0 && $D[0] < $this->min_width)
945 {
946 return FALSE;
947 }
948
949 if ($this->min_height > 0 && $D[1] < $this->min_height)
950 {
951 return FALSE;
952 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000953 }
954
955 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 * Validate Upload Path
962 *
963 * Verifies that it is a valid upload path with proper permissions.
964 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200966 */
Greg Aker58fdee82010-11-10 15:07:09 -0600967 public function validate_upload_path()
Derek Allard2067d1a2008-11-13 22:59:24 +0000968 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100969 if ($this->upload_path === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000970 {
Andrey Andreevd5784082015-06-22 11:50:04 +0300971 $this->set_error('upload_no_filepath', 'error');
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 return FALSE;
973 }
Barry Mienydd671972010-10-04 16:33:58 +0200974
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200975 if (realpath($this->upload_path) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300977 $this->upload_path = str_replace('\\', '/', realpath($this->upload_path));
Derek Allard2067d1a2008-11-13 22:59:24 +0000978 }
979
Andrey Andreev382b5132014-02-26 18:41:59 +0200980 if ( ! is_dir($this->upload_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000981 {
Andrey Andreevd5784082015-06-22 11:50:04 +0300982 $this->set_error('upload_no_filepath', 'error');
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 return FALSE;
984 }
985
986 if ( ! is_really_writable($this->upload_path))
987 {
Andrey Andreevd5784082015-06-22 11:50:04 +0300988 $this->set_error('upload_not_writable', 'error');
Derek Allard2067d1a2008-11-13 22:59:24 +0000989 return FALSE;
990 }
991
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300992 $this->upload_path = preg_replace('/(.+?)\/*$/', '\\1/', $this->upload_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000993 return TRUE;
994 }
Barry Mienydd671972010-10-04 16:33:58 +0200995
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200997
Derek Allard2067d1a2008-11-13 22:59:24 +0000998 /**
999 * Extract the file extension
1000 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001001 * @param string $filename
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001003 */
Greg Aker58fdee82010-11-10 15:07:09 -06001004 public function get_extension($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +00001005 {
1006 $x = explode('.', $filename);
Adrianf496d132013-06-24 15:56:45 +02001007
Adrian74a228b2013-06-25 12:09:22 +02001008 if (count($x) === 1)
1009 {
1010 return '';
1011 }
1012
1013 $ext = ($this->file_ext_tolower) ? strtolower(end($x)) : end($x);
1014 return '.'.$ext;
Barry Mienydd671972010-10-04 16:33:58 +02001015 }
1016
Derek Allard2067d1a2008-11-13 22:59:24 +00001017 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001018
Derek Allard2067d1a2008-11-13 22:59:24 +00001019 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001020 * Limit the File Name Length
1021 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001022 * @param string $filename
1023 * @param int $length
Derek Allard2067d1a2008-11-13 22:59:24 +00001024 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001025 */
Greg Aker58fdee82010-11-10 15:07:09 -06001026 public function limit_filename_length($filename, $length)
Derek Allard2067d1a2008-11-13 22:59:24 +00001027 {
1028 if (strlen($filename) < $length)
1029 {
1030 return $filename;
1031 }
Barry Mienydd671972010-10-04 16:33:58 +02001032
Derek Allard2067d1a2008-11-13 22:59:24 +00001033 $ext = '';
1034 if (strpos($filename, '.') !== FALSE)
1035 {
1036 $parts = explode('.', $filename);
1037 $ext = '.'.array_pop($parts);
1038 $filename = implode('.', $parts);
1039 }
Barry Mienydd671972010-10-04 16:33:58 +02001040
Derek Allard2067d1a2008-11-13 22:59:24 +00001041 return substr($filename, 0, ($length - strlen($ext))).$ext;
1042 }
1043
1044 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001045
Derek Allard2067d1a2008-11-13 22:59:24 +00001046 /**
1047 * Runs the file through the XSS clean function
1048 *
1049 * This prevents people from embedding malicious code in their files.
1050 * I'm not sure that it won't negatively affect certain files in unexpected ways,
1051 * but so far I haven't found that it causes trouble.
1052 *
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +02001053 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001054 */
Greg Aker58fdee82010-11-10 15:07:09 -06001055 public function do_xss_clean()
Barry Mienydd671972010-10-04 16:33:58 +02001056 {
Derek Jonese9d723f2010-07-12 10:10:59 -05001057 $file = $this->file_temp;
Barry Mienydd671972010-10-04 16:33:58 +02001058
Andrey Andreev5036c9c2012-06-04 15:34:56 +03001059 if (filesize($file) == 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001060 {
1061 return FALSE;
1062 }
Barry Mienydd671972010-10-04 16:33:58 +02001063
Andrey Andreevc839d282012-06-07 14:35:27 +03001064 if (memory_get_usage() && ($memory_limit = ini_get('memory_limit')))
Greg Akerf82e51c2010-04-14 19:33:50 -05001065 {
Andrey Andreevc839d282012-06-07 14:35:27 +03001066 $memory_limit *= 1024 * 1024;
Barry Mienydd671972010-10-04 16:33:58 +02001067
Greg Akerc78a2592010-06-09 11:45:32 -05001068 // There was a bug/behavioural change in PHP 5.2, where numbers over one million get output
Andrey Andreev8e6f7a92012-03-26 20:13:00 +03001069 // into scientific notation. number_format() ensures this number is an integer
Greg Akerc78a2592010-06-09 11:45:32 -05001070 // http://bugs.php.net/bug.php?id=43053
Barry Mienydd671972010-10-04 16:33:58 +02001071
Andrey Andreevc839d282012-06-07 14:35:27 +03001072 $memory_limit = number_format(ceil(filesize($file) + $memory_limit), 0, '.', '');
Barry Mienydd671972010-10-04 16:33:58 +02001073
Andrey Andreevc839d282012-06-07 14:35:27 +03001074 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 -05001075 }
1076
1077 // If the file being uploaded is an image, then we should have no problem with XSS attacks (in theory), but
1078 // 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 +03001079 // using IE who looks at the image. It does this by inspecting the first 255 bytes of an image. To get around this
1080 // 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 +02001081 // 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 -05001082 // attempted XSS attack.
1083
1084 if (function_exists('getimagesize') && @getimagesize($file) !== FALSE)
1085 {
Barry Mienydd671972010-10-04 16:33:58 +02001086 if (($file = @fopen($file, 'rb')) === FALSE) // "b" to force binary
1087 {
Greg Akerf82e51c2010-04-14 19:33:50 -05001088 return FALSE; // Couldn't open the file, return FALSE
Barry Mienydd671972010-10-04 16:33:58 +02001089 }
Greg Akerf82e51c2010-04-14 19:33:50 -05001090
Barry Mienydd671972010-10-04 16:33:58 +02001091 $opening_bytes = fread($file, 256);
1092 fclose($file);
Greg Akerf82e51c2010-04-14 19:33:50 -05001093
1094 // These are known to throw IE into mime-type detection chaos
1095 // <a, <body, <head, <html, <img, <plaintext, <pre, <script, <table, <title
1096 // title is basically just in SVG, but we filter it anyhow
1097
vlakoff35672462013-02-15 01:36:04 +01001098 // if it's an image or no "triggers" detected in the first 256 bytes - we're good
Andrey Andreevc839d282012-06-07 14:35:27 +03001099 return ! preg_match('/<(a|body|head|html|img|plaintext|pre|script|table|title)[\s>]/i', $opening_bytes);
Greg Akerf82e51c2010-04-14 19:33:50 -05001100 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001101
1102 if (($data = @file_get_contents($file)) === FALSE)
1103 {
1104 return FALSE;
1105 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001106
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +02001107 return $this->_CI->security->xss_clean($data, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001108 }
Barry Mienydd671972010-10-04 16:33:58 +02001109
Derek Allard2067d1a2008-11-13 22:59:24 +00001110 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001111
Derek Allard2067d1a2008-11-13 22:59:24 +00001112 /**
1113 * Set an error message
1114 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001115 * @param string $msg
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +02001116 * @return CI_Upload
Barry Mienydd671972010-10-04 16:33:58 +02001117 */
Andrey Andreevd5784082015-06-22 11:50:04 +03001118 public function set_error($msg, $log_level = 'error')
Derek Allard2067d1a2008-11-13 22:59:24 +00001119 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +02001120 $this->_CI->lang->load('upload');
Barry Mienydd671972010-10-04 16:33:58 +02001121
Andrey Andreev119d8a72014-01-08 15:27:53 +02001122 is_array($msg) OR $msg = array($msg);
Darren Benney38d5f4b2013-03-30 21:00:38 +00001123 foreach ($msg as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001124 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +02001125 $msg = ($this->_CI->lang->line($val) === FALSE) ? $val : $this->_CI->lang->line($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001126 $this->error_msg[] = $msg;
Andrey Andreevd5784082015-06-22 11:50:04 +03001127 log_message($log_level, $msg);
Derek Allard2067d1a2008-11-13 22:59:24 +00001128 }
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +02001129
1130 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +00001131 }
Barry Mienydd671972010-10-04 16:33:58 +02001132
Derek Allard2067d1a2008-11-13 22:59:24 +00001133 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001134
Derek Allard2067d1a2008-11-13 22:59:24 +00001135 /**
1136 * Display the error message
1137 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001138 * @param string $open
1139 * @param string $close
Derek Allard2067d1a2008-11-13 22:59:24 +00001140 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001141 */
Greg Aker58fdee82010-11-10 15:07:09 -06001142 public function display_errors($open = '<p>', $close = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +00001143 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +03001144 return (count($this->error_msg) > 0) ? $open.implode($close.$open, $this->error_msg).$close : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001145 }
Barry Mienydd671972010-10-04 16:33:58 +02001146
Derek Allard2067d1a2008-11-13 22:59:24 +00001147 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001148
Derek Allard2067d1a2008-11-13 22:59:24 +00001149 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001150 * Prep Filename
1151 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001152 * Prevents possible script execution from Apache's handling
1153 * of files' multiple extensions.
Derek Allard2067d1a2008-11-13 22:59:24 +00001154 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001155 * @link http://httpd.apache.org/docs/1.3/mod/mod_mime.html#multipleext
1156 *
1157 * @param string $filename
Derek Allard2067d1a2008-11-13 22:59:24 +00001158 * @return string
1159 */
Greg Aker58fdee82010-11-10 15:07:09 -06001160 protected function _prep_filename($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +00001161 {
Andrey Andreevf38c9c22014-08-27 14:56:31 +03001162 if ($this->mod_mime_fix === FALSE OR $this->allowed_types === '*' OR ($ext_pos = strrpos($filename, '.')) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001163 {
1164 return $filename;
1165 }
Derek Allard616dab82009-02-16 15:44:32 +00001166
Andrey Andreevf38c9c22014-08-27 14:56:31 +03001167 $ext = substr($filename, $ext_pos);
1168 $filename = substr($filename, 0, $ext_pos);
1169 return str_replace('.', '_', $filename).$ext;
Derek Allard2067d1a2008-11-13 22:59:24 +00001170 }
1171
1172 // --------------------------------------------------------------------
1173
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001174 /**
1175 * File MIME type
1176 *
1177 * Detects the (actual) MIME type of the uploaded file, if possible.
1178 * The input array is expected to be $_FILES[$field]
1179 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001180 * @param array $file
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001181 * @return void
1182 */
1183 protected function _file_mime_type($file)
1184 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001185 // We'll need this to validate the MIME info string (e.g. text/plain; charset=us-ascii)
Andrey Andreevf7aed122011-12-13 11:01:06 +02001186 $regexp = '/^([a-z\-]+\/[a-z0-9\-\.\+]+)(;\s.+)?$/';
Andrey Andreeva49e3812011-12-09 13:05:22 +02001187
1188 /* Fileinfo extension - most reliable method
1189 *
1190 * Unfortunately, prior to PHP 5.3 - it's only available as a PECL extension and the
1191 * more convenient FILEINFO_MIME_TYPE flag doesn't exist.
1192 */
1193 if (function_exists('finfo_file'))
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001194 {
Andrey Andreevd60e7002012-06-17 00:03:03 +03001195 $finfo = @finfo_open(FILEINFO_MIME);
Andrey Andreeva49e3812011-12-09 13:05:22 +02001196 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 +03001197 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001198 $mime = @finfo_file($finfo, $file['tmp_name']);
1199 finfo_close($finfo);
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001200
1201 /* According to the comments section of the PHP manual page,
1202 * it is possible that this function returns an empty string
Andrey Andreev2cec39f2011-09-24 22:59:37 +03001203 * for some files (e.g. if they don't exist in the magic MIME database)
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001204 */
Andrey Andreeva49e3812011-12-09 13:05:22 +02001205 if (is_string($mime) && preg_match($regexp, $mime, $matches))
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001206 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001207 $this->file_type = $matches[1];
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001208 return;
1209 }
1210 }
1211 }
1212
Andrey Andreeva49e3812011-12-09 13:05:22 +02001213 /* This is an ugly hack, but UNIX-type systems provide a "native" way to detect the file type,
1214 * which is still more secure than depending on the value of $_FILES[$field]['type'], and as it
1215 * was reported in issue #750 (https://github.com/EllisLab/CodeIgniter/issues/750) - it's better
1216 * than mime_content_type() as well, hence the attempts to try calling the command line with
1217 * three different functions.
Andrey Andreev6700b932011-09-24 14:25:33 +03001218 *
1219 * Notes:
Andrey Andreev59654312011-12-02 14:28:54 +02001220 * - the DIRECTORY_SEPARATOR comparison ensures that we're not on a Windows system
Andrey Andreeva49e3812011-12-09 13:05:22 +02001221 * - many system admins would disable the exec(), shell_exec(), popen() and similar functions
vlakofff3994252013-02-19 01:45:23 +01001222 * due to security concerns, hence the function_usable() checks
Andrey Andreev6700b932011-09-24 14:25:33 +03001223 */
Andrey Andreeva49e3812011-12-09 13:05:22 +02001224 if (DIRECTORY_SEPARATOR !== '\\')
Andrey Andreev6700b932011-09-24 14:25:33 +03001225 {
Andrey Andreevd60e7002012-06-17 00:03:03 +03001226 $cmd = function_exists('escapeshellarg')
1227 ? 'file --brief --mime '.escapeshellarg($file['tmp_name']).' 2>&1'
1228 : 'file --brief --mime '.$file['tmp_name'].' 2>&1';
Andrey Andreeva49e3812011-12-09 13:05:22 +02001229
Andrey Andreeve9d2dc82012-11-07 14:23:29 +02001230 if (function_usable('exec'))
Andrey Andreev6700b932011-09-24 14:25:33 +03001231 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001232 /* 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 +01001233 * 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 +02001234 * anything that could already be set for $mime previously. This effectively makes the second parameter a dummy
1235 * value, which is only put to allow us to get the return status code.
1236 */
1237 $mime = @exec($cmd, $mime, $return_status);
1238 if ($return_status === 0 && is_string($mime) && preg_match($regexp, $mime, $matches))
1239 {
1240 $this->file_type = $matches[1];
1241 return;
1242 }
1243 }
1244
Andrey Andreevf6274742014-02-20 18:05:58 +02001245 if ( ! ini_get('safe_mode') && function_usable('shell_exec'))
Andrey Andreeva49e3812011-12-09 13:05:22 +02001246 {
1247 $mime = @shell_exec($cmd);
1248 if (strlen($mime) > 0)
1249 {
1250 $mime = explode("\n", trim($mime));
1251 if (preg_match($regexp, $mime[(count($mime) - 1)], $matches))
1252 {
1253 $this->file_type = $matches[1];
1254 return;
1255 }
1256 }
1257 }
1258
Andrey Andreeve9d2dc82012-11-07 14:23:29 +02001259 if (function_usable('popen'))
Andrey Andreeva49e3812011-12-09 13:05:22 +02001260 {
1261 $proc = @popen($cmd, 'r');
1262 if (is_resource($proc))
1263 {
tubalmartin010f1f42012-03-03 22:24:31 +01001264 $mime = @fread($proc, 512);
Andrey Andreeva49e3812011-12-09 13:05:22 +02001265 @pclose($proc);
1266 if ($mime !== FALSE)
1267 {
1268 $mime = explode("\n", trim($mime));
1269 if (preg_match($regexp, $mime[(count($mime) - 1)], $matches))
1270 {
1271 $this->file_type = $matches[1];
1272 return;
1273 }
1274 }
1275 }
1276 }
1277 }
1278
1279 // Fall back to the deprecated mime_content_type(), if available (still better than $_FILES[$field]['type'])
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001280 if (function_exists('mime_content_type'))
1281 {
1282 $this->file_type = @mime_content_type($file['tmp_name']);
Andrey Andreeva49e3812011-12-09 13:05:22 +02001283 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 +03001284 {
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001285 return;
1286 }
1287 }
1288
1289 $this->file_type = $file['type'];
1290 }
1291
Derek Allard2067d1a2008-11-13 22:59:24 +00001292}