blob: d381440cd644bbe1f475ac96f2dcff6fcf13ffb1 [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;
Andrey Andreevd60e7002012-06-17 00:03:03 +030062 public $detect_mime = TRUE;
Andrey Andreev8e6f7a92012-03-26 20:13:00 +030063 public $xss_clean = FALSE;
64 public $temp_prefix = 'temp_file_';
65 public $client_name = '';
Barry Mienydd671972010-10-04 16:33:58 +020066
Greg Aker58fdee82010-11-10 15:07:09 -060067 protected $_file_name_override = '';
Barry Mienydd671972010-10-04 16:33:58 +020068
Derek Allard2067d1a2008-11-13 22:59:24 +000069 /**
70 * Constructor
71 *
Andrey Andreev8e6f7a92012-03-26 20:13:00 +030072 * @param array
73 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +000074 */
Greg Aker58fdee82010-11-10 15:07:09 -060075 public function __construct($props = array())
Derek Allard2067d1a2008-11-13 22:59:24 +000076 {
77 if (count($props) > 0)
78 {
79 $this->initialize($props);
80 }
Barry Mienydd671972010-10-04 16:33:58 +020081
Andrey Andreev6ef498b2012-06-05 22:01:58 +030082 $this->mimes =& get_mimes();
83
Andrey Andreev8e6f7a92012-03-26 20:13:00 +030084 log_message('debug', 'Upload Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +000085 }
Barry Mienydd671972010-10-04 16:33:58 +020086
Derek Allard2067d1a2008-11-13 22:59:24 +000087 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020088
Derek Allard2067d1a2008-11-13 22:59:24 +000089 /**
90 * Initialize preferences
91 *
Derek Allard2067d1a2008-11-13 22:59:24 +000092 * @param array
93 * @return void
Barry Mienydd671972010-10-04 16:33:58 +020094 */
Greg Aker58fdee82010-11-10 15:07:09 -060095 public function initialize($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +000096 {
97 $defaults = array(
Andrey Andreev8e6f7a92012-03-26 20:13:00 +030098 'max_size' => 0,
99 'max_width' => 0,
100 'max_height' => 0,
101 'max_filename' => 0,
102 'max_filename_increment' => 100,
103 'allowed_types' => '',
104 'file_temp' => '',
105 'file_name' => '',
106 'orig_name' => '',
107 'file_type' => '',
108 'file_size' => '',
109 'file_ext' => '',
110 'upload_path' => '',
111 'overwrite' => FALSE,
112 'encrypt_name' => FALSE,
113 'is_image' => FALSE,
114 'image_width' => '',
115 'image_height' => '',
116 'image_type' => '',
117 'image_size_str' => '',
118 'error_msg' => array(),
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300119 'remove_spaces' => TRUE,
Andrey Andreevd60e7002012-06-17 00:03:03 +0300120 'detect_mime' => TRUE,
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300121 'xss_clean' => FALSE,
122 'temp_prefix' => 'temp_file_',
123 'client_name' => ''
124 );
Barry Mienydd671972010-10-04 16:33:58 +0200125
126
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 foreach ($defaults as $key => $val)
128 {
129 if (isset($config[$key]))
130 {
131 $method = 'set_'.$key;
132 if (method_exists($this, $method))
133 {
134 $this->$method($config[$key]);
135 }
136 else
137 {
138 $this->$key = $config[$key];
Barry Mienydd671972010-10-04 16:33:58 +0200139 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 }
141 else
142 {
143 $this->$key = $val;
144 }
145 }
Barry Mienydd671972010-10-04 16:33:58 +0200146
Derek Jonese9d723f2010-07-12 10:10:59 -0500147 // if a file_name was provided in the config, use it instead of the user input
148 // supplied file name for all uploads until initialized again
149 $this->_file_name_override = $this->file_name;
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 }
Barry Mienydd671972010-10-04 16:33:58 +0200151
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200153
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 /**
155 * Perform the file upload
156 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200158 */
Greg Aker58fdee82010-11-10 15:07:09 -0600159 public function do_upload($field = 'userfile')
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300161 // Is $_FILES[$field] set? If not, no reason to continue.
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 if ( ! isset($_FILES[$field]))
163 {
164 $this->set_error('upload_no_file_selected');
165 return FALSE;
166 }
Barry Mienydd671972010-10-04 16:33:58 +0200167
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 // Is the upload path valid?
169 if ( ! $this->validate_upload_path())
170 {
171 // errors will already be set by validate_upload_path() so just return FALSE
172 return FALSE;
173 }
174
175 // Was the file able to be uploaded? If not, determine the reason why.
176 if ( ! is_uploaded_file($_FILES[$field]['tmp_name']))
177 {
178 $error = ( ! isset($_FILES[$field]['error'])) ? 4 : $_FILES[$field]['error'];
179
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300180 switch ($error)
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 {
182 case 1: // UPLOAD_ERR_INI_SIZE
183 $this->set_error('upload_file_exceeds_limit');
184 break;
185 case 2: // UPLOAD_ERR_FORM_SIZE
186 $this->set_error('upload_file_exceeds_form_limit');
187 break;
188 case 3: // UPLOAD_ERR_PARTIAL
Barry Mienydd671972010-10-04 16:33:58 +0200189 $this->set_error('upload_file_partial');
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 break;
191 case 4: // UPLOAD_ERR_NO_FILE
Barry Mienydd671972010-10-04 16:33:58 +0200192 $this->set_error('upload_no_file_selected');
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 break;
194 case 6: // UPLOAD_ERR_NO_TMP_DIR
195 $this->set_error('upload_no_temp_directory');
196 break;
197 case 7: // UPLOAD_ERR_CANT_WRITE
198 $this->set_error('upload_unable_to_write_file');
199 break;
200 case 8: // UPLOAD_ERR_EXTENSION
201 $this->set_error('upload_stopped_by_extension');
202 break;
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300203 default:
204 $this->set_error('upload_no_file_selected');
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 break;
206 }
207
208 return FALSE;
209 }
210
211 // Set the uploaded data as class variables
Barry Mienydd671972010-10-04 16:33:58 +0200212 $this->file_temp = $_FILES[$field]['tmp_name'];
213 $this->file_size = $_FILES[$field]['size'];
Andrey Andreevd60e7002012-06-17 00:03:03 +0300214
215 // Skip MIME type detection?
216 if ($this->detect_mime !== FALSE)
217 {
218 $this->_file_mime_type($_FILES[$field]);
219 }
220
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300221 $this->file_type = preg_replace('/^(.+?);.*$/', '\\1', $this->file_type);
Derek Jones616fb022010-04-22 16:52:18 -0500222 $this->file_type = strtolower(trim(stripslashes($this->file_type), '"'));
Derek Jonese9d723f2010-07-12 10:10:59 -0500223 $this->file_name = $this->_prep_filename($_FILES[$field]['name']);
224 $this->file_ext = $this->get_extension($this->file_name);
225 $this->client_name = $this->file_name;
Barry Mienydd671972010-10-04 16:33:58 +0200226
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 // Is the file type allowed to be uploaded?
228 if ( ! $this->is_allowed_filetype())
229 {
230 $this->set_error('upload_invalid_filetype');
231 return FALSE;
232 }
233
Derek Jonese9d723f2010-07-12 10:10:59 -0500234 // if we're overriding, let's now make sure the new name and type is allowed
Alex Bilbied261b1e2012-06-02 11:12:16 +0100235 if ($this->_file_name_override !== '')
Derek Jonese9d723f2010-07-12 10:10:59 -0500236 {
237 $this->file_name = $this->_prep_filename($this->_file_name_override);
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000238
239 // If no extension was provided in the file_name config item, use the uploaded one
Pascal Kriete14287f32011-02-14 13:39:34 -0500240 if (strpos($this->_file_name_override, '.') === FALSE)
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000241 {
242 $this->file_name .= $this->file_ext;
243 }
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000244 else
245 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300246 // An extension was provided, lets have it!
247 $this->file_ext = $this->get_extension($this->_file_name_override);
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000248 }
Derek Jonese9d723f2010-07-12 10:10:59 -0500249
250 if ( ! $this->is_allowed_filetype(TRUE))
251 {
252 $this->set_error('upload_invalid_filetype');
Barry Mienydd671972010-10-04 16:33:58 +0200253 return FALSE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500254 }
255 }
Barry Mienydd671972010-10-04 16:33:58 +0200256
Derek Jonese9d723f2010-07-12 10:10:59 -0500257 // Convert the file size to kilobytes
258 if ($this->file_size > 0)
259 {
260 $this->file_size = round($this->file_size/1024, 2);
261 }
262
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 // Is the file size within the allowed maximum?
264 if ( ! $this->is_allowed_filesize())
265 {
266 $this->set_error('upload_invalid_filesize');
267 return FALSE;
268 }
269
270 // Are the image dimensions within the allowed size?
271 // Note: This can fail if the server has an open_basdir restriction.
272 if ( ! $this->is_allowed_dimensions())
273 {
274 $this->set_error('upload_invalid_dimensions');
275 return FALSE;
276 }
277
278 // Sanitize the file name for security
279 $this->file_name = $this->clean_file_name($this->file_name);
Barry Mienydd671972010-10-04 16:33:58 +0200280
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 // Truncate the file name if it's too long
282 if ($this->max_filename > 0)
283 {
284 $this->file_name = $this->limit_filename_length($this->file_name, $this->max_filename);
285 }
286
287 // Remove white spaces in the name
Alex Bilbied261b1e2012-06-02 11:12:16 +0100288 if ($this->remove_spaces === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300290 $this->file_name = preg_replace('/\s+/', '_', $this->file_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 }
292
293 /*
294 * Validate the file name
295 * This function appends an number onto the end of
296 * the file if one with the same name already exists.
297 * If it returns false there was a problem.
298 */
299 $this->orig_name = $this->file_name;
300
Alex Bilbied261b1e2012-06-02 11:12:16 +0100301 if ($this->overwrite === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 {
303 $this->file_name = $this->set_filename($this->upload_path, $this->file_name);
Barry Mienydd671972010-10-04 16:33:58 +0200304
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 if ($this->file_name === FALSE)
306 {
307 return FALSE;
308 }
309 }
310
311 /*
Derek Jonese9d723f2010-07-12 10:10:59 -0500312 * Run the file through the XSS hacking filter
313 * This helps prevent malicious code from being
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300314 * embedded within a file. Scripts can easily
Derek Jonese9d723f2010-07-12 10:10:59 -0500315 * be disguised as images or other file types.
316 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300317 if ($this->xss_clean && $this->do_xss_clean() === FALSE)
Derek Jonese9d723f2010-07-12 10:10:59 -0500318 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300319 $this->set_error('upload_unable_to_write_file');
320 return FALSE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500321 }
322
323 /*
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 * Move the file to the final destination
325 * To deal with different server configurations
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300326 * we'll attempt to use copy() first. If that fails
327 * we'll use move_uploaded_file(). One of the two should
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 * reliably work in most environments
329 */
330 if ( ! @copy($this->file_temp, $this->upload_path.$this->file_name))
331 {
332 if ( ! @move_uploaded_file($this->file_temp, $this->upload_path.$this->file_name))
333 {
Barry Mienydd671972010-10-04 16:33:58 +0200334 $this->set_error('upload_destination_error');
335 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 }
337 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000338
339 /*
340 * Set the finalized image dimensions
341 * This sets the image width/height (assuming the
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300342 * file was an image). We use this information
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 * in the "data" function.
344 */
345 $this->set_image_properties($this->upload_path.$this->file_name);
346
347 return TRUE;
348 }
Barry Mienydd671972010-10-04 16:33:58 +0200349
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200351
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 /**
353 * Finalized Data Array
Barry Mienydd671972010-10-04 16:33:58 +0200354 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 * Returns an associative array containing all of the information
356 * related to the upload, allowing the developer easy access in one array.
357 *
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200358 * @param string
Michiel Vugteveen0ee287e2012-06-11 10:38:14 +0200359 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200360 */
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200361 public function data($index = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 {
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200363 $data = array(
Michiel Vugteveen3a7fb042012-06-11 09:29:16 +0200364 'file_name' => $this->file_name,
365 'file_type' => $this->file_type,
366 'file_path' => $this->upload_path,
367 'full_path' => $this->upload_path.$this->file_name,
368 'raw_name' => str_replace($this->file_ext, '', $this->file_name),
369 'orig_name' => $this->orig_name,
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300370 'client_name' => $this->client_name,
Michiel Vugteveen3a7fb042012-06-11 09:29:16 +0200371 'file_ext' => $this->file_ext,
372 'file_size' => $this->file_size,
373 'is_image' => $this->is_image(),
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300374 'image_width' => $this->image_width,
375 'image_height' => $this->image_height,
376 'image_type' => $this->image_type,
377 'image_size_str' => $this->image_size_str,
378 );
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200379
Michiel Vugteveen46a04292012-06-11 10:37:52 +0200380 if ( ! empty($index))
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200381 {
Michiel Vugteveen46a04292012-06-11 10:37:52 +0200382 return isset($data[$index]) ? $data[$index] : NULL;
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200383 }
384
Michiel Vugteveen46a04292012-06-11 10:37:52 +0200385 return $data;
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 }
Barry Mienydd671972010-10-04 16:33:58 +0200387
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200389
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 /**
391 * Set Upload Path
392 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 * @param string
394 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200395 */
Greg Aker58fdee82010-11-10 15:07:09 -0600396 public function set_upload_path($path)
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 {
398 // Make sure it has a trailing slash
399 $this->upload_path = rtrim($path, '/').'/';
400 }
Barry Mienydd671972010-10-04 16:33:58 +0200401
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200403
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 /**
405 * Set the file name
406 *
407 * This function takes a filename/path as input and looks for the
408 * existence of a file with the same name. If found, it will append a
409 * number to the end of the filename to avoid overwriting a pre-existing file.
410 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 * @param string
412 * @param string
413 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200414 */
Greg Aker58fdee82010-11-10 15:07:09 -0600415 public function set_filename($path, $filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100417 if ($this->encrypt_name === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200418 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 mt_srand();
Barry Mienydd671972010-10-04 16:33:58 +0200420 $filename = md5(uniqid(mt_rand())).$this->file_ext;
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 }
Barry Mienydd671972010-10-04 16:33:58 +0200422
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 if ( ! file_exists($path.$filename))
424 {
425 return $filename;
426 }
Barry Mienydd671972010-10-04 16:33:58 +0200427
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 $filename = str_replace($this->file_ext, '', $filename);
Barry Mienydd671972010-10-04 16:33:58 +0200429
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 $new_filename = '';
Adam Jackettccbbea12011-08-21 16:19:11 -0400431 for ($i = 1; $i < $this->max_filename_increment; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200432 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 if ( ! file_exists($path.$filename.$i.$this->file_ext))
434 {
435 $new_filename = $filename.$i.$this->file_ext;
436 break;
437 }
438 }
439
Alex Bilbied261b1e2012-06-02 11:12:16 +0100440 if ($new_filename === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 {
442 $this->set_error('upload_bad_filename');
443 return FALSE;
444 }
445 else
446 {
447 return $new_filename;
448 }
449 }
Barry Mienydd671972010-10-04 16:33:58 +0200450
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200452
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 /**
454 * Set Maximum File Size
455 *
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300456 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200458 */
Greg Aker58fdee82010-11-10 15:07:09 -0600459 public function set_max_filesize($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300461 $this->max_size = ((int) $n < 0) ? 0 : (int) $n;
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 }
Barry Mienydd671972010-10-04 16:33:58 +0200463
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200465
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 /**
467 * Set Maximum File Name Length
468 *
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300469 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200471 */
Greg Aker58fdee82010-11-10 15:07:09 -0600472 public function set_max_filename($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300474 $this->max_filename = ((int) $n < 0) ? 0 : (int) $n;
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 }
476
477 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200478
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 /**
480 * Set Maximum Image Width
481 *
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300482 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200484 */
Greg Aker58fdee82010-11-10 15:07:09 -0600485 public function set_max_width($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300487 $this->max_width = ((int) $n < 0) ? 0 : (int) $n;
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 }
Barry Mienydd671972010-10-04 16:33:58 +0200489
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200491
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 /**
493 * Set Maximum Image Height
494 *
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300495 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200497 */
Greg Aker58fdee82010-11-10 15:07:09 -0600498 public function set_max_height($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300500 $this->max_height = ((int) $n < 0) ? 0 : (int) $n;
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 }
Barry Mienydd671972010-10-04 16:33:58 +0200502
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200504
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 /**
506 * Set Allowed File Types
507 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 * @param string
509 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200510 */
Greg Aker58fdee82010-11-10 15:07:09 -0600511 public function set_allowed_types($types)
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300513 if ( ! is_array($types) && $types === '*')
Derek Jonese12f64e2010-03-02 22:55:08 -0600514 {
515 $this->allowed_types = '*';
516 return;
517 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 $this->allowed_types = explode('|', $types);
519 }
Barry Mienydd671972010-10-04 16:33:58 +0200520
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200522
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 /**
524 * Set Image Properties
525 *
526 * Uses GD to determine the width/height/type of image
527 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 * @param string
529 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200530 */
Greg Aker58fdee82010-11-10 15:07:09 -0600531 public function set_image_properties($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 {
533 if ( ! $this->is_image())
534 {
535 return;
536 }
537
538 if (function_exists('getimagesize'))
539 {
540 if (FALSE !== ($D = @getimagesize($path)))
Barry Mienydd671972010-10-04 16:33:58 +0200541 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
543
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300544 $this->image_width = $D[0];
545 $this->image_height = $D[1];
546 $this->image_type = isset($types[$D[2]]) ? $types[$D[2]] : 'unknown';
547 $this->image_size_str = $D[3]; // string containing height and width
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 }
549 }
550 }
Barry Mienydd671972010-10-04 16:33:58 +0200551
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200553
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 /**
555 * Set XSS Clean
556 *
557 * Enables the XSS flag so that the file that was uploaded
558 * will be run through the XSS filter.
559 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 * @param bool
561 * @return void
562 */
Greg Aker58fdee82010-11-10 15:07:09 -0600563 public function set_xss_clean($flag = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100565 $this->xss_clean = ($flag === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 }
Barry Mienydd671972010-10-04 16:33:58 +0200567
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200569
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 /**
571 * Validate the image
572 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200574 */
Greg Aker58fdee82010-11-10 15:07:09 -0600575 public function is_image()
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 {
577 // IE will sometimes return odd mime-types during upload, so here we just standardize all
578 // jpegs or pngs to the same file type.
579
Derek Jones4b9c6292011-07-01 17:40:48 -0500580 $png_mimes = array('image/x-png');
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 $jpeg_mimes = array('image/jpg', 'image/jpe', 'image/jpeg', 'image/pjpeg');
Barry Mienydd671972010-10-04 16:33:58 +0200582
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 if (in_array($this->file_type, $png_mimes))
584 {
585 $this->file_type = 'image/png';
586 }
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300587 elseif (in_array($this->file_type, $jpeg_mimes))
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 {
589 $this->file_type = 'image/jpeg';
590 }
591
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300592 $img_mimes = array('image/gif', 'image/jpeg', 'image/png');
Derek Allard2067d1a2008-11-13 22:59:24 +0000593
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300594 return in_array($this->file_type, $img_mimes, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 }
Barry Mienydd671972010-10-04 16:33:58 +0200596
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200598
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 /**
600 * Verify that the filetype is allowed
601 *
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200602 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200604 */
Greg Aker58fdee82010-11-10 15:07:09 -0600605 public function is_allowed_filetype($ignore_mime = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 {
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200607 if ($this->allowed_types === '*')
Derek Jonese12f64e2010-03-02 22:55:08 -0600608 {
609 return TRUE;
610 }
Barry Mienydd671972010-10-04 16:33:58 +0200611
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200612 if ( ! is_array($this->allowed_types) OR count($this->allowed_types) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 {
614 $this->set_error('upload_no_file_types');
615 return FALSE;
616 }
Barry Mienydd671972010-10-04 16:33:58 +0200617
Derek Jonese9d723f2010-07-12 10:10:59 -0500618 $ext = strtolower(ltrim($this->file_ext, '.'));
Barry Mienydd671972010-10-04 16:33:58 +0200619
Derek Jonese9d723f2010-07-12 10:10:59 -0500620 if ( ! in_array($ext, $this->allowed_types))
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 {
Derek Jonese9d723f2010-07-12 10:10:59 -0500622 return FALSE;
623 }
Derek Jonesafa282f2009-02-10 17:11:52 +0000624
Barry Mienydd671972010-10-04 16:33:58 +0200625 // Images get some additional checks
Derek Jonese9d723f2010-07-12 10:10:59 -0500626 $image_types = array('gif', 'jpg', 'jpeg', 'png', 'jpe');
Barry Mienydd671972010-10-04 16:33:58 +0200627
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200628 if (in_array($ext, $image_types) && @getimagesize($this->file_temp) === FALSE)
Derek Jonese9d723f2010-07-12 10:10:59 -0500629 {
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200630 return FALSE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500631 }
Barry Mienydd671972010-10-04 16:33:58 +0200632
Derek Jonese9d723f2010-07-12 10:10:59 -0500633 if ($ignore_mime === TRUE)
634 {
635 return TRUE;
636 }
Barry Mienydd671972010-10-04 16:33:58 +0200637
Derek Jonese9d723f2010-07-12 10:10:59 -0500638 $mime = $this->mimes_types($ext);
Barry Mienydd671972010-10-04 16:33:58 +0200639
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300640 if (is_array($mime) && in_array($this->file_type, $mime, TRUE))
Derek Jonese9d723f2010-07-12 10:10:59 -0500641 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300642 return TRUE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500643 }
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200644 elseif ($mime === $this->file_type)
Derek Jonese9d723f2010-07-12 10:10:59 -0500645 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300646 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 }
Barry Mienydd671972010-10-04 16:33:58 +0200648
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 return FALSE;
650 }
Barry Mienydd671972010-10-04 16:33:58 +0200651
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200653
Derek Allard2067d1a2008-11-13 22:59:24 +0000654 /**
655 * Verify that the file is within the allowed size
656 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200658 */
Greg Aker58fdee82010-11-10 15:07:09 -0600659 public function is_allowed_filesize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100661 return ($this->max_size === 0 OR $this->max_size > $this->file_size);
Derek Allard2067d1a2008-11-13 22:59:24 +0000662 }
Barry Mienydd671972010-10-04 16:33:58 +0200663
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200665
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 /**
667 * Verify that the image is within the allowed width/height
668 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000669 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200670 */
Greg Aker58fdee82010-11-10 15:07:09 -0600671 public function is_allowed_dimensions()
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 {
673 if ( ! $this->is_image())
674 {
675 return TRUE;
676 }
677
678 if (function_exists('getimagesize'))
679 {
680 $D = @getimagesize($this->file_temp);
681
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300682 if ($this->max_width > 0 && $D[0] > $this->max_width)
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 {
684 return FALSE;
685 }
686
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300687 if ($this->max_height > 0 && $D[1] > $this->max_height)
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 {
689 return FALSE;
690 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000691 }
692
693 return TRUE;
694 }
Barry Mienydd671972010-10-04 16:33:58 +0200695
Derek Allard2067d1a2008-11-13 22:59:24 +0000696 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200697
Derek Allard2067d1a2008-11-13 22:59:24 +0000698 /**
699 * Validate Upload Path
700 *
701 * Verifies that it is a valid upload path with proper permissions.
702 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000703 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200704 */
Greg Aker58fdee82010-11-10 15:07:09 -0600705 public function validate_upload_path()
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100707 if ($this->upload_path === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 {
709 $this->set_error('upload_no_filepath');
710 return FALSE;
711 }
Barry Mienydd671972010-10-04 16:33:58 +0200712
Andrey Andreevc839d282012-06-07 14:35:27 +0300713 if (@realpath($this->upload_path) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000714 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300715 $this->upload_path = str_replace('\\', '/', realpath($this->upload_path));
Derek Allard2067d1a2008-11-13 22:59:24 +0000716 }
717
718 if ( ! @is_dir($this->upload_path))
719 {
720 $this->set_error('upload_no_filepath');
721 return FALSE;
722 }
723
724 if ( ! is_really_writable($this->upload_path))
725 {
726 $this->set_error('upload_not_writable');
727 return FALSE;
728 }
729
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300730 $this->upload_path = preg_replace('/(.+?)\/*$/', '\\1/', $this->upload_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000731 return TRUE;
732 }
Barry Mienydd671972010-10-04 16:33:58 +0200733
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200735
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 /**
737 * Extract the file extension
738 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000739 * @param string
740 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200741 */
Greg Aker58fdee82010-11-10 15:07:09 -0600742 public function get_extension($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000743 {
744 $x = explode('.', $filename);
Andrey Andreev46d53fb2012-05-11 13:42:24 +0300745 return (count($x) !== 1) ? '.'.end($x) : '';
Barry Mienydd671972010-10-04 16:33:58 +0200746 }
747
Derek Allard2067d1a2008-11-13 22:59:24 +0000748 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200749
Derek Allard2067d1a2008-11-13 22:59:24 +0000750 /**
751 * Clean the file name for security
752 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000753 * @param string
754 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200755 */
Greg Aker58fdee82010-11-10 15:07:09 -0600756 public function clean_file_name($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000757 {
758 $bad = array(
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300759 '<!--', '-->',
760 "'", '"',
761 '<', '>',
762 '&', '$',
763 '=',
764 ';',
765 '?',
766 '/',
Andrey Andreev470805b2012-05-24 21:57:21 +0300767 '!',
768 '#',
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300769 '%20',
770 '%22',
771 '%3c', // <
772 '%253c', // <
773 '%3e', // >
774 '%0e', // >
775 '%28', // (
776 '%29', // )
777 '%2528', // (
778 '%26', // &
779 '%24', // $
780 '%3f', // ?
781 '%3b', // ;
782 '%3d' // =
783 );
Barry Mienydd671972010-10-04 16:33:58 +0200784
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300785 return stripslashes(str_replace($bad, '', $filename));
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 }
787
788 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200789
Derek Allard2067d1a2008-11-13 22:59:24 +0000790 /**
791 * Limit the File Name Length
792 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000793 * @param string
794 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200795 */
Greg Aker58fdee82010-11-10 15:07:09 -0600796 public function limit_filename_length($filename, $length)
Derek Allard2067d1a2008-11-13 22:59:24 +0000797 {
798 if (strlen($filename) < $length)
799 {
800 return $filename;
801 }
Barry Mienydd671972010-10-04 16:33:58 +0200802
Derek Allard2067d1a2008-11-13 22:59:24 +0000803 $ext = '';
804 if (strpos($filename, '.') !== FALSE)
805 {
806 $parts = explode('.', $filename);
807 $ext = '.'.array_pop($parts);
808 $filename = implode('.', $parts);
809 }
Barry Mienydd671972010-10-04 16:33:58 +0200810
Derek Allard2067d1a2008-11-13 22:59:24 +0000811 return substr($filename, 0, ($length - strlen($ext))).$ext;
812 }
813
814 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200815
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 /**
817 * Runs the file through the XSS clean function
818 *
819 * This prevents people from embedding malicious code in their files.
820 * I'm not sure that it won't negatively affect certain files in unexpected ways,
821 * but so far I haven't found that it causes trouble.
822 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000823 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200824 */
Greg Aker58fdee82010-11-10 15:07:09 -0600825 public function do_xss_clean()
Barry Mienydd671972010-10-04 16:33:58 +0200826 {
Derek Jonese9d723f2010-07-12 10:10:59 -0500827 $file = $this->file_temp;
Barry Mienydd671972010-10-04 16:33:58 +0200828
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300829 if (filesize($file) == 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000830 {
831 return FALSE;
832 }
Barry Mienydd671972010-10-04 16:33:58 +0200833
Andrey Andreevc839d282012-06-07 14:35:27 +0300834 if (memory_get_usage() && ($memory_limit = ini_get('memory_limit')))
Greg Akerf82e51c2010-04-14 19:33:50 -0500835 {
Andrey Andreevc839d282012-06-07 14:35:27 +0300836 $memory_limit *= 1024 * 1024;
Barry Mienydd671972010-10-04 16:33:58 +0200837
Greg Akerc78a2592010-06-09 11:45:32 -0500838 // There was a bug/behavioural change in PHP 5.2, where numbers over one million get output
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300839 // into scientific notation. number_format() ensures this number is an integer
Greg Akerc78a2592010-06-09 11:45:32 -0500840 // http://bugs.php.net/bug.php?id=43053
Barry Mienydd671972010-10-04 16:33:58 +0200841
Andrey Andreevc839d282012-06-07 14:35:27 +0300842 $memory_limit = number_format(ceil(filesize($file) + $memory_limit), 0, '.', '');
Barry Mienydd671972010-10-04 16:33:58 +0200843
Andrey Andreevc839d282012-06-07 14:35:27 +0300844 ini_set('memory_limit', $memory_limit); // When an integer is used, the value is measured in bytes. - PHP.net
Greg Akerf82e51c2010-04-14 19:33:50 -0500845 }
846
847 // If the file being uploaded is an image, then we should have no problem with XSS attacks (in theory), but
848 // IE can be fooled into mime-type detecting a malformed image as an html file, thus executing an XSS attack on anyone
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300849 // using IE who looks at the image. It does this by inspecting the first 255 bytes of an image. To get around this
850 // CI will itself look at the first 255 bytes of an image to determine its relative safety. This can save a lot of
Barry Mienydd671972010-10-04 16:33:58 +0200851 // processor power and time if it is actually a clean image, as it will be in nearly all instances _except_ an
Greg Akerf82e51c2010-04-14 19:33:50 -0500852 // attempted XSS attack.
853
854 if (function_exists('getimagesize') && @getimagesize($file) !== FALSE)
855 {
Barry Mienydd671972010-10-04 16:33:58 +0200856 if (($file = @fopen($file, 'rb')) === FALSE) // "b" to force binary
857 {
Greg Akerf82e51c2010-04-14 19:33:50 -0500858 return FALSE; // Couldn't open the file, return FALSE
Barry Mienydd671972010-10-04 16:33:58 +0200859 }
Greg Akerf82e51c2010-04-14 19:33:50 -0500860
Barry Mienydd671972010-10-04 16:33:58 +0200861 $opening_bytes = fread($file, 256);
862 fclose($file);
Greg Akerf82e51c2010-04-14 19:33:50 -0500863
864 // These are known to throw IE into mime-type detection chaos
865 // <a, <body, <head, <html, <img, <plaintext, <pre, <script, <table, <title
866 // title is basically just in SVG, but we filter it anyhow
867
Andrey Andreevc839d282012-06-07 14:35:27 +0300868 // if its an image or no "triggers" detected in the first 256 bytes - we're good
869 return ! preg_match('/<(a|body|head|html|img|plaintext|pre|script|table|title)[\s>]/i', $opening_bytes);
Greg Akerf82e51c2010-04-14 19:33:50 -0500870 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000871
872 if (($data = @file_get_contents($file)) === FALSE)
873 {
874 return FALSE;
875 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000876
Greg Akerf82e51c2010-04-14 19:33:50 -0500877 $CI =& get_instance();
Greg Akerf82e51c2010-04-14 19:33:50 -0500878 return $CI->security->xss_clean($data, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 }
Barry Mienydd671972010-10-04 16:33:58 +0200880
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200882
Derek Allard2067d1a2008-11-13 22:59:24 +0000883 /**
884 * Set an error message
885 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 * @param string
887 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200888 */
Greg Aker58fdee82010-11-10 15:07:09 -0600889 public function set_error($msg)
Derek Allard2067d1a2008-11-13 22:59:24 +0000890 {
Barry Mienydd671972010-10-04 16:33:58 +0200891 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000892 $CI->lang->load('upload');
Barry Mienydd671972010-10-04 16:33:58 +0200893
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 if (is_array($msg))
895 {
896 foreach ($msg as $val)
897 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100898 $msg = ($CI->lang->line($val) === FALSE) ? $val : $CI->lang->line($val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 $this->error_msg[] = $msg;
900 log_message('error', $msg);
Barry Mienydd671972010-10-04 16:33:58 +0200901 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 }
903 else
904 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100905 $msg = ($CI->lang->line($msg) === FALSE) ? $msg : $CI->lang->line($msg);
Derek Allard2067d1a2008-11-13 22:59:24 +0000906 $this->error_msg[] = $msg;
907 log_message('error', $msg);
908 }
909 }
Barry Mienydd671972010-10-04 16:33:58 +0200910
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200912
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 /**
914 * Display the error message
915 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 * @param string
917 * @param string
918 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200919 */
Greg Aker58fdee82010-11-10 15:07:09 -0600920 public function display_errors($open = '<p>', $close = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300922 return (count($this->error_msg) > 0) ? $open.implode($close.$open, $this->error_msg).$close : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000923 }
Barry Mienydd671972010-10-04 16:33:58 +0200924
Derek Allard2067d1a2008-11-13 22:59:24 +0000925 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200926
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 /**
928 * List of Mime Types
929 *
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300930 * This is a list of mime types. We use it to validate
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 * the "allowed types" set by the developer
932 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 * @param string
934 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200935 */
Greg Aker58fdee82010-11-10 15:07:09 -0600936 public function mimes_types($mime)
Derek Allard2067d1a2008-11-13 22:59:24 +0000937 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300938 return isset($this->mimes[$mime]) ? $this->mimes[$mime] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 }
940
941 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200942
Derek Allard2067d1a2008-11-13 22:59:24 +0000943 /**
944 * Prep Filename
945 *
946 * Prevents possible script execution from Apache's handling of files multiple extensions
947 * http://httpd.apache.org/docs/1.3/mod/mod_mime.html#multipleext
948 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 * @param string
950 * @return string
951 */
Greg Aker58fdee82010-11-10 15:07:09 -0600952 protected function _prep_filename($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000953 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100954 if (strpos($filename, '.') === FALSE OR $this->allowed_types === '*')
Derek Allard2067d1a2008-11-13 22:59:24 +0000955 {
956 return $filename;
957 }
Derek Allard616dab82009-02-16 15:44:32 +0000958
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 $parts = explode('.', $filename);
960 $ext = array_pop($parts);
961 $filename = array_shift($parts);
Derek Allard616dab82009-02-16 15:44:32 +0000962
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 foreach ($parts as $part)
964 {
Derek Jonese9d723f2010-07-12 10:10:59 -0500965 if ( ! in_array(strtolower($part), $this->allowed_types) OR $this->mimes_types(strtolower($part)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000966 {
967 $filename .= '.'.$part.'_';
968 }
969 else
970 {
971 $filename .= '.'.$part;
972 }
973 }
Derek Allardd70b0642009-02-16 13:51:42 +0000974
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300975 return $filename.'.'.$ext;
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 }
977
978 // --------------------------------------------------------------------
979
Andrey Andreev3a3c9472011-09-24 14:25:33 +0300980 /**
981 * File MIME type
982 *
983 * Detects the (actual) MIME type of the uploaded file, if possible.
984 * The input array is expected to be $_FILES[$field]
985 *
986 * @param array
987 * @return void
988 */
989 protected function _file_mime_type($file)
990 {
Andrey Andreeva49e3812011-12-09 13:05:22 +0200991 // We'll need this to validate the MIME info string (e.g. text/plain; charset=us-ascii)
Andrey Andreevf7aed122011-12-13 11:01:06 +0200992 $regexp = '/^([a-z\-]+\/[a-z0-9\-\.\+]+)(;\s.+)?$/';
Andrey Andreeva49e3812011-12-09 13:05:22 +0200993
994 /* Fileinfo extension - most reliable method
995 *
996 * Unfortunately, prior to PHP 5.3 - it's only available as a PECL extension and the
997 * more convenient FILEINFO_MIME_TYPE flag doesn't exist.
998 */
999 if (function_exists('finfo_file'))
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001000 {
Andrey Andreevd60e7002012-06-17 00:03:03 +03001001 $finfo = @finfo_open(FILEINFO_MIME);
Andrey Andreeva49e3812011-12-09 13:05:22 +02001002 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 +03001003 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001004 $mime = @finfo_file($finfo, $file['tmp_name']);
1005 finfo_close($finfo);
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001006
1007 /* According to the comments section of the PHP manual page,
1008 * it is possible that this function returns an empty string
Andrey Andreev2cec39f2011-09-24 22:59:37 +03001009 * for some files (e.g. if they don't exist in the magic MIME database)
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001010 */
Andrey Andreeva49e3812011-12-09 13:05:22 +02001011 if (is_string($mime) && preg_match($regexp, $mime, $matches))
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001012 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001013 $this->file_type = $matches[1];
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001014 return;
1015 }
1016 }
1017 }
1018
Andrey Andreeva49e3812011-12-09 13:05:22 +02001019 /* This is an ugly hack, but UNIX-type systems provide a "native" way to detect the file type,
1020 * which is still more secure than depending on the value of $_FILES[$field]['type'], and as it
1021 * was reported in issue #750 (https://github.com/EllisLab/CodeIgniter/issues/750) - it's better
1022 * than mime_content_type() as well, hence the attempts to try calling the command line with
1023 * three different functions.
Andrey Andreev6700b932011-09-24 14:25:33 +03001024 *
1025 * Notes:
Andrey Andreev59654312011-12-02 14:28:54 +02001026 * - the DIRECTORY_SEPARATOR comparison ensures that we're not on a Windows system
Andrey Andreeva49e3812011-12-09 13:05:22 +02001027 * - many system admins would disable the exec(), shell_exec(), popen() and similar functions
1028 * due to security concerns, hence the function_exists() checks
Andrey Andreev6700b932011-09-24 14:25:33 +03001029 */
Andrey Andreeva49e3812011-12-09 13:05:22 +02001030 if (DIRECTORY_SEPARATOR !== '\\')
Andrey Andreev6700b932011-09-24 14:25:33 +03001031 {
Andrey Andreevd60e7002012-06-17 00:03:03 +03001032 $cmd = function_exists('escapeshellarg')
1033 ? 'file --brief --mime '.escapeshellarg($file['tmp_name']).' 2>&1'
1034 : 'file --brief --mime '.$file['tmp_name'].' 2>&1';
Andrey Andreeva49e3812011-12-09 13:05:22 +02001035
1036 if (function_exists('exec'))
Andrey Andreev6700b932011-09-24 14:25:33 +03001037 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001038 /* This might look confusing, as $mime is being populated with all of the output when set in the second parameter.
1039 * However, we only neeed the last line, which is the actual return value of exec(), and as such - it overwrites
1040 * anything that could already be set for $mime previously. This effectively makes the second parameter a dummy
1041 * value, which is only put to allow us to get the return status code.
1042 */
1043 $mime = @exec($cmd, $mime, $return_status);
1044 if ($return_status === 0 && is_string($mime) && preg_match($regexp, $mime, $matches))
1045 {
1046 $this->file_type = $matches[1];
1047 return;
1048 }
1049 }
1050
1051 if ( (bool) @ini_get('safe_mode') === FALSE && function_exists('shell_exec'))
1052 {
1053 $mime = @shell_exec($cmd);
1054 if (strlen($mime) > 0)
1055 {
1056 $mime = explode("\n", trim($mime));
1057 if (preg_match($regexp, $mime[(count($mime) - 1)], $matches))
1058 {
1059 $this->file_type = $matches[1];
1060 return;
1061 }
1062 }
1063 }
1064
1065 if (function_exists('popen'))
1066 {
1067 $proc = @popen($cmd, 'r');
1068 if (is_resource($proc))
1069 {
tubalmartin010f1f42012-03-03 22:24:31 +01001070 $mime = @fread($proc, 512);
Andrey Andreeva49e3812011-12-09 13:05:22 +02001071 @pclose($proc);
1072 if ($mime !== FALSE)
1073 {
1074 $mime = explode("\n", trim($mime));
1075 if (preg_match($regexp, $mime[(count($mime) - 1)], $matches))
1076 {
1077 $this->file_type = $matches[1];
1078 return;
1079 }
1080 }
1081 }
1082 }
1083 }
1084
1085 // Fall back to the deprecated mime_content_type(), if available (still better than $_FILES[$field]['type'])
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001086 if (function_exists('mime_content_type'))
1087 {
1088 $this->file_type = @mime_content_type($file['tmp_name']);
Andrey Andreeva49e3812011-12-09 13:05:22 +02001089 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 +03001090 {
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001091 return;
1092 }
1093 }
1094
1095 $this->file_type = $file['type'];
1096 }
1097
Derek Allard2067d1a2008-11-13 22:59:24 +00001098}
Derek Allard2067d1a2008-11-13 22:59:24 +00001099
1100/* End of file Upload.php */
Andrey Andreev56454792012-05-17 14:32:19 +03001101/* Location: ./system/libraries/Upload.php */