blob: 25fe102b5ff8abcfabd2fe157f193aa67a377200 [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
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
Derek Jones69fc4fc2010-03-02 13:36:31 -060031 var $ip_address = FALSE;
32 var $user_agent = FALSE;
Dan Horrigan65d603e2010-12-15 08:38:30 -050033 var $_allow_get_array = TRUE;
Derek Jones69fc4fc2010-03-02 13:36:31 -060034 var $_standardize_newlines = TRUE;
35 var $_enable_xss = FALSE; // Set automatically based on config setting
36 var $_enable_csrf = FALSE; // Set automatically based on config setting
37
Greg Akerec2f5712010-11-15 16:22:12 -060038 protected $headers = array();
39
40
Derek Allard2067d1a2008-11-13 22:59:24 +000041 /**
Greg Akera9263282010-11-10 15:26:43 -060042 * Constructor
43 *
44 * Sets whether to globally enable the XSS processing
45 * and whether to allow the $_GET array
46 *
47 */
48 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000049 {
50 log_message('debug', "Input Class Initialized");
51
Phil Sturgeonc8089152010-12-27 19:06:28 +000052 $this->_allow_get_array = (config_item('allow_get_array') === TRUE);
53 $this->_enable_xss = (config_item('global_xss_filtering') === TRUE);
54 $this->_enable_csrf = (config_item('csrf_protection') === TRUE);
Derek Jones69fc4fc2010-03-02 13:36:31 -060055
56 // Do we need to load the security class?
57 if ($this->_enable_xss == TRUE OR $this->_enable_csrf == TRUE)
58 {
59 $this->security =& load_class('Security');
60 }
61
Pascal Krieteaaec1e42011-01-20 00:01:21 -050062 // Do we need the UTF-8 class?
Derek Jones69fc4fc2010-03-02 13:36:31 -060063 if (UTF8_ENABLED === TRUE)
64 {
65 global $UNI;
66 $this->uni =& $UNI;
67 }
68
69 // Sanitize global arrays
Derek Allard2067d1a2008-11-13 22:59:24 +000070 $this->_sanitize_globals();
71 }
72
73 // --------------------------------------------------------------------
74
75 /**
Greg Akera9263282010-11-10 15:26:43 -060076 * Fetch from array
77 *
78 * This is a helper function to retrieve values from global arrays
79 *
80 * @access private
81 * @param array
82 * @param string
83 * @param bool
84 * @return string
85 */
Derek Allard2067d1a2008-11-13 22:59:24 +000086 function _fetch_from_array(&$array, $index = '', $xss_clean = FALSE)
87 {
88 if ( ! isset($array[$index]))
89 {
90 return FALSE;
91 }
92
93 if ($xss_clean === TRUE)
94 {
Derek Jones69fc4fc2010-03-02 13:36:31 -060095 $_security =& load_class('Security');
96 return $_security->xss_clean($array[$index]);
Derek Allard2067d1a2008-11-13 22:59:24 +000097 }
98
99 return $array[$index];
100 }
101
102 // --------------------------------------------------------------------
103
104 /**
105 * Fetch an item from the GET array
106 *
107 * @access public
108 * @param string
109 * @param bool
110 * @return string
111 */
Phil Sturgeon44f21052011-02-15 21:39:25 +0000112 function get($index = NULL, $xss_clean = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 {
Phil Sturgeon44f21052011-02-15 21:39:25 +0000114 // Check if a field has been provided
115 if ($index === NULL AND ! empty($_GET))
vascopjff1cfa12011-02-13 21:30:19 +0000116 {
Phil Sturgeon44f21052011-02-15 21:39:25 +0000117 $get = array();
vascopjff1cfa12011-02-13 21:30:19 +0000118
119 // loop through the full _GET array
Phil Sturgeon44f21052011-02-15 21:39:25 +0000120 foreach (array_keys($_GET) as $key)
vascopjff1cfa12011-02-13 21:30:19 +0000121 {
Phil Sturgeon44f21052011-02-15 21:39:25 +0000122 $get[$key] = $this->_fetch_from_array($_GET, $key, $xss_clean);
vascopjff1cfa12011-02-13 21:30:19 +0000123 }
Phil Sturgeon44f21052011-02-15 21:39:25 +0000124 return $get;
vascopjff1cfa12011-02-13 21:30:19 +0000125 }
126
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 return $this->_fetch_from_array($_GET, $index, $xss_clean);
128 }
129
130 // --------------------------------------------------------------------
131
132 /**
133 * Fetch an item from the POST array
134 *
135 * @access public
136 * @param string
137 * @param bool
138 * @return string
139 */
Phil Sturgeon44f21052011-02-15 21:39:25 +0000140 function post($index = NULL, $xss_clean = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 {
Phil Sturgeon44f21052011-02-15 21:39:25 +0000142 // Check if a field has been provided
143 if ($index === NULL AND ! empty($_POST))
vascopj0ba58b82011-02-06 14:20:21 +0000144 {
Phil Sturgeon44f21052011-02-15 21:39:25 +0000145 $post = array();
vascopj0ba58b82011-02-06 14:20:21 +0000146
Phil Sturgeon44f21052011-02-15 21:39:25 +0000147 // Loop through the full _POST array and return it
148 foreach (array_keys($_POST) as $key)
vascopj0ba58b82011-02-06 14:20:21 +0000149 {
Phil Sturgeon44f21052011-02-15 21:39:25 +0000150 $post[$key] = $this->_fetch_from_array($_POST, $key, $xss_clean);
vascopj0ba58b82011-02-06 14:20:21 +0000151 }
Phil Sturgeon44f21052011-02-15 21:39:25 +0000152 return $post;
vascopj0ba58b82011-02-06 14:20:21 +0000153 }
154
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 return $this->_fetch_from_array($_POST, $index, $xss_clean);
156 }
157
Derek Jones69fc4fc2010-03-02 13:36:31 -0600158
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 // --------------------------------------------------------------------
160
161 /**
162 * Fetch an item from either the GET array or the POST
163 *
164 * @access public
165 * @param string The index key
166 * @param bool XSS cleaning
167 * @return string
168 */
169 function get_post($index = '', $xss_clean = FALSE)
170 {
171 if ( ! isset($_POST[$index]) )
172 {
173 return $this->get($index, $xss_clean);
174 }
175 else
176 {
177 return $this->post($index, $xss_clean);
178 }
179 }
180
181 // --------------------------------------------------------------------
182
183 /**
184 * Fetch an item from the COOKIE array
185 *
186 * @access public
187 * @param string
188 * @param bool
189 * @return string
190 */
191 function cookie($index = '', $xss_clean = FALSE)
192 {
193 return $this->_fetch_from_array($_COOKIE, $index, $xss_clean);
194 }
195
Derek Jones69fc4fc2010-03-02 13:36:31 -0600196 // ------------------------------------------------------------------------
197
198 /**
199 * Set cookie
200 *
201 * Accepts six parameter, or you can submit an associative
202 * array in the first parameter containing all the values.
203 *
204 * @access public
205 * @param mixed
206 * @param string the value of the cookie
207 * @param string the number of seconds until expiration
208 * @param string the cookie domain. Usually: .yourdomain.com
209 * @param string the cookie path
210 * @param string the cookie prefix
Phil Sturgeond8d1e242011-02-16 17:23:16 +0000211 * @param bool true makes the cookie secure
Derek Jones69fc4fc2010-03-02 13:36:31 -0600212 * @return void
213 */
Phil Sturgeon33ed0f32011-02-16 19:03:49 +0000214 function set_cookie($name = '', $value = '', $expire = '', $domain = '', $path = '/', $prefix = '', $secure = NULL)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600215 {
216 if (is_array($name))
217 {
Phil Sturgeond8d1e242011-02-16 17:23:16 +0000218 foreach (array('value', 'expire', 'domain', 'path', 'prefix', 'name', 'secure') as $item)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600219 {
220 if (isset($name[$item]))
221 {
222 $$item = $name[$item];
223 }
224 }
225 }
226
227 if ($prefix == '' AND config_item('cookie_prefix') != '')
228 {
229 $prefix = config_item('cookie_prefix');
230 }
231 if ($domain == '' AND config_item('cookie_domain') != '')
232 {
233 $domain = config_item('cookie_domain');
234 }
235 if ($path == '/' AND config_item('cookie_path') != '/')
236 {
237 $path = config_item('cookie_path');
238 }
239
240 if ( ! is_numeric($expire))
241 {
242 $expire = time() - 86500;
243 }
244 else
245 {
Phil Sturgeonc8089152010-12-27 19:06:28 +0000246 $expire = ($expire > 0) ? time() + $expire : 0;
Derek Jones69fc4fc2010-03-02 13:36:31 -0600247 }
248
Phil Sturgeon33ed0f32011-02-16 19:03:49 +0000249 // If TRUE/FALSE is not provided, use the config
250 if ( ! is_bool($secure))
251 {
252 $secure = (bool) (config_item('cookie_secure') === TRUE);
253 }
254
Phil Sturgeond8d1e242011-02-16 17:23:16 +0000255 setcookie($prefix.$name, $value, $expire, $path, $domain, $secure);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600256 }
257
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 // --------------------------------------------------------------------
259
260 /**
261 * Fetch an item from the SERVER array
262 *
263 * @access public
264 * @param string
265 * @param bool
266 * @return string
267 */
268 function server($index = '', $xss_clean = FALSE)
269 {
270 return $this->_fetch_from_array($_SERVER, $index, $xss_clean);
271 }
272
273 // --------------------------------------------------------------------
274
275 /**
276 * Fetch the IP Address
277 *
278 * @access public
279 * @return string
280 */
281 function ip_address()
282 {
283 if ($this->ip_address !== FALSE)
284 {
285 return $this->ip_address;
286 }
Barry Mienydd671972010-10-04 16:33:58 +0200287
Derek Jones42b2e172009-02-05 16:59:45 +0000288 if (config_item('proxy_ips') != '' && $this->server('HTTP_X_FORWARDED_FOR') && $this->server('REMOTE_ADDR'))
Derek Jonesc5972282009-02-04 21:40:20 +0000289 {
Derek Jones42b2e172009-02-05 16:59:45 +0000290 $proxies = preg_split('/[\s,]/', config_item('proxy_ips'), -1, PREG_SPLIT_NO_EMPTY);
Derek Jonesc5972282009-02-04 21:40:20 +0000291 $proxies = is_array($proxies) ? $proxies : array($proxies);
Derek Allard2067d1a2008-11-13 22:59:24 +0000292
Derek Jonesc5972282009-02-04 21:40:20 +0000293 $this->ip_address = in_array($_SERVER['REMOTE_ADDR'], $proxies) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
294 }
295 elseif ($this->server('REMOTE_ADDR') AND $this->server('HTTP_CLIENT_IP'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 {
297 $this->ip_address = $_SERVER['HTTP_CLIENT_IP'];
298 }
299 elseif ($this->server('REMOTE_ADDR'))
300 {
301 $this->ip_address = $_SERVER['REMOTE_ADDR'];
302 }
303 elseif ($this->server('HTTP_CLIENT_IP'))
304 {
305 $this->ip_address = $_SERVER['HTTP_CLIENT_IP'];
306 }
307 elseif ($this->server('HTTP_X_FORWARDED_FOR'))
308 {
309 $this->ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
310 }
311
312 if ($this->ip_address === FALSE)
313 {
314 $this->ip_address = '0.0.0.0';
315 return $this->ip_address;
316 }
317
Robin Sowell76b369e2010-03-19 11:15:28 -0400318 if (strpos($this->ip_address, ',') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 {
320 $x = explode(',', $this->ip_address);
Derek Jonesc5972282009-02-04 21:40:20 +0000321 $this->ip_address = trim(end($x));
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 }
323
324 if ( ! $this->valid_ip($this->ip_address))
325 {
326 $this->ip_address = '0.0.0.0';
327 }
328
329 return $this->ip_address;
330 }
331
332 // --------------------------------------------------------------------
333
334 /**
335 * Validate IP Address
336 *
337 * Updated version suggested by Geert De Deckere
Barry Mienydd671972010-10-04 16:33:58 +0200338 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 * @access public
340 * @param string
341 * @return string
342 */
343 function valid_ip($ip)
344 {
345 $ip_segments = explode('.', $ip);
346
347 // Always 4 segments needed
348 if (count($ip_segments) != 4)
349 {
350 return FALSE;
351 }
352 // IP can not start with 0
353 if ($ip_segments[0][0] == '0')
354 {
355 return FALSE;
356 }
357 // Check each segment
358 foreach ($ip_segments as $segment)
359 {
Barry Mienydd671972010-10-04 16:33:58 +0200360 // IP segments must be digits and can not be
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 // longer than 3 digits or greater then 255
362 if ($segment == '' OR preg_match("/[^0-9]/", $segment) OR $segment > 255 OR strlen($segment) > 3)
363 {
364 return FALSE;
365 }
366 }
367
368 return TRUE;
369 }
370
371 // --------------------------------------------------------------------
372
373 /**
374 * User Agent
375 *
376 * @access public
377 * @return string
378 */
379 function user_agent()
380 {
381 if ($this->user_agent !== FALSE)
382 {
383 return $this->user_agent;
384 }
385
386 $this->user_agent = ( ! isset($_SERVER['HTTP_USER_AGENT'])) ? FALSE : $_SERVER['HTTP_USER_AGENT'];
387
388 return $this->user_agent;
389 }
390
391 // --------------------------------------------------------------------
392
393 /**
Derek Jones69fc4fc2010-03-02 13:36:31 -0600394 * Sanitize Globals
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 *
Derek Jones69fc4fc2010-03-02 13:36:31 -0600396 * This function does the following:
397 *
398 * Unsets $_GET data (if query strings are not enabled)
399 *
400 * Unsets all globals if register_globals is enabled
401 *
402 * Standardizes newline characters to \n
403 *
404 * @access private
405 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 */
Derek Jones69fc4fc2010-03-02 13:36:31 -0600407 function _sanitize_globals()
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600409 // It would be "wrong" to unset any of these GLOBALS.
Greg Akerec2f5712010-11-15 16:22:12 -0600410 $protected = array('_SERVER', '_GET', '_POST', '_FILES', '_REQUEST',
411 '_SESSION', '_ENV', 'GLOBALS', 'HTTP_RAW_POST_DATA',
412 'system_folder', 'application_folder', 'BM', 'EXT',
413 'CFG', 'URI', 'RTR', 'OUT', 'IN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000414
Barry Mienydd671972010-10-04 16:33:58 +0200415 // Unset globals for securiy.
Derek Jones69fc4fc2010-03-02 13:36:31 -0600416 // This is effectively the same as register_globals = off
417 foreach (array($_GET, $_POST, $_COOKIE) as $global)
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600419 if ( ! is_array($global))
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600421 if ( ! in_array($global, $protected))
422 {
423 global $$global;
424 $$global = NULL;
425 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 }
Derek Jones69fc4fc2010-03-02 13:36:31 -0600427 else
428 {
429 foreach ($global as $key => $val)
430 {
431 if ( ! in_array($key, $protected))
432 {
433 global $$key;
434 $$key = NULL;
435 }
436 }
437 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 }
439
Derek Jones69fc4fc2010-03-02 13:36:31 -0600440 // Is $_GET data allowed? If not we'll set the $_GET to an empty array
441 if ($this->_allow_get_array == FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600443 $_GET = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 }
445 else
446 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600447 if (is_array($_GET) AND count($_GET) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 {
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500449 foreach ($_GET as $key => $val)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600450 {
451 $_GET[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
452 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 }
454 }
455
Derek Jones69fc4fc2010-03-02 13:36:31 -0600456 // Clean $_POST Data
457 if (is_array($_POST) AND count($_POST) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 {
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500459 foreach ($_POST as $key => $val)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600460 {
461 $_POST[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
462 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 }
464
Derek Jones69fc4fc2010-03-02 13:36:31 -0600465 // Clean $_COOKIE Data
466 if (is_array($_COOKIE) AND count($_COOKIE) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600468 // Also get rid of specially treated cookies that might be set by a server
469 // or silly application, that are of no use to a CI application anyway
470 // but that when present will trip our 'Disallowed Key Characters' alarm
471 // http://www.ietf.org/rfc/rfc2109.txt
472 // note that the key names below are single quoted strings, and are not PHP variables
473 unset($_COOKIE['$Version']);
474 unset($_COOKIE['$Path']);
475 unset($_COOKIE['$Domain']);
476
Pascal Kriete5d5895f2011-02-14 13:27:07 -0500477 foreach ($_COOKIE as $key => $val)
Derek Jones69fc4fc2010-03-02 13:36:31 -0600478 {
479 $_COOKIE[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
480 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 }
482
Derek Jones69fc4fc2010-03-02 13:36:31 -0600483 // Sanitize PHP_SELF
484 $_SERVER['PHP_SELF'] = strip_tags($_SERVER['PHP_SELF']);
485
486
487 // CSRF Protection check
488 if ($this->_enable_csrf == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600490 $this->security->csrf_verify();
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000492
Derek Jones69fc4fc2010-03-02 13:36:31 -0600493 log_message('debug', "Global POST and COOKIE data sanitized");
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 }
495
496 // --------------------------------------------------------------------
497
498 /**
Derek Jones69fc4fc2010-03-02 13:36:31 -0600499 * Clean Input Data
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 *
Derek Jones69fc4fc2010-03-02 13:36:31 -0600501 * This is a helper function. It escapes data and
502 * standardizes newline characters to \n
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 *
504 * @access private
505 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 * @return string
507 */
Derek Jones69fc4fc2010-03-02 13:36:31 -0600508 function _clean_input_data($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600510 if (is_array($str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600512 $new_array = array();
513 foreach ($str as $key => $val)
514 {
515 $new_array[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
516 }
517 return $new_array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 }
519
Derek Jones69fc4fc2010-03-02 13:36:31 -0600520 // We strip slashes if magic quotes is on to keep things consistent
Phil Sturgeonfd694892010-12-15 10:52:37 +0000521 if (function_exists('get_magic_quotes_gpc') AND get_magic_quotes_gpc())
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600523 $str = stripslashes($str);
524 }
525
526 // Clean UTF-8 if supported
527 if (UTF8_ENABLED === TRUE)
528 {
529 $str = $this->uni->clean_string($str);
530 }
531
532 // Should we filter the input data?
533 if ($this->_enable_xss === TRUE)
534 {
535 $str = $this->security->xss_clean($str);
536 }
537
538 // Standardize newlines if needed
539 if ($this->_standardize_newlines == TRUE)
540 {
541 if (strpos($str, "\r") !== FALSE)
542 {
Phil Sturgeonc8089152010-12-27 19:06:28 +0000543 $str = str_replace(array("\r\n", "\r"), PHP_EOL, $str);
Derek Jones69fc4fc2010-03-02 13:36:31 -0600544 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 }
546
547 return $str;
548 }
549
550 // --------------------------------------------------------------------
551
552 /**
Derek Jones69fc4fc2010-03-02 13:36:31 -0600553 * Clean Keys
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 *
Derek Jones69fc4fc2010-03-02 13:36:31 -0600555 * This is a helper function. To prevent malicious users
556 * from trying to exploit keys we make sure that keys are
557 * only named with alpha-numeric text and a few other items.
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 *
Derek Jones69fc4fc2010-03-02 13:36:31 -0600559 * @access private
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 * @param string
561 * @return string
562 */
Derek Jones69fc4fc2010-03-02 13:36:31 -0600563 function _clean_input_keys($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600565 if ( ! preg_match("/^[a-z0-9:_\/-]+$/i", $str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600567 exit('Disallowed Key Characters.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 }
569
Derek Jones69fc4fc2010-03-02 13:36:31 -0600570 // Clean UTF-8 if supported
571 if (UTF8_ENABLED === TRUE)
572 {
573 $str = $this->uni->clean_string($str);
574 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000575
Derek Jones69fc4fc2010-03-02 13:36:31 -0600576 return $str;
577 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000578
Greg Akerec2f5712010-11-15 16:22:12 -0600579 // --------------------------------------------------------------------
580
581 /**
582 * Request Headers
583 *
584 * In Apache, you can simply call apache_request_headers(), however for
585 * people running other webservers the function is undefined.
586 *
587 * @return array
588 */
589 public function request_headers($xss_clean = FALSE)
590 {
591 // Look at Apache go!
592 if (function_exists('apache_request_headers'))
593 {
594 $headers = apache_request_headers();
595 }
596 else
597 {
598 $headers['Content-Type'] = (isset($_SERVER['CONTENT_TYPE'])) ? $_SERVER['CONTENT_TYPE'] : @getenv('CONTENT_TYPE');
599
600 foreach ($_SERVER as $key => $val)
601 {
602 if (strncmp($key, 'HTTP_', 5) === 0)
603 {
604 $headers[substr($key, 5)] = $this->_fetch_from_array($_SERVER, $key, $xss_clean);
605 }
606 }
607 }
608
609 // take SOME_HEADER and turn it into Some-Header
610 foreach ($headers as $key => $val)
611 {
612 $key = str_replace('_', ' ', strtolower($key));
613 $key = str_replace(' ', '-', ucwords($key));
614
615 $this->headers[$key] = $val;
616 }
617
618 return $this->headers;
619 }
620
621 // --------------------------------------------------------------------
622
623 /**
624 * Get Request Header
625 *
626 * Returns the value of a single member of the headers class member
627 *
628 * @param string array key for $this->headers
629 * @param boolean XSS Clean or not
630 * @return mixed FALSE on failure, string on success
631 */
632 public function get_request_header($index, $xss_clean = FALSE)
633 {
634 if (empty($this->headers))
635 {
636 $this->request_headers();
637 }
638
639 if ( ! isset($this->headers[$index]))
640 {
641 return FALSE;
642 }
643
644 if ($xss_clean === TRUE)
645 {
646 $_security =& load_class('Security');
647 return $_security->xss_clean($this->headers[$index]);
648 }
649
650 return $this->headers[$index];
651 }
652
Greg Aker081ac9d2010-11-22 14:42:53 -0600653 // --------------------------------------------------------------------
Phil Sturgeonc3828712011-01-19 12:31:47 +0000654
Greg Aker081ac9d2010-11-22 14:42:53 -0600655 /**
656 * Is ajax Request?
657 *
658 * Test to see if a request contains the HTTP_X_REQUESTED_WITH header
659 *
Phil Sturgeonc3828712011-01-19 12:31:47 +0000660 * @return boolean
Greg Aker081ac9d2010-11-22 14:42:53 -0600661 */
662 public function is_ajax_request()
663 {
Greg Aker2fae66e2010-12-09 15:49:34 -0600664 return ($this->server('HTTP_X_REQUESTED_WITH') === 'XMLHttpRequest');
Greg Aker081ac9d2010-11-22 14:42:53 -0600665 }
666
Phil Sturgeonc3828712011-01-19 12:31:47 +0000667 // --------------------------------------------------------------------
668
669 /**
670 * Is cli Request?
671 *
672 * Test to see if a request was made from the command line
673 *
674 * @return boolean
675 */
676 public function is_cli_request()
677 {
678 return (bool) defined('STDIN');
679 }
680
Derek Allard2067d1a2008-11-13 22:59:24 +0000681}
682// END Input class
683
684/* End of file Input.php */
Phil Sturgeon33ed0f32011-02-16 19:03:49 +0000685/* Location: ./system/core/Input.php */