blob: 745f4980f95a2e9e320bd879f124b1f597596da9 [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 *
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 Andreevbdb96ca2014-10-28 00:13:31 +02009 * Copyright (c) 2014, 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 Andreevbdb96ca2014-10-28 00:13:31 +020032 * @copyright Copyright (c) 2014, British Columbia Institute of Technology (http://bcit.ca/)
33 * @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 Andreev8e6f7a92012-03-26 20:13:00 +0300299 log_message('debug', '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 }
Joseba Juániza8027ff2014-08-06 20:03:25 +0200341
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200342 }
Joseba Juániza8027ff2014-08-06 20:03:25 +0200343 else
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200344 {
Joseba Juániza8027ff2014-08-06 20:03:25 +0200345
346 foreach ($config as $key => &$value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 {
Joseba Juániza8027ff2014-08-06 20:03:25 +0200348 if ($key[0] !== '_' && $reflection->hasProperty($key))
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200349 {
Joseba Juániza8027ff2014-08-06 20:03:25 +0200350 if ($reflection->hasMethod('set_'.$key))
351 {
352 $this->{'set_'.$key}($value);
353 }
354 else
355 {
356 $this->$key = $value;
357 }
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200358 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 }
Joseba Juániza8027ff2014-08-06 20:03:25 +0200360
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 }
Barry Mienydd671972010-10-04 16:33:58 +0200362
Derek Jonese9d723f2010-07-12 10:10:59 -0500363 // if a file_name was provided in the config, use it instead of the user input
364 // supplied file name for all uploads until initialized again
365 $this->_file_name_override = $this->file_name;
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200366 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 }
Barry Mienydd671972010-10-04 16:33:58 +0200368
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200370
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 /**
372 * Perform the file upload
373 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200374 * @param string $field
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200376 */
Greg Aker58fdee82010-11-10 15:07:09 -0600377 public function do_upload($field = 'userfile')
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300379 // Is $_FILES[$field] set? If not, no reason to continue.
Andrey Andreeve7d017b2014-02-25 12:23:34 +0200380 if (isset($_FILES[$field]))
381 {
382 $_file = $_FILES[$field];
383 }
384 // Does the field name contain array notation?
385 elseif (($c = preg_match_all('/(?:^[^\[]+)|\[[^]]*\]/', $field, $matches)) > 1)
386 {
387 $_file = $_FILES;
388 for ($i = 0; $i < $c; $i++)
389 {
390 // We can't track numeric iterations, only full field names are accepted
391 if (($field = trim($matches[0][$i], '[]')) === '' OR ! isset($_file[$field]))
392 {
393 $_file = NULL;
394 break;
395 }
396
397 $_file = $_file[$field];
398 }
399 }
400
401 if ( ! isset($_file))
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 {
403 $this->set_error('upload_no_file_selected');
404 return FALSE;
405 }
Barry Mienydd671972010-10-04 16:33:58 +0200406
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 // Is the upload path valid?
408 if ( ! $this->validate_upload_path())
409 {
410 // errors will already be set by validate_upload_path() so just return FALSE
411 return FALSE;
412 }
413
414 // Was the file able to be uploaded? If not, determine the reason why.
Andrey Andreeve7d017b2014-02-25 12:23:34 +0200415 if ( ! is_uploaded_file($_file['tmp_name']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 {
Andrey Andreeve7d017b2014-02-25 12:23:34 +0200417 $error = isset($_file['error']) ? $_file['error'] : 4;
Derek Allard2067d1a2008-11-13 22:59:24 +0000418
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300419 switch ($error)
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 {
Darren Benneye123a602013-03-30 18:17:54 +0000421 case UPLOAD_ERR_INI_SIZE:
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 $this->set_error('upload_file_exceeds_limit');
423 break;
Darren Benneye123a602013-03-30 18:17:54 +0000424 case UPLOAD_ERR_FORM_SIZE:
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 $this->set_error('upload_file_exceeds_form_limit');
426 break;
Darren Benneye123a602013-03-30 18:17:54 +0000427 case UPLOAD_ERR_PARTIAL:
Barry Mienydd671972010-10-04 16:33:58 +0200428 $this->set_error('upload_file_partial');
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 break;
Darren Benneye123a602013-03-30 18:17:54 +0000430 case UPLOAD_ERR_NO_FILE:
Barry Mienydd671972010-10-04 16:33:58 +0200431 $this->set_error('upload_no_file_selected');
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 break;
Darren Benneye123a602013-03-30 18:17:54 +0000433 case UPLOAD_ERR_NO_TMP_DIR:
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 $this->set_error('upload_no_temp_directory');
435 break;
Darren Benneye123a602013-03-30 18:17:54 +0000436 case UPLOAD_ERR_CANT_WRITE:
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 $this->set_error('upload_unable_to_write_file');
438 break;
Darren Benneye123a602013-03-30 18:17:54 +0000439 case UPLOAD_ERR_EXTENSION:
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 $this->set_error('upload_stopped_by_extension');
441 break;
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300442 default:
443 $this->set_error('upload_no_file_selected');
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 break;
445 }
446
447 return FALSE;
448 }
449
450 // Set the uploaded data as class variables
Andrey Andreeve7d017b2014-02-25 12:23:34 +0200451 $this->file_temp = $_file['tmp_name'];
452 $this->file_size = $_file['size'];
Andrey Andreevd60e7002012-06-17 00:03:03 +0300453
454 // Skip MIME type detection?
455 if ($this->detect_mime !== FALSE)
456 {
Andrey Andreeve7d017b2014-02-25 12:23:34 +0200457 $this->_file_mime_type($_file);
Andrey Andreevd60e7002012-06-17 00:03:03 +0300458 }
459
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300460 $this->file_type = preg_replace('/^(.+?);.*$/', '\\1', $this->file_type);
Derek Jones616fb022010-04-22 16:52:18 -0500461 $this->file_type = strtolower(trim(stripslashes($this->file_type), '"'));
Andrey Andreeve7d017b2014-02-25 12:23:34 +0200462 $this->file_name = $this->_prep_filename($_file['name']);
Derek Jonese9d723f2010-07-12 10:10:59 -0500463 $this->file_ext = $this->get_extension($this->file_name);
464 $this->client_name = $this->file_name;
Barry Mienydd671972010-10-04 16:33:58 +0200465
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 // Is the file type allowed to be uploaded?
467 if ( ! $this->is_allowed_filetype())
468 {
469 $this->set_error('upload_invalid_filetype');
470 return FALSE;
471 }
472
Derek Jonese9d723f2010-07-12 10:10:59 -0500473 // if we're overriding, let's now make sure the new name and type is allowed
Alex Bilbied261b1e2012-06-02 11:12:16 +0100474 if ($this->_file_name_override !== '')
Derek Jonese9d723f2010-07-12 10:10:59 -0500475 {
476 $this->file_name = $this->_prep_filename($this->_file_name_override);
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000477
478 // If no extension was provided in the file_name config item, use the uploaded one
Pascal Kriete14287f32011-02-14 13:39:34 -0500479 if (strpos($this->_file_name_override, '.') === FALSE)
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000480 {
481 $this->file_name .= $this->file_ext;
482 }
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000483 else
484 {
vlakoff35672462013-02-15 01:36:04 +0100485 // An extension was provided, let's have it!
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300486 $this->file_ext = $this->get_extension($this->_file_name_override);
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000487 }
Derek Jonese9d723f2010-07-12 10:10:59 -0500488
489 if ( ! $this->is_allowed_filetype(TRUE))
490 {
491 $this->set_error('upload_invalid_filetype');
Barry Mienydd671972010-10-04 16:33:58 +0200492 return FALSE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500493 }
494 }
Barry Mienydd671972010-10-04 16:33:58 +0200495
Derek Jonese9d723f2010-07-12 10:10:59 -0500496 // Convert the file size to kilobytes
497 if ($this->file_size > 0)
498 {
499 $this->file_size = round($this->file_size/1024, 2);
500 }
501
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 // Is the file size within the allowed maximum?
503 if ( ! $this->is_allowed_filesize())
504 {
505 $this->set_error('upload_invalid_filesize');
506 return FALSE;
507 }
508
509 // Are the image dimensions within the allowed size?
vlakoffc941d852013-08-06 14:44:40 +0200510 // Note: This can fail if the server has an open_basedir restriction.
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 if ( ! $this->is_allowed_dimensions())
512 {
513 $this->set_error('upload_invalid_dimensions');
514 return FALSE;
515 }
516
517 // Sanitize the file name for security
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200518 $this->file_name = $this->_CI->security->sanitize_filename($this->file_name);
Barry Mienydd671972010-10-04 16:33:58 +0200519
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 // Truncate the file name if it's too long
521 if ($this->max_filename > 0)
522 {
523 $this->file_name = $this->limit_filename_length($this->file_name, $this->max_filename);
524 }
525
526 // Remove white spaces in the name
Alex Bilbied261b1e2012-06-02 11:12:16 +0100527 if ($this->remove_spaces === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300529 $this->file_name = preg_replace('/\s+/', '_', $this->file_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 }
531
532 /*
533 * Validate the file name
534 * This function appends an number onto the end of
535 * the file if one with the same name already exists.
536 * If it returns false there was a problem.
537 */
538 $this->orig_name = $this->file_name;
539
Alex Bilbied261b1e2012-06-02 11:12:16 +0100540 if ($this->overwrite === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 {
542 $this->file_name = $this->set_filename($this->upload_path, $this->file_name);
Barry Mienydd671972010-10-04 16:33:58 +0200543
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 if ($this->file_name === FALSE)
545 {
546 return FALSE;
547 }
548 }
549
550 /*
Derek Jonese9d723f2010-07-12 10:10:59 -0500551 * Run the file through the XSS hacking filter
552 * This helps prevent malicious code from being
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300553 * embedded within a file. Scripts can easily
Derek Jonese9d723f2010-07-12 10:10:59 -0500554 * be disguised as images or other file types.
555 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300556 if ($this->xss_clean && $this->do_xss_clean() === FALSE)
Derek Jonese9d723f2010-07-12 10:10:59 -0500557 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300558 $this->set_error('upload_unable_to_write_file');
559 return FALSE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500560 }
561
562 /*
Derek Allard2067d1a2008-11-13 22:59:24 +0000563 * Move the file to the final destination
564 * To deal with different server configurations
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300565 * we'll attempt to use copy() first. If that fails
566 * we'll use move_uploaded_file(). One of the two should
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 * reliably work in most environments
568 */
569 if ( ! @copy($this->file_temp, $this->upload_path.$this->file_name))
570 {
571 if ( ! @move_uploaded_file($this->file_temp, $this->upload_path.$this->file_name))
572 {
Barry Mienydd671972010-10-04 16:33:58 +0200573 $this->set_error('upload_destination_error');
574 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 }
576 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000577
578 /*
579 * Set the finalized image dimensions
580 * This sets the image width/height (assuming the
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300581 * file was an image). We use this information
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 * in the "data" function.
583 */
584 $this->set_image_properties($this->upload_path.$this->file_name);
585
586 return TRUE;
587 }
Barry Mienydd671972010-10-04 16:33:58 +0200588
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200590
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 /**
592 * Finalized Data Array
Barry Mienydd671972010-10-04 16:33:58 +0200593 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 * Returns an associative array containing all of the information
595 * related to the upload, allowing the developer easy access in one array.
596 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200597 * @param string $index
Michiel Vugteveen0ee287e2012-06-11 10:38:14 +0200598 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200599 */
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200600 public function data($index = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 {
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200602 $data = array(
Michiel Vugteveen3a7fb042012-06-11 09:29:16 +0200603 'file_name' => $this->file_name,
604 'file_type' => $this->file_type,
605 'file_path' => $this->upload_path,
606 'full_path' => $this->upload_path.$this->file_name,
607 'raw_name' => str_replace($this->file_ext, '', $this->file_name),
608 'orig_name' => $this->orig_name,
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300609 'client_name' => $this->client_name,
Michiel Vugteveen3a7fb042012-06-11 09:29:16 +0200610 'file_ext' => $this->file_ext,
611 'file_size' => $this->file_size,
612 'is_image' => $this->is_image(),
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300613 'image_width' => $this->image_width,
614 'image_height' => $this->image_height,
615 'image_type' => $this->image_type,
616 'image_size_str' => $this->image_size_str,
617 );
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200618
Michiel Vugteveen46a04292012-06-11 10:37:52 +0200619 if ( ! empty($index))
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200620 {
Michiel Vugteveen46a04292012-06-11 10:37:52 +0200621 return isset($data[$index]) ? $data[$index] : NULL;
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200622 }
623
Michiel Vugteveen46a04292012-06-11 10:37:52 +0200624 return $data;
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 }
Barry Mienydd671972010-10-04 16:33:58 +0200626
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200628
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 /**
630 * Set Upload Path
631 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200632 * @param string $path
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200633 * @return CI_Upload
Barry Mienydd671972010-10-04 16:33:58 +0200634 */
Greg Aker58fdee82010-11-10 15:07:09 -0600635 public function set_upload_path($path)
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 {
637 // Make sure it has a trailing slash
638 $this->upload_path = rtrim($path, '/').'/';
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200639 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 }
Barry Mienydd671972010-10-04 16:33:58 +0200641
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200643
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 /**
645 * Set the file name
646 *
647 * This function takes a filename/path as input and looks for the
648 * existence of a file with the same name. If found, it will append a
649 * number to the end of the filename to avoid overwriting a pre-existing file.
650 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200651 * @param string $path
652 * @param string $filename
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200654 */
Greg Aker58fdee82010-11-10 15:07:09 -0600655 public function set_filename($path, $filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000656 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100657 if ($this->encrypt_name === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200658 {
Barry Mienydd671972010-10-04 16:33:58 +0200659 $filename = md5(uniqid(mt_rand())).$this->file_ext;
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 }
Barry Mienydd671972010-10-04 16:33:58 +0200661
Derek Allard2067d1a2008-11-13 22:59:24 +0000662 if ( ! file_exists($path.$filename))
663 {
664 return $filename;
665 }
Barry Mienydd671972010-10-04 16:33:58 +0200666
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 $filename = str_replace($this->file_ext, '', $filename);
Barry Mienydd671972010-10-04 16:33:58 +0200668
Derek Allard2067d1a2008-11-13 22:59:24 +0000669 $new_filename = '';
Adam Jackettccbbea12011-08-21 16:19:11 -0400670 for ($i = 1; $i < $this->max_filename_increment; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200671 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 if ( ! file_exists($path.$filename.$i.$this->file_ext))
673 {
674 $new_filename = $filename.$i.$this->file_ext;
675 break;
676 }
677 }
678
Alex Bilbied261b1e2012-06-02 11:12:16 +0100679 if ($new_filename === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000680 {
681 $this->set_error('upload_bad_filename');
682 return FALSE;
683 }
684 else
685 {
686 return $new_filename;
687 }
688 }
Barry Mienydd671972010-10-04 16:33:58 +0200689
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200691
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 /**
693 * Set Maximum File Size
694 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200695 * @param int $n
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200696 * @return CI_Upload
Barry Mienydd671972010-10-04 16:33:58 +0200697 */
Greg Aker58fdee82010-11-10 15:07:09 -0600698 public function set_max_filesize($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000699 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200700 $this->max_size = ($n < 0) ? 0 : (int) $n;
701 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 }
Barry Mienydd671972010-10-04 16:33:58 +0200703
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200705
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 /**
707 * Set Maximum File Name Length
708 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200709 * @param int $n
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200710 * @return CI_Upload
Barry Mienydd671972010-10-04 16:33:58 +0200711 */
Greg Aker58fdee82010-11-10 15:07:09 -0600712 public function set_max_filename($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000713 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200714 $this->max_filename = ($n < 0) ? 0 : (int) $n;
715 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000716 }
717
718 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200719
Derek Allard2067d1a2008-11-13 22:59:24 +0000720 /**
721 * Set Maximum Image Width
722 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200723 * @param int $n
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200724 * @return CI_Upload
Barry Mienydd671972010-10-04 16:33:58 +0200725 */
Greg Aker58fdee82010-11-10 15:07:09 -0600726 public function set_max_width($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000727 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200728 $this->max_width = ($n < 0) ? 0 : (int) $n;
729 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000730 }
Barry Mienydd671972010-10-04 16:33:58 +0200731
Derek Allard2067d1a2008-11-13 22:59:24 +0000732 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200733
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 /**
735 * Set Maximum Image Height
736 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200737 * @param int $n
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200738 * @return CI_Upload
Barry Mienydd671972010-10-04 16:33:58 +0200739 */
Greg Aker58fdee82010-11-10 15:07:09 -0600740 public function set_max_height($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000741 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200742 $this->max_height = ($n < 0) ? 0 : (int) $n;
743 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000744 }
Barry Mienydd671972010-10-04 16:33:58 +0200745
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200747
Derek Allard2067d1a2008-11-13 22:59:24 +0000748 /**
Andrey Andreev05aa2d62012-12-03 16:06:55 +0200749 * Set minimum image width
750 *
751 * @param int $n
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200752 * @return CI_Upload
Andrey Andreev05aa2d62012-12-03 16:06:55 +0200753 */
754 public function set_min_width($n)
755 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200756 $this->min_width = ($n < 0) ? 0 : (int) $n;
757 return $this;
Andrey Andreev05aa2d62012-12-03 16:06:55 +0200758 }
759
760 // --------------------------------------------------------------------
761
762 /**
763 * Set minimum image height
764 *
765 * @param int $n
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200766 * @return CI_Upload
Andrey Andreev05aa2d62012-12-03 16:06:55 +0200767 */
768 public function set_min_height($n)
769 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200770 $this->min_height = ($n < 0) ? 0 : (int) $n;
771 return $this;
Andrey Andreev05aa2d62012-12-03 16:06:55 +0200772 }
773
774 // --------------------------------------------------------------------
775
776 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000777 * Set Allowed File Types
778 *
Andrey Andreev82179bf2014-02-22 00:29:53 +0200779 * @param mixed $types
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200780 * @return CI_Upload
Barry Mienydd671972010-10-04 16:33:58 +0200781 */
Greg Aker58fdee82010-11-10 15:07:09 -0600782 public function set_allowed_types($types)
Derek Allard2067d1a2008-11-13 22:59:24 +0000783 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200784 $this->allowed_types = (is_array($types) OR $types === '*')
785 ? $types
786 : explode('|', $types);
787 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000788 }
Barry Mienydd671972010-10-04 16:33:58 +0200789
Derek Allard2067d1a2008-11-13 22:59:24 +0000790 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200791
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 /**
793 * Set Image Properties
794 *
795 * Uses GD to determine the width/height/type of image
796 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200797 * @param string $path
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200798 * @return CI_Upload
Barry Mienydd671972010-10-04 16:33:58 +0200799 */
Greg Aker58fdee82010-11-10 15:07:09 -0600800 public function set_image_properties($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000801 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200802 if ($this->is_image() && function_exists('getimagesize'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000803 {
804 if (FALSE !== ($D = @getimagesize($path)))
Barry Mienydd671972010-10-04 16:33:58 +0200805 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000806 $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
807
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300808 $this->image_width = $D[0];
809 $this->image_height = $D[1];
810 $this->image_type = isset($types[$D[2]]) ? $types[$D[2]] : 'unknown';
811 $this->image_size_str = $D[3]; // string containing height and width
Derek Allard2067d1a2008-11-13 22:59:24 +0000812 }
813 }
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200814
815 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 }
Barry Mienydd671972010-10-04 16:33:58 +0200817
Derek Allard2067d1a2008-11-13 22:59:24 +0000818 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200819
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 /**
821 * Set XSS Clean
822 *
823 * Enables the XSS flag so that the file that was uploaded
824 * will be run through the XSS filter.
825 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200826 * @param bool $flag
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200827 * @return CI_Upload
Derek Allard2067d1a2008-11-13 22:59:24 +0000828 */
Greg Aker58fdee82010-11-10 15:07:09 -0600829 public function set_xss_clean($flag = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000830 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100831 $this->xss_clean = ($flag === TRUE);
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200832 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000833 }
Barry Mienydd671972010-10-04 16:33:58 +0200834
Derek Allard2067d1a2008-11-13 22:59:24 +0000835 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200836
Derek Allard2067d1a2008-11-13 22:59:24 +0000837 /**
838 * Validate the image
839 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000840 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200841 */
Greg Aker58fdee82010-11-10 15:07:09 -0600842 public function is_image()
Derek Allard2067d1a2008-11-13 22:59:24 +0000843 {
844 // IE will sometimes return odd mime-types during upload, so here we just standardize all
845 // jpegs or pngs to the same file type.
846
Derek Jones4b9c6292011-07-01 17:40:48 -0500847 $png_mimes = array('image/x-png');
Derek Allard2067d1a2008-11-13 22:59:24 +0000848 $jpeg_mimes = array('image/jpg', 'image/jpe', 'image/jpeg', 'image/pjpeg');
Barry Mienydd671972010-10-04 16:33:58 +0200849
Derek Allard2067d1a2008-11-13 22:59:24 +0000850 if (in_array($this->file_type, $png_mimes))
851 {
852 $this->file_type = 'image/png';
853 }
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300854 elseif (in_array($this->file_type, $jpeg_mimes))
Derek Allard2067d1a2008-11-13 22:59:24 +0000855 {
856 $this->file_type = 'image/jpeg';
857 }
858
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300859 $img_mimes = array('image/gif', 'image/jpeg', 'image/png');
Derek Allard2067d1a2008-11-13 22:59:24 +0000860
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300861 return in_array($this->file_type, $img_mimes, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000862 }
Barry Mienydd671972010-10-04 16:33:58 +0200863
Derek Allard2067d1a2008-11-13 22:59:24 +0000864 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200865
Derek Allard2067d1a2008-11-13 22:59:24 +0000866 /**
867 * Verify that the filetype is allowed
868 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200869 * @param bool $ignore_mime
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200871 */
Greg Aker58fdee82010-11-10 15:07:09 -0600872 public function is_allowed_filetype($ignore_mime = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000873 {
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200874 if ($this->allowed_types === '*')
Derek Jonese12f64e2010-03-02 22:55:08 -0600875 {
876 return TRUE;
877 }
Barry Mienydd671972010-10-04 16:33:58 +0200878
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200879 if (empty($this->allowed_types) OR ! is_array($this->allowed_types))
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 {
881 $this->set_error('upload_no_file_types');
882 return FALSE;
883 }
Barry Mienydd671972010-10-04 16:33:58 +0200884
Derek Jonese9d723f2010-07-12 10:10:59 -0500885 $ext = strtolower(ltrim($this->file_ext, '.'));
Barry Mienydd671972010-10-04 16:33:58 +0200886
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200887 if ( ! in_array($ext, $this->allowed_types, TRUE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 {
Derek Jonese9d723f2010-07-12 10:10:59 -0500889 return FALSE;
890 }
Derek Jonesafa282f2009-02-10 17:11:52 +0000891
Barry Mienydd671972010-10-04 16:33:58 +0200892 // Images get some additional checks
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200893 if (in_array($ext, array('gif', 'jpg', 'jpeg', 'jpe', 'png'), TRUE) && @getimagesize($this->file_temp) === FALSE)
Derek Jonese9d723f2010-07-12 10:10:59 -0500894 {
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200895 return FALSE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500896 }
Barry Mienydd671972010-10-04 16:33:58 +0200897
Derek Jonese9d723f2010-07-12 10:10:59 -0500898 if ($ignore_mime === TRUE)
899 {
900 return TRUE;
901 }
Barry Mienydd671972010-10-04 16:33:58 +0200902
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200903 if (isset($this->_mimes[$ext]))
Derek Jonese9d723f2010-07-12 10:10:59 -0500904 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200905 return is_array($this->_mimes[$ext])
906 ? in_array($this->file_type, $this->_mimes[$ext], TRUE)
907 : ($this->_mimes[$ext] === $this->file_type);
Derek Allard2067d1a2008-11-13 22:59:24 +0000908 }
Barry Mienydd671972010-10-04 16:33:58 +0200909
Derek Allard2067d1a2008-11-13 22:59:24 +0000910 return FALSE;
911 }
Barry Mienydd671972010-10-04 16:33:58 +0200912
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200914
Derek Allard2067d1a2008-11-13 22:59:24 +0000915 /**
916 * Verify that the file is within the allowed size
917 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000918 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200919 */
Greg Aker58fdee82010-11-10 15:07:09 -0600920 public function is_allowed_filesize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100922 return ($this->max_size === 0 OR $this->max_size > $this->file_size);
Derek Allard2067d1a2008-11-13 22:59:24 +0000923 }
Barry Mienydd671972010-10-04 16:33:58 +0200924
Derek Allard2067d1a2008-11-13 22:59:24 +0000925 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200926
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 /**
928 * Verify that the image is within the allowed width/height
929 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000930 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200931 */
Greg Aker58fdee82010-11-10 15:07:09 -0600932 public function is_allowed_dimensions()
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 {
934 if ( ! $this->is_image())
935 {
936 return TRUE;
937 }
938
939 if (function_exists('getimagesize'))
940 {
941 $D = @getimagesize($this->file_temp);
942
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300943 if ($this->max_width > 0 && $D[0] > $this->max_width)
Derek Allard2067d1a2008-11-13 22:59:24 +0000944 {
945 return FALSE;
946 }
947
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300948 if ($this->max_height > 0 && $D[1] > $this->max_height)
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 {
950 return FALSE;
951 }
Andrey Andreev05aa2d62012-12-03 16:06:55 +0200952
953 if ($this->min_width > 0 && $D[0] < $this->min_width)
954 {
955 return FALSE;
956 }
957
958 if ($this->min_height > 0 && $D[1] < $this->min_height)
959 {
960 return FALSE;
961 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000962 }
963
964 return TRUE;
965 }
Barry Mienydd671972010-10-04 16:33:58 +0200966
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200968
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 /**
970 * Validate Upload Path
971 *
972 * Verifies that it is a valid upload path with proper permissions.
973 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000974 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200975 */
Greg Aker58fdee82010-11-10 15:07:09 -0600976 public function validate_upload_path()
Derek Allard2067d1a2008-11-13 22:59:24 +0000977 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100978 if ($this->upload_path === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000979 {
980 $this->set_error('upload_no_filepath');
981 return FALSE;
982 }
Barry Mienydd671972010-10-04 16:33:58 +0200983
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200984 if (realpath($this->upload_path) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300986 $this->upload_path = str_replace('\\', '/', realpath($this->upload_path));
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 }
988
Andrey Andreev382b5132014-02-26 18:41:59 +0200989 if ( ! is_dir($this->upload_path))
Derek Allard2067d1a2008-11-13 22:59:24 +0000990 {
991 $this->set_error('upload_no_filepath');
992 return FALSE;
993 }
994
995 if ( ! is_really_writable($this->upload_path))
996 {
997 $this->set_error('upload_not_writable');
998 return FALSE;
999 }
1000
Andrey Andreev8e6f7a92012-03-26 20:13:00 +03001001 $this->upload_path = preg_replace('/(.+?)\/*$/', '\\1/', $this->upload_path);
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 return TRUE;
1003 }
Barry Mienydd671972010-10-04 16:33:58 +02001004
Derek Allard2067d1a2008-11-13 22:59:24 +00001005 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001006
Derek Allard2067d1a2008-11-13 22:59:24 +00001007 /**
1008 * Extract the file extension
1009 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001010 * @param string $filename
Derek Allard2067d1a2008-11-13 22:59:24 +00001011 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001012 */
Greg Aker58fdee82010-11-10 15:07:09 -06001013 public function get_extension($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +00001014 {
1015 $x = explode('.', $filename);
Adrianf496d132013-06-24 15:56:45 +02001016
Adrian74a228b2013-06-25 12:09:22 +02001017 if (count($x) === 1)
1018 {
1019 return '';
1020 }
1021
1022 $ext = ($this->file_ext_tolower) ? strtolower(end($x)) : end($x);
1023 return '.'.$ext;
Barry Mienydd671972010-10-04 16:33:58 +02001024 }
1025
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001027
Derek Allard2067d1a2008-11-13 22:59:24 +00001028 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001029 * Limit the File Name Length
1030 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001031 * @param string $filename
1032 * @param int $length
Derek Allard2067d1a2008-11-13 22:59:24 +00001033 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001034 */
Greg Aker58fdee82010-11-10 15:07:09 -06001035 public function limit_filename_length($filename, $length)
Derek Allard2067d1a2008-11-13 22:59:24 +00001036 {
1037 if (strlen($filename) < $length)
1038 {
1039 return $filename;
1040 }
Barry Mienydd671972010-10-04 16:33:58 +02001041
Derek Allard2067d1a2008-11-13 22:59:24 +00001042 $ext = '';
1043 if (strpos($filename, '.') !== FALSE)
1044 {
1045 $parts = explode('.', $filename);
1046 $ext = '.'.array_pop($parts);
1047 $filename = implode('.', $parts);
1048 }
Barry Mienydd671972010-10-04 16:33:58 +02001049
Derek Allard2067d1a2008-11-13 22:59:24 +00001050 return substr($filename, 0, ($length - strlen($ext))).$ext;
1051 }
1052
1053 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001054
Derek Allard2067d1a2008-11-13 22:59:24 +00001055 /**
1056 * Runs the file through the XSS clean function
1057 *
1058 * This prevents people from embedding malicious code in their files.
1059 * I'm not sure that it won't negatively affect certain files in unexpected ways,
1060 * but so far I haven't found that it causes trouble.
1061 *
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +02001062 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001063 */
Greg Aker58fdee82010-11-10 15:07:09 -06001064 public function do_xss_clean()
Barry Mienydd671972010-10-04 16:33:58 +02001065 {
Derek Jonese9d723f2010-07-12 10:10:59 -05001066 $file = $this->file_temp;
Barry Mienydd671972010-10-04 16:33:58 +02001067
Andrey Andreev5036c9c2012-06-04 15:34:56 +03001068 if (filesize($file) == 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001069 {
1070 return FALSE;
1071 }
Barry Mienydd671972010-10-04 16:33:58 +02001072
Andrey Andreevc839d282012-06-07 14:35:27 +03001073 if (memory_get_usage() && ($memory_limit = ini_get('memory_limit')))
Greg Akerf82e51c2010-04-14 19:33:50 -05001074 {
Andrey Andreevc839d282012-06-07 14:35:27 +03001075 $memory_limit *= 1024 * 1024;
Barry Mienydd671972010-10-04 16:33:58 +02001076
Greg Akerc78a2592010-06-09 11:45:32 -05001077 // There was a bug/behavioural change in PHP 5.2, where numbers over one million get output
Andrey Andreev8e6f7a92012-03-26 20:13:00 +03001078 // into scientific notation. number_format() ensures this number is an integer
Greg Akerc78a2592010-06-09 11:45:32 -05001079 // http://bugs.php.net/bug.php?id=43053
Barry Mienydd671972010-10-04 16:33:58 +02001080
Andrey Andreevc839d282012-06-07 14:35:27 +03001081 $memory_limit = number_format(ceil(filesize($file) + $memory_limit), 0, '.', '');
Barry Mienydd671972010-10-04 16:33:58 +02001082
Andrey Andreevc839d282012-06-07 14:35:27 +03001083 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 -05001084 }
1085
1086 // If the file being uploaded is an image, then we should have no problem with XSS attacks (in theory), but
1087 // 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 +03001088 // using IE who looks at the image. It does this by inspecting the first 255 bytes of an image. To get around this
1089 // 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 +02001090 // 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 -05001091 // attempted XSS attack.
1092
1093 if (function_exists('getimagesize') && @getimagesize($file) !== FALSE)
1094 {
Barry Mienydd671972010-10-04 16:33:58 +02001095 if (($file = @fopen($file, 'rb')) === FALSE) // "b" to force binary
1096 {
Greg Akerf82e51c2010-04-14 19:33:50 -05001097 return FALSE; // Couldn't open the file, return FALSE
Barry Mienydd671972010-10-04 16:33:58 +02001098 }
Greg Akerf82e51c2010-04-14 19:33:50 -05001099
Barry Mienydd671972010-10-04 16:33:58 +02001100 $opening_bytes = fread($file, 256);
1101 fclose($file);
Greg Akerf82e51c2010-04-14 19:33:50 -05001102
1103 // These are known to throw IE into mime-type detection chaos
1104 // <a, <body, <head, <html, <img, <plaintext, <pre, <script, <table, <title
1105 // title is basically just in SVG, but we filter it anyhow
1106
vlakoff35672462013-02-15 01:36:04 +01001107 // if it's an image or no "triggers" detected in the first 256 bytes - we're good
Andrey Andreevc839d282012-06-07 14:35:27 +03001108 return ! preg_match('/<(a|body|head|html|img|plaintext|pre|script|table|title)[\s>]/i', $opening_bytes);
Greg Akerf82e51c2010-04-14 19:33:50 -05001109 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001110
1111 if (($data = @file_get_contents($file)) === FALSE)
1112 {
1113 return FALSE;
1114 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001115
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +02001116 return $this->_CI->security->xss_clean($data, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001117 }
Barry Mienydd671972010-10-04 16:33:58 +02001118
Derek Allard2067d1a2008-11-13 22:59:24 +00001119 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001120
Derek Allard2067d1a2008-11-13 22:59:24 +00001121 /**
1122 * Set an error message
1123 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001124 * @param string $msg
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +02001125 * @return CI_Upload
Barry Mienydd671972010-10-04 16:33:58 +02001126 */
Greg Aker58fdee82010-11-10 15:07:09 -06001127 public function set_error($msg)
Derek Allard2067d1a2008-11-13 22:59:24 +00001128 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +02001129 $this->_CI->lang->load('upload');
Barry Mienydd671972010-10-04 16:33:58 +02001130
Andrey Andreev119d8a72014-01-08 15:27:53 +02001131 is_array($msg) OR $msg = array($msg);
Darren Benney4a8d1902013-03-30 20:07:49 +00001132
Darren Benney38d5f4b2013-03-30 21:00:38 +00001133 foreach ($msg as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001134 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +02001135 $msg = ($this->_CI->lang->line($val) === FALSE) ? $val : $this->_CI->lang->line($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001136 $this->error_msg[] = $msg;
1137 log_message('error', $msg);
1138 }
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +02001139
1140 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +00001141 }
Barry Mienydd671972010-10-04 16:33:58 +02001142
Derek Allard2067d1a2008-11-13 22:59:24 +00001143 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001144
Derek Allard2067d1a2008-11-13 22:59:24 +00001145 /**
1146 * Display the error message
1147 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001148 * @param string $open
1149 * @param string $close
Derek Allard2067d1a2008-11-13 22:59:24 +00001150 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001151 */
Greg Aker58fdee82010-11-10 15:07:09 -06001152 public function display_errors($open = '<p>', $close = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +00001153 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +03001154 return (count($this->error_msg) > 0) ? $open.implode($close.$open, $this->error_msg).$close : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001155 }
Barry Mienydd671972010-10-04 16:33:58 +02001156
Derek Allard2067d1a2008-11-13 22:59:24 +00001157 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001158
Derek Allard2067d1a2008-11-13 22:59:24 +00001159 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001160 * Prep Filename
1161 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001162 * Prevents possible script execution from Apache's handling
1163 * of files' multiple extensions.
Derek Allard2067d1a2008-11-13 22:59:24 +00001164 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001165 * @link http://httpd.apache.org/docs/1.3/mod/mod_mime.html#multipleext
1166 *
1167 * @param string $filename
Derek Allard2067d1a2008-11-13 22:59:24 +00001168 * @return string
1169 */
Greg Aker58fdee82010-11-10 15:07:09 -06001170 protected function _prep_filename($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +00001171 {
Andrey Andreevf38c9c22014-08-27 14:56:31 +03001172 if ($this->mod_mime_fix === FALSE OR $this->allowed_types === '*' OR ($ext_pos = strrpos($filename, '.')) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001173 {
1174 return $filename;
1175 }
Derek Allard616dab82009-02-16 15:44:32 +00001176
Andrey Andreevf38c9c22014-08-27 14:56:31 +03001177 $ext = substr($filename, $ext_pos);
1178 $filename = substr($filename, 0, $ext_pos);
1179 return str_replace('.', '_', $filename).$ext;
Derek Allard2067d1a2008-11-13 22:59:24 +00001180 }
1181
1182 // --------------------------------------------------------------------
1183
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001184 /**
1185 * File MIME type
1186 *
1187 * Detects the (actual) MIME type of the uploaded file, if possible.
1188 * The input array is expected to be $_FILES[$field]
1189 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001190 * @param array $file
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001191 * @return void
1192 */
1193 protected function _file_mime_type($file)
1194 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001195 // We'll need this to validate the MIME info string (e.g. text/plain; charset=us-ascii)
Andrey Andreevf7aed122011-12-13 11:01:06 +02001196 $regexp = '/^([a-z\-]+\/[a-z0-9\-\.\+]+)(;\s.+)?$/';
Andrey Andreeva49e3812011-12-09 13:05:22 +02001197
1198 /* Fileinfo extension - most reliable method
1199 *
1200 * Unfortunately, prior to PHP 5.3 - it's only available as a PECL extension and the
1201 * more convenient FILEINFO_MIME_TYPE flag doesn't exist.
1202 */
1203 if (function_exists('finfo_file'))
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001204 {
Andrey Andreevd60e7002012-06-17 00:03:03 +03001205 $finfo = @finfo_open(FILEINFO_MIME);
Andrey Andreeva49e3812011-12-09 13:05:22 +02001206 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 +03001207 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001208 $mime = @finfo_file($finfo, $file['tmp_name']);
1209 finfo_close($finfo);
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001210
1211 /* According to the comments section of the PHP manual page,
1212 * it is possible that this function returns an empty string
Andrey Andreev2cec39f2011-09-24 22:59:37 +03001213 * for some files (e.g. if they don't exist in the magic MIME database)
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001214 */
Andrey Andreeva49e3812011-12-09 13:05:22 +02001215 if (is_string($mime) && preg_match($regexp, $mime, $matches))
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001216 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001217 $this->file_type = $matches[1];
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001218 return;
1219 }
1220 }
1221 }
1222
Andrey Andreeva49e3812011-12-09 13:05:22 +02001223 /* This is an ugly hack, but UNIX-type systems provide a "native" way to detect the file type,
1224 * which is still more secure than depending on the value of $_FILES[$field]['type'], and as it
1225 * was reported in issue #750 (https://github.com/EllisLab/CodeIgniter/issues/750) - it's better
1226 * than mime_content_type() as well, hence the attempts to try calling the command line with
1227 * three different functions.
Andrey Andreev6700b932011-09-24 14:25:33 +03001228 *
1229 * Notes:
Andrey Andreev59654312011-12-02 14:28:54 +02001230 * - the DIRECTORY_SEPARATOR comparison ensures that we're not on a Windows system
Andrey Andreeva49e3812011-12-09 13:05:22 +02001231 * - many system admins would disable the exec(), shell_exec(), popen() and similar functions
vlakofff3994252013-02-19 01:45:23 +01001232 * due to security concerns, hence the function_usable() checks
Andrey Andreev6700b932011-09-24 14:25:33 +03001233 */
Andrey Andreeva49e3812011-12-09 13:05:22 +02001234 if (DIRECTORY_SEPARATOR !== '\\')
Andrey Andreev6700b932011-09-24 14:25:33 +03001235 {
Andrey Andreevd60e7002012-06-17 00:03:03 +03001236 $cmd = function_exists('escapeshellarg')
1237 ? 'file --brief --mime '.escapeshellarg($file['tmp_name']).' 2>&1'
1238 : 'file --brief --mime '.$file['tmp_name'].' 2>&1';
Andrey Andreeva49e3812011-12-09 13:05:22 +02001239
Andrey Andreeve9d2dc82012-11-07 14:23:29 +02001240 if (function_usable('exec'))
Andrey Andreev6700b932011-09-24 14:25:33 +03001241 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001242 /* 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 +01001243 * 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 +02001244 * anything that could already be set for $mime previously. This effectively makes the second parameter a dummy
1245 * value, which is only put to allow us to get the return status code.
1246 */
1247 $mime = @exec($cmd, $mime, $return_status);
1248 if ($return_status === 0 && is_string($mime) && preg_match($regexp, $mime, $matches))
1249 {
1250 $this->file_type = $matches[1];
1251 return;
1252 }
1253 }
1254
Andrey Andreevf6274742014-02-20 18:05:58 +02001255 if ( ! ini_get('safe_mode') && function_usable('shell_exec'))
Andrey Andreeva49e3812011-12-09 13:05:22 +02001256 {
1257 $mime = @shell_exec($cmd);
1258 if (strlen($mime) > 0)
1259 {
1260 $mime = explode("\n", trim($mime));
1261 if (preg_match($regexp, $mime[(count($mime) - 1)], $matches))
1262 {
1263 $this->file_type = $matches[1];
1264 return;
1265 }
1266 }
1267 }
1268
Andrey Andreeve9d2dc82012-11-07 14:23:29 +02001269 if (function_usable('popen'))
Andrey Andreeva49e3812011-12-09 13:05:22 +02001270 {
1271 $proc = @popen($cmd, 'r');
1272 if (is_resource($proc))
1273 {
tubalmartin010f1f42012-03-03 22:24:31 +01001274 $mime = @fread($proc, 512);
Andrey Andreeva49e3812011-12-09 13:05:22 +02001275 @pclose($proc);
1276 if ($mime !== FALSE)
1277 {
1278 $mime = explode("\n", trim($mime));
1279 if (preg_match($regexp, $mime[(count($mime) - 1)], $matches))
1280 {
1281 $this->file_type = $matches[1];
1282 return;
1283 }
1284 }
1285 }
1286 }
1287 }
1288
1289 // Fall back to the deprecated mime_content_type(), if available (still better than $_FILES[$field]['type'])
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001290 if (function_exists('mime_content_type'))
1291 {
1292 $this->file_type = @mime_content_type($file['tmp_name']);
Andrey Andreeva49e3812011-12-09 13:05:22 +02001293 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 +03001294 {
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001295 return;
1296 }
1297 }
1298
1299 $this->file_type = $file['type'];
1300 }
1301
Derek Allard2067d1a2008-11-13 22:59:24 +00001302}
Derek Allard2067d1a2008-11-13 22:59:24 +00001303
1304/* End of file Upload.php */
Adrian74a228b2013-06-25 12:09:22 +02001305/* Location: ./system/libraries/Upload.php */