Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author ExpressionEngine Dev Team |
Greg Aker | 0711dc8 | 2011-01-05 10:49:40 -0600 | [diff] [blame] | 9 | * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html |
| 11 | * @link http://codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
| 15 | |
| 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * Input Class |
| 20 | * |
| 21 | * Pre-processes global input data for security |
| 22 | * |
| 23 | * @package CodeIgniter |
| 24 | * @subpackage Libraries |
| 25 | * @category Input |
| 26 | * @author ExpressionEngine Dev Team |
| 27 | * @link http://codeigniter.com/user_guide/libraries/input.html |
| 28 | */ |
| 29 | class CI_Input { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 30 | |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 31 | var $ip_address = FALSE; |
| 32 | var $user_agent = FALSE; |
Dan Horrigan | 65d603e | 2010-12-15 08:38:30 -0500 | [diff] [blame] | 33 | var $_allow_get_array = TRUE; |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 34 | var $_standardize_newlines = TRUE; |
| 35 | var $_enable_xss = FALSE; // Set automatically based on config setting |
| 36 | var $_enable_csrf = FALSE; // Set automatically based on config setting |
| 37 | |
Greg Aker | ec2f571 | 2010-11-15 16:22:12 -0600 | [diff] [blame] | 38 | protected $headers = array(); |
| 39 | |
| 40 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 41 | /** |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 42 | * Constructor |
| 43 | * |
| 44 | * Sets whether to globally enable the XSS processing |
| 45 | * and whether to allow the $_GET array |
| 46 | * |
| 47 | */ |
| 48 | public function __construct() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 49 | { |
| 50 | log_message('debug', "Input Class Initialized"); |
| 51 | |
Phil Sturgeon | c808915 | 2010-12-27 19:06:28 +0000 | [diff] [blame] | 52 | $this->_allow_get_array = (config_item('allow_get_array') === TRUE); |
| 53 | $this->_enable_xss = (config_item('global_xss_filtering') === TRUE); |
| 54 | $this->_enable_csrf = (config_item('csrf_protection') === TRUE); |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 55 | |
| 56 | // Do we need to load the security class? |
| 57 | if ($this->_enable_xss == TRUE OR $this->_enable_csrf == TRUE) |
| 58 | { |
| 59 | $this->security =& load_class('Security'); |
| 60 | } |
| 61 | |
Pascal Kriete | aaec1e4 | 2011-01-20 00:01:21 -0500 | [diff] [blame] | 62 | // Do we need the UTF-8 class? |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 63 | if (UTF8_ENABLED === TRUE) |
| 64 | { |
| 65 | global $UNI; |
| 66 | $this->uni =& $UNI; |
| 67 | } |
| 68 | |
| 69 | // Sanitize global arrays |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 70 | $this->_sanitize_globals(); |
| 71 | } |
| 72 | |
| 73 | // -------------------------------------------------------------------- |
| 74 | |
| 75 | /** |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 76 | * Fetch from array |
| 77 | * |
| 78 | * This is a helper function to retrieve values from global arrays |
| 79 | * |
| 80 | * @access private |
| 81 | * @param array |
| 82 | * @param string |
| 83 | * @param bool |
| 84 | * @return string |
| 85 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 86 | function _fetch_from_array(&$array, $index = '', $xss_clean = FALSE) |
| 87 | { |
| 88 | if ( ! isset($array[$index])) |
| 89 | { |
| 90 | return FALSE; |
| 91 | } |
| 92 | |
| 93 | if ($xss_clean === TRUE) |
| 94 | { |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 95 | $_security =& load_class('Security'); |
| 96 | return $_security->xss_clean($array[$index]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | return $array[$index]; |
| 100 | } |
| 101 | |
| 102 | // -------------------------------------------------------------------- |
| 103 | |
| 104 | /** |
| 105 | * Fetch an item from the GET array |
| 106 | * |
| 107 | * @access public |
| 108 | * @param string |
| 109 | * @param bool |
| 110 | * @return string |
| 111 | */ |
Phil Sturgeon | 44f2105 | 2011-02-15 21:39:25 +0000 | [diff] [blame] | 112 | function get($index = NULL, $xss_clean = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 113 | { |
Phil Sturgeon | 44f2105 | 2011-02-15 21:39:25 +0000 | [diff] [blame] | 114 | // Check if a field has been provided |
| 115 | if ($index === NULL AND ! empty($_GET)) |
vascopj | ff1cfa1 | 2011-02-13 21:30:19 +0000 | [diff] [blame] | 116 | { |
Phil Sturgeon | 44f2105 | 2011-02-15 21:39:25 +0000 | [diff] [blame] | 117 | $get = array(); |
vascopj | ff1cfa1 | 2011-02-13 21:30:19 +0000 | [diff] [blame] | 118 | |
| 119 | // loop through the full _GET array |
Phil Sturgeon | 44f2105 | 2011-02-15 21:39:25 +0000 | [diff] [blame] | 120 | foreach (array_keys($_GET) as $key) |
vascopj | ff1cfa1 | 2011-02-13 21:30:19 +0000 | [diff] [blame] | 121 | { |
Phil Sturgeon | 44f2105 | 2011-02-15 21:39:25 +0000 | [diff] [blame] | 122 | $get[$key] = $this->_fetch_from_array($_GET, $key, $xss_clean); |
vascopj | ff1cfa1 | 2011-02-13 21:30:19 +0000 | [diff] [blame] | 123 | } |
Phil Sturgeon | 44f2105 | 2011-02-15 21:39:25 +0000 | [diff] [blame] | 124 | return $get; |
vascopj | ff1cfa1 | 2011-02-13 21:30:19 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 127 | return $this->_fetch_from_array($_GET, $index, $xss_clean); |
| 128 | } |
| 129 | |
| 130 | // -------------------------------------------------------------------- |
| 131 | |
| 132 | /** |
| 133 | * Fetch an item from the POST array |
| 134 | * |
| 135 | * @access public |
| 136 | * @param string |
| 137 | * @param bool |
| 138 | * @return string |
| 139 | */ |
Phil Sturgeon | 44f2105 | 2011-02-15 21:39:25 +0000 | [diff] [blame] | 140 | function post($index = NULL, $xss_clean = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 141 | { |
Phil Sturgeon | 44f2105 | 2011-02-15 21:39:25 +0000 | [diff] [blame] | 142 | // Check if a field has been provided |
| 143 | if ($index === NULL AND ! empty($_POST)) |
vascopj | 0ba58b8 | 2011-02-06 14:20:21 +0000 | [diff] [blame] | 144 | { |
Phil Sturgeon | 44f2105 | 2011-02-15 21:39:25 +0000 | [diff] [blame] | 145 | $post = array(); |
vascopj | 0ba58b8 | 2011-02-06 14:20:21 +0000 | [diff] [blame] | 146 | |
Phil Sturgeon | 44f2105 | 2011-02-15 21:39:25 +0000 | [diff] [blame] | 147 | // Loop through the full _POST array and return it |
| 148 | foreach (array_keys($_POST) as $key) |
vascopj | 0ba58b8 | 2011-02-06 14:20:21 +0000 | [diff] [blame] | 149 | { |
Phil Sturgeon | 44f2105 | 2011-02-15 21:39:25 +0000 | [diff] [blame] | 150 | $post[$key] = $this->_fetch_from_array($_POST, $key, $xss_clean); |
vascopj | 0ba58b8 | 2011-02-06 14:20:21 +0000 | [diff] [blame] | 151 | } |
Phil Sturgeon | 44f2105 | 2011-02-15 21:39:25 +0000 | [diff] [blame] | 152 | return $post; |
vascopj | 0ba58b8 | 2011-02-06 14:20:21 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 155 | return $this->_fetch_from_array($_POST, $index, $xss_clean); |
| 156 | } |
| 157 | |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 158 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 159 | // -------------------------------------------------------------------- |
| 160 | |
| 161 | /** |
| 162 | * Fetch an item from either the GET array or the POST |
| 163 | * |
| 164 | * @access public |
| 165 | * @param string The index key |
| 166 | * @param bool XSS cleaning |
| 167 | * @return string |
| 168 | */ |
| 169 | function get_post($index = '', $xss_clean = FALSE) |
| 170 | { |
| 171 | if ( ! isset($_POST[$index]) ) |
| 172 | { |
| 173 | return $this->get($index, $xss_clean); |
| 174 | } |
| 175 | else |
| 176 | { |
| 177 | return $this->post($index, $xss_clean); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | // -------------------------------------------------------------------- |
| 182 | |
| 183 | /** |
| 184 | * Fetch an item from the COOKIE array |
| 185 | * |
| 186 | * @access public |
| 187 | * @param string |
| 188 | * @param bool |
| 189 | * @return string |
| 190 | */ |
| 191 | function cookie($index = '', $xss_clean = FALSE) |
| 192 | { |
| 193 | return $this->_fetch_from_array($_COOKIE, $index, $xss_clean); |
| 194 | } |
| 195 | |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 196 | // ------------------------------------------------------------------------ |
| 197 | |
| 198 | /** |
| 199 | * Set cookie |
| 200 | * |
| 201 | * Accepts six parameter, or you can submit an associative |
| 202 | * array in the first parameter containing all the values. |
| 203 | * |
| 204 | * @access public |
| 205 | * @param mixed |
| 206 | * @param string the value of the cookie |
| 207 | * @param string the number of seconds until expiration |
| 208 | * @param string the cookie domain. Usually: .yourdomain.com |
| 209 | * @param string the cookie path |
| 210 | * @param string the cookie prefix |
Phil Sturgeon | d8d1e24 | 2011-02-16 17:23:16 +0000 | [diff] [blame] | 211 | * @param bool true makes the cookie secure |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 212 | * @return void |
| 213 | */ |
tobiasbg | 9aa7dc9 | 2011-02-18 21:57:13 +0100 | [diff] [blame^] | 214 | function set_cookie($name = '', $value = '', $expire = '', $domain = '', $path = '/', $prefix = '', $secure = FALSE) |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 215 | { |
| 216 | if (is_array($name)) |
| 217 | { |
tobiasbg | 9aa7dc9 | 2011-02-18 21:57:13 +0100 | [diff] [blame^] | 218 | // always leave 'name' in last place, as the loop will break otherwise, due to $$item |
| 219 | foreach (array('value', 'expire', 'domain', 'path', 'prefix', 'secure', 'name') as $item) |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 220 | { |
| 221 | if (isset($name[$item])) |
| 222 | { |
| 223 | $$item = $name[$item]; |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | if ($prefix == '' AND config_item('cookie_prefix') != '') |
| 229 | { |
| 230 | $prefix = config_item('cookie_prefix'); |
| 231 | } |
| 232 | if ($domain == '' AND config_item('cookie_domain') != '') |
| 233 | { |
| 234 | $domain = config_item('cookie_domain'); |
| 235 | } |
| 236 | if ($path == '/' AND config_item('cookie_path') != '/') |
| 237 | { |
| 238 | $path = config_item('cookie_path'); |
| 239 | } |
tobiasbg | 9aa7dc9 | 2011-02-18 21:57:13 +0100 | [diff] [blame^] | 240 | if ($secure == FALSE AND config_item('cookie_secure') != FALSE) |
| 241 | { |
| 242 | $secure = config_item('cookie_secure'); |
| 243 | } |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 244 | |
| 245 | if ( ! is_numeric($expire)) |
| 246 | { |
| 247 | $expire = time() - 86500; |
| 248 | } |
| 249 | else |
| 250 | { |
Phil Sturgeon | c808915 | 2010-12-27 19:06:28 +0000 | [diff] [blame] | 251 | $expire = ($expire > 0) ? time() + $expire : 0; |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 252 | } |
| 253 | |
Phil Sturgeon | d8d1e24 | 2011-02-16 17:23:16 +0000 | [diff] [blame] | 254 | setcookie($prefix.$name, $value, $expire, $path, $domain, $secure); |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 255 | } |
| 256 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 257 | // -------------------------------------------------------------------- |
| 258 | |
| 259 | /** |
| 260 | * Fetch an item from the SERVER array |
| 261 | * |
| 262 | * @access public |
| 263 | * @param string |
| 264 | * @param bool |
| 265 | * @return string |
| 266 | */ |
| 267 | function server($index = '', $xss_clean = FALSE) |
| 268 | { |
| 269 | return $this->_fetch_from_array($_SERVER, $index, $xss_clean); |
| 270 | } |
| 271 | |
| 272 | // -------------------------------------------------------------------- |
| 273 | |
| 274 | /** |
| 275 | * Fetch the IP Address |
| 276 | * |
| 277 | * @access public |
| 278 | * @return string |
| 279 | */ |
| 280 | function ip_address() |
| 281 | { |
| 282 | if ($this->ip_address !== FALSE) |
| 283 | { |
| 284 | return $this->ip_address; |
| 285 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 286 | |
Derek Jones | 42b2e17 | 2009-02-05 16:59:45 +0000 | [diff] [blame] | 287 | if (config_item('proxy_ips') != '' && $this->server('HTTP_X_FORWARDED_FOR') && $this->server('REMOTE_ADDR')) |
Derek Jones | c597228 | 2009-02-04 21:40:20 +0000 | [diff] [blame] | 288 | { |
Derek Jones | 42b2e17 | 2009-02-05 16:59:45 +0000 | [diff] [blame] | 289 | $proxies = preg_split('/[\s,]/', config_item('proxy_ips'), -1, PREG_SPLIT_NO_EMPTY); |
Derek Jones | c597228 | 2009-02-04 21:40:20 +0000 | [diff] [blame] | 290 | $proxies = is_array($proxies) ? $proxies : array($proxies); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 291 | |
Derek Jones | c597228 | 2009-02-04 21:40:20 +0000 | [diff] [blame] | 292 | $this->ip_address = in_array($_SERVER['REMOTE_ADDR'], $proxies) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']; |
| 293 | } |
| 294 | elseif ($this->server('REMOTE_ADDR') AND $this->server('HTTP_CLIENT_IP')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 295 | { |
| 296 | $this->ip_address = $_SERVER['HTTP_CLIENT_IP']; |
| 297 | } |
| 298 | elseif ($this->server('REMOTE_ADDR')) |
| 299 | { |
| 300 | $this->ip_address = $_SERVER['REMOTE_ADDR']; |
| 301 | } |
| 302 | elseif ($this->server('HTTP_CLIENT_IP')) |
| 303 | { |
| 304 | $this->ip_address = $_SERVER['HTTP_CLIENT_IP']; |
| 305 | } |
| 306 | elseif ($this->server('HTTP_X_FORWARDED_FOR')) |
| 307 | { |
| 308 | $this->ip_address = $_SERVER['HTTP_X_FORWARDED_FOR']; |
| 309 | } |
| 310 | |
| 311 | if ($this->ip_address === FALSE) |
| 312 | { |
| 313 | $this->ip_address = '0.0.0.0'; |
| 314 | return $this->ip_address; |
| 315 | } |
| 316 | |
Robin Sowell | 76b369e | 2010-03-19 11:15:28 -0400 | [diff] [blame] | 317 | if (strpos($this->ip_address, ',') !== FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 318 | { |
| 319 | $x = explode(',', $this->ip_address); |
Derek Jones | c597228 | 2009-02-04 21:40:20 +0000 | [diff] [blame] | 320 | $this->ip_address = trim(end($x)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | if ( ! $this->valid_ip($this->ip_address)) |
| 324 | { |
| 325 | $this->ip_address = '0.0.0.0'; |
| 326 | } |
| 327 | |
| 328 | return $this->ip_address; |
| 329 | } |
| 330 | |
| 331 | // -------------------------------------------------------------------- |
| 332 | |
| 333 | /** |
| 334 | * Validate IP Address |
| 335 | * |
| 336 | * Updated version suggested by Geert De Deckere |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 337 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 338 | * @access public |
| 339 | * @param string |
| 340 | * @return string |
| 341 | */ |
| 342 | function valid_ip($ip) |
| 343 | { |
| 344 | $ip_segments = explode('.', $ip); |
| 345 | |
| 346 | // Always 4 segments needed |
| 347 | if (count($ip_segments) != 4) |
| 348 | { |
| 349 | return FALSE; |
| 350 | } |
| 351 | // IP can not start with 0 |
| 352 | if ($ip_segments[0][0] == '0') |
| 353 | { |
| 354 | return FALSE; |
| 355 | } |
| 356 | // Check each segment |
| 357 | foreach ($ip_segments as $segment) |
| 358 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 359 | // IP segments must be digits and can not be |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 360 | // longer than 3 digits or greater then 255 |
| 361 | if ($segment == '' OR preg_match("/[^0-9]/", $segment) OR $segment > 255 OR strlen($segment) > 3) |
| 362 | { |
| 363 | return FALSE; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | return TRUE; |
| 368 | } |
| 369 | |
| 370 | // -------------------------------------------------------------------- |
| 371 | |
| 372 | /** |
| 373 | * User Agent |
| 374 | * |
| 375 | * @access public |
| 376 | * @return string |
| 377 | */ |
| 378 | function user_agent() |
| 379 | { |
| 380 | if ($this->user_agent !== FALSE) |
| 381 | { |
| 382 | return $this->user_agent; |
| 383 | } |
| 384 | |
| 385 | $this->user_agent = ( ! isset($_SERVER['HTTP_USER_AGENT'])) ? FALSE : $_SERVER['HTTP_USER_AGENT']; |
| 386 | |
| 387 | return $this->user_agent; |
| 388 | } |
| 389 | |
| 390 | // -------------------------------------------------------------------- |
| 391 | |
| 392 | /** |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 393 | * Sanitize Globals |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 394 | * |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 395 | * This function does the following: |
| 396 | * |
| 397 | * Unsets $_GET data (if query strings are not enabled) |
| 398 | * |
| 399 | * Unsets all globals if register_globals is enabled |
| 400 | * |
| 401 | * Standardizes newline characters to \n |
| 402 | * |
| 403 | * @access private |
| 404 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 405 | */ |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 406 | function _sanitize_globals() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 407 | { |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 408 | // It would be "wrong" to unset any of these GLOBALS. |
Greg Aker | ec2f571 | 2010-11-15 16:22:12 -0600 | [diff] [blame] | 409 | $protected = array('_SERVER', '_GET', '_POST', '_FILES', '_REQUEST', |
| 410 | '_SESSION', '_ENV', 'GLOBALS', 'HTTP_RAW_POST_DATA', |
| 411 | 'system_folder', 'application_folder', 'BM', 'EXT', |
| 412 | 'CFG', 'URI', 'RTR', 'OUT', 'IN'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 413 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 414 | // Unset globals for securiy. |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 415 | // This is effectively the same as register_globals = off |
| 416 | foreach (array($_GET, $_POST, $_COOKIE) as $global) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 417 | { |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 418 | if ( ! is_array($global)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 419 | { |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 420 | if ( ! in_array($global, $protected)) |
| 421 | { |
| 422 | global $$global; |
| 423 | $$global = NULL; |
| 424 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 425 | } |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 426 | else |
| 427 | { |
| 428 | foreach ($global as $key => $val) |
| 429 | { |
| 430 | if ( ! in_array($key, $protected)) |
| 431 | { |
| 432 | global $$key; |
| 433 | $$key = NULL; |
| 434 | } |
| 435 | } |
| 436 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 439 | // Is $_GET data allowed? If not we'll set the $_GET to an empty array |
| 440 | if ($this->_allow_get_array == FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 441 | { |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 442 | $_GET = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 443 | } |
| 444 | else |
| 445 | { |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 446 | if (is_array($_GET) AND count($_GET) > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 447 | { |
Pascal Kriete | 5d5895f | 2011-02-14 13:27:07 -0500 | [diff] [blame] | 448 | foreach ($_GET as $key => $val) |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 449 | { |
| 450 | $_GET[$this->_clean_input_keys($key)] = $this->_clean_input_data($val); |
| 451 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 455 | // Clean $_POST Data |
| 456 | if (is_array($_POST) AND count($_POST) > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 457 | { |
Pascal Kriete | 5d5895f | 2011-02-14 13:27:07 -0500 | [diff] [blame] | 458 | foreach ($_POST as $key => $val) |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 459 | { |
| 460 | $_POST[$this->_clean_input_keys($key)] = $this->_clean_input_data($val); |
| 461 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 464 | // Clean $_COOKIE Data |
| 465 | if (is_array($_COOKIE) AND count($_COOKIE) > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 466 | { |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 467 | // Also get rid of specially treated cookies that might be set by a server |
| 468 | // or silly application, that are of no use to a CI application anyway |
| 469 | // but that when present will trip our 'Disallowed Key Characters' alarm |
| 470 | // http://www.ietf.org/rfc/rfc2109.txt |
| 471 | // note that the key names below are single quoted strings, and are not PHP variables |
| 472 | unset($_COOKIE['$Version']); |
| 473 | unset($_COOKIE['$Path']); |
| 474 | unset($_COOKIE['$Domain']); |
| 475 | |
Pascal Kriete | 5d5895f | 2011-02-14 13:27:07 -0500 | [diff] [blame] | 476 | foreach ($_COOKIE as $key => $val) |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 477 | { |
| 478 | $_COOKIE[$this->_clean_input_keys($key)] = $this->_clean_input_data($val); |
| 479 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 480 | } |
| 481 | |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 482 | // Sanitize PHP_SELF |
| 483 | $_SERVER['PHP_SELF'] = strip_tags($_SERVER['PHP_SELF']); |
| 484 | |
| 485 | |
| 486 | // CSRF Protection check |
| 487 | if ($this->_enable_csrf == TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 488 | { |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 489 | $this->security->csrf_verify(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 490 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 491 | |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 492 | log_message('debug', "Global POST and COOKIE data sanitized"); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | // -------------------------------------------------------------------- |
| 496 | |
| 497 | /** |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 498 | * Clean Input Data |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 499 | * |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 500 | * This is a helper function. It escapes data and |
| 501 | * standardizes newline characters to \n |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 502 | * |
| 503 | * @access private |
| 504 | * @param string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 505 | * @return string |
| 506 | */ |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 507 | function _clean_input_data($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 508 | { |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 509 | if (is_array($str)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 510 | { |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 511 | $new_array = array(); |
| 512 | foreach ($str as $key => $val) |
| 513 | { |
| 514 | $new_array[$this->_clean_input_keys($key)] = $this->_clean_input_data($val); |
| 515 | } |
| 516 | return $new_array; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 519 | // We strip slashes if magic quotes is on to keep things consistent |
Phil Sturgeon | fd69489 | 2010-12-15 10:52:37 +0000 | [diff] [blame] | 520 | if (function_exists('get_magic_quotes_gpc') AND get_magic_quotes_gpc()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 521 | { |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 522 | $str = stripslashes($str); |
| 523 | } |
| 524 | |
| 525 | // Clean UTF-8 if supported |
| 526 | if (UTF8_ENABLED === TRUE) |
| 527 | { |
| 528 | $str = $this->uni->clean_string($str); |
| 529 | } |
| 530 | |
| 531 | // Should we filter the input data? |
| 532 | if ($this->_enable_xss === TRUE) |
| 533 | { |
| 534 | $str = $this->security->xss_clean($str); |
| 535 | } |
| 536 | |
| 537 | // Standardize newlines if needed |
| 538 | if ($this->_standardize_newlines == TRUE) |
| 539 | { |
| 540 | if (strpos($str, "\r") !== FALSE) |
| 541 | { |
Phil Sturgeon | c808915 | 2010-12-27 19:06:28 +0000 | [diff] [blame] | 542 | $str = str_replace(array("\r\n", "\r"), PHP_EOL, $str); |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 543 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | return $str; |
| 547 | } |
| 548 | |
| 549 | // -------------------------------------------------------------------- |
| 550 | |
| 551 | /** |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 552 | * Clean Keys |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 553 | * |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 554 | * This is a helper function. To prevent malicious users |
| 555 | * from trying to exploit keys we make sure that keys are |
| 556 | * only named with alpha-numeric text and a few other items. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 557 | * |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 558 | * @access private |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 559 | * @param string |
| 560 | * @return string |
| 561 | */ |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 562 | function _clean_input_keys($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 563 | { |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 564 | if ( ! preg_match("/^[a-z0-9:_\/-]+$/i", $str)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 565 | { |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 566 | exit('Disallowed Key Characters.'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 567 | } |
| 568 | |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 569 | // Clean UTF-8 if supported |
| 570 | if (UTF8_ENABLED === TRUE) |
| 571 | { |
| 572 | $str = $this->uni->clean_string($str); |
| 573 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 574 | |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 575 | return $str; |
| 576 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 577 | |
Greg Aker | ec2f571 | 2010-11-15 16:22:12 -0600 | [diff] [blame] | 578 | // -------------------------------------------------------------------- |
| 579 | |
| 580 | /** |
| 581 | * Request Headers |
| 582 | * |
| 583 | * In Apache, you can simply call apache_request_headers(), however for |
| 584 | * people running other webservers the function is undefined. |
| 585 | * |
| 586 | * @return array |
| 587 | */ |
| 588 | public function request_headers($xss_clean = FALSE) |
| 589 | { |
| 590 | // Look at Apache go! |
| 591 | if (function_exists('apache_request_headers')) |
| 592 | { |
| 593 | $headers = apache_request_headers(); |
| 594 | } |
| 595 | else |
| 596 | { |
| 597 | $headers['Content-Type'] = (isset($_SERVER['CONTENT_TYPE'])) ? $_SERVER['CONTENT_TYPE'] : @getenv('CONTENT_TYPE'); |
| 598 | |
| 599 | foreach ($_SERVER as $key => $val) |
| 600 | { |
| 601 | if (strncmp($key, 'HTTP_', 5) === 0) |
| 602 | { |
| 603 | $headers[substr($key, 5)] = $this->_fetch_from_array($_SERVER, $key, $xss_clean); |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | // take SOME_HEADER and turn it into Some-Header |
| 609 | foreach ($headers as $key => $val) |
| 610 | { |
| 611 | $key = str_replace('_', ' ', strtolower($key)); |
| 612 | $key = str_replace(' ', '-', ucwords($key)); |
| 613 | |
| 614 | $this->headers[$key] = $val; |
| 615 | } |
| 616 | |
| 617 | return $this->headers; |
| 618 | } |
| 619 | |
| 620 | // -------------------------------------------------------------------- |
| 621 | |
| 622 | /** |
| 623 | * Get Request Header |
| 624 | * |
| 625 | * Returns the value of a single member of the headers class member |
| 626 | * |
| 627 | * @param string array key for $this->headers |
| 628 | * @param boolean XSS Clean or not |
| 629 | * @return mixed FALSE on failure, string on success |
| 630 | */ |
| 631 | public function get_request_header($index, $xss_clean = FALSE) |
| 632 | { |
| 633 | if (empty($this->headers)) |
| 634 | { |
| 635 | $this->request_headers(); |
| 636 | } |
| 637 | |
| 638 | if ( ! isset($this->headers[$index])) |
| 639 | { |
| 640 | return FALSE; |
| 641 | } |
| 642 | |
| 643 | if ($xss_clean === TRUE) |
| 644 | { |
| 645 | $_security =& load_class('Security'); |
| 646 | return $_security->xss_clean($this->headers[$index]); |
| 647 | } |
| 648 | |
| 649 | return $this->headers[$index]; |
| 650 | } |
| 651 | |
Greg Aker | 081ac9d | 2010-11-22 14:42:53 -0600 | [diff] [blame] | 652 | // -------------------------------------------------------------------- |
Phil Sturgeon | c382871 | 2011-01-19 12:31:47 +0000 | [diff] [blame] | 653 | |
Greg Aker | 081ac9d | 2010-11-22 14:42:53 -0600 | [diff] [blame] | 654 | /** |
| 655 | * Is ajax Request? |
| 656 | * |
| 657 | * Test to see if a request contains the HTTP_X_REQUESTED_WITH header |
| 658 | * |
Phil Sturgeon | c382871 | 2011-01-19 12:31:47 +0000 | [diff] [blame] | 659 | * @return boolean |
Greg Aker | 081ac9d | 2010-11-22 14:42:53 -0600 | [diff] [blame] | 660 | */ |
| 661 | public function is_ajax_request() |
| 662 | { |
Greg Aker | 2fae66e | 2010-12-09 15:49:34 -0600 | [diff] [blame] | 663 | return ($this->server('HTTP_X_REQUESTED_WITH') === 'XMLHttpRequest'); |
Greg Aker | 081ac9d | 2010-11-22 14:42:53 -0600 | [diff] [blame] | 664 | } |
| 665 | |
Phil Sturgeon | c382871 | 2011-01-19 12:31:47 +0000 | [diff] [blame] | 666 | // -------------------------------------------------------------------- |
| 667 | |
| 668 | /** |
| 669 | * Is cli Request? |
| 670 | * |
| 671 | * Test to see if a request was made from the command line |
| 672 | * |
| 673 | * @return boolean |
| 674 | */ |
| 675 | public function is_cli_request() |
| 676 | { |
| 677 | return (bool) defined('STDIN'); |
| 678 | } |
| 679 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 680 | } |
| 681 | // END Input class |
| 682 | |
| 683 | /* End of file Input.php */ |
Phil Sturgeon | 33ed0f3 | 2011-02-16 19:03:49 +0000 | [diff] [blame] | 684 | /* Location: ./system/core/Input.php */ |