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