blob: 901b4147e7cb6cd7da5574718a31ed153bda5b02 [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 */
Andrey Andreev64e98aa2012-01-07 20:29:10 +020046 public $ip_address = FALSE;
David Behler9b5df592011-08-14 21:04:17 +020047 /**
48 * user agent (web browser) being used by the current user
49 *
50 * @var string
51 */
Andrey Andreev64e98aa2012-01-07 20:29:10 +020052 public $user_agent = FALSE;
David Behler9b5df592011-08-14 21:04:17 +020053 /**
54 * If FALSE, then $_GET will be set to an empty array
55 *
56 * @var bool
57 */
Andrey Andreev13774972012-01-08 04:30:33 +020058 protected $_allow_get_array = TRUE;
David Behler9b5df592011-08-14 21:04:17 +020059 /**
60 * If TRUE, then newlines are standardized
61 *
62 * @var bool
63 */
Andrey Andreev13774972012-01-08 04:30:33 +020064 protected $_standardize_newlines = TRUE;
David Behler9b5df592011-08-14 21:04:17 +020065 /**
66 * Determines whether the XSS filter is always active when GET, POST or COOKIE data is encountered
67 * Set automatically based on config setting
68 *
69 * @var bool
70 */
Andrey Andreev13774972012-01-08 04:30:33 +020071 protected $_enable_xss = FALSE;
David Behler9b5df592011-08-14 21:04:17 +020072 /**
73 * Enables a CSRF cookie token to be set.
74 * Set automatically based on config setting
75 *
76 * @var bool
77 */
Andrey Andreev64e98aa2012-01-07 20:29:10 +020078 protected $_enable_csrf = FALSE;
David Behler9b5df592011-08-14 21:04:17 +020079 /**
80 * List of all HTTP request headers
81 *
82 * @var array
83 */
Greg Akerec2f5712010-11-15 16:22:12 -060084 protected $headers = array();
David Behler9b5df592011-08-14 21:04:17 +020085
Derek Allard2067d1a2008-11-13 22:59:24 +000086 /**
Greg Akera9263282010-11-10 15:26:43 -060087 * Constructor
88 *
89 * Sets whether to globally enable the XSS processing
90 * and whether to allow the $_GET array
Greg Akera9263282010-11-10 15:26:43 -060091 */
92 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000093 {
Andrey Andreev13774972012-01-08 04:30:33 +020094 log_message('debug', 'Input Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +000095
Phil Sturgeonc8089152010-12-27 19:06:28 +000096 $this->_allow_get_array = (config_item('allow_get_array') === TRUE);
Andrey Andreev64e98aa2012-01-07 20:29:10 +020097 $this->_enable_xss = (config_item('global_xss_filtering') === TRUE);
98 $this->_enable_csrf = (config_item('csrf_protection') === TRUE);
Derek Jones69fc4fc2010-03-02 13:36:31 -060099
Pascal Kriete14a0ac62011-04-05 14:55:56 -0400100 global $SEC;
101 $this->security =& $SEC;
Derek Jones69fc4fc2010-03-02 13:36:31 -0600102
Pascal Krieteaaec1e42011-01-20 00:01:21 -0500103 // Do we need the UTF-8 class?
Derek Jones69fc4fc2010-03-02 13:36:31 -0600104 if (UTF8_ENABLED === TRUE)
105 {
106 global $UNI;
107 $this->uni =& $UNI;
108 }
109
110 // Sanitize global arrays
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 $this->_sanitize_globals();
112 }
113
114 // --------------------------------------------------------------------
115
116 /**
Greg Akera9263282010-11-10 15:26:43 -0600117 * Fetch from array
118 *
119 * This is a helper function to retrieve values from global arrays
120 *
Greg Akera9263282010-11-10 15:26:43 -0600121 * @param array
122 * @param string
123 * @param bool
124 * @return string
125 */
Bo-Yi Wu47213792011-09-13 22:44:07 +0800126 protected function _fetch_from_array(&$array, $index = '', $xss_clean = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 {
128 if ( ! isset($array[$index]))
129 {
130 return FALSE;
131 }
132
133 if ($xss_clean === TRUE)
134 {
Pascal Kriete14a0ac62011-04-05 14:55:56 -0400135 return $this->security->xss_clean($array[$index]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 }
137
138 return $array[$index];
139 }
140
141 // --------------------------------------------------------------------
142
143 /**
144 * Fetch an item from the GET array
145 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 * @param string
147 * @param bool
148 * @return string
149 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800150 public function get($index = NULL, $xss_clean = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 {
Phil Sturgeon44f21052011-02-15 21:39:25 +0000152 // Check if a field has been provided
Andrey Andreev9448afb2012-02-08 19:49:19 +0200153 if ($index === NULL && ! empty($_GET))
vascopjff1cfa12011-02-13 21:30:19 +0000154 {
Phil Sturgeon44f21052011-02-15 21:39:25 +0000155 $get = array();
vascopjff1cfa12011-02-13 21:30:19 +0000156
157 // loop through the full _GET array
Phil Sturgeon44f21052011-02-15 21:39:25 +0000158 foreach (array_keys($_GET) as $key)
vascopjff1cfa12011-02-13 21:30:19 +0000159 {
Phil Sturgeon44f21052011-02-15 21:39:25 +0000160 $get[$key] = $this->_fetch_from_array($_GET, $key, $xss_clean);
vascopjff1cfa12011-02-13 21:30:19 +0000161 }
Phil Sturgeon44f21052011-02-15 21:39:25 +0000162 return $get;
vascopjff1cfa12011-02-13 21:30:19 +0000163 }
164
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 return $this->_fetch_from_array($_GET, $index, $xss_clean);
166 }
167
168 // --------------------------------------------------------------------
169
170 /**
171 * Fetch an item from the POST array
172 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 * @param string
174 * @param bool
175 * @return string
176 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800177 public function post($index = NULL, $xss_clean = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 {
Phil Sturgeon44f21052011-02-15 21:39:25 +0000179 // Check if a field has been provided
Andrey Andreev9448afb2012-02-08 19:49:19 +0200180 if ($index === NULL && ! empty($_POST))
vascopj0ba58b82011-02-06 14:20:21 +0000181 {
Phil Sturgeon44f21052011-02-15 21:39:25 +0000182 $post = array();
vascopj0ba58b82011-02-06 14:20:21 +0000183
Phil Sturgeon44f21052011-02-15 21:39:25 +0000184 // Loop through the full _POST array and return it
185 foreach (array_keys($_POST) as $key)
vascopj0ba58b82011-02-06 14:20:21 +0000186 {
Phil Sturgeon44f21052011-02-15 21:39:25 +0000187 $post[$key] = $this->_fetch_from_array($_POST, $key, $xss_clean);
vascopj0ba58b82011-02-06 14:20:21 +0000188 }
Phil Sturgeon44f21052011-02-15 21:39:25 +0000189 return $post;
vascopj0ba58b82011-02-06 14:20:21 +0000190 }
David Behler9b5df592011-08-14 21:04:17 +0200191
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 return $this->_fetch_from_array($_POST, $index, $xss_clean);
193 }
194
Derek Jones69fc4fc2010-03-02 13:36:31 -0600195
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 // --------------------------------------------------------------------
197
198 /**
199 * Fetch an item from either the GET array or the POST
200 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 * @param string The index key
202 * @param bool XSS cleaning
203 * @return string
204 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800205 public function get_post($index = '', $xss_clean = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200207 return isset($_POST[$index])
208 ? $this->post($index, $xss_clean)
209 : $this->get($index, $xss_clean);
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 }
211
212 // --------------------------------------------------------------------
213
214 /**
215 * Fetch an item from the COOKIE array
216 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 * @param string
218 * @param bool
219 * @return string
220 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800221 public function cookie($index = '', $xss_clean = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 {
223 return $this->_fetch_from_array($_COOKIE, $index, $xss_clean);
224 }
225
Derek Jones69fc4fc2010-03-02 13:36:31 -0600226 // ------------------------------------------------------------------------
227
228 /**
229 * Set cookie
230 *
231 * Accepts six parameter, or you can submit an associative
232 * array in the first parameter containing all the values.
233 *
Derek Jones69fc4fc2010-03-02 13:36:31 -0600234 * @param mixed
235 * @param string the value of the cookie
236 * @param string the number of seconds until expiration
Derek Jones37f4b9c2011-07-01 17:56:50 -0500237 * @param string the cookie domain. Usually: .yourdomain.com
Derek Jones69fc4fc2010-03-02 13:36:31 -0600238 * @param string the cookie path
239 * @param string the cookie prefix
Phil Sturgeond8d1e242011-02-16 17:23:16 +0000240 * @param bool true makes the cookie secure
Derek Jones69fc4fc2010-03-02 13:36:31 -0600241 * @return void
242 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800243 public function set_cookie($name = '', $value = '', $expire = '', $domain = '', $path = '/', $prefix = '', $secure = FALSE)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600244 {
245 if (is_array($name))
246 {
tobiasbg9aa7dc92011-02-18 21:57:13 +0100247 // always leave 'name' in last place, as the loop will break otherwise, due to $$item
248 foreach (array('value', 'expire', 'domain', 'path', 'prefix', 'secure', 'name') as $item)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600249 {
250 if (isset($name[$item]))
251 {
252 $$item = $name[$item];
253 }
254 }
255 }
256
Andrey Andreev9448afb2012-02-08 19:49:19 +0200257 if ($prefix == '' && config_item('cookie_prefix') != '')
Derek Jones69fc4fc2010-03-02 13:36:31 -0600258 {
259 $prefix = config_item('cookie_prefix');
260 }
Andrey Andreev9448afb2012-02-08 19:49:19 +0200261 if ($domain == '' && config_item('cookie_domain') != '')
Derek Jones69fc4fc2010-03-02 13:36:31 -0600262 {
263 $domain = config_item('cookie_domain');
264 }
Andrey Andreev9448afb2012-02-08 19:49:19 +0200265 if ($path == '/' && config_item('cookie_path') !== '/')
Derek Jones69fc4fc2010-03-02 13:36:31 -0600266 {
267 $path = config_item('cookie_path');
268 }
Andrey Andreev9448afb2012-02-08 19:49:19 +0200269 if ($secure == FALSE && config_item('cookie_secure') != FALSE)
tobiasbg9aa7dc92011-02-18 21:57:13 +0100270 {
271 $secure = config_item('cookie_secure');
272 }
Derek Jones69fc4fc2010-03-02 13:36:31 -0600273
274 if ( ! is_numeric($expire))
275 {
276 $expire = time() - 86500;
277 }
278 else
279 {
Phil Sturgeonc8089152010-12-27 19:06:28 +0000280 $expire = ($expire > 0) ? time() + $expire : 0;
Derek Jones69fc4fc2010-03-02 13:36:31 -0600281 }
282
Phil Sturgeond8d1e242011-02-16 17:23:16 +0000283 setcookie($prefix.$name, $value, $expire, $path, $domain, $secure);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600284 }
285
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 // --------------------------------------------------------------------
287
288 /**
289 * Fetch an item from the SERVER array
290 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 * @param string
292 * @param bool
293 * @return string
294 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800295 public function server($index = '', $xss_clean = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 {
297 return $this->_fetch_from_array($_SERVER, $index, $xss_clean);
298 }
299
300 // --------------------------------------------------------------------
301
302 /**
303 * Fetch the IP Address
304 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 * @return string
306 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800307 public function ip_address()
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 {
309 if ($this->ip_address !== FALSE)
310 {
311 return $this->ip_address;
312 }
Barry Mienydd671972010-10-04 16:33:58 +0200313
Derek Jones42b2e172009-02-05 16:59:45 +0000314 if (config_item('proxy_ips') != '' && $this->server('HTTP_X_FORWARDED_FOR') && $this->server('REMOTE_ADDR'))
Derek Jonesc5972282009-02-04 21:40:20 +0000315 {
Derek Jones42b2e172009-02-05 16:59:45 +0000316 $proxies = preg_split('/[\s,]/', config_item('proxy_ips'), -1, PREG_SPLIT_NO_EMPTY);
Derek Jonesc5972282009-02-04 21:40:20 +0000317 $proxies = is_array($proxies) ? $proxies : array($proxies);
Derek Allard2067d1a2008-11-13 22:59:24 +0000318
Derek Jonesc5972282009-02-04 21:40:20 +0000319 $this->ip_address = in_array($_SERVER['REMOTE_ADDR'], $proxies) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
320 }
Andrey Andreev9448afb2012-02-08 19:49:19 +0200321 elseif ( ! $this->server('HTTP_CLIENT_IP') && $this->server('REMOTE_ADDR'))
John Bellone52c10b62011-08-21 11:41:32 -0400322 {
323 $this->ip_address = $_SERVER['REMOTE_ADDR'];
324 }
Andrey Andreev9448afb2012-02-08 19:49:19 +0200325 elseif ($this->server('REMOTE_ADDR') && $this->server('HTTP_CLIENT_IP'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 {
327 $this->ip_address = $_SERVER['HTTP_CLIENT_IP'];
328 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 elseif ($this->server('HTTP_CLIENT_IP'))
330 {
331 $this->ip_address = $_SERVER['HTTP_CLIENT_IP'];
332 }
333 elseif ($this->server('HTTP_X_FORWARDED_FOR'))
334 {
335 $this->ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
336 }
337
338 if ($this->ip_address === FALSE)
339 {
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200340 return $this->ip_address = '0.0.0.0';
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 }
342
Robin Sowell76b369e2010-03-19 11:15:28 -0400343 if (strpos($this->ip_address, ',') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 {
345 $x = explode(',', $this->ip_address);
Derek Jonesc5972282009-02-04 21:40:20 +0000346 $this->ip_address = trim(end($x));
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 }
348
349 if ( ! $this->valid_ip($this->ip_address))
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
354 return $this->ip_address;
355 }
356
357 // --------------------------------------------------------------------
358
359 /**
360 * Validate IP Address
361 *
362 * Updated version suggested by Geert De Deckere
Barry Mienydd671972010-10-04 16:33:58 +0200363 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 * @param string
Bo-Yi Wu013c8952011-09-12 15:03:44 +0800365 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800367 public function valid_ip($ip)
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 {
Andrey Andreev1ae65162012-03-10 16:11:34 +0200369 return (bool) filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 }
371
372 // --------------------------------------------------------------------
373
374 /**
375 * User Agent
376 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 * @return string
378 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800379 public function user_agent()
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 {
381 if ($this->user_agent !== FALSE)
382 {
383 return $this->user_agent;
384 }
385
Andrey Andreev9448afb2012-02-08 19:49:19 +0200386 return $this->user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 }
388
389 // --------------------------------------------------------------------
390
391 /**
Derek Jones69fc4fc2010-03-02 13:36:31 -0600392 * Sanitize Globals
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 *
Derek Jones69fc4fc2010-03-02 13:36:31 -0600394 * This function does the following:
395 *
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200396 * - Unsets $_GET data (if query strings are not enabled)
397 * - Unsets all globals if register_globals is enabled
398 * - Standardizes newline characters to \n
Derek Jones69fc4fc2010-03-02 13:36:31 -0600399 *
Derek Jones69fc4fc2010-03-02 13:36:31 -0600400 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 */
Andrey Andreev90cfe142012-01-08 04:46:42 +0200402 protected function _sanitize_globals()
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600404 // It would be "wrong" to unset any of these GLOBALS.
David Behler9b5df592011-08-14 21:04:17 +0200405 $protected = array('_SERVER', '_GET', '_POST', '_FILES', '_REQUEST',
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200406 '_SESSION', '_ENV', 'GLOBALS', 'HTTP_RAW_POST_DATA',
407 'system_folder', 'application_folder', 'BM', 'EXT',
408 'CFG', 'URI', 'RTR', 'OUT', 'IN'
409 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000410
Barry Mienydd671972010-10-04 16:33:58 +0200411 // Unset globals for securiy.
Derek Jones69fc4fc2010-03-02 13:36:31 -0600412 // This is effectively the same as register_globals = off
413 foreach (array($_GET, $_POST, $_COOKIE) as $global)
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600415 if ( ! is_array($global))
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600417 if ( ! in_array($global, $protected))
418 {
419 global $$global;
420 $$global = NULL;
421 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 }
Derek Jones69fc4fc2010-03-02 13:36:31 -0600423 else
424 {
425 foreach ($global as $key => $val)
426 {
427 if ( ! in_array($key, $protected))
428 {
429 global $$key;
430 $$key = NULL;
431 }
432 }
433 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 }
435
Derek Jones69fc4fc2010-03-02 13:36:31 -0600436 // Is $_GET data allowed? If not we'll set the $_GET to an empty array
437 if ($this->_allow_get_array == FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600439 $_GET = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 }
Andrey Andreev9448afb2012-02-08 19:49:19 +0200441 elseif (is_array($_GET) && count($_GET) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200443 foreach ($_GET as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200445 $_GET[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 }
447 }
448
Derek Jones69fc4fc2010-03-02 13:36:31 -0600449 // Clean $_POST Data
Andrey Andreev9448afb2012-02-08 19:49:19 +0200450 if (is_array($_POST) && count($_POST) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 {
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500452 foreach ($_POST as $key => $val)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600453 {
454 $_POST[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
455 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 }
457
Derek Jones69fc4fc2010-03-02 13:36:31 -0600458 // Clean $_COOKIE Data
Andrey Andreev9448afb2012-02-08 19:49:19 +0200459 if (is_array($_COOKIE) && count($_COOKIE) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600461 // Also get rid of specially treated cookies that might be set by a server
462 // or silly application, that are of no use to a CI application anyway
463 // but that when present will trip our 'Disallowed Key Characters' alarm
464 // http://www.ietf.org/rfc/rfc2109.txt
465 // note that the key names below are single quoted strings, and are not PHP variables
466 unset($_COOKIE['$Version']);
467 unset($_COOKIE['$Path']);
468 unset($_COOKIE['$Domain']);
469
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500470 foreach ($_COOKIE as $key => $val)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600471 {
472 $_COOKIE[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
473 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 }
475
Derek Jones69fc4fc2010-03-02 13:36:31 -0600476 // Sanitize PHP_SELF
477 $_SERVER['PHP_SELF'] = strip_tags($_SERVER['PHP_SELF']);
478
Derek Jones69fc4fc2010-03-02 13:36:31 -0600479 // CSRF Protection check
480 if ($this->_enable_csrf == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600482 $this->security->csrf_verify();
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000484
Andrey Andreev90cfe142012-01-08 04:46:42 +0200485 log_message('debug', 'Global POST and COOKIE data sanitized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 }
487
488 // --------------------------------------------------------------------
489
490 /**
Derek Jones69fc4fc2010-03-02 13:36:31 -0600491 * Clean Input Data
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 *
Derek Jones69fc4fc2010-03-02 13:36:31 -0600493 * This is a helper function. It escapes data and
494 * standardizes newline characters to \n
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 * @return string
498 */
Andrey Andreev90cfe142012-01-08 04:46:42 +0200499 protected function _clean_input_data($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600501 if (is_array($str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600503 $new_array = array();
504 foreach ($str as $key => $val)
505 {
506 $new_array[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
507 }
508 return $new_array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 }
510
Andrey Andreevaf728622011-10-20 10:11:59 +0300511 /* We strip slashes if magic quotes is on to keep things consistent
512
513 NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and
514 it will probably not exist in future versions at all.
515 */
516 if ( ! is_php('5.4') && get_magic_quotes_gpc())
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600518 $str = stripslashes($str);
519 }
520
521 // Clean UTF-8 if supported
522 if (UTF8_ENABLED === TRUE)
523 {
524 $str = $this->uni->clean_string($str);
525 }
David Behler9b5df592011-08-14 21:04:17 +0200526
Pascal Kriete14a0ac62011-04-05 14:55:56 -0400527 // Remove control characters
528 $str = remove_invisible_characters($str);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600529
530 // Should we filter the input data?
531 if ($this->_enable_xss === TRUE)
532 {
533 $str = $this->security->xss_clean($str);
534 }
535
536 // Standardize newlines if needed
Andrey Andreev9448afb2012-02-08 19:49:19 +0200537 if ($this->_standardize_newlines == TRUE && strpos($str, "\r") !== FALSE)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600538 {
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200539 return str_replace(array("\r\n", "\r", "\r\n\n"), PHP_EOL, $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 }
541
542 return $str;
543 }
544
545 // --------------------------------------------------------------------
546
547 /**
Derek Jones69fc4fc2010-03-02 13:36:31 -0600548 * Clean Keys
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 *
Derek Jones69fc4fc2010-03-02 13:36:31 -0600550 * This is a helper function. To prevent malicious users
551 * from trying to exploit keys we make sure that keys are
552 * only named with alpha-numeric text and a few other items.
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 * @param string
555 * @return string
556 */
Andrey Andreev90cfe142012-01-08 04:46:42 +0200557 protected function _clean_input_keys($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 {
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200559 if ( ! preg_match('/^[a-z0-9:_\/-]+$/i', $str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 {
Kevin Cuppd63e4012012-02-05 14:14:32 -0500561 set_status_header(503);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600562 exit('Disallowed Key Characters.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000563 }
564
Derek Jones69fc4fc2010-03-02 13:36:31 -0600565 // Clean UTF-8 if supported
566 if (UTF8_ENABLED === TRUE)
567 {
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200568 return $this->uni->clean_string($str);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600569 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000570
Derek Jones69fc4fc2010-03-02 13:36:31 -0600571 return $str;
572 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000573
Greg Akerec2f5712010-11-15 16:22:12 -0600574 // --------------------------------------------------------------------
575
576 /**
577 * Request Headers
578 *
David Behler9b5df592011-08-14 21:04:17 +0200579 * In Apache, you can simply call apache_request_headers(), however for
Greg Akerec2f5712010-11-15 16:22:12 -0600580 * people running other webservers the function is undefined.
581 *
David Behlercda768a2011-08-14 23:52:48 +0200582 * @param bool XSS cleaning
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200583 * @return array
Greg Akerec2f5712010-11-15 16:22:12 -0600584 */
585 public function request_headers($xss_clean = FALSE)
586 {
587 // Look at Apache go!
588 if (function_exists('apache_request_headers'))
589 {
590 $headers = apache_request_headers();
591 }
592 else
593 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200594 $headers['Content-Type'] = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : @getenv('CONTENT_TYPE');
Greg Akerec2f5712010-11-15 16:22:12 -0600595
596 foreach ($_SERVER as $key => $val)
597 {
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200598 if (strpos($key, 'HTTP_') === 0)
Greg Akerec2f5712010-11-15 16:22:12 -0600599 {
600 $headers[substr($key, 5)] = $this->_fetch_from_array($_SERVER, $key, $xss_clean);
601 }
602 }
603 }
604
605 // take SOME_HEADER and turn it into Some-Header
606 foreach ($headers as $key => $val)
607 {
608 $key = str_replace('_', ' ', strtolower($key));
609 $key = str_replace(' ', '-', ucwords($key));
David Behler9b5df592011-08-14 21:04:17 +0200610
Greg Akerec2f5712010-11-15 16:22:12 -0600611 $this->headers[$key] = $val;
612 }
David Behler9b5df592011-08-14 21:04:17 +0200613
Greg Akerec2f5712010-11-15 16:22:12 -0600614 return $this->headers;
615 }
616
617 // --------------------------------------------------------------------
618
619 /**
620 * Get Request Header
621 *
622 * Returns the value of a single member of the headers class member
623 *
Andrey Andreev773e1172012-02-08 23:02:19 +0200624 * @param string array key for $this->headers
Andrey Andreev9448afb2012-02-08 19:49:19 +0200625 * @param bool XSS Clean or not
Andrey Andreev773e1172012-02-08 23:02:19 +0200626 * @return mixed FALSE on failure, string on success
Greg Akerec2f5712010-11-15 16:22:12 -0600627 */
628 public function get_request_header($index, $xss_clean = FALSE)
629 {
630 if (empty($this->headers))
631 {
632 $this->request_headers();
633 }
David Behler9b5df592011-08-14 21:04:17 +0200634
Greg Akerec2f5712010-11-15 16:22:12 -0600635 if ( ! isset($this->headers[$index]))
636 {
637 return FALSE;
638 }
639
Andrey Andreev9448afb2012-02-08 19:49:19 +0200640 return ($xss_clean === TRUE)
641 ? $this->security->xss_clean($this->headers[$index])
642 : $this->headers[$index];
Greg Akerec2f5712010-11-15 16:22:12 -0600643 }
644
Greg Aker081ac9d2010-11-22 14:42:53 -0600645 // --------------------------------------------------------------------
Phil Sturgeonc3828712011-01-19 12:31:47 +0000646
Greg Aker081ac9d2010-11-22 14:42:53 -0600647 /**
648 * Is ajax Request?
649 *
650 * Test to see if a request contains the HTTP_X_REQUESTED_WITH header
651 *
Andrey Andreev9448afb2012-02-08 19:49:19 +0200652 * @return bool
Greg Aker081ac9d2010-11-22 14:42:53 -0600653 */
654 public function is_ajax_request()
655 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200656 return ( ! empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest');
Greg Aker081ac9d2010-11-22 14:42:53 -0600657 }
658
Phil Sturgeonc3828712011-01-19 12:31:47 +0000659 // --------------------------------------------------------------------
660
661 /**
662 * Is cli Request?
663 *
664 * Test to see if a request was made from the command line
665 *
Andrey Andreev9448afb2012-02-08 19:49:19 +0200666 * @return bool
Phil Sturgeonc3828712011-01-19 12:31:47 +0000667 */
668 public function is_cli_request()
669 {
Andrey Andreev9448afb2012-02-08 19:49:19 +0200670 return (php_sapi_name() === 'cli' OR defined('STDIN'));
Phil Sturgeonc3828712011-01-19 12:31:47 +0000671 }
672
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100673 // --------------------------------------------------------------------
674
675 /**
676 * Get Request Method
677 *
Michiel Vugteveendc900df2012-03-07 20:41:37 +0100678 * Return the Request Method
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100679 *
Michiel Vugteveendc900df2012-03-07 20:41:37 +0100680 * @param bool uppercase or lowercase
Michiel Vugteveen7c8841f2012-03-07 20:49:06 +0100681 * @return bool
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100682 */
Michiel Vugteveen704fb162012-03-07 20:42:33 +0100683 public function method($upper = FALSE)
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100684 {
Michiel Vugteveendc900df2012-03-07 20:41:37 +0100685 return ($upper)
686 ? strtoupper($this->server('REQUEST_METHOD'))
687 : strtolower($this->server('REQUEST_METHOD'));
Michiel Vugteveenbe0ca262012-03-07 19:09:51 +0100688 }
689
Derek Allard2067d1a2008-11-13 22:59:24 +0000690}
Derek Allard2067d1a2008-11-13 22:59:24 +0000691
692/* End of file Input.php */
Phil Sturgeon33ed0f32011-02-16 19:03:49 +0000693/* Location: ./system/core/Input.php */