Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 4.3.2 or newer |
| 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author ExpressionEngine Dev Team |
Derek Jones | 7f3719f | 2010-01-05 13:35:37 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html |
| 11 | * @link http://codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
| 15 | |
| 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * File Uploading Class |
| 20 | * |
| 21 | * @package CodeIgniter |
| 22 | * @subpackage Libraries |
| 23 | * @category Uploads |
| 24 | * @author ExpressionEngine Dev Team |
| 25 | * @link http://codeigniter.com/user_guide/libraries/file_uploading.html |
| 26 | */ |
| 27 | class CI_Upload { |
| 28 | |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame^] | 29 | var $max_size = 0; |
| 30 | var $max_width = 0; |
| 31 | var $max_height = 0; |
| 32 | var $max_filename = 0; |
| 33 | var $allowed_types = ""; |
| 34 | var $file_temp = ""; |
| 35 | var $file_name = ""; |
| 36 | var $orig_name = ""; |
| 37 | var $file_type = ""; |
| 38 | var $file_size = ""; |
| 39 | var $file_ext = ""; |
| 40 | var $upload_path = ""; |
| 41 | var $overwrite = FALSE; |
| 42 | var $encrypt_name = FALSE; |
| 43 | var $is_image = FALSE; |
| 44 | var $image_width = ''; |
| 45 | var $image_height = ''; |
| 46 | var $image_type = ''; |
| 47 | var $image_size_str = ''; |
| 48 | var $error_msg = array(); |
| 49 | var $mimes = array(); |
| 50 | var $remove_spaces = TRUE; |
| 51 | var $xss_clean = FALSE; |
| 52 | var $temp_prefix = "temp_file_"; |
| 53 | var $client_name = ''; |
| 54 | |
| 55 | var $_file_name_override = ''; //@PHP4 (should be private) |
| 56 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 57 | /** |
| 58 | * Constructor |
| 59 | * |
| 60 | * @access public |
| 61 | */ |
| 62 | function CI_Upload($props = array()) |
| 63 | { |
| 64 | if (count($props) > 0) |
| 65 | { |
| 66 | $this->initialize($props); |
| 67 | } |
| 68 | |
| 69 | log_message('debug', "Upload Class Initialized"); |
| 70 | } |
| 71 | |
| 72 | // -------------------------------------------------------------------- |
| 73 | |
| 74 | /** |
| 75 | * Initialize preferences |
| 76 | * |
| 77 | * @access public |
| 78 | * @param array |
| 79 | * @return void |
| 80 | */ |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame^] | 81 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 82 | function initialize($config = array()) |
| 83 | { |
| 84 | $defaults = array( |
| 85 | 'max_size' => 0, |
| 86 | 'max_width' => 0, |
| 87 | 'max_height' => 0, |
| 88 | 'max_filename' => 0, |
| 89 | 'allowed_types' => "", |
| 90 | 'file_temp' => "", |
| 91 | 'file_name' => "", |
| 92 | 'orig_name' => "", |
| 93 | 'file_type' => "", |
| 94 | 'file_size' => "", |
| 95 | 'file_ext' => "", |
| 96 | 'upload_path' => "", |
| 97 | 'overwrite' => FALSE, |
| 98 | 'encrypt_name' => FALSE, |
| 99 | 'is_image' => FALSE, |
| 100 | 'image_width' => '', |
| 101 | 'image_height' => '', |
| 102 | 'image_type' => '', |
| 103 | 'image_size_str' => '', |
| 104 | 'error_msg' => array(), |
| 105 | 'mimes' => array(), |
| 106 | 'remove_spaces' => TRUE, |
| 107 | 'xss_clean' => FALSE, |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame^] | 108 | 'temp_prefix' => "temp_file_", |
| 109 | 'client_name' => '' |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 110 | ); |
| 111 | |
| 112 | |
| 113 | foreach ($defaults as $key => $val) |
| 114 | { |
| 115 | if (isset($config[$key])) |
| 116 | { |
| 117 | $method = 'set_'.$key; |
| 118 | if (method_exists($this, $method)) |
| 119 | { |
| 120 | $this->$method($config[$key]); |
| 121 | } |
| 122 | else |
| 123 | { |
| 124 | $this->$key = $config[$key]; |
| 125 | } |
| 126 | } |
| 127 | else |
| 128 | { |
| 129 | $this->$key = $val; |
| 130 | } |
| 131 | } |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame^] | 132 | |
| 133 | // if a file_name was provided in the config, use it instead of the user input |
| 134 | // supplied file name for all uploads until initialized again |
| 135 | $this->_file_name_override = $this->file_name; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | // -------------------------------------------------------------------- |
| 139 | |
| 140 | /** |
| 141 | * Perform the file upload |
| 142 | * |
| 143 | * @access public |
| 144 | * @return bool |
| 145 | */ |
| 146 | function do_upload($field = 'userfile') |
| 147 | { |
| 148 | // Is $_FILES[$field] set? If not, no reason to continue. |
| 149 | if ( ! isset($_FILES[$field])) |
| 150 | { |
| 151 | $this->set_error('upload_no_file_selected'); |
| 152 | return FALSE; |
| 153 | } |
| 154 | |
| 155 | // Is the upload path valid? |
| 156 | if ( ! $this->validate_upload_path()) |
| 157 | { |
| 158 | // errors will already be set by validate_upload_path() so just return FALSE |
| 159 | return FALSE; |
| 160 | } |
| 161 | |
| 162 | // Was the file able to be uploaded? If not, determine the reason why. |
| 163 | if ( ! is_uploaded_file($_FILES[$field]['tmp_name'])) |
| 164 | { |
| 165 | $error = ( ! isset($_FILES[$field]['error'])) ? 4 : $_FILES[$field]['error']; |
| 166 | |
| 167 | switch($error) |
| 168 | { |
| 169 | case 1: // UPLOAD_ERR_INI_SIZE |
| 170 | $this->set_error('upload_file_exceeds_limit'); |
| 171 | break; |
| 172 | case 2: // UPLOAD_ERR_FORM_SIZE |
| 173 | $this->set_error('upload_file_exceeds_form_limit'); |
| 174 | break; |
| 175 | case 3: // UPLOAD_ERR_PARTIAL |
| 176 | $this->set_error('upload_file_partial'); |
| 177 | break; |
| 178 | case 4: // UPLOAD_ERR_NO_FILE |
| 179 | $this->set_error('upload_no_file_selected'); |
| 180 | break; |
| 181 | case 6: // UPLOAD_ERR_NO_TMP_DIR |
| 182 | $this->set_error('upload_no_temp_directory'); |
| 183 | break; |
| 184 | case 7: // UPLOAD_ERR_CANT_WRITE |
| 185 | $this->set_error('upload_unable_to_write_file'); |
| 186 | break; |
| 187 | case 8: // UPLOAD_ERR_EXTENSION |
| 188 | $this->set_error('upload_stopped_by_extension'); |
| 189 | break; |
| 190 | default : $this->set_error('upload_no_file_selected'); |
| 191 | break; |
| 192 | } |
| 193 | |
| 194 | return FALSE; |
| 195 | } |
| 196 | |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame^] | 197 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 198 | // Set the uploaded data as class variables |
| 199 | $this->file_temp = $_FILES[$field]['tmp_name']; |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame^] | 200 | $this->file_size = $_FILES[$field]['size']; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 201 | $this->file_type = preg_replace("/^(.+?);.*$/", "\\1", $_FILES[$field]['type']); |
Derek Jones | 616fb02 | 2010-04-22 16:52:18 -0500 | [diff] [blame] | 202 | $this->file_type = strtolower(trim(stripslashes($this->file_type), '"')); |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame^] | 203 | $this->file_name = $this->_prep_filename($_FILES[$field]['name']); |
| 204 | $this->file_ext = $this->get_extension($this->file_name); |
| 205 | $this->client_name = $this->file_name; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 206 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 207 | // Is the file type allowed to be uploaded? |
| 208 | if ( ! $this->is_allowed_filetype()) |
| 209 | { |
| 210 | $this->set_error('upload_invalid_filetype'); |
| 211 | return FALSE; |
| 212 | } |
| 213 | |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame^] | 214 | // if we're overriding, let's now make sure the new name and type is allowed |
| 215 | if ($this->_file_name_override != '') |
| 216 | { |
| 217 | $this->file_name = $this->_prep_filename($this->_file_name_override); |
| 218 | $this->file_ext = $this->get_extension($this->file_name); |
| 219 | |
| 220 | if ( ! $this->is_allowed_filetype(TRUE)) |
| 221 | { |
| 222 | $this->set_error('upload_invalid_filetype'); |
| 223 | return FALSE; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | // Convert the file size to kilobytes |
| 228 | if ($this->file_size > 0) |
| 229 | { |
| 230 | $this->file_size = round($this->file_size/1024, 2); |
| 231 | } |
| 232 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 233 | // Is the file size within the allowed maximum? |
| 234 | if ( ! $this->is_allowed_filesize()) |
| 235 | { |
| 236 | $this->set_error('upload_invalid_filesize'); |
| 237 | return FALSE; |
| 238 | } |
| 239 | |
| 240 | // Are the image dimensions within the allowed size? |
| 241 | // Note: This can fail if the server has an open_basdir restriction. |
| 242 | if ( ! $this->is_allowed_dimensions()) |
| 243 | { |
| 244 | $this->set_error('upload_invalid_dimensions'); |
| 245 | return FALSE; |
| 246 | } |
| 247 | |
| 248 | // Sanitize the file name for security |
| 249 | $this->file_name = $this->clean_file_name($this->file_name); |
| 250 | |
| 251 | // Truncate the file name if it's too long |
| 252 | if ($this->max_filename > 0) |
| 253 | { |
| 254 | $this->file_name = $this->limit_filename_length($this->file_name, $this->max_filename); |
| 255 | } |
| 256 | |
| 257 | // Remove white spaces in the name |
| 258 | if ($this->remove_spaces == TRUE) |
| 259 | { |
| 260 | $this->file_name = preg_replace("/\s+/", "_", $this->file_name); |
| 261 | } |
| 262 | |
| 263 | /* |
| 264 | * Validate the file name |
| 265 | * This function appends an number onto the end of |
| 266 | * the file if one with the same name already exists. |
| 267 | * If it returns false there was a problem. |
| 268 | */ |
| 269 | $this->orig_name = $this->file_name; |
| 270 | |
| 271 | if ($this->overwrite == FALSE) |
| 272 | { |
| 273 | $this->file_name = $this->set_filename($this->upload_path, $this->file_name); |
| 274 | |
| 275 | if ($this->file_name === FALSE) |
| 276 | { |
| 277 | return FALSE; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | /* |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame^] | 282 | * Run the file through the XSS hacking filter |
| 283 | * This helps prevent malicious code from being |
| 284 | * embedded within a file. Scripts can easily |
| 285 | * be disguised as images or other file types. |
| 286 | */ |
| 287 | if ($this->xss_clean) |
| 288 | { |
| 289 | if ($this->do_xss_clean() === FALSE) |
| 290 | { |
| 291 | $this->set_error('upload_unable_to_write_file'); |
| 292 | return FALSE; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | /* |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 297 | * Move the file to the final destination |
| 298 | * To deal with different server configurations |
| 299 | * we'll attempt to use copy() first. If that fails |
| 300 | * we'll use move_uploaded_file(). One of the two should |
| 301 | * reliably work in most environments |
| 302 | */ |
| 303 | if ( ! @copy($this->file_temp, $this->upload_path.$this->file_name)) |
| 304 | { |
| 305 | if ( ! @move_uploaded_file($this->file_temp, $this->upload_path.$this->file_name)) |
| 306 | { |
| 307 | $this->set_error('upload_destination_error'); |
| 308 | return FALSE; |
| 309 | } |
| 310 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 311 | |
| 312 | /* |
| 313 | * Set the finalized image dimensions |
| 314 | * This sets the image width/height (assuming the |
| 315 | * file was an image). We use this information |
| 316 | * in the "data" function. |
| 317 | */ |
| 318 | $this->set_image_properties($this->upload_path.$this->file_name); |
| 319 | |
| 320 | return TRUE; |
| 321 | } |
| 322 | |
| 323 | // -------------------------------------------------------------------- |
| 324 | |
| 325 | /** |
| 326 | * Finalized Data Array |
| 327 | * |
| 328 | * Returns an associative array containing all of the information |
| 329 | * related to the upload, allowing the developer easy access in one array. |
| 330 | * |
| 331 | * @access public |
| 332 | * @return array |
| 333 | */ |
| 334 | function data() |
| 335 | { |
| 336 | return array ( |
| 337 | 'file_name' => $this->file_name, |
| 338 | 'file_type' => $this->file_type, |
| 339 | 'file_path' => $this->upload_path, |
| 340 | 'full_path' => $this->upload_path.$this->file_name, |
| 341 | 'raw_name' => str_replace($this->file_ext, '', $this->file_name), |
| 342 | 'orig_name' => $this->orig_name, |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame^] | 343 | 'client_name' => $this->client_name, |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 344 | 'file_ext' => $this->file_ext, |
| 345 | 'file_size' => $this->file_size, |
| 346 | 'is_image' => $this->is_image(), |
| 347 | 'image_width' => $this->image_width, |
| 348 | 'image_height' => $this->image_height, |
| 349 | 'image_type' => $this->image_type, |
| 350 | 'image_size_str' => $this->image_size_str, |
| 351 | ); |
| 352 | } |
| 353 | |
| 354 | // -------------------------------------------------------------------- |
| 355 | |
| 356 | /** |
| 357 | * Set Upload Path |
| 358 | * |
| 359 | * @access public |
| 360 | * @param string |
| 361 | * @return void |
| 362 | */ |
| 363 | function set_upload_path($path) |
| 364 | { |
| 365 | // Make sure it has a trailing slash |
| 366 | $this->upload_path = rtrim($path, '/').'/'; |
| 367 | } |
| 368 | |
| 369 | // -------------------------------------------------------------------- |
| 370 | |
| 371 | /** |
| 372 | * Set the file name |
| 373 | * |
| 374 | * This function takes a filename/path as input and looks for the |
| 375 | * existence of a file with the same name. If found, it will append a |
| 376 | * number to the end of the filename to avoid overwriting a pre-existing file. |
| 377 | * |
| 378 | * @access public |
| 379 | * @param string |
| 380 | * @param string |
| 381 | * @return string |
| 382 | */ |
| 383 | function set_filename($path, $filename) |
| 384 | { |
| 385 | if ($this->encrypt_name == TRUE) |
| 386 | { |
| 387 | mt_srand(); |
| 388 | $filename = md5(uniqid(mt_rand())).$this->file_ext; |
| 389 | } |
| 390 | |
| 391 | if ( ! file_exists($path.$filename)) |
| 392 | { |
| 393 | return $filename; |
| 394 | } |
| 395 | |
| 396 | $filename = str_replace($this->file_ext, '', $filename); |
| 397 | |
| 398 | $new_filename = ''; |
| 399 | for ($i = 1; $i < 100; $i++) |
| 400 | { |
| 401 | if ( ! file_exists($path.$filename.$i.$this->file_ext)) |
| 402 | { |
| 403 | $new_filename = $filename.$i.$this->file_ext; |
| 404 | break; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | if ($new_filename == '') |
| 409 | { |
| 410 | $this->set_error('upload_bad_filename'); |
| 411 | return FALSE; |
| 412 | } |
| 413 | else |
| 414 | { |
| 415 | return $new_filename; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | // -------------------------------------------------------------------- |
| 420 | |
| 421 | /** |
| 422 | * Set Maximum File Size |
| 423 | * |
| 424 | * @access public |
| 425 | * @param integer |
| 426 | * @return void |
| 427 | */ |
| 428 | function set_max_filesize($n) |
| 429 | { |
| 430 | $this->max_size = ((int) $n < 0) ? 0: (int) $n; |
| 431 | } |
| 432 | |
| 433 | // -------------------------------------------------------------------- |
| 434 | |
| 435 | /** |
| 436 | * Set Maximum File Name Length |
| 437 | * |
| 438 | * @access public |
| 439 | * @param integer |
| 440 | * @return void |
| 441 | */ |
| 442 | function set_max_filename($n) |
| 443 | { |
| 444 | $this->max_filename = ((int) $n < 0) ? 0: (int) $n; |
| 445 | } |
| 446 | |
| 447 | // -------------------------------------------------------------------- |
| 448 | |
| 449 | /** |
| 450 | * Set Maximum Image Width |
| 451 | * |
| 452 | * @access public |
| 453 | * @param integer |
| 454 | * @return void |
| 455 | */ |
| 456 | function set_max_width($n) |
| 457 | { |
| 458 | $this->max_width = ((int) $n < 0) ? 0: (int) $n; |
| 459 | } |
| 460 | |
| 461 | // -------------------------------------------------------------------- |
| 462 | |
| 463 | /** |
| 464 | * Set Maximum Image Height |
| 465 | * |
| 466 | * @access public |
| 467 | * @param integer |
| 468 | * @return void |
| 469 | */ |
| 470 | function set_max_height($n) |
| 471 | { |
| 472 | $this->max_height = ((int) $n < 0) ? 0: (int) $n; |
| 473 | } |
| 474 | |
| 475 | // -------------------------------------------------------------------- |
| 476 | |
| 477 | /** |
| 478 | * Set Allowed File Types |
| 479 | * |
| 480 | * @access public |
| 481 | * @param string |
| 482 | * @return void |
| 483 | */ |
| 484 | function set_allowed_types($types) |
| 485 | { |
Derek Jones | e12f64e | 2010-03-02 22:55:08 -0600 | [diff] [blame] | 486 | if ( ! is_array($types) && $types == '*') |
| 487 | { |
| 488 | $this->allowed_types = '*'; |
| 489 | return; |
| 490 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 491 | $this->allowed_types = explode('|', $types); |
| 492 | } |
| 493 | |
| 494 | // -------------------------------------------------------------------- |
| 495 | |
| 496 | /** |
| 497 | * Set Image Properties |
| 498 | * |
| 499 | * Uses GD to determine the width/height/type of image |
| 500 | * |
| 501 | * @access public |
| 502 | * @param string |
| 503 | * @return void |
| 504 | */ |
| 505 | function set_image_properties($path = '') |
| 506 | { |
| 507 | if ( ! $this->is_image()) |
| 508 | { |
| 509 | return; |
| 510 | } |
| 511 | |
| 512 | if (function_exists('getimagesize')) |
| 513 | { |
| 514 | if (FALSE !== ($D = @getimagesize($path))) |
| 515 | { |
| 516 | $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png'); |
| 517 | |
| 518 | $this->image_width = $D['0']; |
| 519 | $this->image_height = $D['1']; |
| 520 | $this->image_type = ( ! isset($types[$D['2']])) ? 'unknown' : $types[$D['2']]; |
| 521 | $this->image_size_str = $D['3']; // string containing height and width |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | // -------------------------------------------------------------------- |
| 527 | |
| 528 | /** |
| 529 | * Set XSS Clean |
| 530 | * |
| 531 | * Enables the XSS flag so that the file that was uploaded |
| 532 | * will be run through the XSS filter. |
| 533 | * |
| 534 | * @access public |
| 535 | * @param bool |
| 536 | * @return void |
| 537 | */ |
| 538 | function set_xss_clean($flag = FALSE) |
| 539 | { |
| 540 | $this->xss_clean = ($flag == TRUE) ? TRUE : FALSE; |
| 541 | } |
| 542 | |
| 543 | // -------------------------------------------------------------------- |
| 544 | |
| 545 | /** |
| 546 | * Validate the image |
| 547 | * |
| 548 | * @access public |
| 549 | * @return bool |
| 550 | */ |
| 551 | function is_image() |
| 552 | { |
| 553 | // IE will sometimes return odd mime-types during upload, so here we just standardize all |
| 554 | // jpegs or pngs to the same file type. |
| 555 | |
| 556 | $png_mimes = array('image/x-png'); |
| 557 | $jpeg_mimes = array('image/jpg', 'image/jpe', 'image/jpeg', 'image/pjpeg'); |
| 558 | |
| 559 | if (in_array($this->file_type, $png_mimes)) |
| 560 | { |
| 561 | $this->file_type = 'image/png'; |
| 562 | } |
| 563 | |
| 564 | if (in_array($this->file_type, $jpeg_mimes)) |
| 565 | { |
| 566 | $this->file_type = 'image/jpeg'; |
| 567 | } |
| 568 | |
| 569 | $img_mimes = array( |
| 570 | 'image/gif', |
| 571 | 'image/jpeg', |
| 572 | 'image/png', |
| 573 | ); |
| 574 | |
| 575 | return (in_array($this->file_type, $img_mimes, TRUE)) ? TRUE : FALSE; |
| 576 | } |
| 577 | |
| 578 | // -------------------------------------------------------------------- |
| 579 | |
| 580 | /** |
| 581 | * Verify that the filetype is allowed |
| 582 | * |
| 583 | * @access public |
| 584 | * @return bool |
| 585 | */ |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame^] | 586 | function is_allowed_filetype($ignore_mime = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 587 | { |
Derek Jones | e12f64e | 2010-03-02 22:55:08 -0600 | [diff] [blame] | 588 | if ($this->allowed_types == '*') |
| 589 | { |
| 590 | return TRUE; |
| 591 | } |
| 592 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 593 | if (count($this->allowed_types) == 0 OR ! is_array($this->allowed_types)) |
| 594 | { |
| 595 | $this->set_error('upload_no_file_types'); |
| 596 | return FALSE; |
| 597 | } |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame^] | 598 | |
| 599 | $ext = strtolower(ltrim($this->file_ext, '.')); |
| 600 | |
| 601 | if ( ! in_array($ext, $this->allowed_types)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 602 | { |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame^] | 603 | return FALSE; |
| 604 | } |
Derek Jones | afa282f | 2009-02-10 17:11:52 +0000 | [diff] [blame] | 605 | |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame^] | 606 | // Images get some additional checks |
| 607 | $image_types = array('gif', 'jpg', 'jpeg', 'png', 'jpe'); |
| 608 | |
| 609 | if (in_array($ext, $image_types)) |
| 610 | { |
| 611 | if (getimagesize($this->file_temp) === FALSE) |
Derek Jones | afa282f | 2009-02-10 17:11:52 +0000 | [diff] [blame] | 612 | { |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame^] | 613 | return FALSE; |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | if ($ignore_mime === TRUE) |
| 618 | { |
| 619 | return TRUE; |
| 620 | } |
| 621 | |
| 622 | $mime = $this->mimes_types($ext); |
| 623 | |
| 624 | if (is_array($mime)) |
| 625 | { |
| 626 | if (in_array($this->file_type, $mime, TRUE)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 627 | { |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame^] | 628 | return TRUE; |
| 629 | } |
| 630 | } |
| 631 | elseif ($mime == $this->file_type) |
| 632 | { |
| 633 | return TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | return FALSE; |
| 637 | } |
| 638 | |
| 639 | // -------------------------------------------------------------------- |
| 640 | |
| 641 | /** |
| 642 | * Verify that the file is within the allowed size |
| 643 | * |
| 644 | * @access public |
| 645 | * @return bool |
| 646 | */ |
| 647 | function is_allowed_filesize() |
| 648 | { |
| 649 | if ($this->max_size != 0 AND $this->file_size > $this->max_size) |
| 650 | { |
| 651 | return FALSE; |
| 652 | } |
| 653 | else |
| 654 | { |
| 655 | return TRUE; |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | // -------------------------------------------------------------------- |
| 660 | |
| 661 | /** |
| 662 | * Verify that the image is within the allowed width/height |
| 663 | * |
| 664 | * @access public |
| 665 | * @return bool |
| 666 | */ |
| 667 | function is_allowed_dimensions() |
| 668 | { |
| 669 | if ( ! $this->is_image()) |
| 670 | { |
| 671 | return TRUE; |
| 672 | } |
| 673 | |
| 674 | if (function_exists('getimagesize')) |
| 675 | { |
| 676 | $D = @getimagesize($this->file_temp); |
| 677 | |
| 678 | if ($this->max_width > 0 AND $D['0'] > $this->max_width) |
| 679 | { |
| 680 | return FALSE; |
| 681 | } |
| 682 | |
| 683 | if ($this->max_height > 0 AND $D['1'] > $this->max_height) |
| 684 | { |
| 685 | return FALSE; |
| 686 | } |
| 687 | |
| 688 | return TRUE; |
| 689 | } |
| 690 | |
| 691 | return TRUE; |
| 692 | } |
| 693 | |
| 694 | // -------------------------------------------------------------------- |
| 695 | |
| 696 | /** |
| 697 | * Validate Upload Path |
| 698 | * |
| 699 | * Verifies that it is a valid upload path with proper permissions. |
| 700 | * |
| 701 | * |
| 702 | * @access public |
| 703 | * @return bool |
| 704 | */ |
| 705 | function validate_upload_path() |
| 706 | { |
| 707 | if ($this->upload_path == '') |
| 708 | { |
| 709 | $this->set_error('upload_no_filepath'); |
| 710 | return FALSE; |
| 711 | } |
| 712 | |
| 713 | if (function_exists('realpath') AND @realpath($this->upload_path) !== FALSE) |
| 714 | { |
| 715 | $this->upload_path = str_replace("\\", "/", realpath($this->upload_path)); |
| 716 | } |
| 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 | |
| 730 | $this->upload_path = preg_replace("/(.+?)\/*$/", "\\1/", $this->upload_path); |
| 731 | return TRUE; |
| 732 | } |
| 733 | |
| 734 | // -------------------------------------------------------------------- |
| 735 | |
| 736 | /** |
| 737 | * Extract the file extension |
| 738 | * |
| 739 | * @access public |
| 740 | * @param string |
| 741 | * @return string |
| 742 | */ |
| 743 | function get_extension($filename) |
| 744 | { |
| 745 | $x = explode('.', $filename); |
| 746 | return '.'.end($x); |
| 747 | } |
| 748 | |
| 749 | // -------------------------------------------------------------------- |
| 750 | |
| 751 | /** |
| 752 | * Clean the file name for security |
| 753 | * |
| 754 | * @access public |
| 755 | * @param string |
| 756 | * @return string |
| 757 | */ |
| 758 | function clean_file_name($filename) |
| 759 | { |
| 760 | $bad = array( |
| 761 | "<!--", |
| 762 | "-->", |
| 763 | "'", |
| 764 | "<", |
| 765 | ">", |
| 766 | '"', |
| 767 | '&', |
| 768 | '$', |
| 769 | '=', |
| 770 | ';', |
| 771 | '?', |
| 772 | '/', |
| 773 | "%20", |
| 774 | "%22", |
| 775 | "%3c", // < |
| 776 | "%253c", // < |
| 777 | "%3e", // > |
| 778 | "%0e", // > |
| 779 | "%28", // ( |
| 780 | "%29", // ) |
| 781 | "%2528", // ( |
| 782 | "%26", // & |
| 783 | "%24", // $ |
| 784 | "%3f", // ? |
| 785 | "%3b", // ; |
| 786 | "%3d" // = |
| 787 | ); |
| 788 | |
| 789 | $filename = str_replace($bad, '', $filename); |
| 790 | |
| 791 | return stripslashes($filename); |
| 792 | } |
| 793 | |
| 794 | // -------------------------------------------------------------------- |
| 795 | |
| 796 | /** |
| 797 | * Limit the File Name Length |
| 798 | * |
| 799 | * @access public |
| 800 | * @param string |
| 801 | * @return string |
| 802 | */ |
| 803 | function limit_filename_length($filename, $length) |
| 804 | { |
| 805 | if (strlen($filename) < $length) |
| 806 | { |
| 807 | return $filename; |
| 808 | } |
| 809 | |
| 810 | $ext = ''; |
| 811 | if (strpos($filename, '.') !== FALSE) |
| 812 | { |
| 813 | $parts = explode('.', $filename); |
| 814 | $ext = '.'.array_pop($parts); |
| 815 | $filename = implode('.', $parts); |
| 816 | } |
| 817 | |
| 818 | return substr($filename, 0, ($length - strlen($ext))).$ext; |
| 819 | } |
| 820 | |
| 821 | // -------------------------------------------------------------------- |
| 822 | |
| 823 | /** |
| 824 | * Runs the file through the XSS clean function |
| 825 | * |
| 826 | * This prevents people from embedding malicious code in their files. |
| 827 | * I'm not sure that it won't negatively affect certain files in unexpected ways, |
| 828 | * but so far I haven't found that it causes trouble. |
| 829 | * |
| 830 | * @access public |
| 831 | * @return void |
| 832 | */ |
| 833 | function do_xss_clean() |
| 834 | { |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame^] | 835 | $file = $this->file_temp; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 836 | |
| 837 | if (filesize($file) == 0) |
| 838 | { |
| 839 | return FALSE; |
| 840 | } |
Greg Aker | f82e51c | 2010-04-14 19:33:50 -0500 | [diff] [blame] | 841 | |
| 842 | if (function_exists('memory_get_usage') && memory_get_usage() && ini_get('memory_limit') != '') |
| 843 | { |
| 844 | $current = ini_get('memory_limit') * 1024 * 1024; |
| 845 | |
Greg Aker | c78a259 | 2010-06-09 11:45:32 -0500 | [diff] [blame] | 846 | // There was a bug/behavioural change in PHP 5.2, where numbers over one million get output |
| 847 | // into scientific notation. number_format() ensures this number is an integer |
| 848 | // http://bugs.php.net/bug.php?id=43053 |
| 849 | |
| 850 | $new_memory = number_format(ceil(filesize($file) + $current), 0, '.', ''); |
| 851 | |
| 852 | 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] | 853 | } |
| 854 | |
| 855 | // If the file being uploaded is an image, then we should have no problem with XSS attacks (in theory), but |
| 856 | // IE can be fooled into mime-type detecting a malformed image as an html file, thus executing an XSS attack on anyone |
| 857 | // using IE who looks at the image. It does this by inspecting the first 255 bytes of an image. To get around this |
| 858 | // CI will itself look at the first 255 bytes of an image to determine its relative safety. This can save a lot of |
| 859 | // processor power and time if it is actually a clean image, as it will be in nearly all instances _except_ an |
| 860 | // attempted XSS attack. |
| 861 | |
| 862 | if (function_exists('getimagesize') && @getimagesize($file) !== FALSE) |
| 863 | { |
| 864 | if (($file = @fopen($file, 'rb')) === FALSE) // "b" to force binary |
| 865 | { |
| 866 | return FALSE; // Couldn't open the file, return FALSE |
| 867 | } |
| 868 | |
| 869 | $opening_bytes = fread($file, 256); |
| 870 | fclose($file); |
| 871 | |
| 872 | // These are known to throw IE into mime-type detection chaos |
| 873 | // <a, <body, <head, <html, <img, <plaintext, <pre, <script, <table, <title |
| 874 | // title is basically just in SVG, but we filter it anyhow |
| 875 | |
| 876 | if ( ! preg_match('/<(a|body|head|html|img|plaintext|pre|script|table|title)[\s>]/i', $opening_bytes)) |
| 877 | { |
| 878 | return TRUE; // its an image, no "triggers" detected in the first 256 bytes, we're good |
| 879 | } |
| 880 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 881 | |
| 882 | if (($data = @file_get_contents($file)) === FALSE) |
| 883 | { |
| 884 | return FALSE; |
| 885 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 886 | |
Greg Aker | f82e51c | 2010-04-14 19:33:50 -0500 | [diff] [blame] | 887 | $CI =& get_instance(); |
Derek Jones | 5640a71 | 2010-04-23 11:22:40 -0500 | [diff] [blame] | 888 | |
Derek Jones | 3084167 | 2010-04-26 09:09:21 -0500 | [diff] [blame] | 889 | if ( ! isset($CI->security)) |
Derek Jones | 5640a71 | 2010-04-23 11:22:40 -0500 | [diff] [blame] | 890 | { |
Derek Jones | 247f029 | 2010-04-26 09:10:21 -0500 | [diff] [blame] | 891 | $CI->load->library('security'); |
Derek Jones | 5640a71 | 2010-04-23 11:22:40 -0500 | [diff] [blame] | 892 | } |
| 893 | |
Greg Aker | f82e51c | 2010-04-14 19:33:50 -0500 | [diff] [blame] | 894 | return $CI->security->xss_clean($data, TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | // -------------------------------------------------------------------- |
| 898 | |
| 899 | /** |
| 900 | * Set an error message |
| 901 | * |
| 902 | * @access public |
| 903 | * @param string |
| 904 | * @return void |
| 905 | */ |
| 906 | function set_error($msg) |
| 907 | { |
| 908 | $CI =& get_instance(); |
| 909 | $CI->lang->load('upload'); |
| 910 | |
| 911 | if (is_array($msg)) |
| 912 | { |
| 913 | foreach ($msg as $val) |
| 914 | { |
| 915 | $msg = ($CI->lang->line($val) == FALSE) ? $val : $CI->lang->line($val); |
| 916 | $this->error_msg[] = $msg; |
| 917 | log_message('error', $msg); |
| 918 | } |
| 919 | } |
| 920 | else |
| 921 | { |
| 922 | $msg = ($CI->lang->line($msg) == FALSE) ? $msg : $CI->lang->line($msg); |
| 923 | $this->error_msg[] = $msg; |
| 924 | log_message('error', $msg); |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | // -------------------------------------------------------------------- |
| 929 | |
| 930 | /** |
| 931 | * Display the error message |
| 932 | * |
| 933 | * @access public |
| 934 | * @param string |
| 935 | * @param string |
| 936 | * @return string |
| 937 | */ |
| 938 | function display_errors($open = '<p>', $close = '</p>') |
| 939 | { |
| 940 | $str = ''; |
| 941 | foreach ($this->error_msg as $val) |
| 942 | { |
| 943 | $str .= $open.$val.$close; |
| 944 | } |
| 945 | |
| 946 | return $str; |
| 947 | } |
| 948 | |
| 949 | // -------------------------------------------------------------------- |
| 950 | |
| 951 | /** |
| 952 | * List of Mime Types |
| 953 | * |
| 954 | * This is a list of mime types. We use it to validate |
| 955 | * the "allowed types" set by the developer |
| 956 | * |
| 957 | * @access public |
| 958 | * @param string |
| 959 | * @return string |
| 960 | */ |
| 961 | function mimes_types($mime) |
| 962 | { |
| 963 | global $mimes; |
| 964 | |
| 965 | if (count($this->mimes) == 0) |
| 966 | { |
| 967 | if (@require_once(APPPATH.'config/mimes'.EXT)) |
| 968 | { |
| 969 | $this->mimes = $mimes; |
| 970 | unset($mimes); |
| 971 | } |
| 972 | } |
| 973 | |
| 974 | return ( ! isset($this->mimes[$mime])) ? FALSE : $this->mimes[$mime]; |
| 975 | } |
| 976 | |
| 977 | // -------------------------------------------------------------------- |
| 978 | |
| 979 | /** |
| 980 | * Prep Filename |
| 981 | * |
| 982 | * Prevents possible script execution from Apache's handling of files multiple extensions |
| 983 | * http://httpd.apache.org/docs/1.3/mod/mod_mime.html#multipleext |
| 984 | * |
| 985 | * @access private |
| 986 | * @param string |
| 987 | * @return string |
| 988 | */ |
| 989 | function _prep_filename($filename) |
| 990 | { |
| 991 | if (strpos($filename, '.') === FALSE) |
| 992 | { |
| 993 | return $filename; |
| 994 | } |
Derek Allard | 616dab8 | 2009-02-16 15:44:32 +0000 | [diff] [blame] | 995 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 996 | $parts = explode('.', $filename); |
| 997 | $ext = array_pop($parts); |
| 998 | $filename = array_shift($parts); |
Derek Allard | 616dab8 | 2009-02-16 15:44:32 +0000 | [diff] [blame] | 999 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1000 | foreach ($parts as $part) |
| 1001 | { |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame^] | 1002 | 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] | 1003 | { |
| 1004 | $filename .= '.'.$part.'_'; |
| 1005 | } |
| 1006 | else |
| 1007 | { |
| 1008 | $filename .= '.'.$part; |
| 1009 | } |
| 1010 | } |
Derek Allard | d70b064 | 2009-02-16 13:51:42 +0000 | [diff] [blame] | 1011 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1012 | $filename .= '.'.$ext; |
| 1013 | |
| 1014 | return $filename; |
| 1015 | } |
| 1016 | |
| 1017 | // -------------------------------------------------------------------- |
| 1018 | |
| 1019 | } |
| 1020 | // END Upload Class |
| 1021 | |
| 1022 | /* End of file Upload.php */ |
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 1023 | /* Location: ./system/libraries/Upload.php */ |