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