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