blob: 0ce2d893ad4bcc716401a905b4b07447834747bc [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
Derek Jones7f3719f2010-01-05 13:35:37 +00009 * @copyright Copyright (c) 2008 - 2010, 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;
33 var $_allow_get_array = FALSE;
34 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
Derek Allard2067d1a2008-11-13 22:59:24 +000038 /**
Greg Akera9263282010-11-10 15:26:43 -060039 * Constructor
40 *
41 * Sets whether to globally enable the XSS processing
42 * and whether to allow the $_GET array
43 *
44 */
45 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000046 {
47 log_message('debug', "Input Class Initialized");
48
Derek Jones69fc4fc2010-03-02 13:36:31 -060049 $this->_allow_get_array = (config_item('enable_query_strings') === TRUE) ? TRUE : FALSE;
50 $this->_enable_xss = (config_item('global_xss_filtering') === TRUE) ? TRUE : FALSE;
51 $this->_enable_csrf = (config_item('csrf_protection') === TRUE) ? TRUE : FALSE;
52
53 // Do we need to load the security class?
54 if ($this->_enable_xss == TRUE OR $this->_enable_csrf == TRUE)
55 {
56 $this->security =& load_class('Security');
57 }
58
59 // Do we need the Unicode class?
60 if (UTF8_ENABLED === TRUE)
61 {
62 global $UNI;
63 $this->uni =& $UNI;
64 }
65
66 // Sanitize global arrays
Derek Allard2067d1a2008-11-13 22:59:24 +000067 $this->_sanitize_globals();
68 }
69
70 // --------------------------------------------------------------------
71
72 /**
Greg Akera9263282010-11-10 15:26:43 -060073 * Fetch from array
74 *
75 * This is a helper function to retrieve values from global arrays
76 *
77 * @access private
78 * @param array
79 * @param string
80 * @param bool
81 * @return string
82 */
Derek Allard2067d1a2008-11-13 22:59:24 +000083 function _fetch_from_array(&$array, $index = '', $xss_clean = FALSE)
84 {
85 if ( ! isset($array[$index]))
86 {
87 return FALSE;
88 }
89
90 if ($xss_clean === TRUE)
91 {
Derek Jones69fc4fc2010-03-02 13:36:31 -060092 $_security =& load_class('Security');
93 return $_security->xss_clean($array[$index]);
Derek Allard2067d1a2008-11-13 22:59:24 +000094 }
95
96 return $array[$index];
97 }
98
99 // --------------------------------------------------------------------
100
101 /**
102 * Fetch an item from the GET array
103 *
104 * @access public
105 * @param string
106 * @param bool
107 * @return string
108 */
109 function get($index = '', $xss_clean = FALSE)
110 {
111 return $this->_fetch_from_array($_GET, $index, $xss_clean);
112 }
113
114 // --------------------------------------------------------------------
115
116 /**
117 * Fetch an item from the POST array
118 *
119 * @access public
120 * @param string
121 * @param bool
122 * @return string
123 */
124 function post($index = '', $xss_clean = FALSE)
125 {
126 return $this->_fetch_from_array($_POST, $index, $xss_clean);
127 }
128
Derek Jones69fc4fc2010-03-02 13:36:31 -0600129
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 // --------------------------------------------------------------------
131
132 /**
133 * Fetch an item from either the GET array or the POST
134 *
135 * @access public
136 * @param string The index key
137 * @param bool XSS cleaning
138 * @return string
139 */
140 function get_post($index = '', $xss_clean = FALSE)
141 {
142 if ( ! isset($_POST[$index]) )
143 {
144 return $this->get($index, $xss_clean);
145 }
146 else
147 {
148 return $this->post($index, $xss_clean);
149 }
150 }
151
152 // --------------------------------------------------------------------
153
154 /**
155 * Fetch an item from the COOKIE array
156 *
157 * @access public
158 * @param string
159 * @param bool
160 * @return string
161 */
162 function cookie($index = '', $xss_clean = FALSE)
163 {
164 return $this->_fetch_from_array($_COOKIE, $index, $xss_clean);
165 }
166
Derek Jones69fc4fc2010-03-02 13:36:31 -0600167 // ------------------------------------------------------------------------
168
169 /**
170 * Set cookie
171 *
172 * Accepts six parameter, or you can submit an associative
173 * array in the first parameter containing all the values.
174 *
175 * @access public
176 * @param mixed
177 * @param string the value of the cookie
178 * @param string the number of seconds until expiration
179 * @param string the cookie domain. Usually: .yourdomain.com
180 * @param string the cookie path
181 * @param string the cookie prefix
182 * @return void
183 */
184 function set_cookie($name = '', $value = '', $expire = '', $domain = '', $path = '/', $prefix = '')
185 {
186 if (is_array($name))
187 {
188 foreach (array('value', 'expire', 'domain', 'path', 'prefix', 'name') as $item)
189 {
190 if (isset($name[$item]))
191 {
192 $$item = $name[$item];
193 }
194 }
195 }
196
197 if ($prefix == '' AND config_item('cookie_prefix') != '')
198 {
199 $prefix = config_item('cookie_prefix');
200 }
201 if ($domain == '' AND config_item('cookie_domain') != '')
202 {
203 $domain = config_item('cookie_domain');
204 }
205 if ($path == '/' AND config_item('cookie_path') != '/')
206 {
207 $path = config_item('cookie_path');
208 }
209
210 if ( ! is_numeric($expire))
211 {
212 $expire = time() - 86500;
213 }
214 else
215 {
216 if ($expire > 0)
217 {
218 $expire = time() + $expire;
219 }
220 else
221 {
222 $expire = 0;
223 }
224 }
225
226 setcookie($prefix.$name, $value, $expire, $path, $domain, 0);
227 }
228
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 // --------------------------------------------------------------------
230
231 /**
232 * Fetch an item from the SERVER array
233 *
234 * @access public
235 * @param string
236 * @param bool
237 * @return string
238 */
239 function server($index = '', $xss_clean = FALSE)
240 {
241 return $this->_fetch_from_array($_SERVER, $index, $xss_clean);
242 }
243
244 // --------------------------------------------------------------------
245
246 /**
247 * Fetch the IP Address
248 *
249 * @access public
250 * @return string
251 */
252 function ip_address()
253 {
254 if ($this->ip_address !== FALSE)
255 {
256 return $this->ip_address;
257 }
Barry Mienydd671972010-10-04 16:33:58 +0200258
Derek Jones42b2e172009-02-05 16:59:45 +0000259 if (config_item('proxy_ips') != '' && $this->server('HTTP_X_FORWARDED_FOR') && $this->server('REMOTE_ADDR'))
Derek Jonesc5972282009-02-04 21:40:20 +0000260 {
Derek Jones42b2e172009-02-05 16:59:45 +0000261 $proxies = preg_split('/[\s,]/', config_item('proxy_ips'), -1, PREG_SPLIT_NO_EMPTY);
Derek Jonesc5972282009-02-04 21:40:20 +0000262 $proxies = is_array($proxies) ? $proxies : array($proxies);
Derek Allard2067d1a2008-11-13 22:59:24 +0000263
Derek Jonesc5972282009-02-04 21:40:20 +0000264 $this->ip_address = in_array($_SERVER['REMOTE_ADDR'], $proxies) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
265 }
266 elseif ($this->server('REMOTE_ADDR') AND $this->server('HTTP_CLIENT_IP'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 {
268 $this->ip_address = $_SERVER['HTTP_CLIENT_IP'];
269 }
270 elseif ($this->server('REMOTE_ADDR'))
271 {
272 $this->ip_address = $_SERVER['REMOTE_ADDR'];
273 }
274 elseif ($this->server('HTTP_CLIENT_IP'))
275 {
276 $this->ip_address = $_SERVER['HTTP_CLIENT_IP'];
277 }
278 elseif ($this->server('HTTP_X_FORWARDED_FOR'))
279 {
280 $this->ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
281 }
282
283 if ($this->ip_address === FALSE)
284 {
285 $this->ip_address = '0.0.0.0';
286 return $this->ip_address;
287 }
288
Robin Sowell76b369e2010-03-19 11:15:28 -0400289 if (strpos($this->ip_address, ',') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 {
291 $x = explode(',', $this->ip_address);
Derek Jonesc5972282009-02-04 21:40:20 +0000292 $this->ip_address = trim(end($x));
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 }
294
295 if ( ! $this->valid_ip($this->ip_address))
296 {
297 $this->ip_address = '0.0.0.0';
298 }
299
300 return $this->ip_address;
301 }
302
303 // --------------------------------------------------------------------
304
305 /**
306 * Validate IP Address
307 *
308 * Updated version suggested by Geert De Deckere
Barry Mienydd671972010-10-04 16:33:58 +0200309 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 * @access public
311 * @param string
312 * @return string
313 */
314 function valid_ip($ip)
315 {
316 $ip_segments = explode('.', $ip);
317
318 // Always 4 segments needed
319 if (count($ip_segments) != 4)
320 {
321 return FALSE;
322 }
323 // IP can not start with 0
324 if ($ip_segments[0][0] == '0')
325 {
326 return FALSE;
327 }
328 // Check each segment
329 foreach ($ip_segments as $segment)
330 {
Barry Mienydd671972010-10-04 16:33:58 +0200331 // IP segments must be digits and can not be
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 // longer than 3 digits or greater then 255
333 if ($segment == '' OR preg_match("/[^0-9]/", $segment) OR $segment > 255 OR strlen($segment) > 3)
334 {
335 return FALSE;
336 }
337 }
338
339 return TRUE;
340 }
341
342 // --------------------------------------------------------------------
343
344 /**
345 * User Agent
346 *
347 * @access public
348 * @return string
349 */
350 function user_agent()
351 {
352 if ($this->user_agent !== FALSE)
353 {
354 return $this->user_agent;
355 }
356
357 $this->user_agent = ( ! isset($_SERVER['HTTP_USER_AGENT'])) ? FALSE : $_SERVER['HTTP_USER_AGENT'];
358
359 return $this->user_agent;
360 }
361
362 // --------------------------------------------------------------------
363
364 /**
Derek Jones69fc4fc2010-03-02 13:36:31 -0600365 * Sanitize Globals
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 *
Derek Jones69fc4fc2010-03-02 13:36:31 -0600367 * This function does the following:
368 *
369 * Unsets $_GET data (if query strings are not enabled)
370 *
371 * Unsets all globals if register_globals is enabled
372 *
373 * Standardizes newline characters to \n
374 *
375 * @access private
376 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 */
Derek Jones69fc4fc2010-03-02 13:36:31 -0600378 function _sanitize_globals()
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600380 // It would be "wrong" to unset any of these GLOBALS.
381 $protected = array('_SERVER', '_GET', '_POST', '_FILES', '_REQUEST', '_SESSION', '_ENV', 'GLOBALS', 'HTTP_RAW_POST_DATA',
382 'system_folder', 'application_folder', 'BM', 'EXT', 'CFG', 'URI', 'RTR', 'OUT', 'IN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000383
Barry Mienydd671972010-10-04 16:33:58 +0200384 // Unset globals for securiy.
Derek Jones69fc4fc2010-03-02 13:36:31 -0600385 // This is effectively the same as register_globals = off
386 foreach (array($_GET, $_POST, $_COOKIE) as $global)
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600388 if ( ! is_array($global))
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600390 if ( ! in_array($global, $protected))
391 {
392 global $$global;
393 $$global = NULL;
394 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 }
Derek Jones69fc4fc2010-03-02 13:36:31 -0600396 else
397 {
398 foreach ($global as $key => $val)
399 {
400 if ( ! in_array($key, $protected))
401 {
402 global $$key;
403 $$key = NULL;
404 }
405 }
406 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 }
408
Derek Jones69fc4fc2010-03-02 13:36:31 -0600409 // Is $_GET data allowed? If not we'll set the $_GET to an empty array
410 if ($this->_allow_get_array == FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600412 $_GET = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 }
414 else
415 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600416 if (is_array($_GET) AND count($_GET) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600418 foreach($_GET as $key => $val)
419 {
420 $_GET[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
421 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 }
423 }
424
Derek Jones69fc4fc2010-03-02 13:36:31 -0600425 // Clean $_POST Data
426 if (is_array($_POST) AND count($_POST) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600428 foreach($_POST as $key => $val)
429 {
430 $_POST[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
431 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 }
433
Derek Jones69fc4fc2010-03-02 13:36:31 -0600434 // Clean $_COOKIE Data
435 if (is_array($_COOKIE) AND count($_COOKIE) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600437 // Also get rid of specially treated cookies that might be set by a server
438 // or silly application, that are of no use to a CI application anyway
439 // but that when present will trip our 'Disallowed Key Characters' alarm
440 // http://www.ietf.org/rfc/rfc2109.txt
441 // note that the key names below are single quoted strings, and are not PHP variables
442 unset($_COOKIE['$Version']);
443 unset($_COOKIE['$Path']);
444 unset($_COOKIE['$Domain']);
445
446 foreach($_COOKIE as $key => $val)
447 {
448 $_COOKIE[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
449 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 }
451
Derek Jones69fc4fc2010-03-02 13:36:31 -0600452 // Sanitize PHP_SELF
453 $_SERVER['PHP_SELF'] = strip_tags($_SERVER['PHP_SELF']);
454
455
456 // CSRF Protection check
457 if ($this->_enable_csrf == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600459 $this->security->csrf_verify();
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000461
Derek Jones69fc4fc2010-03-02 13:36:31 -0600462 log_message('debug', "Global POST and COOKIE data sanitized");
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 }
464
465 // --------------------------------------------------------------------
466
467 /**
Derek Jones69fc4fc2010-03-02 13:36:31 -0600468 * Clean Input Data
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 *
Derek Jones69fc4fc2010-03-02 13:36:31 -0600470 * This is a helper function. It escapes data and
471 * standardizes newline characters to \n
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 *
473 * @access private
474 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 * @return string
476 */
Derek Jones69fc4fc2010-03-02 13:36:31 -0600477 function _clean_input_data($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600479 if (is_array($str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600481 $new_array = array();
482 foreach ($str as $key => $val)
483 {
484 $new_array[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
485 }
486 return $new_array;
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 }
488
Derek Jones69fc4fc2010-03-02 13:36:31 -0600489 // We strip slashes if magic quotes is on to keep things consistent
490 if (get_magic_quotes_gpc())
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600492 $str = stripslashes($str);
493 }
494
495 // Clean UTF-8 if supported
496 if (UTF8_ENABLED === TRUE)
497 {
498 $str = $this->uni->clean_string($str);
499 }
500
501 // Should we filter the input data?
502 if ($this->_enable_xss === TRUE)
503 {
504 $str = $this->security->xss_clean($str);
505 }
506
507 // Standardize newlines if needed
508 if ($this->_standardize_newlines == TRUE)
509 {
510 if (strpos($str, "\r") !== FALSE)
511 {
512 $str = str_replace(array("\r\n", "\r"), "\n", $str);
513 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 }
515
516 return $str;
517 }
518
519 // --------------------------------------------------------------------
520
521 /**
Derek Jones69fc4fc2010-03-02 13:36:31 -0600522 * Clean Keys
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 *
Derek Jones69fc4fc2010-03-02 13:36:31 -0600524 * This is a helper function. To prevent malicious users
525 * from trying to exploit keys we make sure that keys are
526 * only named with alpha-numeric text and a few other items.
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 *
Derek Jones69fc4fc2010-03-02 13:36:31 -0600528 * @access private
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 * @param string
530 * @return string
531 */
Derek Jones69fc4fc2010-03-02 13:36:31 -0600532 function _clean_input_keys($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600534 if ( ! preg_match("/^[a-z0-9:_\/-]+$/i", $str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 {
Derek Jones69fc4fc2010-03-02 13:36:31 -0600536 exit('Disallowed Key Characters.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 }
538
Derek Jones69fc4fc2010-03-02 13:36:31 -0600539 // Clean UTF-8 if supported
540 if (UTF8_ENABLED === TRUE)
541 {
542 $str = $this->uni->clean_string($str);
543 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000544
Derek Jones69fc4fc2010-03-02 13:36:31 -0600545 return $str;
546 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000547
548}
549// END Input class
550
551/* End of file Input.php */
Derek Jonesc68dfbf2010-03-02 12:59:23 -0600552/* Location: ./system/core/Input.php */