blob: 4ccc032e93908f85119c0060c69723b6004fff4f [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);
215 $this->file_ext = $this->get_extension($this->file_name);
216
217 if ( ! $this->is_allowed_filetype(TRUE))
218 {
219 $this->set_error('upload_invalid_filetype');
Barry Mienydd671972010-10-04 16:33:58 +0200220 return FALSE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500221 }
222 }
Barry Mienydd671972010-10-04 16:33:58 +0200223
Derek Jonese9d723f2010-07-12 10:10:59 -0500224 // Convert the file size to kilobytes
225 if ($this->file_size > 0)
226 {
227 $this->file_size = round($this->file_size/1024, 2);
228 }
229
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 // Is the file size within the allowed maximum?
231 if ( ! $this->is_allowed_filesize())
232 {
233 $this->set_error('upload_invalid_filesize');
234 return FALSE;
235 }
236
237 // Are the image dimensions within the allowed size?
238 // Note: This can fail if the server has an open_basdir restriction.
239 if ( ! $this->is_allowed_dimensions())
240 {
241 $this->set_error('upload_invalid_dimensions');
242 return FALSE;
243 }
244
245 // Sanitize the file name for security
246 $this->file_name = $this->clean_file_name($this->file_name);
Barry Mienydd671972010-10-04 16:33:58 +0200247
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 // Truncate the file name if it's too long
249 if ($this->max_filename > 0)
250 {
251 $this->file_name = $this->limit_filename_length($this->file_name, $this->max_filename);
252 }
253
254 // Remove white spaces in the name
255 if ($this->remove_spaces == TRUE)
256 {
257 $this->file_name = preg_replace("/\s+/", "_", $this->file_name);
258 }
259
260 /*
261 * Validate the file name
262 * This function appends an number onto the end of
263 * the file if one with the same name already exists.
264 * If it returns false there was a problem.
265 */
266 $this->orig_name = $this->file_name;
267
268 if ($this->overwrite == FALSE)
269 {
270 $this->file_name = $this->set_filename($this->upload_path, $this->file_name);
Barry Mienydd671972010-10-04 16:33:58 +0200271
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 if ($this->file_name === FALSE)
273 {
274 return FALSE;
275 }
276 }
277
278 /*
Derek Jonese9d723f2010-07-12 10:10:59 -0500279 * Run the file through the XSS hacking filter
280 * This helps prevent malicious code from being
281 * embedded within a file. Scripts can easily
282 * be disguised as images or other file types.
283 */
284 if ($this->xss_clean)
285 {
286 if ($this->do_xss_clean() === FALSE)
287 {
288 $this->set_error('upload_unable_to_write_file');
289 return FALSE;
290 }
291 }
292
293 /*
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 * Move the file to the final destination
295 * To deal with different server configurations
296 * we'll attempt to use copy() first. If that fails
297 * we'll use move_uploaded_file(). One of the two should
298 * reliably work in most environments
299 */
300 if ( ! @copy($this->file_temp, $this->upload_path.$this->file_name))
301 {
302 if ( ! @move_uploaded_file($this->file_temp, $this->upload_path.$this->file_name))
303 {
Barry Mienydd671972010-10-04 16:33:58 +0200304 $this->set_error('upload_destination_error');
305 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 }
307 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000308
309 /*
310 * Set the finalized image dimensions
311 * This sets the image width/height (assuming the
312 * file was an image). We use this information
313 * in the "data" function.
314 */
315 $this->set_image_properties($this->upload_path.$this->file_name);
316
317 return TRUE;
318 }
Barry Mienydd671972010-10-04 16:33:58 +0200319
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200321
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 /**
323 * Finalized Data Array
Barry Mienydd671972010-10-04 16:33:58 +0200324 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 * Returns an associative array containing all of the information
326 * related to the upload, allowing the developer easy access in one array.
327 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200329 */
Greg Aker58fdee82010-11-10 15:07:09 -0600330 public function data()
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 {
332 return array (
333 'file_name' => $this->file_name,
334 'file_type' => $this->file_type,
335 'file_path' => $this->upload_path,
336 'full_path' => $this->upload_path.$this->file_name,
337 'raw_name' => str_replace($this->file_ext, '', $this->file_name),
338 'orig_name' => $this->orig_name,
Derek Jonese9d723f2010-07-12 10:10:59 -0500339 'client_name' => $this->client_name,
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 'file_ext' => $this->file_ext,
341 'file_size' => $this->file_size,
342 'is_image' => $this->is_image(),
343 'image_width' => $this->image_width,
344 'image_height' => $this->image_height,
345 'image_type' => $this->image_type,
346 'image_size_str' => $this->image_size_str,
347 );
348 }
Barry Mienydd671972010-10-04 16:33:58 +0200349
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200351
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 /**
353 * Set Upload Path
354 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 * @param string
356 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200357 */
Greg Aker58fdee82010-11-10 15:07:09 -0600358 public function set_upload_path($path)
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 {
360 // Make sure it has a trailing slash
361 $this->upload_path = rtrim($path, '/').'/';
362 }
Barry Mienydd671972010-10-04 16:33:58 +0200363
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200365
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 /**
367 * Set the file name
368 *
369 * This function takes a filename/path as input and looks for the
370 * existence of a file with the same name. If found, it will append a
371 * number to the end of the filename to avoid overwriting a pre-existing file.
372 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 * @param string
374 * @param string
375 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200376 */
Greg Aker58fdee82010-11-10 15:07:09 -0600377 public function set_filename($path, $filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 {
379 if ($this->encrypt_name == TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200380 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 mt_srand();
Barry Mienydd671972010-10-04 16:33:58 +0200382 $filename = md5(uniqid(mt_rand())).$this->file_ext;
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 }
Barry Mienydd671972010-10-04 16:33:58 +0200384
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 if ( ! file_exists($path.$filename))
386 {
387 return $filename;
388 }
Barry Mienydd671972010-10-04 16:33:58 +0200389
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 $filename = str_replace($this->file_ext, '', $filename);
Barry Mienydd671972010-10-04 16:33:58 +0200391
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 $new_filename = '';
393 for ($i = 1; $i < 100; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200394 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 if ( ! file_exists($path.$filename.$i.$this->file_ext))
396 {
397 $new_filename = $filename.$i.$this->file_ext;
398 break;
399 }
400 }
401
402 if ($new_filename == '')
403 {
404 $this->set_error('upload_bad_filename');
405 return FALSE;
406 }
407 else
408 {
409 return $new_filename;
410 }
411 }
Barry Mienydd671972010-10-04 16:33:58 +0200412
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200414
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 /**
416 * Set Maximum File Size
417 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 * @param integer
419 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200420 */
Greg Aker58fdee82010-11-10 15:07:09 -0600421 public function set_max_filesize($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 {
423 $this->max_size = ((int) $n < 0) ? 0: (int) $n;
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 Name Length
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_filename($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 {
436 $this->max_filename = ((int) $n < 0) ? 0: (int) $n;
437 }
438
439 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200440
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 /**
442 * Set Maximum Image Width
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_width($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 {
449 $this->max_width = ((int) $n < 0) ? 0: (int) $n;
450 }
Barry Mienydd671972010-10-04 16:33:58 +0200451
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200453
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 /**
455 * Set Maximum Image Height
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_height($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 {
462 $this->max_height = ((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 Allowed File Types
469 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 * @param string
471 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200472 */
Greg Aker58fdee82010-11-10 15:07:09 -0600473 public function set_allowed_types($types)
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 {
Derek Jonese12f64e2010-03-02 22:55:08 -0600475 if ( ! is_array($types) && $types == '*')
476 {
477 $this->allowed_types = '*';
478 return;
479 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 $this->allowed_types = explode('|', $types);
481 }
Barry Mienydd671972010-10-04 16:33:58 +0200482
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200484
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 /**
486 * Set Image Properties
487 *
488 * Uses GD to determine the width/height/type of image
489 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 * @param string
491 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200492 */
Greg Aker58fdee82010-11-10 15:07:09 -0600493 public function set_image_properties($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 {
495 if ( ! $this->is_image())
496 {
497 return;
498 }
499
500 if (function_exists('getimagesize'))
501 {
502 if (FALSE !== ($D = @getimagesize($path)))
Barry Mienydd671972010-10-04 16:33:58 +0200503 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
505
506 $this->image_width = $D['0'];
507 $this->image_height = $D['1'];
508 $this->image_type = ( ! isset($types[$D['2']])) ? 'unknown' : $types[$D['2']];
509 $this->image_size_str = $D['3']; // string containing height and width
510 }
511 }
512 }
Barry Mienydd671972010-10-04 16:33:58 +0200513
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200515
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 /**
517 * Set XSS Clean
518 *
519 * Enables the XSS flag so that the file that was uploaded
520 * will be run through the XSS filter.
521 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 * @param bool
523 * @return void
524 */
Greg Aker58fdee82010-11-10 15:07:09 -0600525 public function set_xss_clean($flag = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 {
527 $this->xss_clean = ($flag == TRUE) ? TRUE : FALSE;
528 }
Barry Mienydd671972010-10-04 16:33:58 +0200529
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200531
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 /**
533 * Validate the image
534 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200536 */
Greg Aker58fdee82010-11-10 15:07:09 -0600537 public function is_image()
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 {
539 // IE will sometimes return odd mime-types during upload, so here we just standardize all
540 // jpegs or pngs to the same file type.
541
542 $png_mimes = array('image/x-png');
543 $jpeg_mimes = array('image/jpg', 'image/jpe', 'image/jpeg', 'image/pjpeg');
Barry Mienydd671972010-10-04 16:33:58 +0200544
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 if (in_array($this->file_type, $png_mimes))
546 {
547 $this->file_type = 'image/png';
548 }
Barry Mienydd671972010-10-04 16:33:58 +0200549
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 if (in_array($this->file_type, $jpeg_mimes))
551 {
552 $this->file_type = 'image/jpeg';
553 }
554
555 $img_mimes = array(
556 'image/gif',
557 'image/jpeg',
558 'image/png',
Barry Mienydd671972010-10-04 16:33:58 +0200559 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000560
561 return (in_array($this->file_type, $img_mimes, TRUE)) ? TRUE : FALSE;
562 }
Barry Mienydd671972010-10-04 16:33:58 +0200563
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200565
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 /**
567 * Verify that the filetype is allowed
568 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200570 */
Greg Aker58fdee82010-11-10 15:07:09 -0600571 public function is_allowed_filetype($ignore_mime = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 {
Derek Jonese12f64e2010-03-02 22:55:08 -0600573 if ($this->allowed_types == '*')
574 {
575 return TRUE;
576 }
Barry Mienydd671972010-10-04 16:33:58 +0200577
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 if (count($this->allowed_types) == 0 OR ! is_array($this->allowed_types))
579 {
580 $this->set_error('upload_no_file_types');
581 return FALSE;
582 }
Barry Mienydd671972010-10-04 16:33:58 +0200583
Derek Jonese9d723f2010-07-12 10:10:59 -0500584 $ext = strtolower(ltrim($this->file_ext, '.'));
Barry Mienydd671972010-10-04 16:33:58 +0200585
Derek Jonese9d723f2010-07-12 10:10:59 -0500586 if ( ! in_array($ext, $this->allowed_types))
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 {
Derek Jonese9d723f2010-07-12 10:10:59 -0500588 return FALSE;
589 }
Derek Jonesafa282f2009-02-10 17:11:52 +0000590
Barry Mienydd671972010-10-04 16:33:58 +0200591 // Images get some additional checks
Derek Jonese9d723f2010-07-12 10:10:59 -0500592 $image_types = array('gif', 'jpg', 'jpeg', 'png', 'jpe');
Barry Mienydd671972010-10-04 16:33:58 +0200593
Derek Jonese9d723f2010-07-12 10:10:59 -0500594 if (in_array($ext, $image_types))
595 {
596 if (getimagesize($this->file_temp) === FALSE)
Derek Jonesafa282f2009-02-10 17:11:52 +0000597 {
Derek Jonese9d723f2010-07-12 10:10:59 -0500598 return FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200599 }
Derek Jonese9d723f2010-07-12 10:10:59 -0500600 }
Barry Mienydd671972010-10-04 16:33:58 +0200601
Derek Jonese9d723f2010-07-12 10:10:59 -0500602 if ($ignore_mime === TRUE)
603 {
604 return TRUE;
605 }
Barry Mienydd671972010-10-04 16:33:58 +0200606
Derek Jonese9d723f2010-07-12 10:10:59 -0500607 $mime = $this->mimes_types($ext);
Barry Mienydd671972010-10-04 16:33:58 +0200608
Derek Jonese9d723f2010-07-12 10:10:59 -0500609 if (is_array($mime))
610 {
611 if (in_array($this->file_type, $mime, TRUE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 {
Derek Jonese9d723f2010-07-12 10:10:59 -0500613 return TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200614 }
Derek Jonese9d723f2010-07-12 10:10:59 -0500615 }
616 elseif ($mime == $this->file_type)
617 {
618 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 }
Barry Mienydd671972010-10-04 16:33:58 +0200620
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 return FALSE;
622 }
Barry Mienydd671972010-10-04 16:33:58 +0200623
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200625
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 /**
627 * Verify that the file is within the allowed size
628 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200630 */
Greg Aker58fdee82010-11-10 15:07:09 -0600631 public function is_allowed_filesize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 {
633 if ($this->max_size != 0 AND $this->file_size > $this->max_size)
634 {
635 return FALSE;
636 }
637 else
638 {
639 return TRUE;
640 }
641 }
Barry Mienydd671972010-10-04 16:33:58 +0200642
Derek Allard2067d1a2008-11-13 22:59:24 +0000643 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200644
Derek Allard2067d1a2008-11-13 22:59:24 +0000645 /**
646 * Verify that the image is within the allowed width/height
647 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200649 */
Greg Aker58fdee82010-11-10 15:07:09 -0600650 public function is_allowed_dimensions()
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 {
652 if ( ! $this->is_image())
653 {
654 return TRUE;
655 }
656
657 if (function_exists('getimagesize'))
658 {
659 $D = @getimagesize($this->file_temp);
660
661 if ($this->max_width > 0 AND $D['0'] > $this->max_width)
662 {
663 return FALSE;
664 }
665
666 if ($this->max_height > 0 AND $D['1'] > $this->max_height)
667 {
668 return FALSE;
669 }
670
671 return TRUE;
672 }
673
674 return TRUE;
675 }
Barry Mienydd671972010-10-04 16:33:58 +0200676
Derek Allard2067d1a2008-11-13 22:59:24 +0000677 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200678
Derek Allard2067d1a2008-11-13 22:59:24 +0000679 /**
680 * Validate Upload Path
681 *
682 * Verifies that it is a valid upload path with proper permissions.
683 *
684 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000685 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200686 */
Greg Aker58fdee82010-11-10 15:07:09 -0600687 public function validate_upload_path()
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 {
689 if ($this->upload_path == '')
690 {
691 $this->set_error('upload_no_filepath');
692 return FALSE;
693 }
Barry Mienydd671972010-10-04 16:33:58 +0200694
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 if (function_exists('realpath') AND @realpath($this->upload_path) !== FALSE)
696 {
697 $this->upload_path = str_replace("\\", "/", realpath($this->upload_path));
698 }
699
700 if ( ! @is_dir($this->upload_path))
701 {
702 $this->set_error('upload_no_filepath');
703 return FALSE;
704 }
705
706 if ( ! is_really_writable($this->upload_path))
707 {
708 $this->set_error('upload_not_writable');
709 return FALSE;
710 }
711
712 $this->upload_path = preg_replace("/(.+?)\/*$/", "\\1/", $this->upload_path);
713 return TRUE;
714 }
Barry Mienydd671972010-10-04 16:33:58 +0200715
Derek Allard2067d1a2008-11-13 22:59:24 +0000716 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200717
Derek Allard2067d1a2008-11-13 22:59:24 +0000718 /**
719 * Extract the file extension
720 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000721 * @param string
722 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200723 */
Greg Aker58fdee82010-11-10 15:07:09 -0600724 public function get_extension($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000725 {
726 $x = explode('.', $filename);
727 return '.'.end($x);
Barry Mienydd671972010-10-04 16:33:58 +0200728 }
729
Derek Allard2067d1a2008-11-13 22:59:24 +0000730 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200731
Derek Allard2067d1a2008-11-13 22:59:24 +0000732 /**
733 * Clean the file name for security
734 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000735 * @param string
736 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200737 */
Greg Aker58fdee82010-11-10 15:07:09 -0600738 public function clean_file_name($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000739 {
740 $bad = array(
741 "<!--",
742 "-->",
743 "'",
744 "<",
745 ">",
746 '"',
747 '&',
748 '$',
749 '=',
750 ';',
751 '?',
752 '/',
753 "%20",
754 "%22",
755 "%3c", // <
Barry Mienydd671972010-10-04 16:33:58 +0200756 "%253c", // <
757 "%3e", // >
758 "%0e", // >
759 "%28", // (
760 "%29", // )
761 "%2528", // (
762 "%26", // &
763 "%24", // $
764 "%3f", // ?
765 "%3b", // ;
Derek Allard2067d1a2008-11-13 22:59:24 +0000766 "%3d" // =
767 );
Barry Mienydd671972010-10-04 16:33:58 +0200768
Derek Allard2067d1a2008-11-13 22:59:24 +0000769 $filename = str_replace($bad, '', $filename);
770
771 return stripslashes($filename);
772 }
773
774 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200775
Derek Allard2067d1a2008-11-13 22:59:24 +0000776 /**
777 * Limit the File Name Length
778 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 * @param string
780 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200781 */
Greg Aker58fdee82010-11-10 15:07:09 -0600782 public function limit_filename_length($filename, $length)
Derek Allard2067d1a2008-11-13 22:59:24 +0000783 {
784 if (strlen($filename) < $length)
785 {
786 return $filename;
787 }
Barry Mienydd671972010-10-04 16:33:58 +0200788
Derek Allard2067d1a2008-11-13 22:59:24 +0000789 $ext = '';
790 if (strpos($filename, '.') !== FALSE)
791 {
792 $parts = explode('.', $filename);
793 $ext = '.'.array_pop($parts);
794 $filename = implode('.', $parts);
795 }
Barry Mienydd671972010-10-04 16:33:58 +0200796
Derek Allard2067d1a2008-11-13 22:59:24 +0000797 return substr($filename, 0, ($length - strlen($ext))).$ext;
798 }
799
800 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200801
Derek Allard2067d1a2008-11-13 22:59:24 +0000802 /**
803 * Runs the file through the XSS clean function
804 *
805 * This prevents people from embedding malicious code in their files.
806 * I'm not sure that it won't negatively affect certain files in unexpected ways,
807 * but so far I haven't found that it causes trouble.
808 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000809 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200810 */
Greg Aker58fdee82010-11-10 15:07:09 -0600811 public function do_xss_clean()
Barry Mienydd671972010-10-04 16:33:58 +0200812 {
Derek Jonese9d723f2010-07-12 10:10:59 -0500813 $file = $this->file_temp;
Barry Mienydd671972010-10-04 16:33:58 +0200814
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 if (filesize($file) == 0)
816 {
817 return FALSE;
818 }
Barry Mienydd671972010-10-04 16:33:58 +0200819
Greg Akerf82e51c2010-04-14 19:33:50 -0500820 if (function_exists('memory_get_usage') && memory_get_usage() && ini_get('memory_limit') != '')
821 {
822 $current = ini_get('memory_limit') * 1024 * 1024;
Barry Mienydd671972010-10-04 16:33:58 +0200823
Greg Akerc78a2592010-06-09 11:45:32 -0500824 // There was a bug/behavioural change in PHP 5.2, where numbers over one million get output
825 // into scientific notation. number_format() ensures this number is an integer
826 // http://bugs.php.net/bug.php?id=43053
Barry Mienydd671972010-10-04 16:33:58 +0200827
Greg Akerc78a2592010-06-09 11:45:32 -0500828 $new_memory = number_format(ceil(filesize($file) + $current), 0, '.', '');
Barry Mienydd671972010-10-04 16:33:58 +0200829
Greg Akerc78a2592010-06-09 11:45:32 -0500830 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 -0500831 }
832
833 // If the file being uploaded is an image, then we should have no problem with XSS attacks (in theory), but
834 // IE can be fooled into mime-type detecting a malformed image as an html file, thus executing an XSS attack on anyone
835 // using IE who looks at the image. It does this by inspecting the first 255 bytes of an image. To get around this
836 // 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 +0200837 // 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 -0500838 // attempted XSS attack.
839
840 if (function_exists('getimagesize') && @getimagesize($file) !== FALSE)
841 {
Barry Mienydd671972010-10-04 16:33:58 +0200842 if (($file = @fopen($file, 'rb')) === FALSE) // "b" to force binary
843 {
Greg Akerf82e51c2010-04-14 19:33:50 -0500844 return FALSE; // Couldn't open the file, return FALSE
Barry Mienydd671972010-10-04 16:33:58 +0200845 }
Greg Akerf82e51c2010-04-14 19:33:50 -0500846
Barry Mienydd671972010-10-04 16:33:58 +0200847 $opening_bytes = fread($file, 256);
848 fclose($file);
Greg Akerf82e51c2010-04-14 19:33:50 -0500849
850 // These are known to throw IE into mime-type detection chaos
851 // <a, <body, <head, <html, <img, <plaintext, <pre, <script, <table, <title
852 // title is basically just in SVG, but we filter it anyhow
853
854 if ( ! preg_match('/<(a|body|head|html|img|plaintext|pre|script|table|title)[\s>]/i', $opening_bytes))
855 {
856 return TRUE; // its an image, no "triggers" detected in the first 256 bytes, we're good
857 }
858 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000859
860 if (($data = @file_get_contents($file)) === FALSE)
861 {
862 return FALSE;
863 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000864
Greg Akerf82e51c2010-04-14 19:33:50 -0500865 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200866
Derek Jones30841672010-04-26 09:09:21 -0500867 if ( ! isset($CI->security))
Derek Jones5640a712010-04-23 11:22:40 -0500868 {
Derek Jones247f0292010-04-26 09:10:21 -0500869 $CI->load->library('security');
Derek Jones5640a712010-04-23 11:22:40 -0500870 }
Barry Mienydd671972010-10-04 16:33:58 +0200871
Greg Akerf82e51c2010-04-14 19:33:50 -0500872 return $CI->security->xss_clean($data, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000873 }
Barry Mienydd671972010-10-04 16:33:58 +0200874
Derek Allard2067d1a2008-11-13 22:59:24 +0000875 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200876
Derek Allard2067d1a2008-11-13 22:59:24 +0000877 /**
878 * Set an error message
879 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 * @param string
881 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200882 */
Greg Aker58fdee82010-11-10 15:07:09 -0600883 public function set_error($msg)
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 {
Barry Mienydd671972010-10-04 16:33:58 +0200885 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 $CI->lang->load('upload');
Barry Mienydd671972010-10-04 16:33:58 +0200887
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 if (is_array($msg))
889 {
890 foreach ($msg as $val)
891 {
Barry Mienydd671972010-10-04 16:33:58 +0200892 $msg = ($CI->lang->line($val) == FALSE) ? $val : $CI->lang->line($val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 $this->error_msg[] = $msg;
894 log_message('error', $msg);
Barry Mienydd671972010-10-04 16:33:58 +0200895 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 }
897 else
898 {
899 $msg = ($CI->lang->line($msg) == FALSE) ? $msg : $CI->lang->line($msg);
900 $this->error_msg[] = $msg;
901 log_message('error', $msg);
902 }
903 }
Barry Mienydd671972010-10-04 16:33:58 +0200904
Derek Allard2067d1a2008-11-13 22:59:24 +0000905 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200906
Derek Allard2067d1a2008-11-13 22:59:24 +0000907 /**
908 * Display the error message
909 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000910 * @param string
911 * @param string
912 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200913 */
Greg Aker58fdee82010-11-10 15:07:09 -0600914 public function display_errors($open = '<p>', $close = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +0000915 {
916 $str = '';
917 foreach ($this->error_msg as $val)
918 {
919 $str .= $open.$val.$close;
920 }
Barry Mienydd671972010-10-04 16:33:58 +0200921
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 return $str;
923 }
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 * List of Mime Types
929 *
930 * This is a list of mime types. We use it to validate
931 * the "allowed types" set by the developer
932 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 * @param string
934 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200935 */
Greg Aker58fdee82010-11-10 15:07:09 -0600936 public function mimes_types($mime)
Derek Allard2067d1a2008-11-13 22:59:24 +0000937 {
938 global $mimes;
Barry Mienydd671972010-10-04 16:33:58 +0200939
Derek Allard2067d1a2008-11-13 22:59:24 +0000940 if (count($this->mimes) == 0)
941 {
942 if (@require_once(APPPATH.'config/mimes'.EXT))
943 {
944 $this->mimes = $mimes;
945 unset($mimes);
946 }
947 }
Barry Mienydd671972010-10-04 16:33:58 +0200948
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 return ( ! isset($this->mimes[$mime])) ? FALSE : $this->mimes[$mime];
950 }
951
952 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200953
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 /**
955 * Prep Filename
956 *
957 * Prevents possible script execution from Apache's handling of files multiple extensions
958 * http://httpd.apache.org/docs/1.3/mod/mod_mime.html#multipleext
959 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000960 * @param string
961 * @return string
962 */
Greg Aker58fdee82010-11-10 15:07:09 -0600963 protected function _prep_filename($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000964 {
Greg Aker924000e2010-07-22 11:04:58 -0500965 if (strpos($filename, '.') === FALSE OR $this->allowed_types == '*')
Derek Allard2067d1a2008-11-13 22:59:24 +0000966 {
967 return $filename;
968 }
Derek Allard616dab82009-02-16 15:44:32 +0000969
Derek Allard2067d1a2008-11-13 22:59:24 +0000970 $parts = explode('.', $filename);
971 $ext = array_pop($parts);
972 $filename = array_shift($parts);
Derek Allard616dab82009-02-16 15:44:32 +0000973
Derek Allard2067d1a2008-11-13 22:59:24 +0000974 foreach ($parts as $part)
975 {
Derek Jonese9d723f2010-07-12 10:10:59 -0500976 if ( ! in_array(strtolower($part), $this->allowed_types) OR $this->mimes_types(strtolower($part)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000977 {
978 $filename .= '.'.$part.'_';
979 }
980 else
981 {
982 $filename .= '.'.$part;
983 }
984 }
Derek Allardd70b0642009-02-16 13:51:42 +0000985
Derek Allard2067d1a2008-11-13 22:59:24 +0000986 $filename .= '.'.$ext;
Barry Mienydd671972010-10-04 16:33:58 +0200987
Derek Allard2067d1a2008-11-13 22:59:24 +0000988 return $filename;
989 }
990
991 // --------------------------------------------------------------------
992
993}
994// END Upload Class
995
996/* End of file Upload.php */
Greg Aker58fdee82010-11-10 15:07:09 -0600997/* Location: ./system/libraries/Upload.php */