blob: 049935632e8ec48cfb801a67342670159d15bbff [file] [log] [blame]
Andrey Andreev64e98aa2012-01-07 20:29:10 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev64e98aa2012-01-07 20:29:10 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev64e98aa2012-01-07 20:29:10 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * Input Class
30 *
31 * Pre-processes global input data for security
32 *
33 * @package CodeIgniter
34 * @subpackage Libraries
35 * @category Input
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/libraries/input.html
38 */
39class CI_Input {
Derek Allard2067d1a2008-11-13 22:59:24 +000040
David Behler9b5df592011-08-14 21:04:17 +020041 /**
42 * IP address of the current user
43 *
44 * @var string
45 */
Timothy Warren40403d22012-04-19 16:38:50 -040046 public $ip_address = FALSE;
47
David Behler9b5df592011-08-14 21:04:17 +020048 /**
49 * user agent (web browser) being used by the current user
50 *
51 * @var string
52 */
Timothy Warren40403d22012-04-19 16:38:50 -040053 public $user_agent = FALSE;
54
David Behler9b5df592011-08-14 21:04:17 +020055 /**
56 * If FALSE, then $_GET will be set to an empty array
57 *
58 * @var bool
59 */
Timothy Warren40403d22012-04-19 16:38:50 -040060 protected $_allow_get_array = TRUE;
61
David Behler9b5df592011-08-14 21:04:17 +020062 /**
63 * If TRUE, then newlines are standardized
64 *
65 * @var bool
66 */
Timothy Warren40403d22012-04-19 16:38:50 -040067 protected $_standardize_newlines = TRUE;
68
David Behler9b5df592011-08-14 21:04:17 +020069 /**
70 * Determines whether the XSS filter is always active when GET, POST or COOKIE data is encountered
71 * Set automatically based on config setting
72 *
73 * @var bool
74 */
Timothy Warren40403d22012-04-19 16:38:50 -040075 protected $_enable_xss = FALSE;
76
David Behler9b5df592011-08-14 21:04:17 +020077 /**
78 * Enables a CSRF cookie token to be set.
79 * Set automatically based on config setting
80 *
81 * @var bool
82 */
Timothy Warren40403d22012-04-19 16:38:50 -040083 protected $_enable_csrf = FALSE;
84
David Behler9b5df592011-08-14 21:04:17 +020085 /**
86 * List of all HTTP request headers
87 *
88 * @var array
89 */
Timothy Warren40403d22012-04-19 16:38:50 -040090 protected $headers = array();
David Behler9b5df592011-08-14 21:04:17 +020091
Derek Allard2067d1a2008-11-13 22:59:24 +000092 /**
Greg Akera9263282010-11-10 15:26:43 -060093 * Constructor
94 *
95 * Sets whether to globally enable the XSS processing
96 * and whether to allow the $_GET array
Greg Akera9263282010-11-10 15:26:43 -060097 */
98 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000099 {
Andrey Andreev13774972012-01-08 04:30:33 +0200100 log_message('debug', 'Input Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000101
Phil Sturgeonc8089152010-12-27 19:06:28 +0000102 $this->_allow_get_array = (config_item('allow_get_array') === TRUE);
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200103 $this->_enable_xss = (config_item('global_xss_filtering') === TRUE);
104 $this->_enable_csrf = (config_item('csrf_protection') === TRUE);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600105
Pascal Kriete14a0ac62011-04-05 14:55:56 -0400106 global $SEC;
107 $this->security =& $SEC;
Derek Jones69fc4fc2010-03-02 13:36:31 -0600108
Pascal Krieteaaec1e42011-01-20 00:01:21 -0500109 // Do we need the UTF-8 class?
Derek Jones69fc4fc2010-03-02 13:36:31 -0600110 if (UTF8_ENABLED === TRUE)
111 {
112 global $UNI;
113 $this->uni =& $UNI;
114 }
115
116 // Sanitize global arrays
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 $this->_sanitize_globals();
118 }
119
120 // --------------------------------------------------------------------
121
122 /**
Greg Akera9263282010-11-10 15:26:43 -0600123 * Fetch from array
124 *
125 * This is a helper function to retrieve values from global arrays
126 *
Greg Akera9263282010-11-10 15:26:43 -0600127 * @param array
128 * @param string
129 * @param bool
130 * @return string
131 */
Bo-Yi Wu47213792011-09-13 22:44:07 +0800132 protected function _fetch_from_array(&$array, $index = '', $xss_clean = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 {
134 if ( ! isset($array[$index]))
135 {
136 return FALSE;
137 }
138
139 if ($xss_clean === TRUE)
140 {
Pascal Kriete14a0ac62011-04-05 14:55:56 -0400141 return $this->security->xss_clean($array[$index]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 }
143
144 return $array[$index];
145 }
146
147 // --------------------------------------------------------------------
148
149 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400150 * Fetch an item from the GET array
151 *
152 * @param string
153 * @param bool
154 * @return string
155 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800156 public function get($index = NULL, $xss_clean = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 {
Phil Sturgeon44f21052011-02-15 21:39:25 +0000158 // Check if a field has been provided
Andrey Andreev9448afb2012-02-08 19:49:19 +0200159 if ($index === NULL && ! empty($_GET))
vascopjff1cfa12011-02-13 21:30:19 +0000160 {
Phil Sturgeon44f21052011-02-15 21:39:25 +0000161 $get = array();
vascopjff1cfa12011-02-13 21:30:19 +0000162
163 // loop through the full _GET array
Phil Sturgeon44f21052011-02-15 21:39:25 +0000164 foreach (array_keys($_GET) as $key)
vascopjff1cfa12011-02-13 21:30:19 +0000165 {
Phil Sturgeon44f21052011-02-15 21:39:25 +0000166 $get[$key] = $this->_fetch_from_array($_GET, $key, $xss_clean);
vascopjff1cfa12011-02-13 21:30:19 +0000167 }
Phil Sturgeon44f21052011-02-15 21:39:25 +0000168 return $get;
vascopjff1cfa12011-02-13 21:30:19 +0000169 }
170
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 return $this->_fetch_from_array($_GET, $index, $xss_clean);
172 }
173
174 // --------------------------------------------------------------------
175
176 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400177 * Fetch an item from the POST array
178 *
179 * @param string
180 * @param bool
181 * @return string
182 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800183 public function post($index = NULL, $xss_clean = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 {
Phil Sturgeon44f21052011-02-15 21:39:25 +0000185 // Check if a field has been provided
Andrey Andreev9448afb2012-02-08 19:49:19 +0200186 if ($index === NULL && ! empty($_POST))
vascopj0ba58b82011-02-06 14:20:21 +0000187 {
Phil Sturgeon44f21052011-02-15 21:39:25 +0000188 $post = array();
vascopj0ba58b82011-02-06 14:20:21 +0000189
Phil Sturgeon44f21052011-02-15 21:39:25 +0000190 // Loop through the full _POST array and return it
191 foreach (array_keys($_POST) as $key)
vascopj0ba58b82011-02-06 14:20:21 +0000192 {
Phil Sturgeon44f21052011-02-15 21:39:25 +0000193 $post[$key] = $this->_fetch_from_array($_POST, $key, $xss_clean);
vascopj0ba58b82011-02-06 14:20:21 +0000194 }
Phil Sturgeon44f21052011-02-15 21:39:25 +0000195 return $post;
vascopj0ba58b82011-02-06 14:20:21 +0000196 }
David Behler9b5df592011-08-14 21:04:17 +0200197
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 return $this->_fetch_from_array($_POST, $index, $xss_clean);
199 }
200
Derek Jones69fc4fc2010-03-02 13:36:31 -0600201
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 // --------------------------------------------------------------------
203
204 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400205 * Fetch an item from either the GET array or the POST
206 *
207 * @param string The index key
208 * @param bool XSS cleaning
209 * @return string
210 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800211 public function get_post($index = '', $xss_clean = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200213 return isset($_POST[$index])
214 ? $this->post($index, $xss_clean)
215 : $this->get($index, $xss_clean);
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 }
217
218 // --------------------------------------------------------------------
219
220 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400221 * Fetch an item from the COOKIE array
222 *
223 * @param string
224 * @param bool
225 * @return string
226 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800227 public function cookie($index = '', $xss_clean = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 {
229 return $this->_fetch_from_array($_COOKIE, $index, $xss_clean);
230 }
231
Derek Jones69fc4fc2010-03-02 13:36:31 -0600232 // ------------------------------------------------------------------------
233
234 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400235 * Set cookie
236 *
237 * Accepts seven parameters, or you can submit an associative
238 * array in the first parameter containing all the values.
239 *
240 * @param mixed
241 * @param string the value of the cookie
242 * @param string the number of seconds until expiration
243 * @param string the cookie domain. Usually: .yourdomain.com
244 * @param string the cookie path
245 * @param string the cookie prefix
246 * @param bool true makes the cookie secure
247 * @param bool true makes the cookie accessible via http(s) only (no javascript)
248 * @return void
249 */
freewil4ad0fd82012-03-13 22:37:42 -0400250 public function set_cookie($name = '', $value = '', $expire = '', $domain = '', $path = '/', $prefix = '', $secure = FALSE, $httponly = FALSE)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600251 {
252 if (is_array($name))
253 {
tobiasbg9aa7dc92011-02-18 21:57:13 +0100254 // always leave 'name' in last place, as the loop will break otherwise, due to $$item
freewil4ad0fd82012-03-13 22:37:42 -0400255 foreach (array('value', 'expire', 'domain', 'path', 'prefix', 'secure', 'httponly', 'name') as $item)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600256 {
257 if (isset($name[$item]))
258 {
259 $$item = $name[$item];
260 }
261 }
262 }
263
Andrey Andreev9448afb2012-02-08 19:49:19 +0200264 if ($prefix == '' && config_item('cookie_prefix') != '')
Derek Jones69fc4fc2010-03-02 13:36:31 -0600265 {
266 $prefix = config_item('cookie_prefix');
267 }
Andrey Andreev9448afb2012-02-08 19:49:19 +0200268 if ($domain == '' && config_item('cookie_domain') != '')
Derek Jones69fc4fc2010-03-02 13:36:31 -0600269 {
270 $domain = config_item('cookie_domain');
271 }
Andrey Andreev9448afb2012-02-08 19:49:19 +0200272 if ($path == '/' && config_item('cookie_path') !== '/')
Derek Jones69fc4fc2010-03-02 13:36:31 -0600273 {
274 $path = config_item('cookie_path');
275 }
Andrey Andreev9448afb2012-02-08 19:49:19 +0200276 if ($secure == FALSE && config_item('cookie_secure') != FALSE)
tobiasbg9aa7dc92011-02-18 21:57:13 +0100277 {
278 $secure = config_item('cookie_secure');
279 }
freewil4ad0fd82012-03-13 22:37:42 -0400280 if ($httponly == FALSE && config_item('cookie_httponly') != FALSE)
281 {
282 $httponly = config_item('cookie_httponly');
283 }
Derek Jones69fc4fc2010-03-02 13:36:31 -0600284
285 if ( ! is_numeric($expire))
286 {
287 $expire = time() - 86500;
288 }
289 else
290 {
Phil Sturgeonc8089152010-12-27 19:06:28 +0000291 $expire = ($expire > 0) ? time() + $expire : 0;
Derek Jones69fc4fc2010-03-02 13:36:31 -0600292 }
293
freewil4ad0fd82012-03-13 22:37:42 -0400294 setcookie($prefix.$name, $value, $expire, $path, $domain, $secure, $httponly);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600295 }
296
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 // --------------------------------------------------------------------
298
299 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400300 * Fetch an item from the SERVER array
301 *
302 * @param string
303 * @param bool
304 * @return string
305 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800306 public function server($index = '', $xss_clean = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 {
308 return $this->_fetch_from_array($_SERVER, $index, $xss_clean);
309 }
310
311 // --------------------------------------------------------------------
312
313 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400314 * Fetch the IP Address
315 *
316 * @return string
317 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800318 public function ip_address()
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 {
320 if ($this->ip_address !== FALSE)
321 {
322 return $this->ip_address;
323 }
Barry Mienydd671972010-10-04 16:33:58 +0200324
Derek Jones42b2e172009-02-05 16:59:45 +0000325 if (config_item('proxy_ips') != '' && $this->server('HTTP_X_FORWARDED_FOR') && $this->server('REMOTE_ADDR'))
Derek Jonesc5972282009-02-04 21:40:20 +0000326 {
Derek Jones42b2e172009-02-05 16:59:45 +0000327 $proxies = preg_split('/[\s,]/', config_item('proxy_ips'), -1, PREG_SPLIT_NO_EMPTY);
Derek Jonesc5972282009-02-04 21:40:20 +0000328 $proxies = is_array($proxies) ? $proxies : array($proxies);
Derek Allard2067d1a2008-11-13 22:59:24 +0000329
Derek Jonesc5972282009-02-04 21:40:20 +0000330 $this->ip_address = in_array($_SERVER['REMOTE_ADDR'], $proxies) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
331 }
Andrey Andreev9448afb2012-02-08 19:49:19 +0200332 elseif ( ! $this->server('HTTP_CLIENT_IP') && $this->server('REMOTE_ADDR'))
John Bellone52c10b62011-08-21 11:41:32 -0400333 {
334 $this->ip_address = $_SERVER['REMOTE_ADDR'];
335 }
Andrey Andreev9448afb2012-02-08 19:49:19 +0200336 elseif ($this->server('REMOTE_ADDR') && $this->server('HTTP_CLIENT_IP'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 {
338 $this->ip_address = $_SERVER['HTTP_CLIENT_IP'];
339 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 elseif ($this->server('HTTP_CLIENT_IP'))
341 {
342 $this->ip_address = $_SERVER['HTTP_CLIENT_IP'];
343 }
344 elseif ($this->server('HTTP_X_FORWARDED_FOR'))
345 {
346 $this->ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
347 }
348
349 if ($this->ip_address === FALSE)
350 {
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200351 return $this->ip_address = '0.0.0.0';
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 }
353
Robin Sowell76b369e2010-03-19 11:15:28 -0400354 if (strpos($this->ip_address, ',') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 {
356 $x = explode(',', $this->ip_address);
Derek Jonesc5972282009-02-04 21:40:20 +0000357 $this->ip_address = trim(end($x));
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 }
359
360 if ( ! $this->valid_ip($this->ip_address))
361 {
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200362 return $this->ip_address = '0.0.0.0';
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 }
364
365 return $this->ip_address;
366 }
367
368 // --------------------------------------------------------------------
369
370 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400371 * Validate IP Address
372 *
373 * Updated version suggested by Geert De Deckere
374 *
375 * @param string
376 * @return bool
377 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800378 public function valid_ip($ip)
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 {
Andrey Andreev1ae65162012-03-10 16:11:34 +0200380 return (bool) filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 }
382
383 // --------------------------------------------------------------------
384
385 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400386 * User Agent
387 *
388 * @return string
389 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800390 public function user_agent()
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 {
392 if ($this->user_agent !== FALSE)
393 {
394 return $this->user_agent;
395 }
396
Andrey Andreev9448afb2012-02-08 19:49:19 +0200397 return $this->user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 }
399
400 // --------------------------------------------------------------------
401
402 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400403 * Sanitize Globals
404 *
405 * This function does the following:
406 *
407 * - Unsets $_GET data (if query strings are not enabled)
408 * - Unsets all globals if register_globals is enabled
409 * - Standardizes newline characters to \n
410 *
411 * @return void
412 */
Andrey Andreev90cfe142012-01-08 04:46:42 +0200413 protected function _sanitize_globals()
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600415 // It would be "wrong" to unset any of these GLOBALS.
Timothy Warren40403d22012-04-19 16:38:50 -0400416 $protected = array(
417 '_SERVER',
418 '_GET',
419 '_POST',
420 '_FILES',
421 '_REQUEST',
422 '_SESSION',
423 '_ENV',
424 'GLOBALS',
425 'HTTP_RAW_POST_DATA',
426 'system_folder',
427 'application_folder',
428 'BM',
429 'EXT',
430 'CFG',
431 'URI',
432 'RTR',
433 'OUT'
434 'IN'
435 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000436
Barry Mienydd671972010-10-04 16:33:58 +0200437 // Unset globals for securiy.
Derek Jones69fc4fc2010-03-02 13:36:31 -0600438 // This is effectively the same as register_globals = off
439 foreach (array($_GET, $_POST, $_COOKIE) as $global)
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600441 if ( ! is_array($global))
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600443 if ( ! in_array($global, $protected))
444 {
445 global $$global;
446 $$global = NULL;
447 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 }
Derek Jones69fc4fc2010-03-02 13:36:31 -0600449 else
450 {
451 foreach ($global as $key => $val)
452 {
453 if ( ! in_array($key, $protected))
454 {
455 global $$key;
456 $$key = NULL;
457 }
458 }
459 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 }
461
Derek Jones69fc4fc2010-03-02 13:36:31 -0600462 // Is $_GET data allowed? If not we'll set the $_GET to an empty array
463 if ($this->_allow_get_array == FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600465 $_GET = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 }
Andrey Andreev9448afb2012-02-08 19:49:19 +0200467 elseif (is_array($_GET) && count($_GET) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200469 foreach ($_GET as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200471 $_GET[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 }
473 }
474
Derek Jones69fc4fc2010-03-02 13:36:31 -0600475 // Clean $_POST Data
Andrey Andreev9448afb2012-02-08 19:49:19 +0200476 if (is_array($_POST) && count($_POST) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 {
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500478 foreach ($_POST as $key => $val)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600479 {
480 $_POST[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
481 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 }
483
Derek Jones69fc4fc2010-03-02 13:36:31 -0600484 // Clean $_COOKIE Data
Andrey Andreev9448afb2012-02-08 19:49:19 +0200485 if (is_array($_COOKIE) && count($_COOKIE) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600487 // Also get rid of specially treated cookies that might be set by a server
488 // or silly application, that are of no use to a CI application anyway
489 // but that when present will trip our 'Disallowed Key Characters' alarm
490 // http://www.ietf.org/rfc/rfc2109.txt
491 // note that the key names below are single quoted strings, and are not PHP variables
492 unset($_COOKIE['$Version']);
493 unset($_COOKIE['$Path']);
494 unset($_COOKIE['$Domain']);
495
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500496 foreach ($_COOKIE as $key => $val)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600497 {
498 $_COOKIE[$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 // Sanitize PHP_SELF
503 $_SERVER['PHP_SELF'] = strip_tags($_SERVER['PHP_SELF']);
504
Derek Jones69fc4fc2010-03-02 13:36:31 -0600505 // CSRF Protection check
506 if ($this->_enable_csrf == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600508 $this->security->csrf_verify();
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000510
Andrey Andreev90cfe142012-01-08 04:46:42 +0200511 log_message('debug', 'Global POST and COOKIE data sanitized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 }
513
514 // --------------------------------------------------------------------
515
516 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400517 * Clean Input Data
518 *
519 * This is a helper function. It escapes data and
520 * standardizes newline characters to \n
521 *
522 * @param string
523 * @return string
524 */
Andrey Andreev90cfe142012-01-08 04:46:42 +0200525 protected function _clean_input_data($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600527 if (is_array($str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600529 $new_array = array();
530 foreach ($str as $key => $val)
531 {
532 $new_array[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
533 }
534 return $new_array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 }
536
Andrey Andreevaf728622011-10-20 10:11:59 +0300537 /* We strip slashes if magic quotes is on to keep things consistent
538
539 NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and
540 it will probably not exist in future versions at all.
541 */
542 if ( ! is_php('5.4') && get_magic_quotes_gpc())
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600544 $str = stripslashes($str);
545 }
546
547 // Clean UTF-8 if supported
548 if (UTF8_ENABLED === TRUE)
549 {
550 $str = $this->uni->clean_string($str);
551 }
David Behler9b5df592011-08-14 21:04:17 +0200552
Pascal Kriete14a0ac62011-04-05 14:55:56 -0400553 // Remove control characters
554 $str = remove_invisible_characters($str);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600555
556 // Should we filter the input data?
557 if ($this->_enable_xss === TRUE)
558 {
559 $str = $this->security->xss_clean($str);
560 }
561
562 // Standardize newlines if needed
Andrey Andreev9448afb2012-02-08 19:49:19 +0200563 if ($this->_standardize_newlines == TRUE && strpos($str, "\r") !== FALSE)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600564 {
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200565 return str_replace(array("\r\n", "\r", "\r\n\n"), PHP_EOL, $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 }
567
568 return $str;
569 }
570
571 // --------------------------------------------------------------------
572
573 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400574 * Clean Keys
575 *
576 * This is a helper function. To prevent malicious users
577 * from trying to exploit keys we make sure that keys are
578 * only named with alpha-numeric text and a few other items.
579 *
580 * @param string
581 * @return string
582 */
Andrey Andreev90cfe142012-01-08 04:46:42 +0200583 protected function _clean_input_keys($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 {
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200585 if ( ! preg_match('/^[a-z0-9:_\/-]+$/i', $str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 {
Kevin Cuppd63e4012012-02-05 14:14:32 -0500587 set_status_header(503);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600588 exit('Disallowed Key Characters.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 }
590
Derek Jones69fc4fc2010-03-02 13:36:31 -0600591 // Clean UTF-8 if supported
592 if (UTF8_ENABLED === TRUE)
593 {
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200594 return $this->uni->clean_string($str);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600595 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000596
Derek Jones69fc4fc2010-03-02 13:36:31 -0600597 return $str;
598 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000599
Greg Akerec2f5712010-11-15 16:22:12 -0600600 // --------------------------------------------------------------------
601
602 /**
603 * Request Headers
604 *
David Behler9b5df592011-08-14 21:04:17 +0200605 * In Apache, you can simply call apache_request_headers(), however for
Greg Akerec2f5712010-11-15 16:22:12 -0600606 * people running other webservers the function is undefined.
607 *
David Behlercda768a2011-08-14 23:52:48 +0200608 * @param bool XSS cleaning
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200609 * @return array
Greg Akerec2f5712010-11-15 16:22:12 -0600610 */
611 public function request_headers($xss_clean = FALSE)
612 {
613 // Look at Apache go!
614 if (function_exists('apache_request_headers'))
615 {
616 $headers = apache_request_headers();
617 }
618 else
619 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200620 $headers['Content-Type'] = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : @getenv('CONTENT_TYPE');
Greg Akerec2f5712010-11-15 16:22:12 -0600621
622 foreach ($_SERVER as $key => $val)
623 {
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200624 if (strpos($key, 'HTTP_') === 0)
Greg Akerec2f5712010-11-15 16:22:12 -0600625 {
626 $headers[substr($key, 5)] = $this->_fetch_from_array($_SERVER, $key, $xss_clean);
627 }
628 }
629 }
630
631 // take SOME_HEADER and turn it into Some-Header
632 foreach ($headers as $key => $val)
633 {
634 $key = str_replace('_', ' ', strtolower($key));
635 $key = str_replace(' ', '-', ucwords($key));
David Behler9b5df592011-08-14 21:04:17 +0200636
Greg Akerec2f5712010-11-15 16:22:12 -0600637 $this->headers[$key] = $val;
638 }
David Behler9b5df592011-08-14 21:04:17 +0200639
Greg Akerec2f5712010-11-15 16:22:12 -0600640 return $this->headers;
641 }
642
643 // --------------------------------------------------------------------
644
645 /**
646 * Get Request Header
647 *
648 * Returns the value of a single member of the headers class member
649 *
Andrey Andreev773e1172012-02-08 23:02:19 +0200650 * @param string array key for $this->headers
Andrey Andreev9448afb2012-02-08 19:49:19 +0200651 * @param bool XSS Clean or not
Andrey Andreev773e1172012-02-08 23:02:19 +0200652 * @return mixed FALSE on failure, string on success
Greg Akerec2f5712010-11-15 16:22:12 -0600653 */
654 public function get_request_header($index, $xss_clean = FALSE)
655 {
656 if (empty($this->headers))
657 {
658 $this->request_headers();
659 }
David Behler9b5df592011-08-14 21:04:17 +0200660
Greg Akerec2f5712010-11-15 16:22:12 -0600661 if ( ! isset($this->headers[$index]))
662 {
663 return FALSE;
664 }
665
Andrey Andreev9448afb2012-02-08 19:49:19 +0200666 return ($xss_clean === TRUE)
667 ? $this->security->xss_clean($this->headers[$index])
668 : $this->headers[$index];
Greg Akerec2f5712010-11-15 16:22:12 -0600669 }
670
Greg Aker081ac9d2010-11-22 14:42:53 -0600671 // --------------------------------------------------------------------
Phil Sturgeonc3828712011-01-19 12:31:47 +0000672
Greg Aker081ac9d2010-11-22 14:42:53 -0600673 /**
674 * Is ajax Request?
675 *
676 * Test to see if a request contains the HTTP_X_REQUESTED_WITH header
677 *
Andrey Andreev9448afb2012-02-08 19:49:19 +0200678 * @return bool
Greg Aker081ac9d2010-11-22 14:42:53 -0600679 */
680 public function is_ajax_request()
681 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200682 return ( ! empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest');
Greg Aker081ac9d2010-11-22 14:42:53 -0600683 }
684
Phil Sturgeonc3828712011-01-19 12:31:47 +0000685 // --------------------------------------------------------------------
686
687 /**
688 * Is cli Request?
689 *
690 * Test to see if a request was made from the command line
691 *
Andrey Andreev9448afb2012-02-08 19:49:19 +0200692 * @return bool
Phil Sturgeonc3828712011-01-19 12:31:47 +0000693 */
694 public function is_cli_request()
695 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200696 return (php_sapi_name() === 'cli' OR defined('STDIN'));
Phil Sturgeonc3828712011-01-19 12:31:47 +0000697 }
698
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100699 // --------------------------------------------------------------------
700
701 /**
702 * Get Request Method
703 *
Michiel Vugteveendc900df2012-03-07 20:41:37 +0100704 * Return the Request Method
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100705 *
Michiel Vugteveendc900df2012-03-07 20:41:37 +0100706 * @param bool uppercase or lowercase
Michiel Vugteveen7c8841f2012-03-07 20:49:06 +0100707 * @return bool
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100708 */
Michiel Vugteveen704fb162012-03-07 20:42:33 +0100709 public function method($upper = FALSE)
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100710 {
Michiel Vugteveendc900df2012-03-07 20:41:37 +0100711 return ($upper)
712 ? strtoupper($this->server('REQUEST_METHOD'))
713 : strtolower($this->server('REQUEST_METHOD'));
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100714 }
715
Derek Allard2067d1a2008-11-13 22:59:24 +0000716}
Derek Allard2067d1a2008-11-13 22:59:24 +0000717
718/* End of file Input.php */
Timothy Warren40403d22012-04-19 16:38:50 -0400719/* Location: ./system/core/Input.php */