blob: f8e89066e7e58e24d46be7728a45eebedbb64581 [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Greg Aker0711dc82011-01-05 10:49:40 -06009 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @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 */
29class CI_Input {
Derek Allard2067d1a2008-11-13 22:59:24 +000030
David Behler9b5df592011-08-14 21:04:17 +020031 /**
32 * IP address of the current user
33 *
34 * @var string
35 */
Derek Jones69fc4fc2010-03-02 13:36:31 -060036 var $ip_address = FALSE;
David Behler9b5df592011-08-14 21:04:17 +020037 /**
38 * user agent (web browser) being used by the current user
39 *
40 * @var string
41 */
Derek Jones69fc4fc2010-03-02 13:36:31 -060042 var $user_agent = FALSE;
David Behler9b5df592011-08-14 21:04:17 +020043 /**
44 * If FALSE, then $_GET will be set to an empty array
45 *
46 * @var bool
47 */
Dan Horrigan65d603e2010-12-15 08:38:30 -050048 var $_allow_get_array = TRUE;
David Behler9b5df592011-08-14 21:04:17 +020049 /**
50 * If TRUE, then newlines are standardized
51 *
52 * @var bool
53 */
Derek Jones69fc4fc2010-03-02 13:36:31 -060054 var $_standardize_newlines = TRUE;
David Behler9b5df592011-08-14 21:04:17 +020055 /**
56 * Determines whether the XSS filter is always active when GET, POST or COOKIE data is encountered
57 * Set automatically based on config setting
58 *
59 * @var bool
60 */
61 var $_enable_xss = FALSE;
62 /**
63 * Enables a CSRF cookie token to be set.
64 * Set automatically based on config setting
65 *
66 * @var bool
67 */
68 var $_enable_csrf = FALSE;
69 /**
70 * List of all HTTP request headers
71 *
72 * @var array
73 */
Greg Akerec2f5712010-11-15 16:22:12 -060074 protected $headers = array();
David Behler9b5df592011-08-14 21:04:17 +020075
Greg Akerec2f5712010-11-15 16:22:12 -060076
Derek Allard2067d1a2008-11-13 22:59:24 +000077 /**
Greg Akera9263282010-11-10 15:26:43 -060078 * Constructor
79 *
80 * Sets whether to globally enable the XSS processing
81 * and whether to allow the $_GET array
82 *
83 */
84 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000085 {
86 log_message('debug', "Input Class Initialized");
87
Phil Sturgeonc8089152010-12-27 19:06:28 +000088 $this->_allow_get_array = (config_item('allow_get_array') === TRUE);
89 $this->_enable_xss = (config_item('global_xss_filtering') === TRUE);
90 $this->_enable_csrf = (config_item('csrf_protection') === TRUE);
Derek Jones69fc4fc2010-03-02 13:36:31 -060091
Pascal Kriete14a0ac62011-04-05 14:55:56 -040092 global $SEC;
93 $this->security =& $SEC;
Derek Jones69fc4fc2010-03-02 13:36:31 -060094
Pascal Krieteaaec1e42011-01-20 00:01:21 -050095 // Do we need the UTF-8 class?
Derek Jones69fc4fc2010-03-02 13:36:31 -060096 if (UTF8_ENABLED === TRUE)
97 {
98 global $UNI;
99 $this->uni =& $UNI;
100 }
101
102 // Sanitize global arrays
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 $this->_sanitize_globals();
104 }
105
106 // --------------------------------------------------------------------
107
108 /**
Greg Akera9263282010-11-10 15:26:43 -0600109 * Fetch from array
110 *
111 * This is a helper function to retrieve values from global arrays
112 *
Bo-Yi Wu47213792011-09-13 22:44:07 +0800113 * @access protected
Greg Akera9263282010-11-10 15:26:43 -0600114 * @param array
115 * @param string
116 * @param bool
117 * @return string
118 */
Bo-Yi Wu47213792011-09-13 22:44:07 +0800119 protected function _fetch_from_array(&$array, $index = '', $xss_clean = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 {
121 if ( ! isset($array[$index]))
122 {
123 return FALSE;
124 }
125
126 if ($xss_clean === TRUE)
127 {
Pascal Kriete14a0ac62011-04-05 14:55:56 -0400128 return $this->security->xss_clean($array[$index]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 }
130
131 return $array[$index];
132 }
133
134 // --------------------------------------------------------------------
135
136 /**
137 * Fetch an item from the GET array
138 *
139 * @access public
140 * @param string
141 * @param bool
142 * @return string
143 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800144 public function get($index = NULL, $xss_clean = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 {
Phil Sturgeon44f21052011-02-15 21:39:25 +0000146 // Check if a field has been provided
147 if ($index === NULL AND ! empty($_GET))
vascopjff1cfa12011-02-13 21:30:19 +0000148 {
Phil Sturgeon44f21052011-02-15 21:39:25 +0000149 $get = array();
vascopjff1cfa12011-02-13 21:30:19 +0000150
151 // loop through the full _GET array
Phil Sturgeon44f21052011-02-15 21:39:25 +0000152 foreach (array_keys($_GET) as $key)
vascopjff1cfa12011-02-13 21:30:19 +0000153 {
Phil Sturgeon44f21052011-02-15 21:39:25 +0000154 $get[$key] = $this->_fetch_from_array($_GET, $key, $xss_clean);
vascopjff1cfa12011-02-13 21:30:19 +0000155 }
Phil Sturgeon44f21052011-02-15 21:39:25 +0000156 return $get;
vascopjff1cfa12011-02-13 21:30:19 +0000157 }
158
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 return $this->_fetch_from_array($_GET, $index, $xss_clean);
160 }
161
162 // --------------------------------------------------------------------
163
164 /**
165 * Fetch an item from the POST array
166 *
167 * @access public
168 * @param string
169 * @param bool
170 * @return string
171 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800172 public function post($index = NULL, $xss_clean = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 {
Phil Sturgeon44f21052011-02-15 21:39:25 +0000174 // Check if a field has been provided
175 if ($index === NULL AND ! empty($_POST))
vascopj0ba58b82011-02-06 14:20:21 +0000176 {
Phil Sturgeon44f21052011-02-15 21:39:25 +0000177 $post = array();
vascopj0ba58b82011-02-06 14:20:21 +0000178
Phil Sturgeon44f21052011-02-15 21:39:25 +0000179 // Loop through the full _POST array and return it
180 foreach (array_keys($_POST) as $key)
vascopj0ba58b82011-02-06 14:20:21 +0000181 {
Phil Sturgeon44f21052011-02-15 21:39:25 +0000182 $post[$key] = $this->_fetch_from_array($_POST, $key, $xss_clean);
vascopj0ba58b82011-02-06 14:20:21 +0000183 }
Phil Sturgeon44f21052011-02-15 21:39:25 +0000184 return $post;
vascopj0ba58b82011-02-06 14:20:21 +0000185 }
David Behler9b5df592011-08-14 21:04:17 +0200186
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 return $this->_fetch_from_array($_POST, $index, $xss_clean);
188 }
189
Derek Jones69fc4fc2010-03-02 13:36:31 -0600190
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 // --------------------------------------------------------------------
192
193 /**
194 * Fetch an item from either the GET array or the POST
195 *
196 * @access public
197 * @param string The index key
198 * @param bool XSS cleaning
199 * @return string
200 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800201 public function get_post($index = '', $xss_clean = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 {
203 if ( ! isset($_POST[$index]) )
204 {
205 return $this->get($index, $xss_clean);
206 }
207 else
208 {
209 return $this->post($index, $xss_clean);
210 }
211 }
212
213 // --------------------------------------------------------------------
214
215 /**
216 * Fetch an item from the COOKIE array
217 *
218 * @access public
219 * @param string
220 * @param bool
221 * @return string
222 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800223 public function cookie($index = '', $xss_clean = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 {
225 return $this->_fetch_from_array($_COOKIE, $index, $xss_clean);
226 }
227
Derek Jones69fc4fc2010-03-02 13:36:31 -0600228 // ------------------------------------------------------------------------
229
230 /**
231 * Set cookie
232 *
233 * Accepts six parameter, or you can submit an associative
234 * array in the first parameter containing all the values.
235 *
236 * @access public
237 * @param mixed
238 * @param string the value of the cookie
239 * @param string the number of seconds until expiration
Derek Jones37f4b9c2011-07-01 17:56:50 -0500240 * @param string the cookie domain. Usually: .yourdomain.com
Derek Jones69fc4fc2010-03-02 13:36:31 -0600241 * @param string the cookie path
242 * @param string the cookie prefix
Phil Sturgeond8d1e242011-02-16 17:23:16 +0000243 * @param bool true makes the cookie secure
Derek Jones69fc4fc2010-03-02 13:36:31 -0600244 * @return void
245 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800246 public function set_cookie($name = '', $value = '', $expire = '', $domain = '', $path = '/', $prefix = '', $secure = FALSE)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600247 {
248 if (is_array($name))
249 {
tobiasbg9aa7dc92011-02-18 21:57:13 +0100250 // always leave 'name' in last place, as the loop will break otherwise, due to $$item
251 foreach (array('value', 'expire', 'domain', 'path', 'prefix', 'secure', 'name') as $item)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600252 {
253 if (isset($name[$item]))
254 {
255 $$item = $name[$item];
256 }
257 }
258 }
259
260 if ($prefix == '' AND config_item('cookie_prefix') != '')
261 {
262 $prefix = config_item('cookie_prefix');
263 }
264 if ($domain == '' AND config_item('cookie_domain') != '')
265 {
266 $domain = config_item('cookie_domain');
267 }
268 if ($path == '/' AND config_item('cookie_path') != '/')
269 {
270 $path = config_item('cookie_path');
271 }
tobiasbg9aa7dc92011-02-18 21:57:13 +0100272 if ($secure == FALSE AND config_item('cookie_secure') != FALSE)
273 {
274 $secure = config_item('cookie_secure');
275 }
Derek Jones69fc4fc2010-03-02 13:36:31 -0600276
277 if ( ! is_numeric($expire))
278 {
279 $expire = time() - 86500;
280 }
281 else
282 {
Phil Sturgeonc8089152010-12-27 19:06:28 +0000283 $expire = ($expire > 0) ? time() + $expire : 0;
Derek Jones69fc4fc2010-03-02 13:36:31 -0600284 }
285
Phil Sturgeond8d1e242011-02-16 17:23:16 +0000286 setcookie($prefix.$name, $value, $expire, $path, $domain, $secure);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600287 }
288
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 // --------------------------------------------------------------------
290
291 /**
292 * Fetch an item from the SERVER array
293 *
294 * @access public
295 * @param string
296 * @param bool
297 * @return string
298 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800299 public function server($index = '', $xss_clean = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 {
301 return $this->_fetch_from_array($_SERVER, $index, $xss_clean);
302 }
303
304 // --------------------------------------------------------------------
305
306 /**
307 * Fetch the IP Address
308 *
309 * @access public
310 * @return string
311 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800312 public function ip_address()
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 {
314 if ($this->ip_address !== FALSE)
315 {
316 return $this->ip_address;
317 }
Barry Mienydd671972010-10-04 16:33:58 +0200318
Derek Jones42b2e172009-02-05 16:59:45 +0000319 if (config_item('proxy_ips') != '' && $this->server('HTTP_X_FORWARDED_FOR') && $this->server('REMOTE_ADDR'))
Derek Jonesc5972282009-02-04 21:40:20 +0000320 {
Derek Jones42b2e172009-02-05 16:59:45 +0000321 $proxies = preg_split('/[\s,]/', config_item('proxy_ips'), -1, PREG_SPLIT_NO_EMPTY);
Derek Jonesc5972282009-02-04 21:40:20 +0000322 $proxies = is_array($proxies) ? $proxies : array($proxies);
Derek Allard2067d1a2008-11-13 22:59:24 +0000323
Derek Jonesc5972282009-02-04 21:40:20 +0000324 $this->ip_address = in_array($_SERVER['REMOTE_ADDR'], $proxies) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
325 }
John Bellone16f27b42011-08-21 11:45:11 -0400326 elseif (! $this->server('HTTP_CLIENT_IP') AND $this->server('REMOTE_ADDR'))
John Bellone52c10b62011-08-21 11:41:32 -0400327 {
328 $this->ip_address = $_SERVER['REMOTE_ADDR'];
329 }
Derek Jonesc5972282009-02-04 21:40:20 +0000330 elseif ($this->server('REMOTE_ADDR') AND $this->server('HTTP_CLIENT_IP'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 {
332 $this->ip_address = $_SERVER['HTTP_CLIENT_IP'];
333 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 elseif ($this->server('HTTP_CLIENT_IP'))
335 {
336 $this->ip_address = $_SERVER['HTTP_CLIENT_IP'];
337 }
338 elseif ($this->server('HTTP_X_FORWARDED_FOR'))
339 {
340 $this->ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
341 }
342
343 if ($this->ip_address === FALSE)
344 {
345 $this->ip_address = '0.0.0.0';
346 return $this->ip_address;
347 }
348
Robin Sowell76b369e2010-03-19 11:15:28 -0400349 if (strpos($this->ip_address, ',') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 {
351 $x = explode(',', $this->ip_address);
Derek Jonesc5972282009-02-04 21:40:20 +0000352 $this->ip_address = trim(end($x));
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 }
354
355 if ( ! $this->valid_ip($this->ip_address))
356 {
357 $this->ip_address = '0.0.0.0';
358 }
359
360 return $this->ip_address;
361 }
362
363 // --------------------------------------------------------------------
364
365 /**
366 * Validate IP Address
367 *
368 * Updated version suggested by Geert De Deckere
Barry Mienydd671972010-10-04 16:33:58 +0200369 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 * @access public
371 * @param string
Bo-Yi Wu013c8952011-09-12 15:03:44 +0800372 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800374 public function valid_ip($ip)
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 {
Bo-Yi Wuc9f84c12011-09-12 10:45:39 +0800376 // if php version >= 5.2, use filter_var to check validate ip.
Bo-Yi Wu47213792011-09-13 22:44:07 +0800377 if (function_exists('filter_var'))
Bo-Yi Wuc9f84c12011-09-12 10:45:39 +0800378 {
379 return (bool) filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
380 }
381
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 $ip_segments = explode('.', $ip);
383
384 // Always 4 segments needed
385 if (count($ip_segments) != 4)
386 {
387 return FALSE;
388 }
389 // IP can not start with 0
390 if ($ip_segments[0][0] == '0')
391 {
392 return FALSE;
393 }
394 // Check each segment
395 foreach ($ip_segments as $segment)
396 {
Barry Mienydd671972010-10-04 16:33:58 +0200397 // IP segments must be digits and can not be
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 // longer than 3 digits or greater then 255
399 if ($segment == '' OR preg_match("/[^0-9]/", $segment) OR $segment > 255 OR strlen($segment) > 3)
400 {
401 return FALSE;
402 }
403 }
404
405 return TRUE;
406 }
407
408 // --------------------------------------------------------------------
409
410 /**
411 * User Agent
412 *
413 * @access public
414 * @return string
415 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800416 public function user_agent()
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 {
418 if ($this->user_agent !== FALSE)
419 {
420 return $this->user_agent;
421 }
422
423 $this->user_agent = ( ! isset($_SERVER['HTTP_USER_AGENT'])) ? FALSE : $_SERVER['HTTP_USER_AGENT'];
424
425 return $this->user_agent;
426 }
427
428 // --------------------------------------------------------------------
429
430 /**
Derek Jones69fc4fc2010-03-02 13:36:31 -0600431 * Sanitize Globals
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 *
Derek Jones69fc4fc2010-03-02 13:36:31 -0600433 * This function does the following:
434 *
435 * Unsets $_GET data (if query strings are not enabled)
436 *
437 * Unsets all globals if register_globals is enabled
438 *
439 * Standardizes newline characters to \n
440 *
441 * @access private
442 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800444 private function _sanitize_globals()
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600446 // It would be "wrong" to unset any of these GLOBALS.
David Behler9b5df592011-08-14 21:04:17 +0200447 $protected = array('_SERVER', '_GET', '_POST', '_FILES', '_REQUEST',
Greg Akerec2f5712010-11-15 16:22:12 -0600448 '_SESSION', '_ENV', 'GLOBALS', 'HTTP_RAW_POST_DATA',
David Behler9b5df592011-08-14 21:04:17 +0200449 'system_folder', 'application_folder', 'BM', 'EXT',
Greg Akerec2f5712010-11-15 16:22:12 -0600450 'CFG', 'URI', 'RTR', 'OUT', 'IN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000451
Barry Mienydd671972010-10-04 16:33:58 +0200452 // Unset globals for securiy.
Derek Jones69fc4fc2010-03-02 13:36:31 -0600453 // This is effectively the same as register_globals = off
454 foreach (array($_GET, $_POST, $_COOKIE) as $global)
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600456 if ( ! is_array($global))
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600458 if ( ! in_array($global, $protected))
459 {
460 global $$global;
461 $$global = NULL;
462 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 }
Derek Jones69fc4fc2010-03-02 13:36:31 -0600464 else
465 {
466 foreach ($global as $key => $val)
467 {
468 if ( ! in_array($key, $protected))
469 {
470 global $$key;
471 $$key = NULL;
472 }
473 }
474 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 }
476
Derek Jones69fc4fc2010-03-02 13:36:31 -0600477 // Is $_GET data allowed? If not we'll set the $_GET to an empty array
478 if ($this->_allow_get_array == FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600480 $_GET = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 }
482 else
483 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600484 if (is_array($_GET) AND count($_GET) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 {
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500486 foreach ($_GET as $key => $val)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600487 {
488 $_GET[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
489 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 }
491 }
492
Derek Jones69fc4fc2010-03-02 13:36:31 -0600493 // Clean $_POST Data
494 if (is_array($_POST) AND count($_POST) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 {
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500496 foreach ($_POST as $key => $val)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600497 {
498 $_POST[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
499 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 }
501
Derek Jones69fc4fc2010-03-02 13:36:31 -0600502 // Clean $_COOKIE Data
503 if (is_array($_COOKIE) AND count($_COOKIE) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600505 // Also get rid of specially treated cookies that might be set by a server
506 // or silly application, that are of no use to a CI application anyway
507 // but that when present will trip our 'Disallowed Key Characters' alarm
508 // http://www.ietf.org/rfc/rfc2109.txt
509 // note that the key names below are single quoted strings, and are not PHP variables
510 unset($_COOKIE['$Version']);
511 unset($_COOKIE['$Path']);
512 unset($_COOKIE['$Domain']);
513
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500514 foreach ($_COOKIE as $key => $val)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600515 {
516 $_COOKIE[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
517 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 }
519
Derek Jones69fc4fc2010-03-02 13:36:31 -0600520 // Sanitize PHP_SELF
521 $_SERVER['PHP_SELF'] = strip_tags($_SERVER['PHP_SELF']);
522
523
524 // CSRF Protection check
525 if ($this->_enable_csrf == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600527 $this->security->csrf_verify();
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000529
Derek Jones69fc4fc2010-03-02 13:36:31 -0600530 log_message('debug', "Global POST and COOKIE data sanitized");
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 }
532
533 // --------------------------------------------------------------------
534
535 /**
Derek Jones69fc4fc2010-03-02 13:36:31 -0600536 * Clean Input Data
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 *
Derek Jones69fc4fc2010-03-02 13:36:31 -0600538 * This is a helper function. It escapes data and
539 * standardizes newline characters to \n
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 *
541 * @access private
542 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 * @return string
544 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800545 private function _clean_input_data($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600547 if (is_array($str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600549 $new_array = array();
550 foreach ($str as $key => $val)
551 {
552 $new_array[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
553 }
554 return $new_array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 }
556
Andrey Andreevaf728622011-10-20 10:11:59 +0300557 /* We strip slashes if magic quotes is on to keep things consistent
558
559 NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and
560 it will probably not exist in future versions at all.
561 */
562 if ( ! is_php('5.4') && get_magic_quotes_gpc())
Derek Allard2067d1a2008-11-13 22:59:24 +0000563 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600564 $str = stripslashes($str);
565 }
566
567 // Clean UTF-8 if supported
568 if (UTF8_ENABLED === TRUE)
569 {
570 $str = $this->uni->clean_string($str);
571 }
David Behler9b5df592011-08-14 21:04:17 +0200572
Pascal Kriete14a0ac62011-04-05 14:55:56 -0400573 // Remove control characters
574 $str = remove_invisible_characters($str);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600575
576 // Should we filter the input data?
577 if ($this->_enable_xss === TRUE)
578 {
579 $str = $this->security->xss_clean($str);
580 }
581
582 // Standardize newlines if needed
583 if ($this->_standardize_newlines == TRUE)
584 {
585 if (strpos($str, "\r") !== FALSE)
586 {
Phil Sturgeon82f9b152011-03-16 22:18:08 +0000587 $str = str_replace(array("\r\n", "\r", "\r\n\n"), PHP_EOL, $str);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600588 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 }
590
591 return $str;
592 }
593
594 // --------------------------------------------------------------------
595
596 /**
Derek Jones69fc4fc2010-03-02 13:36:31 -0600597 * Clean Keys
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 *
Derek Jones69fc4fc2010-03-02 13:36:31 -0600599 * This is a helper function. To prevent malicious users
600 * from trying to exploit keys we make sure that keys are
601 * only named with alpha-numeric text and a few other items.
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 *
Derek Jones69fc4fc2010-03-02 13:36:31 -0600603 * @access private
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 * @param string
605 * @return string
606 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800607 private function _clean_input_keys($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600609 if ( ! preg_match("/^[a-z0-9:_\/-]+$/i", $str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600611 exit('Disallowed Key Characters.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 }
613
Derek Jones69fc4fc2010-03-02 13:36:31 -0600614 // Clean UTF-8 if supported
615 if (UTF8_ENABLED === TRUE)
616 {
617 $str = $this->uni->clean_string($str);
618 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000619
Derek Jones69fc4fc2010-03-02 13:36:31 -0600620 return $str;
621 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000622
Greg Akerec2f5712010-11-15 16:22:12 -0600623 // --------------------------------------------------------------------
624
625 /**
626 * Request Headers
627 *
David Behler9b5df592011-08-14 21:04:17 +0200628 * In Apache, you can simply call apache_request_headers(), however for
Greg Akerec2f5712010-11-15 16:22:12 -0600629 * people running other webservers the function is undefined.
630 *
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800631 * @access public
David Behlercda768a2011-08-14 23:52:48 +0200632 * @param bool XSS cleaning
633 *
Greg Akerec2f5712010-11-15 16:22:12 -0600634 * @return array
635 */
636 public function request_headers($xss_clean = FALSE)
637 {
638 // Look at Apache go!
639 if (function_exists('apache_request_headers'))
640 {
641 $headers = apache_request_headers();
642 }
643 else
644 {
645 $headers['Content-Type'] = (isset($_SERVER['CONTENT_TYPE'])) ? $_SERVER['CONTENT_TYPE'] : @getenv('CONTENT_TYPE');
646
647 foreach ($_SERVER as $key => $val)
648 {
649 if (strncmp($key, 'HTTP_', 5) === 0)
650 {
651 $headers[substr($key, 5)] = $this->_fetch_from_array($_SERVER, $key, $xss_clean);
652 }
653 }
654 }
655
656 // take SOME_HEADER and turn it into Some-Header
657 foreach ($headers as $key => $val)
658 {
659 $key = str_replace('_', ' ', strtolower($key));
660 $key = str_replace(' ', '-', ucwords($key));
David Behler9b5df592011-08-14 21:04:17 +0200661
Greg Akerec2f5712010-11-15 16:22:12 -0600662 $this->headers[$key] = $val;
663 }
David Behler9b5df592011-08-14 21:04:17 +0200664
Greg Akerec2f5712010-11-15 16:22:12 -0600665 return $this->headers;
666 }
667
668 // --------------------------------------------------------------------
669
670 /**
671 * Get Request Header
672 *
673 * Returns the value of a single member of the headers class member
674 *
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800675 * @access public
Greg Akerec2f5712010-11-15 16:22:12 -0600676 * @param string array key for $this->headers
677 * @param boolean XSS Clean or not
678 * @return mixed FALSE on failure, string on success
679 */
680 public function get_request_header($index, $xss_clean = FALSE)
681 {
682 if (empty($this->headers))
683 {
684 $this->request_headers();
685 }
David Behler9b5df592011-08-14 21:04:17 +0200686
Greg Akerec2f5712010-11-15 16:22:12 -0600687 if ( ! isset($this->headers[$index]))
688 {
689 return FALSE;
690 }
691
692 if ($xss_clean === TRUE)
693 {
Pascal Kriete14a0ac62011-04-05 14:55:56 -0400694 return $this->security->xss_clean($this->headers[$index]);
Greg Akerec2f5712010-11-15 16:22:12 -0600695 }
696
David Behler9b5df592011-08-14 21:04:17 +0200697 return $this->headers[$index];
Greg Akerec2f5712010-11-15 16:22:12 -0600698 }
699
Greg Aker081ac9d2010-11-22 14:42:53 -0600700 // --------------------------------------------------------------------
Phil Sturgeonc3828712011-01-19 12:31:47 +0000701
Greg Aker081ac9d2010-11-22 14:42:53 -0600702 /**
703 * Is ajax Request?
704 *
705 * Test to see if a request contains the HTTP_X_REQUESTED_WITH header
706 *
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800707 * @access public
Phil Sturgeonc3828712011-01-19 12:31:47 +0000708 * @return boolean
Greg Aker081ac9d2010-11-22 14:42:53 -0600709 */
710 public function is_ajax_request()
711 {
Greg Aker2fae66e2010-12-09 15:49:34 -0600712 return ($this->server('HTTP_X_REQUESTED_WITH') === 'XMLHttpRequest');
Greg Aker081ac9d2010-11-22 14:42:53 -0600713 }
714
Phil Sturgeonc3828712011-01-19 12:31:47 +0000715 // --------------------------------------------------------------------
716
717 /**
718 * Is cli Request?
719 *
720 * Test to see if a request was made from the command line
721 *
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800722 * @access public
Phil Sturgeonc3828712011-01-19 12:31:47 +0000723 * @return boolean
724 */
725 public function is_cli_request()
726 {
Phil Sturgeonc5dccf72011-08-13 11:06:57 -0600727 return (php_sapi_name() == 'cli') or defined('STDIN');
Phil Sturgeonc3828712011-01-19 12:31:47 +0000728 }
729
Derek Allard2067d1a2008-11-13 22:59:24 +0000730}
731// END Input class
732
733/* End of file Input.php */
Phil Sturgeon33ed0f32011-02-16 19:03:49 +0000734/* Location: ./system/core/Input.php */