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