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