blob: 8ad67050da79b7c718a891d96765744f155353e8 [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
226 if ($this->_file_name_override != '')
227 {
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
279 if ($this->remove_spaces == TRUE)
280 {
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
292 if ($this->overwrite == FALSE)
293 {
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 {
400 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
423 if ($new_filename == '')
424 {
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 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300548 $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 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300644 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 {
690 if ($this->upload_path == '')
691 {
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);
728 return '.'.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 '/',
750 '%20',
751 '%22',
752 '%3c', // <
753 '%253c', // <
754 '%3e', // >
755 '%0e', // >
756 '%28', // (
757 '%29', // )
758 '%2528', // (
759 '%26', // &
760 '%24', // $
761 '%3f', // ?
762 '%3b', // ;
763 '%3d' // =
764 );
Barry Mienydd671972010-10-04 16:33:58 +0200765
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300766 return stripslashes(str_replace($bad, '', $filename));
Derek Allard2067d1a2008-11-13 22:59:24 +0000767 }
768
769 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200770
Derek Allard2067d1a2008-11-13 22:59:24 +0000771 /**
772 * Limit the File Name Length
773 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000774 * @param string
775 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200776 */
Greg Aker58fdee82010-11-10 15:07:09 -0600777 public function limit_filename_length($filename, $length)
Derek Allard2067d1a2008-11-13 22:59:24 +0000778 {
779 if (strlen($filename) < $length)
780 {
781 return $filename;
782 }
Barry Mienydd671972010-10-04 16:33:58 +0200783
Derek Allard2067d1a2008-11-13 22:59:24 +0000784 $ext = '';
785 if (strpos($filename, '.') !== FALSE)
786 {
787 $parts = explode('.', $filename);
788 $ext = '.'.array_pop($parts);
789 $filename = implode('.', $parts);
790 }
Barry Mienydd671972010-10-04 16:33:58 +0200791
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 return substr($filename, 0, ($length - strlen($ext))).$ext;
793 }
794
795 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200796
Derek Allard2067d1a2008-11-13 22:59:24 +0000797 /**
798 * Runs the file through the XSS clean function
799 *
800 * This prevents people from embedding malicious code in their files.
801 * I'm not sure that it won't negatively affect certain files in unexpected ways,
802 * but so far I haven't found that it causes trouble.
803 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200805 */
Greg Aker58fdee82010-11-10 15:07:09 -0600806 public function do_xss_clean()
Barry Mienydd671972010-10-04 16:33:58 +0200807 {
Derek Jonese9d723f2010-07-12 10:10:59 -0500808 $file = $this->file_temp;
Barry Mienydd671972010-10-04 16:33:58 +0200809
Derek Allard2067d1a2008-11-13 22:59:24 +0000810 if (filesize($file) == 0)
811 {
812 return FALSE;
813 }
Barry Mienydd671972010-10-04 16:33:58 +0200814
Greg Akerf82e51c2010-04-14 19:33:50 -0500815 if (function_exists('memory_get_usage') && memory_get_usage() && ini_get('memory_limit') != '')
816 {
817 $current = ini_get('memory_limit') * 1024 * 1024;
Barry Mienydd671972010-10-04 16:33:58 +0200818
Greg Akerc78a2592010-06-09 11:45:32 -0500819 // There was a bug/behavioural change in PHP 5.2, where numbers over one million get output
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300820 // into scientific notation. number_format() ensures this number is an integer
Greg Akerc78a2592010-06-09 11:45:32 -0500821 // http://bugs.php.net/bug.php?id=43053
Barry Mienydd671972010-10-04 16:33:58 +0200822
Greg Akerc78a2592010-06-09 11:45:32 -0500823 $new_memory = number_format(ceil(filesize($file) + $current), 0, '.', '');
Barry Mienydd671972010-10-04 16:33:58 +0200824
Greg Akerc78a2592010-06-09 11:45:32 -0500825 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 -0500826 }
827
828 // If the file being uploaded is an image, then we should have no problem with XSS attacks (in theory), but
829 // 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 +0300830 // using IE who looks at the image. It does this by inspecting the first 255 bytes of an image. To get around this
831 // 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 +0200832 // 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 -0500833 // attempted XSS attack.
834
835 if (function_exists('getimagesize') && @getimagesize($file) !== FALSE)
836 {
Barry Mienydd671972010-10-04 16:33:58 +0200837 if (($file = @fopen($file, 'rb')) === FALSE) // "b" to force binary
838 {
Greg Akerf82e51c2010-04-14 19:33:50 -0500839 return FALSE; // Couldn't open the file, return FALSE
Barry Mienydd671972010-10-04 16:33:58 +0200840 }
Greg Akerf82e51c2010-04-14 19:33:50 -0500841
Barry Mienydd671972010-10-04 16:33:58 +0200842 $opening_bytes = fread($file, 256);
843 fclose($file);
Greg Akerf82e51c2010-04-14 19:33:50 -0500844
845 // These are known to throw IE into mime-type detection chaos
846 // <a, <body, <head, <html, <img, <plaintext, <pre, <script, <table, <title
847 // title is basically just in SVG, but we filter it anyhow
848
849 if ( ! preg_match('/<(a|body|head|html|img|plaintext|pre|script|table|title)[\s>]/i', $opening_bytes))
850 {
851 return TRUE; // its an image, no "triggers" detected in the first 256 bytes, we're good
852 }
853 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000854
855 if (($data = @file_get_contents($file)) === FALSE)
856 {
857 return FALSE;
858 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000859
Greg Akerf82e51c2010-04-14 19:33:50 -0500860 $CI =& get_instance();
Greg Akerf82e51c2010-04-14 19:33:50 -0500861 return $CI->security->xss_clean($data, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000862 }
Barry Mienydd671972010-10-04 16:33:58 +0200863
Derek Allard2067d1a2008-11-13 22:59:24 +0000864 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200865
Derek Allard2067d1a2008-11-13 22:59:24 +0000866 /**
867 * Set an error message
868 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 * @param string
870 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200871 */
Greg Aker58fdee82010-11-10 15:07:09 -0600872 public function set_error($msg)
Derek Allard2067d1a2008-11-13 22:59:24 +0000873 {
Barry Mienydd671972010-10-04 16:33:58 +0200874 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000875 $CI->lang->load('upload');
Barry Mienydd671972010-10-04 16:33:58 +0200876
Derek Allard2067d1a2008-11-13 22:59:24 +0000877 if (is_array($msg))
878 {
879 foreach ($msg as $val)
880 {
Barry Mienydd671972010-10-04 16:33:58 +0200881 $msg = ($CI->lang->line($val) == FALSE) ? $val : $CI->lang->line($val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000882 $this->error_msg[] = $msg;
883 log_message('error', $msg);
Barry Mienydd671972010-10-04 16:33:58 +0200884 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 }
886 else
887 {
888 $msg = ($CI->lang->line($msg) == FALSE) ? $msg : $CI->lang->line($msg);
889 $this->error_msg[] = $msg;
890 log_message('error', $msg);
891 }
892 }
Barry Mienydd671972010-10-04 16:33:58 +0200893
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200895
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 /**
897 * Display the error message
898 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 * @param string
900 * @param string
901 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200902 */
Greg Aker58fdee82010-11-10 15:07:09 -0600903 public function display_errors($open = '<p>', $close = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300905 return (count($this->error_msg) > 0) ? $open.implode($close.$open, $this->error_msg).$close : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000906 }
Barry Mienydd671972010-10-04 16:33:58 +0200907
Derek Allard2067d1a2008-11-13 22:59:24 +0000908 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200909
Derek Allard2067d1a2008-11-13 22:59:24 +0000910 /**
911 * List of Mime Types
912 *
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300913 * This is a list of mime types. We use it to validate
Derek Allard2067d1a2008-11-13 22:59:24 +0000914 * the "allowed types" set by the developer
915 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 * @param string
917 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200918 */
Greg Aker58fdee82010-11-10 15:07:09 -0600919 public function mimes_types($mime)
Derek Allard2067d1a2008-11-13 22:59:24 +0000920 {
921 global $mimes;
Barry Mienydd671972010-10-04 16:33:58 +0200922
Greg Akerd96f8822011-12-27 16:23:47 -0600923 if (count($this->mimes) == 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300925 if (defined('ENVIRONMENT') && is_file(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'))
Greg Akerd96f8822011-12-27 16:23:47 -0600926 {
927 include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php');
928 }
929 elseif (is_file(APPPATH.'config/mimes.php'))
930 {
Michiel Vugteveenb5783262012-02-29 15:40:28 +0100931 include(APPPATH.'config/mimes.php');
Greg Akerd96f8822011-12-27 16:23:47 -0600932 }
933 else
bubbafoley0ea04142011-03-17 14:55:41 -0500934 {
Eric Barnesfdd5b112011-03-21 21:28:58 -0400935 return FALSE;
bubbafoley0ea04142011-03-17 14:55:41 -0500936 }
Eric Barnes92808342011-03-18 09:02:37 -0400937
Eric Barnesfdd5b112011-03-21 21:28:58 -0400938 $this->mimes = $mimes;
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 }
Barry Mienydd671972010-10-04 16:33:58 +0200940
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300941 return isset($this->mimes[$mime]) ? $this->mimes[$mime] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000942 }
943
944 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200945
Derek Allard2067d1a2008-11-13 22:59:24 +0000946 /**
947 * Prep Filename
948 *
949 * Prevents possible script execution from Apache's handling of files multiple extensions
950 * http://httpd.apache.org/docs/1.3/mod/mod_mime.html#multipleext
951 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 * @param string
953 * @return string
954 */
Greg Aker58fdee82010-11-10 15:07:09 -0600955 protected function _prep_filename($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000956 {
Greg Aker924000e2010-07-22 11:04:58 -0500957 if (strpos($filename, '.') === FALSE OR $this->allowed_types == '*')
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 {
959 return $filename;
960 }
Derek Allard616dab82009-02-16 15:44:32 +0000961
Derek Allard2067d1a2008-11-13 22:59:24 +0000962 $parts = explode('.', $filename);
963 $ext = array_pop($parts);
964 $filename = array_shift($parts);
Derek Allard616dab82009-02-16 15:44:32 +0000965
Derek Allard2067d1a2008-11-13 22:59:24 +0000966 foreach ($parts as $part)
967 {
Derek Jonese9d723f2010-07-12 10:10:59 -0500968 if ( ! in_array(strtolower($part), $this->allowed_types) OR $this->mimes_types(strtolower($part)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 {
970 $filename .= '.'.$part.'_';
971 }
972 else
973 {
974 $filename .= '.'.$part;
975 }
976 }
Derek Allardd70b0642009-02-16 13:51:42 +0000977
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300978 return $filename.'.'.$ext;
Derek Allard2067d1a2008-11-13 22:59:24 +0000979 }
980
981 // --------------------------------------------------------------------
982
Andrey Andreev3a3c9472011-09-24 14:25:33 +0300983 /**
984 * File MIME type
985 *
986 * Detects the (actual) MIME type of the uploaded file, if possible.
987 * The input array is expected to be $_FILES[$field]
988 *
989 * @param array
990 * @return void
991 */
992 protected function _file_mime_type($file)
993 {
Andrey Andreeva49e3812011-12-09 13:05:22 +0200994 // We'll need this to validate the MIME info string (e.g. text/plain; charset=us-ascii)
Andrey Andreevf7aed122011-12-13 11:01:06 +0200995 $regexp = '/^([a-z\-]+\/[a-z0-9\-\.\+]+)(;\s.+)?$/';
Andrey Andreeva49e3812011-12-09 13:05:22 +0200996
997 /* Fileinfo extension - most reliable method
998 *
999 * Unfortunately, prior to PHP 5.3 - it's only available as a PECL extension and the
1000 * more convenient FILEINFO_MIME_TYPE flag doesn't exist.
1001 */
1002 if (function_exists('finfo_file'))
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001003 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001004 $finfo = finfo_open(FILEINFO_MIME);
1005 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 +03001006 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001007 $mime = @finfo_file($finfo, $file['tmp_name']);
1008 finfo_close($finfo);
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001009
1010 /* According to the comments section of the PHP manual page,
1011 * it is possible that this function returns an empty string
Andrey Andreev2cec39f2011-09-24 22:59:37 +03001012 * for some files (e.g. if they don't exist in the magic MIME database)
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001013 */
Andrey Andreeva49e3812011-12-09 13:05:22 +02001014 if (is_string($mime) && preg_match($regexp, $mime, $matches))
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001015 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001016 $this->file_type = $matches[1];
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001017 return;
1018 }
1019 }
1020 }
1021
Andrey Andreeva49e3812011-12-09 13:05:22 +02001022 /* This is an ugly hack, but UNIX-type systems provide a "native" way to detect the file type,
1023 * which is still more secure than depending on the value of $_FILES[$field]['type'], and as it
1024 * was reported in issue #750 (https://github.com/EllisLab/CodeIgniter/issues/750) - it's better
1025 * than mime_content_type() as well, hence the attempts to try calling the command line with
1026 * three different functions.
Andrey Andreev6700b932011-09-24 14:25:33 +03001027 *
1028 * Notes:
Andrey Andreev59654312011-12-02 14:28:54 +02001029 * - the DIRECTORY_SEPARATOR comparison ensures that we're not on a Windows system
Andrey Andreeva49e3812011-12-09 13:05:22 +02001030 * - many system admins would disable the exec(), shell_exec(), popen() and similar functions
1031 * due to security concerns, hence the function_exists() checks
Andrey Andreev6700b932011-09-24 14:25:33 +03001032 */
Andrey Andreeva49e3812011-12-09 13:05:22 +02001033 if (DIRECTORY_SEPARATOR !== '\\')
Andrey Andreev6700b932011-09-24 14:25:33 +03001034 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001035 $cmd = 'file --brief --mime ' . escapeshellarg($file['tmp_name']) . ' 2>&1';
1036
1037 if (function_exists('exec'))
Andrey Andreev6700b932011-09-24 14:25:33 +03001038 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001039 /* This might look confusing, as $mime is being populated with all of the output when set in the second parameter.
1040 * However, we only neeed the last line, which is the actual return value of exec(), and as such - it overwrites
1041 * anything that could already be set for $mime previously. This effectively makes the second parameter a dummy
1042 * value, which is only put to allow us to get the return status code.
1043 */
1044 $mime = @exec($cmd, $mime, $return_status);
1045 if ($return_status === 0 && is_string($mime) && preg_match($regexp, $mime, $matches))
1046 {
1047 $this->file_type = $matches[1];
1048 return;
1049 }
1050 }
1051
1052 if ( (bool) @ini_get('safe_mode') === FALSE && function_exists('shell_exec'))
1053 {
1054 $mime = @shell_exec($cmd);
1055 if (strlen($mime) > 0)
1056 {
1057 $mime = explode("\n", trim($mime));
1058 if (preg_match($regexp, $mime[(count($mime) - 1)], $matches))
1059 {
1060 $this->file_type = $matches[1];
1061 return;
1062 }
1063 }
1064 }
1065
1066 if (function_exists('popen'))
1067 {
1068 $proc = @popen($cmd, 'r');
1069 if (is_resource($proc))
1070 {
tubalmartin010f1f42012-03-03 22:24:31 +01001071 $mime = @fread($proc, 512);
Andrey Andreeva49e3812011-12-09 13:05:22 +02001072 @pclose($proc);
1073 if ($mime !== FALSE)
1074 {
1075 $mime = explode("\n", trim($mime));
1076 if (preg_match($regexp, $mime[(count($mime) - 1)], $matches))
1077 {
1078 $this->file_type = $matches[1];
1079 return;
1080 }
1081 }
1082 }
1083 }
1084 }
1085
1086 // Fall back to the deprecated mime_content_type(), if available (still better than $_FILES[$field]['type'])
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001087 if (function_exists('mime_content_type'))
1088 {
1089 $this->file_type = @mime_content_type($file['tmp_name']);
Andrey Andreeva49e3812011-12-09 13:05:22 +02001090 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 +03001091 {
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001092 return;
1093 }
1094 }
1095
1096 $this->file_type = $file['type'];
1097 }
1098
Derek Allard2067d1a2008-11-13 22:59:24 +00001099}
Derek Allard2067d1a2008-11-13 22:59:24 +00001100
1101/* End of file Upload.php */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +03001102/* Location: ./system/libraries/Upload.php */