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