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 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 38 | /** |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 39 | * Constructor |
| 40 | * |
| 41 | * Sets whether to globally enable the XSS processing |
| 42 | * and whether to allow the $_GET array |
| 43 | * |
| 44 | */ |
| 45 | public function __construct() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 46 | { |
| 47 | log_message('debug', "Input Class Initialized"); |
| 48 | |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 49 | $this->_allow_get_array = (config_item('enable_query_strings') === TRUE) ? TRUE : FALSE; |
| 50 | $this->_enable_xss = (config_item('global_xss_filtering') === TRUE) ? TRUE : FALSE; |
| 51 | $this->_enable_csrf = (config_item('csrf_protection') === TRUE) ? TRUE : FALSE; |
| 52 | |
| 53 | // Do we need to load the security class? |
| 54 | if ($this->_enable_xss == TRUE OR $this->_enable_csrf == TRUE) |
| 55 | { |
| 56 | $this->security =& load_class('Security'); |
| 57 | } |
| 58 | |
| 59 | // Do we need the Unicode class? |
| 60 | if (UTF8_ENABLED === TRUE) |
| 61 | { |
| 62 | global $UNI; |
| 63 | $this->uni =& $UNI; |
| 64 | } |
| 65 | |
| 66 | // Sanitize global arrays |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 67 | $this->_sanitize_globals(); |
| 68 | } |
| 69 | |
| 70 | // -------------------------------------------------------------------- |
| 71 | |
| 72 | /** |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 73 | * Fetch from array |
| 74 | * |
| 75 | * This is a helper function to retrieve values from global arrays |
| 76 | * |
| 77 | * @access private |
| 78 | * @param array |
| 79 | * @param string |
| 80 | * @param bool |
| 81 | * @return string |
| 82 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 83 | function _fetch_from_array(&$array, $index = '', $xss_clean = FALSE) |
| 84 | { |
| 85 | if ( ! isset($array[$index])) |
| 86 | { |
| 87 | return FALSE; |
| 88 | } |
| 89 | |
| 90 | if ($xss_clean === TRUE) |
| 91 | { |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 92 | $_security =& load_class('Security'); |
| 93 | return $_security->xss_clean($array[$index]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | return $array[$index]; |
| 97 | } |
| 98 | |
| 99 | // -------------------------------------------------------------------- |
| 100 | |
| 101 | /** |
| 102 | * Fetch an item from the GET array |
| 103 | * |
| 104 | * @access public |
| 105 | * @param string |
| 106 | * @param bool |
| 107 | * @return string |
| 108 | */ |
| 109 | function get($index = '', $xss_clean = FALSE) |
| 110 | { |
| 111 | return $this->_fetch_from_array($_GET, $index, $xss_clean); |
| 112 | } |
| 113 | |
| 114 | // -------------------------------------------------------------------- |
| 115 | |
| 116 | /** |
| 117 | * Fetch an item from the POST array |
| 118 | * |
| 119 | * @access public |
| 120 | * @param string |
| 121 | * @param bool |
| 122 | * @return string |
| 123 | */ |
| 124 | function post($index = '', $xss_clean = FALSE) |
| 125 | { |
| 126 | return $this->_fetch_from_array($_POST, $index, $xss_clean); |
| 127 | } |
| 128 | |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 129 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 130 | // -------------------------------------------------------------------- |
| 131 | |
| 132 | /** |
| 133 | * Fetch an item from either the GET array or the POST |
| 134 | * |
| 135 | * @access public |
| 136 | * @param string The index key |
| 137 | * @param bool XSS cleaning |
| 138 | * @return string |
| 139 | */ |
| 140 | function get_post($index = '', $xss_clean = FALSE) |
| 141 | { |
| 142 | if ( ! isset($_POST[$index]) ) |
| 143 | { |
| 144 | return $this->get($index, $xss_clean); |
| 145 | } |
| 146 | else |
| 147 | { |
| 148 | return $this->post($index, $xss_clean); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | // -------------------------------------------------------------------- |
| 153 | |
| 154 | /** |
| 155 | * Fetch an item from the COOKIE array |
| 156 | * |
| 157 | * @access public |
| 158 | * @param string |
| 159 | * @param bool |
| 160 | * @return string |
| 161 | */ |
| 162 | function cookie($index = '', $xss_clean = FALSE) |
| 163 | { |
| 164 | return $this->_fetch_from_array($_COOKIE, $index, $xss_clean); |
| 165 | } |
| 166 | |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 167 | // ------------------------------------------------------------------------ |
| 168 | |
| 169 | /** |
| 170 | * Set cookie |
| 171 | * |
| 172 | * Accepts six parameter, or you can submit an associative |
| 173 | * array in the first parameter containing all the values. |
| 174 | * |
| 175 | * @access public |
| 176 | * @param mixed |
| 177 | * @param string the value of the cookie |
| 178 | * @param string the number of seconds until expiration |
| 179 | * @param string the cookie domain. Usually: .yourdomain.com |
| 180 | * @param string the cookie path |
| 181 | * @param string the cookie prefix |
| 182 | * @return void |
| 183 | */ |
| 184 | function set_cookie($name = '', $value = '', $expire = '', $domain = '', $path = '/', $prefix = '') |
| 185 | { |
| 186 | if (is_array($name)) |
| 187 | { |
| 188 | foreach (array('value', 'expire', 'domain', 'path', 'prefix', 'name') as $item) |
| 189 | { |
| 190 | if (isset($name[$item])) |
| 191 | { |
| 192 | $$item = $name[$item]; |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | if ($prefix == '' AND config_item('cookie_prefix') != '') |
| 198 | { |
| 199 | $prefix = config_item('cookie_prefix'); |
| 200 | } |
| 201 | if ($domain == '' AND config_item('cookie_domain') != '') |
| 202 | { |
| 203 | $domain = config_item('cookie_domain'); |
| 204 | } |
| 205 | if ($path == '/' AND config_item('cookie_path') != '/') |
| 206 | { |
| 207 | $path = config_item('cookie_path'); |
| 208 | } |
| 209 | |
| 210 | if ( ! is_numeric($expire)) |
| 211 | { |
| 212 | $expire = time() - 86500; |
| 213 | } |
| 214 | else |
| 215 | { |
| 216 | if ($expire > 0) |
| 217 | { |
| 218 | $expire = time() + $expire; |
| 219 | } |
| 220 | else |
| 221 | { |
| 222 | $expire = 0; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | setcookie($prefix.$name, $value, $expire, $path, $domain, 0); |
| 227 | } |
| 228 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 229 | // -------------------------------------------------------------------- |
| 230 | |
| 231 | /** |
| 232 | * Fetch an item from the SERVER array |
| 233 | * |
| 234 | * @access public |
| 235 | * @param string |
| 236 | * @param bool |
| 237 | * @return string |
| 238 | */ |
| 239 | function server($index = '', $xss_clean = FALSE) |
| 240 | { |
| 241 | return $this->_fetch_from_array($_SERVER, $index, $xss_clean); |
| 242 | } |
| 243 | |
| 244 | // -------------------------------------------------------------------- |
| 245 | |
| 246 | /** |
| 247 | * Fetch the IP Address |
| 248 | * |
| 249 | * @access public |
| 250 | * @return string |
| 251 | */ |
| 252 | function ip_address() |
| 253 | { |
| 254 | if ($this->ip_address !== FALSE) |
| 255 | { |
| 256 | return $this->ip_address; |
| 257 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 258 | |
Derek Jones | 42b2e17 | 2009-02-05 16:59:45 +0000 | [diff] [blame] | 259 | 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] | 260 | { |
Derek Jones | 42b2e17 | 2009-02-05 16:59:45 +0000 | [diff] [blame] | 261 | $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] | 262 | $proxies = is_array($proxies) ? $proxies : array($proxies); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 263 | |
Derek Jones | c597228 | 2009-02-04 21:40:20 +0000 | [diff] [blame] | 264 | $this->ip_address = in_array($_SERVER['REMOTE_ADDR'], $proxies) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']; |
| 265 | } |
| 266 | elseif ($this->server('REMOTE_ADDR') AND $this->server('HTTP_CLIENT_IP')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 267 | { |
| 268 | $this->ip_address = $_SERVER['HTTP_CLIENT_IP']; |
| 269 | } |
| 270 | elseif ($this->server('REMOTE_ADDR')) |
| 271 | { |
| 272 | $this->ip_address = $_SERVER['REMOTE_ADDR']; |
| 273 | } |
| 274 | elseif ($this->server('HTTP_CLIENT_IP')) |
| 275 | { |
| 276 | $this->ip_address = $_SERVER['HTTP_CLIENT_IP']; |
| 277 | } |
| 278 | elseif ($this->server('HTTP_X_FORWARDED_FOR')) |
| 279 | { |
| 280 | $this->ip_address = $_SERVER['HTTP_X_FORWARDED_FOR']; |
| 281 | } |
| 282 | |
| 283 | if ($this->ip_address === FALSE) |
| 284 | { |
| 285 | $this->ip_address = '0.0.0.0'; |
| 286 | return $this->ip_address; |
| 287 | } |
| 288 | |
Robin Sowell | 76b369e | 2010-03-19 11:15:28 -0400 | [diff] [blame] | 289 | if (strpos($this->ip_address, ',') !== FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 290 | { |
| 291 | $x = explode(',', $this->ip_address); |
Derek Jones | c597228 | 2009-02-04 21:40:20 +0000 | [diff] [blame] | 292 | $this->ip_address = trim(end($x)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | if ( ! $this->valid_ip($this->ip_address)) |
| 296 | { |
| 297 | $this->ip_address = '0.0.0.0'; |
| 298 | } |
| 299 | |
| 300 | return $this->ip_address; |
| 301 | } |
| 302 | |
| 303 | // -------------------------------------------------------------------- |
| 304 | |
| 305 | /** |
| 306 | * Validate IP Address |
| 307 | * |
| 308 | * Updated version suggested by Geert De Deckere |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 309 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 310 | * @access public |
| 311 | * @param string |
| 312 | * @return string |
| 313 | */ |
| 314 | function valid_ip($ip) |
| 315 | { |
| 316 | $ip_segments = explode('.', $ip); |
| 317 | |
| 318 | // Always 4 segments needed |
| 319 | if (count($ip_segments) != 4) |
| 320 | { |
| 321 | return FALSE; |
| 322 | } |
| 323 | // IP can not start with 0 |
| 324 | if ($ip_segments[0][0] == '0') |
| 325 | { |
| 326 | return FALSE; |
| 327 | } |
| 328 | // Check each segment |
| 329 | foreach ($ip_segments as $segment) |
| 330 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 331 | // IP segments must be digits and can not be |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 332 | // longer than 3 digits or greater then 255 |
| 333 | if ($segment == '' OR preg_match("/[^0-9]/", $segment) OR $segment > 255 OR strlen($segment) > 3) |
| 334 | { |
| 335 | return FALSE; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | return TRUE; |
| 340 | } |
| 341 | |
| 342 | // -------------------------------------------------------------------- |
| 343 | |
| 344 | /** |
| 345 | * User Agent |
| 346 | * |
| 347 | * @access public |
| 348 | * @return string |
| 349 | */ |
| 350 | function user_agent() |
| 351 | { |
| 352 | if ($this->user_agent !== FALSE) |
| 353 | { |
| 354 | return $this->user_agent; |
| 355 | } |
| 356 | |
| 357 | $this->user_agent = ( ! isset($_SERVER['HTTP_USER_AGENT'])) ? FALSE : $_SERVER['HTTP_USER_AGENT']; |
| 358 | |
| 359 | return $this->user_agent; |
| 360 | } |
| 361 | |
| 362 | // -------------------------------------------------------------------- |
| 363 | |
| 364 | /** |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 365 | * Sanitize Globals |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 366 | * |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 367 | * This function does the following: |
| 368 | * |
| 369 | * Unsets $_GET data (if query strings are not enabled) |
| 370 | * |
| 371 | * Unsets all globals if register_globals is enabled |
| 372 | * |
| 373 | * Standardizes newline characters to \n |
| 374 | * |
| 375 | * @access private |
| 376 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 377 | */ |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 378 | function _sanitize_globals() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 379 | { |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 380 | // It would be "wrong" to unset any of these GLOBALS. |
| 381 | $protected = array('_SERVER', '_GET', '_POST', '_FILES', '_REQUEST', '_SESSION', '_ENV', 'GLOBALS', 'HTTP_RAW_POST_DATA', |
| 382 | 'system_folder', 'application_folder', 'BM', 'EXT', 'CFG', 'URI', 'RTR', 'OUT', 'IN'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 383 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 384 | // Unset globals for securiy. |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 385 | // This is effectively the same as register_globals = off |
| 386 | foreach (array($_GET, $_POST, $_COOKIE) as $global) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 387 | { |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 388 | if ( ! is_array($global)) |
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 | if ( ! in_array($global, $protected)) |
| 391 | { |
| 392 | global $$global; |
| 393 | $$global = NULL; |
| 394 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 395 | } |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 396 | else |
| 397 | { |
| 398 | foreach ($global as $key => $val) |
| 399 | { |
| 400 | if ( ! in_array($key, $protected)) |
| 401 | { |
| 402 | global $$key; |
| 403 | $$key = NULL; |
| 404 | } |
| 405 | } |
| 406 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 409 | // Is $_GET data allowed? If not we'll set the $_GET to an empty array |
| 410 | if ($this->_allow_get_array == FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 411 | { |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 412 | $_GET = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 413 | } |
| 414 | else |
| 415 | { |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 416 | if (is_array($_GET) AND count($_GET) > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 417 | { |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 418 | foreach($_GET as $key => $val) |
| 419 | { |
| 420 | $_GET[$this->_clean_input_keys($key)] = $this->_clean_input_data($val); |
| 421 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 422 | } |
| 423 | } |
| 424 | |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 425 | // Clean $_POST Data |
| 426 | if (is_array($_POST) AND count($_POST) > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 427 | { |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 428 | foreach($_POST as $key => $val) |
| 429 | { |
| 430 | $_POST[$this->_clean_input_keys($key)] = $this->_clean_input_data($val); |
| 431 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 434 | // Clean $_COOKIE Data |
| 435 | if (is_array($_COOKIE) AND count($_COOKIE) > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 436 | { |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 437 | // Also get rid of specially treated cookies that might be set by a server |
| 438 | // or silly application, that are of no use to a CI application anyway |
| 439 | // but that when present will trip our 'Disallowed Key Characters' alarm |
| 440 | // http://www.ietf.org/rfc/rfc2109.txt |
| 441 | // note that the key names below are single quoted strings, and are not PHP variables |
| 442 | unset($_COOKIE['$Version']); |
| 443 | unset($_COOKIE['$Path']); |
| 444 | unset($_COOKIE['$Domain']); |
| 445 | |
| 446 | foreach($_COOKIE as $key => $val) |
| 447 | { |
| 448 | $_COOKIE[$this->_clean_input_keys($key)] = $this->_clean_input_data($val); |
| 449 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 452 | // Sanitize PHP_SELF |
| 453 | $_SERVER['PHP_SELF'] = strip_tags($_SERVER['PHP_SELF']); |
| 454 | |
| 455 | |
| 456 | // CSRF Protection check |
| 457 | if ($this->_enable_csrf == TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 458 | { |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 459 | $this->security->csrf_verify(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 460 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 461 | |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 462 | log_message('debug', "Global POST and COOKIE data sanitized"); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | // -------------------------------------------------------------------- |
| 466 | |
| 467 | /** |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 468 | * Clean Input Data |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 469 | * |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 470 | * This is a helper function. It escapes data and |
| 471 | * standardizes newline characters to \n |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 472 | * |
| 473 | * @access private |
| 474 | * @param string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 475 | * @return string |
| 476 | */ |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 477 | function _clean_input_data($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 478 | { |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 479 | if (is_array($str)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 480 | { |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 481 | $new_array = array(); |
| 482 | foreach ($str as $key => $val) |
| 483 | { |
| 484 | $new_array[$this->_clean_input_keys($key)] = $this->_clean_input_data($val); |
| 485 | } |
| 486 | return $new_array; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 489 | // We strip slashes if magic quotes is on to keep things consistent |
| 490 | if (get_magic_quotes_gpc()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 491 | { |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 492 | $str = stripslashes($str); |
| 493 | } |
| 494 | |
| 495 | // Clean UTF-8 if supported |
| 496 | if (UTF8_ENABLED === TRUE) |
| 497 | { |
| 498 | $str = $this->uni->clean_string($str); |
| 499 | } |
| 500 | |
| 501 | // Should we filter the input data? |
| 502 | if ($this->_enable_xss === TRUE) |
| 503 | { |
| 504 | $str = $this->security->xss_clean($str); |
| 505 | } |
| 506 | |
| 507 | // Standardize newlines if needed |
| 508 | if ($this->_standardize_newlines == TRUE) |
| 509 | { |
| 510 | if (strpos($str, "\r") !== FALSE) |
| 511 | { |
| 512 | $str = str_replace(array("\r\n", "\r"), "\n", $str); |
| 513 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | return $str; |
| 517 | } |
| 518 | |
| 519 | // -------------------------------------------------------------------- |
| 520 | |
| 521 | /** |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 522 | * Clean Keys |
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 | * This is a helper function. To prevent malicious users |
| 525 | * from trying to exploit keys we make sure that keys are |
| 526 | * only named with alpha-numeric text and a few other items. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 527 | * |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 528 | * @access private |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 529 | * @param string |
| 530 | * @return string |
| 531 | */ |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 532 | function _clean_input_keys($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 533 | { |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 534 | if ( ! preg_match("/^[a-z0-9:_\/-]+$/i", $str)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 535 | { |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 536 | exit('Disallowed Key Characters.'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 539 | // Clean UTF-8 if supported |
| 540 | if (UTF8_ENABLED === TRUE) |
| 541 | { |
| 542 | $str = $this->uni->clean_string($str); |
| 543 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 544 | |
Derek Jones | 69fc4fc | 2010-03-02 13:36:31 -0600 | [diff] [blame] | 545 | return $str; |
| 546 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 547 | |
| 548 | } |
| 549 | // END Input class |
| 550 | |
| 551 | /* End of file Input.php */ |
Derek Jones | c68dfbf | 2010-03-02 12:59:23 -0600 | [diff] [blame] | 552 | /* Location: ./system/core/Input.php */ |