blob: 8f324de7970b1e00925a59ba8b27a97126d5c2af [file] [log] [blame]
Derek Jones4b9c6292011-07-01 17:40:48 -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;
Adam Jackettccbbea12011-08-21 16:19:11 -040033 public $max_filename_increment = 100;
Greg Aker58fdee82010-11-10 15:07:09 -060034 public $allowed_types = "";
35 public $file_temp = "";
36 public $file_name = "";
37 public $orig_name = "";
38 public $file_type = "";
39 public $file_size = "";
40 public $file_ext = "";
41 public $upload_path = "";
42 public $overwrite = FALSE;
43 public $encrypt_name = FALSE;
44 public $is_image = FALSE;
45 public $image_width = '';
46 public $image_height = '';
47 public $image_type = '';
48 public $image_size_str = '';
49 public $error_msg = array();
50 public $mimes = array();
51 public $remove_spaces = TRUE;
52 public $xss_clean = FALSE;
53 public $temp_prefix = "temp_file_";
54 public $client_name = '';
Barry Mienydd671972010-10-04 16:33:58 +020055
Greg Aker58fdee82010-11-10 15:07:09 -060056 protected $_file_name_override = '';
Barry Mienydd671972010-10-04 16:33:58 +020057
Derek Allard2067d1a2008-11-13 22:59:24 +000058 /**
59 * Constructor
60 *
61 * @access public
62 */
Greg Aker58fdee82010-11-10 15:07:09 -060063 public function __construct($props = array())
Derek Allard2067d1a2008-11-13 22:59:24 +000064 {
65 if (count($props) > 0)
66 {
67 $this->initialize($props);
68 }
Barry Mienydd671972010-10-04 16:33:58 +020069
Derek Allard2067d1a2008-11-13 22:59:24 +000070 log_message('debug', "Upload Class Initialized");
71 }
Barry Mienydd671972010-10-04 16:33:58 +020072
Derek Allard2067d1a2008-11-13 22:59:24 +000073 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020074
Derek Allard2067d1a2008-11-13 22:59:24 +000075 /**
76 * Initialize preferences
77 *
Derek Allard2067d1a2008-11-13 22:59:24 +000078 * @param array
79 * @return void
Barry Mienydd671972010-10-04 16:33:58 +020080 */
Greg Aker58fdee82010-11-10 15:07:09 -060081 public function initialize($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +000082 {
83 $defaults = array(
Adam Jackettccbbea12011-08-21 16:19:11 -040084 'max_size' => 0,
85 'max_width' => 0,
86 'max_height' => 0,
87 'max_filename' => 0,
88 'max_filename_increment' => 100,
89 'allowed_types' => "",
90 'file_temp' => "",
91 'file_name' => "",
92 'orig_name' => "",
93 'file_type' => "",
94 'file_size' => "",
95 'file_ext' => "",
96 'upload_path' => "",
97 'overwrite' => FALSE,
98 'encrypt_name' => FALSE,
99 'is_image' => FALSE,
100 'image_width' => '',
101 'image_height' => '',
102 'image_type' => '',
103 'image_size_str' => '',
104 'error_msg' => array(),
105 'mimes' => array(),
106 'remove_spaces' => TRUE,
107 'xss_clean' => FALSE,
108 'temp_prefix' => "temp_file_",
109 'client_name' => ''
Barry Mienydd671972010-10-04 16:33:58 +0200110 );
111
112
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 foreach ($defaults as $key => $val)
114 {
115 if (isset($config[$key]))
116 {
117 $method = 'set_'.$key;
118 if (method_exists($this, $method))
119 {
120 $this->$method($config[$key]);
121 }
122 else
123 {
124 $this->$key = $config[$key];
Barry Mienydd671972010-10-04 16:33:58 +0200125 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 }
127 else
128 {
129 $this->$key = $val;
130 }
131 }
Barry Mienydd671972010-10-04 16:33:58 +0200132
Derek Jonese9d723f2010-07-12 10:10:59 -0500133 // if a file_name was provided in the config, use it instead of the user input
134 // supplied file name for all uploads until initialized again
135 $this->_file_name_override = $this->file_name;
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 }
Barry Mienydd671972010-10-04 16:33:58 +0200137
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200139
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 /**
141 * Perform the file upload
142 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200144 */
Greg Aker58fdee82010-11-10 15:07:09 -0600145 public function do_upload($field = 'userfile')
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 {
Eric Barnes92808342011-03-18 09:02:37 -0400147
Robin Sowell0ecc0622011-01-30 16:47:43 -0500148 // Is $_FILES[$field] set? If not, no reason to continue.
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 if ( ! isset($_FILES[$field]))
150 {
151 $this->set_error('upload_no_file_selected');
152 return FALSE;
153 }
Barry Mienydd671972010-10-04 16:33:58 +0200154
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 // Is the upload path valid?
156 if ( ! $this->validate_upload_path())
157 {
158 // errors will already be set by validate_upload_path() so just return FALSE
159 return FALSE;
160 }
161
162 // Was the file able to be uploaded? If not, determine the reason why.
163 if ( ! is_uploaded_file($_FILES[$field]['tmp_name']))
164 {
165 $error = ( ! isset($_FILES[$field]['error'])) ? 4 : $_FILES[$field]['error'];
166
167 switch($error)
168 {
169 case 1: // UPLOAD_ERR_INI_SIZE
170 $this->set_error('upload_file_exceeds_limit');
171 break;
172 case 2: // UPLOAD_ERR_FORM_SIZE
173 $this->set_error('upload_file_exceeds_form_limit');
174 break;
175 case 3: // UPLOAD_ERR_PARTIAL
Barry Mienydd671972010-10-04 16:33:58 +0200176 $this->set_error('upload_file_partial');
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 break;
178 case 4: // UPLOAD_ERR_NO_FILE
Barry Mienydd671972010-10-04 16:33:58 +0200179 $this->set_error('upload_no_file_selected');
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 break;
181 case 6: // UPLOAD_ERR_NO_TMP_DIR
182 $this->set_error('upload_no_temp_directory');
183 break;
184 case 7: // UPLOAD_ERR_CANT_WRITE
185 $this->set_error('upload_unable_to_write_file');
186 break;
187 case 8: // UPLOAD_ERR_EXTENSION
188 $this->set_error('upload_stopped_by_extension');
189 break;
Derek Jones4b9c6292011-07-01 17:40:48 -0500190 default : $this->set_error('upload_no_file_selected');
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 break;
192 }
193
194 return FALSE;
195 }
196
Derek Jonese9d723f2010-07-12 10:10:59 -0500197
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 // Set the uploaded data as class variables
Barry Mienydd671972010-10-04 16:33:58 +0200199 $this->file_temp = $_FILES[$field]['tmp_name'];
200 $this->file_size = $_FILES[$field]['size'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 $this->file_type = preg_replace("/^(.+?);.*$/", "\\1", $_FILES[$field]['type']);
Derek Jones616fb022010-04-22 16:52:18 -0500202 $this->file_type = strtolower(trim(stripslashes($this->file_type), '"'));
Derek Jonese9d723f2010-07-12 10:10:59 -0500203 $this->file_name = $this->_prep_filename($_FILES[$field]['name']);
204 $this->file_ext = $this->get_extension($this->file_name);
205 $this->client_name = $this->file_name;
Barry Mienydd671972010-10-04 16:33:58 +0200206
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 // Is the file type allowed to be uploaded?
208 if ( ! $this->is_allowed_filetype())
209 {
210 $this->set_error('upload_invalid_filetype');
211 return FALSE;
212 }
213
Derek Jonese9d723f2010-07-12 10:10:59 -0500214 // if we're overriding, let's now make sure the new name and type is allowed
215 if ($this->_file_name_override != '')
216 {
217 $this->file_name = $this->_prep_filename($this->_file_name_override);
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000218
219 // If no extension was provided in the file_name config item, use the uploaded one
Pascal Kriete14287f32011-02-14 13:39:34 -0500220 if (strpos($this->_file_name_override, '.') === FALSE)
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000221 {
222 $this->file_name .= $this->file_ext;
223 }
224
225 // An extension was provided, lets have it!
226 else
227 {
228 $this->file_ext = $this->get_extension($this->_file_name_override);
229 }
Derek Jonese9d723f2010-07-12 10:10:59 -0500230
231 if ( ! $this->is_allowed_filetype(TRUE))
232 {
233 $this->set_error('upload_invalid_filetype');
Barry Mienydd671972010-10-04 16:33:58 +0200234 return FALSE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500235 }
236 }
Barry Mienydd671972010-10-04 16:33:58 +0200237
Derek Jonese9d723f2010-07-12 10:10:59 -0500238 // Convert the file size to kilobytes
239 if ($this->file_size > 0)
240 {
241 $this->file_size = round($this->file_size/1024, 2);
242 }
243
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 // Is the file size within the allowed maximum?
245 if ( ! $this->is_allowed_filesize())
246 {
247 $this->set_error('upload_invalid_filesize');
248 return FALSE;
249 }
250
251 // Are the image dimensions within the allowed size?
252 // Note: This can fail if the server has an open_basdir restriction.
253 if ( ! $this->is_allowed_dimensions())
254 {
255 $this->set_error('upload_invalid_dimensions');
256 return FALSE;
257 }
258
259 // Sanitize the file name for security
260 $this->file_name = $this->clean_file_name($this->file_name);
Barry Mienydd671972010-10-04 16:33:58 +0200261
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 // Truncate the file name if it's too long
263 if ($this->max_filename > 0)
264 {
265 $this->file_name = $this->limit_filename_length($this->file_name, $this->max_filename);
266 }
267
268 // Remove white spaces in the name
269 if ($this->remove_spaces == TRUE)
270 {
271 $this->file_name = preg_replace("/\s+/", "_", $this->file_name);
272 }
273
274 /*
275 * Validate the file name
276 * This function appends an number onto the end of
277 * the file if one with the same name already exists.
278 * If it returns false there was a problem.
279 */
280 $this->orig_name = $this->file_name;
281
282 if ($this->overwrite == FALSE)
283 {
284 $this->file_name = $this->set_filename($this->upload_path, $this->file_name);
Barry Mienydd671972010-10-04 16:33:58 +0200285
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 if ($this->file_name === FALSE)
287 {
288 return FALSE;
289 }
290 }
291
292 /*
Derek Jonese9d723f2010-07-12 10:10:59 -0500293 * Run the file through the XSS hacking filter
294 * This helps prevent malicious code from being
Derek Jones4b9c6292011-07-01 17:40:48 -0500295 * embedded within a file. Scripts can easily
Derek Jonese9d723f2010-07-12 10:10:59 -0500296 * be disguised as images or other file types.
297 */
298 if ($this->xss_clean)
299 {
300 if ($this->do_xss_clean() === FALSE)
301 {
302 $this->set_error('upload_unable_to_write_file');
303 return FALSE;
304 }
305 }
306
307 /*
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 * Move the file to the final destination
309 * To deal with different server configurations
Derek Jones4b9c6292011-07-01 17:40:48 -0500310 * we'll attempt to use copy() first. If that fails
311 * we'll use move_uploaded_file(). One of the two should
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 * reliably work in most environments
313 */
314 if ( ! @copy($this->file_temp, $this->upload_path.$this->file_name))
315 {
316 if ( ! @move_uploaded_file($this->file_temp, $this->upload_path.$this->file_name))
317 {
Barry Mienydd671972010-10-04 16:33:58 +0200318 $this->set_error('upload_destination_error');
319 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 }
321 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000322
323 /*
324 * Set the finalized image dimensions
325 * This sets the image width/height (assuming the
Derek Jones4b9c6292011-07-01 17:40:48 -0500326 * file was an image). We use this information
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 * in the "data" function.
328 */
329 $this->set_image_properties($this->upload_path.$this->file_name);
330
331 return TRUE;
332 }
Barry Mienydd671972010-10-04 16:33:58 +0200333
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200335
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 /**
337 * Finalized Data Array
Barry Mienydd671972010-10-04 16:33:58 +0200338 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 * Returns an associative array containing all of the information
340 * related to the upload, allowing the developer easy access in one array.
341 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200343 */
Greg Aker58fdee82010-11-10 15:07:09 -0600344 public function data()
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 {
346 return array (
347 'file_name' => $this->file_name,
348 'file_type' => $this->file_type,
349 'file_path' => $this->upload_path,
350 'full_path' => $this->upload_path.$this->file_name,
351 'raw_name' => str_replace($this->file_ext, '', $this->file_name),
352 'orig_name' => $this->orig_name,
Derek Jonese9d723f2010-07-12 10:10:59 -0500353 'client_name' => $this->client_name,
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 'file_ext' => $this->file_ext,
355 'file_size' => $this->file_size,
356 'is_image' => $this->is_image(),
357 'image_width' => $this->image_width,
358 'image_height' => $this->image_height,
359 'image_type' => $this->image_type,
360 'image_size_str' => $this->image_size_str,
361 );
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 Upload Path
368 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 * @param string
370 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200371 */
Greg Aker58fdee82010-11-10 15:07:09 -0600372 public function set_upload_path($path)
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 {
374 // Make sure it has a trailing slash
375 $this->upload_path = rtrim($path, '/').'/';
376 }
Barry Mienydd671972010-10-04 16:33:58 +0200377
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200379
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 /**
381 * Set the file name
382 *
383 * This function takes a filename/path as input and looks for the
384 * existence of a file with the same name. If found, it will append a
385 * number to the end of the filename to avoid overwriting a pre-existing file.
386 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 * @param string
388 * @param string
389 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200390 */
Greg Aker58fdee82010-11-10 15:07:09 -0600391 public function set_filename($path, $filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 {
393 if ($this->encrypt_name == TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200394 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 mt_srand();
Barry Mienydd671972010-10-04 16:33:58 +0200396 $filename = md5(uniqid(mt_rand())).$this->file_ext;
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 }
Barry Mienydd671972010-10-04 16:33:58 +0200398
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 if ( ! file_exists($path.$filename))
400 {
401 return $filename;
402 }
Barry Mienydd671972010-10-04 16:33:58 +0200403
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 $filename = str_replace($this->file_ext, '', $filename);
Barry Mienydd671972010-10-04 16:33:58 +0200405
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 $new_filename = '';
Adam Jackettccbbea12011-08-21 16:19:11 -0400407 for ($i = 1; $i < $this->max_filename_increment; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200408 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 if ( ! file_exists($path.$filename.$i.$this->file_ext))
410 {
411 $new_filename = $filename.$i.$this->file_ext;
412 break;
413 }
414 }
415
416 if ($new_filename == '')
417 {
418 $this->set_error('upload_bad_filename');
419 return FALSE;
420 }
421 else
422 {
423 return $new_filename;
424 }
425 }
Barry Mienydd671972010-10-04 16:33:58 +0200426
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200428
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 /**
430 * Set Maximum File Size
431 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 * @param integer
433 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200434 */
Greg Aker58fdee82010-11-10 15:07:09 -0600435 public function set_max_filesize($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 {
437 $this->max_size = ((int) $n < 0) ? 0: (int) $n;
438 }
Barry Mienydd671972010-10-04 16:33:58 +0200439
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200441
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 /**
443 * Set Maximum File Name Length
444 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 * @param integer
446 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200447 */
Greg Aker58fdee82010-11-10 15:07:09 -0600448 public function set_max_filename($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 {
450 $this->max_filename = ((int) $n < 0) ? 0: (int) $n;
451 }
452
453 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200454
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 /**
456 * Set Maximum Image Width
457 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 * @param integer
459 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200460 */
Greg Aker58fdee82010-11-10 15:07:09 -0600461 public function set_max_width($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 {
463 $this->max_width = ((int) $n < 0) ? 0: (int) $n;
464 }
Barry Mienydd671972010-10-04 16:33:58 +0200465
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200467
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 /**
469 * Set Maximum Image Height
470 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 * @param integer
472 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200473 */
Greg Aker58fdee82010-11-10 15:07:09 -0600474 public function set_max_height($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 {
476 $this->max_height = ((int) $n < 0) ? 0: (int) $n;
477 }
Barry Mienydd671972010-10-04 16:33:58 +0200478
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200480
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 /**
482 * Set Allowed File Types
483 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 * @param string
485 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200486 */
Greg Aker58fdee82010-11-10 15:07:09 -0600487 public function set_allowed_types($types)
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 {
Derek Jonese12f64e2010-03-02 22:55:08 -0600489 if ( ! is_array($types) && $types == '*')
490 {
491 $this->allowed_types = '*';
492 return;
493 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 $this->allowed_types = explode('|', $types);
495 }
Barry Mienydd671972010-10-04 16:33:58 +0200496
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200498
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 /**
500 * Set Image Properties
501 *
502 * Uses GD to determine the width/height/type of image
503 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 * @param string
505 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200506 */
Greg Aker58fdee82010-11-10 15:07:09 -0600507 public function set_image_properties($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 {
509 if ( ! $this->is_image())
510 {
511 return;
512 }
513
514 if (function_exists('getimagesize'))
515 {
516 if (FALSE !== ($D = @getimagesize($path)))
Barry Mienydd671972010-10-04 16:33:58 +0200517 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
519
520 $this->image_width = $D['0'];
521 $this->image_height = $D['1'];
522 $this->image_type = ( ! isset($types[$D['2']])) ? 'unknown' : $types[$D['2']];
Derek Jones4b9c6292011-07-01 17:40:48 -0500523 $this->image_size_str = $D['3']; // string containing height and width
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 }
525 }
526 }
Barry Mienydd671972010-10-04 16:33:58 +0200527
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200529
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 /**
531 * Set XSS Clean
532 *
533 * Enables the XSS flag so that the file that was uploaded
534 * will be run through the XSS filter.
535 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 * @param bool
537 * @return void
538 */
Greg Aker58fdee82010-11-10 15:07:09 -0600539 public function set_xss_clean($flag = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 {
541 $this->xss_clean = ($flag == TRUE) ? TRUE : FALSE;
542 }
Barry Mienydd671972010-10-04 16:33:58 +0200543
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200545
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 /**
547 * Validate the image
548 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200550 */
Greg Aker58fdee82010-11-10 15:07:09 -0600551 public function is_image()
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 {
553 // IE will sometimes return odd mime-types during upload, so here we just standardize all
554 // jpegs or pngs to the same file type.
555
Derek Jones4b9c6292011-07-01 17:40:48 -0500556 $png_mimes = array('image/x-png');
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 $jpeg_mimes = array('image/jpg', 'image/jpe', 'image/jpeg', 'image/pjpeg');
Barry Mienydd671972010-10-04 16:33:58 +0200558
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 if (in_array($this->file_type, $png_mimes))
560 {
561 $this->file_type = 'image/png';
562 }
Barry Mienydd671972010-10-04 16:33:58 +0200563
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 if (in_array($this->file_type, $jpeg_mimes))
565 {
566 $this->file_type = 'image/jpeg';
567 }
568
569 $img_mimes = array(
570 'image/gif',
571 'image/jpeg',
572 'image/png',
Barry Mienydd671972010-10-04 16:33:58 +0200573 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000574
575 return (in_array($this->file_type, $img_mimes, TRUE)) ? TRUE : FALSE;
576 }
Barry Mienydd671972010-10-04 16:33:58 +0200577
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200579
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 /**
581 * Verify that the filetype is allowed
582 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200584 */
Greg Aker58fdee82010-11-10 15:07:09 -0600585 public function is_allowed_filetype($ignore_mime = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 {
Derek Jonese12f64e2010-03-02 22:55:08 -0600587 if ($this->allowed_types == '*')
588 {
589 return TRUE;
590 }
Barry Mienydd671972010-10-04 16:33:58 +0200591
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 if (count($this->allowed_types) == 0 OR ! is_array($this->allowed_types))
593 {
594 $this->set_error('upload_no_file_types');
595 return FALSE;
596 }
Barry Mienydd671972010-10-04 16:33:58 +0200597
Derek Jonese9d723f2010-07-12 10:10:59 -0500598 $ext = strtolower(ltrim($this->file_ext, '.'));
Barry Mienydd671972010-10-04 16:33:58 +0200599
Derek Jonese9d723f2010-07-12 10:10:59 -0500600 if ( ! in_array($ext, $this->allowed_types))
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 {
Derek Jonese9d723f2010-07-12 10:10:59 -0500602 return FALSE;
603 }
Derek Jonesafa282f2009-02-10 17:11:52 +0000604
Barry Mienydd671972010-10-04 16:33:58 +0200605 // Images get some additional checks
Derek Jonese9d723f2010-07-12 10:10:59 -0500606 $image_types = array('gif', 'jpg', 'jpeg', 'png', 'jpe');
Barry Mienydd671972010-10-04 16:33:58 +0200607
Derek Jonese9d723f2010-07-12 10:10:59 -0500608 if (in_array($ext, $image_types))
609 {
610 if (getimagesize($this->file_temp) === FALSE)
Derek Jonesafa282f2009-02-10 17:11:52 +0000611 {
Derek Jonese9d723f2010-07-12 10:10:59 -0500612 return FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200613 }
Derek Jonese9d723f2010-07-12 10:10:59 -0500614 }
Barry Mienydd671972010-10-04 16:33:58 +0200615
Derek Jonese9d723f2010-07-12 10:10:59 -0500616 if ($ignore_mime === TRUE)
617 {
618 return TRUE;
619 }
Barry Mienydd671972010-10-04 16:33:58 +0200620
Derek Jonese9d723f2010-07-12 10:10:59 -0500621 $mime = $this->mimes_types($ext);
Barry Mienydd671972010-10-04 16:33:58 +0200622
Derek Jonese9d723f2010-07-12 10:10:59 -0500623 if (is_array($mime))
624 {
625 if (in_array($this->file_type, $mime, TRUE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 {
Derek Jonese9d723f2010-07-12 10:10:59 -0500627 return TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200628 }
Derek Jonese9d723f2010-07-12 10:10:59 -0500629 }
630 elseif ($mime == $this->file_type)
631 {
632 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 }
Barry Mienydd671972010-10-04 16:33:58 +0200634
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 return FALSE;
636 }
Barry Mienydd671972010-10-04 16:33:58 +0200637
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200639
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 /**
641 * Verify that the file is within the allowed size
642 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000643 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200644 */
Greg Aker58fdee82010-11-10 15:07:09 -0600645 public function is_allowed_filesize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500647 if ($this->max_size != 0 AND $this->file_size > $this->max_size)
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 {
649 return FALSE;
650 }
651 else
652 {
653 return TRUE;
654 }
655 }
Barry Mienydd671972010-10-04 16:33:58 +0200656
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200658
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 /**
660 * Verify that the image is within the allowed width/height
661 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000662 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200663 */
Greg Aker58fdee82010-11-10 15:07:09 -0600664 public function is_allowed_dimensions()
Derek Allard2067d1a2008-11-13 22:59:24 +0000665 {
666 if ( ! $this->is_image())
667 {
668 return TRUE;
669 }
670
671 if (function_exists('getimagesize'))
672 {
673 $D = @getimagesize($this->file_temp);
674
675 if ($this->max_width > 0 AND $D['0'] > $this->max_width)
676 {
677 return FALSE;
678 }
679
680 if ($this->max_height > 0 AND $D['1'] > $this->max_height)
681 {
682 return FALSE;
683 }
684
685 return TRUE;
686 }
687
688 return TRUE;
689 }
Barry Mienydd671972010-10-04 16:33:58 +0200690
Derek Allard2067d1a2008-11-13 22:59:24 +0000691 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200692
Derek Allard2067d1a2008-11-13 22:59:24 +0000693 /**
694 * Validate Upload Path
695 *
696 * Verifies that it is a valid upload path with proper permissions.
697 *
698 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000699 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200700 */
Greg Aker58fdee82010-11-10 15:07:09 -0600701 public function validate_upload_path()
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 {
703 if ($this->upload_path == '')
704 {
705 $this->set_error('upload_no_filepath');
706 return FALSE;
707 }
Barry Mienydd671972010-10-04 16:33:58 +0200708
Derek Allard2067d1a2008-11-13 22:59:24 +0000709 if (function_exists('realpath') AND @realpath($this->upload_path) !== FALSE)
710 {
711 $this->upload_path = str_replace("\\", "/", realpath($this->upload_path));
712 }
713
714 if ( ! @is_dir($this->upload_path))
715 {
716 $this->set_error('upload_no_filepath');
717 return FALSE;
718 }
719
720 if ( ! is_really_writable($this->upload_path))
721 {
722 $this->set_error('upload_not_writable');
723 return FALSE;
724 }
725
Derek Jones4b9c6292011-07-01 17:40:48 -0500726 $this->upload_path = preg_replace("/(.+?)\/*$/", "\\1/", $this->upload_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000727 return TRUE;
728 }
Barry Mienydd671972010-10-04 16:33:58 +0200729
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 * Extract the file extension
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 get_extension($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000739 {
740 $x = explode('.', $filename);
741 return '.'.end($x);
Barry Mienydd671972010-10-04 16:33:58 +0200742 }
743
Derek Allard2067d1a2008-11-13 22:59:24 +0000744 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200745
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 /**
747 * Clean the file name for security
748 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000749 * @param string
750 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200751 */
Greg Aker58fdee82010-11-10 15:07:09 -0600752 public function clean_file_name($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000753 {
754 $bad = array(
755 "<!--",
756 "-->",
757 "'",
758 "<",
759 ">",
760 '"',
761 '&',
762 '$',
763 '=',
764 ';',
765 '?',
766 '/',
767 "%20",
768 "%22",
769 "%3c", // <
Barry Mienydd671972010-10-04 16:33:58 +0200770 "%253c", // <
771 "%3e", // >
772 "%0e", // >
773 "%28", // (
774 "%29", // )
775 "%2528", // (
776 "%26", // &
777 "%24", // $
778 "%3f", // ?
779 "%3b", // ;
Derek Allard2067d1a2008-11-13 22:59:24 +0000780 "%3d" // =
781 );
Barry Mienydd671972010-10-04 16:33:58 +0200782
Derek Allard2067d1a2008-11-13 22:59:24 +0000783 $filename = str_replace($bad, '', $filename);
784
785 return stripslashes($filename);
786 }
787
788 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200789
Derek Allard2067d1a2008-11-13 22:59:24 +0000790 /**
791 * Limit the File Name Length
792 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000793 * @param string
794 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200795 */
Greg Aker58fdee82010-11-10 15:07:09 -0600796 public function limit_filename_length($filename, $length)
Derek Allard2067d1a2008-11-13 22:59:24 +0000797 {
798 if (strlen($filename) < $length)
799 {
800 return $filename;
801 }
Barry Mienydd671972010-10-04 16:33:58 +0200802
Derek Allard2067d1a2008-11-13 22:59:24 +0000803 $ext = '';
804 if (strpos($filename, '.') !== FALSE)
805 {
806 $parts = explode('.', $filename);
807 $ext = '.'.array_pop($parts);
808 $filename = implode('.', $parts);
809 }
Barry Mienydd671972010-10-04 16:33:58 +0200810
Derek Allard2067d1a2008-11-13 22:59:24 +0000811 return substr($filename, 0, ($length - strlen($ext))).$ext;
812 }
813
814 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200815
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 /**
817 * Runs the file through the XSS clean function
818 *
819 * This prevents people from embedding malicious code in their files.
820 * I'm not sure that it won't negatively affect certain files in unexpected ways,
821 * but so far I haven't found that it causes trouble.
822 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000823 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200824 */
Greg Aker58fdee82010-11-10 15:07:09 -0600825 public function do_xss_clean()
Barry Mienydd671972010-10-04 16:33:58 +0200826 {
Derek Jonese9d723f2010-07-12 10:10:59 -0500827 $file = $this->file_temp;
Barry Mienydd671972010-10-04 16:33:58 +0200828
Derek Allard2067d1a2008-11-13 22:59:24 +0000829 if (filesize($file) == 0)
830 {
831 return FALSE;
832 }
Barry Mienydd671972010-10-04 16:33:58 +0200833
Greg Akerf82e51c2010-04-14 19:33:50 -0500834 if (function_exists('memory_get_usage') && memory_get_usage() && ini_get('memory_limit') != '')
835 {
836 $current = ini_get('memory_limit') * 1024 * 1024;
Barry Mienydd671972010-10-04 16:33:58 +0200837
Greg Akerc78a2592010-06-09 11:45:32 -0500838 // There was a bug/behavioural change in PHP 5.2, where numbers over one million get output
Derek Jones4b9c6292011-07-01 17:40:48 -0500839 // into scientific notation. number_format() ensures this number is an integer
Greg Akerc78a2592010-06-09 11:45:32 -0500840 // http://bugs.php.net/bug.php?id=43053
Barry Mienydd671972010-10-04 16:33:58 +0200841
Greg Akerc78a2592010-06-09 11:45:32 -0500842 $new_memory = number_format(ceil(filesize($file) + $current), 0, '.', '');
Barry Mienydd671972010-10-04 16:33:58 +0200843
Greg Akerc78a2592010-06-09 11:45:32 -0500844 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 -0500845 }
846
847 // If the file being uploaded is an image, then we should have no problem with XSS attacks (in theory), but
848 // IE can be fooled into mime-type detecting a malformed image as an html file, thus executing an XSS attack on anyone
Derek Jones4b9c6292011-07-01 17:40:48 -0500849 // using IE who looks at the image. It does this by inspecting the first 255 bytes of an image. To get around this
850 // 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 +0200851 // 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 -0500852 // attempted XSS attack.
853
854 if (function_exists('getimagesize') && @getimagesize($file) !== FALSE)
855 {
Barry Mienydd671972010-10-04 16:33:58 +0200856 if (($file = @fopen($file, 'rb')) === FALSE) // "b" to force binary
857 {
Greg Akerf82e51c2010-04-14 19:33:50 -0500858 return FALSE; // Couldn't open the file, return FALSE
Barry Mienydd671972010-10-04 16:33:58 +0200859 }
Greg Akerf82e51c2010-04-14 19:33:50 -0500860
Barry Mienydd671972010-10-04 16:33:58 +0200861 $opening_bytes = fread($file, 256);
862 fclose($file);
Greg Akerf82e51c2010-04-14 19:33:50 -0500863
864 // These are known to throw IE into mime-type detection chaos
865 // <a, <body, <head, <html, <img, <plaintext, <pre, <script, <table, <title
866 // title is basically just in SVG, but we filter it anyhow
867
868 if ( ! preg_match('/<(a|body|head|html|img|plaintext|pre|script|table|title)[\s>]/i', $opening_bytes))
869 {
870 return TRUE; // its an image, no "triggers" detected in the first 256 bytes, we're good
871 }
872 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000873
874 if (($data = @file_get_contents($file)) === FALSE)
875 {
876 return FALSE;
877 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000878
Greg Akerf82e51c2010-04-14 19:33:50 -0500879 $CI =& get_instance();
Greg Akerf82e51c2010-04-14 19:33:50 -0500880 return $CI->security->xss_clean($data, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 }
Barry Mienydd671972010-10-04 16:33:58 +0200882
Derek Allard2067d1a2008-11-13 22:59:24 +0000883 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200884
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 /**
886 * Set an error message
887 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 * @param string
889 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200890 */
Greg Aker58fdee82010-11-10 15:07:09 -0600891 public function set_error($msg)
Derek Allard2067d1a2008-11-13 22:59:24 +0000892 {
Barry Mienydd671972010-10-04 16:33:58 +0200893 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 $CI->lang->load('upload');
Barry Mienydd671972010-10-04 16:33:58 +0200895
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 if (is_array($msg))
897 {
898 foreach ($msg as $val)
899 {
Barry Mienydd671972010-10-04 16:33:58 +0200900 $msg = ($CI->lang->line($val) == FALSE) ? $val : $CI->lang->line($val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 $this->error_msg[] = $msg;
902 log_message('error', $msg);
Barry Mienydd671972010-10-04 16:33:58 +0200903 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 }
905 else
906 {
907 $msg = ($CI->lang->line($msg) == FALSE) ? $msg : $CI->lang->line($msg);
908 $this->error_msg[] = $msg;
909 log_message('error', $msg);
910 }
911 }
Barry Mienydd671972010-10-04 16:33:58 +0200912
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200914
Derek Allard2067d1a2008-11-13 22:59:24 +0000915 /**
916 * Display the error message
917 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000918 * @param string
919 * @param string
920 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200921 */
Greg Aker58fdee82010-11-10 15:07:09 -0600922 public function display_errors($open = '<p>', $close = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +0000923 {
924 $str = '';
925 foreach ($this->error_msg as $val)
926 {
927 $str .= $open.$val.$close;
928 }
Barry Mienydd671972010-10-04 16:33:58 +0200929
Derek Allard2067d1a2008-11-13 22:59:24 +0000930 return $str;
931 }
Barry Mienydd671972010-10-04 16:33:58 +0200932
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200934
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 /**
936 * List of Mime Types
937 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500938 * This is a list of mime types. We use it to validate
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 * the "allowed types" set by the developer
940 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000941 * @param string
942 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200943 */
Greg Aker58fdee82010-11-10 15:07:09 -0600944 public function mimes_types($mime)
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 {
946 global $mimes;
Barry Mienydd671972010-10-04 16:33:58 +0200947
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 if (count($this->mimes) == 0)
949 {
Greg Aker3a746652011-04-19 10:59:47 -0500950 if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'))
bubbafoley0ea04142011-03-17 14:55:41 -0500951 {
Greg Aker3a746652011-04-19 10:59:47 -0500952 include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php');
Eric Barnesfdd5b112011-03-21 21:28:58 -0400953 }
Greg Aker3a746652011-04-19 10:59:47 -0500954 elseif (is_file(APPPATH.'config/mimes.php'))
Eric Barnesfdd5b112011-03-21 21:28:58 -0400955 {
Greg Aker3a746652011-04-19 10:59:47 -0500956 include(APPPATH.'config//mimes.php');
bubbafoley0ea04142011-03-17 14:55:41 -0500957 }
958 else
959 {
Eric Barnesfdd5b112011-03-21 21:28:58 -0400960 return FALSE;
bubbafoley0ea04142011-03-17 14:55:41 -0500961 }
Eric Barnes92808342011-03-18 09:02:37 -0400962
Eric Barnesfdd5b112011-03-21 21:28:58 -0400963 $this->mimes = $mimes;
964 unset($mimes);
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 }
Barry Mienydd671972010-10-04 16:33:58 +0200966
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 return ( ! isset($this->mimes[$mime])) ? FALSE : $this->mimes[$mime];
968 }
969
970 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200971
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 /**
973 * Prep Filename
974 *
975 * Prevents possible script execution from Apache's handling of files multiple extensions
976 * http://httpd.apache.org/docs/1.3/mod/mod_mime.html#multipleext
977 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000978 * @param string
979 * @return string
980 */
Greg Aker58fdee82010-11-10 15:07:09 -0600981 protected function _prep_filename($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000982 {
Greg Aker924000e2010-07-22 11:04:58 -0500983 if (strpos($filename, '.') === FALSE OR $this->allowed_types == '*')
Derek Allard2067d1a2008-11-13 22:59:24 +0000984 {
985 return $filename;
986 }
Derek Allard616dab82009-02-16 15:44:32 +0000987
Derek Allard2067d1a2008-11-13 22:59:24 +0000988 $parts = explode('.', $filename);
989 $ext = array_pop($parts);
990 $filename = array_shift($parts);
Derek Allard616dab82009-02-16 15:44:32 +0000991
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 foreach ($parts as $part)
993 {
Derek Jonese9d723f2010-07-12 10:10:59 -0500994 if ( ! in_array(strtolower($part), $this->allowed_types) OR $this->mimes_types(strtolower($part)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000995 {
996 $filename .= '.'.$part.'_';
997 }
998 else
999 {
1000 $filename .= '.'.$part;
1001 }
1002 }
Derek Allardd70b0642009-02-16 13:51:42 +00001003
Derek Allard2067d1a2008-11-13 22:59:24 +00001004 $filename .= '.'.$ext;
Barry Mienydd671972010-10-04 16:33:58 +02001005
Derek Allard2067d1a2008-11-13 22:59:24 +00001006 return $filename;
1007 }
1008
1009 // --------------------------------------------------------------------
1010
1011}
1012// END Upload Class
1013
1014/* End of file Upload.php */
Greg Aker58fdee82010-11-10 15:07:09 -06001015/* Location: ./system/libraries/Upload.php */