blob: 07bb30b155b7de34cd5700899bf58228b88661df [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 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
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
28// ------------------------------------------------------------------------
29
30/**
31 * Input Class
32 *
33 * Pre-processes global input data for security
34 *
35 * @package CodeIgniter
36 * @subpackage Libraries
37 * @category Input
Derek Jonesf4a4bd82011-10-20 12:18:42 -050038 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000039 * @link http://codeigniter.com/user_guide/libraries/input.html
40 */
41class CI_Input {
Derek Allard2067d1a2008-11-13 22:59:24 +000042
David Behler9b5df592011-08-14 21:04:17 +020043 /**
44 * IP address of the current user
45 *
46 * @var string
47 */
Andrey Andreev64e98aa2012-01-07 20:29:10 +020048 public $ip_address = FALSE;
David Behler9b5df592011-08-14 21:04:17 +020049 /**
50 * user agent (web browser) being used by the current user
51 *
52 * @var string
53 */
Andrey Andreev64e98aa2012-01-07 20:29:10 +020054 public $user_agent = FALSE;
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 */
Andrey Andreev64e98aa2012-01-07 20:29:10 +020060 public $_allow_get_array = TRUE;
David Behler9b5df592011-08-14 21:04:17 +020061 /**
62 * If TRUE, then newlines are standardized
63 *
64 * @var bool
65 */
Andrey Andreev64e98aa2012-01-07 20:29:10 +020066 public $_standardize_newlines = TRUE;
David Behler9b5df592011-08-14 21:04:17 +020067 /**
68 * Determines whether the XSS filter is always active when GET, POST or COOKIE data is encountered
69 * Set automatically based on config setting
70 *
71 * @var bool
72 */
Andrey Andreev64e98aa2012-01-07 20:29:10 +020073 public $_enable_xss = FALSE;
David Behler9b5df592011-08-14 21:04:17 +020074 /**
75 * Enables a CSRF cookie token to be set.
76 * Set automatically based on config setting
77 *
78 * @var bool
79 */
Andrey Andreev64e98aa2012-01-07 20:29:10 +020080 protected $_enable_csrf = FALSE;
David Behler9b5df592011-08-14 21:04:17 +020081 /**
82 * List of all HTTP request headers
83 *
84 * @var array
85 */
Greg Akerec2f5712010-11-15 16:22:12 -060086 protected $headers = array();
David Behler9b5df592011-08-14 21:04:17 +020087
Greg Akerec2f5712010-11-15 16:22:12 -060088
Derek Allard2067d1a2008-11-13 22:59:24 +000089 /**
Greg Akera9263282010-11-10 15:26:43 -060090 * Constructor
91 *
92 * Sets whether to globally enable the XSS processing
93 * and whether to allow the $_GET array
94 *
95 */
96 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000097 {
98 log_message('debug', "Input Class Initialized");
99
Phil Sturgeonc8089152010-12-27 19:06:28 +0000100 $this->_allow_get_array = (config_item('allow_get_array') === TRUE);
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200101 $this->_enable_xss = (config_item('global_xss_filtering') === TRUE);
102 $this->_enable_csrf = (config_item('csrf_protection') === TRUE);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600103
Pascal Kriete14a0ac62011-04-05 14:55:56 -0400104 global $SEC;
105 $this->security =& $SEC;
Derek Jones69fc4fc2010-03-02 13:36:31 -0600106
Pascal Krieteaaec1e42011-01-20 00:01:21 -0500107 // Do we need the UTF-8 class?
Derek Jones69fc4fc2010-03-02 13:36:31 -0600108 if (UTF8_ENABLED === TRUE)
109 {
110 global $UNI;
111 $this->uni =& $UNI;
112 }
113
114 // Sanitize global arrays
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 $this->_sanitize_globals();
116 }
117
118 // --------------------------------------------------------------------
119
120 /**
Greg Akera9263282010-11-10 15:26:43 -0600121 * Fetch from array
122 *
123 * This is a helper function to retrieve values from global arrays
124 *
Greg Akera9263282010-11-10 15:26:43 -0600125 * @param array
126 * @param string
127 * @param bool
128 * @return string
129 */
Bo-Yi Wu47213792011-09-13 22:44:07 +0800130 protected function _fetch_from_array(&$array, $index = '', $xss_clean = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 {
132 if ( ! isset($array[$index]))
133 {
134 return FALSE;
135 }
136
137 if ($xss_clean === TRUE)
138 {
Pascal Kriete14a0ac62011-04-05 14:55:56 -0400139 return $this->security->xss_clean($array[$index]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 }
141
142 return $array[$index];
143 }
144
145 // --------------------------------------------------------------------
146
147 /**
148 * Fetch an item from the GET array
149 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 * @param string
151 * @param bool
152 * @return string
153 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800154 public function get($index = NULL, $xss_clean = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 {
Phil Sturgeon44f21052011-02-15 21:39:25 +0000156 // Check if a field has been provided
157 if ($index === NULL AND ! empty($_GET))
vascopjff1cfa12011-02-13 21:30:19 +0000158 {
Phil Sturgeon44f21052011-02-15 21:39:25 +0000159 $get = array();
vascopjff1cfa12011-02-13 21:30:19 +0000160
161 // loop through the full _GET array
Phil Sturgeon44f21052011-02-15 21:39:25 +0000162 foreach (array_keys($_GET) as $key)
vascopjff1cfa12011-02-13 21:30:19 +0000163 {
Phil Sturgeon44f21052011-02-15 21:39:25 +0000164 $get[$key] = $this->_fetch_from_array($_GET, $key, $xss_clean);
vascopjff1cfa12011-02-13 21:30:19 +0000165 }
Phil Sturgeon44f21052011-02-15 21:39:25 +0000166 return $get;
vascopjff1cfa12011-02-13 21:30:19 +0000167 }
168
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 return $this->_fetch_from_array($_GET, $index, $xss_clean);
170 }
171
172 // --------------------------------------------------------------------
173
174 /**
175 * Fetch an item from the POST array
176 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 * @param string
178 * @param bool
179 * @return string
180 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800181 public function post($index = NULL, $xss_clean = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 {
Phil Sturgeon44f21052011-02-15 21:39:25 +0000183 // Check if a field has been provided
184 if ($index === NULL AND ! empty($_POST))
vascopj0ba58b82011-02-06 14:20:21 +0000185 {
Phil Sturgeon44f21052011-02-15 21:39:25 +0000186 $post = array();
vascopj0ba58b82011-02-06 14:20:21 +0000187
Phil Sturgeon44f21052011-02-15 21:39:25 +0000188 // Loop through the full _POST array and return it
189 foreach (array_keys($_POST) as $key)
vascopj0ba58b82011-02-06 14:20:21 +0000190 {
Phil Sturgeon44f21052011-02-15 21:39:25 +0000191 $post[$key] = $this->_fetch_from_array($_POST, $key, $xss_clean);
vascopj0ba58b82011-02-06 14:20:21 +0000192 }
Phil Sturgeon44f21052011-02-15 21:39:25 +0000193 return $post;
vascopj0ba58b82011-02-06 14:20:21 +0000194 }
David Behler9b5df592011-08-14 21:04:17 +0200195
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 return $this->_fetch_from_array($_POST, $index, $xss_clean);
197 }
198
Derek Jones69fc4fc2010-03-02 13:36:31 -0600199
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 // --------------------------------------------------------------------
201
202 /**
203 * Fetch an item from either the GET array or the POST
204 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 * @param string The index key
206 * @param bool XSS cleaning
207 * @return string
208 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800209 public function get_post($index = '', $xss_clean = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 {
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200211 return ( ! isset($_POST[$index]))
212 ? $this->get($index, $xss_clean)
213 : $this->post($index, $xss_clean);
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 }
215
216 // --------------------------------------------------------------------
217
218 /**
219 * Fetch an item from the COOKIE array
220 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 * @param string
222 * @param bool
223 * @return string
224 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800225 public function cookie($index = '', $xss_clean = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 {
227 return $this->_fetch_from_array($_COOKIE, $index, $xss_clean);
228 }
229
Derek Jones69fc4fc2010-03-02 13:36:31 -0600230 // ------------------------------------------------------------------------
231
232 /**
233 * Set cookie
234 *
235 * Accepts six parameter, or you can submit an associative
236 * array in the first parameter containing all the values.
237 *
Derek Jones69fc4fc2010-03-02 13:36:31 -0600238 * @param mixed
239 * @param string the value of the cookie
240 * @param string the number of seconds until expiration
Derek Jones37f4b9c2011-07-01 17:56:50 -0500241 * @param string the cookie domain. Usually: .yourdomain.com
Derek Jones69fc4fc2010-03-02 13:36:31 -0600242 * @param string the cookie path
243 * @param string the cookie prefix
Phil Sturgeond8d1e242011-02-16 17:23:16 +0000244 * @param bool true makes the cookie secure
Derek Jones69fc4fc2010-03-02 13:36:31 -0600245 * @return void
246 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800247 public function set_cookie($name = '', $value = '', $expire = '', $domain = '', $path = '/', $prefix = '', $secure = FALSE)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600248 {
249 if (is_array($name))
250 {
tobiasbg9aa7dc92011-02-18 21:57:13 +0100251 // always leave 'name' in last place, as the loop will break otherwise, due to $$item
252 foreach (array('value', 'expire', 'domain', 'path', 'prefix', 'secure', 'name') as $item)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600253 {
254 if (isset($name[$item]))
255 {
256 $$item = $name[$item];
257 }
258 }
259 }
260
261 if ($prefix == '' AND config_item('cookie_prefix') != '')
262 {
263 $prefix = config_item('cookie_prefix');
264 }
265 if ($domain == '' AND config_item('cookie_domain') != '')
266 {
267 $domain = config_item('cookie_domain');
268 }
269 if ($path == '/' AND config_item('cookie_path') != '/')
270 {
271 $path = config_item('cookie_path');
272 }
tobiasbg9aa7dc92011-02-18 21:57:13 +0100273 if ($secure == FALSE AND config_item('cookie_secure') != FALSE)
274 {
275 $secure = config_item('cookie_secure');
276 }
Derek Jones69fc4fc2010-03-02 13:36:31 -0600277
278 if ( ! is_numeric($expire))
279 {
280 $expire = time() - 86500;
281 }
282 else
283 {
Phil Sturgeonc8089152010-12-27 19:06:28 +0000284 $expire = ($expire > 0) ? time() + $expire : 0;
Derek Jones69fc4fc2010-03-02 13:36:31 -0600285 }
286
Phil Sturgeond8d1e242011-02-16 17:23:16 +0000287 setcookie($prefix.$name, $value, $expire, $path, $domain, $secure);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600288 }
289
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 // --------------------------------------------------------------------
291
292 /**
293 * Fetch an item from the SERVER array
294 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 * @param string
296 * @param bool
297 * @return string
298 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800299 public function server($index = '', $xss_clean = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 {
301 return $this->_fetch_from_array($_SERVER, $index, $xss_clean);
302 }
303
304 // --------------------------------------------------------------------
305
306 /**
307 * Fetch the IP Address
308 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 * @return string
310 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800311 public function ip_address()
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 {
313 if ($this->ip_address !== FALSE)
314 {
315 return $this->ip_address;
316 }
Barry Mienydd671972010-10-04 16:33:58 +0200317
Derek Jones42b2e172009-02-05 16:59:45 +0000318 if (config_item('proxy_ips') != '' && $this->server('HTTP_X_FORWARDED_FOR') && $this->server('REMOTE_ADDR'))
Derek Jonesc5972282009-02-04 21:40:20 +0000319 {
Derek Jones42b2e172009-02-05 16:59:45 +0000320 $proxies = preg_split('/[\s,]/', config_item('proxy_ips'), -1, PREG_SPLIT_NO_EMPTY);
Derek Jonesc5972282009-02-04 21:40:20 +0000321 $proxies = is_array($proxies) ? $proxies : array($proxies);
Derek Allard2067d1a2008-11-13 22:59:24 +0000322
Derek Jonesc5972282009-02-04 21:40:20 +0000323 $this->ip_address = in_array($_SERVER['REMOTE_ADDR'], $proxies) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
324 }
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200325 elseif ( ! $this->server('HTTP_CLIENT_IP') AND $this->server('REMOTE_ADDR'))
John Bellone52c10b62011-08-21 11:41:32 -0400326 {
327 $this->ip_address = $_SERVER['REMOTE_ADDR'];
328 }
Derek Jonesc5972282009-02-04 21:40:20 +0000329 elseif ($this->server('REMOTE_ADDR') AND $this->server('HTTP_CLIENT_IP'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 {
331 $this->ip_address = $_SERVER['HTTP_CLIENT_IP'];
332 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 elseif ($this->server('HTTP_CLIENT_IP'))
334 {
335 $this->ip_address = $_SERVER['HTTP_CLIENT_IP'];
336 }
337 elseif ($this->server('HTTP_X_FORWARDED_FOR'))
338 {
339 $this->ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
340 }
341
342 if ($this->ip_address === FALSE)
343 {
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200344 return $this->ip_address = '0.0.0.0';
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 }
346
Robin Sowell76b369e2010-03-19 11:15:28 -0400347 if (strpos($this->ip_address, ',') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 {
349 $x = explode(',', $this->ip_address);
Derek Jonesc5972282009-02-04 21:40:20 +0000350 $this->ip_address = trim(end($x));
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 }
352
353 if ( ! $this->valid_ip($this->ip_address))
354 {
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200355 return $this->ip_address = '0.0.0.0';
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 }
357
358 return $this->ip_address;
359 }
360
361 // --------------------------------------------------------------------
362
363 /**
364 * Validate IP Address
365 *
366 * Updated version suggested by Geert De Deckere
Barry Mienydd671972010-10-04 16:33:58 +0200367 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 * @param string
Bo-Yi Wu013c8952011-09-12 15:03:44 +0800369 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800371 public function valid_ip($ip)
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 {
Bo-Yi Wuc9f84c12011-09-12 10:45:39 +0800373 // if php version >= 5.2, use filter_var to check validate ip.
Bo-Yi Wu47213792011-09-13 22:44:07 +0800374 if (function_exists('filter_var'))
Bo-Yi Wuc9f84c12011-09-12 10:45:39 +0800375 {
376 return (bool) filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
377 }
378
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 $ip_segments = explode('.', $ip);
380
381 // Always 4 segments needed
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200382 if (count($ip_segments) !== 4)
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 {
384 return FALSE;
385 }
386 // IP can not start with 0
387 if ($ip_segments[0][0] == '0')
388 {
389 return FALSE;
390 }
391 // Check each segment
392 foreach ($ip_segments as $segment)
393 {
Barry Mienydd671972010-10-04 16:33:58 +0200394 // IP segments must be digits and can not be
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 // longer than 3 digits or greater then 255
396 if ($segment == '' OR preg_match("/[^0-9]/", $segment) OR $segment > 255 OR strlen($segment) > 3)
397 {
398 return FALSE;
399 }
400 }
401
402 return TRUE;
403 }
404
405 // --------------------------------------------------------------------
406
407 /**
408 * User Agent
409 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 * @return string
411 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800412 public function user_agent()
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 {
414 if ($this->user_agent !== FALSE)
415 {
416 return $this->user_agent;
417 }
418
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200419 return $this->user_agent = ( ! isset($_SERVER['HTTP_USER_AGENT'])) ? FALSE : $_SERVER['HTTP_USER_AGENT'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 }
421
422 // --------------------------------------------------------------------
423
424 /**
Derek Jones69fc4fc2010-03-02 13:36:31 -0600425 * Sanitize Globals
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 *
Derek Jones69fc4fc2010-03-02 13:36:31 -0600427 * This function does the following:
428 *
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200429 * - Unsets $_GET data (if query strings are not enabled)
430 * - Unsets all globals if register_globals is enabled
431 * - Standardizes newline characters to \n
Derek Jones69fc4fc2010-03-02 13:36:31 -0600432 *
Derek Jones69fc4fc2010-03-02 13:36:31 -0600433 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800435 private function _sanitize_globals()
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600437 // It would be "wrong" to unset any of these GLOBALS.
David Behler9b5df592011-08-14 21:04:17 +0200438 $protected = array('_SERVER', '_GET', '_POST', '_FILES', '_REQUEST',
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200439 '_SESSION', '_ENV', 'GLOBALS', 'HTTP_RAW_POST_DATA',
440 'system_folder', 'application_folder', 'BM', 'EXT',
441 'CFG', 'URI', 'RTR', 'OUT', 'IN'
442 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000443
Barry Mienydd671972010-10-04 16:33:58 +0200444 // Unset globals for securiy.
Derek Jones69fc4fc2010-03-02 13:36:31 -0600445 // This is effectively the same as register_globals = off
446 foreach (array($_GET, $_POST, $_COOKIE) as $global)
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600448 if ( ! is_array($global))
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600450 if ( ! in_array($global, $protected))
451 {
452 global $$global;
453 $$global = NULL;
454 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 }
Derek Jones69fc4fc2010-03-02 13:36:31 -0600456 else
457 {
458 foreach ($global as $key => $val)
459 {
460 if ( ! in_array($key, $protected))
461 {
462 global $$key;
463 $$key = NULL;
464 }
465 }
466 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 }
468
Derek Jones69fc4fc2010-03-02 13:36:31 -0600469 // Is $_GET data allowed? If not we'll set the $_GET to an empty array
470 if ($this->_allow_get_array == FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600472 $_GET = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 }
474 else
475 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600476 if (is_array($_GET) AND count($_GET) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 {
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500478 foreach ($_GET as $key => $val)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600479 {
480 $_GET[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
481 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 }
483 }
484
Derek Jones69fc4fc2010-03-02 13:36:31 -0600485 // Clean $_POST Data
486 if (is_array($_POST) AND count($_POST) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 {
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500488 foreach ($_POST as $key => $val)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600489 {
490 $_POST[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
491 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 }
493
Derek Jones69fc4fc2010-03-02 13:36:31 -0600494 // Clean $_COOKIE Data
495 if (is_array($_COOKIE) AND count($_COOKIE) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600497 // Also get rid of specially treated cookies that might be set by a server
498 // or silly application, that are of no use to a CI application anyway
499 // but that when present will trip our 'Disallowed Key Characters' alarm
500 // http://www.ietf.org/rfc/rfc2109.txt
501 // note that the key names below are single quoted strings, and are not PHP variables
502 unset($_COOKIE['$Version']);
503 unset($_COOKIE['$Path']);
504 unset($_COOKIE['$Domain']);
505
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500506 foreach ($_COOKIE as $key => $val)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600507 {
508 $_COOKIE[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
509 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 }
511
Derek Jones69fc4fc2010-03-02 13:36:31 -0600512 // Sanitize PHP_SELF
513 $_SERVER['PHP_SELF'] = strip_tags($_SERVER['PHP_SELF']);
514
Derek Jones69fc4fc2010-03-02 13:36:31 -0600515 // CSRF Protection check
516 if ($this->_enable_csrf == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600518 $this->security->csrf_verify();
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000520
Derek Jones69fc4fc2010-03-02 13:36:31 -0600521 log_message('debug', "Global POST and COOKIE data sanitized");
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 }
523
524 // --------------------------------------------------------------------
525
526 /**
Derek Jones69fc4fc2010-03-02 13:36:31 -0600527 * Clean Input Data
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 *
Derek Jones69fc4fc2010-03-02 13:36:31 -0600529 * This is a helper function. It escapes data and
530 * standardizes newline characters to \n
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 * @return string
534 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800535 private function _clean_input_data($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600537 if (is_array($str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600539 $new_array = array();
540 foreach ($str as $key => $val)
541 {
542 $new_array[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
543 }
544 return $new_array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 }
546
Andrey Andreevaf728622011-10-20 10:11:59 +0300547 /* We strip slashes if magic quotes is on to keep things consistent
548
549 NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and
550 it will probably not exist in future versions at all.
551 */
552 if ( ! is_php('5.4') && get_magic_quotes_gpc())
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600554 $str = stripslashes($str);
555 }
556
557 // Clean UTF-8 if supported
558 if (UTF8_ENABLED === TRUE)
559 {
560 $str = $this->uni->clean_string($str);
561 }
David Behler9b5df592011-08-14 21:04:17 +0200562
Pascal Kriete14a0ac62011-04-05 14:55:56 -0400563 // Remove control characters
564 $str = remove_invisible_characters($str);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600565
566 // Should we filter the input data?
567 if ($this->_enable_xss === TRUE)
568 {
569 $str = $this->security->xss_clean($str);
570 }
571
572 // Standardize newlines if needed
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200573 if ($this->_standardize_newlines == TRUE AND strpos($str, "\r") !== FALSE)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600574 {
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200575 return str_replace(array("\r\n", "\r", "\r\n\n"), PHP_EOL, $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 }
577
578 return $str;
579 }
580
581 // --------------------------------------------------------------------
582
583 /**
Derek Jones69fc4fc2010-03-02 13:36:31 -0600584 * Clean Keys
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 *
Derek Jones69fc4fc2010-03-02 13:36:31 -0600586 * This is a helper function. To prevent malicious users
587 * from trying to exploit keys we make sure that keys are
588 * only named with alpha-numeric text and a few other items.
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 * @param string
591 * @return string
592 */
Bo-Yi Wu4db872f2011-09-12 10:52:37 +0800593 private function _clean_input_keys($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 {
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200595 if ( ! preg_match('/^[a-z0-9:_\/-]+$/i', $str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600597 exit('Disallowed Key Characters.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 }
599
Derek Jones69fc4fc2010-03-02 13:36:31 -0600600 // Clean UTF-8 if supported
601 if (UTF8_ENABLED === TRUE)
602 {
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200603 return $this->uni->clean_string($str);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600604 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000605
Derek Jones69fc4fc2010-03-02 13:36:31 -0600606 return $str;
607 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000608
Greg Akerec2f5712010-11-15 16:22:12 -0600609 // --------------------------------------------------------------------
610
611 /**
612 * Request Headers
613 *
David Behler9b5df592011-08-14 21:04:17 +0200614 * In Apache, you can simply call apache_request_headers(), however for
Greg Akerec2f5712010-11-15 16:22:12 -0600615 * people running other webservers the function is undefined.
616 *
David Behlercda768a2011-08-14 23:52:48 +0200617 * @param bool XSS cleaning
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200618 * @return array
Greg Akerec2f5712010-11-15 16:22:12 -0600619 */
620 public function request_headers($xss_clean = FALSE)
621 {
622 // Look at Apache go!
623 if (function_exists('apache_request_headers'))
624 {
625 $headers = apache_request_headers();
626 }
627 else
628 {
629 $headers['Content-Type'] = (isset($_SERVER['CONTENT_TYPE'])) ? $_SERVER['CONTENT_TYPE'] : @getenv('CONTENT_TYPE');
630
631 foreach ($_SERVER as $key => $val)
632 {
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200633 if (strpos($key, 'HTTP_') === 0)
Greg Akerec2f5712010-11-15 16:22:12 -0600634 {
635 $headers[substr($key, 5)] = $this->_fetch_from_array($_SERVER, $key, $xss_clean);
636 }
637 }
638 }
639
640 // take SOME_HEADER and turn it into Some-Header
641 foreach ($headers as $key => $val)
642 {
643 $key = str_replace('_', ' ', strtolower($key));
644 $key = str_replace(' ', '-', ucwords($key));
David Behler9b5df592011-08-14 21:04:17 +0200645
Greg Akerec2f5712010-11-15 16:22:12 -0600646 $this->headers[$key] = $val;
647 }
David Behler9b5df592011-08-14 21:04:17 +0200648
Greg Akerec2f5712010-11-15 16:22:12 -0600649 return $this->headers;
650 }
651
652 // --------------------------------------------------------------------
653
654 /**
655 * Get Request Header
656 *
657 * Returns the value of a single member of the headers class member
658 *
659 * @param string array key for $this->headers
660 * @param boolean XSS Clean or not
661 * @return mixed FALSE on failure, string on success
662 */
663 public function get_request_header($index, $xss_clean = FALSE)
664 {
665 if (empty($this->headers))
666 {
667 $this->request_headers();
668 }
David Behler9b5df592011-08-14 21:04:17 +0200669
Greg Akerec2f5712010-11-15 16:22:12 -0600670 if ( ! isset($this->headers[$index]))
671 {
672 return FALSE;
673 }
674
675 if ($xss_clean === TRUE)
676 {
Pascal Kriete14a0ac62011-04-05 14:55:56 -0400677 return $this->security->xss_clean($this->headers[$index]);
Greg Akerec2f5712010-11-15 16:22:12 -0600678 }
679
David Behler9b5df592011-08-14 21:04:17 +0200680 return $this->headers[$index];
Greg Akerec2f5712010-11-15 16:22:12 -0600681 }
682
Greg Aker081ac9d2010-11-22 14:42:53 -0600683 // --------------------------------------------------------------------
Phil Sturgeonc3828712011-01-19 12:31:47 +0000684
Greg Aker081ac9d2010-11-22 14:42:53 -0600685 /**
686 * Is ajax Request?
687 *
688 * Test to see if a request contains the HTTP_X_REQUESTED_WITH header
689 *
Phil Sturgeonc3828712011-01-19 12:31:47 +0000690 * @return boolean
Greg Aker081ac9d2010-11-22 14:42:53 -0600691 */
692 public function is_ajax_request()
693 {
Greg Aker2fae66e2010-12-09 15:49:34 -0600694 return ($this->server('HTTP_X_REQUESTED_WITH') === 'XMLHttpRequest');
Greg Aker081ac9d2010-11-22 14:42:53 -0600695 }
696
Phil Sturgeonc3828712011-01-19 12:31:47 +0000697 // --------------------------------------------------------------------
698
699 /**
700 * Is cli Request?
701 *
702 * Test to see if a request was made from the command line
703 *
704 * @return boolean
705 */
706 public function is_cli_request()
707 {
Andrey Andreev64e98aa2012-01-07 20:29:10 +0200708 return (php_sapi_name() === 'cli') or defined('STDIN');
Phil Sturgeonc3828712011-01-19 12:31:47 +0000709 }
710
Derek Allard2067d1a2008-11-13 22:59:24 +0000711}
Derek Allard2067d1a2008-11-13 22:59:24 +0000712
713/* End of file Input.php */
Phil Sturgeon33ed0f32011-02-16 19:03:49 +0000714/* Location: ./system/core/Input.php */