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