blob: 64d6c36004ecb16080228fdbe4ab23f588f27ef7 [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Greg Aker0711dc82011-01-05 10:49:40 -06009 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * Input Class
20 *
21 * Pre-processes global input data for security
22 *
23 * @package CodeIgniter
24 * @subpackage Libraries
25 * @category Input
26 * @author ExpressionEngine Dev Team
27 * @link http://codeigniter.com/user_guide/libraries/input.html
28 */
29class CI_Input {
Derek Allard2067d1a2008-11-13 22:59:24 +000030
David Behler9b5df592011-08-14 21:04:17 +020031 /**
32 * IP address of the current user
33 *
34 * @var string
35 */
Derek Jones69fc4fc2010-03-02 13:36:31 -060036 var $ip_address = FALSE;
David Behler9b5df592011-08-14 21:04:17 +020037 /**
38 * user agent (web browser) being used by the current user
39 *
40 * @var string
41 */
Derek Jones69fc4fc2010-03-02 13:36:31 -060042 var $user_agent = FALSE;
David Behler9b5df592011-08-14 21:04:17 +020043 /**
44 * If FALSE, then $_GET will be set to an empty array
45 *
46 * @var bool
47 */
Dan Horrigan65d603e2010-12-15 08:38:30 -050048 var $_allow_get_array = TRUE;
David Behler9b5df592011-08-14 21:04:17 +020049 /**
50 * If TRUE, then newlines are standardized
51 *
52 * @var bool
53 */
Derek Jones69fc4fc2010-03-02 13:36:31 -060054 var $_standardize_newlines = TRUE;
David Behler9b5df592011-08-14 21:04:17 +020055 /**
56 * Determines whether the XSS filter is always active when GET, POST or COOKIE data is encountered
57 * Set automatically based on config setting
58 *
59 * @var bool
60 */
61 var $_enable_xss = FALSE;
62 /**
63 * Enables a CSRF cookie token to be set.
64 * Set automatically based on config setting
65 *
66 * @var bool
67 */
68 var $_enable_csrf = FALSE;
69 /**
70 * List of all HTTP request headers
71 *
72 * @var array
73 */
Greg Akerec2f5712010-11-15 16:22:12 -060074 protected $headers = array();
David Behler9b5df592011-08-14 21:04:17 +020075
Greg Akerec2f5712010-11-15 16:22:12 -060076
Derek Allard2067d1a2008-11-13 22:59:24 +000077 /**
Greg Akera9263282010-11-10 15:26:43 -060078 * Constructor
79 *
80 * Sets whether to globally enable the XSS processing
81 * and whether to allow the $_GET array
82 *
83 */
84 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000085 {
86 log_message('debug', "Input Class Initialized");
87
Phil Sturgeonc8089152010-12-27 19:06:28 +000088 $this->_allow_get_array = (config_item('allow_get_array') === TRUE);
89 $this->_enable_xss = (config_item('global_xss_filtering') === TRUE);
90 $this->_enable_csrf = (config_item('csrf_protection') === TRUE);
Derek Jones69fc4fc2010-03-02 13:36:31 -060091
Pascal Kriete14a0ac62011-04-05 14:55:56 -040092 global $SEC;
93 $this->security =& $SEC;
Derek Jones69fc4fc2010-03-02 13:36:31 -060094
Pascal Krieteaaec1e42011-01-20 00:01:21 -050095 // Do we need the UTF-8 class?
Derek Jones69fc4fc2010-03-02 13:36:31 -060096 if (UTF8_ENABLED === TRUE)
97 {
98 global $UNI;
99 $this->uni =& $UNI;
100 }
101
102 // Sanitize global arrays
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 $this->_sanitize_globals();
104 }
105
106 // --------------------------------------------------------------------
107
108 /**
Greg Akera9263282010-11-10 15:26:43 -0600109 * Fetch from array
110 *
111 * This is a helper function to retrieve values from global arrays
112 *
113 * @access private
114 * @param array
115 * @param string
116 * @param bool
117 * @return string
118 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 function _fetch_from_array(&$array, $index = '', $xss_clean = FALSE)
120 {
121 if ( ! isset($array[$index]))
122 {
123 return FALSE;
124 }
125
126 if ($xss_clean === TRUE)
127 {
Pascal Kriete14a0ac62011-04-05 14:55:56 -0400128 return $this->security->xss_clean($array[$index]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 }
130
131 return $array[$index];
132 }
133
134 // --------------------------------------------------------------------
135
136 /**
137 * Fetch an item from the GET array
138 *
139 * @access public
140 * @param string
141 * @param bool
142 * @return string
143 */
Phil Sturgeon44f21052011-02-15 21:39:25 +0000144 function get($index = NULL, $xss_clean = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 {
Phil Sturgeon44f21052011-02-15 21:39:25 +0000146 // Check if a field has been provided
147 if ($index === NULL AND ! empty($_GET))
vascopjff1cfa12011-02-13 21:30:19 +0000148 {
Phil Sturgeon44f21052011-02-15 21:39:25 +0000149 $get = array();
vascopjff1cfa12011-02-13 21:30:19 +0000150
151 // loop through the full _GET array
Phil Sturgeon44f21052011-02-15 21:39:25 +0000152 foreach (array_keys($_GET) as $key)
vascopjff1cfa12011-02-13 21:30:19 +0000153 {
Phil Sturgeon44f21052011-02-15 21:39:25 +0000154 $get[$key] = $this->_fetch_from_array($_GET, $key, $xss_clean);
vascopjff1cfa12011-02-13 21:30:19 +0000155 }
Phil Sturgeon44f21052011-02-15 21:39:25 +0000156 return $get;
vascopjff1cfa12011-02-13 21:30:19 +0000157 }
158
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 return $this->_fetch_from_array($_GET, $index, $xss_clean);
160 }
161
162 // --------------------------------------------------------------------
163
164 /**
165 * Fetch an item from the POST array
166 *
167 * @access public
168 * @param string
169 * @param bool
170 * @return string
171 */
Phil Sturgeon44f21052011-02-15 21:39:25 +0000172 function post($index = NULL, $xss_clean = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 {
Phil Sturgeon44f21052011-02-15 21:39:25 +0000174 // Check if a field has been provided
175 if ($index === NULL AND ! empty($_POST))
vascopj0ba58b82011-02-06 14:20:21 +0000176 {
Phil Sturgeon44f21052011-02-15 21:39:25 +0000177 $post = array();
vascopj0ba58b82011-02-06 14:20:21 +0000178
Phil Sturgeon44f21052011-02-15 21:39:25 +0000179 // Loop through the full _POST array and return it
180 foreach (array_keys($_POST) as $key)
vascopj0ba58b82011-02-06 14:20:21 +0000181 {
Phil Sturgeon44f21052011-02-15 21:39:25 +0000182 $post[$key] = $this->_fetch_from_array($_POST, $key, $xss_clean);
vascopj0ba58b82011-02-06 14:20:21 +0000183 }
Phil Sturgeon44f21052011-02-15 21:39:25 +0000184 return $post;
vascopj0ba58b82011-02-06 14:20:21 +0000185 }
David Behler9b5df592011-08-14 21:04:17 +0200186
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 return $this->_fetch_from_array($_POST, $index, $xss_clean);
188 }
189
Derek Jones69fc4fc2010-03-02 13:36:31 -0600190
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 // --------------------------------------------------------------------
192
193 /**
194 * Fetch an item from either the GET array or the POST
195 *
196 * @access public
197 * @param string The index key
198 * @param bool XSS cleaning
199 * @return string
200 */
201 function get_post($index = '', $xss_clean = FALSE)
202 {
203 if ( ! isset($_POST[$index]) )
204 {
205 return $this->get($index, $xss_clean);
206 }
207 else
208 {
209 return $this->post($index, $xss_clean);
210 }
211 }
212
213 // --------------------------------------------------------------------
214
215 /**
216 * Fetch an item from the COOKIE array
217 *
218 * @access public
219 * @param string
220 * @param bool
221 * @return string
222 */
223 function cookie($index = '', $xss_clean = FALSE)
224 {
225 return $this->_fetch_from_array($_COOKIE, $index, $xss_clean);
226 }
227
Derek Jones69fc4fc2010-03-02 13:36:31 -0600228 // ------------------------------------------------------------------------
229
230 /**
231 * Set cookie
232 *
233 * Accepts six parameter, or you can submit an associative
234 * array in the first parameter containing all the values.
235 *
236 * @access public
237 * @param mixed
238 * @param string the value of the cookie
239 * @param string the number of seconds until expiration
Derek Jones37f4b9c2011-07-01 17:56:50 -0500240 * @param string the cookie domain. Usually: .yourdomain.com
Derek Jones69fc4fc2010-03-02 13:36:31 -0600241 * @param string the cookie path
242 * @param string the cookie prefix
Phil Sturgeond8d1e242011-02-16 17:23:16 +0000243 * @param bool true makes the cookie secure
Derek Jones69fc4fc2010-03-02 13:36:31 -0600244 * @return void
245 */
tobiasbg9aa7dc92011-02-18 21:57:13 +0100246 function set_cookie($name = '', $value = '', $expire = '', $domain = '', $path = '/', $prefix = '', $secure = FALSE)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600247 {
248 if (is_array($name))
249 {
tobiasbg9aa7dc92011-02-18 21:57:13 +0100250 // always leave 'name' in last place, as the loop will break otherwise, due to $$item
251 foreach (array('value', 'expire', 'domain', 'path', 'prefix', 'secure', 'name') as $item)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600252 {
253 if (isset($name[$item]))
254 {
255 $$item = $name[$item];
256 }
257 }
258 }
259
260 if ($prefix == '' AND config_item('cookie_prefix') != '')
261 {
262 $prefix = config_item('cookie_prefix');
263 }
264 if ($domain == '' AND config_item('cookie_domain') != '')
265 {
266 $domain = config_item('cookie_domain');
267 }
268 if ($path == '/' AND config_item('cookie_path') != '/')
269 {
270 $path = config_item('cookie_path');
271 }
tobiasbg9aa7dc92011-02-18 21:57:13 +0100272 if ($secure == FALSE AND config_item('cookie_secure') != FALSE)
273 {
274 $secure = config_item('cookie_secure');
275 }
Derek Jones69fc4fc2010-03-02 13:36:31 -0600276
277 if ( ! is_numeric($expire))
278 {
279 $expire = time() - 86500;
280 }
281 else
282 {
Phil Sturgeonc8089152010-12-27 19:06:28 +0000283 $expire = ($expire > 0) ? time() + $expire : 0;
Derek Jones69fc4fc2010-03-02 13:36:31 -0600284 }
285
Phil Sturgeond8d1e242011-02-16 17:23:16 +0000286 setcookie($prefix.$name, $value, $expire, $path, $domain, $secure);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600287 }
288
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 // --------------------------------------------------------------------
290
291 /**
292 * Fetch an item from the SERVER array
293 *
294 * @access public
295 * @param string
296 * @param bool
297 * @return string
298 */
299 function server($index = '', $xss_clean = FALSE)
300 {
301 return $this->_fetch_from_array($_SERVER, $index, $xss_clean);
302 }
303
304 // --------------------------------------------------------------------
305
306 /**
307 * Fetch the IP Address
308 *
309 * @access public
310 * @return string
311 */
312 function ip_address()
313 {
314 if ($this->ip_address !== FALSE)
315 {
316 return $this->ip_address;
317 }
Barry Mienydd671972010-10-04 16:33:58 +0200318
Derek Jones42b2e172009-02-05 16:59:45 +0000319 if (config_item('proxy_ips') != '' && $this->server('HTTP_X_FORWARDED_FOR') && $this->server('REMOTE_ADDR'))
Derek Jonesc5972282009-02-04 21:40:20 +0000320 {
Derek Jones42b2e172009-02-05 16:59:45 +0000321 $proxies = preg_split('/[\s,]/', config_item('proxy_ips'), -1, PREG_SPLIT_NO_EMPTY);
Derek Jonesc5972282009-02-04 21:40:20 +0000322 $proxies = is_array($proxies) ? $proxies : array($proxies);
Derek Allard2067d1a2008-11-13 22:59:24 +0000323
Derek Jonesc5972282009-02-04 21:40:20 +0000324 $this->ip_address = in_array($_SERVER['REMOTE_ADDR'], $proxies) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
325 }
326 elseif ($this->server('REMOTE_ADDR') AND $this->server('HTTP_CLIENT_IP'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 {
328 $this->ip_address = $_SERVER['HTTP_CLIENT_IP'];
329 }
330 elseif ($this->server('REMOTE_ADDR'))
331 {
332 $this->ip_address = $_SERVER['REMOTE_ADDR'];
333 }
334 elseif ($this->server('HTTP_CLIENT_IP'))
335 {
336 $this->ip_address = $_SERVER['HTTP_CLIENT_IP'];
337 }
338 elseif ($this->server('HTTP_X_FORWARDED_FOR'))
339 {
340 $this->ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
341 }
342
343 if ($this->ip_address === FALSE)
344 {
345 $this->ip_address = '0.0.0.0';
346 return $this->ip_address;
347 }
348
Robin Sowell76b369e2010-03-19 11:15:28 -0400349 if (strpos($this->ip_address, ',') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 {
351 $x = explode(',', $this->ip_address);
Derek Jonesc5972282009-02-04 21:40:20 +0000352 $this->ip_address = trim(end($x));
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 }
354
355 if ( ! $this->valid_ip($this->ip_address))
356 {
357 $this->ip_address = '0.0.0.0';
358 }
359
360 return $this->ip_address;
361 }
362
363 // --------------------------------------------------------------------
364
365 /**
366 * Validate IP Address
367 *
368 * Updated version suggested by Geert De Deckere
Barry Mienydd671972010-10-04 16:33:58 +0200369 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 * @access public
371 * @param string
372 * @return string
373 */
374 function valid_ip($ip)
375 {
376 $ip_segments = explode('.', $ip);
377
378 // Always 4 segments needed
379 if (count($ip_segments) != 4)
380 {
381 return FALSE;
382 }
383 // IP can not start with 0
384 if ($ip_segments[0][0] == '0')
385 {
386 return FALSE;
387 }
388 // Check each segment
389 foreach ($ip_segments as $segment)
390 {
Barry Mienydd671972010-10-04 16:33:58 +0200391 // IP segments must be digits and can not be
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 // longer than 3 digits or greater then 255
393 if ($segment == '' OR preg_match("/[^0-9]/", $segment) OR $segment > 255 OR strlen($segment) > 3)
394 {
395 return FALSE;
396 }
397 }
398
399 return TRUE;
400 }
401
402 // --------------------------------------------------------------------
403
404 /**
405 * User Agent
406 *
407 * @access public
408 * @return string
409 */
410 function user_agent()
411 {
412 if ($this->user_agent !== FALSE)
413 {
414 return $this->user_agent;
415 }
416
417 $this->user_agent = ( ! isset($_SERVER['HTTP_USER_AGENT'])) ? FALSE : $_SERVER['HTTP_USER_AGENT'];
418
419 return $this->user_agent;
420 }
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 *
429 * Unsets $_GET data (if query strings are not enabled)
430 *
431 * Unsets all globals if register_globals is enabled
432 *
433 * Standardizes newline characters to \n
434 *
435 * @access private
436 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 */
Derek Jones69fc4fc2010-03-02 13:36:31 -0600438 function _sanitize_globals()
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600440 // It would be "wrong" to unset any of these GLOBALS.
David Behler9b5df592011-08-14 21:04:17 +0200441 $protected = array('_SERVER', '_GET', '_POST', '_FILES', '_REQUEST',
Greg Akerec2f5712010-11-15 16:22:12 -0600442 '_SESSION', '_ENV', 'GLOBALS', 'HTTP_RAW_POST_DATA',
David Behler9b5df592011-08-14 21:04:17 +0200443 'system_folder', 'application_folder', 'BM', 'EXT',
Greg Akerec2f5712010-11-15 16:22:12 -0600444 'CFG', 'URI', 'RTR', 'OUT', 'IN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000445
Barry Mienydd671972010-10-04 16:33:58 +0200446 // Unset globals for securiy.
Derek Jones69fc4fc2010-03-02 13:36:31 -0600447 // This is effectively the same as register_globals = off
448 foreach (array($_GET, $_POST, $_COOKIE) as $global)
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600450 if ( ! is_array($global))
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600452 if ( ! in_array($global, $protected))
453 {
454 global $$global;
455 $$global = NULL;
456 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 }
Derek Jones69fc4fc2010-03-02 13:36:31 -0600458 else
459 {
460 foreach ($global as $key => $val)
461 {
462 if ( ! in_array($key, $protected))
463 {
464 global $$key;
465 $$key = NULL;
466 }
467 }
468 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 }
470
Derek Jones69fc4fc2010-03-02 13:36:31 -0600471 // Is $_GET data allowed? If not we'll set the $_GET to an empty array
472 if ($this->_allow_get_array == FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600474 $_GET = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 }
476 else
477 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600478 if (is_array($_GET) AND count($_GET) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 {
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500480 foreach ($_GET as $key => $val)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600481 {
482 $_GET[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
483 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 }
485 }
486
Derek Jones69fc4fc2010-03-02 13:36:31 -0600487 // Clean $_POST Data
488 if (is_array($_POST) AND count($_POST) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 {
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500490 foreach ($_POST as $key => $val)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600491 {
492 $_POST[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
493 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 }
495
Derek Jones69fc4fc2010-03-02 13:36:31 -0600496 // Clean $_COOKIE Data
497 if (is_array($_COOKIE) AND count($_COOKIE) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600499 // Also get rid of specially treated cookies that might be set by a server
500 // or silly application, that are of no use to a CI application anyway
501 // but that when present will trip our 'Disallowed Key Characters' alarm
502 // http://www.ietf.org/rfc/rfc2109.txt
503 // note that the key names below are single quoted strings, and are not PHP variables
504 unset($_COOKIE['$Version']);
505 unset($_COOKIE['$Path']);
506 unset($_COOKIE['$Domain']);
507
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500508 foreach ($_COOKIE as $key => $val)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600509 {
510 $_COOKIE[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
511 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 }
513
Derek Jones69fc4fc2010-03-02 13:36:31 -0600514 // Sanitize PHP_SELF
515 $_SERVER['PHP_SELF'] = strip_tags($_SERVER['PHP_SELF']);
516
517
518 // CSRF Protection check
519 if ($this->_enable_csrf == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600521 $this->security->csrf_verify();
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000523
Derek Jones69fc4fc2010-03-02 13:36:31 -0600524 log_message('debug', "Global POST and COOKIE data sanitized");
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 }
526
527 // --------------------------------------------------------------------
528
529 /**
Derek Jones69fc4fc2010-03-02 13:36:31 -0600530 * Clean Input Data
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 *
Derek Jones69fc4fc2010-03-02 13:36:31 -0600532 * This is a helper function. It escapes data and
533 * standardizes newline characters to \n
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 *
535 * @access private
536 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 * @return string
538 */
Derek Jones69fc4fc2010-03-02 13:36:31 -0600539 function _clean_input_data($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600541 if (is_array($str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600543 $new_array = array();
544 foreach ($str as $key => $val)
545 {
546 $new_array[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
547 }
548 return $new_array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 }
550
Derek Jones69fc4fc2010-03-02 13:36:31 -0600551 // We strip slashes if magic quotes is on to keep things consistent
Phil Sturgeonfd694892010-12-15 10:52:37 +0000552 if (function_exists('get_magic_quotes_gpc') AND 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
573 if ($this->_standardize_newlines == TRUE)
574 {
575 if (strpos($str, "\r") !== FALSE)
576 {
Phil Sturgeon82f9b152011-03-16 22:18:08 +0000577 $str = str_replace(array("\r\n", "\r", "\r\n\n"), PHP_EOL, $str);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600578 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 }
580
581 return $str;
582 }
583
584 // --------------------------------------------------------------------
585
586 /**
Derek Jones69fc4fc2010-03-02 13:36:31 -0600587 * Clean Keys
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 *
Derek Jones69fc4fc2010-03-02 13:36:31 -0600589 * This is a helper function. To prevent malicious users
590 * from trying to exploit keys we make sure that keys are
591 * only named with alpha-numeric text and a few other items.
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 *
Derek Jones69fc4fc2010-03-02 13:36:31 -0600593 * @access private
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 * @param string
595 * @return string
596 */
Derek Jones69fc4fc2010-03-02 13:36:31 -0600597 function _clean_input_keys($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600599 if ( ! preg_match("/^[a-z0-9:_\/-]+$/i", $str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600601 exit('Disallowed Key Characters.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 }
603
Derek Jones69fc4fc2010-03-02 13:36:31 -0600604 // Clean UTF-8 if supported
605 if (UTF8_ENABLED === TRUE)
606 {
607 $str = $this->uni->clean_string($str);
608 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000609
Derek Jones69fc4fc2010-03-02 13:36:31 -0600610 return $str;
611 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000612
Greg Akerec2f5712010-11-15 16:22:12 -0600613 // --------------------------------------------------------------------
614
615 /**
616 * Request Headers
617 *
David Behler9b5df592011-08-14 21:04:17 +0200618 * In Apache, you can simply call apache_request_headers(), however for
Greg Akerec2f5712010-11-15 16:22:12 -0600619 * people running other webservers the function is undefined.
620 *
621 * @return array
622 */
623 public function request_headers($xss_clean = FALSE)
624 {
625 // Look at Apache go!
626 if (function_exists('apache_request_headers'))
627 {
628 $headers = apache_request_headers();
629 }
630 else
631 {
632 $headers['Content-Type'] = (isset($_SERVER['CONTENT_TYPE'])) ? $_SERVER['CONTENT_TYPE'] : @getenv('CONTENT_TYPE');
633
634 foreach ($_SERVER as $key => $val)
635 {
636 if (strncmp($key, 'HTTP_', 5) === 0)
637 {
638 $headers[substr($key, 5)] = $this->_fetch_from_array($_SERVER, $key, $xss_clean);
639 }
640 }
641 }
642
643 // take SOME_HEADER and turn it into Some-Header
644 foreach ($headers as $key => $val)
645 {
646 $key = str_replace('_', ' ', strtolower($key));
647 $key = str_replace(' ', '-', ucwords($key));
David Behler9b5df592011-08-14 21:04:17 +0200648
Greg Akerec2f5712010-11-15 16:22:12 -0600649 $this->headers[$key] = $val;
650 }
David Behler9b5df592011-08-14 21:04:17 +0200651
Greg Akerec2f5712010-11-15 16:22:12 -0600652 return $this->headers;
653 }
654
655 // --------------------------------------------------------------------
656
657 /**
658 * Get Request Header
659 *
660 * Returns the value of a single member of the headers class member
661 *
662 * @param string array key for $this->headers
663 * @param boolean XSS Clean or not
664 * @return mixed FALSE on failure, string on success
665 */
666 public function get_request_header($index, $xss_clean = FALSE)
667 {
668 if (empty($this->headers))
669 {
670 $this->request_headers();
671 }
David Behler9b5df592011-08-14 21:04:17 +0200672
Greg Akerec2f5712010-11-15 16:22:12 -0600673 if ( ! isset($this->headers[$index]))
674 {
675 return FALSE;
676 }
677
678 if ($xss_clean === TRUE)
679 {
Pascal Kriete14a0ac62011-04-05 14:55:56 -0400680 return $this->security->xss_clean($this->headers[$index]);
Greg Akerec2f5712010-11-15 16:22:12 -0600681 }
682
David Behler9b5df592011-08-14 21:04:17 +0200683 return $this->headers[$index];
Greg Akerec2f5712010-11-15 16:22:12 -0600684 }
685
Greg Aker081ac9d2010-11-22 14:42:53 -0600686 // --------------------------------------------------------------------
Phil Sturgeonc3828712011-01-19 12:31:47 +0000687
Greg Aker081ac9d2010-11-22 14:42:53 -0600688 /**
689 * Is ajax Request?
690 *
691 * Test to see if a request contains the HTTP_X_REQUESTED_WITH header
692 *
Phil Sturgeonc3828712011-01-19 12:31:47 +0000693 * @return boolean
Greg Aker081ac9d2010-11-22 14:42:53 -0600694 */
695 public function is_ajax_request()
696 {
Greg Aker2fae66e2010-12-09 15:49:34 -0600697 return ($this->server('HTTP_X_REQUESTED_WITH') === 'XMLHttpRequest');
Greg Aker081ac9d2010-11-22 14:42:53 -0600698 }
699
Phil Sturgeonc3828712011-01-19 12:31:47 +0000700 // --------------------------------------------------------------------
701
702 /**
703 * Is cli Request?
704 *
705 * Test to see if a request was made from the command line
706 *
707 * @return boolean
708 */
709 public function is_cli_request()
710 {
711 return (bool) defined('STDIN');
712 }
713
Derek Allard2067d1a2008-11-13 22:59:24 +0000714}
715// END Input class
716
717/* End of file Input.php */
Phil Sturgeon33ed0f32011-02-16 19:03:49 +0000718/* Location: ./system/core/Input.php */