blob: c8c42d88598baa3af8ca358a69704b1d400681bc [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
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 {
145 // Is $_FILES[$field] set? If not, no reason to continue.
146 if ( ! isset($_FILES[$field]))
147 {
148 $this->set_error('upload_no_file_selected');
149 return FALSE;
150 }
Barry Mienydd671972010-10-04 16:33:58 +0200151
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 // Is the upload path valid?
153 if ( ! $this->validate_upload_path())
154 {
155 // errors will already be set by validate_upload_path() so just return FALSE
156 return FALSE;
157 }
158
159 // Was the file able to be uploaded? If not, determine the reason why.
160 if ( ! is_uploaded_file($_FILES[$field]['tmp_name']))
161 {
162 $error = ( ! isset($_FILES[$field]['error'])) ? 4 : $_FILES[$field]['error'];
163
164 switch($error)
165 {
166 case 1: // UPLOAD_ERR_INI_SIZE
167 $this->set_error('upload_file_exceeds_limit');
168 break;
169 case 2: // UPLOAD_ERR_FORM_SIZE
170 $this->set_error('upload_file_exceeds_form_limit');
171 break;
172 case 3: // UPLOAD_ERR_PARTIAL
Barry Mienydd671972010-10-04 16:33:58 +0200173 $this->set_error('upload_file_partial');
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 break;
175 case 4: // UPLOAD_ERR_NO_FILE
Barry Mienydd671972010-10-04 16:33:58 +0200176 $this->set_error('upload_no_file_selected');
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 break;
178 case 6: // UPLOAD_ERR_NO_TMP_DIR
179 $this->set_error('upload_no_temp_directory');
180 break;
181 case 7: // UPLOAD_ERR_CANT_WRITE
182 $this->set_error('upload_unable_to_write_file');
183 break;
184 case 8: // UPLOAD_ERR_EXTENSION
185 $this->set_error('upload_stopped_by_extension');
186 break;
187 default : $this->set_error('upload_no_file_selected');
188 break;
189 }
190
191 return FALSE;
192 }
193
Derek Jonese9d723f2010-07-12 10:10:59 -0500194
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 // Set the uploaded data as class variables
Barry Mienydd671972010-10-04 16:33:58 +0200196 $this->file_temp = $_FILES[$field]['tmp_name'];
197 $this->file_size = $_FILES[$field]['size'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 $this->file_type = preg_replace("/^(.+?);.*$/", "\\1", $_FILES[$field]['type']);
Derek Jones616fb022010-04-22 16:52:18 -0500199 $this->file_type = strtolower(trim(stripslashes($this->file_type), '"'));
Derek Jonese9d723f2010-07-12 10:10:59 -0500200 $this->file_name = $this->_prep_filename($_FILES[$field]['name']);
201 $this->file_ext = $this->get_extension($this->file_name);
202 $this->client_name = $this->file_name;
Barry Mienydd671972010-10-04 16:33:58 +0200203
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 // Is the file type allowed to be uploaded?
205 if ( ! $this->is_allowed_filetype())
206 {
207 $this->set_error('upload_invalid_filetype');
208 return FALSE;
209 }
210
Derek Jonese9d723f2010-07-12 10:10:59 -0500211 // if we're overriding, let's now make sure the new name and type is allowed
212 if ($this->_file_name_override != '')
213 {
214 $this->file_name = $this->_prep_filename($this->_file_name_override);
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000215
216 // If no extension was provided in the file_name config item, use the uploaded one
Pascal Kriete14287f32011-02-14 13:39:34 -0500217 if (strpos($this->_file_name_override, '.') === FALSE)
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000218 {
219 $this->file_name .= $this->file_ext;
220 }
221
222 // An extension was provided, lets have it!
223 else
224 {
225 $this->file_ext = $this->get_extension($this->_file_name_override);
226 }
Derek Jonese9d723f2010-07-12 10:10:59 -0500227
228 if ( ! $this->is_allowed_filetype(TRUE))
229 {
230 $this->set_error('upload_invalid_filetype');
Barry Mienydd671972010-10-04 16:33:58 +0200231 return FALSE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500232 }
233 }
Barry Mienydd671972010-10-04 16:33:58 +0200234
Derek Jonese9d723f2010-07-12 10:10:59 -0500235 // Convert the file size to kilobytes
236 if ($this->file_size > 0)
237 {
238 $this->file_size = round($this->file_size/1024, 2);
239 }
240
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 // Is the file size within the allowed maximum?
242 if ( ! $this->is_allowed_filesize())
243 {
244 $this->set_error('upload_invalid_filesize');
245 return FALSE;
246 }
247
248 // Are the image dimensions within the allowed size?
249 // Note: This can fail if the server has an open_basdir restriction.
250 if ( ! $this->is_allowed_dimensions())
251 {
252 $this->set_error('upload_invalid_dimensions');
253 return FALSE;
254 }
255
256 // Sanitize the file name for security
257 $this->file_name = $this->clean_file_name($this->file_name);
Barry Mienydd671972010-10-04 16:33:58 +0200258
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 // Truncate the file name if it's too long
260 if ($this->max_filename > 0)
261 {
262 $this->file_name = $this->limit_filename_length($this->file_name, $this->max_filename);
263 }
264
265 // Remove white spaces in the name
266 if ($this->remove_spaces == TRUE)
267 {
268 $this->file_name = preg_replace("/\s+/", "_", $this->file_name);
269 }
270
271 /*
272 * Validate the file name
273 * This function appends an number onto the end of
274 * the file if one with the same name already exists.
275 * If it returns false there was a problem.
276 */
277 $this->orig_name = $this->file_name;
278
279 if ($this->overwrite == FALSE)
280 {
281 $this->file_name = $this->set_filename($this->upload_path, $this->file_name);
Barry Mienydd671972010-10-04 16:33:58 +0200282
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 if ($this->file_name === FALSE)
284 {
285 return FALSE;
286 }
287 }
288
289 /*
Derek Jonese9d723f2010-07-12 10:10:59 -0500290 * Run the file through the XSS hacking filter
291 * This helps prevent malicious code from being
292 * embedded within a file. Scripts can easily
293 * be disguised as images or other file types.
294 */
295 if ($this->xss_clean)
296 {
297 if ($this->do_xss_clean() === FALSE)
298 {
299 $this->set_error('upload_unable_to_write_file');
300 return FALSE;
301 }
302 }
303
304 /*
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 * Move the file to the final destination
306 * To deal with different server configurations
307 * we'll attempt to use copy() first. If that fails
308 * we'll use move_uploaded_file(). One of the two should
309 * reliably work in most environments
310 */
311 if ( ! @copy($this->file_temp, $this->upload_path.$this->file_name))
312 {
313 if ( ! @move_uploaded_file($this->file_temp, $this->upload_path.$this->file_name))
314 {
Barry Mienydd671972010-10-04 16:33:58 +0200315 $this->set_error('upload_destination_error');
316 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 }
318 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000319
320 /*
321 * Set the finalized image dimensions
322 * This sets the image width/height (assuming the
323 * file was an image). We use this information
324 * in the "data" function.
325 */
326 $this->set_image_properties($this->upload_path.$this->file_name);
327
328 return TRUE;
329 }
Barry Mienydd671972010-10-04 16:33:58 +0200330
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200332
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 /**
334 * Finalized Data Array
Barry Mienydd671972010-10-04 16:33:58 +0200335 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 * Returns an associative array containing all of the information
337 * related to the upload, allowing the developer easy access in one array.
338 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200340 */
Greg Aker58fdee82010-11-10 15:07:09 -0600341 public function data()
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 {
343 return array (
344 'file_name' => $this->file_name,
345 'file_type' => $this->file_type,
346 'file_path' => $this->upload_path,
347 'full_path' => $this->upload_path.$this->file_name,
348 'raw_name' => str_replace($this->file_ext, '', $this->file_name),
349 'orig_name' => $this->orig_name,
Derek Jonese9d723f2010-07-12 10:10:59 -0500350 'client_name' => $this->client_name,
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 'file_ext' => $this->file_ext,
352 'file_size' => $this->file_size,
353 'is_image' => $this->is_image(),
354 'image_width' => $this->image_width,
355 'image_height' => $this->image_height,
356 'image_type' => $this->image_type,
357 'image_size_str' => $this->image_size_str,
358 );
359 }
Barry Mienydd671972010-10-04 16:33:58 +0200360
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200362
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 /**
364 * Set Upload Path
365 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 * @param string
367 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200368 */
Greg Aker58fdee82010-11-10 15:07:09 -0600369 public function set_upload_path($path)
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 {
371 // Make sure it has a trailing slash
372 $this->upload_path = rtrim($path, '/').'/';
373 }
Barry Mienydd671972010-10-04 16:33:58 +0200374
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200376
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 /**
378 * Set the file name
379 *
380 * This function takes a filename/path as input and looks for the
381 * existence of a file with the same name. If found, it will append a
382 * number to the end of the filename to avoid overwriting a pre-existing file.
383 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 * @param string
385 * @param string
386 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200387 */
Greg Aker58fdee82010-11-10 15:07:09 -0600388 public function set_filename($path, $filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 {
390 if ($this->encrypt_name == TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200391 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 mt_srand();
Barry Mienydd671972010-10-04 16:33:58 +0200393 $filename = md5(uniqid(mt_rand())).$this->file_ext;
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 }
Barry Mienydd671972010-10-04 16:33:58 +0200395
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 if ( ! file_exists($path.$filename))
397 {
398 return $filename;
399 }
Barry Mienydd671972010-10-04 16:33:58 +0200400
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 $filename = str_replace($this->file_ext, '', $filename);
Barry Mienydd671972010-10-04 16:33:58 +0200402
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 $new_filename = '';
404 for ($i = 1; $i < 100; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200405 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 if ( ! file_exists($path.$filename.$i.$this->file_ext))
407 {
408 $new_filename = $filename.$i.$this->file_ext;
409 break;
410 }
411 }
412
413 if ($new_filename == '')
414 {
415 $this->set_error('upload_bad_filename');
416 return FALSE;
417 }
418 else
419 {
420 return $new_filename;
421 }
422 }
Barry Mienydd671972010-10-04 16:33:58 +0200423
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200425
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 /**
427 * Set Maximum File Size
428 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 * @param integer
430 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200431 */
Greg Aker58fdee82010-11-10 15:07:09 -0600432 public function set_max_filesize($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 {
434 $this->max_size = ((int) $n < 0) ? 0: (int) $n;
435 }
Barry Mienydd671972010-10-04 16:33:58 +0200436
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200438
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 /**
440 * Set Maximum File Name Length
441 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 * @param integer
443 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200444 */
Greg Aker58fdee82010-11-10 15:07:09 -0600445 public function set_max_filename($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 {
447 $this->max_filename = ((int) $n < 0) ? 0: (int) $n;
448 }
449
450 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200451
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 /**
453 * Set Maximum Image Width
454 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 * @param integer
456 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200457 */
Greg Aker58fdee82010-11-10 15:07:09 -0600458 public function set_max_width($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 {
460 $this->max_width = ((int) $n < 0) ? 0: (int) $n;
461 }
Barry Mienydd671972010-10-04 16:33:58 +0200462
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200464
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 /**
466 * Set Maximum Image Height
467 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 * @param integer
469 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200470 */
Greg Aker58fdee82010-11-10 15:07:09 -0600471 public function set_max_height($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 {
473 $this->max_height = ((int) $n < 0) ? 0: (int) $n;
474 }
Barry Mienydd671972010-10-04 16:33:58 +0200475
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200477
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 /**
479 * Set Allowed File Types
480 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 * @param string
482 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200483 */
Greg Aker58fdee82010-11-10 15:07:09 -0600484 public function set_allowed_types($types)
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 {
Derek Jonese12f64e2010-03-02 22:55:08 -0600486 if ( ! is_array($types) && $types == '*')
487 {
488 $this->allowed_types = '*';
489 return;
490 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 $this->allowed_types = explode('|', $types);
492 }
Barry Mienydd671972010-10-04 16:33:58 +0200493
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200495
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 /**
497 * Set Image Properties
498 *
499 * Uses GD to determine the width/height/type of image
500 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 * @param string
502 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200503 */
Greg Aker58fdee82010-11-10 15:07:09 -0600504 public function set_image_properties($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 {
506 if ( ! $this->is_image())
507 {
508 return;
509 }
510
511 if (function_exists('getimagesize'))
512 {
513 if (FALSE !== ($D = @getimagesize($path)))
Barry Mienydd671972010-10-04 16:33:58 +0200514 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
516
517 $this->image_width = $D['0'];
518 $this->image_height = $D['1'];
519 $this->image_type = ( ! isset($types[$D['2']])) ? 'unknown' : $types[$D['2']];
520 $this->image_size_str = $D['3']; // string containing height and width
521 }
522 }
523 }
Barry Mienydd671972010-10-04 16:33:58 +0200524
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200526
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 /**
528 * Set XSS Clean
529 *
530 * Enables the XSS flag so that the file that was uploaded
531 * will be run through the XSS filter.
532 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 * @param bool
534 * @return void
535 */
Greg Aker58fdee82010-11-10 15:07:09 -0600536 public function set_xss_clean($flag = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 {
538 $this->xss_clean = ($flag == TRUE) ? TRUE : FALSE;
539 }
Barry Mienydd671972010-10-04 16:33:58 +0200540
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200542
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 /**
544 * Validate the image
545 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200547 */
Greg Aker58fdee82010-11-10 15:07:09 -0600548 public function is_image()
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 {
550 // IE will sometimes return odd mime-types during upload, so here we just standardize all
551 // jpegs or pngs to the same file type.
552
553 $png_mimes = array('image/x-png');
554 $jpeg_mimes = array('image/jpg', 'image/jpe', 'image/jpeg', 'image/pjpeg');
Barry Mienydd671972010-10-04 16:33:58 +0200555
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 if (in_array($this->file_type, $png_mimes))
557 {
558 $this->file_type = 'image/png';
559 }
Barry Mienydd671972010-10-04 16:33:58 +0200560
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 if (in_array($this->file_type, $jpeg_mimes))
562 {
563 $this->file_type = 'image/jpeg';
564 }
565
566 $img_mimes = array(
567 'image/gif',
568 'image/jpeg',
569 'image/png',
Barry Mienydd671972010-10-04 16:33:58 +0200570 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000571
572 return (in_array($this->file_type, $img_mimes, TRUE)) ? TRUE : FALSE;
573 }
Barry Mienydd671972010-10-04 16:33:58 +0200574
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200576
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 /**
578 * Verify that the filetype is allowed
579 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200581 */
Greg Aker58fdee82010-11-10 15:07:09 -0600582 public function is_allowed_filetype($ignore_mime = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 {
Derek Jonese12f64e2010-03-02 22:55:08 -0600584 if ($this->allowed_types == '*')
585 {
586 return TRUE;
587 }
Barry Mienydd671972010-10-04 16:33:58 +0200588
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 if (count($this->allowed_types) == 0 OR ! is_array($this->allowed_types))
590 {
591 $this->set_error('upload_no_file_types');
592 return FALSE;
593 }
Barry Mienydd671972010-10-04 16:33:58 +0200594
Derek Jonese9d723f2010-07-12 10:10:59 -0500595 $ext = strtolower(ltrim($this->file_ext, '.'));
Barry Mienydd671972010-10-04 16:33:58 +0200596
Derek Jonese9d723f2010-07-12 10:10:59 -0500597 if ( ! in_array($ext, $this->allowed_types))
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 {
Derek Jonese9d723f2010-07-12 10:10:59 -0500599 return FALSE;
600 }
Derek Jonesafa282f2009-02-10 17:11:52 +0000601
Barry Mienydd671972010-10-04 16:33:58 +0200602 // Images get some additional checks
Derek Jonese9d723f2010-07-12 10:10:59 -0500603 $image_types = array('gif', 'jpg', 'jpeg', 'png', 'jpe');
Barry Mienydd671972010-10-04 16:33:58 +0200604
Derek Jonese9d723f2010-07-12 10:10:59 -0500605 if (in_array($ext, $image_types))
606 {
607 if (getimagesize($this->file_temp) === FALSE)
Derek Jonesafa282f2009-02-10 17:11:52 +0000608 {
Derek Jonese9d723f2010-07-12 10:10:59 -0500609 return FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200610 }
Derek Jonese9d723f2010-07-12 10:10:59 -0500611 }
Barry Mienydd671972010-10-04 16:33:58 +0200612
Derek Jonese9d723f2010-07-12 10:10:59 -0500613 if ($ignore_mime === TRUE)
614 {
615 return TRUE;
616 }
Barry Mienydd671972010-10-04 16:33:58 +0200617
Derek Jonese9d723f2010-07-12 10:10:59 -0500618 $mime = $this->mimes_types($ext);
Barry Mienydd671972010-10-04 16:33:58 +0200619
Derek Jonese9d723f2010-07-12 10:10:59 -0500620 if (is_array($mime))
621 {
622 if (in_array($this->file_type, $mime, TRUE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 {
Derek Jonese9d723f2010-07-12 10:10:59 -0500624 return TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200625 }
Derek Jonese9d723f2010-07-12 10:10:59 -0500626 }
627 elseif ($mime == $this->file_type)
628 {
629 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 }
Barry Mienydd671972010-10-04 16:33:58 +0200631
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 return FALSE;
633 }
Barry Mienydd671972010-10-04 16:33:58 +0200634
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200636
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 /**
638 * Verify that the file is within the allowed size
639 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200641 */
Greg Aker58fdee82010-11-10 15:07:09 -0600642 public function is_allowed_filesize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000643 {
644 if ($this->max_size != 0 AND $this->file_size > $this->max_size)
645 {
646 return FALSE;
647 }
648 else
649 {
650 return TRUE;
651 }
652 }
Barry Mienydd671972010-10-04 16:33:58 +0200653
Derek Allard2067d1a2008-11-13 22:59:24 +0000654 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200655
Derek Allard2067d1a2008-11-13 22:59:24 +0000656 /**
657 * Verify that the image is within the allowed width/height
658 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200660 */
Greg Aker58fdee82010-11-10 15:07:09 -0600661 public function is_allowed_dimensions()
Derek Allard2067d1a2008-11-13 22:59:24 +0000662 {
663 if ( ! $this->is_image())
664 {
665 return TRUE;
666 }
667
668 if (function_exists('getimagesize'))
669 {
670 $D = @getimagesize($this->file_temp);
671
672 if ($this->max_width > 0 AND $D['0'] > $this->max_width)
673 {
674 return FALSE;
675 }
676
677 if ($this->max_height > 0 AND $D['1'] > $this->max_height)
678 {
679 return FALSE;
680 }
681
682 return TRUE;
683 }
684
685 return TRUE;
686 }
Barry Mienydd671972010-10-04 16:33:58 +0200687
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200689
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 /**
691 * Validate Upload Path
692 *
693 * Verifies that it is a valid upload path with proper permissions.
694 *
695 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000696 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200697 */
Greg Aker58fdee82010-11-10 15:07:09 -0600698 public function validate_upload_path()
Derek Allard2067d1a2008-11-13 22:59:24 +0000699 {
700 if ($this->upload_path == '')
701 {
702 $this->set_error('upload_no_filepath');
703 return FALSE;
704 }
Barry Mienydd671972010-10-04 16:33:58 +0200705
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 if (function_exists('realpath') AND @realpath($this->upload_path) !== FALSE)
707 {
708 $this->upload_path = str_replace("\\", "/", realpath($this->upload_path));
709 }
710
711 if ( ! @is_dir($this->upload_path))
712 {
713 $this->set_error('upload_no_filepath');
714 return FALSE;
715 }
716
717 if ( ! is_really_writable($this->upload_path))
718 {
719 $this->set_error('upload_not_writable');
720 return FALSE;
721 }
722
723 $this->upload_path = preg_replace("/(.+?)\/*$/", "\\1/", $this->upload_path);
724 return TRUE;
725 }
Barry Mienydd671972010-10-04 16:33:58 +0200726
Derek Allard2067d1a2008-11-13 22:59:24 +0000727 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200728
Derek Allard2067d1a2008-11-13 22:59:24 +0000729 /**
730 * Extract the file extension
731 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000732 * @param string
733 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200734 */
Greg Aker58fdee82010-11-10 15:07:09 -0600735 public function get_extension($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 {
737 $x = explode('.', $filename);
738 return '.'.end($x);
Barry Mienydd671972010-10-04 16:33:58 +0200739 }
740
Derek Allard2067d1a2008-11-13 22:59:24 +0000741 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200742
Derek Allard2067d1a2008-11-13 22:59:24 +0000743 /**
744 * Clean the file name for security
745 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 * @param string
747 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200748 */
Greg Aker58fdee82010-11-10 15:07:09 -0600749 public function clean_file_name($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000750 {
751 $bad = array(
752 "<!--",
753 "-->",
754 "'",
755 "<",
756 ">",
757 '"',
758 '&',
759 '$',
760 '=',
761 ';',
762 '?',
763 '/',
764 "%20",
765 "%22",
766 "%3c", // <
Barry Mienydd671972010-10-04 16:33:58 +0200767 "%253c", // <
768 "%3e", // >
769 "%0e", // >
770 "%28", // (
771 "%29", // )
772 "%2528", // (
773 "%26", // &
774 "%24", // $
775 "%3f", // ?
776 "%3b", // ;
Derek Allard2067d1a2008-11-13 22:59:24 +0000777 "%3d" // =
778 );
Barry Mienydd671972010-10-04 16:33:58 +0200779
Derek Allard2067d1a2008-11-13 22:59:24 +0000780 $filename = str_replace($bad, '', $filename);
781
782 return stripslashes($filename);
783 }
784
785 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200786
Derek Allard2067d1a2008-11-13 22:59:24 +0000787 /**
788 * Limit the File Name Length
789 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000790 * @param string
791 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200792 */
Greg Aker58fdee82010-11-10 15:07:09 -0600793 public function limit_filename_length($filename, $length)
Derek Allard2067d1a2008-11-13 22:59:24 +0000794 {
795 if (strlen($filename) < $length)
796 {
797 return $filename;
798 }
Barry Mienydd671972010-10-04 16:33:58 +0200799
Derek Allard2067d1a2008-11-13 22:59:24 +0000800 $ext = '';
801 if (strpos($filename, '.') !== FALSE)
802 {
803 $parts = explode('.', $filename);
804 $ext = '.'.array_pop($parts);
805 $filename = implode('.', $parts);
806 }
Barry Mienydd671972010-10-04 16:33:58 +0200807
Derek Allard2067d1a2008-11-13 22:59:24 +0000808 return substr($filename, 0, ($length - strlen($ext))).$ext;
809 }
810
811 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200812
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 /**
814 * Runs the file through the XSS clean function
815 *
816 * This prevents people from embedding malicious code in their files.
817 * I'm not sure that it won't negatively affect certain files in unexpected ways,
818 * but so far I haven't found that it causes trouble.
819 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200821 */
Greg Aker58fdee82010-11-10 15:07:09 -0600822 public function do_xss_clean()
Barry Mienydd671972010-10-04 16:33:58 +0200823 {
Derek Jonese9d723f2010-07-12 10:10:59 -0500824 $file = $this->file_temp;
Barry Mienydd671972010-10-04 16:33:58 +0200825
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 if (filesize($file) == 0)
827 {
828 return FALSE;
829 }
Barry Mienydd671972010-10-04 16:33:58 +0200830
Greg Akerf82e51c2010-04-14 19:33:50 -0500831 if (function_exists('memory_get_usage') && memory_get_usage() && ini_get('memory_limit') != '')
832 {
833 $current = ini_get('memory_limit') * 1024 * 1024;
Barry Mienydd671972010-10-04 16:33:58 +0200834
Greg Akerc78a2592010-06-09 11:45:32 -0500835 // There was a bug/behavioural change in PHP 5.2, where numbers over one million get output
836 // into scientific notation. number_format() ensures this number is an integer
837 // http://bugs.php.net/bug.php?id=43053
Barry Mienydd671972010-10-04 16:33:58 +0200838
Greg Akerc78a2592010-06-09 11:45:32 -0500839 $new_memory = number_format(ceil(filesize($file) + $current), 0, '.', '');
Barry Mienydd671972010-10-04 16:33:58 +0200840
Greg Akerc78a2592010-06-09 11:45:32 -0500841 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 -0500842 }
843
844 // If the file being uploaded is an image, then we should have no problem with XSS attacks (in theory), but
845 // IE can be fooled into mime-type detecting a malformed image as an html file, thus executing an XSS attack on anyone
846 // using IE who looks at the image. It does this by inspecting the first 255 bytes of an image. To get around this
847 // 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 +0200848 // 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 -0500849 // attempted XSS attack.
850
851 if (function_exists('getimagesize') && @getimagesize($file) !== FALSE)
852 {
Barry Mienydd671972010-10-04 16:33:58 +0200853 if (($file = @fopen($file, 'rb')) === FALSE) // "b" to force binary
854 {
Greg Akerf82e51c2010-04-14 19:33:50 -0500855 return FALSE; // Couldn't open the file, return FALSE
Barry Mienydd671972010-10-04 16:33:58 +0200856 }
Greg Akerf82e51c2010-04-14 19:33:50 -0500857
Barry Mienydd671972010-10-04 16:33:58 +0200858 $opening_bytes = fread($file, 256);
859 fclose($file);
Greg Akerf82e51c2010-04-14 19:33:50 -0500860
861 // These are known to throw IE into mime-type detection chaos
862 // <a, <body, <head, <html, <img, <plaintext, <pre, <script, <table, <title
863 // title is basically just in SVG, but we filter it anyhow
864
865 if ( ! preg_match('/<(a|body|head|html|img|plaintext|pre|script|table|title)[\s>]/i', $opening_bytes))
866 {
867 return TRUE; // its an image, no "triggers" detected in the first 256 bytes, we're good
868 }
869 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000870
871 if (($data = @file_get_contents($file)) === FALSE)
872 {
873 return FALSE;
874 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000875
Greg Akerf82e51c2010-04-14 19:33:50 -0500876 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200877
Derek Jones30841672010-04-26 09:09:21 -0500878 if ( ! isset($CI->security))
Derek Jones5640a712010-04-23 11:22:40 -0500879 {
Derek Jones247f0292010-04-26 09:10:21 -0500880 $CI->load->library('security');
Derek Jones5640a712010-04-23 11:22:40 -0500881 }
Barry Mienydd671972010-10-04 16:33:58 +0200882
Greg Akerf82e51c2010-04-14 19:33:50 -0500883 return $CI->security->xss_clean($data, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 }
Barry Mienydd671972010-10-04 16:33:58 +0200885
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200887
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 /**
889 * Set an error message
890 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 * @param string
892 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200893 */
Greg Aker58fdee82010-11-10 15:07:09 -0600894 public function set_error($msg)
Derek Allard2067d1a2008-11-13 22:59:24 +0000895 {
Barry Mienydd671972010-10-04 16:33:58 +0200896 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000897 $CI->lang->load('upload');
Barry Mienydd671972010-10-04 16:33:58 +0200898
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 if (is_array($msg))
900 {
901 foreach ($msg as $val)
902 {
Barry Mienydd671972010-10-04 16:33:58 +0200903 $msg = ($CI->lang->line($val) == FALSE) ? $val : $CI->lang->line($val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 $this->error_msg[] = $msg;
905 log_message('error', $msg);
Barry Mienydd671972010-10-04 16:33:58 +0200906 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000907 }
908 else
909 {
910 $msg = ($CI->lang->line($msg) == FALSE) ? $msg : $CI->lang->line($msg);
911 $this->error_msg[] = $msg;
912 log_message('error', $msg);
913 }
914 }
Barry Mienydd671972010-10-04 16:33:58 +0200915
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200917
Derek Allard2067d1a2008-11-13 22:59:24 +0000918 /**
919 * Display the error message
920 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 * @param string
922 * @param string
923 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200924 */
Greg Aker58fdee82010-11-10 15:07:09 -0600925 public function display_errors($open = '<p>', $close = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 {
927 $str = '';
928 foreach ($this->error_msg as $val)
929 {
930 $str .= $open.$val.$close;
931 }
Barry Mienydd671972010-10-04 16:33:58 +0200932
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 return $str;
934 }
Barry Mienydd671972010-10-04 16:33:58 +0200935
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200937
Derek Allard2067d1a2008-11-13 22:59:24 +0000938 /**
939 * List of Mime Types
940 *
941 * This is a list of mime types. We use it to validate
942 * the "allowed types" set by the developer
943 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000944 * @param string
945 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200946 */
Greg Aker58fdee82010-11-10 15:07:09 -0600947 public function mimes_types($mime)
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 {
949 global $mimes;
Barry Mienydd671972010-10-04 16:33:58 +0200950
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 if (count($this->mimes) == 0)
952 {
953 if (@require_once(APPPATH.'config/mimes'.EXT))
954 {
955 $this->mimes = $mimes;
956 unset($mimes);
957 }
958 }
Barry Mienydd671972010-10-04 16:33:58 +0200959
Derek Allard2067d1a2008-11-13 22:59:24 +0000960 return ( ! isset($this->mimes[$mime])) ? FALSE : $this->mimes[$mime];
961 }
962
963 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200964
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 /**
966 * Prep Filename
967 *
968 * Prevents possible script execution from Apache's handling of files multiple extensions
969 * http://httpd.apache.org/docs/1.3/mod/mod_mime.html#multipleext
970 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 * @param string
972 * @return string
973 */
Greg Aker58fdee82010-11-10 15:07:09 -0600974 protected function _prep_filename($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000975 {
Greg Aker924000e2010-07-22 11:04:58 -0500976 if (strpos($filename, '.') === FALSE OR $this->allowed_types == '*')
Derek Allard2067d1a2008-11-13 22:59:24 +0000977 {
978 return $filename;
979 }
Derek Allard616dab82009-02-16 15:44:32 +0000980
Derek Allard2067d1a2008-11-13 22:59:24 +0000981 $parts = explode('.', $filename);
982 $ext = array_pop($parts);
983 $filename = array_shift($parts);
Derek Allard616dab82009-02-16 15:44:32 +0000984
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 foreach ($parts as $part)
986 {
Derek Jonese9d723f2010-07-12 10:10:59 -0500987 if ( ! in_array(strtolower($part), $this->allowed_types) OR $this->mimes_types(strtolower($part)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000988 {
989 $filename .= '.'.$part.'_';
990 }
991 else
992 {
993 $filename .= '.'.$part;
994 }
995 }
Derek Allardd70b0642009-02-16 13:51:42 +0000996
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 $filename .= '.'.$ext;
Barry Mienydd671972010-10-04 16:33:58 +0200998
Derek Allard2067d1a2008-11-13 22:59:24 +0000999 return $filename;
1000 }
1001
1002 // --------------------------------------------------------------------
1003
1004}
1005// END Upload Class
1006
1007/* End of file Upload.php */
Greg Aker58fdee82010-11-10 15:07:09 -06001008/* Location: ./system/libraries/Upload.php */