blob: c1e07de7a9e4dc9295c80060f1b4c9be612cbedc [file] [log] [blame]
Andrey Andreev6ca407e2012-03-01 15:33:21 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev3a459572011-12-21 11:23:11 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev3a459572011-12-21 11:23:11 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * File Uploading Class
30 *
31 * @package CodeIgniter
32 * @subpackage Libraries
33 * @category Uploads
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000035 * @link http://codeigniter.com/user_guide/libraries/file_uploading.html
36 */
37class CI_Upload {
Barry Mienydd671972010-10-04 16:33:58 +020038
Andrey Andreev8e6f7a92012-03-26 20:13:00 +030039 public $max_size = 0;
40 public $max_width = 0;
41 public $max_height = 0;
42 public $max_filename = 0;
Adam Jackettccbbea12011-08-21 16:19:11 -040043 public $max_filename_increment = 100;
Andrey Andreev8e6f7a92012-03-26 20:13:00 +030044 public $allowed_types = '';
45 public $file_temp = '';
46 public $file_name = '';
47 public $orig_name = '';
48 public $file_type = '';
49 public $file_size = '';
50 public $file_ext = '';
51 public $upload_path = '';
52 public $overwrite = FALSE;
53 public $encrypt_name = FALSE;
54 public $is_image = FALSE;
55 public $image_width = '';
56 public $image_height = '';
57 public $image_type = '';
58 public $image_size_str = '';
59 public $error_msg = array();
60 public $mimes = array();
61 public $remove_spaces = TRUE;
62 public $xss_clean = FALSE;
63 public $temp_prefix = 'temp_file_';
64 public $client_name = '';
Barry Mienydd671972010-10-04 16:33:58 +020065
Greg Aker58fdee82010-11-10 15:07:09 -060066 protected $_file_name_override = '';
Barry Mienydd671972010-10-04 16:33:58 +020067
Derek Allard2067d1a2008-11-13 22:59:24 +000068 /**
69 * Constructor
70 *
Andrey Andreev8e6f7a92012-03-26 20:13:00 +030071 * @param array
72 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +000073 */
Greg Aker58fdee82010-11-10 15:07:09 -060074 public function __construct($props = array())
Derek Allard2067d1a2008-11-13 22:59:24 +000075 {
76 if (count($props) > 0)
77 {
78 $this->initialize($props);
79 }
Barry Mienydd671972010-10-04 16:33:58 +020080
Andrey Andreev6ef498b2012-06-05 22:01:58 +030081 $this->mimes =& get_mimes();
82
Andrey Andreev8e6f7a92012-03-26 20:13:00 +030083 log_message('debug', 'Upload Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +000084 }
Barry Mienydd671972010-10-04 16:33:58 +020085
Derek Allard2067d1a2008-11-13 22:59:24 +000086 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020087
Derek Allard2067d1a2008-11-13 22:59:24 +000088 /**
89 * Initialize preferences
90 *
Derek Allard2067d1a2008-11-13 22:59:24 +000091 * @param array
92 * @return void
Barry Mienydd671972010-10-04 16:33:58 +020093 */
Greg Aker58fdee82010-11-10 15:07:09 -060094 public function initialize($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +000095 {
96 $defaults = array(
Andrey Andreev8e6f7a92012-03-26 20:13:00 +030097 'max_size' => 0,
98 'max_width' => 0,
99 'max_height' => 0,
100 'max_filename' => 0,
101 'max_filename_increment' => 100,
102 'allowed_types' => '',
103 'file_temp' => '',
104 'file_name' => '',
105 'orig_name' => '',
106 'file_type' => '',
107 'file_size' => '',
108 'file_ext' => '',
109 'upload_path' => '',
110 'overwrite' => FALSE,
111 'encrypt_name' => FALSE,
112 'is_image' => FALSE,
113 'image_width' => '',
114 'image_height' => '',
115 'image_type' => '',
116 'image_size_str' => '',
117 'error_msg' => array(),
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300118 'remove_spaces' => TRUE,
119 'xss_clean' => FALSE,
120 'temp_prefix' => 'temp_file_',
121 'client_name' => ''
122 );
Barry Mienydd671972010-10-04 16:33:58 +0200123
124
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 foreach ($defaults as $key => $val)
126 {
127 if (isset($config[$key]))
128 {
129 $method = 'set_'.$key;
130 if (method_exists($this, $method))
131 {
132 $this->$method($config[$key]);
133 }
134 else
135 {
136 $this->$key = $config[$key];
Barry Mienydd671972010-10-04 16:33:58 +0200137 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 }
139 else
140 {
141 $this->$key = $val;
142 }
143 }
Barry Mienydd671972010-10-04 16:33:58 +0200144
Derek Jonese9d723f2010-07-12 10:10:59 -0500145 // if a file_name was provided in the config, use it instead of the user input
146 // supplied file name for all uploads until initialized again
147 $this->_file_name_override = $this->file_name;
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 }
Barry Mienydd671972010-10-04 16:33:58 +0200149
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200151
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 /**
153 * Perform the file upload
154 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200156 */
Greg Aker58fdee82010-11-10 15:07:09 -0600157 public function do_upload($field = 'userfile')
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300159 // Is $_FILES[$field] set? If not, no reason to continue.
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 if ( ! isset($_FILES[$field]))
161 {
162 $this->set_error('upload_no_file_selected');
163 return FALSE;
164 }
Barry Mienydd671972010-10-04 16:33:58 +0200165
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 // Is the upload path valid?
167 if ( ! $this->validate_upload_path())
168 {
169 // errors will already be set by validate_upload_path() so just return FALSE
170 return FALSE;
171 }
172
173 // Was the file able to be uploaded? If not, determine the reason why.
174 if ( ! is_uploaded_file($_FILES[$field]['tmp_name']))
175 {
176 $error = ( ! isset($_FILES[$field]['error'])) ? 4 : $_FILES[$field]['error'];
177
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300178 switch ($error)
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 {
180 case 1: // UPLOAD_ERR_INI_SIZE
181 $this->set_error('upload_file_exceeds_limit');
182 break;
183 case 2: // UPLOAD_ERR_FORM_SIZE
184 $this->set_error('upload_file_exceeds_form_limit');
185 break;
186 case 3: // UPLOAD_ERR_PARTIAL
Barry Mienydd671972010-10-04 16:33:58 +0200187 $this->set_error('upload_file_partial');
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 break;
189 case 4: // UPLOAD_ERR_NO_FILE
Barry Mienydd671972010-10-04 16:33:58 +0200190 $this->set_error('upload_no_file_selected');
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 break;
192 case 6: // UPLOAD_ERR_NO_TMP_DIR
193 $this->set_error('upload_no_temp_directory');
194 break;
195 case 7: // UPLOAD_ERR_CANT_WRITE
196 $this->set_error('upload_unable_to_write_file');
197 break;
198 case 8: // UPLOAD_ERR_EXTENSION
199 $this->set_error('upload_stopped_by_extension');
200 break;
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300201 default:
202 $this->set_error('upload_no_file_selected');
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 break;
204 }
205
206 return FALSE;
207 }
208
209 // Set the uploaded data as class variables
Barry Mienydd671972010-10-04 16:33:58 +0200210 $this->file_temp = $_FILES[$field]['tmp_name'];
211 $this->file_size = $_FILES[$field]['size'];
Andrey Andreev3a3c9472011-09-24 14:25:33 +0300212 $this->_file_mime_type($_FILES[$field]);
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300213 $this->file_type = preg_replace('/^(.+?);.*$/', '\\1', $this->file_type);
Derek Jones616fb022010-04-22 16:52:18 -0500214 $this->file_type = strtolower(trim(stripslashes($this->file_type), '"'));
Derek Jonese9d723f2010-07-12 10:10:59 -0500215 $this->file_name = $this->_prep_filename($_FILES[$field]['name']);
216 $this->file_ext = $this->get_extension($this->file_name);
217 $this->client_name = $this->file_name;
Barry Mienydd671972010-10-04 16:33:58 +0200218
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 // Is the file type allowed to be uploaded?
220 if ( ! $this->is_allowed_filetype())
221 {
222 $this->set_error('upload_invalid_filetype');
223 return FALSE;
224 }
225
Derek Jonese9d723f2010-07-12 10:10:59 -0500226 // if we're overriding, let's now make sure the new name and type is allowed
Alex Bilbied261b1e2012-06-02 11:12:16 +0100227 if ($this->_file_name_override !== '')
Derek Jonese9d723f2010-07-12 10:10:59 -0500228 {
229 $this->file_name = $this->_prep_filename($this->_file_name_override);
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000230
231 // If no extension was provided in the file_name config item, use the uploaded one
Pascal Kriete14287f32011-02-14 13:39:34 -0500232 if (strpos($this->_file_name_override, '.') === FALSE)
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000233 {
234 $this->file_name .= $this->file_ext;
235 }
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000236 else
237 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300238 // An extension was provided, lets have it!
239 $this->file_ext = $this->get_extension($this->_file_name_override);
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000240 }
Derek Jonese9d723f2010-07-12 10:10:59 -0500241
242 if ( ! $this->is_allowed_filetype(TRUE))
243 {
244 $this->set_error('upload_invalid_filetype');
Barry Mienydd671972010-10-04 16:33:58 +0200245 return FALSE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500246 }
247 }
Barry Mienydd671972010-10-04 16:33:58 +0200248
Derek Jonese9d723f2010-07-12 10:10:59 -0500249 // Convert the file size to kilobytes
250 if ($this->file_size > 0)
251 {
252 $this->file_size = round($this->file_size/1024, 2);
253 }
254
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 // Is the file size within the allowed maximum?
256 if ( ! $this->is_allowed_filesize())
257 {
258 $this->set_error('upload_invalid_filesize');
259 return FALSE;
260 }
261
262 // Are the image dimensions within the allowed size?
263 // Note: This can fail if the server has an open_basdir restriction.
264 if ( ! $this->is_allowed_dimensions())
265 {
266 $this->set_error('upload_invalid_dimensions');
267 return FALSE;
268 }
269
270 // Sanitize the file name for security
271 $this->file_name = $this->clean_file_name($this->file_name);
Barry Mienydd671972010-10-04 16:33:58 +0200272
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 // Truncate the file name if it's too long
274 if ($this->max_filename > 0)
275 {
276 $this->file_name = $this->limit_filename_length($this->file_name, $this->max_filename);
277 }
278
279 // Remove white spaces in the name
Alex Bilbied261b1e2012-06-02 11:12:16 +0100280 if ($this->remove_spaces === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300282 $this->file_name = preg_replace('/\s+/', '_', $this->file_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 }
284
285 /*
286 * Validate the file name
287 * This function appends an number onto the end of
288 * the file if one with the same name already exists.
289 * If it returns false there was a problem.
290 */
291 $this->orig_name = $this->file_name;
292
Alex Bilbied261b1e2012-06-02 11:12:16 +0100293 if ($this->overwrite === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 {
295 $this->file_name = $this->set_filename($this->upload_path, $this->file_name);
Barry Mienydd671972010-10-04 16:33:58 +0200296
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 if ($this->file_name === FALSE)
298 {
299 return FALSE;
300 }
301 }
302
303 /*
Derek Jonese9d723f2010-07-12 10:10:59 -0500304 * Run the file through the XSS hacking filter
305 * This helps prevent malicious code from being
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300306 * embedded within a file. Scripts can easily
Derek Jonese9d723f2010-07-12 10:10:59 -0500307 * be disguised as images or other file types.
308 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300309 if ($this->xss_clean && $this->do_xss_clean() === FALSE)
Derek Jonese9d723f2010-07-12 10:10:59 -0500310 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300311 $this->set_error('upload_unable_to_write_file');
312 return FALSE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500313 }
314
315 /*
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 * Move the file to the final destination
317 * To deal with different server configurations
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300318 * we'll attempt to use copy() first. If that fails
319 * we'll use move_uploaded_file(). One of the two should
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 * reliably work in most environments
321 */
322 if ( ! @copy($this->file_temp, $this->upload_path.$this->file_name))
323 {
324 if ( ! @move_uploaded_file($this->file_temp, $this->upload_path.$this->file_name))
325 {
Barry Mienydd671972010-10-04 16:33:58 +0200326 $this->set_error('upload_destination_error');
327 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 }
329 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000330
331 /*
332 * Set the finalized image dimensions
333 * This sets the image width/height (assuming the
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300334 * file was an image). We use this information
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 * in the "data" function.
336 */
337 $this->set_image_properties($this->upload_path.$this->file_name);
338
339 return TRUE;
340 }
Barry Mienydd671972010-10-04 16:33:58 +0200341
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200343
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 /**
345 * Finalized Data Array
Barry Mienydd671972010-10-04 16:33:58 +0200346 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 * Returns an associative array containing all of the information
348 * related to the upload, allowing the developer easy access in one array.
349 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200351 */
Greg Aker58fdee82010-11-10 15:07:09 -0600352 public function data()
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300354 return array(
355 'file_name' => $this->file_name,
356 'file_type' => $this->file_type,
357 'file_path' => $this->upload_path,
358 'full_path' => $this->upload_path.$this->file_name,
359 'raw_name' => str_replace($this->file_ext, '', $this->file_name),
360 'orig_name' => $this->orig_name,
361 'client_name' => $this->client_name,
362 'file_ext' => $this->file_ext,
363 'file_size' => $this->file_size,
364 'is_image' => $this->is_image(),
365 'image_width' => $this->image_width,
366 'image_height' => $this->image_height,
367 'image_type' => $this->image_type,
368 'image_size_str' => $this->image_size_str,
369 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 }
Barry Mienydd671972010-10-04 16:33:58 +0200371
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200373
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 /**
375 * Set Upload Path
376 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 * @param string
378 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200379 */
Greg Aker58fdee82010-11-10 15:07:09 -0600380 public function set_upload_path($path)
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 {
382 // Make sure it has a trailing slash
383 $this->upload_path = rtrim($path, '/').'/';
384 }
Barry Mienydd671972010-10-04 16:33:58 +0200385
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200387
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 /**
389 * Set the file name
390 *
391 * This function takes a filename/path as input and looks for the
392 * existence of a file with the same name. If found, it will append a
393 * number to the end of the filename to avoid overwriting a pre-existing file.
394 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 * @param string
396 * @param string
397 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200398 */
Greg Aker58fdee82010-11-10 15:07:09 -0600399 public function set_filename($path, $filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100401 if ($this->encrypt_name === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200402 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 mt_srand();
Barry Mienydd671972010-10-04 16:33:58 +0200404 $filename = md5(uniqid(mt_rand())).$this->file_ext;
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 }
Barry Mienydd671972010-10-04 16:33:58 +0200406
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 if ( ! file_exists($path.$filename))
408 {
409 return $filename;
410 }
Barry Mienydd671972010-10-04 16:33:58 +0200411
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 $filename = str_replace($this->file_ext, '', $filename);
Barry Mienydd671972010-10-04 16:33:58 +0200413
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 $new_filename = '';
Adam Jackettccbbea12011-08-21 16:19:11 -0400415 for ($i = 1; $i < $this->max_filename_increment; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200416 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 if ( ! file_exists($path.$filename.$i.$this->file_ext))
418 {
419 $new_filename = $filename.$i.$this->file_ext;
420 break;
421 }
422 }
423
Alex Bilbied261b1e2012-06-02 11:12:16 +0100424 if ($new_filename === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 {
426 $this->set_error('upload_bad_filename');
427 return FALSE;
428 }
429 else
430 {
431 return $new_filename;
432 }
433 }
Barry Mienydd671972010-10-04 16:33:58 +0200434
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200436
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 /**
438 * Set Maximum File Size
439 *
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300440 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200442 */
Greg Aker58fdee82010-11-10 15:07:09 -0600443 public function set_max_filesize($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300445 $this->max_size = ((int) $n < 0) ? 0 : (int) $n;
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 }
Barry Mienydd671972010-10-04 16:33:58 +0200447
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200449
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 /**
451 * Set Maximum File Name Length
452 *
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300453 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200455 */
Greg Aker58fdee82010-11-10 15:07:09 -0600456 public function set_max_filename($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300458 $this->max_filename = ((int) $n < 0) ? 0 : (int) $n;
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 }
460
461 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200462
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 /**
464 * Set Maximum Image Width
465 *
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300466 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200468 */
Greg Aker58fdee82010-11-10 15:07:09 -0600469 public function set_max_width($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300471 $this->max_width = ((int) $n < 0) ? 0 : (int) $n;
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 }
Barry Mienydd671972010-10-04 16:33:58 +0200473
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200475
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 /**
477 * Set Maximum Image Height
478 *
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300479 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200481 */
Greg Aker58fdee82010-11-10 15:07:09 -0600482 public function set_max_height($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300484 $this->max_height = ((int) $n < 0) ? 0 : (int) $n;
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 }
Barry Mienydd671972010-10-04 16:33:58 +0200486
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200488
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 /**
490 * Set Allowed File Types
491 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 * @param string
493 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200494 */
Greg Aker58fdee82010-11-10 15:07:09 -0600495 public function set_allowed_types($types)
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300497 if ( ! is_array($types) && $types === '*')
Derek Jonese12f64e2010-03-02 22:55:08 -0600498 {
499 $this->allowed_types = '*';
500 return;
501 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 $this->allowed_types = explode('|', $types);
503 }
Barry Mienydd671972010-10-04 16:33:58 +0200504
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200506
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 /**
508 * Set Image Properties
509 *
510 * Uses GD to determine the width/height/type of image
511 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 * @param string
513 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200514 */
Greg Aker58fdee82010-11-10 15:07:09 -0600515 public function set_image_properties($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 {
517 if ( ! $this->is_image())
518 {
519 return;
520 }
521
522 if (function_exists('getimagesize'))
523 {
524 if (FALSE !== ($D = @getimagesize($path)))
Barry Mienydd671972010-10-04 16:33:58 +0200525 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
527
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300528 $this->image_width = $D[0];
529 $this->image_height = $D[1];
530 $this->image_type = isset($types[$D[2]]) ? $types[$D[2]] : 'unknown';
531 $this->image_size_str = $D[3]; // string containing height and width
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 }
533 }
534 }
Barry Mienydd671972010-10-04 16:33:58 +0200535
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200537
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 /**
539 * Set XSS Clean
540 *
541 * Enables the XSS flag so that the file that was uploaded
542 * will be run through the XSS filter.
543 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 * @param bool
545 * @return void
546 */
Greg Aker58fdee82010-11-10 15:07:09 -0600547 public function set_xss_clean($flag = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100549 $this->xss_clean = ($flag === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 }
Barry Mienydd671972010-10-04 16:33:58 +0200551
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200553
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 /**
555 * Validate the image
556 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200558 */
Greg Aker58fdee82010-11-10 15:07:09 -0600559 public function is_image()
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 {
561 // IE will sometimes return odd mime-types during upload, so here we just standardize all
562 // jpegs or pngs to the same file type.
563
Derek Jones4b9c6292011-07-01 17:40:48 -0500564 $png_mimes = array('image/x-png');
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 $jpeg_mimes = array('image/jpg', 'image/jpe', 'image/jpeg', 'image/pjpeg');
Barry Mienydd671972010-10-04 16:33:58 +0200566
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 if (in_array($this->file_type, $png_mimes))
568 {
569 $this->file_type = 'image/png';
570 }
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300571 elseif (in_array($this->file_type, $jpeg_mimes))
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 {
573 $this->file_type = 'image/jpeg';
574 }
575
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300576 $img_mimes = array('image/gif', 'image/jpeg', 'image/png');
Derek Allard2067d1a2008-11-13 22:59:24 +0000577
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300578 return in_array($this->file_type, $img_mimes, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 }
Barry Mienydd671972010-10-04 16:33:58 +0200580
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200582
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 /**
584 * Verify that the filetype is allowed
585 *
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200586 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200588 */
Greg Aker58fdee82010-11-10 15:07:09 -0600589 public function is_allowed_filetype($ignore_mime = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 {
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200591 if ($this->allowed_types === '*')
Derek Jonese12f64e2010-03-02 22:55:08 -0600592 {
593 return TRUE;
594 }
Barry Mienydd671972010-10-04 16:33:58 +0200595
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200596 if ( ! is_array($this->allowed_types) OR count($this->allowed_types) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 {
598 $this->set_error('upload_no_file_types');
599 return FALSE;
600 }
Barry Mienydd671972010-10-04 16:33:58 +0200601
Derek Jonese9d723f2010-07-12 10:10:59 -0500602 $ext = strtolower(ltrim($this->file_ext, '.'));
Barry Mienydd671972010-10-04 16:33:58 +0200603
Derek Jonese9d723f2010-07-12 10:10:59 -0500604 if ( ! in_array($ext, $this->allowed_types))
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 {
Derek Jonese9d723f2010-07-12 10:10:59 -0500606 return FALSE;
607 }
Derek Jonesafa282f2009-02-10 17:11:52 +0000608
Barry Mienydd671972010-10-04 16:33:58 +0200609 // Images get some additional checks
Derek Jonese9d723f2010-07-12 10:10:59 -0500610 $image_types = array('gif', 'jpg', 'jpeg', 'png', 'jpe');
Barry Mienydd671972010-10-04 16:33:58 +0200611
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200612 if (in_array($ext, $image_types) && @getimagesize($this->file_temp) === FALSE)
Derek Jonese9d723f2010-07-12 10:10:59 -0500613 {
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200614 return FALSE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500615 }
Barry Mienydd671972010-10-04 16:33:58 +0200616
Derek Jonese9d723f2010-07-12 10:10:59 -0500617 if ($ignore_mime === TRUE)
618 {
619 return TRUE;
620 }
Barry Mienydd671972010-10-04 16:33:58 +0200621
Derek Jonese9d723f2010-07-12 10:10:59 -0500622 $mime = $this->mimes_types($ext);
Barry Mienydd671972010-10-04 16:33:58 +0200623
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300624 if (is_array($mime) && in_array($this->file_type, $mime, TRUE))
Derek Jonese9d723f2010-07-12 10:10:59 -0500625 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300626 return TRUE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500627 }
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200628 elseif ($mime === $this->file_type)
Derek Jonese9d723f2010-07-12 10:10:59 -0500629 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300630 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 }
Barry Mienydd671972010-10-04 16:33:58 +0200632
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 return FALSE;
634 }
Barry Mienydd671972010-10-04 16:33:58 +0200635
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200637
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 /**
639 * Verify that the file is within the allowed size
640 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200642 */
Greg Aker58fdee82010-11-10 15:07:09 -0600643 public function is_allowed_filesize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100645 return ($this->max_size === 0 OR $this->max_size > $this->file_size);
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 }
Barry Mienydd671972010-10-04 16:33:58 +0200647
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200649
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 /**
651 * Verify that the image is within the allowed width/height
652 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200654 */
Greg Aker58fdee82010-11-10 15:07:09 -0600655 public function is_allowed_dimensions()
Derek Allard2067d1a2008-11-13 22:59:24 +0000656 {
657 if ( ! $this->is_image())
658 {
659 return TRUE;
660 }
661
662 if (function_exists('getimagesize'))
663 {
664 $D = @getimagesize($this->file_temp);
665
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300666 if ($this->max_width > 0 && $D[0] > $this->max_width)
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 {
668 return FALSE;
669 }
670
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300671 if ($this->max_height > 0 && $D[1] > $this->max_height)
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 {
673 return FALSE;
674 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 }
676
677 return TRUE;
678 }
Barry Mienydd671972010-10-04 16:33:58 +0200679
Derek Allard2067d1a2008-11-13 22:59:24 +0000680 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200681
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 /**
683 * Validate Upload Path
684 *
685 * Verifies that it is a valid upload path with proper permissions.
686 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200688 */
Greg Aker58fdee82010-11-10 15:07:09 -0600689 public function validate_upload_path()
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100691 if ($this->upload_path === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 {
693 $this->set_error('upload_no_filepath');
694 return FALSE;
695 }
Barry Mienydd671972010-10-04 16:33:58 +0200696
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300697 if (function_exists('realpath') && @realpath($this->upload_path) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000698 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300699 $this->upload_path = str_replace('\\', '/', realpath($this->upload_path));
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 }
701
702 if ( ! @is_dir($this->upload_path))
703 {
704 $this->set_error('upload_no_filepath');
705 return FALSE;
706 }
707
708 if ( ! is_really_writable($this->upload_path))
709 {
710 $this->set_error('upload_not_writable');
711 return FALSE;
712 }
713
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300714 $this->upload_path = preg_replace('/(.+?)\/*$/', '\\1/', $this->upload_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000715 return TRUE;
716 }
Barry Mienydd671972010-10-04 16:33:58 +0200717
Derek Allard2067d1a2008-11-13 22:59:24 +0000718 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200719
Derek Allard2067d1a2008-11-13 22:59:24 +0000720 /**
721 * Extract the file extension
722 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000723 * @param string
724 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200725 */
Greg Aker58fdee82010-11-10 15:07:09 -0600726 public function get_extension($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000727 {
728 $x = explode('.', $filename);
Andrey Andreev46d53fb2012-05-11 13:42:24 +0300729 return (count($x) !== 1) ? '.'.end($x) : '';
Barry Mienydd671972010-10-04 16:33:58 +0200730 }
731
Derek Allard2067d1a2008-11-13 22:59:24 +0000732 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200733
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 /**
735 * Clean the file name for security
736 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000737 * @param string
738 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200739 */
Greg Aker58fdee82010-11-10 15:07:09 -0600740 public function clean_file_name($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000741 {
742 $bad = array(
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300743 '<!--', '-->',
744 "'", '"',
745 '<', '>',
746 '&', '$',
747 '=',
748 ';',
749 '?',
750 '/',
Andrey Andreev470805b2012-05-24 21:57:21 +0300751 '!',
752 '#',
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300753 '%20',
754 '%22',
755 '%3c', // <
756 '%253c', // <
757 '%3e', // >
758 '%0e', // >
759 '%28', // (
760 '%29', // )
761 '%2528', // (
762 '%26', // &
763 '%24', // $
764 '%3f', // ?
765 '%3b', // ;
766 '%3d' // =
767 );
Barry Mienydd671972010-10-04 16:33:58 +0200768
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300769 return stripslashes(str_replace($bad, '', $filename));
Derek Allard2067d1a2008-11-13 22:59:24 +0000770 }
771
772 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200773
Derek Allard2067d1a2008-11-13 22:59:24 +0000774 /**
775 * Limit the File Name Length
776 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000777 * @param string
778 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200779 */
Greg Aker58fdee82010-11-10 15:07:09 -0600780 public function limit_filename_length($filename, $length)
Derek Allard2067d1a2008-11-13 22:59:24 +0000781 {
782 if (strlen($filename) < $length)
783 {
784 return $filename;
785 }
Barry Mienydd671972010-10-04 16:33:58 +0200786
Derek Allard2067d1a2008-11-13 22:59:24 +0000787 $ext = '';
788 if (strpos($filename, '.') !== FALSE)
789 {
790 $parts = explode('.', $filename);
791 $ext = '.'.array_pop($parts);
792 $filename = implode('.', $parts);
793 }
Barry Mienydd671972010-10-04 16:33:58 +0200794
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 return substr($filename, 0, ($length - strlen($ext))).$ext;
796 }
797
798 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200799
Derek Allard2067d1a2008-11-13 22:59:24 +0000800 /**
801 * Runs the file through the XSS clean function
802 *
803 * This prevents people from embedding malicious code in their files.
804 * I'm not sure that it won't negatively affect certain files in unexpected ways,
805 * but so far I haven't found that it causes trouble.
806 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000807 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200808 */
Greg Aker58fdee82010-11-10 15:07:09 -0600809 public function do_xss_clean()
Barry Mienydd671972010-10-04 16:33:58 +0200810 {
Derek Jonese9d723f2010-07-12 10:10:59 -0500811 $file = $this->file_temp;
Barry Mienydd671972010-10-04 16:33:58 +0200812
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300813 if (filesize($file) == 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000814 {
815 return FALSE;
816 }
Barry Mienydd671972010-10-04 16:33:58 +0200817
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300818 if (function_exists('memory_get_usage') && memory_get_usage() && ini_get('memory_limit'))
Greg Akerf82e51c2010-04-14 19:33:50 -0500819 {
820 $current = ini_get('memory_limit') * 1024 * 1024;
Barry Mienydd671972010-10-04 16:33:58 +0200821
Greg Akerc78a2592010-06-09 11:45:32 -0500822 // There was a bug/behavioural change in PHP 5.2, where numbers over one million get output
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300823 // into scientific notation. number_format() ensures this number is an integer
Greg Akerc78a2592010-06-09 11:45:32 -0500824 // http://bugs.php.net/bug.php?id=43053
Barry Mienydd671972010-10-04 16:33:58 +0200825
Greg Akerc78a2592010-06-09 11:45:32 -0500826 $new_memory = number_format(ceil(filesize($file) + $current), 0, '.', '');
Barry Mienydd671972010-10-04 16:33:58 +0200827
Greg Akerc78a2592010-06-09 11:45:32 -0500828 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 -0500829 }
830
831 // If the file being uploaded is an image, then we should have no problem with XSS attacks (in theory), but
832 // IE can be fooled into mime-type detecting a malformed image as an html file, thus executing an XSS attack on anyone
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300833 // using IE who looks at the image. It does this by inspecting the first 255 bytes of an image. To get around this
834 // 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 +0200835 // 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 -0500836 // attempted XSS attack.
837
838 if (function_exists('getimagesize') && @getimagesize($file) !== FALSE)
839 {
Barry Mienydd671972010-10-04 16:33:58 +0200840 if (($file = @fopen($file, 'rb')) === FALSE) // "b" to force binary
841 {
Greg Akerf82e51c2010-04-14 19:33:50 -0500842 return FALSE; // Couldn't open the file, return FALSE
Barry Mienydd671972010-10-04 16:33:58 +0200843 }
Greg Akerf82e51c2010-04-14 19:33:50 -0500844
Barry Mienydd671972010-10-04 16:33:58 +0200845 $opening_bytes = fread($file, 256);
846 fclose($file);
Greg Akerf82e51c2010-04-14 19:33:50 -0500847
848 // These are known to throw IE into mime-type detection chaos
849 // <a, <body, <head, <html, <img, <plaintext, <pre, <script, <table, <title
850 // title is basically just in SVG, but we filter it anyhow
851
852 if ( ! preg_match('/<(a|body|head|html|img|plaintext|pre|script|table|title)[\s>]/i', $opening_bytes))
853 {
854 return TRUE; // its an image, no "triggers" detected in the first 256 bytes, we're good
855 }
Wes Bakerbb2c83b2012-05-04 18:44:24 -0400856 else
857 {
858 return FALSE;
859 }
Greg Akerf82e51c2010-04-14 19:33:50 -0500860 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000861
862 if (($data = @file_get_contents($file)) === FALSE)
863 {
864 return FALSE;
865 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000866
Greg Akerf82e51c2010-04-14 19:33:50 -0500867 $CI =& get_instance();
Greg Akerf82e51c2010-04-14 19:33:50 -0500868 return $CI->security->xss_clean($data, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 }
Barry Mienydd671972010-10-04 16:33:58 +0200870
Derek Allard2067d1a2008-11-13 22:59:24 +0000871 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200872
Derek Allard2067d1a2008-11-13 22:59:24 +0000873 /**
874 * Set an error message
875 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000876 * @param string
877 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200878 */
Greg Aker58fdee82010-11-10 15:07:09 -0600879 public function set_error($msg)
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 {
Barry Mienydd671972010-10-04 16:33:58 +0200881 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000882 $CI->lang->load('upload');
Barry Mienydd671972010-10-04 16:33:58 +0200883
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 if (is_array($msg))
885 {
886 foreach ($msg as $val)
887 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100888 $msg = ($CI->lang->line($val) === FALSE) ? $val : $CI->lang->line($val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 $this->error_msg[] = $msg;
890 log_message('error', $msg);
Barry Mienydd671972010-10-04 16:33:58 +0200891 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000892 }
893 else
894 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100895 $msg = ($CI->lang->line($msg) === FALSE) ? $msg : $CI->lang->line($msg);
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 $this->error_msg[] = $msg;
897 log_message('error', $msg);
898 }
899 }
Barry Mienydd671972010-10-04 16:33:58 +0200900
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200902
Derek Allard2067d1a2008-11-13 22:59:24 +0000903 /**
904 * Display the error message
905 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000906 * @param string
907 * @param string
908 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200909 */
Greg Aker58fdee82010-11-10 15:07:09 -0600910 public function display_errors($open = '<p>', $close = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300912 return (count($this->error_msg) > 0) ? $open.implode($close.$open, $this->error_msg).$close : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 }
Barry Mienydd671972010-10-04 16:33:58 +0200914
Derek Allard2067d1a2008-11-13 22:59:24 +0000915 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200916
Derek Allard2067d1a2008-11-13 22:59:24 +0000917 /**
918 * List of Mime Types
919 *
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300920 * This is a list of mime types. We use it to validate
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 * the "allowed types" set by the developer
922 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000923 * @param string
924 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200925 */
Greg Aker58fdee82010-11-10 15:07:09 -0600926 public function mimes_types($mime)
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300928 return isset($this->mimes[$mime]) ? $this->mimes[$mime] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 }
930
931 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200932
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 /**
934 * Prep Filename
935 *
936 * Prevents possible script execution from Apache's handling of files multiple extensions
937 * http://httpd.apache.org/docs/1.3/mod/mod_mime.html#multipleext
938 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 * @param string
940 * @return string
941 */
Greg Aker58fdee82010-11-10 15:07:09 -0600942 protected function _prep_filename($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000943 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100944 if (strpos($filename, '.') === FALSE OR $this->allowed_types === '*')
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 {
946 return $filename;
947 }
Derek Allard616dab82009-02-16 15:44:32 +0000948
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 $parts = explode('.', $filename);
950 $ext = array_pop($parts);
951 $filename = array_shift($parts);
Derek Allard616dab82009-02-16 15:44:32 +0000952
Derek Allard2067d1a2008-11-13 22:59:24 +0000953 foreach ($parts as $part)
954 {
Derek Jonese9d723f2010-07-12 10:10:59 -0500955 if ( ! in_array(strtolower($part), $this->allowed_types) OR $this->mimes_types(strtolower($part)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000956 {
957 $filename .= '.'.$part.'_';
958 }
959 else
960 {
961 $filename .= '.'.$part;
962 }
963 }
Derek Allardd70b0642009-02-16 13:51:42 +0000964
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300965 return $filename.'.'.$ext;
Derek Allard2067d1a2008-11-13 22:59:24 +0000966 }
967
968 // --------------------------------------------------------------------
969
Andrey Andreev3a3c9472011-09-24 14:25:33 +0300970 /**
971 * File MIME type
972 *
973 * Detects the (actual) MIME type of the uploaded file, if possible.
974 * The input array is expected to be $_FILES[$field]
975 *
976 * @param array
977 * @return void
978 */
979 protected function _file_mime_type($file)
980 {
Andrey Andreeva49e3812011-12-09 13:05:22 +0200981 // We'll need this to validate the MIME info string (e.g. text/plain; charset=us-ascii)
Andrey Andreevf7aed122011-12-13 11:01:06 +0200982 $regexp = '/^([a-z\-]+\/[a-z0-9\-\.\+]+)(;\s.+)?$/';
Andrey Andreeva49e3812011-12-09 13:05:22 +0200983
984 /* Fileinfo extension - most reliable method
985 *
986 * Unfortunately, prior to PHP 5.3 - it's only available as a PECL extension and the
987 * more convenient FILEINFO_MIME_TYPE flag doesn't exist.
988 */
989 if (function_exists('finfo_file'))
Andrey Andreev3a3c9472011-09-24 14:25:33 +0300990 {
Andrey Andreeva49e3812011-12-09 13:05:22 +0200991 $finfo = finfo_open(FILEINFO_MIME);
992 if (is_resource($finfo)) // It is possible that a FALSE value is returned, if there is no magic MIME database file found on the system
Andrey Andreev3a3c9472011-09-24 14:25:33 +0300993 {
Andrey Andreeva49e3812011-12-09 13:05:22 +0200994 $mime = @finfo_file($finfo, $file['tmp_name']);
995 finfo_close($finfo);
Andrey Andreev3a3c9472011-09-24 14:25:33 +0300996
997 /* According to the comments section of the PHP manual page,
998 * it is possible that this function returns an empty string
Andrey Andreev2cec39f2011-09-24 22:59:37 +0300999 * for some files (e.g. if they don't exist in the magic MIME database)
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001000 */
Andrey Andreeva49e3812011-12-09 13:05:22 +02001001 if (is_string($mime) && preg_match($regexp, $mime, $matches))
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001002 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001003 $this->file_type = $matches[1];
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001004 return;
1005 }
1006 }
1007 }
1008
Andrey Andreeva49e3812011-12-09 13:05:22 +02001009 /* This is an ugly hack, but UNIX-type systems provide a "native" way to detect the file type,
1010 * which is still more secure than depending on the value of $_FILES[$field]['type'], and as it
1011 * was reported in issue #750 (https://github.com/EllisLab/CodeIgniter/issues/750) - it's better
1012 * than mime_content_type() as well, hence the attempts to try calling the command line with
1013 * three different functions.
Andrey Andreev6700b932011-09-24 14:25:33 +03001014 *
1015 * Notes:
Andrey Andreev59654312011-12-02 14:28:54 +02001016 * - the DIRECTORY_SEPARATOR comparison ensures that we're not on a Windows system
Andrey Andreeva49e3812011-12-09 13:05:22 +02001017 * - many system admins would disable the exec(), shell_exec(), popen() and similar functions
1018 * due to security concerns, hence the function_exists() checks
Andrey Andreev6700b932011-09-24 14:25:33 +03001019 */
Andrey Andreeva49e3812011-12-09 13:05:22 +02001020 if (DIRECTORY_SEPARATOR !== '\\')
Andrey Andreev6700b932011-09-24 14:25:33 +03001021 {
Andrey Andreev56454792012-05-17 14:32:19 +03001022 $cmd = 'file --brief --mime '.escapeshellarg($file['tmp_name']).' 2>&1';
Andrey Andreeva49e3812011-12-09 13:05:22 +02001023
1024 if (function_exists('exec'))
Andrey Andreev6700b932011-09-24 14:25:33 +03001025 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001026 /* This might look confusing, as $mime is being populated with all of the output when set in the second parameter.
1027 * However, we only neeed the last line, which is the actual return value of exec(), and as such - it overwrites
1028 * anything that could already be set for $mime previously. This effectively makes the second parameter a dummy
1029 * value, which is only put to allow us to get the return status code.
1030 */
1031 $mime = @exec($cmd, $mime, $return_status);
1032 if ($return_status === 0 && is_string($mime) && preg_match($regexp, $mime, $matches))
1033 {
1034 $this->file_type = $matches[1];
1035 return;
1036 }
1037 }
1038
1039 if ( (bool) @ini_get('safe_mode') === FALSE && function_exists('shell_exec'))
1040 {
1041 $mime = @shell_exec($cmd);
1042 if (strlen($mime) > 0)
1043 {
1044 $mime = explode("\n", trim($mime));
1045 if (preg_match($regexp, $mime[(count($mime) - 1)], $matches))
1046 {
1047 $this->file_type = $matches[1];
1048 return;
1049 }
1050 }
1051 }
1052
1053 if (function_exists('popen'))
1054 {
1055 $proc = @popen($cmd, 'r');
1056 if (is_resource($proc))
1057 {
tubalmartin010f1f42012-03-03 22:24:31 +01001058 $mime = @fread($proc, 512);
Andrey Andreeva49e3812011-12-09 13:05:22 +02001059 @pclose($proc);
1060 if ($mime !== FALSE)
1061 {
1062 $mime = explode("\n", trim($mime));
1063 if (preg_match($regexp, $mime[(count($mime) - 1)], $matches))
1064 {
1065 $this->file_type = $matches[1];
1066 return;
1067 }
1068 }
1069 }
1070 }
1071 }
1072
1073 // Fall back to the deprecated mime_content_type(), if available (still better than $_FILES[$field]['type'])
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001074 if (function_exists('mime_content_type'))
1075 {
1076 $this->file_type = @mime_content_type($file['tmp_name']);
Andrey Andreeva49e3812011-12-09 13:05:22 +02001077 if (strlen($this->file_type) > 0) // It's possible that mime_content_type() returns FALSE or an empty string
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001078 {
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001079 return;
1080 }
1081 }
1082
1083 $this->file_type = $file['type'];
1084 }
1085
Derek Allard2067d1a2008-11-13 22:59:24 +00001086}
Derek Allard2067d1a2008-11-13 22:59:24 +00001087
1088/* End of file Upload.php */
Andrey Andreev56454792012-05-17 14:32:19 +03001089/* Location: ./system/libraries/Upload.php */