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