blob: b0490de30aaeadd18ced45a662a208d2e269b67e [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Greg Aker0711dc82011-01-05 10:49:40 -06009 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * File Uploading Class
20 *
21 * @package CodeIgniter
22 * @subpackage Libraries
23 * @category Uploads
24 * @author ExpressionEngine Dev Team
25 * @link http://codeigniter.com/user_guide/libraries/file_uploading.html
26 */
27class CI_Upload {
Barry Mienydd671972010-10-04 16:33:58 +020028
Greg Aker58fdee82010-11-10 15:07:09 -060029 public $max_size = 0;
30 public $max_width = 0;
31 public $max_height = 0;
32 public $max_filename = 0;
33 public $allowed_types = "";
34 public $file_temp = "";
35 public $file_name = "";
36 public $orig_name = "";
37 public $file_type = "";
38 public $file_size = "";
39 public $file_ext = "";
40 public $upload_path = "";
41 public $overwrite = FALSE;
42 public $encrypt_name = FALSE;
43 public $is_image = FALSE;
44 public $image_width = '';
45 public $image_height = '';
46 public $image_type = '';
47 public $image_size_str = '';
48 public $error_msg = array();
49 public $mimes = array();
50 public $remove_spaces = TRUE;
51 public $xss_clean = FALSE;
52 public $temp_prefix = "temp_file_";
53 public $client_name = '';
Barry Mienydd671972010-10-04 16:33:58 +020054
Greg Aker58fdee82010-11-10 15:07:09 -060055 protected $_file_name_override = '';
Barry Mienydd671972010-10-04 16:33:58 +020056
Derek Allard2067d1a2008-11-13 22:59:24 +000057 /**
58 * Constructor
59 *
60 * @access public
61 */
Greg Aker58fdee82010-11-10 15:07:09 -060062 public function __construct($props = array())
Derek Allard2067d1a2008-11-13 22:59:24 +000063 {
64 if (count($props) > 0)
65 {
66 $this->initialize($props);
67 }
Barry Mienydd671972010-10-04 16:33:58 +020068
Derek Allard2067d1a2008-11-13 22:59:24 +000069 log_message('debug', "Upload Class Initialized");
70 }
Barry Mienydd671972010-10-04 16:33:58 +020071
Derek Allard2067d1a2008-11-13 22:59:24 +000072 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020073
Derek Allard2067d1a2008-11-13 22:59:24 +000074 /**
75 * Initialize preferences
76 *
Derek Allard2067d1a2008-11-13 22:59:24 +000077 * @param array
78 * @return void
Barry Mienydd671972010-10-04 16:33:58 +020079 */
Greg Aker58fdee82010-11-10 15:07:09 -060080 public function initialize($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +000081 {
82 $defaults = array(
83 'max_size' => 0,
84 'max_width' => 0,
85 'max_height' => 0,
86 'max_filename' => 0,
87 'allowed_types' => "",
88 'file_temp' => "",
89 'file_name' => "",
90 'orig_name' => "",
91 'file_type' => "",
92 'file_size' => "",
93 'file_ext' => "",
94 'upload_path' => "",
95 'overwrite' => FALSE,
96 'encrypt_name' => FALSE,
97 'is_image' => FALSE,
98 'image_width' => '',
99 'image_height' => '',
100 'image_type' => '',
101 'image_size_str' => '',
102 'error_msg' => array(),
103 'mimes' => array(),
104 'remove_spaces' => TRUE,
105 'xss_clean' => FALSE,
Derek Jonese9d723f2010-07-12 10:10:59 -0500106 'temp_prefix' => "temp_file_",
107 'client_name' => ''
Barry Mienydd671972010-10-04 16:33:58 +0200108 );
109
110
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 foreach ($defaults as $key => $val)
112 {
113 if (isset($config[$key]))
114 {
115 $method = 'set_'.$key;
116 if (method_exists($this, $method))
117 {
118 $this->$method($config[$key]);
119 }
120 else
121 {
122 $this->$key = $config[$key];
Barry Mienydd671972010-10-04 16:33:58 +0200123 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 }
125 else
126 {
127 $this->$key = $val;
128 }
129 }
Barry Mienydd671972010-10-04 16:33:58 +0200130
Derek Jonese9d723f2010-07-12 10:10:59 -0500131 // if a file_name was provided in the config, use it instead of the user input
132 // supplied file name for all uploads until initialized again
133 $this->_file_name_override = $this->file_name;
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 }
Barry Mienydd671972010-10-04 16:33:58 +0200135
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200137
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 /**
139 * Perform the file upload
140 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200142 */
Greg Aker58fdee82010-11-10 15:07:09 -0600143 public function do_upload($field = 'userfile')
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 {
Eric Barnes92808342011-03-18 09:02:37 -0400145
Robin Sowell0ecc0622011-01-30 16:47:43 -0500146 // Is $_FILES[$field] set? If not, no reason to continue.
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 if ( ! isset($_FILES[$field]))
148 {
149 $this->set_error('upload_no_file_selected');
150 return FALSE;
151 }
Barry Mienydd671972010-10-04 16:33:58 +0200152
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 // Is the upload path valid?
154 if ( ! $this->validate_upload_path())
155 {
156 // errors will already be set by validate_upload_path() so just return FALSE
157 return FALSE;
158 }
159
160 // Was the file able to be uploaded? If not, determine the reason why.
161 if ( ! is_uploaded_file($_FILES[$field]['tmp_name']))
162 {
163 $error = ( ! isset($_FILES[$field]['error'])) ? 4 : $_FILES[$field]['error'];
164
165 switch($error)
166 {
167 case 1: // UPLOAD_ERR_INI_SIZE
168 $this->set_error('upload_file_exceeds_limit');
169 break;
170 case 2: // UPLOAD_ERR_FORM_SIZE
171 $this->set_error('upload_file_exceeds_form_limit');
172 break;
173 case 3: // UPLOAD_ERR_PARTIAL
Barry Mienydd671972010-10-04 16:33:58 +0200174 $this->set_error('upload_file_partial');
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 break;
176 case 4: // UPLOAD_ERR_NO_FILE
Barry Mienydd671972010-10-04 16:33:58 +0200177 $this->set_error('upload_no_file_selected');
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 break;
179 case 6: // UPLOAD_ERR_NO_TMP_DIR
180 $this->set_error('upload_no_temp_directory');
181 break;
182 case 7: // UPLOAD_ERR_CANT_WRITE
183 $this->set_error('upload_unable_to_write_file');
184 break;
185 case 8: // UPLOAD_ERR_EXTENSION
186 $this->set_error('upload_stopped_by_extension');
187 break;
Derek Jones37f4b9c2011-07-01 17:56:50 -0500188 default : $this->set_error('upload_no_file_selected');
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 break;
190 }
191
192 return FALSE;
193 }
194
Derek Jonese9d723f2010-07-12 10:10:59 -0500195
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 // Set the uploaded data as class variables
Barry Mienydd671972010-10-04 16:33:58 +0200197 $this->file_temp = $_FILES[$field]['tmp_name'];
198 $this->file_size = $_FILES[$field]['size'];
Andrey Andreev6700b932011-09-24 14:25:33 +0300199 $this->_file_mime_type($_FILES[$field]);
200 $this->file_type = preg_replace("/^(.+?);.*$/", "\\1", $this->file_type);
Derek Jones616fb022010-04-22 16:52:18 -0500201 $this->file_type = strtolower(trim(stripslashes($this->file_type), '"'));
Derek Jonese9d723f2010-07-12 10:10:59 -0500202 $this->file_name = $this->_prep_filename($_FILES[$field]['name']);
203 $this->file_ext = $this->get_extension($this->file_name);
204 $this->client_name = $this->file_name;
Barry Mienydd671972010-10-04 16:33:58 +0200205
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 // Is the file type allowed to be uploaded?
207 if ( ! $this->is_allowed_filetype())
208 {
209 $this->set_error('upload_invalid_filetype');
210 return FALSE;
211 }
212
Derek Jonese9d723f2010-07-12 10:10:59 -0500213 // if we're overriding, let's now make sure the new name and type is allowed
214 if ($this->_file_name_override != '')
215 {
216 $this->file_name = $this->_prep_filename($this->_file_name_override);
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000217
218 // If no extension was provided in the file_name config item, use the uploaded one
Pascal Kriete14287f32011-02-14 13:39:34 -0500219 if (strpos($this->_file_name_override, '.') === FALSE)
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000220 {
221 $this->file_name .= $this->file_ext;
222 }
223
224 // An extension was provided, lets have it!
225 else
226 {
227 $this->file_ext = $this->get_extension($this->_file_name_override);
228 }
Derek Jonese9d723f2010-07-12 10:10:59 -0500229
230 if ( ! $this->is_allowed_filetype(TRUE))
231 {
232 $this->set_error('upload_invalid_filetype');
Barry Mienydd671972010-10-04 16:33:58 +0200233 return FALSE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500234 }
235 }
Barry Mienydd671972010-10-04 16:33:58 +0200236
Derek Jonese9d723f2010-07-12 10:10:59 -0500237 // Convert the file size to kilobytes
238 if ($this->file_size > 0)
239 {
240 $this->file_size = round($this->file_size/1024, 2);
241 }
242
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 // Is the file size within the allowed maximum?
244 if ( ! $this->is_allowed_filesize())
245 {
246 $this->set_error('upload_invalid_filesize');
247 return FALSE;
248 }
249
250 // Are the image dimensions within the allowed size?
251 // Note: This can fail if the server has an open_basdir restriction.
252 if ( ! $this->is_allowed_dimensions())
253 {
254 $this->set_error('upload_invalid_dimensions');
255 return FALSE;
256 }
257
258 // Sanitize the file name for security
259 $this->file_name = $this->clean_file_name($this->file_name);
Barry Mienydd671972010-10-04 16:33:58 +0200260
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 // Truncate the file name if it's too long
262 if ($this->max_filename > 0)
263 {
264 $this->file_name = $this->limit_filename_length($this->file_name, $this->max_filename);
265 }
266
267 // Remove white spaces in the name
268 if ($this->remove_spaces == TRUE)
269 {
270 $this->file_name = preg_replace("/\s+/", "_", $this->file_name);
271 }
272
273 /*
274 * Validate the file name
275 * This function appends an number onto the end of
276 * the file if one with the same name already exists.
277 * If it returns false there was a problem.
278 */
279 $this->orig_name = $this->file_name;
280
281 if ($this->overwrite == FALSE)
282 {
283 $this->file_name = $this->set_filename($this->upload_path, $this->file_name);
Barry Mienydd671972010-10-04 16:33:58 +0200284
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 if ($this->file_name === FALSE)
286 {
287 return FALSE;
288 }
289 }
290
291 /*
Derek Jonese9d723f2010-07-12 10:10:59 -0500292 * Run the file through the XSS hacking filter
293 * This helps prevent malicious code from being
Derek Jones37f4b9c2011-07-01 17:56:50 -0500294 * embedded within a file. Scripts can easily
Derek Jonese9d723f2010-07-12 10:10:59 -0500295 * be disguised as images or other file types.
296 */
297 if ($this->xss_clean)
298 {
299 if ($this->do_xss_clean() === FALSE)
300 {
301 $this->set_error('upload_unable_to_write_file');
302 return FALSE;
303 }
304 }
305
306 /*
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 * Move the file to the final destination
308 * To deal with different server configurations
Derek Jones37f4b9c2011-07-01 17:56:50 -0500309 * we'll attempt to use copy() first. If that fails
310 * we'll use move_uploaded_file(). One of the two should
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 * reliably work in most environments
312 */
313 if ( ! @copy($this->file_temp, $this->upload_path.$this->file_name))
314 {
315 if ( ! @move_uploaded_file($this->file_temp, $this->upload_path.$this->file_name))
316 {
Barry Mienydd671972010-10-04 16:33:58 +0200317 $this->set_error('upload_destination_error');
318 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 }
320 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000321
322 /*
323 * Set the finalized image dimensions
324 * This sets the image width/height (assuming the
Derek Jones37f4b9c2011-07-01 17:56:50 -0500325 * file was an image). We use this information
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 * in the "data" function.
327 */
328 $this->set_image_properties($this->upload_path.$this->file_name);
329
330 return TRUE;
331 }
Barry Mienydd671972010-10-04 16:33:58 +0200332
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200334
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 /**
336 * Finalized Data Array
Barry Mienydd671972010-10-04 16:33:58 +0200337 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 * Returns an associative array containing all of the information
339 * related to the upload, allowing the developer easy access in one array.
340 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200342 */
Greg Aker58fdee82010-11-10 15:07:09 -0600343 public function data()
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 {
345 return array (
346 'file_name' => $this->file_name,
347 'file_type' => $this->file_type,
348 'file_path' => $this->upload_path,
349 'full_path' => $this->upload_path.$this->file_name,
350 'raw_name' => str_replace($this->file_ext, '', $this->file_name),
351 'orig_name' => $this->orig_name,
Derek Jonese9d723f2010-07-12 10:10:59 -0500352 'client_name' => $this->client_name,
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 'file_ext' => $this->file_ext,
354 'file_size' => $this->file_size,
355 'is_image' => $this->is_image(),
356 'image_width' => $this->image_width,
357 'image_height' => $this->image_height,
358 'image_type' => $this->image_type,
359 'image_size_str' => $this->image_size_str,
360 );
361 }
Barry Mienydd671972010-10-04 16:33:58 +0200362
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200364
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 /**
366 * Set Upload Path
367 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 * @param string
369 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200370 */
Greg Aker58fdee82010-11-10 15:07:09 -0600371 public function set_upload_path($path)
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 {
373 // Make sure it has a trailing slash
374 $this->upload_path = rtrim($path, '/').'/';
375 }
Barry Mienydd671972010-10-04 16:33:58 +0200376
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200378
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 /**
380 * Set the file name
381 *
382 * This function takes a filename/path as input and looks for the
383 * existence of a file with the same name. If found, it will append a
384 * number to the end of the filename to avoid overwriting a pre-existing file.
385 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 * @param string
387 * @param string
388 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200389 */
Greg Aker58fdee82010-11-10 15:07:09 -0600390 public function set_filename($path, $filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 {
392 if ($this->encrypt_name == TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200393 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 mt_srand();
Barry Mienydd671972010-10-04 16:33:58 +0200395 $filename = md5(uniqid(mt_rand())).$this->file_ext;
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 }
Barry Mienydd671972010-10-04 16:33:58 +0200397
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 if ( ! file_exists($path.$filename))
399 {
400 return $filename;
401 }
Barry Mienydd671972010-10-04 16:33:58 +0200402
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 $filename = str_replace($this->file_ext, '', $filename);
Barry Mienydd671972010-10-04 16:33:58 +0200404
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 $new_filename = '';
406 for ($i = 1; $i < 100; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200407 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 if ( ! file_exists($path.$filename.$i.$this->file_ext))
409 {
410 $new_filename = $filename.$i.$this->file_ext;
411 break;
412 }
413 }
414
415 if ($new_filename == '')
416 {
417 $this->set_error('upload_bad_filename');
418 return FALSE;
419 }
420 else
421 {
422 return $new_filename;
423 }
424 }
Barry Mienydd671972010-10-04 16:33:58 +0200425
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200427
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 /**
429 * Set Maximum File Size
430 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 * @param integer
432 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200433 */
Greg Aker58fdee82010-11-10 15:07:09 -0600434 public function set_max_filesize($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 {
436 $this->max_size = ((int) $n < 0) ? 0: (int) $n;
437 }
Barry Mienydd671972010-10-04 16:33:58 +0200438
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200440
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 /**
442 * Set Maximum File Name Length
443 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 * @param integer
445 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200446 */
Greg Aker58fdee82010-11-10 15:07:09 -0600447 public function set_max_filename($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 {
449 $this->max_filename = ((int) $n < 0) ? 0: (int) $n;
450 }
451
452 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200453
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 /**
455 * Set Maximum Image Width
456 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 * @param integer
458 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200459 */
Greg Aker58fdee82010-11-10 15:07:09 -0600460 public function set_max_width($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 {
462 $this->max_width = ((int) $n < 0) ? 0: (int) $n;
463 }
Barry Mienydd671972010-10-04 16:33:58 +0200464
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200466
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 /**
468 * Set Maximum Image Height
469 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 * @param integer
471 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200472 */
Greg Aker58fdee82010-11-10 15:07:09 -0600473 public function set_max_height($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 {
475 $this->max_height = ((int) $n < 0) ? 0: (int) $n;
476 }
Barry Mienydd671972010-10-04 16:33:58 +0200477
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200479
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 /**
481 * Set Allowed File Types
482 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 * @param string
484 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200485 */
Greg Aker58fdee82010-11-10 15:07:09 -0600486 public function set_allowed_types($types)
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 {
Derek Jonese12f64e2010-03-02 22:55:08 -0600488 if ( ! is_array($types) && $types == '*')
489 {
490 $this->allowed_types = '*';
491 return;
492 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 $this->allowed_types = explode('|', $types);
494 }
Barry Mienydd671972010-10-04 16:33:58 +0200495
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200497
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 /**
499 * Set Image Properties
500 *
501 * Uses GD to determine the width/height/type of image
502 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 * @param string
504 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200505 */
Greg Aker58fdee82010-11-10 15:07:09 -0600506 public function set_image_properties($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 {
508 if ( ! $this->is_image())
509 {
510 return;
511 }
512
513 if (function_exists('getimagesize'))
514 {
515 if (FALSE !== ($D = @getimagesize($path)))
Barry Mienydd671972010-10-04 16:33:58 +0200516 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
518
519 $this->image_width = $D['0'];
520 $this->image_height = $D['1'];
521 $this->image_type = ( ! isset($types[$D['2']])) ? 'unknown' : $types[$D['2']];
Derek Jones37f4b9c2011-07-01 17:56:50 -0500522 $this->image_size_str = $D['3']; // string containing height and width
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 }
524 }
525 }
Barry Mienydd671972010-10-04 16:33:58 +0200526
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200528
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 /**
530 * Set XSS Clean
531 *
532 * Enables the XSS flag so that the file that was uploaded
533 * will be run through the XSS filter.
534 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 * @param bool
536 * @return void
537 */
Greg Aker58fdee82010-11-10 15:07:09 -0600538 public function set_xss_clean($flag = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 {
540 $this->xss_clean = ($flag == TRUE) ? TRUE : FALSE;
541 }
Barry Mienydd671972010-10-04 16:33:58 +0200542
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200544
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 /**
546 * Validate the image
547 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200549 */
Greg Aker58fdee82010-11-10 15:07:09 -0600550 public function is_image()
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 {
552 // IE will sometimes return odd mime-types during upload, so here we just standardize all
553 // jpegs or pngs to the same file type.
554
Derek Jones37f4b9c2011-07-01 17:56:50 -0500555 $png_mimes = array('image/x-png');
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 $jpeg_mimes = array('image/jpg', 'image/jpe', 'image/jpeg', 'image/pjpeg');
Barry Mienydd671972010-10-04 16:33:58 +0200557
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 if (in_array($this->file_type, $png_mimes))
559 {
560 $this->file_type = 'image/png';
561 }
Barry Mienydd671972010-10-04 16:33:58 +0200562
Derek Allard2067d1a2008-11-13 22:59:24 +0000563 if (in_array($this->file_type, $jpeg_mimes))
564 {
565 $this->file_type = 'image/jpeg';
566 }
567
568 $img_mimes = array(
569 'image/gif',
570 'image/jpeg',
571 'image/png',
Barry Mienydd671972010-10-04 16:33:58 +0200572 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000573
574 return (in_array($this->file_type, $img_mimes, TRUE)) ? TRUE : FALSE;
575 }
Barry Mienydd671972010-10-04 16:33:58 +0200576
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200578
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 /**
580 * Verify that the filetype is allowed
581 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200583 */
Greg Aker58fdee82010-11-10 15:07:09 -0600584 public function is_allowed_filetype($ignore_mime = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 {
Derek Jonese12f64e2010-03-02 22:55:08 -0600586 if ($this->allowed_types == '*')
587 {
588 return TRUE;
589 }
Barry Mienydd671972010-10-04 16:33:58 +0200590
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 if (count($this->allowed_types) == 0 OR ! is_array($this->allowed_types))
592 {
593 $this->set_error('upload_no_file_types');
594 return FALSE;
595 }
Barry Mienydd671972010-10-04 16:33:58 +0200596
Derek Jonese9d723f2010-07-12 10:10:59 -0500597 $ext = strtolower(ltrim($this->file_ext, '.'));
Barry Mienydd671972010-10-04 16:33:58 +0200598
Derek Jonese9d723f2010-07-12 10:10:59 -0500599 if ( ! in_array($ext, $this->allowed_types))
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 {
Derek Jonese9d723f2010-07-12 10:10:59 -0500601 return FALSE;
602 }
Derek Jonesafa282f2009-02-10 17:11:52 +0000603
Barry Mienydd671972010-10-04 16:33:58 +0200604 // Images get some additional checks
Derek Jonese9d723f2010-07-12 10:10:59 -0500605 $image_types = array('gif', 'jpg', 'jpeg', 'png', 'jpe');
Barry Mienydd671972010-10-04 16:33:58 +0200606
Derek Jonese9d723f2010-07-12 10:10:59 -0500607 if (in_array($ext, $image_types))
608 {
609 if (getimagesize($this->file_temp) === FALSE)
Derek Jonesafa282f2009-02-10 17:11:52 +0000610 {
Derek Jonese9d723f2010-07-12 10:10:59 -0500611 return FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200612 }
Derek Jonese9d723f2010-07-12 10:10:59 -0500613 }
Barry Mienydd671972010-10-04 16:33:58 +0200614
Derek Jonese9d723f2010-07-12 10:10:59 -0500615 if ($ignore_mime === TRUE)
616 {
617 return TRUE;
618 }
Barry Mienydd671972010-10-04 16:33:58 +0200619
Derek Jonese9d723f2010-07-12 10:10:59 -0500620 $mime = $this->mimes_types($ext);
Barry Mienydd671972010-10-04 16:33:58 +0200621
Derek Jonese9d723f2010-07-12 10:10:59 -0500622 if (is_array($mime))
623 {
624 if (in_array($this->file_type, $mime, TRUE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 {
Derek Jonese9d723f2010-07-12 10:10:59 -0500626 return TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200627 }
Derek Jonese9d723f2010-07-12 10:10:59 -0500628 }
629 elseif ($mime == $this->file_type)
630 {
631 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 }
Barry Mienydd671972010-10-04 16:33:58 +0200633
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 return FALSE;
635 }
Barry Mienydd671972010-10-04 16:33:58 +0200636
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200638
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 /**
640 * Verify that the file is within the allowed size
641 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200643 */
Greg Aker58fdee82010-11-10 15:07:09 -0600644 public function is_allowed_filesize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000645 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500646 if ($this->max_size != 0 AND $this->file_size > $this->max_size)
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 {
648 return FALSE;
649 }
650 else
651 {
652 return TRUE;
653 }
654 }
Barry Mienydd671972010-10-04 16:33:58 +0200655
Derek Allard2067d1a2008-11-13 22:59:24 +0000656 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200657
Derek Allard2067d1a2008-11-13 22:59:24 +0000658 /**
659 * Verify that the image is within the allowed width/height
660 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200662 */
Greg Aker58fdee82010-11-10 15:07:09 -0600663 public function is_allowed_dimensions()
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 {
665 if ( ! $this->is_image())
666 {
667 return TRUE;
668 }
669
670 if (function_exists('getimagesize'))
671 {
672 $D = @getimagesize($this->file_temp);
673
674 if ($this->max_width > 0 AND $D['0'] > $this->max_width)
675 {
676 return FALSE;
677 }
678
679 if ($this->max_height > 0 AND $D['1'] > $this->max_height)
680 {
681 return FALSE;
682 }
683
684 return TRUE;
685 }
686
687 return TRUE;
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 * Validate Upload Path
694 *
695 * Verifies that it is a valid upload path with proper permissions.
696 *
697 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000698 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200699 */
Greg Aker58fdee82010-11-10 15:07:09 -0600700 public function validate_upload_path()
Derek Allard2067d1a2008-11-13 22:59:24 +0000701 {
702 if ($this->upload_path == '')
703 {
704 $this->set_error('upload_no_filepath');
705 return FALSE;
706 }
Barry Mienydd671972010-10-04 16:33:58 +0200707
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 if (function_exists('realpath') AND @realpath($this->upload_path) !== FALSE)
709 {
710 $this->upload_path = str_replace("\\", "/", realpath($this->upload_path));
711 }
712
713 if ( ! @is_dir($this->upload_path))
714 {
715 $this->set_error('upload_no_filepath');
716 return FALSE;
717 }
718
719 if ( ! is_really_writable($this->upload_path))
720 {
721 $this->set_error('upload_not_writable');
722 return FALSE;
723 }
724
Derek Jones37f4b9c2011-07-01 17:56:50 -0500725 $this->upload_path = preg_replace("/(.+?)\/*$/", "\\1/", $this->upload_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000726 return TRUE;
727 }
Barry Mienydd671972010-10-04 16:33:58 +0200728
Derek Allard2067d1a2008-11-13 22:59:24 +0000729 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200730
Derek Allard2067d1a2008-11-13 22:59:24 +0000731 /**
732 * Extract the file extension
733 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 * @param string
735 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200736 */
Greg Aker58fdee82010-11-10 15:07:09 -0600737 public function get_extension($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000738 {
739 $x = explode('.', $filename);
740 return '.'.end($x);
Barry Mienydd671972010-10-04 16:33:58 +0200741 }
742
Derek Allard2067d1a2008-11-13 22:59:24 +0000743 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200744
Derek Allard2067d1a2008-11-13 22:59:24 +0000745 /**
746 * Clean the file name for security
747 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000748 * @param string
749 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200750 */
Greg Aker58fdee82010-11-10 15:07:09 -0600751 public function clean_file_name($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000752 {
753 $bad = array(
754 "<!--",
755 "-->",
756 "'",
757 "<",
758 ">",
759 '"',
760 '&',
761 '$',
762 '=',
763 ';',
764 '?',
765 '/',
766 "%20",
767 "%22",
768 "%3c", // <
Barry Mienydd671972010-10-04 16:33:58 +0200769 "%253c", // <
770 "%3e", // >
771 "%0e", // >
772 "%28", // (
773 "%29", // )
774 "%2528", // (
775 "%26", // &
776 "%24", // $
777 "%3f", // ?
778 "%3b", // ;
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 "%3d" // =
780 );
Barry Mienydd671972010-10-04 16:33:58 +0200781
Derek Allard2067d1a2008-11-13 22:59:24 +0000782 $filename = str_replace($bad, '', $filename);
783
784 return stripslashes($filename);
785 }
786
787 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200788
Derek Allard2067d1a2008-11-13 22:59:24 +0000789 /**
790 * Limit the File Name Length
791 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 * @param string
793 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200794 */
Greg Aker58fdee82010-11-10 15:07:09 -0600795 public function limit_filename_length($filename, $length)
Derek Allard2067d1a2008-11-13 22:59:24 +0000796 {
797 if (strlen($filename) < $length)
798 {
799 return $filename;
800 }
Barry Mienydd671972010-10-04 16:33:58 +0200801
Derek Allard2067d1a2008-11-13 22:59:24 +0000802 $ext = '';
803 if (strpos($filename, '.') !== FALSE)
804 {
805 $parts = explode('.', $filename);
806 $ext = '.'.array_pop($parts);
807 $filename = implode('.', $parts);
808 }
Barry Mienydd671972010-10-04 16:33:58 +0200809
Derek Allard2067d1a2008-11-13 22:59:24 +0000810 return substr($filename, 0, ($length - strlen($ext))).$ext;
811 }
812
813 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200814
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 /**
816 * Runs the file through the XSS clean function
817 *
818 * This prevents people from embedding malicious code in their files.
819 * I'm not sure that it won't negatively affect certain files in unexpected ways,
820 * but so far I haven't found that it causes trouble.
821 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000822 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200823 */
Greg Aker58fdee82010-11-10 15:07:09 -0600824 public function do_xss_clean()
Barry Mienydd671972010-10-04 16:33:58 +0200825 {
Derek Jonese9d723f2010-07-12 10:10:59 -0500826 $file = $this->file_temp;
Barry Mienydd671972010-10-04 16:33:58 +0200827
Derek Allard2067d1a2008-11-13 22:59:24 +0000828 if (filesize($file) == 0)
829 {
830 return FALSE;
831 }
Barry Mienydd671972010-10-04 16:33:58 +0200832
Greg Akerf82e51c2010-04-14 19:33:50 -0500833 if (function_exists('memory_get_usage') && memory_get_usage() && ini_get('memory_limit') != '')
834 {
835 $current = ini_get('memory_limit') * 1024 * 1024;
Barry Mienydd671972010-10-04 16:33:58 +0200836
Greg Akerc78a2592010-06-09 11:45:32 -0500837 // There was a bug/behavioural change in PHP 5.2, where numbers over one million get output
Derek Jones37f4b9c2011-07-01 17:56:50 -0500838 // into scientific notation. number_format() ensures this number is an integer
Greg Akerc78a2592010-06-09 11:45:32 -0500839 // http://bugs.php.net/bug.php?id=43053
Barry Mienydd671972010-10-04 16:33:58 +0200840
Greg Akerc78a2592010-06-09 11:45:32 -0500841 $new_memory = number_format(ceil(filesize($file) + $current), 0, '.', '');
Barry Mienydd671972010-10-04 16:33:58 +0200842
Greg Akerc78a2592010-06-09 11:45:32 -0500843 ini_set('memory_limit', $new_memory); // When an integer is used, the value is measured in bytes. - PHP.net
Greg Akerf82e51c2010-04-14 19:33:50 -0500844 }
845
846 // If the file being uploaded is an image, then we should have no problem with XSS attacks (in theory), but
847 // IE can be fooled into mime-type detecting a malformed image as an html file, thus executing an XSS attack on anyone
Derek Jones37f4b9c2011-07-01 17:56:50 -0500848 // using IE who looks at the image. It does this by inspecting the first 255 bytes of an image. To get around this
849 // 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 +0200850 // 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 -0500851 // attempted XSS attack.
852
853 if (function_exists('getimagesize') && @getimagesize($file) !== FALSE)
854 {
Barry Mienydd671972010-10-04 16:33:58 +0200855 if (($file = @fopen($file, 'rb')) === FALSE) // "b" to force binary
856 {
Greg Akerf82e51c2010-04-14 19:33:50 -0500857 return FALSE; // Couldn't open the file, return FALSE
Barry Mienydd671972010-10-04 16:33:58 +0200858 }
Greg Akerf82e51c2010-04-14 19:33:50 -0500859
Barry Mienydd671972010-10-04 16:33:58 +0200860 $opening_bytes = fread($file, 256);
861 fclose($file);
Greg Akerf82e51c2010-04-14 19:33:50 -0500862
863 // These are known to throw IE into mime-type detection chaos
864 // <a, <body, <head, <html, <img, <plaintext, <pre, <script, <table, <title
865 // title is basically just in SVG, but we filter it anyhow
866
867 if ( ! preg_match('/<(a|body|head|html|img|plaintext|pre|script|table|title)[\s>]/i', $opening_bytes))
868 {
869 return TRUE; // its an image, no "triggers" detected in the first 256 bytes, we're good
870 }
871 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000872
873 if (($data = @file_get_contents($file)) === FALSE)
874 {
875 return FALSE;
876 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000877
Greg Akerf82e51c2010-04-14 19:33:50 -0500878 $CI =& get_instance();
Greg Akerf82e51c2010-04-14 19:33:50 -0500879 return $CI->security->xss_clean($data, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 }
Barry Mienydd671972010-10-04 16:33:58 +0200881
Derek Allard2067d1a2008-11-13 22:59:24 +0000882 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200883
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 /**
885 * Set an error message
886 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 * @param string
888 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200889 */
Greg Aker58fdee82010-11-10 15:07:09 -0600890 public function set_error($msg)
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 {
Barry Mienydd671972010-10-04 16:33:58 +0200892 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 $CI->lang->load('upload');
Barry Mienydd671972010-10-04 16:33:58 +0200894
Derek Allard2067d1a2008-11-13 22:59:24 +0000895 if (is_array($msg))
896 {
897 foreach ($msg as $val)
898 {
Barry Mienydd671972010-10-04 16:33:58 +0200899 $msg = ($CI->lang->line($val) == FALSE) ? $val : $CI->lang->line($val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000900 $this->error_msg[] = $msg;
901 log_message('error', $msg);
Barry Mienydd671972010-10-04 16:33:58 +0200902 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000903 }
904 else
905 {
906 $msg = ($CI->lang->line($msg) == FALSE) ? $msg : $CI->lang->line($msg);
907 $this->error_msg[] = $msg;
908 log_message('error', $msg);
909 }
910 }
Barry Mienydd671972010-10-04 16:33:58 +0200911
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200913
Derek Allard2067d1a2008-11-13 22:59:24 +0000914 /**
915 * Display the error message
916 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000917 * @param string
918 * @param string
919 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200920 */
Greg Aker58fdee82010-11-10 15:07:09 -0600921 public function display_errors($open = '<p>', $close = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 {
923 $str = '';
924 foreach ($this->error_msg as $val)
925 {
926 $str .= $open.$val.$close;
927 }
Barry Mienydd671972010-10-04 16:33:58 +0200928
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 return $str;
930 }
Barry Mienydd671972010-10-04 16:33:58 +0200931
Derek Allard2067d1a2008-11-13 22:59:24 +0000932 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200933
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 /**
935 * List of Mime Types
936 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500937 * This is a list of mime types. We use it to validate
Derek Allard2067d1a2008-11-13 22:59:24 +0000938 * the "allowed types" set by the developer
939 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000940 * @param string
941 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200942 */
Greg Aker58fdee82010-11-10 15:07:09 -0600943 public function mimes_types($mime)
Derek Allard2067d1a2008-11-13 22:59:24 +0000944 {
945 global $mimes;
Barry Mienydd671972010-10-04 16:33:58 +0200946
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 if (count($this->mimes) == 0)
948 {
Greg Aker3a746652011-04-19 10:59:47 -0500949 if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'))
bubbafoley0ea04142011-03-17 14:55:41 -0500950 {
Greg Aker3a746652011-04-19 10:59:47 -0500951 include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php');
Eric Barnesfdd5b112011-03-21 21:28:58 -0400952 }
Greg Aker3a746652011-04-19 10:59:47 -0500953 elseif (is_file(APPPATH.'config/mimes.php'))
Eric Barnesfdd5b112011-03-21 21:28:58 -0400954 {
Greg Aker3a746652011-04-19 10:59:47 -0500955 include(APPPATH.'config//mimes.php');
bubbafoley0ea04142011-03-17 14:55:41 -0500956 }
957 else
958 {
Eric Barnesfdd5b112011-03-21 21:28:58 -0400959 return FALSE;
bubbafoley0ea04142011-03-17 14:55:41 -0500960 }
Eric Barnes92808342011-03-18 09:02:37 -0400961
Eric Barnesfdd5b112011-03-21 21:28:58 -0400962 $this->mimes = $mimes;
963 unset($mimes);
Derek Allard2067d1a2008-11-13 22:59:24 +0000964 }
Barry Mienydd671972010-10-04 16:33:58 +0200965
Derek Allard2067d1a2008-11-13 22:59:24 +0000966 return ( ! isset($this->mimes[$mime])) ? FALSE : $this->mimes[$mime];
967 }
968
969 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200970
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 /**
972 * Prep Filename
973 *
974 * Prevents possible script execution from Apache's handling of files multiple extensions
975 * http://httpd.apache.org/docs/1.3/mod/mod_mime.html#multipleext
976 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000977 * @param string
978 * @return string
979 */
Greg Aker58fdee82010-11-10 15:07:09 -0600980 protected function _prep_filename($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000981 {
Greg Aker924000e2010-07-22 11:04:58 -0500982 if (strpos($filename, '.') === FALSE OR $this->allowed_types == '*')
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 {
984 return $filename;
985 }
Derek Allard616dab82009-02-16 15:44:32 +0000986
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 $parts = explode('.', $filename);
988 $ext = array_pop($parts);
989 $filename = array_shift($parts);
Derek Allard616dab82009-02-16 15:44:32 +0000990
Derek Allard2067d1a2008-11-13 22:59:24 +0000991 foreach ($parts as $part)
992 {
Derek Jonese9d723f2010-07-12 10:10:59 -0500993 if ( ! in_array(strtolower($part), $this->allowed_types) OR $this->mimes_types(strtolower($part)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000994 {
995 $filename .= '.'.$part.'_';
996 }
997 else
998 {
999 $filename .= '.'.$part;
1000 }
1001 }
Derek Allardd70b0642009-02-16 13:51:42 +00001002
Derek Allard2067d1a2008-11-13 22:59:24 +00001003 $filename .= '.'.$ext;
Barry Mienydd671972010-10-04 16:33:58 +02001004
Derek Allard2067d1a2008-11-13 22:59:24 +00001005 return $filename;
1006 }
1007
1008 // --------------------------------------------------------------------
1009
Andrey Andreev6700b932011-09-24 14:25:33 +03001010 /**
1011 * File MIME type
1012 *
1013 * Detects the (actual) MIME type of the uploaded file, if possible.
1014 * The input array is expected to be $_FILES[$field]
1015 *
1016 * @param array
1017 * @return void
1018 */
1019 protected function _file_mime_type($file)
1020 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001021 // We'll need this to validate the MIME info string (e.g. text/plain; charset=us-ascii)
Andrey Andreevf7aed122011-12-13 11:01:06 +02001022 $regexp = '/^([a-z\-]+\/[a-z0-9\-\.\+]+)(;\s.+)?$/';
Andrey Andreeva49e3812011-12-09 13:05:22 +02001023
1024 /* Fileinfo extension - most reliable method
1025 *
1026 * Unfortunately, prior to PHP 5.3 - it's only available as a PECL extension and the
1027 * more convenient FILEINFO_MIME_TYPE flag doesn't exist.
1028 */
1029 if (function_exists('finfo_file'))
Andrey Andreev6700b932011-09-24 14:25:33 +03001030 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001031 $finfo = finfo_open(FILEINFO_MIME);
1032 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 Andreev6700b932011-09-24 14:25:33 +03001033 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001034 $mime = @finfo_file($finfo, $file['tmp_name']);
1035 finfo_close($finfo);
Andrey Andreev6700b932011-09-24 14:25:33 +03001036
1037 /* According to the comments section of the PHP manual page,
1038 * it is possible that this function returns an empty string
Andrey Andreevf1649bf2011-09-24 22:59:37 +03001039 * for some files (e.g. if they don't exist in the magic MIME database)
Andrey Andreev6700b932011-09-24 14:25:33 +03001040 */
Andrey Andreeva49e3812011-12-09 13:05:22 +02001041 if (is_string($mime) && preg_match($regexp, $mime, $matches))
Andrey Andreev6700b932011-09-24 14:25:33 +03001042 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001043 $this->file_type = $matches[1];
Andrey Andreev6700b932011-09-24 14:25:33 +03001044 return;
1045 }
1046 }
1047 }
1048
Andrey Andreeva49e3812011-12-09 13:05:22 +02001049 /* This is an ugly hack, but UNIX-type systems provide a "native" way to detect the file type,
1050 * which is still more secure than depending on the value of $_FILES[$field]['type'], and as it
1051 * was reported in issue #750 (https://github.com/EllisLab/CodeIgniter/issues/750) - it's better
1052 * than mime_content_type() as well, hence the attempts to try calling the command line with
1053 * three different functions.
Andrey Andreev6700b932011-09-24 14:25:33 +03001054 *
1055 * Notes:
Andrey Andreev59654312011-12-02 14:28:54 +02001056 * - the DIRECTORY_SEPARATOR comparison ensures that we're not on a Windows system
Andrey Andreeva49e3812011-12-09 13:05:22 +02001057 * - many system admins would disable the exec(), shell_exec(), popen() and similar functions
1058 * due to security concerns, hence the function_exists() checks
Andrey Andreev6700b932011-09-24 14:25:33 +03001059 */
Andrey Andreeva49e3812011-12-09 13:05:22 +02001060 if (DIRECTORY_SEPARATOR !== '\\')
Andrey Andreev6700b932011-09-24 14:25:33 +03001061 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001062 $cmd = 'file --brief --mime ' . escapeshellarg($file['tmp_name']) . ' 2>&1';
1063
1064 if (function_exists('exec'))
Andrey Andreev6700b932011-09-24 14:25:33 +03001065 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001066 /* This might look confusing, as $mime is being populated with all of the output when set in the second parameter.
1067 * However, we only neeed the last line, which is the actual return value of exec(), and as such - it overwrites
1068 * anything that could already be set for $mime previously. This effectively makes the second parameter a dummy
1069 * value, which is only put to allow us to get the return status code.
1070 */
1071 $mime = @exec($cmd, $mime, $return_status);
1072 if ($return_status === 0 && is_string($mime) && preg_match($regexp, $mime, $matches))
1073 {
1074 $this->file_type = $matches[1];
1075 return;
1076 }
1077 }
1078
1079 if ( (bool) @ini_get('safe_mode') === FALSE && function_exists('shell_exec'))
1080 {
1081 $mime = @shell_exec($cmd);
1082 if (strlen($mime) > 0)
1083 {
1084 $mime = explode("\n", trim($mime));
1085 if (preg_match($regexp, $mime[(count($mime) - 1)], $matches))
1086 {
1087 $this->file_type = $matches[1];
1088 return;
1089 }
1090 }
1091 }
1092
1093 if (function_exists('popen'))
1094 {
1095 $proc = @popen($cmd, 'r');
1096 if (is_resource($proc))
1097 {
tubalmartin010f1f42012-03-03 22:24:31 +01001098 $mime = @fread($proc, 512);
Andrey Andreeva49e3812011-12-09 13:05:22 +02001099 @pclose($proc);
1100 if ($mime !== FALSE)
1101 {
1102 $mime = explode("\n", trim($mime));
1103 if (preg_match($regexp, $mime[(count($mime) - 1)], $matches))
1104 {
1105 $this->file_type = $matches[1];
1106 return;
1107 }
1108 }
1109 }
1110 }
1111 }
1112
1113 // Fall back to the deprecated mime_content_type(), if available (still better than $_FILES[$field]['type'])
1114 if (function_exists('mime_content_type'))
1115 {
1116 $this->file_type = @mime_content_type($file['tmp_name']);
1117 if (strlen($this->file_type) > 0) // It's possible that mime_content_type() returns FALSE or an empty string
1118 {
Andrey Andreev6700b932011-09-24 14:25:33 +03001119 return;
1120 }
1121 }
1122
1123 $this->file_type = $file['type'];
1124 }
1125
1126 // --------------------------------------------------------------------
1127
Derek Allard2067d1a2008-11-13 22:59:24 +00001128}
1129// END Upload Class
1130
1131/* End of file Upload.php */
Andrey Andreev6700b932011-09-24 14:25:33 +03001132/* Location: ./system/libraries/Upload.php */