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 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame^] | 5 | * An open source application development framework for PHP |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 7 | * This content is released under the MIT License (MIT) |
Andrey Andreev | 3a45957 | 2011-12-21 11:23:11 +0200 | [diff] [blame] | 8 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame^] | 9 | * Copyright (c) 2014 - 2015, British Columbia Institute of Technology |
Andrey Andreev | 3a45957 | 2011-12-21 11:23:11 +0200 | [diff] [blame] | 10 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 17 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | * THE SOFTWARE. |
| 28 | * |
| 29 | * @package CodeIgniter |
| 30 | * @author EllisLab Dev Team |
darwinel | 871754a | 2014-02-11 17:34:57 +0100 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame^] | 32 | * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/) |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 33 | * @license http://opensource.org/licenses/MIT MIT License |
| 34 | * @link http://codeigniter.com |
| 35 | * @since Version 1.0.0 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | * @filesource |
| 37 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | /** |
| 41 | * File Uploading Class |
| 42 | * |
| 43 | * @package CodeIgniter |
| 44 | * @subpackage Libraries |
| 45 | * @category Uploads |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 46 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 47 | * @link http://codeigniter.com/user_guide/libraries/file_uploading.html |
| 48 | */ |
| 49 | class CI_Upload { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 50 | |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 51 | /** |
| 52 | * Maximum file size |
| 53 | * |
| 54 | * @var int |
| 55 | */ |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 56 | public $max_size = 0; |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * Maximum image width |
| 60 | * |
| 61 | * @var int |
| 62 | */ |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 63 | public $max_width = 0; |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * Maximum image height |
| 67 | * |
| 68 | * @var int |
| 69 | */ |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 70 | public $max_height = 0; |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 71 | |
| 72 | /** |
Andrey Andreev | 05aa2d6 | 2012-12-03 16:06:55 +0200 | [diff] [blame] | 73 | * Minimum image width |
| 74 | * |
| 75 | * @var int |
| 76 | */ |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 77 | public $min_width = 0; |
Andrey Andreev | 05aa2d6 | 2012-12-03 16:06:55 +0200 | [diff] [blame] | 78 | |
| 79 | /** |
| 80 | * Minimum image height |
| 81 | * |
| 82 | * @var int |
| 83 | */ |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 84 | public $min_height = 0; |
Andrey Andreev | 05aa2d6 | 2012-12-03 16:06:55 +0200 | [diff] [blame] | 85 | |
| 86 | /** |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 87 | * Maximum filename length |
| 88 | * |
| 89 | * @var int |
| 90 | */ |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 91 | public $max_filename = 0; |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 92 | |
| 93 | /** |
| 94 | * Maximum duplicate filename increment ID |
| 95 | * |
| 96 | * @var int |
| 97 | */ |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 98 | public $max_filename_increment = 100; |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 99 | |
| 100 | /** |
| 101 | * Allowed file types |
| 102 | * |
| 103 | * @var string |
| 104 | */ |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 105 | public $allowed_types = ''; |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 106 | |
| 107 | /** |
| 108 | * Temporary filename |
| 109 | * |
| 110 | * @var string |
| 111 | */ |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 112 | public $file_temp = ''; |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 113 | |
| 114 | /** |
| 115 | * Filename |
| 116 | * |
| 117 | * @var string |
| 118 | */ |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 119 | public $file_name = ''; |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 120 | |
| 121 | /** |
| 122 | * Original filename |
| 123 | * |
| 124 | * @var string |
| 125 | */ |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 126 | public $orig_name = ''; |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 127 | |
| 128 | /** |
| 129 | * File type |
| 130 | * |
| 131 | * @var string |
| 132 | */ |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 133 | public $file_type = ''; |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 134 | |
| 135 | /** |
| 136 | * File size |
| 137 | * |
| 138 | * @var int |
| 139 | */ |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 140 | public $file_size = NULL; |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 141 | |
| 142 | /** |
| 143 | * Filename extension |
| 144 | * |
| 145 | * @var string |
| 146 | */ |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 147 | public $file_ext = ''; |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 148 | |
| 149 | /** |
Adrian | f496d13 | 2013-06-24 15:56:45 +0200 | [diff] [blame] | 150 | * Force filename extension to lowercase |
| 151 | * |
| 152 | * @var string |
| 153 | */ |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 154 | public $file_ext_tolower = FALSE; |
Adrian | f496d13 | 2013-06-24 15:56:45 +0200 | [diff] [blame] | 155 | |
| 156 | /** |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 157 | * Upload path |
| 158 | * |
| 159 | * @var string |
| 160 | */ |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 161 | public $upload_path = ''; |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 162 | |
| 163 | /** |
| 164 | * Overwrite flag |
| 165 | * |
| 166 | * @var bool |
| 167 | */ |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 168 | public $overwrite = FALSE; |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 169 | |
| 170 | /** |
| 171 | * Obfuscate filename flag |
| 172 | * |
| 173 | * @var bool |
| 174 | */ |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 175 | public $encrypt_name = FALSE; |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 176 | |
| 177 | /** |
| 178 | * Is image flag |
| 179 | * |
| 180 | * @var bool |
| 181 | */ |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 182 | public $is_image = FALSE; |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 183 | |
| 184 | /** |
| 185 | * Image width |
| 186 | * |
| 187 | * @var int |
| 188 | */ |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 189 | public $image_width = NULL; |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 190 | |
| 191 | /** |
| 192 | * Image height |
| 193 | * |
| 194 | * @var int |
| 195 | */ |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 196 | public $image_height = NULL; |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 197 | |
| 198 | /** |
| 199 | * Image type |
| 200 | * |
| 201 | * @var string |
| 202 | */ |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 203 | public $image_type = ''; |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 204 | |
| 205 | /** |
| 206 | * Image size string |
| 207 | * |
| 208 | * @var string |
| 209 | */ |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 210 | public $image_size_str = ''; |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 211 | |
| 212 | /** |
| 213 | * Error messages list |
| 214 | * |
| 215 | * @var array |
| 216 | */ |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 217 | public $error_msg = array(); |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 218 | |
| 219 | /** |
| 220 | * Remove spaces flag |
| 221 | * |
| 222 | * @var bool |
| 223 | */ |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 224 | public $remove_spaces = TRUE; |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 225 | |
| 226 | /** |
| 227 | * MIME detection flag |
| 228 | * |
| 229 | * @var bool |
| 230 | */ |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 231 | public $detect_mime = TRUE; |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 232 | |
| 233 | /** |
| 234 | * XSS filter flag |
| 235 | * |
| 236 | * @var bool |
| 237 | */ |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 238 | public $xss_clean = FALSE; |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 239 | |
| 240 | /** |
Andrey Andreev | 32c7212 | 2013-10-21 15:35:05 +0300 | [diff] [blame] | 241 | * Apache mod_mime fix flag |
| 242 | * |
| 243 | * @var bool |
| 244 | */ |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 245 | public $mod_mime_fix = TRUE; |
Andrey Andreev | 32c7212 | 2013-10-21 15:35:05 +0300 | [diff] [blame] | 246 | |
| 247 | /** |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 248 | * Temporary filename prefix |
| 249 | * |
| 250 | * @var string |
| 251 | */ |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 252 | public $temp_prefix = 'temp_file_'; |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 253 | |
| 254 | /** |
| 255 | * Filename sent by the client |
| 256 | * |
| 257 | * @var bool |
| 258 | */ |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 259 | public $client_name = ''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 260 | |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 261 | // -------------------------------------------------------------------- |
| 262 | |
| 263 | /** |
| 264 | * Filename override |
| 265 | * |
| 266 | * @var string |
| 267 | */ |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 268 | protected $_file_name_override = ''; |
| 269 | |
| 270 | /** |
| 271 | * MIME types list |
| 272 | * |
| 273 | * @var array |
| 274 | */ |
| 275 | protected $_mimes = array(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 276 | |
Andrey Andreev | 119d8a7 | 2014-01-08 15:27:53 +0200 | [diff] [blame] | 277 | /** |
| 278 | * CI Singleton |
| 279 | * |
| 280 | * @var object |
| 281 | */ |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 282 | protected $_CI; |
Andrey Andreev | 119d8a7 | 2014-01-08 15:27:53 +0200 | [diff] [blame] | 283 | |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 284 | // -------------------------------------------------------------------- |
| 285 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 286 | /** |
| 287 | * Constructor |
| 288 | * |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 289 | * @param array $props |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 290 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 291 | */ |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 292 | public function __construct($config = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 293 | { |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 294 | empty($config) OR $this->initialize($config, FALSE); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 295 | |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 296 | $this->_mimes =& get_mimes(); |
| 297 | $this->_CI =& get_instance(); |
Andrey Andreev | 6ef498b | 2012-06-05 22:01:58 +0300 | [diff] [blame] | 298 | |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 299 | log_message('debug', 'Upload Class Initialized'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 300 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 301 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 302 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 303 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 304 | /** |
| 305 | * Initialize preferences |
| 306 | * |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 307 | * @param array $config |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 308 | * @param bool $reset |
| 309 | * @return CI_Upload |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 310 | */ |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 311 | public function initialize(array $config = array(), $reset = TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 312 | { |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 313 | $reflection = new ReflectionClass($this); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 314 | |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 315 | if ($reset === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 316 | { |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 317 | $defaults = $reflection->getDefaultProperties(); |
| 318 | foreach (array_keys($defaults) as $key) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 319 | { |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 320 | if ($key[0] === '_') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 321 | { |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 322 | continue; |
| 323 | } |
| 324 | |
| 325 | if (isset($config[$key])) |
| 326 | { |
| 327 | if ($reflection->hasMethod('set_'.$key)) |
| 328 | { |
| 329 | $this->{'set_'.$key}($config[$key]); |
| 330 | } |
| 331 | else |
| 332 | { |
| 333 | $this->$key = $config[$key]; |
| 334 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 335 | } |
| 336 | else |
| 337 | { |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 338 | $this->$key = $defaults[$key]; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 339 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 340 | } |
Joseba Juániz | a8027ff | 2014-08-06 20:03:25 +0200 | [diff] [blame] | 341 | |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 342 | } |
Joseba Juániz | a8027ff | 2014-08-06 20:03:25 +0200 | [diff] [blame] | 343 | else |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 344 | { |
Joseba Juániz | a8027ff | 2014-08-06 20:03:25 +0200 | [diff] [blame] | 345 | |
| 346 | foreach ($config as $key => &$value) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 347 | { |
Joseba Juániz | a8027ff | 2014-08-06 20:03:25 +0200 | [diff] [blame] | 348 | if ($key[0] !== '_' && $reflection->hasProperty($key)) |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 349 | { |
Joseba Juániz | a8027ff | 2014-08-06 20:03:25 +0200 | [diff] [blame] | 350 | if ($reflection->hasMethod('set_'.$key)) |
| 351 | { |
| 352 | $this->{'set_'.$key}($value); |
| 353 | } |
| 354 | else |
| 355 | { |
| 356 | $this->$key = $value; |
| 357 | } |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 358 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 359 | } |
Joseba Juániz | a8027ff | 2014-08-06 20:03:25 +0200 | [diff] [blame] | 360 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 361 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 362 | |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame] | 363 | // if a file_name was provided in the config, use it instead of the user input |
| 364 | // supplied file name for all uploads until initialized again |
| 365 | $this->_file_name_override = $this->file_name; |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 366 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 367 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 368 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 369 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 370 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 371 | /** |
| 372 | * Perform the file upload |
| 373 | * |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 374 | * @param string $field |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 375 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 376 | */ |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 377 | public function do_upload($field = 'userfile') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 378 | { |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 379 | // Is $_FILES[$field] set? If not, no reason to continue. |
Andrey Andreev | e7d017b | 2014-02-25 12:23:34 +0200 | [diff] [blame] | 380 | if (isset($_FILES[$field])) |
| 381 | { |
| 382 | $_file = $_FILES[$field]; |
| 383 | } |
| 384 | // Does the field name contain array notation? |
| 385 | elseif (($c = preg_match_all('/(?:^[^\[]+)|\[[^]]*\]/', $field, $matches)) > 1) |
| 386 | { |
| 387 | $_file = $_FILES; |
| 388 | for ($i = 0; $i < $c; $i++) |
| 389 | { |
| 390 | // We can't track numeric iterations, only full field names are accepted |
| 391 | if (($field = trim($matches[0][$i], '[]')) === '' OR ! isset($_file[$field])) |
| 392 | { |
| 393 | $_file = NULL; |
| 394 | break; |
| 395 | } |
| 396 | |
| 397 | $_file = $_file[$field]; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | if ( ! isset($_file)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 402 | { |
| 403 | $this->set_error('upload_no_file_selected'); |
| 404 | return FALSE; |
| 405 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 406 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 407 | // Is the upload path valid? |
| 408 | if ( ! $this->validate_upload_path()) |
| 409 | { |
| 410 | // errors will already be set by validate_upload_path() so just return FALSE |
| 411 | return FALSE; |
| 412 | } |
| 413 | |
| 414 | // 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] | 415 | if ( ! is_uploaded_file($_file['tmp_name'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 416 | { |
Andrey Andreev | e7d017b | 2014-02-25 12:23:34 +0200 | [diff] [blame] | 417 | $error = isset($_file['error']) ? $_file['error'] : 4; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 418 | |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 419 | switch ($error) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 420 | { |
Darren Benney | e123a60 | 2013-03-30 18:17:54 +0000 | [diff] [blame] | 421 | case UPLOAD_ERR_INI_SIZE: |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 422 | $this->set_error('upload_file_exceeds_limit'); |
| 423 | break; |
Darren Benney | e123a60 | 2013-03-30 18:17:54 +0000 | [diff] [blame] | 424 | case UPLOAD_ERR_FORM_SIZE: |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 425 | $this->set_error('upload_file_exceeds_form_limit'); |
| 426 | break; |
Darren Benney | e123a60 | 2013-03-30 18:17:54 +0000 | [diff] [blame] | 427 | case UPLOAD_ERR_PARTIAL: |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 428 | $this->set_error('upload_file_partial'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 429 | break; |
Darren Benney | e123a60 | 2013-03-30 18:17:54 +0000 | [diff] [blame] | 430 | case UPLOAD_ERR_NO_FILE: |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 431 | $this->set_error('upload_no_file_selected'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 432 | break; |
Darren Benney | e123a60 | 2013-03-30 18:17:54 +0000 | [diff] [blame] | 433 | case UPLOAD_ERR_NO_TMP_DIR: |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 434 | $this->set_error('upload_no_temp_directory'); |
| 435 | break; |
Darren Benney | e123a60 | 2013-03-30 18:17:54 +0000 | [diff] [blame] | 436 | case UPLOAD_ERR_CANT_WRITE: |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 437 | $this->set_error('upload_unable_to_write_file'); |
| 438 | break; |
Darren Benney | e123a60 | 2013-03-30 18:17:54 +0000 | [diff] [blame] | 439 | case UPLOAD_ERR_EXTENSION: |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 440 | $this->set_error('upload_stopped_by_extension'); |
| 441 | break; |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 442 | default: |
| 443 | $this->set_error('upload_no_file_selected'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 444 | break; |
| 445 | } |
| 446 | |
| 447 | return FALSE; |
| 448 | } |
| 449 | |
| 450 | // Set the uploaded data as class variables |
Andrey Andreev | e7d017b | 2014-02-25 12:23:34 +0200 | [diff] [blame] | 451 | $this->file_temp = $_file['tmp_name']; |
| 452 | $this->file_size = $_file['size']; |
Andrey Andreev | d60e700 | 2012-06-17 00:03:03 +0300 | [diff] [blame] | 453 | |
| 454 | // Skip MIME type detection? |
| 455 | if ($this->detect_mime !== FALSE) |
| 456 | { |
Andrey Andreev | e7d017b | 2014-02-25 12:23:34 +0200 | [diff] [blame] | 457 | $this->_file_mime_type($_file); |
Andrey Andreev | d60e700 | 2012-06-17 00:03:03 +0300 | [diff] [blame] | 458 | } |
| 459 | |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 460 | $this->file_type = preg_replace('/^(.+?);.*$/', '\\1', $this->file_type); |
Derek Jones | 616fb02 | 2010-04-22 16:52:18 -0500 | [diff] [blame] | 461 | $this->file_type = strtolower(trim(stripslashes($this->file_type), '"')); |
Andrey Andreev | e7d017b | 2014-02-25 12:23:34 +0200 | [diff] [blame] | 462 | $this->file_name = $this->_prep_filename($_file['name']); |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame] | 463 | $this->file_ext = $this->get_extension($this->file_name); |
| 464 | $this->client_name = $this->file_name; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 465 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 466 | // Is the file type allowed to be uploaded? |
| 467 | if ( ! $this->is_allowed_filetype()) |
| 468 | { |
| 469 | $this->set_error('upload_invalid_filetype'); |
| 470 | return FALSE; |
| 471 | } |
| 472 | |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame] | 473 | // 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] | 474 | if ($this->_file_name_override !== '') |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame] | 475 | { |
| 476 | $this->file_name = $this->_prep_filename($this->_file_name_override); |
Phil Sturgeon | 1e74da2 | 2010-12-15 10:45:06 +0000 | [diff] [blame] | 477 | |
| 478 | // 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] | 479 | if (strpos($this->_file_name_override, '.') === FALSE) |
Phil Sturgeon | 1e74da2 | 2010-12-15 10:45:06 +0000 | [diff] [blame] | 480 | { |
| 481 | $this->file_name .= $this->file_ext; |
| 482 | } |
Phil Sturgeon | 1e74da2 | 2010-12-15 10:45:06 +0000 | [diff] [blame] | 483 | else |
| 484 | { |
vlakoff | 3567246 | 2013-02-15 01:36:04 +0100 | [diff] [blame] | 485 | // An extension was provided, let's have it! |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 486 | $this->file_ext = $this->get_extension($this->_file_name_override); |
Phil Sturgeon | 1e74da2 | 2010-12-15 10:45:06 +0000 | [diff] [blame] | 487 | } |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame] | 488 | |
| 489 | if ( ! $this->is_allowed_filetype(TRUE)) |
| 490 | { |
| 491 | $this->set_error('upload_invalid_filetype'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 492 | return FALSE; |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame] | 493 | } |
| 494 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 495 | |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame] | 496 | // Convert the file size to kilobytes |
| 497 | if ($this->file_size > 0) |
| 498 | { |
| 499 | $this->file_size = round($this->file_size/1024, 2); |
| 500 | } |
| 501 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 502 | // Is the file size within the allowed maximum? |
| 503 | if ( ! $this->is_allowed_filesize()) |
| 504 | { |
| 505 | $this->set_error('upload_invalid_filesize'); |
| 506 | return FALSE; |
| 507 | } |
| 508 | |
| 509 | // Are the image dimensions within the allowed size? |
vlakoff | c941d85 | 2013-08-06 14:44:40 +0200 | [diff] [blame] | 510 | // Note: This can fail if the server has an open_basedir restriction. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 511 | if ( ! $this->is_allowed_dimensions()) |
| 512 | { |
| 513 | $this->set_error('upload_invalid_dimensions'); |
| 514 | return FALSE; |
| 515 | } |
| 516 | |
| 517 | // Sanitize the file name for security |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 518 | $this->file_name = $this->_CI->security->sanitize_filename($this->file_name); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 519 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 520 | // Truncate the file name if it's too long |
| 521 | if ($this->max_filename > 0) |
| 522 | { |
| 523 | $this->file_name = $this->limit_filename_length($this->file_name, $this->max_filename); |
| 524 | } |
| 525 | |
| 526 | // Remove white spaces in the name |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 527 | if ($this->remove_spaces === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 528 | { |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 529 | $this->file_name = preg_replace('/\s+/', '_', $this->file_name); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | /* |
| 533 | * Validate the file name |
| 534 | * This function appends an number onto the end of |
| 535 | * the file if one with the same name already exists. |
| 536 | * If it returns false there was a problem. |
| 537 | */ |
| 538 | $this->orig_name = $this->file_name; |
| 539 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 540 | if ($this->overwrite === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 541 | { |
| 542 | $this->file_name = $this->set_filename($this->upload_path, $this->file_name); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 543 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 544 | if ($this->file_name === FALSE) |
| 545 | { |
| 546 | return FALSE; |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | /* |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame] | 551 | * Run the file through the XSS hacking filter |
| 552 | * This helps prevent malicious code from being |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 553 | * embedded within a file. Scripts can easily |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame] | 554 | * be disguised as images or other file types. |
| 555 | */ |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 556 | if ($this->xss_clean && $this->do_xss_clean() === FALSE) |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame] | 557 | { |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 558 | $this->set_error('upload_unable_to_write_file'); |
| 559 | return FALSE; |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | /* |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 563 | * Move the file to the final destination |
| 564 | * To deal with different server configurations |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 565 | * we'll attempt to use copy() first. If that fails |
| 566 | * we'll use move_uploaded_file(). One of the two should |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 567 | * reliably work in most environments |
| 568 | */ |
| 569 | if ( ! @copy($this->file_temp, $this->upload_path.$this->file_name)) |
| 570 | { |
| 571 | if ( ! @move_uploaded_file($this->file_temp, $this->upload_path.$this->file_name)) |
| 572 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 573 | $this->set_error('upload_destination_error'); |
| 574 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 575 | } |
| 576 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 577 | |
| 578 | /* |
| 579 | * Set the finalized image dimensions |
| 580 | * This sets the image width/height (assuming the |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 581 | * file was an image). We use this information |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 582 | * in the "data" function. |
| 583 | */ |
| 584 | $this->set_image_properties($this->upload_path.$this->file_name); |
| 585 | |
| 586 | return TRUE; |
| 587 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 588 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 589 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 590 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 591 | /** |
| 592 | * Finalized Data Array |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 593 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 594 | * Returns an associative array containing all of the information |
| 595 | * related to the upload, allowing the developer easy access in one array. |
| 596 | * |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 597 | * @param string $index |
Michiel Vugteveen | 0ee287e | 2012-06-11 10:38:14 +0200 | [diff] [blame] | 598 | * @return mixed |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 599 | */ |
Michiel Vugteveen | dca9f23 | 2012-06-11 09:17:14 +0200 | [diff] [blame] | 600 | public function data($index = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 601 | { |
Michiel Vugteveen | dca9f23 | 2012-06-11 09:17:14 +0200 | [diff] [blame] | 602 | $data = array( |
Michiel Vugteveen | 3a7fb04 | 2012-06-11 09:29:16 +0200 | [diff] [blame] | 603 | 'file_name' => $this->file_name, |
| 604 | 'file_type' => $this->file_type, |
| 605 | 'file_path' => $this->upload_path, |
| 606 | 'full_path' => $this->upload_path.$this->file_name, |
| 607 | 'raw_name' => str_replace($this->file_ext, '', $this->file_name), |
| 608 | 'orig_name' => $this->orig_name, |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 609 | 'client_name' => $this->client_name, |
Michiel Vugteveen | 3a7fb04 | 2012-06-11 09:29:16 +0200 | [diff] [blame] | 610 | 'file_ext' => $this->file_ext, |
| 611 | 'file_size' => $this->file_size, |
| 612 | 'is_image' => $this->is_image(), |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 613 | 'image_width' => $this->image_width, |
| 614 | 'image_height' => $this->image_height, |
| 615 | 'image_type' => $this->image_type, |
| 616 | 'image_size_str' => $this->image_size_str, |
| 617 | ); |
Michiel Vugteveen | dca9f23 | 2012-06-11 09:17:14 +0200 | [diff] [blame] | 618 | |
Michiel Vugteveen | 46a0429 | 2012-06-11 10:37:52 +0200 | [diff] [blame] | 619 | if ( ! empty($index)) |
Michiel Vugteveen | dca9f23 | 2012-06-11 09:17:14 +0200 | [diff] [blame] | 620 | { |
Michiel Vugteveen | 46a0429 | 2012-06-11 10:37:52 +0200 | [diff] [blame] | 621 | return isset($data[$index]) ? $data[$index] : NULL; |
Michiel Vugteveen | dca9f23 | 2012-06-11 09:17:14 +0200 | [diff] [blame] | 622 | } |
| 623 | |
Michiel Vugteveen | 46a0429 | 2012-06-11 10:37:52 +0200 | [diff] [blame] | 624 | return $data; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 625 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 626 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 627 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 628 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 629 | /** |
| 630 | * Set Upload Path |
| 631 | * |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 632 | * @param string $path |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 633 | * @return CI_Upload |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 634 | */ |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 635 | public function set_upload_path($path) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 636 | { |
| 637 | // Make sure it has a trailing slash |
| 638 | $this->upload_path = rtrim($path, '/').'/'; |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 639 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 640 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 641 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 642 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 643 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 644 | /** |
| 645 | * Set the file name |
| 646 | * |
| 647 | * This function takes a filename/path as input and looks for the |
| 648 | * existence of a file with the same name. If found, it will append a |
| 649 | * number to the end of the filename to avoid overwriting a pre-existing file. |
| 650 | * |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 651 | * @param string $path |
| 652 | * @param string $filename |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 653 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 654 | */ |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 655 | public function set_filename($path, $filename) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 656 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 657 | if ($this->encrypt_name === TRUE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 658 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 659 | $filename = md5(uniqid(mt_rand())).$this->file_ext; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 660 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 661 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 662 | if ( ! file_exists($path.$filename)) |
| 663 | { |
| 664 | return $filename; |
| 665 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 666 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 667 | $filename = str_replace($this->file_ext, '', $filename); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 668 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 669 | $new_filename = ''; |
Adam Jackett | ccbbea1 | 2011-08-21 16:19:11 -0400 | [diff] [blame] | 670 | for ($i = 1; $i < $this->max_filename_increment; $i++) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 671 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 672 | if ( ! file_exists($path.$filename.$i.$this->file_ext)) |
| 673 | { |
| 674 | $new_filename = $filename.$i.$this->file_ext; |
| 675 | break; |
| 676 | } |
| 677 | } |
| 678 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 679 | if ($new_filename === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 680 | { |
| 681 | $this->set_error('upload_bad_filename'); |
| 682 | return FALSE; |
| 683 | } |
| 684 | else |
| 685 | { |
| 686 | return $new_filename; |
| 687 | } |
| 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 Size |
| 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_filesize($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_size = ($n < 0) ? 0 : (int) $n; |
| 701 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 702 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 703 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 File Name Length |
| 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_filename($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_filename = ($n < 0) ? 0 : (int) $n; |
| 715 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 716 | } |
| 717 | |
| 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 Width |
| 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_width($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_width = ($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 | /** |
| 735 | * Set Maximum Image Height |
| 736 | * |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 737 | * @param int $n |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 738 | * @return CI_Upload |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 739 | */ |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 740 | public function set_max_height($n) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 741 | { |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 742 | $this->max_height = ($n < 0) ? 0 : (int) $n; |
| 743 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 744 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 745 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 746 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 747 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 748 | /** |
Andrey Andreev | 05aa2d6 | 2012-12-03 16:06:55 +0200 | [diff] [blame] | 749 | * Set minimum image width |
| 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_width($n) |
| 755 | { |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 756 | $this->min_width = ($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 | /** |
| 763 | * Set minimum image height |
| 764 | * |
| 765 | * @param int $n |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 766 | * @return CI_Upload |
Andrey Andreev | 05aa2d6 | 2012-12-03 16:06:55 +0200 | [diff] [blame] | 767 | */ |
| 768 | public function set_min_height($n) |
| 769 | { |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 770 | $this->min_height = ($n < 0) ? 0 : (int) $n; |
| 771 | return $this; |
Andrey Andreev | 05aa2d6 | 2012-12-03 16:06:55 +0200 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | // -------------------------------------------------------------------- |
| 775 | |
| 776 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 777 | * Set Allowed File Types |
| 778 | * |
Andrey Andreev | 82179bf | 2014-02-22 00:29:53 +0200 | [diff] [blame] | 779 | * @param mixed $types |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 780 | * @return CI_Upload |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 781 | */ |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 782 | public function set_allowed_types($types) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 783 | { |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 784 | $this->allowed_types = (is_array($types) OR $types === '*') |
| 785 | ? $types |
| 786 | : explode('|', $types); |
| 787 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 788 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 789 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 790 | // -------------------------------------------------------------------- |
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 | /** |
| 793 | * Set Image Properties |
| 794 | * |
| 795 | * Uses GD to determine the width/height/type of image |
| 796 | * |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 797 | * @param string $path |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 798 | * @return CI_Upload |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 799 | */ |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 800 | public function set_image_properties($path = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 801 | { |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 802 | if ($this->is_image() && function_exists('getimagesize')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 803 | { |
| 804 | if (FALSE !== ($D = @getimagesize($path))) |
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 | $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png'); |
| 807 | |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 808 | $this->image_width = $D[0]; |
| 809 | $this->image_height = $D[1]; |
| 810 | $this->image_type = isset($types[$D[2]]) ? $types[$D[2]] : 'unknown'; |
| 811 | $this->image_size_str = $D[3]; // string containing height and width |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 812 | } |
| 813 | } |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 814 | |
| 815 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 816 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 817 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 818 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 819 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 820 | /** |
| 821 | * Set XSS Clean |
| 822 | * |
| 823 | * Enables the XSS flag so that the file that was uploaded |
| 824 | * will be run through the XSS filter. |
| 825 | * |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 826 | * @param bool $flag |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 827 | * @return CI_Upload |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 828 | */ |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 829 | public function set_xss_clean($flag = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 830 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 831 | $this->xss_clean = ($flag === TRUE); |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 832 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 833 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 834 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 835 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 836 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 837 | /** |
| 838 | * Validate the image |
| 839 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 840 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 841 | */ |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 842 | public function is_image() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 843 | { |
| 844 | // IE will sometimes return odd mime-types during upload, so here we just standardize all |
| 845 | // jpegs or pngs to the same file type. |
| 846 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 847 | $png_mimes = array('image/x-png'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 848 | $jpeg_mimes = array('image/jpg', 'image/jpe', 'image/jpeg', 'image/pjpeg'); |
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 | if (in_array($this->file_type, $png_mimes)) |
| 851 | { |
| 852 | $this->file_type = 'image/png'; |
| 853 | } |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 854 | elseif (in_array($this->file_type, $jpeg_mimes)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 855 | { |
| 856 | $this->file_type = 'image/jpeg'; |
| 857 | } |
| 858 | |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 859 | $img_mimes = array('image/gif', 'image/jpeg', 'image/png'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 860 | |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 861 | return in_array($this->file_type, $img_mimes, TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 862 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 863 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 864 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 865 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 866 | /** |
| 867 | * Verify that the filetype is allowed |
| 868 | * |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 869 | * @param bool $ignore_mime |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 870 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 871 | */ |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 872 | public function is_allowed_filetype($ignore_mime = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 873 | { |
Andrey Andreev | 6ca407e | 2012-03-01 15:33:21 +0200 | [diff] [blame] | 874 | if ($this->allowed_types === '*') |
Derek Jones | e12f64e | 2010-03-02 22:55:08 -0600 | [diff] [blame] | 875 | { |
| 876 | return TRUE; |
| 877 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 878 | |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 879 | if (empty($this->allowed_types) OR ! is_array($this->allowed_types)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 880 | { |
| 881 | $this->set_error('upload_no_file_types'); |
| 882 | return FALSE; |
| 883 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 884 | |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame] | 885 | $ext = strtolower(ltrim($this->file_ext, '.')); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 886 | |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 887 | if ( ! in_array($ext, $this->allowed_types, TRUE)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 888 | { |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame] | 889 | return FALSE; |
| 890 | } |
Derek Jones | afa282f | 2009-02-10 17:11:52 +0000 | [diff] [blame] | 891 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 892 | // Images get some additional checks |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 893 | 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] | 894 | { |
Andrey Andreev | 6ca407e | 2012-03-01 15:33:21 +0200 | [diff] [blame] | 895 | return FALSE; |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame] | 896 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 897 | |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame] | 898 | if ($ignore_mime === TRUE) |
| 899 | { |
| 900 | return TRUE; |
| 901 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 902 | |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 903 | if (isset($this->_mimes[$ext])) |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame] | 904 | { |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 905 | return is_array($this->_mimes[$ext]) |
| 906 | ? in_array($this->file_type, $this->_mimes[$ext], TRUE) |
| 907 | : ($this->_mimes[$ext] === $this->file_type); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 908 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 909 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 910 | return FALSE; |
| 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 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 914 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 915 | /** |
| 916 | * Verify that the file is within the allowed size |
| 917 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 918 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 919 | */ |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 920 | public function is_allowed_filesize() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 921 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 922 | return ($this->max_size === 0 OR $this->max_size > $this->file_size); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 923 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 924 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 925 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 926 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 927 | /** |
| 928 | * Verify that the image is within the allowed width/height |
| 929 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 930 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 931 | */ |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 932 | public function is_allowed_dimensions() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 933 | { |
| 934 | if ( ! $this->is_image()) |
| 935 | { |
| 936 | return TRUE; |
| 937 | } |
| 938 | |
| 939 | if (function_exists('getimagesize')) |
| 940 | { |
| 941 | $D = @getimagesize($this->file_temp); |
| 942 | |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 943 | if ($this->max_width > 0 && $D[0] > $this->max_width) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 944 | { |
| 945 | return FALSE; |
| 946 | } |
| 947 | |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 948 | if ($this->max_height > 0 && $D[1] > $this->max_height) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 949 | { |
| 950 | return FALSE; |
| 951 | } |
Andrey Andreev | 05aa2d6 | 2012-12-03 16:06:55 +0200 | [diff] [blame] | 952 | |
| 953 | if ($this->min_width > 0 && $D[0] < $this->min_width) |
| 954 | { |
| 955 | return FALSE; |
| 956 | } |
| 957 | |
| 958 | if ($this->min_height > 0 && $D[1] < $this->min_height) |
| 959 | { |
| 960 | return FALSE; |
| 961 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | return TRUE; |
| 965 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 966 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 967 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 968 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 969 | /** |
| 970 | * Validate Upload Path |
| 971 | * |
| 972 | * Verifies that it is a valid upload path with proper permissions. |
| 973 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 974 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 975 | */ |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 976 | public function validate_upload_path() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 977 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 978 | if ($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 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 983 | |
Andrey Andreev | ea41c8a | 2014-02-26 18:31:02 +0200 | [diff] [blame] | 984 | if (realpath($this->upload_path) !== FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 985 | { |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 986 | $this->upload_path = str_replace('\\', '/', realpath($this->upload_path)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 987 | } |
| 988 | |
Andrey Andreev | 382b513 | 2014-02-26 18:41:59 +0200 | [diff] [blame] | 989 | if ( ! is_dir($this->upload_path)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 990 | { |
| 991 | $this->set_error('upload_no_filepath'); |
| 992 | return FALSE; |
| 993 | } |
| 994 | |
| 995 | if ( ! is_really_writable($this->upload_path)) |
| 996 | { |
| 997 | $this->set_error('upload_not_writable'); |
| 998 | return FALSE; |
| 999 | } |
| 1000 | |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 1001 | $this->upload_path = preg_replace('/(.+?)\/*$/', '\\1/', $this->upload_path); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1002 | return TRUE; |
| 1003 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1004 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1005 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1006 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1007 | /** |
| 1008 | * Extract the file extension |
| 1009 | * |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 1010 | * @param string $filename |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1011 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1012 | */ |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 1013 | public function get_extension($filename) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1014 | { |
| 1015 | $x = explode('.', $filename); |
Adrian | f496d13 | 2013-06-24 15:56:45 +0200 | [diff] [blame] | 1016 | |
Adrian | 74a228b | 2013-06-25 12:09:22 +0200 | [diff] [blame] | 1017 | if (count($x) === 1) |
| 1018 | { |
| 1019 | return ''; |
| 1020 | } |
| 1021 | |
| 1022 | $ext = ($this->file_ext_tolower) ? strtolower(end($x)) : end($x); |
| 1023 | return '.'.$ext; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1024 | } |
| 1025 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1029 | * Limit the File Name Length |
| 1030 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 1031 | * @param string $filename |
| 1032 | * @param int $length |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1033 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1034 | */ |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 1035 | public function limit_filename_length($filename, $length) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1036 | { |
| 1037 | if (strlen($filename) < $length) |
| 1038 | { |
| 1039 | return $filename; |
| 1040 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1041 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1042 | $ext = ''; |
| 1043 | if (strpos($filename, '.') !== FALSE) |
| 1044 | { |
| 1045 | $parts = explode('.', $filename); |
| 1046 | $ext = '.'.array_pop($parts); |
| 1047 | $filename = implode('.', $parts); |
| 1048 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1049 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1050 | return substr($filename, 0, ($length - strlen($ext))).$ext; |
| 1051 | } |
| 1052 | |
| 1053 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1054 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1055 | /** |
| 1056 | * Runs the file through the XSS clean function |
| 1057 | * |
| 1058 | * This prevents people from embedding malicious code in their files. |
| 1059 | * I'm not sure that it won't negatively affect certain files in unexpected ways, |
| 1060 | * but so far I haven't found that it causes trouble. |
| 1061 | * |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 1062 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1063 | */ |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 1064 | public function do_xss_clean() |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1065 | { |
Derek Jones | e9d723f | 2010-07-12 10:10:59 -0500 | [diff] [blame] | 1066 | $file = $this->file_temp; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1067 | |
Andrey Andreev | 5036c9c | 2012-06-04 15:34:56 +0300 | [diff] [blame] | 1068 | if (filesize($file) == 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1069 | { |
| 1070 | return FALSE; |
| 1071 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1072 | |
Andrey Andreev | c839d28 | 2012-06-07 14:35:27 +0300 | [diff] [blame] | 1073 | if (memory_get_usage() && ($memory_limit = ini_get('memory_limit'))) |
Greg Aker | f82e51c | 2010-04-14 19:33:50 -0500 | [diff] [blame] | 1074 | { |
Andrey Andreev | c839d28 | 2012-06-07 14:35:27 +0300 | [diff] [blame] | 1075 | $memory_limit *= 1024 * 1024; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1076 | |
Greg Aker | c78a259 | 2010-06-09 11:45:32 -0500 | [diff] [blame] | 1077 | // 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] | 1078 | // into scientific notation. number_format() ensures this number is an integer |
Greg Aker | c78a259 | 2010-06-09 11:45:32 -0500 | [diff] [blame] | 1079 | // http://bugs.php.net/bug.php?id=43053 |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1080 | |
Andrey Andreev | c839d28 | 2012-06-07 14:35:27 +0300 | [diff] [blame] | 1081 | $memory_limit = number_format(ceil(filesize($file) + $memory_limit), 0, '.', ''); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1082 | |
Andrey Andreev | c839d28 | 2012-06-07 14:35:27 +0300 | [diff] [blame] | 1083 | 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] | 1084 | } |
| 1085 | |
| 1086 | // If the file being uploaded is an image, then we should have no problem with XSS attacks (in theory), but |
| 1087 | // 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] | 1088 | // using IE who looks at the image. It does this by inspecting the first 255 bytes of an image. To get around this |
| 1089 | // 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] | 1090 | // 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] | 1091 | // attempted XSS attack. |
| 1092 | |
| 1093 | if (function_exists('getimagesize') && @getimagesize($file) !== FALSE) |
| 1094 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1095 | if (($file = @fopen($file, 'rb')) === FALSE) // "b" to force binary |
| 1096 | { |
Greg Aker | f82e51c | 2010-04-14 19:33:50 -0500 | [diff] [blame] | 1097 | return FALSE; // Couldn't open the file, return FALSE |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1098 | } |
Greg Aker | f82e51c | 2010-04-14 19:33:50 -0500 | [diff] [blame] | 1099 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1100 | $opening_bytes = fread($file, 256); |
| 1101 | fclose($file); |
Greg Aker | f82e51c | 2010-04-14 19:33:50 -0500 | [diff] [blame] | 1102 | |
| 1103 | // These are known to throw IE into mime-type detection chaos |
| 1104 | // <a, <body, <head, <html, <img, <plaintext, <pre, <script, <table, <title |
| 1105 | // title is basically just in SVG, but we filter it anyhow |
| 1106 | |
vlakoff | 3567246 | 2013-02-15 01:36:04 +0100 | [diff] [blame] | 1107 | // 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] | 1108 | 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] | 1109 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1110 | |
| 1111 | if (($data = @file_get_contents($file)) === FALSE) |
| 1112 | { |
| 1113 | return FALSE; |
| 1114 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1115 | |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 1116 | return $this->_CI->security->xss_clean($data, TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1117 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1118 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1119 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1120 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1121 | /** |
| 1122 | * Set an error message |
| 1123 | * |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 1124 | * @param string $msg |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 1125 | * @return CI_Upload |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1126 | */ |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 1127 | public function set_error($msg) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1128 | { |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 1129 | $this->_CI->lang->load('upload'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1130 | |
Andrey Andreev | 119d8a7 | 2014-01-08 15:27:53 +0200 | [diff] [blame] | 1131 | is_array($msg) OR $msg = array($msg); |
Darren Benney | 4a8d190 | 2013-03-30 20:07:49 +0000 | [diff] [blame] | 1132 | |
Darren Benney | 38d5f4b | 2013-03-30 21:00:38 +0000 | [diff] [blame] | 1133 | foreach ($msg as $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1134 | { |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 1135 | $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] | 1136 | $this->error_msg[] = $msg; |
| 1137 | log_message('error', $msg); |
| 1138 | } |
Andrey Andreev | 2cf4c9b | 2014-02-21 15:01:48 +0200 | [diff] [blame] | 1139 | |
| 1140 | return $this; |
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 | /** |
| 1146 | * Display the error message |
| 1147 | * |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 1148 | * @param string $open |
| 1149 | * @param string $close |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1150 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1151 | */ |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 1152 | public function display_errors($open = '<p>', $close = '</p>') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1153 | { |
Andrey Andreev | 8e6f7a9 | 2012-03-26 20:13:00 +0300 | [diff] [blame] | 1154 | 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] | 1155 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1156 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1157 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1158 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1159 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1160 | * Prep Filename |
| 1161 | * |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 1162 | * Prevents possible script execution from Apache's handling |
| 1163 | * of files' multiple extensions. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1164 | * |
Andrey Andreev | f5ccd12 | 2012-11-02 00:17:39 +0200 | [diff] [blame] | 1165 | * @link http://httpd.apache.org/docs/1.3/mod/mod_mime.html#multipleext |
| 1166 | * |
| 1167 | * @param string $filename |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1168 | * @return string |
| 1169 | */ |
Greg Aker | 58fdee8 | 2010-11-10 15:07:09 -0600 | [diff] [blame] | 1170 | protected function _prep_filename($filename) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1171 | { |
Andrey Andreev | f38c9c2 | 2014-08-27 14:56:31 +0300 | [diff] [blame] | 1172 | 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] | 1173 | { |
| 1174 | return $filename; |
| 1175 | } |
Derek Allard | 616dab8 | 2009-02-16 15:44:32 +0000 | [diff] [blame] | 1176 | |
Andrey Andreev | f38c9c2 | 2014-08-27 14:56:31 +0300 | [diff] [blame] | 1177 | $ext = substr($filename, $ext_pos); |
| 1178 | $filename = substr($filename, 0, $ext_pos); |
| 1179 | return str_replace('.', '_', $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 */ |