Andrey Andreev | 6ca407e | 2012-03-01 15:33:21 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | 3a45957 | 2011-12-21 11:23:11 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 3a45957 | 2011-12-21 11:23:11 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | /** |
| 29 | * File Uploading Class |
| 30 | * |
| 31 | * @package CodeIgniter |
| 32 | * @subpackage Libraries |
| 33 | * @category Uploads |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 34 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 35 | * @link http://codeigniter.com/user_guide/libraries/file_uploading.html |
| 36 | */ |
| 37 | class CI_Upload { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 38 | |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 39 | public $max_size = 0; |
| 40 | public $max_width = 0; |
| 41 | public $max_height = 0; |
| 42 | public $max_filename = 0; |
Adam Jackett | ccbbea1 | 2011-08-21 16:19:11 -0400 | [diff] [blame] | 43 | public $max_filename_increment = 100; |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 44 | 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 65 | |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 66 | protected $_file_name_override = ''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 67 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 68 | /** |
| 69 | * Constructor |
| 70 | * |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 71 | * @param array |
| 72 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 73 | */ |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 74 | public function __construct($props = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 75 | { |
| 76 | if (count($props) > 0) |
| 77 | { |
| 78 | $this->initialize($props); |
| 79 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 80 | |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 81 | log_message('debug', 'Upload Class Initialized'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 82 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 83 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 84 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 85 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 86 | /** |
| 87 | * Initialize preferences |
| 88 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 89 | * @param array |
| 90 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 91 | */ |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 92 | public function initialize($config = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 93 | { |
| 94 | $defaults = array( |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 95 | '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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 122 | |
| 123 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 124 | 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 136 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 137 | } |
| 138 | else |
| 139 | { |
| 140 | $this->$key = $val; |
| 141 | } |
| 142 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 143 | |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame] | 144 | // 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 147 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 148 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 149 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 150 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 151 | /** |
| 152 | * Perform the file upload |
| 153 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 154 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 155 | */ |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 156 | public function do_upload($field = 'userfile') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 157 | { |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 158 | // Is $_FILES[$field] set? If not, no reason to continue. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 159 | if ( ! isset($_FILES[$field])) |
| 160 | { |
| 161 | $this->set_error('upload_no_file_selected'); |
| 162 | return FALSE; |
| 163 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 164 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 165 | // 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 Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 177 | switch ($error) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 178 | { |
| 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 186 | $this->set_error('upload_file_partial'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 187 | break; |
| 188 | case 4: // UPLOAD_ERR_NO_FILE |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 189 | $this->set_error('upload_no_file_selected'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 190 | 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 Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 200 | default: |
| 201 | $this->set_error('upload_no_file_selected'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 202 | break; |
| 203 | } |
| 204 | |
| 205 | return FALSE; |
| 206 | } |
| 207 | |
| 208 | // Set the uploaded data as class variables |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 209 | $this->file_temp = $_FILES[$field]['tmp_name']; |
| 210 | $this->file_size = $_FILES[$field]['size']; |
Andrey Andreev | 3a3c947 | 2011-09-24 14:25:33 +0300 | [diff] [blame] | 211 | $this->_file_mime_type($_FILES[$field]); |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 212 | $this->file_type = preg_replace('/^(.+?);.*$/', '\\1', $this->file_type); |
Derek Jones | 616fb02 | 2010-04-22 16:52:18 -0500 | [diff] [blame] | 213 | $this->file_type = strtolower(trim(stripslashes($this->file_type), '"')); |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame] | 214 | $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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 217 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 218 | // 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 Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame] | 225 | // if we're overriding, let's now make sure the new name and type is allowed |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame^] | 226 | if ($this->_file_name_override !== '') |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame] | 227 | { |
| 228 | $this->file_name = $this->_prep_filename($this->_file_name_override); |
Phil Sturgeon | 1e74da2 | 2010-12-15 10:45:06 +0000 | [diff] [blame] | 229 | |
| 230 | // If no extension was provided in the file_name config item, use the uploaded one |
Pascal Kriete | 14287f3 | 2011-02-14 13:39:34 -0500 | [diff] [blame] | 231 | if (strpos($this->_file_name_override, '.') === FALSE) |
Phil Sturgeon | 1e74da2 | 2010-12-15 10:45:06 +0000 | [diff] [blame] | 232 | { |
| 233 | $this->file_name .= $this->file_ext; |
| 234 | } |
Phil Sturgeon | 1e74da2 | 2010-12-15 10:45:06 +0000 | [diff] [blame] | 235 | else |
| 236 | { |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 237 | // An extension was provided, lets have it! |
| 238 | $this->file_ext = $this->get_extension($this->_file_name_override); |
Phil Sturgeon | 1e74da2 | 2010-12-15 10:45:06 +0000 | [diff] [blame] | 239 | } |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame] | 240 | |
| 241 | if ( ! $this->is_allowed_filetype(TRUE)) |
| 242 | { |
| 243 | $this->set_error('upload_invalid_filetype'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 244 | return FALSE; |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame] | 245 | } |
| 246 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 247 | |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame] | 248 | // 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 254 | // 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 271 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 272 | // Truncate the file name if it's too long |
| 273 | if ($this->max_filename > 0) |
| 274 | { |
| 275 | $this->file_name = $this->limit_filename_length($this->file_name, $this->max_filename); |
| 276 | } |
| 277 | |
| 278 | // Remove white spaces in the name |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame^] | 279 | if ($this->remove_spaces === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 280 | { |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 281 | $this->file_name = preg_replace('/\s+/', '_', $this->file_name); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /* |
| 285 | * Validate the file name |
| 286 | * This function appends an number onto the end of |
| 287 | * the file if one with the same name already exists. |
| 288 | * If it returns false there was a problem. |
| 289 | */ |
| 290 | $this->orig_name = $this->file_name; |
| 291 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame^] | 292 | if ($this->overwrite === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 293 | { |
| 294 | $this->file_name = $this->set_filename($this->upload_path, $this->file_name); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 295 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 296 | if ($this->file_name === FALSE) |
| 297 | { |
| 298 | return FALSE; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | /* |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame] | 303 | * Run the file through the XSS hacking filter |
| 304 | * This helps prevent malicious code from being |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 305 | * embedded within a file. Scripts can easily |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame] | 306 | * be disguised as images or other file types. |
| 307 | */ |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 308 | if ($this->xss_clean && $this->do_xss_clean() === FALSE) |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame] | 309 | { |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 310 | $this->set_error('upload_unable_to_write_file'); |
| 311 | return FALSE; |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | /* |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 315 | * Move the file to the final destination |
| 316 | * To deal with different server configurations |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 317 | * we'll attempt to use copy() first. If that fails |
| 318 | * we'll use move_uploaded_file(). One of the two should |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 319 | * 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 325 | $this->set_error('upload_destination_error'); |
| 326 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 327 | } |
| 328 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 329 | |
| 330 | /* |
| 331 | * Set the finalized image dimensions |
| 332 | * This sets the image width/height (assuming the |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 333 | * file was an image). We use this information |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 334 | * in the "data" function. |
| 335 | */ |
| 336 | $this->set_image_properties($this->upload_path.$this->file_name); |
| 337 | |
| 338 | return TRUE; |
| 339 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 340 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 341 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 342 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 343 | /** |
| 344 | * Finalized Data Array |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 345 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 346 | * Returns an associative array containing all of the information |
| 347 | * related to the upload, allowing the developer easy access in one array. |
| 348 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 349 | * @return array |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 350 | */ |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 351 | public function data() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 352 | { |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 353 | 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 369 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 370 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 371 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 372 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 373 | /** |
| 374 | * Set Upload Path |
| 375 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 376 | * @param string |
| 377 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 378 | */ |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 379 | public function set_upload_path($path) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 380 | { |
| 381 | // Make sure it has a trailing slash |
| 382 | $this->upload_path = rtrim($path, '/').'/'; |
| 383 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 384 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 385 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 386 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 387 | /** |
| 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 394 | * @param string |
| 395 | * @param string |
| 396 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 397 | */ |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 398 | public function set_filename($path, $filename) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 399 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame^] | 400 | if ($this->encrypt_name === TRUE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 401 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 402 | mt_srand(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 403 | $filename = md5(uniqid(mt_rand())).$this->file_ext; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 404 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 405 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 406 | if ( ! file_exists($path.$filename)) |
| 407 | { |
| 408 | return $filename; |
| 409 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 410 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 411 | $filename = str_replace($this->file_ext, '', $filename); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 412 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 413 | $new_filename = ''; |
Adam Jackett | ccbbea1 | 2011-08-21 16:19:11 -0400 | [diff] [blame] | 414 | for ($i = 1; $i < $this->max_filename_increment; $i++) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 415 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 416 | if ( ! file_exists($path.$filename.$i.$this->file_ext)) |
| 417 | { |
| 418 | $new_filename = $filename.$i.$this->file_ext; |
| 419 | break; |
| 420 | } |
| 421 | } |
| 422 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame^] | 423 | if ($new_filename === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 424 | { |
| 425 | $this->set_error('upload_bad_filename'); |
| 426 | return FALSE; |
| 427 | } |
| 428 | else |
| 429 | { |
| 430 | return $new_filename; |
| 431 | } |
| 432 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 433 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 434 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 435 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 436 | /** |
| 437 | * Set Maximum File Size |
| 438 | * |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 439 | * @param int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 440 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 441 | */ |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 442 | public function set_max_filesize($n) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 443 | { |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 444 | $this->max_size = ((int) $n < 0) ? 0 : (int) $n; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 445 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 446 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 447 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 448 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 449 | /** |
| 450 | * Set Maximum File Name Length |
| 451 | * |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 452 | * @param int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 453 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 454 | */ |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 455 | public function set_max_filename($n) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 456 | { |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 457 | $this->max_filename = ((int) $n < 0) ? 0 : (int) $n; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 461 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 462 | /** |
| 463 | * Set Maximum Image Width |
| 464 | * |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 465 | * @param int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 466 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 467 | */ |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 468 | public function set_max_width($n) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 469 | { |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 470 | $this->max_width = ((int) $n < 0) ? 0 : (int) $n; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 471 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 472 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 473 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 474 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 475 | /** |
| 476 | * Set Maximum Image Height |
| 477 | * |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 478 | * @param int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 479 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 480 | */ |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 481 | public function set_max_height($n) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 482 | { |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 483 | $this->max_height = ((int) $n < 0) ? 0 : (int) $n; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 484 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 485 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 486 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 487 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 488 | /** |
| 489 | * Set Allowed File Types |
| 490 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 491 | * @param string |
| 492 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 493 | */ |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 494 | public function set_allowed_types($types) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 495 | { |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 496 | if ( ! is_array($types) && $types === '*') |
Derek Jones | e12f64e | 2010-03-02 22:55:08 -0600 | [diff] [blame] | 497 | { |
| 498 | $this->allowed_types = '*'; |
| 499 | return; |
| 500 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 501 | $this->allowed_types = explode('|', $types); |
| 502 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 503 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 504 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 505 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 506 | /** |
| 507 | * Set Image Properties |
| 508 | * |
| 509 | * Uses GD to determine the width/height/type of image |
| 510 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 511 | * @param string |
| 512 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 513 | */ |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 514 | public function set_image_properties($path = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 515 | { |
| 516 | if ( ! $this->is_image()) |
| 517 | { |
| 518 | return; |
| 519 | } |
| 520 | |
| 521 | if (function_exists('getimagesize')) |
| 522 | { |
| 523 | if (FALSE !== ($D = @getimagesize($path))) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 524 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 525 | $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png'); |
| 526 | |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 527 | $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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 534 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 535 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 536 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 537 | /** |
| 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 543 | * @param bool |
| 544 | * @return void |
| 545 | */ |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 546 | public function set_xss_clean($flag = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 547 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame^] | 548 | $this->xss_clean = ($flag === TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 549 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 550 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 551 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 552 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 553 | /** |
| 554 | * Validate the image |
| 555 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 556 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 557 | */ |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 558 | public function is_image() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 559 | { |
| 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 Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 563 | $png_mimes = array('image/x-png'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 564 | $jpeg_mimes = array('image/jpg', 'image/jpe', 'image/jpeg', 'image/pjpeg'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 565 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 566 | if (in_array($this->file_type, $png_mimes)) |
| 567 | { |
| 568 | $this->file_type = 'image/png'; |
| 569 | } |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 570 | elseif (in_array($this->file_type, $jpeg_mimes)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 571 | { |
| 572 | $this->file_type = 'image/jpeg'; |
| 573 | } |
| 574 | |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 575 | $img_mimes = array('image/gif', 'image/jpeg', 'image/png'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 576 | |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 577 | return in_array($this->file_type, $img_mimes, TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 578 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 579 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 580 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 581 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 582 | /** |
| 583 | * Verify that the filetype is allowed |
| 584 | * |
Andrey Andreev | 6ca407e | 2012-03-01 15:33:21 +0200 | [diff] [blame] | 585 | * @param bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 586 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 587 | */ |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 588 | public function is_allowed_filetype($ignore_mime = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 589 | { |
Andrey Andreev | 6ca407e | 2012-03-01 15:33:21 +0200 | [diff] [blame] | 590 | if ($this->allowed_types === '*') |
Derek Jones | e12f64e | 2010-03-02 22:55:08 -0600 | [diff] [blame] | 591 | { |
| 592 | return TRUE; |
| 593 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 594 | |
Andrey Andreev | 6ca407e | 2012-03-01 15:33:21 +0200 | [diff] [blame] | 595 | if ( ! is_array($this->allowed_types) OR count($this->allowed_types) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 596 | { |
| 597 | $this->set_error('upload_no_file_types'); |
| 598 | return FALSE; |
| 599 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 600 | |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame] | 601 | $ext = strtolower(ltrim($this->file_ext, '.')); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 602 | |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame] | 603 | if ( ! in_array($ext, $this->allowed_types)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 604 | { |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame] | 605 | return FALSE; |
| 606 | } |
Derek Jones | afa282f | 2009-02-10 17:11:52 +0000 | [diff] [blame] | 607 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 608 | // Images get some additional checks |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame] | 609 | $image_types = array('gif', 'jpg', 'jpeg', 'png', 'jpe'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 610 | |
Andrey Andreev | 6ca407e | 2012-03-01 15:33:21 +0200 | [diff] [blame] | 611 | if (in_array($ext, $image_types) && @getimagesize($this->file_temp) === FALSE) |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame] | 612 | { |
Andrey Andreev | 6ca407e | 2012-03-01 15:33:21 +0200 | [diff] [blame] | 613 | return FALSE; |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame] | 614 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 615 | |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame] | 616 | if ($ignore_mime === TRUE) |
| 617 | { |
| 618 | return TRUE; |
| 619 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 620 | |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame] | 621 | $mime = $this->mimes_types($ext); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 622 | |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 623 | if (is_array($mime) && in_array($this->file_type, $mime, TRUE)) |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame] | 624 | { |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 625 | return TRUE; |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame] | 626 | } |
Andrey Andreev | 6ca407e | 2012-03-01 15:33:21 +0200 | [diff] [blame] | 627 | elseif ($mime === $this->file_type) |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame] | 628 | { |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 629 | return TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 630 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 631 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 632 | return FALSE; |
| 633 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 634 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 635 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 636 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 637 | /** |
| 638 | * Verify that the file is within the allowed size |
| 639 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 640 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 641 | */ |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 642 | public function is_allowed_filesize() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 643 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame^] | 644 | return ($this->max_size === 0 OR $this->max_size > $this->file_size); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 645 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 646 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 647 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 648 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 649 | /** |
| 650 | * Verify that the image is within the allowed width/height |
| 651 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 652 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 653 | */ |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 654 | public function is_allowed_dimensions() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 655 | { |
| 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 Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 665 | if ($this->max_width > 0 && $D[0] > $this->max_width) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 666 | { |
| 667 | return FALSE; |
| 668 | } |
| 669 | |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 670 | if ($this->max_height > 0 && $D[1] > $this->max_height) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 671 | { |
| 672 | return FALSE; |
| 673 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | return TRUE; |
| 677 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 678 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 679 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 680 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 681 | /** |
| 682 | * Validate Upload Path |
| 683 | * |
| 684 | * Verifies that it is a valid upload path with proper permissions. |
| 685 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 686 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 687 | */ |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 688 | public function validate_upload_path() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 689 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame^] | 690 | if ($this->upload_path === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 691 | { |
| 692 | $this->set_error('upload_no_filepath'); |
| 693 | return FALSE; |
| 694 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 695 | |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 696 | if (function_exists('realpath') && @realpath($this->upload_path) !== FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 697 | { |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 698 | $this->upload_path = str_replace('\\', '/', realpath($this->upload_path)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 699 | } |
| 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 Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 713 | $this->upload_path = preg_replace('/(.+?)\/*$/', '\\1/', $this->upload_path); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 714 | return TRUE; |
| 715 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 716 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 717 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 718 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 719 | /** |
| 720 | * Extract the file extension |
| 721 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 722 | * @param string |
| 723 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 724 | */ |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 725 | public function get_extension($filename) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 726 | { |
| 727 | $x = explode('.', $filename); |
Andrey Andreev | 46d53fb | 2012-05-11 13:42:24 +0300 | [diff] [blame] | 728 | return (count($x) !== 1) ? '.'.end($x) : ''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 729 | } |
| 730 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 731 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 732 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 733 | /** |
| 734 | * Clean the file name for security |
| 735 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 736 | * @param string |
| 737 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 738 | */ |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 739 | public function clean_file_name($filename) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 740 | { |
| 741 | $bad = array( |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 742 | '<!--', '-->', |
| 743 | "'", '"', |
| 744 | '<', '>', |
| 745 | '&', '$', |
| 746 | '=', |
| 747 | ';', |
| 748 | '?', |
| 749 | '/', |
Andrey Andreev | 470805b | 2012-05-24 21:57:21 +0300 | [diff] [blame] | 750 | '!', |
| 751 | '#', |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 752 | '%20', |
| 753 | '%22', |
| 754 | '%3c', // < |
| 755 | '%253c', // < |
| 756 | '%3e', // > |
| 757 | '%0e', // > |
| 758 | '%28', // ( |
| 759 | '%29', // ) |
| 760 | '%2528', // ( |
| 761 | '%26', // & |
| 762 | '%24', // $ |
| 763 | '%3f', // ? |
| 764 | '%3b', // ; |
| 765 | '%3d' // = |
| 766 | ); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 767 | |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 768 | return stripslashes(str_replace($bad, '', $filename)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 772 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 773 | /** |
| 774 | * Limit the File Name Length |
| 775 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 776 | * @param string |
| 777 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 778 | */ |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 779 | public function limit_filename_length($filename, $length) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 780 | { |
| 781 | if (strlen($filename) < $length) |
| 782 | { |
| 783 | return $filename; |
| 784 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 785 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 786 | $ext = ''; |
| 787 | if (strpos($filename, '.') !== FALSE) |
| 788 | { |
| 789 | $parts = explode('.', $filename); |
| 790 | $ext = '.'.array_pop($parts); |
| 791 | $filename = implode('.', $parts); |
| 792 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 793 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 794 | return substr($filename, 0, ($length - strlen($ext))).$ext; |
| 795 | } |
| 796 | |
| 797 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 798 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 799 | /** |
| 800 | * Runs the file through the XSS clean function |
| 801 | * |
| 802 | * This prevents people from embedding malicious code in their files. |
| 803 | * I'm not sure that it won't negatively affect certain files in unexpected ways, |
| 804 | * but so far I haven't found that it causes trouble. |
| 805 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 806 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 807 | */ |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 808 | public function do_xss_clean() |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 809 | { |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame] | 810 | $file = $this->file_temp; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 811 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame^] | 812 | if (filesize($file) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 813 | { |
| 814 | return FALSE; |
| 815 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 816 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame^] | 817 | if (function_exists('memory_get_usage') && memory_get_usage() && ini_get('memory_limit') !== '') |
Greg Aker | f82e51c | 2010-04-14 19:33:50 -0500 | [diff] [blame] | 818 | { |
| 819 | $current = ini_get('memory_limit') * 1024 * 1024; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 820 | |
Greg Aker | c78a259 | 2010-06-09 11:45:32 -0500 | [diff] [blame] | 821 | // There was a bug/behavioural change in PHP 5.2, where numbers over one million get output |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 822 | // into scientific notation. number_format() ensures this number is an integer |
Greg Aker | c78a259 | 2010-06-09 11:45:32 -0500 | [diff] [blame] | 823 | // http://bugs.php.net/bug.php?id=43053 |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 824 | |
Greg Aker | c78a259 | 2010-06-09 11:45:32 -0500 | [diff] [blame] | 825 | $new_memory = number_format(ceil(filesize($file) + $current), 0, '.', ''); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 826 | |
Greg Aker | c78a259 | 2010-06-09 11:45:32 -0500 | [diff] [blame] | 827 | ini_set('memory_limit', $new_memory); // When an integer is used, the value is measured in bytes. - PHP.net |
Greg Aker | f82e51c | 2010-04-14 19:33:50 -0500 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | // If the file being uploaded is an image, then we should have no problem with XSS attacks (in theory), but |
| 831 | // IE can be fooled into mime-type detecting a malformed image as an html file, thus executing an XSS attack on anyone |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 832 | // using IE who looks at the image. It does this by inspecting the first 255 bytes of an image. To get around this |
| 833 | // CI will itself look at the first 255 bytes of an image to determine its relative safety. This can save a lot of |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 834 | // processor power and time if it is actually a clean image, as it will be in nearly all instances _except_ an |
Greg Aker | f82e51c | 2010-04-14 19:33:50 -0500 | [diff] [blame] | 835 | // attempted XSS attack. |
| 836 | |
| 837 | if (function_exists('getimagesize') && @getimagesize($file) !== FALSE) |
| 838 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 839 | if (($file = @fopen($file, 'rb')) === FALSE) // "b" to force binary |
| 840 | { |
Greg Aker | f82e51c | 2010-04-14 19:33:50 -0500 | [diff] [blame] | 841 | return FALSE; // Couldn't open the file, return FALSE |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 842 | } |
Greg Aker | f82e51c | 2010-04-14 19:33:50 -0500 | [diff] [blame] | 843 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 844 | $opening_bytes = fread($file, 256); |
| 845 | fclose($file); |
Greg Aker | f82e51c | 2010-04-14 19:33:50 -0500 | [diff] [blame] | 846 | |
| 847 | // These are known to throw IE into mime-type detection chaos |
| 848 | // <a, <body, <head, <html, <img, <plaintext, <pre, <script, <table, <title |
| 849 | // title is basically just in SVG, but we filter it anyhow |
| 850 | |
| 851 | if ( ! preg_match('/<(a|body|head|html|img|plaintext|pre|script|table|title)[\s>]/i', $opening_bytes)) |
| 852 | { |
| 853 | return TRUE; // its an image, no "triggers" detected in the first 256 bytes, we're good |
| 854 | } |
Wes Baker | bb2c83b | 2012-05-04 18:44:24 -0400 | [diff] [blame] | 855 | else |
| 856 | { |
| 857 | return FALSE; |
| 858 | } |
Greg Aker | f82e51c | 2010-04-14 19:33:50 -0500 | [diff] [blame] | 859 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 860 | |
| 861 | if (($data = @file_get_contents($file)) === FALSE) |
| 862 | { |
| 863 | return FALSE; |
| 864 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 865 | |
Greg Aker | f82e51c | 2010-04-14 19:33:50 -0500 | [diff] [blame] | 866 | $CI =& get_instance(); |
Greg Aker | f82e51c | 2010-04-14 19:33:50 -0500 | [diff] [blame] | 867 | return $CI->security->xss_clean($data, TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 868 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 869 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 870 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 871 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 872 | /** |
| 873 | * Set an error message |
| 874 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 875 | * @param string |
| 876 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 877 | */ |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 878 | public function set_error($msg) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 879 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 880 | $CI =& get_instance(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 881 | $CI->lang->load('upload'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 882 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 883 | if (is_array($msg)) |
| 884 | { |
| 885 | foreach ($msg as $val) |
| 886 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame^] | 887 | $msg = ($CI->lang->line($val) === FALSE) ? $val : $CI->lang->line($val); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 888 | $this->error_msg[] = $msg; |
| 889 | log_message('error', $msg); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 890 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 891 | } |
| 892 | else |
| 893 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame^] | 894 | $msg = ($CI->lang->line($msg) === FALSE) ? $msg : $CI->lang->line($msg); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 895 | $this->error_msg[] = $msg; |
| 896 | log_message('error', $msg); |
| 897 | } |
| 898 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 899 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 900 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 901 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 902 | /** |
| 903 | * Display the error message |
| 904 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 905 | * @param string |
| 906 | * @param string |
| 907 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 908 | */ |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 909 | public function display_errors($open = '<p>', $close = '</p>') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 910 | { |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 911 | return (count($this->error_msg) > 0) ? $open.implode($close.$open, $this->error_msg).$close : ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 912 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 913 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 914 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 915 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 916 | /** |
| 917 | * List of Mime Types |
| 918 | * |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 919 | * This is a list of mime types. We use it to validate |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 920 | * the "allowed types" set by the developer |
| 921 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 922 | * @param string |
| 923 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 924 | */ |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 925 | public function mimes_types($mime) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 926 | { |
| 927 | global $mimes; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 928 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame^] | 929 | if (count($this->mimes) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 930 | { |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 931 | if (defined('ENVIRONMENT') && is_file(APPPATH.'config/'.ENVIRONMENT.'/mimes.php')) |
Greg Aker | d96f882 | 2011-12-27 16:23:47 -0600 | [diff] [blame] | 932 | { |
| 933 | include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'); |
| 934 | } |
| 935 | elseif (is_file(APPPATH.'config/mimes.php')) |
| 936 | { |
Michiel Vugteveen | b578326 | 2012-02-29 15:40:28 +0100 | [diff] [blame] | 937 | include(APPPATH.'config/mimes.php'); |
Greg Aker | d96f882 | 2011-12-27 16:23:47 -0600 | [diff] [blame] | 938 | } |
| 939 | else |
bubbafoley | 0ea0414 | 2011-03-17 14:55:41 -0500 | [diff] [blame] | 940 | { |
Eric Barnes | fdd5b11 | 2011-03-21 21:28:58 -0400 | [diff] [blame] | 941 | return FALSE; |
bubbafoley | 0ea0414 | 2011-03-17 14:55:41 -0500 | [diff] [blame] | 942 | } |
Eric Barnes | 9280834 | 2011-03-18 09:02:37 -0400 | [diff] [blame] | 943 | |
Eric Barnes | fdd5b11 | 2011-03-21 21:28:58 -0400 | [diff] [blame] | 944 | $this->mimes = $mimes; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 945 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 946 | |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 947 | return isset($this->mimes[$mime]) ? $this->mimes[$mime] : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 951 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 952 | /** |
| 953 | * Prep Filename |
| 954 | * |
| 955 | * Prevents possible script execution from Apache's handling of files multiple extensions |
| 956 | * http://httpd.apache.org/docs/1.3/mod/mod_mime.html#multipleext |
| 957 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 958 | * @param string |
| 959 | * @return string |
| 960 | */ |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 961 | protected function _prep_filename($filename) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 962 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame^] | 963 | if (strpos($filename, '.') === FALSE OR $this->allowed_types === '*') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 964 | { |
| 965 | return $filename; |
| 966 | } |
Derek Allard | 616dab8 | 2009-02-16 15:44:32 +0000 | [diff] [blame] | 967 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 968 | $parts = explode('.', $filename); |
| 969 | $ext = array_pop($parts); |
| 970 | $filename = array_shift($parts); |
Derek Allard | 616dab8 | 2009-02-16 15:44:32 +0000 | [diff] [blame] | 971 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 972 | foreach ($parts as $part) |
| 973 | { |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame] | 974 | if ( ! in_array(strtolower($part), $this->allowed_types) OR $this->mimes_types(strtolower($part)) === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 975 | { |
| 976 | $filename .= '.'.$part.'_'; |
| 977 | } |
| 978 | else |
| 979 | { |
| 980 | $filename .= '.'.$part; |
| 981 | } |
| 982 | } |
Derek Allard | d70b064 | 2009-02-16 13:51:42 +0000 | [diff] [blame] | 983 | |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 984 | return $filename.'.'.$ext; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | // -------------------------------------------------------------------- |
| 988 | |
Andrey Andreev | 3a3c947 | 2011-09-24 14:25:33 +0300 | [diff] [blame] | 989 | /** |
| 990 | * File MIME type |
| 991 | * |
| 992 | * Detects the (actual) MIME type of the uploaded file, if possible. |
| 993 | * The input array is expected to be $_FILES[$field] |
| 994 | * |
| 995 | * @param array |
| 996 | * @return void |
| 997 | */ |
| 998 | protected function _file_mime_type($file) |
| 999 | { |
Andrey Andreev | a49e381 | 2011-12-09 13:05:22 +0200 | [diff] [blame] | 1000 | // We'll need this to validate the MIME info string (e.g. text/plain; charset=us-ascii) |
Andrey Andreev | f7aed12 | 2011-12-13 11:01:06 +0200 | [diff] [blame] | 1001 | $regexp = '/^([a-z\-]+\/[a-z0-9\-\.\+]+)(;\s.+)?$/'; |
Andrey Andreev | a49e381 | 2011-12-09 13:05:22 +0200 | [diff] [blame] | 1002 | |
| 1003 | /* Fileinfo extension - most reliable method |
| 1004 | * |
| 1005 | * Unfortunately, prior to PHP 5.3 - it's only available as a PECL extension and the |
| 1006 | * more convenient FILEINFO_MIME_TYPE flag doesn't exist. |
| 1007 | */ |
| 1008 | if (function_exists('finfo_file')) |
Andrey Andreev | 3a3c947 | 2011-09-24 14:25:33 +0300 | [diff] [blame] | 1009 | { |
Andrey Andreev | a49e381 | 2011-12-09 13:05:22 +0200 | [diff] [blame] | 1010 | $finfo = finfo_open(FILEINFO_MIME); |
| 1011 | if (is_resource($finfo)) // It is possible that a FALSE value is returned, if there is no magic MIME database file found on the system |
Andrey Andreev | 3a3c947 | 2011-09-24 14:25:33 +0300 | [diff] [blame] | 1012 | { |
Andrey Andreev | a49e381 | 2011-12-09 13:05:22 +0200 | [diff] [blame] | 1013 | $mime = @finfo_file($finfo, $file['tmp_name']); |
| 1014 | finfo_close($finfo); |
Andrey Andreev | 3a3c947 | 2011-09-24 14:25:33 +0300 | [diff] [blame] | 1015 | |
| 1016 | /* According to the comments section of the PHP manual page, |
| 1017 | * it is possible that this function returns an empty string |
Andrey Andreev | 2cec39f | 2011-09-24 22:59:37 +0300 | [diff] [blame] | 1018 | * for some files (e.g. if they don't exist in the magic MIME database) |
Andrey Andreev | 3a3c947 | 2011-09-24 14:25:33 +0300 | [diff] [blame] | 1019 | */ |
Andrey Andreev | a49e381 | 2011-12-09 13:05:22 +0200 | [diff] [blame] | 1020 | if (is_string($mime) && preg_match($regexp, $mime, $matches)) |
Andrey Andreev | 3a3c947 | 2011-09-24 14:25:33 +0300 | [diff] [blame] | 1021 | { |
Andrey Andreev | a49e381 | 2011-12-09 13:05:22 +0200 | [diff] [blame] | 1022 | $this->file_type = $matches[1]; |
Andrey Andreev | 3a3c947 | 2011-09-24 14:25:33 +0300 | [diff] [blame] | 1023 | return; |
| 1024 | } |
| 1025 | } |
| 1026 | } |
| 1027 | |
Andrey Andreev | a49e381 | 2011-12-09 13:05:22 +0200 | [diff] [blame] | 1028 | /* This is an ugly hack, but UNIX-type systems provide a "native" way to detect the file type, |
| 1029 | * which is still more secure than depending on the value of $_FILES[$field]['type'], and as it |
| 1030 | * was reported in issue #750 (https://github.com/EllisLab/CodeIgniter/issues/750) - it's better |
| 1031 | * than mime_content_type() as well, hence the attempts to try calling the command line with |
| 1032 | * three different functions. |
Andrey Andreev | 6700b93 | 2011-09-24 14:25:33 +0300 | [diff] [blame] | 1033 | * |
| 1034 | * Notes: |
Andrey Andreev | 5965431 | 2011-12-02 14:28:54 +0200 | [diff] [blame] | 1035 | * - the DIRECTORY_SEPARATOR comparison ensures that we're not on a Windows system |
Andrey Andreev | a49e381 | 2011-12-09 13:05:22 +0200 | [diff] [blame] | 1036 | * - many system admins would disable the exec(), shell_exec(), popen() and similar functions |
| 1037 | * due to security concerns, hence the function_exists() checks |
Andrey Andreev | 6700b93 | 2011-09-24 14:25:33 +0300 | [diff] [blame] | 1038 | */ |
Andrey Andreev | a49e381 | 2011-12-09 13:05:22 +0200 | [diff] [blame] | 1039 | if (DIRECTORY_SEPARATOR !== '\\') |
Andrey Andreev | 6700b93 | 2011-09-24 14:25:33 +0300 | [diff] [blame] | 1040 | { |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1041 | $cmd = 'file --brief --mime '.escapeshellarg($file['tmp_name']).' 2>&1'; |
Andrey Andreev | a49e381 | 2011-12-09 13:05:22 +0200 | [diff] [blame] | 1042 | |
| 1043 | if (function_exists('exec')) |
Andrey Andreev | 6700b93 | 2011-09-24 14:25:33 +0300 | [diff] [blame] | 1044 | { |
Andrey Andreev | a49e381 | 2011-12-09 13:05:22 +0200 | [diff] [blame] | 1045 | /* This might look confusing, as $mime is being populated with all of the output when set in the second parameter. |
| 1046 | * However, we only neeed the last line, which is the actual return value of exec(), and as such - it overwrites |
| 1047 | * anything that could already be set for $mime previously. This effectively makes the second parameter a dummy |
| 1048 | * value, which is only put to allow us to get the return status code. |
| 1049 | */ |
| 1050 | $mime = @exec($cmd, $mime, $return_status); |
| 1051 | if ($return_status === 0 && is_string($mime) && preg_match($regexp, $mime, $matches)) |
| 1052 | { |
| 1053 | $this->file_type = $matches[1]; |
| 1054 | return; |
| 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | if ( (bool) @ini_get('safe_mode') === FALSE && function_exists('shell_exec')) |
| 1059 | { |
| 1060 | $mime = @shell_exec($cmd); |
| 1061 | if (strlen($mime) > 0) |
| 1062 | { |
| 1063 | $mime = explode("\n", trim($mime)); |
| 1064 | if (preg_match($regexp, $mime[(count($mime) - 1)], $matches)) |
| 1065 | { |
| 1066 | $this->file_type = $matches[1]; |
| 1067 | return; |
| 1068 | } |
| 1069 | } |
| 1070 | } |
| 1071 | |
| 1072 | if (function_exists('popen')) |
| 1073 | { |
| 1074 | $proc = @popen($cmd, 'r'); |
| 1075 | if (is_resource($proc)) |
| 1076 | { |
tubalmartin | 010f1f4 | 2012-03-03 22:24:31 +0100 | [diff] [blame] | 1077 | $mime = @fread($proc, 512); |
Andrey Andreev | a49e381 | 2011-12-09 13:05:22 +0200 | [diff] [blame] | 1078 | @pclose($proc); |
| 1079 | if ($mime !== FALSE) |
| 1080 | { |
| 1081 | $mime = explode("\n", trim($mime)); |
| 1082 | if (preg_match($regexp, $mime[(count($mime) - 1)], $matches)) |
| 1083 | { |
| 1084 | $this->file_type = $matches[1]; |
| 1085 | return; |
| 1086 | } |
| 1087 | } |
| 1088 | } |
| 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | // Fall back to the deprecated mime_content_type(), if available (still better than $_FILES[$field]['type']) |
Andrey Andreev | 3a3c947 | 2011-09-24 14:25:33 +0300 | [diff] [blame] | 1093 | if (function_exists('mime_content_type')) |
| 1094 | { |
| 1095 | $this->file_type = @mime_content_type($file['tmp_name']); |
Andrey Andreev | a49e381 | 2011-12-09 13:05:22 +0200 | [diff] [blame] | 1096 | if (strlen($this->file_type) > 0) // It's possible that mime_content_type() returns FALSE or an empty string |
Andrey Andreev | 3a3c947 | 2011-09-24 14:25:33 +0300 | [diff] [blame] | 1097 | { |
Andrey Andreev | 3a3c947 | 2011-09-24 14:25:33 +0300 | [diff] [blame] | 1098 | return; |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | $this->file_type = $file['type']; |
| 1103 | } |
| 1104 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1105 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1106 | |
| 1107 | /* End of file Upload.php */ |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1108 | /* Location: ./system/libraries/Upload.php */ |