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