blob: 7594a2e4594c4cf76f78c1f18a143e5e9cd977ee [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 Warren48a7fbb2012-04-23 11:58:16 -040046 public $ip_address = FALSE;
Timothy Warren40403d22012-04-19 16:38:50 -040047
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 Warren48a7fbb2012-04-23 11:58:16 -040053 public $user_agent = FALSE;
Timothy Warren40403d22012-04-19 16:38:50 -040054
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 Warren48a7fbb2012-04-23 11:58:16 -040060 protected $_allow_get_array = TRUE;
Timothy Warren40403d22012-04-19 16:38:50 -040061
David Behler9b5df592011-08-14 21:04:17 +020062 /**
63 * If TRUE, then newlines are standardized
64 *
65 * @var bool
66 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040067 protected $_standardize_newlines = TRUE;
Timothy Warren40403d22012-04-19 16:38:50 -040068
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 Warren48a7fbb2012-04-23 11:58:16 -040075 protected $_enable_xss = FALSE;
Timothy Warren40403d22012-04-19 16:38:50 -040076
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 Warren48a7fbb2012-04-23 11:58:16 -040083 protected $_enable_csrf = FALSE;
Timothy Warren40403d22012-04-19 16:38:50 -040084
David Behler9b5df592011-08-14 21:04:17 +020085 /**
86 * List of all HTTP request headers
87 *
88 * @var array
89 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -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 {
ziv03edf582012-04-27 15:09:04 +0800229 $index = config_item('cookie_prefix').$index;
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 return $this->_fetch_from_array($_COOKIE, $index, $xss_clean);
231 }
232
Derek Jones69fc4fc2010-03-02 13:36:31 -0600233 // ------------------------------------------------------------------------
234
235 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400236 * Set cookie
237 *
238 * Accepts seven parameters, or you can submit an associative
239 * array in the first parameter containing all the values.
240 *
241 * @param mixed
242 * @param string the value of the cookie
243 * @param string the number of seconds until expiration
244 * @param string the cookie domain. Usually: .yourdomain.com
245 * @param string the cookie path
246 * @param string the cookie prefix
247 * @param bool true makes the cookie secure
248 * @param bool true makes the cookie accessible via http(s) only (no javascript)
249 * @return void
250 */
freewil4ad0fd82012-03-13 22:37:42 -0400251 public function set_cookie($name = '', $value = '', $expire = '', $domain = '', $path = '/', $prefix = '', $secure = FALSE, $httponly = FALSE)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600252 {
253 if (is_array($name))
254 {
tobiasbg9aa7dc92011-02-18 21:57:13 +0100255 // always leave 'name' in last place, as the loop will break otherwise, due to $$item
freewil4ad0fd82012-03-13 22:37:42 -0400256 foreach (array('value', 'expire', 'domain', 'path', 'prefix', 'secure', 'httponly', 'name') as $item)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600257 {
258 if (isset($name[$item]))
259 {
260 $$item = $name[$item];
261 }
262 }
263 }
264
Andrey Andreev9448afb2012-02-08 19:49:19 +0200265 if ($prefix == '' && config_item('cookie_prefix') != '')
Derek Jones69fc4fc2010-03-02 13:36:31 -0600266 {
267 $prefix = config_item('cookie_prefix');
268 }
Andrey Andreev9448afb2012-02-08 19:49:19 +0200269 if ($domain == '' && config_item('cookie_domain') != '')
Derek Jones69fc4fc2010-03-02 13:36:31 -0600270 {
271 $domain = config_item('cookie_domain');
272 }
Andrey Andreev9448afb2012-02-08 19:49:19 +0200273 if ($path == '/' && config_item('cookie_path') !== '/')
Derek Jones69fc4fc2010-03-02 13:36:31 -0600274 {
275 $path = config_item('cookie_path');
276 }
Andrey Andreev9448afb2012-02-08 19:49:19 +0200277 if ($secure == FALSE && config_item('cookie_secure') != FALSE)
tobiasbg9aa7dc92011-02-18 21:57:13 +0100278 {
279 $secure = config_item('cookie_secure');
280 }
freewil4ad0fd82012-03-13 22:37:42 -0400281 if ($httponly == FALSE && config_item('cookie_httponly') != FALSE)
282 {
283 $httponly = config_item('cookie_httponly');
284 }
Derek Jones69fc4fc2010-03-02 13:36:31 -0600285
286 if ( ! is_numeric($expire))
287 {
288 $expire = time() - 86500;
289 }
290 else
291 {
Phil Sturgeonc8089152010-12-27 19:06:28 +0000292 $expire = ($expire > 0) ? time() + $expire : 0;
Derek Jones69fc4fc2010-03-02 13:36:31 -0600293 }
294
freewil4ad0fd82012-03-13 22:37:42 -0400295 setcookie($prefix.$name, $value, $expire, $path, $domain, $secure, $httponly);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600296 }
297
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 // --------------------------------------------------------------------
299
300 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400301 * Fetch an item from the SERVER array
302 *
303 * @param string
304 * @param bool
305 * @return string
306 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800307 public function server($index = '', $xss_clean = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 {
309 return $this->_fetch_from_array($_SERVER, $index, $xss_clean);
310 }
311
312 // --------------------------------------------------------------------
313
314 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400315 * Fetch the IP Address
316 *
317 * @return string
318 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800319 public function ip_address()
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 {
321 if ($this->ip_address !== FALSE)
322 {
323 return $this->ip_address;
324 }
Barry Mienydd671972010-10-04 16:33:58 +0200325
Derek Jones42b2e172009-02-05 16:59:45 +0000326 if (config_item('proxy_ips') != '' && $this->server('HTTP_X_FORWARDED_FOR') && $this->server('REMOTE_ADDR'))
Derek Jonesc5972282009-02-04 21:40:20 +0000327 {
Derek Jones42b2e172009-02-05 16:59:45 +0000328 $proxies = preg_split('/[\s,]/', config_item('proxy_ips'), -1, PREG_SPLIT_NO_EMPTY);
Derek Jonesc5972282009-02-04 21:40:20 +0000329 $proxies = is_array($proxies) ? $proxies : array($proxies);
Derek Allard2067d1a2008-11-13 22:59:24 +0000330
Derek Jonesc5972282009-02-04 21:40:20 +0000331 $this->ip_address = in_array($_SERVER['REMOTE_ADDR'], $proxies) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
332 }
Andrey Andreev9448afb2012-02-08 19:49:19 +0200333 elseif ( ! $this->server('HTTP_CLIENT_IP') && $this->server('REMOTE_ADDR'))
John Bellone52c10b62011-08-21 11:41:32 -0400334 {
335 $this->ip_address = $_SERVER['REMOTE_ADDR'];
336 }
Andrey Andreev9448afb2012-02-08 19:49:19 +0200337 elseif ($this->server('REMOTE_ADDR') && $this->server('HTTP_CLIENT_IP'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 {
339 $this->ip_address = $_SERVER['HTTP_CLIENT_IP'];
340 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 elseif ($this->server('HTTP_CLIENT_IP'))
342 {
343 $this->ip_address = $_SERVER['HTTP_CLIENT_IP'];
344 }
345 elseif ($this->server('HTTP_X_FORWARDED_FOR'))
346 {
347 $this->ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
348 }
349
350 if ($this->ip_address === FALSE)
351 {
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200352 return $this->ip_address = '0.0.0.0';
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 }
354
Robin Sowell76b369e2010-03-19 11:15:28 -0400355 if (strpos($this->ip_address, ',') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 {
357 $x = explode(',', $this->ip_address);
Derek Jonesc5972282009-02-04 21:40:20 +0000358 $this->ip_address = trim(end($x));
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 }
360
361 if ( ! $this->valid_ip($this->ip_address))
362 {
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200363 return $this->ip_address = '0.0.0.0';
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 }
365
366 return $this->ip_address;
367 }
368
369 // --------------------------------------------------------------------
370
371 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400372 * Validate IP Address
373 *
374 * Updated version suggested by Geert De Deckere
375 *
376 * @param string
377 * @return bool
378 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800379 public function valid_ip($ip)
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 {
Andrey Andreev1ae65162012-03-10 16:11:34 +0200381 return (bool) filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 }
383
384 // --------------------------------------------------------------------
385
386 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400387 * User Agent
388 *
389 * @return string
390 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800391 public function user_agent()
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 {
393 if ($this->user_agent !== FALSE)
394 {
395 return $this->user_agent;
396 }
397
Andrey Andreev9448afb2012-02-08 19:49:19 +0200398 return $this->user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 }
400
401 // --------------------------------------------------------------------
402
403 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400404 * Sanitize Globals
405 *
406 * This function does the following:
407 *
408 * - Unsets $_GET data (if query strings are not enabled)
409 * - Unsets all globals if register_globals is enabled
410 * - Standardizes newline characters to \n
411 *
412 * @return void
413 */
Andrey Andreev90cfe142012-01-08 04:46:42 +0200414 protected function _sanitize_globals()
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600416 // It would be "wrong" to unset any of these GLOBALS.
Timothy Warren40403d22012-04-19 16:38:50 -0400417 $protected = array(
418 '_SERVER',
419 '_GET',
420 '_POST',
421 '_FILES',
422 '_REQUEST',
423 '_SESSION',
424 '_ENV',
425 'GLOBALS',
426 'HTTP_RAW_POST_DATA',
427 'system_folder',
428 'application_folder',
429 'BM',
430 'EXT',
431 'CFG',
432 'URI',
433 'RTR',
Timothy Warren67cb3ee2012-04-19 16:41:52 -0400434 'OUT',
Timothy Warren40403d22012-04-19 16:38:50 -0400435 'IN'
436 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000437
Barry Mienydd671972010-10-04 16:33:58 +0200438 // Unset globals for securiy.
Derek Jones69fc4fc2010-03-02 13:36:31 -0600439 // This is effectively the same as register_globals = off
440 foreach (array($_GET, $_POST, $_COOKIE) as $global)
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600442 if ( ! is_array($global))
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600444 if ( ! in_array($global, $protected))
445 {
446 global $$global;
447 $$global = NULL;
448 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 }
Derek Jones69fc4fc2010-03-02 13:36:31 -0600450 else
451 {
452 foreach ($global as $key => $val)
453 {
454 if ( ! in_array($key, $protected))
455 {
456 global $$key;
457 $$key = NULL;
458 }
459 }
460 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 }
462
Derek Jones69fc4fc2010-03-02 13:36:31 -0600463 // Is $_GET data allowed? If not we'll set the $_GET to an empty array
464 if ($this->_allow_get_array == FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600466 $_GET = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 }
Andrey Andreev9448afb2012-02-08 19:49:19 +0200468 elseif (is_array($_GET) && count($_GET) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200470 foreach ($_GET as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200472 $_GET[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 }
474 }
475
Derek Jones69fc4fc2010-03-02 13:36:31 -0600476 // Clean $_POST Data
Andrey Andreev9448afb2012-02-08 19:49:19 +0200477 if (is_array($_POST) && count($_POST) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 {
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500479 foreach ($_POST as $key => $val)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600480 {
481 $_POST[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
482 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 }
484
Derek Jones69fc4fc2010-03-02 13:36:31 -0600485 // Clean $_COOKIE Data
Andrey Andreev9448afb2012-02-08 19:49:19 +0200486 if (is_array($_COOKIE) && count($_COOKIE) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600488 // Also get rid of specially treated cookies that might be set by a server
489 // or silly application, that are of no use to a CI application anyway
490 // but that when present will trip our 'Disallowed Key Characters' alarm
491 // http://www.ietf.org/rfc/rfc2109.txt
492 // note that the key names below are single quoted strings, and are not PHP variables
493 unset($_COOKIE['$Version']);
494 unset($_COOKIE['$Path']);
495 unset($_COOKIE['$Domain']);
496
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500497 foreach ($_COOKIE as $key => $val)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600498 {
499 $_COOKIE[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
500 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 }
502
Derek Jones69fc4fc2010-03-02 13:36:31 -0600503 // Sanitize PHP_SELF
504 $_SERVER['PHP_SELF'] = strip_tags($_SERVER['PHP_SELF']);
505
Derek Jones69fc4fc2010-03-02 13:36:31 -0600506 // CSRF Protection check
507 if ($this->_enable_csrf == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600509 $this->security->csrf_verify();
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000511
Andrey Andreev90cfe142012-01-08 04:46:42 +0200512 log_message('debug', 'Global POST and COOKIE data sanitized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 }
514
515 // --------------------------------------------------------------------
516
517 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400518 * Clean Input Data
519 *
520 * This is a helper function. It escapes data and
521 * standardizes newline characters to \n
522 *
523 * @param string
524 * @return string
525 */
Andrey Andreev90cfe142012-01-08 04:46:42 +0200526 protected function _clean_input_data($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600528 if (is_array($str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600530 $new_array = array();
531 foreach ($str as $key => $val)
532 {
533 $new_array[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
534 }
535 return $new_array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 }
537
Andrey Andreevaf728622011-10-20 10:11:59 +0300538 /* We strip slashes if magic quotes is on to keep things consistent
539
540 NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and
541 it will probably not exist in future versions at all.
542 */
543 if ( ! is_php('5.4') && get_magic_quotes_gpc())
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600545 $str = stripslashes($str);
546 }
547
548 // Clean UTF-8 if supported
549 if (UTF8_ENABLED === TRUE)
550 {
551 $str = $this->uni->clean_string($str);
552 }
David Behler9b5df592011-08-14 21:04:17 +0200553
Pascal Kriete14a0ac62011-04-05 14:55:56 -0400554 // Remove control characters
555 $str = remove_invisible_characters($str);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600556
557 // Should we filter the input data?
558 if ($this->_enable_xss === TRUE)
559 {
560 $str = $this->security->xss_clean($str);
561 }
562
563 // Standardize newlines if needed
Andrey Andreev9448afb2012-02-08 19:49:19 +0200564 if ($this->_standardize_newlines == TRUE && strpos($str, "\r") !== FALSE)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600565 {
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200566 return str_replace(array("\r\n", "\r", "\r\n\n"), PHP_EOL, $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 }
568
569 return $str;
570 }
571
572 // --------------------------------------------------------------------
573
574 /**
Timothy Warren40403d22012-04-19 16:38:50 -0400575 * Clean Keys
576 *
577 * This is a helper function. To prevent malicious users
578 * from trying to exploit keys we make sure that keys are
579 * only named with alpha-numeric text and a few other items.
580 *
581 * @param string
582 * @return string
583 */
Andrey Andreev90cfe142012-01-08 04:46:42 +0200584 protected function _clean_input_keys($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 {
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200586 if ( ! preg_match('/^[a-z0-9:_\/-]+$/i', $str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 {
Kevin Cuppd63e4012012-02-05 14:14:32 -0500588 set_status_header(503);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600589 exit('Disallowed Key Characters.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 }
591
Derek Jones69fc4fc2010-03-02 13:36:31 -0600592 // Clean UTF-8 if supported
593 if (UTF8_ENABLED === TRUE)
594 {
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200595 return $this->uni->clean_string($str);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600596 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000597
Derek Jones69fc4fc2010-03-02 13:36:31 -0600598 return $str;
599 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000600
Greg Akerec2f5712010-11-15 16:22:12 -0600601 // --------------------------------------------------------------------
602
603 /**
604 * Request Headers
605 *
David Behler9b5df592011-08-14 21:04:17 +0200606 * In Apache, you can simply call apache_request_headers(), however for
Greg Akerec2f5712010-11-15 16:22:12 -0600607 * people running other webservers the function is undefined.
608 *
David Behlercda768a2011-08-14 23:52:48 +0200609 * @param bool XSS cleaning
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200610 * @return array
Greg Akerec2f5712010-11-15 16:22:12 -0600611 */
612 public function request_headers($xss_clean = FALSE)
613 {
614 // Look at Apache go!
615 if (function_exists('apache_request_headers'))
616 {
617 $headers = apache_request_headers();
618 }
619 else
620 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200621 $headers['Content-Type'] = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : @getenv('CONTENT_TYPE');
Greg Akerec2f5712010-11-15 16:22:12 -0600622
623 foreach ($_SERVER as $key => $val)
624 {
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200625 if (strpos($key, 'HTTP_') === 0)
Greg Akerec2f5712010-11-15 16:22:12 -0600626 {
627 $headers[substr($key, 5)] = $this->_fetch_from_array($_SERVER, $key, $xss_clean);
628 }
629 }
630 }
631
632 // take SOME_HEADER and turn it into Some-Header
633 foreach ($headers as $key => $val)
634 {
635 $key = str_replace('_', ' ', strtolower($key));
636 $key = str_replace(' ', '-', ucwords($key));
David Behler9b5df592011-08-14 21:04:17 +0200637
Greg Akerec2f5712010-11-15 16:22:12 -0600638 $this->headers[$key] = $val;
639 }
David Behler9b5df592011-08-14 21:04:17 +0200640
Greg Akerec2f5712010-11-15 16:22:12 -0600641 return $this->headers;
642 }
643
644 // --------------------------------------------------------------------
645
646 /**
647 * Get Request Header
648 *
649 * Returns the value of a single member of the headers class member
650 *
Andrey Andreev773e1172012-02-08 23:02:19 +0200651 * @param string array key for $this->headers
Andrey Andreev9448afb2012-02-08 19:49:19 +0200652 * @param bool XSS Clean or not
Andrey Andreev773e1172012-02-08 23:02:19 +0200653 * @return mixed FALSE on failure, string on success
Greg Akerec2f5712010-11-15 16:22:12 -0600654 */
655 public function get_request_header($index, $xss_clean = FALSE)
656 {
657 if (empty($this->headers))
658 {
659 $this->request_headers();
660 }
David Behler9b5df592011-08-14 21:04:17 +0200661
Greg Akerec2f5712010-11-15 16:22:12 -0600662 if ( ! isset($this->headers[$index]))
663 {
664 return FALSE;
665 }
666
Andrey Andreev9448afb2012-02-08 19:49:19 +0200667 return ($xss_clean === TRUE)
668 ? $this->security->xss_clean($this->headers[$index])
669 : $this->headers[$index];
Greg Akerec2f5712010-11-15 16:22:12 -0600670 }
671
Greg Aker081ac9d2010-11-22 14:42:53 -0600672 // --------------------------------------------------------------------
Phil Sturgeonc3828712011-01-19 12:31:47 +0000673
Greg Aker081ac9d2010-11-22 14:42:53 -0600674 /**
675 * Is ajax Request?
676 *
677 * Test to see if a request contains the HTTP_X_REQUESTED_WITH header
678 *
Andrey Andreev9448afb2012-02-08 19:49:19 +0200679 * @return bool
Greg Aker081ac9d2010-11-22 14:42:53 -0600680 */
681 public function is_ajax_request()
682 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200683 return ( ! empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest');
Greg Aker081ac9d2010-11-22 14:42:53 -0600684 }
685
Phil Sturgeonc3828712011-01-19 12:31:47 +0000686 // --------------------------------------------------------------------
687
688 /**
689 * Is cli Request?
690 *
691 * Test to see if a request was made from the command line
692 *
Andrey Andreev9448afb2012-02-08 19:49:19 +0200693 * @return bool
Phil Sturgeonc3828712011-01-19 12:31:47 +0000694 */
695 public function is_cli_request()
696 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200697 return (php_sapi_name() === 'cli' OR defined('STDIN'));
Phil Sturgeonc3828712011-01-19 12:31:47 +0000698 }
699
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100700 // --------------------------------------------------------------------
701
702 /**
703 * Get Request Method
704 *
Michiel Vugteveendc900df2012-03-07 20:41:37 +0100705 * Return the Request Method
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100706 *
Michiel Vugteveendc900df2012-03-07 20:41:37 +0100707 * @param bool uppercase or lowercase
Michiel Vugteveen7c8841f2012-03-07 20:49:06 +0100708 * @return bool
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100709 */
Michiel Vugteveen704fb162012-03-07 20:42:33 +0100710 public function method($upper = FALSE)
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100711 {
Michiel Vugteveendc900df2012-03-07 20:41:37 +0100712 return ($upper)
713 ? strtoupper($this->server('REQUEST_METHOD'))
714 : strtolower($this->server('REQUEST_METHOD'));
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100715 }
716
Derek Allard2067d1a2008-11-13 22:59:24 +0000717}
Derek Allard2067d1a2008-11-13 22:59:24 +0000718
719/* End of file Input.php */
Timothy Warren40403d22012-04-19 16:38:50 -0400720/* Location: ./system/core/Input.php */