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