blob: e4cc54b2192cc72520fe28690c2e189c96864d29 [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 Andreev8e6f7a92012-03-26 20:13:00 +030081 log_message('debug', 'Upload Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +000082 }
Barry Mienydd671972010-10-04 16:33:58 +020083
Derek Allard2067d1a2008-11-13 22:59:24 +000084 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020085
Derek Allard2067d1a2008-11-13 22:59:24 +000086 /**
87 * Initialize preferences
88 *
Derek Allard2067d1a2008-11-13 22:59:24 +000089 * @param array
90 * @return void
Barry Mienydd671972010-10-04 16:33:58 +020091 */
Greg Aker58fdee82010-11-10 15:07:09 -060092 public function initialize($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +000093 {
94 $defaults = array(
Andrey Andreev8e6f7a92012-03-26 20:13:00 +030095 'max_size' => 0,
96 'max_width' => 0,
97 'max_height' => 0,
98 'max_filename' => 0,
99 'max_filename_increment' => 100,
100 'allowed_types' => '',
101 'file_temp' => '',
102 'file_name' => '',
103 'orig_name' => '',
104 'file_type' => '',
105 'file_size' => '',
106 'file_ext' => '',
107 'upload_path' => '',
108 'overwrite' => FALSE,
109 'encrypt_name' => FALSE,
110 'is_image' => FALSE,
111 'image_width' => '',
112 'image_height' => '',
113 'image_type' => '',
114 'image_size_str' => '',
115 'error_msg' => array(),
116 'mimes' => array(),
117 'remove_spaces' => TRUE,
118 'xss_clean' => FALSE,
119 'temp_prefix' => 'temp_file_',
120 'client_name' => ''
121 );
Barry Mienydd671972010-10-04 16:33:58 +0200122
123
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 foreach ($defaults as $key => $val)
125 {
126 if (isset($config[$key]))
127 {
128 $method = 'set_'.$key;
129 if (method_exists($this, $method))
130 {
131 $this->$method($config[$key]);
132 }
133 else
134 {
135 $this->$key = $config[$key];
Barry Mienydd671972010-10-04 16:33:58 +0200136 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 }
138 else
139 {
140 $this->$key = $val;
141 }
142 }
Barry Mienydd671972010-10-04 16:33:58 +0200143
Derek Jonese9d723f2010-07-12 10:10:59 -0500144 // if a file_name was provided in the config, use it instead of the user input
145 // supplied file name for all uploads until initialized again
146 $this->_file_name_override = $this->file_name;
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 }
Barry Mienydd671972010-10-04 16:33:58 +0200148
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200150
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 /**
152 * Perform the file upload
153 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200155 */
Greg Aker58fdee82010-11-10 15:07:09 -0600156 public function do_upload($field = 'userfile')
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300158 // Is $_FILES[$field] set? If not, no reason to continue.
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 if ( ! isset($_FILES[$field]))
160 {
161 $this->set_error('upload_no_file_selected');
162 return FALSE;
163 }
Barry Mienydd671972010-10-04 16:33:58 +0200164
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 // Is the upload path valid?
166 if ( ! $this->validate_upload_path())
167 {
168 // errors will already be set by validate_upload_path() so just return FALSE
169 return FALSE;
170 }
171
172 // Was the file able to be uploaded? If not, determine the reason why.
173 if ( ! is_uploaded_file($_FILES[$field]['tmp_name']))
174 {
175 $error = ( ! isset($_FILES[$field]['error'])) ? 4 : $_FILES[$field]['error'];
176
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300177 switch ($error)
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 {
179 case 1: // UPLOAD_ERR_INI_SIZE
180 $this->set_error('upload_file_exceeds_limit');
181 break;
182 case 2: // UPLOAD_ERR_FORM_SIZE
183 $this->set_error('upload_file_exceeds_form_limit');
184 break;
185 case 3: // UPLOAD_ERR_PARTIAL
Barry Mienydd671972010-10-04 16:33:58 +0200186 $this->set_error('upload_file_partial');
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 break;
188 case 4: // UPLOAD_ERR_NO_FILE
Barry Mienydd671972010-10-04 16:33:58 +0200189 $this->set_error('upload_no_file_selected');
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 break;
191 case 6: // UPLOAD_ERR_NO_TMP_DIR
192 $this->set_error('upload_no_temp_directory');
193 break;
194 case 7: // UPLOAD_ERR_CANT_WRITE
195 $this->set_error('upload_unable_to_write_file');
196 break;
197 case 8: // UPLOAD_ERR_EXTENSION
198 $this->set_error('upload_stopped_by_extension');
199 break;
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300200 default:
201 $this->set_error('upload_no_file_selected');
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 break;
203 }
204
205 return FALSE;
206 }
207
208 // Set the uploaded data as class variables
Barry Mienydd671972010-10-04 16:33:58 +0200209 $this->file_temp = $_FILES[$field]['tmp_name'];
210 $this->file_size = $_FILES[$field]['size'];
Andrey Andreev3a3c9472011-09-24 14:25:33 +0300211 $this->_file_mime_type($_FILES[$field]);
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300212 $this->file_type = preg_replace('/^(.+?);.*$/', '\\1', $this->file_type);
Derek Jones616fb022010-04-22 16:52:18 -0500213 $this->file_type = strtolower(trim(stripslashes($this->file_type), '"'));
Derek Jonese9d723f2010-07-12 10:10:59 -0500214 $this->file_name = $this->_prep_filename($_FILES[$field]['name']);
215 $this->file_ext = $this->get_extension($this->file_name);
216 $this->client_name = $this->file_name;
Barry Mienydd671972010-10-04 16:33:58 +0200217
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 // Is the file type allowed to be uploaded?
219 if ( ! $this->is_allowed_filetype())
220 {
221 $this->set_error('upload_invalid_filetype');
222 return FALSE;
223 }
224
Derek Jonese9d723f2010-07-12 10:10:59 -0500225 // if we're overriding, let's now make sure the new name and type is allowed
Alex Bilbied261b1e2012-06-02 11:12:16 +0100226 if ($this->_file_name_override !== '')
Derek Jonese9d723f2010-07-12 10:10:59 -0500227 {
228 $this->file_name = $this->_prep_filename($this->_file_name_override);
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000229
230 // If no extension was provided in the file_name config item, use the uploaded one
Pascal Kriete14287f32011-02-14 13:39:34 -0500231 if (strpos($this->_file_name_override, '.') === FALSE)
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000232 {
233 $this->file_name .= $this->file_ext;
234 }
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000235 else
236 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300237 // An extension was provided, lets have it!
238 $this->file_ext = $this->get_extension($this->_file_name_override);
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000239 }
Derek Jonese9d723f2010-07-12 10:10:59 -0500240
241 if ( ! $this->is_allowed_filetype(TRUE))
242 {
243 $this->set_error('upload_invalid_filetype');
Barry Mienydd671972010-10-04 16:33:58 +0200244 return FALSE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500245 }
246 }
Barry Mienydd671972010-10-04 16:33:58 +0200247
Derek Jonese9d723f2010-07-12 10:10:59 -0500248 // Convert the file size to kilobytes
249 if ($this->file_size > 0)
250 {
251 $this->file_size = round($this->file_size/1024, 2);
252 }
253
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 // Is the file size within the allowed maximum?
255 if ( ! $this->is_allowed_filesize())
256 {
257 $this->set_error('upload_invalid_filesize');
258 return FALSE;
259 }
260
261 // Are the image dimensions within the allowed size?
262 // Note: This can fail if the server has an open_basdir restriction.
263 if ( ! $this->is_allowed_dimensions())
264 {
265 $this->set_error('upload_invalid_dimensions');
266 return FALSE;
267 }
268
269 // Sanitize the file name for security
270 $this->file_name = $this->clean_file_name($this->file_name);
Barry Mienydd671972010-10-04 16:33:58 +0200271
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 // Truncate the file name if it's too long
273 if ($this->max_filename > 0)
274 {
275 $this->file_name = $this->limit_filename_length($this->file_name, $this->max_filename);
276 }
277
278 // Remove white spaces in the name
Alex Bilbied261b1e2012-06-02 11:12:16 +0100279 if ($this->remove_spaces === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300281 $this->file_name = preg_replace('/\s+/', '_', $this->file_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 }
283
284 /*
285 * Validate the file name
286 * This function appends an number onto the end of
287 * the file if one with the same name already exists.
288 * If it returns false there was a problem.
289 */
290 $this->orig_name = $this->file_name;
291
Alex Bilbied261b1e2012-06-02 11:12:16 +0100292 if ($this->overwrite === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 {
294 $this->file_name = $this->set_filename($this->upload_path, $this->file_name);
Barry Mienydd671972010-10-04 16:33:58 +0200295
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 if ($this->file_name === FALSE)
297 {
298 return FALSE;
299 }
300 }
301
302 /*
Derek Jonese9d723f2010-07-12 10:10:59 -0500303 * Run the file through the XSS hacking filter
304 * This helps prevent malicious code from being
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300305 * embedded within a file. Scripts can easily
Derek Jonese9d723f2010-07-12 10:10:59 -0500306 * be disguised as images or other file types.
307 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300308 if ($this->xss_clean && $this->do_xss_clean() === FALSE)
Derek Jonese9d723f2010-07-12 10:10:59 -0500309 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300310 $this->set_error('upload_unable_to_write_file');
311 return FALSE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500312 }
313
314 /*
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 * Move the file to the final destination
316 * To deal with different server configurations
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300317 * we'll attempt to use copy() first. If that fails
318 * we'll use move_uploaded_file(). One of the two should
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 * reliably work in most environments
320 */
321 if ( ! @copy($this->file_temp, $this->upload_path.$this->file_name))
322 {
323 if ( ! @move_uploaded_file($this->file_temp, $this->upload_path.$this->file_name))
324 {
Barry Mienydd671972010-10-04 16:33:58 +0200325 $this->set_error('upload_destination_error');
326 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 }
328 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000329
330 /*
331 * Set the finalized image dimensions
332 * This sets the image width/height (assuming the
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300333 * file was an image). We use this information
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 * in the "data" function.
335 */
336 $this->set_image_properties($this->upload_path.$this->file_name);
337
338 return TRUE;
339 }
Barry Mienydd671972010-10-04 16:33:58 +0200340
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200342
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 /**
344 * Finalized Data Array
Barry Mienydd671972010-10-04 16:33:58 +0200345 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 * Returns an associative array containing all of the information
347 * related to the upload, allowing the developer easy access in one array.
348 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200350 */
Greg Aker58fdee82010-11-10 15:07:09 -0600351 public function data()
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300353 return array(
354 'file_name' => $this->file_name,
355 'file_type' => $this->file_type,
356 'file_path' => $this->upload_path,
357 'full_path' => $this->upload_path.$this->file_name,
358 'raw_name' => str_replace($this->file_ext, '', $this->file_name),
359 'orig_name' => $this->orig_name,
360 'client_name' => $this->client_name,
361 'file_ext' => $this->file_ext,
362 'file_size' => $this->file_size,
363 'is_image' => $this->is_image(),
364 'image_width' => $this->image_width,
365 'image_height' => $this->image_height,
366 'image_type' => $this->image_type,
367 'image_size_str' => $this->image_size_str,
368 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 }
Barry Mienydd671972010-10-04 16:33:58 +0200370
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200372
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 /**
374 * Set Upload Path
375 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 * @param string
377 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200378 */
Greg Aker58fdee82010-11-10 15:07:09 -0600379 public function set_upload_path($path)
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 {
381 // Make sure it has a trailing slash
382 $this->upload_path = rtrim($path, '/').'/';
383 }
Barry Mienydd671972010-10-04 16:33:58 +0200384
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200386
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 /**
388 * Set the file name
389 *
390 * This function takes a filename/path as input and looks for the
391 * existence of a file with the same name. If found, it will append a
392 * number to the end of the filename to avoid overwriting a pre-existing file.
393 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 * @param string
395 * @param string
396 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200397 */
Greg Aker58fdee82010-11-10 15:07:09 -0600398 public function set_filename($path, $filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100400 if ($this->encrypt_name === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200401 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 mt_srand();
Barry Mienydd671972010-10-04 16:33:58 +0200403 $filename = md5(uniqid(mt_rand())).$this->file_ext;
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 }
Barry Mienydd671972010-10-04 16:33:58 +0200405
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 if ( ! file_exists($path.$filename))
407 {
408 return $filename;
409 }
Barry Mienydd671972010-10-04 16:33:58 +0200410
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 $filename = str_replace($this->file_ext, '', $filename);
Barry Mienydd671972010-10-04 16:33:58 +0200412
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 $new_filename = '';
Adam Jackettccbbea12011-08-21 16:19:11 -0400414 for ($i = 1; $i < $this->max_filename_increment; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200415 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 if ( ! file_exists($path.$filename.$i.$this->file_ext))
417 {
418 $new_filename = $filename.$i.$this->file_ext;
419 break;
420 }
421 }
422
Alex Bilbied261b1e2012-06-02 11:12:16 +0100423 if ($new_filename === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 {
425 $this->set_error('upload_bad_filename');
426 return FALSE;
427 }
428 else
429 {
430 return $new_filename;
431 }
432 }
Barry Mienydd671972010-10-04 16:33:58 +0200433
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200435
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 /**
437 * Set Maximum File Size
438 *
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300439 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200441 */
Greg Aker58fdee82010-11-10 15:07:09 -0600442 public function set_max_filesize($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300444 $this->max_size = ((int) $n < 0) ? 0 : (int) $n;
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 }
Barry Mienydd671972010-10-04 16:33:58 +0200446
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200448
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 /**
450 * Set Maximum File Name Length
451 *
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300452 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200454 */
Greg Aker58fdee82010-11-10 15:07:09 -0600455 public function set_max_filename($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300457 $this->max_filename = ((int) $n < 0) ? 0 : (int) $n;
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 }
459
460 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200461
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 /**
463 * Set Maximum Image Width
464 *
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300465 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200467 */
Greg Aker58fdee82010-11-10 15:07:09 -0600468 public function set_max_width($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300470 $this->max_width = ((int) $n < 0) ? 0 : (int) $n;
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 }
Barry Mienydd671972010-10-04 16:33:58 +0200472
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200474
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 /**
476 * Set Maximum Image Height
477 *
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300478 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200480 */
Greg Aker58fdee82010-11-10 15:07:09 -0600481 public function set_max_height($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300483 $this->max_height = ((int) $n < 0) ? 0 : (int) $n;
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 }
Barry Mienydd671972010-10-04 16:33:58 +0200485
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200487
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 /**
489 * Set Allowed File Types
490 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 * @param string
492 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200493 */
Greg Aker58fdee82010-11-10 15:07:09 -0600494 public function set_allowed_types($types)
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300496 if ( ! is_array($types) && $types === '*')
Derek Jonese12f64e2010-03-02 22:55:08 -0600497 {
498 $this->allowed_types = '*';
499 return;
500 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 $this->allowed_types = explode('|', $types);
502 }
Barry Mienydd671972010-10-04 16:33:58 +0200503
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200505
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 /**
507 * Set Image Properties
508 *
509 * Uses GD to determine the width/height/type of image
510 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 * @param string
512 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200513 */
Greg Aker58fdee82010-11-10 15:07:09 -0600514 public function set_image_properties($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 {
516 if ( ! $this->is_image())
517 {
518 return;
519 }
520
521 if (function_exists('getimagesize'))
522 {
523 if (FALSE !== ($D = @getimagesize($path)))
Barry Mienydd671972010-10-04 16:33:58 +0200524 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
526
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300527 $this->image_width = $D[0];
528 $this->image_height = $D[1];
529 $this->image_type = isset($types[$D[2]]) ? $types[$D[2]] : 'unknown';
530 $this->image_size_str = $D[3]; // string containing height and width
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 }
532 }
533 }
Barry Mienydd671972010-10-04 16:33:58 +0200534
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200536
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 /**
538 * Set XSS Clean
539 *
540 * Enables the XSS flag so that the file that was uploaded
541 * will be run through the XSS filter.
542 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 * @param bool
544 * @return void
545 */
Greg Aker58fdee82010-11-10 15:07:09 -0600546 public function set_xss_clean($flag = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100548 $this->xss_clean = ($flag === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 }
Barry Mienydd671972010-10-04 16:33:58 +0200550
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200552
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 /**
554 * Validate the image
555 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200557 */
Greg Aker58fdee82010-11-10 15:07:09 -0600558 public function is_image()
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 {
560 // IE will sometimes return odd mime-types during upload, so here we just standardize all
561 // jpegs or pngs to the same file type.
562
Derek Jones4b9c6292011-07-01 17:40:48 -0500563 $png_mimes = array('image/x-png');
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 $jpeg_mimes = array('image/jpg', 'image/jpe', 'image/jpeg', 'image/pjpeg');
Barry Mienydd671972010-10-04 16:33:58 +0200565
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 if (in_array($this->file_type, $png_mimes))
567 {
568 $this->file_type = 'image/png';
569 }
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300570 elseif (in_array($this->file_type, $jpeg_mimes))
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 {
572 $this->file_type = 'image/jpeg';
573 }
574
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300575 $img_mimes = array('image/gif', 'image/jpeg', 'image/png');
Derek Allard2067d1a2008-11-13 22:59:24 +0000576
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300577 return in_array($this->file_type, $img_mimes, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 }
Barry Mienydd671972010-10-04 16:33:58 +0200579
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200581
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 /**
583 * Verify that the filetype is allowed
584 *
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200585 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200587 */
Greg Aker58fdee82010-11-10 15:07:09 -0600588 public function is_allowed_filetype($ignore_mime = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 {
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200590 if ($this->allowed_types === '*')
Derek Jonese12f64e2010-03-02 22:55:08 -0600591 {
592 return TRUE;
593 }
Barry Mienydd671972010-10-04 16:33:58 +0200594
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200595 if ( ! is_array($this->allowed_types) OR count($this->allowed_types) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 {
597 $this->set_error('upload_no_file_types');
598 return FALSE;
599 }
Barry Mienydd671972010-10-04 16:33:58 +0200600
Derek Jonese9d723f2010-07-12 10:10:59 -0500601 $ext = strtolower(ltrim($this->file_ext, '.'));
Barry Mienydd671972010-10-04 16:33:58 +0200602
Derek Jonese9d723f2010-07-12 10:10:59 -0500603 if ( ! in_array($ext, $this->allowed_types))
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 {
Derek Jonese9d723f2010-07-12 10:10:59 -0500605 return FALSE;
606 }
Derek Jonesafa282f2009-02-10 17:11:52 +0000607
Barry Mienydd671972010-10-04 16:33:58 +0200608 // Images get some additional checks
Derek Jonese9d723f2010-07-12 10:10:59 -0500609 $image_types = array('gif', 'jpg', 'jpeg', 'png', 'jpe');
Barry Mienydd671972010-10-04 16:33:58 +0200610
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200611 if (in_array($ext, $image_types) && @getimagesize($this->file_temp) === FALSE)
Derek Jonese9d723f2010-07-12 10:10:59 -0500612 {
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200613 return FALSE;
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
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300623 if (is_array($mime) && in_array($this->file_type, $mime, TRUE))
Derek Jonese9d723f2010-07-12 10:10:59 -0500624 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300625 return TRUE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500626 }
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200627 elseif ($mime === $this->file_type)
Derek Jonese9d723f2010-07-12 10:10:59 -0500628 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300629 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 }
Barry Mienydd671972010-10-04 16:33:58 +0200631
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 return FALSE;
633 }
Barry Mienydd671972010-10-04 16:33:58 +0200634
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200636
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 /**
638 * Verify that the file is within the allowed size
639 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200641 */
Greg Aker58fdee82010-11-10 15:07:09 -0600642 public function is_allowed_filesize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000643 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100644 return ($this->max_size === 0 OR $this->max_size > $this->file_size);
Derek Allard2067d1a2008-11-13 22:59:24 +0000645 }
Barry Mienydd671972010-10-04 16:33:58 +0200646
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200648
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 /**
650 * Verify that the image is within the allowed width/height
651 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200653 */
Greg Aker58fdee82010-11-10 15:07:09 -0600654 public function is_allowed_dimensions()
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 {
656 if ( ! $this->is_image())
657 {
658 return TRUE;
659 }
660
661 if (function_exists('getimagesize'))
662 {
663 $D = @getimagesize($this->file_temp);
664
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300665 if ($this->max_width > 0 && $D[0] > $this->max_width)
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 {
667 return FALSE;
668 }
669
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300670 if ($this->max_height > 0 && $D[1] > $this->max_height)
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 {
672 return FALSE;
673 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 }
675
676 return TRUE;
677 }
Barry Mienydd671972010-10-04 16:33:58 +0200678
Derek Allard2067d1a2008-11-13 22:59:24 +0000679 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200680
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 /**
682 * Validate Upload Path
683 *
684 * Verifies that it is a valid upload path with proper permissions.
685 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200687 */
Greg Aker58fdee82010-11-10 15:07:09 -0600688 public function validate_upload_path()
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100690 if ($this->upload_path === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000691 {
692 $this->set_error('upload_no_filepath');
693 return FALSE;
694 }
Barry Mienydd671972010-10-04 16:33:58 +0200695
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300696 if (function_exists('realpath') && @realpath($this->upload_path) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300698 $this->upload_path = str_replace('\\', '/', realpath($this->upload_path));
Derek Allard2067d1a2008-11-13 22:59:24 +0000699 }
700
701 if ( ! @is_dir($this->upload_path))
702 {
703 $this->set_error('upload_no_filepath');
704 return FALSE;
705 }
706
707 if ( ! is_really_writable($this->upload_path))
708 {
709 $this->set_error('upload_not_writable');
710 return FALSE;
711 }
712
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300713 $this->upload_path = preg_replace('/(.+?)\/*$/', '\\1/', $this->upload_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000714 return TRUE;
715 }
Barry Mienydd671972010-10-04 16:33:58 +0200716
Derek Allard2067d1a2008-11-13 22:59:24 +0000717 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200718
Derek Allard2067d1a2008-11-13 22:59:24 +0000719 /**
720 * Extract the file extension
721 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000722 * @param string
723 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200724 */
Greg Aker58fdee82010-11-10 15:07:09 -0600725 public function get_extension($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000726 {
727 $x = explode('.', $filename);
Andrey Andreev46d53fb2012-05-11 13:42:24 +0300728 return (count($x) !== 1) ? '.'.end($x) : '';
Barry Mienydd671972010-10-04 16:33:58 +0200729 }
730
Derek Allard2067d1a2008-11-13 22:59:24 +0000731 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200732
Derek Allard2067d1a2008-11-13 22:59:24 +0000733 /**
734 * Clean the file name for security
735 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 * @param string
737 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200738 */
Greg Aker58fdee82010-11-10 15:07:09 -0600739 public function clean_file_name($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000740 {
741 $bad = array(
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300742 '<!--', '-->',
743 "'", '"',
744 '<', '>',
745 '&', '$',
746 '=',
747 ';',
748 '?',
749 '/',
Andrey Andreev470805b2012-05-24 21:57:21 +0300750 '!',
751 '#',
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300752 '%20',
753 '%22',
754 '%3c', // <
755 '%253c', // <
756 '%3e', // >
757 '%0e', // >
758 '%28', // (
759 '%29', // )
760 '%2528', // (
761 '%26', // &
762 '%24', // $
763 '%3f', // ?
764 '%3b', // ;
765 '%3d' // =
766 );
Barry Mienydd671972010-10-04 16:33:58 +0200767
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300768 return stripslashes(str_replace($bad, '', $filename));
Derek Allard2067d1a2008-11-13 22:59:24 +0000769 }
770
771 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200772
Derek Allard2067d1a2008-11-13 22:59:24 +0000773 /**
774 * Limit the File Name Length
775 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000776 * @param string
777 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200778 */
Greg Aker58fdee82010-11-10 15:07:09 -0600779 public function limit_filename_length($filename, $length)
Derek Allard2067d1a2008-11-13 22:59:24 +0000780 {
781 if (strlen($filename) < $length)
782 {
783 return $filename;
784 }
Barry Mienydd671972010-10-04 16:33:58 +0200785
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 $ext = '';
787 if (strpos($filename, '.') !== FALSE)
788 {
789 $parts = explode('.', $filename);
790 $ext = '.'.array_pop($parts);
791 $filename = implode('.', $parts);
792 }
Barry Mienydd671972010-10-04 16:33:58 +0200793
Derek Allard2067d1a2008-11-13 22:59:24 +0000794 return substr($filename, 0, ($length - strlen($ext))).$ext;
795 }
796
797 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200798
Derek Allard2067d1a2008-11-13 22:59:24 +0000799 /**
800 * Runs the file through the XSS clean function
801 *
802 * This prevents people from embedding malicious code in their files.
803 * I'm not sure that it won't negatively affect certain files in unexpected ways,
804 * but so far I haven't found that it causes trouble.
805 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000806 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200807 */
Greg Aker58fdee82010-11-10 15:07:09 -0600808 public function do_xss_clean()
Barry Mienydd671972010-10-04 16:33:58 +0200809 {
Derek Jonese9d723f2010-07-12 10:10:59 -0500810 $file = $this->file_temp;
Barry Mienydd671972010-10-04 16:33:58 +0200811
Alex Bilbied261b1e2012-06-02 11:12:16 +0100812 if (filesize($file) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 {
814 return FALSE;
815 }
Barry Mienydd671972010-10-04 16:33:58 +0200816
Alex Bilbied261b1e2012-06-02 11:12:16 +0100817 if (function_exists('memory_get_usage') && memory_get_usage() && ini_get('memory_limit') !== '')
Greg Akerf82e51c2010-04-14 19:33:50 -0500818 {
819 $current = ini_get('memory_limit') * 1024 * 1024;
Barry Mienydd671972010-10-04 16:33:58 +0200820
Greg Akerc78a2592010-06-09 11:45:32 -0500821 // There was a bug/behavioural change in PHP 5.2, where numbers over one million get output
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300822 // into scientific notation. number_format() ensures this number is an integer
Greg Akerc78a2592010-06-09 11:45:32 -0500823 // http://bugs.php.net/bug.php?id=43053
Barry Mienydd671972010-10-04 16:33:58 +0200824
Greg Akerc78a2592010-06-09 11:45:32 -0500825 $new_memory = number_format(ceil(filesize($file) + $current), 0, '.', '');
Barry Mienydd671972010-10-04 16:33:58 +0200826
Greg Akerc78a2592010-06-09 11:45:32 -0500827 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 -0500828 }
829
830 // If the file being uploaded is an image, then we should have no problem with XSS attacks (in theory), but
831 // 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 +0300832 // using IE who looks at the image. It does this by inspecting the first 255 bytes of an image. To get around this
833 // 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 +0200834 // 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 -0500835 // attempted XSS attack.
836
837 if (function_exists('getimagesize') && @getimagesize($file) !== FALSE)
838 {
Barry Mienydd671972010-10-04 16:33:58 +0200839 if (($file = @fopen($file, 'rb')) === FALSE) // "b" to force binary
840 {
Greg Akerf82e51c2010-04-14 19:33:50 -0500841 return FALSE; // Couldn't open the file, return FALSE
Barry Mienydd671972010-10-04 16:33:58 +0200842 }
Greg Akerf82e51c2010-04-14 19:33:50 -0500843
Barry Mienydd671972010-10-04 16:33:58 +0200844 $opening_bytes = fread($file, 256);
845 fclose($file);
Greg Akerf82e51c2010-04-14 19:33:50 -0500846
847 // These are known to throw IE into mime-type detection chaos
848 // <a, <body, <head, <html, <img, <plaintext, <pre, <script, <table, <title
849 // title is basically just in SVG, but we filter it anyhow
850
851 if ( ! preg_match('/<(a|body|head|html|img|plaintext|pre|script|table|title)[\s>]/i', $opening_bytes))
852 {
853 return TRUE; // its an image, no "triggers" detected in the first 256 bytes, we're good
854 }
Wes Bakerbb2c83b2012-05-04 18:44:24 -0400855 else
856 {
857 return FALSE;
858 }
Greg Akerf82e51c2010-04-14 19:33:50 -0500859 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000860
861 if (($data = @file_get_contents($file)) === FALSE)
862 {
863 return FALSE;
864 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000865
Greg Akerf82e51c2010-04-14 19:33:50 -0500866 $CI =& get_instance();
Greg Akerf82e51c2010-04-14 19:33:50 -0500867 return $CI->security->xss_clean($data, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000868 }
Barry Mienydd671972010-10-04 16:33:58 +0200869
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200871
Derek Allard2067d1a2008-11-13 22:59:24 +0000872 /**
873 * Set an error message
874 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000875 * @param string
876 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200877 */
Greg Aker58fdee82010-11-10 15:07:09 -0600878 public function set_error($msg)
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 {
Barry Mienydd671972010-10-04 16:33:58 +0200880 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 $CI->lang->load('upload');
Barry Mienydd671972010-10-04 16:33:58 +0200882
Derek Allard2067d1a2008-11-13 22:59:24 +0000883 if (is_array($msg))
884 {
885 foreach ($msg as $val)
886 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100887 $msg = ($CI->lang->line($val) === FALSE) ? $val : $CI->lang->line($val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 $this->error_msg[] = $msg;
889 log_message('error', $msg);
Barry Mienydd671972010-10-04 16:33:58 +0200890 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 }
892 else
893 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100894 $msg = ($CI->lang->line($msg) === FALSE) ? $msg : $CI->lang->line($msg);
Derek Allard2067d1a2008-11-13 22:59:24 +0000895 $this->error_msg[] = $msg;
896 log_message('error', $msg);
897 }
898 }
Barry Mienydd671972010-10-04 16:33:58 +0200899
Derek Allard2067d1a2008-11-13 22:59:24 +0000900 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200901
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 /**
903 * Display the error message
904 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000905 * @param string
906 * @param string
907 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200908 */
Greg Aker58fdee82010-11-10 15:07:09 -0600909 public function display_errors($open = '<p>', $close = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +0000910 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300911 return (count($this->error_msg) > 0) ? $open.implode($close.$open, $this->error_msg).$close : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 }
Barry Mienydd671972010-10-04 16:33:58 +0200913
Derek Allard2067d1a2008-11-13 22:59:24 +0000914 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200915
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 /**
917 * List of Mime Types
918 *
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300919 * This is a list of mime types. We use it to validate
Derek Allard2067d1a2008-11-13 22:59:24 +0000920 * the "allowed types" set by the developer
921 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 * @param string
923 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200924 */
Greg Aker58fdee82010-11-10 15:07:09 -0600925 public function mimes_types($mime)
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 {
927 global $mimes;
Barry Mienydd671972010-10-04 16:33:58 +0200928
Alex Bilbied261b1e2012-06-02 11:12:16 +0100929 if (count($this->mimes) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000930 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300931 if (defined('ENVIRONMENT') && is_file(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'))
Greg Akerd96f8822011-12-27 16:23:47 -0600932 {
933 include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php');
934 }
935 elseif (is_file(APPPATH.'config/mimes.php'))
936 {
Michiel Vugteveenb5783262012-02-29 15:40:28 +0100937 include(APPPATH.'config/mimes.php');
Greg Akerd96f8822011-12-27 16:23:47 -0600938 }
939 else
bubbafoley0ea04142011-03-17 14:55:41 -0500940 {
Eric Barnesfdd5b112011-03-21 21:28:58 -0400941 return FALSE;
bubbafoley0ea04142011-03-17 14:55:41 -0500942 }
Eric Barnes92808342011-03-18 09:02:37 -0400943
Eric Barnesfdd5b112011-03-21 21:28:58 -0400944 $this->mimes = $mimes;
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 }
Barry Mienydd671972010-10-04 16:33:58 +0200946
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300947 return isset($this->mimes[$mime]) ? $this->mimes[$mime] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 }
949
950 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200951
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 /**
953 * Prep Filename
954 *
955 * Prevents possible script execution from Apache's handling of files multiple extensions
956 * http://httpd.apache.org/docs/1.3/mod/mod_mime.html#multipleext
957 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 * @param string
959 * @return string
960 */
Greg Aker58fdee82010-11-10 15:07:09 -0600961 protected function _prep_filename($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000962 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100963 if (strpos($filename, '.') === FALSE OR $this->allowed_types === '*')
Derek Allard2067d1a2008-11-13 22:59:24 +0000964 {
965 return $filename;
966 }
Derek Allard616dab82009-02-16 15:44:32 +0000967
Derek Allard2067d1a2008-11-13 22:59:24 +0000968 $parts = explode('.', $filename);
969 $ext = array_pop($parts);
970 $filename = array_shift($parts);
Derek Allard616dab82009-02-16 15:44:32 +0000971
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 foreach ($parts as $part)
973 {
Derek Jonese9d723f2010-07-12 10:10:59 -0500974 if ( ! in_array(strtolower($part), $this->allowed_types) OR $this->mimes_types(strtolower($part)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000975 {
976 $filename .= '.'.$part.'_';
977 }
978 else
979 {
980 $filename .= '.'.$part;
981 }
982 }
Derek Allardd70b0642009-02-16 13:51:42 +0000983
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300984 return $filename.'.'.$ext;
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 }
986
987 // --------------------------------------------------------------------
988
Andrey Andreev3a3c9472011-09-24 14:25:33 +0300989 /**
990 * File MIME type
991 *
992 * Detects the (actual) MIME type of the uploaded file, if possible.
993 * The input array is expected to be $_FILES[$field]
994 *
995 * @param array
996 * @return void
997 */
998 protected function _file_mime_type($file)
999 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001000 // We'll need this to validate the MIME info string (e.g. text/plain; charset=us-ascii)
Andrey Andreevf7aed122011-12-13 11:01:06 +02001001 $regexp = '/^([a-z\-]+\/[a-z0-9\-\.\+]+)(;\s.+)?$/';
Andrey Andreeva49e3812011-12-09 13:05:22 +02001002
1003 /* Fileinfo extension - most reliable method
1004 *
1005 * Unfortunately, prior to PHP 5.3 - it's only available as a PECL extension and the
1006 * more convenient FILEINFO_MIME_TYPE flag doesn't exist.
1007 */
1008 if (function_exists('finfo_file'))
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001009 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001010 $finfo = finfo_open(FILEINFO_MIME);
1011 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 +03001012 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001013 $mime = @finfo_file($finfo, $file['tmp_name']);
1014 finfo_close($finfo);
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001015
1016 /* According to the comments section of the PHP manual page,
1017 * it is possible that this function returns an empty string
Andrey Andreev2cec39f2011-09-24 22:59:37 +03001018 * for some files (e.g. if they don't exist in the magic MIME database)
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001019 */
Andrey Andreeva49e3812011-12-09 13:05:22 +02001020 if (is_string($mime) && preg_match($regexp, $mime, $matches))
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001021 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001022 $this->file_type = $matches[1];
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001023 return;
1024 }
1025 }
1026 }
1027
Andrey Andreeva49e3812011-12-09 13:05:22 +02001028 /* This is an ugly hack, but UNIX-type systems provide a "native" way to detect the file type,
1029 * which is still more secure than depending on the value of $_FILES[$field]['type'], and as it
1030 * was reported in issue #750 (https://github.com/EllisLab/CodeIgniter/issues/750) - it's better
1031 * than mime_content_type() as well, hence the attempts to try calling the command line with
1032 * three different functions.
Andrey Andreev6700b932011-09-24 14:25:33 +03001033 *
1034 * Notes:
Andrey Andreev59654312011-12-02 14:28:54 +02001035 * - the DIRECTORY_SEPARATOR comparison ensures that we're not on a Windows system
Andrey Andreeva49e3812011-12-09 13:05:22 +02001036 * - many system admins would disable the exec(), shell_exec(), popen() and similar functions
1037 * due to security concerns, hence the function_exists() checks
Andrey Andreev6700b932011-09-24 14:25:33 +03001038 */
Andrey Andreeva49e3812011-12-09 13:05:22 +02001039 if (DIRECTORY_SEPARATOR !== '\\')
Andrey Andreev6700b932011-09-24 14:25:33 +03001040 {
Andrey Andreev56454792012-05-17 14:32:19 +03001041 $cmd = 'file --brief --mime '.escapeshellarg($file['tmp_name']).' 2>&1';
Andrey Andreeva49e3812011-12-09 13:05:22 +02001042
1043 if (function_exists('exec'))
Andrey Andreev6700b932011-09-24 14:25:33 +03001044 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001045 /* This might look confusing, as $mime is being populated with all of the output when set in the second parameter.
1046 * However, we only neeed the last line, which is the actual return value of exec(), and as such - it overwrites
1047 * anything that could already be set for $mime previously. This effectively makes the second parameter a dummy
1048 * value, which is only put to allow us to get the return status code.
1049 */
1050 $mime = @exec($cmd, $mime, $return_status);
1051 if ($return_status === 0 && is_string($mime) && preg_match($regexp, $mime, $matches))
1052 {
1053 $this->file_type = $matches[1];
1054 return;
1055 }
1056 }
1057
1058 if ( (bool) @ini_get('safe_mode') === FALSE && function_exists('shell_exec'))
1059 {
1060 $mime = @shell_exec($cmd);
1061 if (strlen($mime) > 0)
1062 {
1063 $mime = explode("\n", trim($mime));
1064 if (preg_match($regexp, $mime[(count($mime) - 1)], $matches))
1065 {
1066 $this->file_type = $matches[1];
1067 return;
1068 }
1069 }
1070 }
1071
1072 if (function_exists('popen'))
1073 {
1074 $proc = @popen($cmd, 'r');
1075 if (is_resource($proc))
1076 {
tubalmartin010f1f42012-03-03 22:24:31 +01001077 $mime = @fread($proc, 512);
Andrey Andreeva49e3812011-12-09 13:05:22 +02001078 @pclose($proc);
1079 if ($mime !== FALSE)
1080 {
1081 $mime = explode("\n", trim($mime));
1082 if (preg_match($regexp, $mime[(count($mime) - 1)], $matches))
1083 {
1084 $this->file_type = $matches[1];
1085 return;
1086 }
1087 }
1088 }
1089 }
1090 }
1091
1092 // Fall back to the deprecated mime_content_type(), if available (still better than $_FILES[$field]['type'])
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001093 if (function_exists('mime_content_type'))
1094 {
1095 $this->file_type = @mime_content_type($file['tmp_name']);
Andrey Andreeva49e3812011-12-09 13:05:22 +02001096 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 +03001097 {
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001098 return;
1099 }
1100 }
1101
1102 $this->file_type = $file['type'];
1103 }
1104
Derek Allard2067d1a2008-11-13 22:59:24 +00001105}
Derek Allard2067d1a2008-11-13 22:59:24 +00001106
1107/* End of file Upload.php */
Andrey Andreev56454792012-05-17 14:32:19 +03001108/* Location: ./system/libraries/Upload.php */