blob: 783446aeccf97f4d4da5db033333dac764fc48af [file] [log] [blame]
Derek Jones0b59f272008-05-13 04:22:33 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allarda72b60d2007-01-31 23:56:11 +00002/**
Derek Allardd2df9bc2007-04-15 17:41:17 +00003 * CodeIgniter
Derek Allarda72b60d2007-01-31 23:56:11 +00004 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
Derek Allard3d879d52008-01-18 19:41:32 +00008 * @author ExpressionEngine Dev Team
Derek Allardd2df9bc2007-04-15 17:41:17 +00009 * @copyright Copyright (c) 2006, EllisLab, Inc.
Derek Jones7a9193a2008-01-21 18:39:20 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
Derek Allarda72b60d2007-01-31 23:56:11 +000012 * @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
Derek Allard3d879d52008-01-18 19:41:32 +000026 * @author ExpressionEngine Dev Team
Derek Jones7a9193a2008-01-21 18:39:20 +000027 * @link http://codeigniter.com/user_guide/libraries/input.html
Derek Allarda72b60d2007-01-31 23:56:11 +000028 */
29class CI_Input {
30 var $use_xss_clean = FALSE;
Derek Jones53437de2008-05-12 18:07:08 +000031 var $xss_hash = '';
Derek Allarda72b60d2007-01-31 23:56:11 +000032 var $ip_address = FALSE;
33 var $user_agent = FALSE;
34 var $allow_get_array = FALSE;
Derek Jonese3332b02008-05-13 14:44:32 +000035
36 /* never allowed, string replacement */
37 var $never_allowed_str = array(
38 'document.cookie' => '[removed]',
39 'document.write' => '[removed]',
40 '.parentNode' => '[removed]',
41 '.innerHTML' => '[removed]',
42 'window.location' => '[removed]',
43 '-moz-binding' => '[removed]',
44 '<!--' => '&lt;!--',
45 '-->' => '--&gt;',
46 '<![CDATA[' => '&lt;![CDATA['
47 );
48 /* never allowed, regex replacement */
49 var $never_allowed_regex = array(
50 "javascript\s*:" => '[removed]',
51 "expression\s*\(" => '[removed]', // CSS and IE
52 "Redirect\s+302" => '[removed]'
53 );
54
Derek Allarda72b60d2007-01-31 23:56:11 +000055 /**
56 * Constructor
57 *
58 * Sets whether to globally enable the XSS processing
59 * and whether to allow the $_GET array
60 *
61 * @access public
Derek Allard15dcf492008-05-12 21:37:04 +000062 */
Derek Allarda72b60d2007-01-31 23:56:11 +000063 function CI_Input()
Derek Allard15dcf492008-05-12 21:37:04 +000064 {
Derek Allarda72b60d2007-01-31 23:56:11 +000065 log_message('debug', "Input Class Initialized");
Derek Allard15dcf492008-05-12 21:37:04 +000066
Derek Allarda72b60d2007-01-31 23:56:11 +000067 $CFG =& load_class('Config');
68 $this->use_xss_clean = ($CFG->item('global_xss_filtering') === TRUE) ? TRUE : FALSE;
Derek Allard15dcf492008-05-12 21:37:04 +000069 $this->allow_get_array = ($CFG->item('enable_query_strings') === TRUE) ? TRUE : FALSE;
Derek Allarda72b60d2007-01-31 23:56:11 +000070 $this->_sanitize_globals();
71 }
Derek Allard15dcf492008-05-12 21:37:04 +000072
Derek Allarda72b60d2007-01-31 23:56:11 +000073 // --------------------------------------------------------------------
Derek Allard15dcf492008-05-12 21:37:04 +000074
Derek Allarda72b60d2007-01-31 23:56:11 +000075 /**
76 * Sanitize Globals
77 *
78 * This function does the following:
79 *
80 * Unsets $_GET data (if query strings are not enabled)
81 *
82 * Unsets all globals if register_globals is enabled
83 *
84 * Standardizes newline characters to \n
85 *
86 * @access private
87 * @return void
88 */
89 function _sanitize_globals()
90 {
Derek Jonesd85a11e2008-01-24 20:48:07 +000091 // Would kind of be "wrong" to unset any of these GLOBALS
92 $protected = array('_SERVER', '_GET', '_POST', '_FILES', '_REQUEST', '_SESSION', '_ENV', 'GLOBALS', 'HTTP_RAW_POST_DATA',
93 'system_folder', 'application_folder', 'BM', 'EXT', 'CFG', 'URI', 'RTR', 'OUT', 'IN');
Derek Allard15dcf492008-05-12 21:37:04 +000094
Derek Jones0ea06fd2008-02-05 15:23:51 +000095 // Unset globals for security.
Rick Ellisbb2041d2007-06-09 00:16:13 +000096 // This is effectively the same as register_globals = off
Derek Jones0ea06fd2008-02-05 15:23:51 +000097 foreach (array($_GET, $_POST, $_COOKIE, $_SERVER, $_FILES, $_ENV, (isset($_SESSION) && is_array($_SESSION)) ? $_SESSION : array()) as $global)
Derek Allarda72b60d2007-01-31 23:56:11 +000098 {
Derek Jones0b59f272008-05-13 04:22:33 +000099 if ( ! is_array($global))
Derek Allarda72b60d2007-01-31 23:56:11 +0000100 {
Derek Jones0b59f272008-05-13 04:22:33 +0000101 if ( ! in_array($global, $protected))
paulburdick8816aaa2007-06-27 23:07:36 +0000102 {
Derek Jones0ea06fd2008-02-05 15:23:51 +0000103 unset($GLOBALS[$global]);
paulburdick8816aaa2007-06-27 23:07:36 +0000104 }
Derek Allarda72b60d2007-01-31 23:56:11 +0000105 }
106 else
107 {
108 foreach ($global as $key => $val)
109 {
Derek Jones0b59f272008-05-13 04:22:33 +0000110 if ( ! in_array($key, $protected))
paulburdick8816aaa2007-06-27 23:07:36 +0000111 {
Derek Jones0ea06fd2008-02-05 15:23:51 +0000112 unset($GLOBALS[$key]);
113 }
Derek Allard15dcf492008-05-12 21:37:04 +0000114
Derek Jones0ea06fd2008-02-05 15:23:51 +0000115 if (is_array($val))
116 {
117 foreach($val as $k => $v)
118 {
Derek Jones0b59f272008-05-13 04:22:33 +0000119 if ( ! in_array($k, $protected))
Derek Jones0ea06fd2008-02-05 15:23:51 +0000120 {
121 unset($GLOBALS[$k]);
122 }
123 }
paulburdick8816aaa2007-06-27 23:07:36 +0000124 }
Derek Allard15dcf492008-05-12 21:37:04 +0000125 }
Derek Allarda72b60d2007-01-31 23:56:11 +0000126 }
127 }
128
129 // Is $_GET data allowed? If not we'll set the $_GET to an empty array
130 if ($this->allow_get_array == FALSE)
131 {
132 $_GET = array();
133 }
Rick Ellis112569d2007-02-26 19:19:08 +0000134 else
135 {
Derek Jones144cb5b2008-06-04 19:38:00 +0000136 $_GET = $this->_clean_input_data($_GET);
Rick Ellis112569d2007-02-26 19:19:08 +0000137 }
Derek Allard15dcf492008-05-12 21:37:04 +0000138
Derek Allarda72b60d2007-01-31 23:56:11 +0000139 // Clean $_POST Data
Derek Jones144cb5b2008-06-04 19:38:00 +0000140 $_POST = $this->_clean_input_data($_POST);
141
Derek Allarda72b60d2007-01-31 23:56:11 +0000142 // Clean $_COOKIE Data
Derek Jones144cb5b2008-06-04 19:38:00 +0000143 $_COOKIE = $this->_clean_input_data($_COOKIE);
Derek Allard15dcf492008-05-12 21:37:04 +0000144
Derek Allarda72b60d2007-01-31 23:56:11 +0000145 log_message('debug', "Global POST and COOKIE data sanitized");
Derek Allard15dcf492008-05-12 21:37:04 +0000146 }
147
Derek Allarda72b60d2007-01-31 23:56:11 +0000148 // --------------------------------------------------------------------
Derek Allard15dcf492008-05-12 21:37:04 +0000149
Derek Allarda72b60d2007-01-31 23:56:11 +0000150 /**
151 * Clean Input Data
152 *
153 * This is a helper function. It escapes data and
154 * standardizes newline characters to \n
155 *
156 * @access private
157 * @param string
158 * @return string
Derek Allard15dcf492008-05-12 21:37:04 +0000159 */
Derek Allarda72b60d2007-01-31 23:56:11 +0000160 function _clean_input_data($str)
161 {
162 if (is_array($str))
163 {
164 $new_array = array();
165 foreach ($str as $key => $val)
166 {
167 $new_array[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
168 }
169 return $new_array;
170 }
Derek Allard15dcf492008-05-12 21:37:04 +0000171
Rick Ellisbb2041d2007-06-09 00:16:13 +0000172 // We strip slashes if magic quotes is on to keep things consistent
173 if (get_magic_quotes_gpc())
174 {
175 $str = stripslashes($str);
176 }
Derek Allard15dcf492008-05-12 21:37:04 +0000177
Rick Ellisbb2041d2007-06-09 00:16:13 +0000178 // Should we filter the input data?
Derek Allarda72b60d2007-01-31 23:56:11 +0000179 if ($this->use_xss_clean === TRUE)
180 {
181 $str = $this->xss_clean($str);
182 }
Derek Allard15dcf492008-05-12 21:37:04 +0000183
Derek Allarda72b60d2007-01-31 23:56:11 +0000184 // Standardize newlines
Derek Jones0b59f272008-05-13 04:22:33 +0000185 if (strpos($str, "\r") !== FALSE)
186 {
187 $str = str_replace(array("\r\n", "\r"), "\n", $str);
188 }
189
190 return $str;
Derek Allarda72b60d2007-01-31 23:56:11 +0000191 }
Derek Allard15dcf492008-05-12 21:37:04 +0000192
Derek Allarda72b60d2007-01-31 23:56:11 +0000193 // --------------------------------------------------------------------
Derek Allard15dcf492008-05-12 21:37:04 +0000194
Derek Allarda72b60d2007-01-31 23:56:11 +0000195 /**
196 * Clean Keys
197 *
198 * This is a helper function. To prevent malicious users
199 * from trying to exploit keys we make sure that keys are
200 * only named with alpha-numeric text and a few other items.
201 *
202 * @access private
203 * @param string
204 * @return string
205 */
206 function _clean_input_keys($str)
Derek Allard15dcf492008-05-12 21:37:04 +0000207 {
Derek Jones0b59f272008-05-13 04:22:33 +0000208 if ( ! preg_match("/^[a-z0-9:_\/-]+$/i", $str))
Derek Allarda72b60d2007-01-31 23:56:11 +0000209 {
210 exit('Disallowed Key Characters.');
211 }
Rick Ellisbb2041d2007-06-09 00:16:13 +0000212
Derek Allarda72b60d2007-01-31 23:56:11 +0000213 return $str;
214 }
Rick Ellis112569d2007-02-26 19:19:08 +0000215
216 // --------------------------------------------------------------------
Derek Jones144cb5b2008-06-04 19:38:00 +0000217
218 /**
219 * Fetch from array
220 *
221 * This is a helper function to retrieve values from global arrays
222 *
223 * @access private
224 * @param array
225 * @param string
226 * @param bool
227 * @return string
228 */
229 function _fetch_from_array(&$array, $index = '', $xss_clean = FALSE)
230 {
231 if ( ! isset($array[$index]))
232 {
233 return FALSE;
234 }
Derek Allard15dcf492008-05-12 21:37:04 +0000235
Derek Jones144cb5b2008-06-04 19:38:00 +0000236 if ($xss_clean === TRUE)
237 {
238 return $this->xss_clean($array[$index]);
239 }
240
241 return $array[$index];
242 }
243
244 // --------------------------------------------------------------------
245
Rick Ellis112569d2007-02-26 19:19:08 +0000246 /**
247 * Fetch an item from the GET array
248 *
249 * @access public
250 * @param string
251 * @param bool
252 * @return string
253 */
Derek Allard87d1eeb2007-03-01 13:20:43 +0000254 function get($index = '', $xss_clean = FALSE)
Derek Allard15dcf492008-05-12 21:37:04 +0000255 {
Derek Jones144cb5b2008-06-04 19:38:00 +0000256 return $this->_fetch_from_array($_GET, $index, $xss_clean);
Rick Ellis112569d2007-02-26 19:19:08 +0000257 }
Derek Allard15dcf492008-05-12 21:37:04 +0000258
Derek Allarda72b60d2007-01-31 23:56:11 +0000259 // --------------------------------------------------------------------
Derek Allard15dcf492008-05-12 21:37:04 +0000260
Derek Allarda72b60d2007-01-31 23:56:11 +0000261 /**
262 * Fetch an item from the POST array
263 *
264 * @access public
265 * @param string
266 * @param bool
267 * @return string
268 */
269 function post($index = '', $xss_clean = FALSE)
Derek Allard15dcf492008-05-12 21:37:04 +0000270 {
Derek Jones144cb5b2008-06-04 19:38:00 +0000271 return $this->_fetch_from_array($_POST, $index, $xss_clean);
Derek Allarda72b60d2007-01-31 23:56:11 +0000272 }
Derek Allard15dcf492008-05-12 21:37:04 +0000273
Derek Allarda72b60d2007-01-31 23:56:11 +0000274 // --------------------------------------------------------------------
Derek Allard15dcf492008-05-12 21:37:04 +0000275
Derek Allarda72b60d2007-01-31 23:56:11 +0000276 /**
Derek Allardd3ee0412008-06-20 05:39:18 +0000277 * Fetch an item from either the GET array or the POST
278 *
279 * @access public
280 * @param string The index key
281 * @param bool XSS cleaning
282 * @return string
283 */
284 function get_post($index = '', $xss_clean = FALSE)
285 {
286 if ( ! isset($_POST[$index]) )
287 {
288 return $this->get($index, $xss_clean);
289 }
290 else
291 {
292 return $this->post($index, $xss_clean);
293 }
294 }
295
296 // --------------------------------------------------------------------
297
298 /**
Derek Allarda72b60d2007-01-31 23:56:11 +0000299 * Fetch an item from the COOKIE array
300 *
301 * @access public
302 * @param string
303 * @param bool
304 * @return string
305 */
306 function cookie($index = '', $xss_clean = FALSE)
307 {
Derek Jones144cb5b2008-06-04 19:38:00 +0000308 return $this->_fetch_from_array($_COOKIE, $index, $xss_clean);
Derek Allarda72b60d2007-01-31 23:56:11 +0000309 }
310
311 // --------------------------------------------------------------------
Derek Allard15dcf492008-05-12 21:37:04 +0000312
Derek Allarda72b60d2007-01-31 23:56:11 +0000313 /**
314 * Fetch an item from the SERVER array
315 *
316 * @access public
317 * @param string
318 * @param bool
319 * @return string
320 */
321 function server($index = '', $xss_clean = FALSE)
Derek Allard15dcf492008-05-12 21:37:04 +0000322 {
Derek Jones144cb5b2008-06-04 19:38:00 +0000323 return $this->_fetch_from_array($_SERVER, $index, $xss_clean);
Derek Allarda72b60d2007-01-31 23:56:11 +0000324 }
Derek Allard15dcf492008-05-12 21:37:04 +0000325
Derek Allarda72b60d2007-01-31 23:56:11 +0000326 // --------------------------------------------------------------------
Derek Allard15dcf492008-05-12 21:37:04 +0000327
Derek Allarda72b60d2007-01-31 23:56:11 +0000328 /**
329 * Fetch the IP Address
330 *
331 * @access public
332 * @return string
333 */
334 function ip_address()
335 {
336 if ($this->ip_address !== FALSE)
337 {
338 return $this->ip_address;
339 }
Derek Allard15dcf492008-05-12 21:37:04 +0000340
Derek Allarda72b60d2007-01-31 23:56:11 +0000341 if ($this->server('REMOTE_ADDR') AND $this->server('HTTP_CLIENT_IP'))
342 {
343 $this->ip_address = $_SERVER['HTTP_CLIENT_IP'];
344 }
345 elseif ($this->server('REMOTE_ADDR'))
346 {
347 $this->ip_address = $_SERVER['REMOTE_ADDR'];
348 }
349 elseif ($this->server('HTTP_CLIENT_IP'))
350 {
351 $this->ip_address = $_SERVER['HTTP_CLIENT_IP'];
352 }
353 elseif ($this->server('HTTP_X_FORWARDED_FOR'))
354 {
355 $this->ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
356 }
Derek Allard15dcf492008-05-12 21:37:04 +0000357
Derek Allarda72b60d2007-01-31 23:56:11 +0000358 if ($this->ip_address === FALSE)
359 {
360 $this->ip_address = '0.0.0.0';
361 return $this->ip_address;
362 }
Derek Allard15dcf492008-05-12 21:37:04 +0000363
Derek Allarda72b60d2007-01-31 23:56:11 +0000364 if (strstr($this->ip_address, ','))
365 {
366 $x = explode(',', $this->ip_address);
367 $this->ip_address = end($x);
368 }
Derek Allard15dcf492008-05-12 21:37:04 +0000369
Derek Jones0b59f272008-05-13 04:22:33 +0000370 if ( ! $this->valid_ip($this->ip_address))
Derek Allarda72b60d2007-01-31 23:56:11 +0000371 {
372 $this->ip_address = '0.0.0.0';
373 }
Derek Allard15dcf492008-05-12 21:37:04 +0000374
Derek Allarda72b60d2007-01-31 23:56:11 +0000375 return $this->ip_address;
376 }
Derek Allard15dcf492008-05-12 21:37:04 +0000377
Derek Allarda72b60d2007-01-31 23:56:11 +0000378 // --------------------------------------------------------------------
Derek Allard15dcf492008-05-12 21:37:04 +0000379
Derek Allarda72b60d2007-01-31 23:56:11 +0000380 /**
381 * Validate IP Address
382 *
Rick Ellise666afc2007-06-11 05:03:11 +0000383 * Updated version suggested by Geert De Deckere
384 *
Derek Allarda72b60d2007-01-31 23:56:11 +0000385 * @access public
386 * @param string
387 * @return string
388 */
389 function valid_ip($ip)
390 {
Rick Ellise666afc2007-06-11 05:03:11 +0000391 $ip_segments = explode('.', $ip);
Derek Allard15dcf492008-05-12 21:37:04 +0000392
Rick Ellise666afc2007-06-11 05:03:11 +0000393 // Always 4 segments needed
394 if (count($ip_segments) != 4)
Rick Ellis112569d2007-02-26 19:19:08 +0000395 {
396 return FALSE;
397 }
Rick Ellis65e8f0e2007-06-12 03:53:21 +0000398 // IP can not start with 0
Rick Ellis39213142007-06-12 03:53:12 +0000399 if (substr($ip_segments[0], 0, 1) == '0')
Rick Ellis112569d2007-02-26 19:19:08 +0000400 {
Rick Ellise666afc2007-06-11 05:03:11 +0000401 return FALSE;
402 }
403 // Check each segment
404 foreach ($ip_segments as $segment)
405 {
406 // IP segments must be digits and can not be
407 // longer than 3 digits or greater then 255
Rick Ellisba648932007-06-12 03:39:38 +0000408 if (preg_match("/[^0-9]/", $segment) OR $segment > 255 OR strlen($segment) > 3)
Rick Ellis112569d2007-02-26 19:19:08 +0000409 {
Rick Ellise666afc2007-06-11 05:03:11 +0000410 return FALSE;
Rick Ellis112569d2007-02-26 19:19:08 +0000411 }
412 }
Derek Allard15dcf492008-05-12 21:37:04 +0000413
Rick Ellis112569d2007-02-26 19:19:08 +0000414 return TRUE;
Derek Allarda72b60d2007-01-31 23:56:11 +0000415 }
Derek Allard15dcf492008-05-12 21:37:04 +0000416
Derek Allarda72b60d2007-01-31 23:56:11 +0000417 // --------------------------------------------------------------------
Derek Allard15dcf492008-05-12 21:37:04 +0000418
Derek Allarda72b60d2007-01-31 23:56:11 +0000419 /**
420 * User Agent
421 *
422 * @access public
423 * @return string
424 */
425 function user_agent()
426 {
427 if ($this->user_agent !== FALSE)
428 {
429 return $this->user_agent;
430 }
Derek Allard15dcf492008-05-12 21:37:04 +0000431
Derek Jones0b59f272008-05-13 04:22:33 +0000432 $this->user_agent = ( ! isset($_SERVER['HTTP_USER_AGENT'])) ? FALSE : $_SERVER['HTTP_USER_AGENT'];
Derek Allard15dcf492008-05-12 21:37:04 +0000433
Derek Allarda72b60d2007-01-31 23:56:11 +0000434 return $this->user_agent;
435 }
Derek Allard15dcf492008-05-12 21:37:04 +0000436
Derek Allarda72b60d2007-01-31 23:56:11 +0000437 // --------------------------------------------------------------------
Derek Allard15dcf492008-05-12 21:37:04 +0000438
Derek Allarda72b60d2007-01-31 23:56:11 +0000439 /**
paulburdick763064b2007-06-27 23:25:55 +0000440 * Filename Security
441 *
442 * @access public
443 * @param string
444 * @return string
445 */
446 function filename_security($str)
447 {
448 $bad = array(
449 "../",
450 "./",
451 "<!--",
452 "-->",
453 "<",
454 ">",
455 "'",
456 '"',
457 '&',
458 '$',
459 '#',
460 '{',
461 '}',
462 '[',
463 ']',
464 '=',
465 ';',
466 '?',
paulburdick763064b2007-06-27 23:25:55 +0000467 "%20",
468 "%22",
469 "%3c", // <
470 "%253c", // <
471 "%3e", // >
472 "%0e", // >
473 "%28", // (
474 "%29", // )
475 "%2528", // (
476 "%26", // &
477 "%24", // $
478 "%3f", // ?
479 "%3b", // ;
480 "%3d" // =
Derek Allard15dcf492008-05-12 21:37:04 +0000481 );
482
483 return stripslashes(str_replace($bad, '', $str));
paulburdick763064b2007-06-27 23:25:55 +0000484 }
Derek Allard15dcf492008-05-12 21:37:04 +0000485
paulburdick763064b2007-06-27 23:25:55 +0000486 // --------------------------------------------------------------------
Derek Allard15dcf492008-05-12 21:37:04 +0000487
paulburdick763064b2007-06-27 23:25:55 +0000488 /**
Derek Allarda72b60d2007-01-31 23:56:11 +0000489 * XSS Clean
490 *
491 * Sanitizes data so that Cross Site Scripting Hacks can be
492 * prevented.  This function does a fair amount of work but
493 * it is extremely thorough, designed to prevent even the
494 * most obscure XSS attempts.  Nothing is ever 100% foolproof,
495 * of course, but I haven't been able to get anything passed
496 * the filter.
497 *
498 * Note: This function should only be used to deal with data
499 * upon submission.  It's not something that should
500 * be used for general runtime processing.
501 *
502 * This function was based in part on some code and ideas I
503 * got from Bitflux: http://blog.bitflux.ch/wiki/XSS_Prevention
504 *
505 * To help develop this script I used this great list of
506 * vulnerabilities along with a few other hacks I've
507 * harvested from examining vulnerabilities in other programs:
508 * http://ha.ckers.org/xss.html
509 *
510 * @access public
511 * @param string
512 * @return string
513 */
Derek Jones7aae9052008-06-25 16:02:06 +0000514 function xss_clean($str, $is_image = FALSE)
Derek Jones53437de2008-05-12 18:07:08 +0000515 {
516 /*
517 * Is the string an array?
518 *
519 */
520 if (is_array($str))
521 {
522 while (list($key) = each($str))
523 {
524 $str[$key] = $this->xss_clean($str[$key]);
525 }
Derek Allard15dcf492008-05-12 21:37:04 +0000526
Derek Jones53437de2008-05-12 18:07:08 +0000527 return $str;
528 }
Derek Jones908ecc62008-05-21 19:53:40 +0000529
Derek Jones63fc5fe2008-05-15 20:13:14 +0000530 /*
Derek Jones7aae9052008-06-25 16:02:06 +0000531 * Remove Invisible Characters
Derek Jones63fc5fe2008-05-15 20:13:14 +0000532 */
Derek Jones7aae9052008-06-25 16:02:06 +0000533 $str = $this->_remove_invisible_characters($str);
Derek Allarda72b60d2007-01-31 23:56:11 +0000534
535 /*
Derek Jones53437de2008-05-12 18:07:08 +0000536 * Protect GET variables in URLs
537 */
538
539 // 901119URL5918AMP18930PROTECT8198
540
541 $str = preg_replace('|\&([a-z\_0-9]+)\=([a-z\_0-9]+)|i', $this->xss_hash()."\\1=\\2", $str);
542
543 /*
Derek Allarda72b60d2007-01-31 23:56:11 +0000544 * Validate standard character entities
545 *
546 * Add a semicolon if missing. We do this to enable
547 * the conversion of entities to ASCII later.
548 *
549 */
Derek Jones48bb32a2007-07-12 13:10:42 +0000550 $str = preg_replace('#(&\#?[0-9a-z]+)[\x00-\x20]*;?#i', "\\1;", $str);
Derek Allard15dcf492008-05-12 21:37:04 +0000551
Derek Allarda72b60d2007-01-31 23:56:11 +0000552 /*
Derek Jones48bb32a2007-07-12 13:10:42 +0000553 * Validate UTF16 two byte encoding (x00)
Derek Allarda72b60d2007-01-31 23:56:11 +0000554 *
555 * Just as above, adds a semicolon if missing.
556 *
557 */
Derek Jones48bb32a2007-07-12 13:10:42 +0000558 $str = preg_replace('#(&\#x?)([0-9A-F]+);?#i',"\\1\\2;",$str);
Derek Allarda72b60d2007-01-31 23:56:11 +0000559
560 /*
Derek Jones53437de2008-05-12 18:07:08 +0000561 * Un-Protect GET variables in URLs
562 */
Derek Jones53437de2008-05-12 18:07:08 +0000563 $str = str_replace($this->xss_hash(), '&', $str);
Derek Allard15dcf492008-05-12 21:37:04 +0000564
Derek Jones53437de2008-05-12 18:07:08 +0000565 /*
Derek Allarda72b60d2007-01-31 23:56:11 +0000566 * URL Decode
567 *
568 * Just in case stuff like this is submitted:
569 *
570 * <a href="http://%77%77%77%2E%67%6F%6F%67%6C%65%2E%63%6F%6D">Google</a>
571 *
Derek Jonesab32a422008-02-04 22:02:11 +0000572 * Note: Use rawurldecode() so it does not remove plus signs
Derek Allarda72b60d2007-01-31 23:56:11 +0000573 *
Derek Allard15dcf492008-05-12 21:37:04 +0000574 */
Derek Jonesab32a422008-02-04 22:02:11 +0000575 $str = rawurldecode($str);
Derek Jones63fc5fe2008-05-15 20:13:14 +0000576
Derek Allarda72b60d2007-01-31 23:56:11 +0000577 /*
Derek Jones303c9cb2007-07-12 19:12:37 +0000578 * Convert character entities to ASCII
Derek Allarda72b60d2007-01-31 23:56:11 +0000579 *
580 * This permits our tests below to work reliably.
581 * We only convert entities that are within tags since
582 * these are the ones that will pose security problems.
583 *
584 */
Derek Allard15dcf492008-05-12 21:37:04 +0000585
Derek Jones908ecc62008-05-21 19:53:40 +0000586 $str = preg_replace_callback("/[a-z]+=([\'\"]).*?\\1/si", array($this, '_convert_attribute'), $str);
587
Derek Jones63fc5fe2008-05-15 20:13:14 +0000588 $str = preg_replace_callback("/<\w+.*?(?=>|<|$)/si", array($this, '_html_entity_decode_callback'), $str);
Derek Allard15dcf492008-05-12 21:37:04 +0000589
Derek Jones48bb32a2007-07-12 13:10:42 +0000590 /*
Derek Jones7aae9052008-06-25 16:02:06 +0000591 * Remove Invisible Characters Again!
592 */
593 $str = $this->_remove_invisible_characters($str);
594
595 /*
Derek Jones48bb32a2007-07-12 13:10:42 +0000596 * Convert all tabs to spaces
597 *
598 * This prevents strings like this: ja vascript
599 * NOTE: we deal with spaces between characters later.
600 * NOTE: preg_replace was found to be amazingly slow here on large blocks of data,
601 * so we use str_replace.
602 *
603 */
Derek Allard15dcf492008-05-12 21:37:04 +0000604
Derek Jones0b59f272008-05-13 04:22:33 +0000605 if (strpos($str, "\t") !== FALSE)
606 {
607 $str = str_replace("\t", ' ', $str);
Derek Jones908ecc62008-05-21 19:53:40 +0000608 }
Derek Jones63fc5fe2008-05-15 20:13:14 +0000609
Derek Allarda72b60d2007-01-31 23:56:11 +0000610 /*
611 * Not Allowed Under Any Conditions
Derek Allard15dcf492008-05-12 21:37:04 +0000612 */
Derek Jonese3332b02008-05-13 14:44:32 +0000613
614 foreach ($this->never_allowed_str as $key => $val)
Derek Jones48bb32a2007-07-12 13:10:42 +0000615 {
616 $str = str_replace($key, $val, $str);
617 }
Derek Jonese3332b02008-05-13 14:44:32 +0000618
619 foreach ($this->never_allowed_regex as $key => $val)
Derek Allarda72b60d2007-01-31 23:56:11 +0000620 {
621 $str = preg_replace("#".$key."#i", $val, $str);
622 }
Derek Allard15dcf492008-05-12 21:37:04 +0000623
Derek Allarda72b60d2007-01-31 23:56:11 +0000624 /*
Derek Allarda72b60d2007-01-31 23:56:11 +0000625 * Makes PHP tags safe
626 *
627 * Note: XML tags are inadvertently replaced too:
628 *
629 * <?xml
630 *
631 * But it doesn't seem to pose a problem.
632 *
Derek Allard15dcf492008-05-12 21:37:04 +0000633 */
Derek Jones48bb32a2007-07-12 13:10:42 +0000634 $str = str_replace(array('<?php', '<?PHP', '<?', '?'.'>'), array('&lt;?php', '&lt;?PHP', '&lt;?', '?&gt;'), $str);
Derek Allard15dcf492008-05-12 21:37:04 +0000635
Derek Allarda72b60d2007-01-31 23:56:11 +0000636 /*
637 * Compact any exploded words
638 *
639 * This corrects words like: j a v a s c r i p t
640 * These words are compacted back to their correct state.
641 *
Derek Allard15dcf492008-05-12 21:37:04 +0000642 */
paulburdickb614d392007-06-26 21:58:56 +0000643 $words = array('javascript', 'expression', 'vbscript', 'script', 'applet', 'alert', 'document', 'write', 'cookie', 'window');
Derek Allarda72b60d2007-01-31 23:56:11 +0000644 foreach ($words as $word)
645 {
646 $temp = '';
Derek Allardc1acb412008-06-04 20:58:03 +0000647
Derek Jones7a3b96e2008-06-04 21:01:56 +0000648 for ($i = 0, $wordlen = strlen($word); $i < $wordlen; $i++)
Derek Allarda72b60d2007-01-31 23:56:11 +0000649 {
650 $temp .= substr($word, $i, 1)."\s*";
651 }
Derek Jones9f23e7c2008-05-30 20:00:11 +0000652
Derek Jones01f72ca2007-05-04 18:19:17 +0000653 // We only want to do this when it is followed by a non-word character
654 // That way valid stuff like "dealer to" does not become "dealerto"
Derek Jones9f23e7c2008-05-30 20:00:11 +0000655 $str = preg_replace_callback('#('.substr($temp, 0, -3).')(\W)#is', array($this, '_compact_exploded_words'), $str);
Derek Allarda72b60d2007-01-31 23:56:11 +0000656 }
Derek Jones7aae9052008-06-25 16:02:06 +0000657
Derek Allarda72b60d2007-01-31 23:56:11 +0000658 /*
659 * Remove disallowed Javascript in links or img tags
Derek Jonesbd08d842008-05-20 15:07:27 +0000660 * We used to do some version comparisons and use of stripos for PHP5, but it is dog slow compared
661 * to these simplified non-capturing preg_match(), especially if the pattern exists in the string
paulburdick391eb032007-06-27 22:58:24 +0000662 */
663 do
664 {
665 $original = $str;
Derek Allard15dcf492008-05-12 21:37:04 +0000666
Derek Jonesbd08d842008-05-20 15:07:27 +0000667 if (preg_match("/<a/i", $str))
Derek Jones48bb32a2007-07-12 13:10:42 +0000668 {
Derek Jones7aae9052008-06-25 16:02:06 +0000669 $str = preg_replace_callback("#<a\s*([^>]*?)(>|$)#si", array($this, '_js_link_removal'), $str);
Derek Jones48bb32a2007-07-12 13:10:42 +0000670 }
Derek Allard15dcf492008-05-12 21:37:04 +0000671
Derek Jonesbd08d842008-05-20 15:07:27 +0000672 if (preg_match("/<img/i", $str))
Derek Jones48bb32a2007-07-12 13:10:42 +0000673 {
Derek Jones7aae9052008-06-25 16:02:06 +0000674 $str = preg_replace_callback("#<img\s*([^>]*?)(>|$)#si", array($this, '_js_img_removal'), $str);
Derek Jones48bb32a2007-07-12 13:10:42 +0000675 }
Derek Allard15dcf492008-05-12 21:37:04 +0000676
Derek Jonesbd08d842008-05-20 15:07:27 +0000677 if (preg_match("/script/i", $str) OR preg_match("/xss/i", $str))
Derek Jones48bb32a2007-07-12 13:10:42 +0000678 {
Derek Jones7aae9052008-06-25 16:02:06 +0000679 $str = preg_replace("#<(/*)(script|xss)(.*?)\>#si", '[removed]', $str);
Derek Jones48bb32a2007-07-12 13:10:42 +0000680 }
paulburdick391eb032007-06-27 22:58:24 +0000681 }
682 while($original != $str);
Derek Allard15dcf492008-05-12 21:37:04 +0000683
paulburdick391eb032007-06-27 22:58:24 +0000684 unset($original);
Derek Allarda72b60d2007-01-31 23:56:11 +0000685
686 /*
687 * Remove JavaScript Event Handlers
688 *
689 * Note: This code is a little blunt. It removes
690 * the event handler and anything up to the closing >,
691 * but it's unlikely to be a problem.
692 *
Derek Allard15dcf492008-05-12 21:37:04 +0000693 */
Derek Jonesc04f0fc2008-06-04 18:20:18 +0000694 $event_handlers = array('on\w*','xmlns');
Derek Jones245038d2008-05-15 21:58:07 +0000695
696 if ($is_image === TRUE)
697 {
698 /*
699 * Adobe Photoshop puts XML metadata into JFIF images, including namespacing,
700 * so we have to allow this for images. -Paul
701 */
702 unset($event_handlers[array_search('xmlns', $event_handlers)]);
703 }
704
Derek Jonesc04f0fc2008-06-04 18:20:18 +0000705 $str = preg_replace("#<([^><]+)(".implode('|', $event_handlers).")(\s*=\s*[^><]*)([><]*)#i", "<\\1\\4", $str);
Derek Jones7aae9052008-06-25 16:02:06 +0000706
Derek Allarda72b60d2007-01-31 23:56:11 +0000707 /*
708 * Sanitize naughty HTML elements
709 *
710 * If a tag containing any of the words in the list
711 * below is found, the tag gets converted to entities.
712 *
713 * So this: <blink>
714 * Becomes: &lt;blink&gt;
715 *
Derek Allard15dcf492008-05-12 21:37:04 +0000716 */
Derek Jonesbd08d842008-05-20 15:07:27 +0000717 $naughty = 'alert|applet|audio|basefont|base|behavior|bgsound|blink|body|embed|expression|form|frameset|frame|head|html|ilayer|iframe|input|layer|link|meta|object|plaintext|style|script|textarea|title|video|xml|xss';
Derek Jonese3332b02008-05-13 14:44:32 +0000718 $str = preg_replace_callback('#<(/*\s*)('.$naughty.')([^><]*)([><]*)#is', array($this, '_sanitize_naughty_html'), $str);
Derek Allard15dcf492008-05-12 21:37:04 +0000719
Derek Allarda72b60d2007-01-31 23:56:11 +0000720 /*
721 * Sanitize naughty scripting elements
722 *
723 * Similar to above, only instead of looking for
724 * tags it looks for PHP and JavaScript commands
725 * that are disallowed. Rather than removing the
726 * code, it simply converts the parenthesis to entities
727 * rendering the code un-executable.
728 *
729 * For example: eval('some code')
730 * Becomes: eval&#40;'some code'&#41;
731 *
732 */
paulburdick033ef022007-06-26 21:52:52 +0000733 $str = preg_replace('#(alert|cmd|passthru|eval|exec|expression|system|fopen|fsockopen|file|file_get_contents|readfile|unlink)(\s*)\((.*?)\)#si', "\\1\\2&#40;\\3&#41;", $str);
Derek Allard15dcf492008-05-12 21:37:04 +0000734
Derek Allarda72b60d2007-01-31 23:56:11 +0000735 /*
736 * Final clean up
737 *
738 * This adds a bit of extra precaution in case
739 * something got through the above filters
740 *
Derek Allard15dcf492008-05-12 21:37:04 +0000741 */
Derek Jones000ab692008-05-13 14:46:38 +0000742 foreach ($this->never_allowed_str as $key => $val)
Derek Allarda72b60d2007-01-31 23:56:11 +0000743 {
Derek Jones48bb32a2007-07-12 13:10:42 +0000744 $str = str_replace($key, $val, $str);
745 }
Derek Jones000ab692008-05-13 14:46:38 +0000746
747 foreach ($this->never_allowed_regex as $key => $val)
Derek Jones48bb32a2007-07-12 13:10:42 +0000748 {
Derek Jones63fc5fe2008-05-15 20:13:14 +0000749 $str = preg_replace("#".$key."#i", $val, $str);
Derek Allarda72b60d2007-01-31 23:56:11 +0000750 }
Derek Allard15dcf492008-05-12 21:37:04 +0000751
Derek Jones63fc5fe2008-05-15 20:13:14 +0000752 /*
753 * Images are Handled in a Special Way
754 * - Essentially, we want to know that after all of the character conversion is done whether
755 * any unwanted, likely XSS, code was found. If not, we return TRUE, as the image is clean.
756 * However, if the string post-conversion does not matched the string post-removal of XSS,
757 * then it fails, as there was unwanted XSS code found and removed/changed during processing.
758 */
Derek Allard15dcf492008-05-12 21:37:04 +0000759
Derek Jones63fc5fe2008-05-15 20:13:14 +0000760 if ($is_image === TRUE)
761 {
762 if ($str == $converted_string)
763 {
764 return TRUE;
765 }
766 else
767 {
768 return FALSE;
769 }
770 }
Derek Jones63fc5fe2008-05-15 20:13:14 +0000771
Derek Allarda72b60d2007-01-31 23:56:11 +0000772 log_message('debug', "XSS Filtering completed");
773 return $str;
774 }
775
776 // --------------------------------------------------------------------
Derek Allard15dcf492008-05-12 21:37:04 +0000777
Derek Jones01f72ca2007-05-04 18:19:17 +0000778 /**
Derek Jones53437de2008-05-12 18:07:08 +0000779 * Random Hash for protecting URLs
780 *
781 * @access public
782 * @return string
783 */
784 function xss_hash()
Derek Allard15dcf492008-05-12 21:37:04 +0000785 {
Derek Jones53437de2008-05-12 18:07:08 +0000786 if ($this->xss_hash == '')
787 {
788 if (phpversion() >= 4.2)
789 mt_srand();
790 else
791 mt_srand(hexdec(substr(md5(microtime()), -8)) & 0x7fffffff);
Derek Allard15dcf492008-05-12 21:37:04 +0000792
Derek Jones53437de2008-05-12 18:07:08 +0000793 $this->xss_hash = md5(time() + mt_rand(0, 1999999999));
794 }
Derek Allard15dcf492008-05-12 21:37:04 +0000795
Derek Jones53437de2008-05-12 18:07:08 +0000796 return $this->xss_hash;
797 }
798
799 // --------------------------------------------------------------------
Derek Jonese3332b02008-05-13 14:44:32 +0000800
801 /**
Derek Jones7aae9052008-06-25 16:02:06 +0000802 * Remove Invisible Characters
803 *
804 * This prevents sandwiching null characters
805 * between ascii characters, like Java\0script.
806 *
807 * @access public
808 * @param string
809 * @return string
810 */
811 function _remove_invisible_characters($str)
812 {
813 static $non_displayables;
814
815 if ( ! isset($non_displayables))
816 {
817 // every control character except newline (10), carriage return (13), and horizontal tab (09),
818 // both as a URL encoded character (::shakes fist at IE and WebKit::), and the actual character
819 $non_displayables = array(
820 '/%0[0-8]/', '/[\x00-\x08]/', // 00-08
821 '/%11/', '/\x0b/', '/%12/', '/\x0c/', // 11, 12
822 '/%1[4-9]/', '/%2[0-9]/', '/%3[0-1]/', // url encoded 14-31
823 '/[\x0e-\x1f]/'); // 14-31
824
825 }
826
827 do
828 {
829 $cleaned = $str;
830 $str = preg_replace($non_displayables, '', $str);
831 }
832 while ($cleaned != $str);
833
834 return $str;
835 }
836
837 // --------------------------------------------------------------------
838
839 /**
Derek Jones9f23e7c2008-05-30 20:00:11 +0000840 * Compact Exploded Words
841 *
842 * Callback function for xss_clean() to remove whitespace from
843 * things like j a v a s c r i p t
844 *
845 * @access public
846 * @param type
847 * @return type
848 */
849 function _compact_exploded_words($matches)
850 {
851 return preg_replace('/\s+/s', '', $matches[1]).$matches[2];
852 }
853
854 // --------------------------------------------------------------------
855
856 /**
Derek Jonese3332b02008-05-13 14:44:32 +0000857 * Sanitize Naughty HTML
858 *
859 * Callback function for xss_clean() to remove naughty HTML elements
860 *
861 * @access private
862 * @param array
863 * @return string
864 */
865 function _sanitize_naughty_html($matches)
866 {
867 // encode opening brace
868 $str = '&lt;'.$matches[1].$matches[2].$matches[3];
869
870 // encode captured opening or closing brace to prevent recursive vectors
Derek Jones908ecc62008-05-21 19:53:40 +0000871 $str .= str_replace(array('>', '<'), array('&gt;', '&lt;'), $matches[4]);
Derek Jonesbd08d842008-05-20 15:07:27 +0000872
Derek Jonese3332b02008-05-13 14:44:32 +0000873 return $str;
874 }
875
876 // --------------------------------------------------------------------
877
Derek Jones53437de2008-05-12 18:07:08 +0000878 /**
Derek Jones01f72ca2007-05-04 18:19:17 +0000879 * JS Link Removal
880 *
881 * Callback function for xss_clean() to sanitize links
882 * This limits the PCRE backtracks, making it more performance friendly
883 * and prevents PREG_BACKTRACK_LIMIT_ERROR from being triggered in
884 * PHP 5.2+ on link-heavy strings
885 *
886 * @access private
887 * @param array
888 * @return string
889 */
890 function _js_link_removal($match)
891 {
Derek Jones7aae9052008-06-25 16:02:06 +0000892 $attributes = $this->_filter_attributes(str_replace(array('<', '>'), '', $match[1]));
893 return str_replace($match[1], preg_replace("#href=.*?(alert\(|alert&\#40;|javascript\:|charset\=|window\.|document\.|\.cookie|<script|<xss|base64\s*,)#si", "", $attributes), $match[0]);
Derek Jones01f72ca2007-05-04 18:19:17 +0000894 }
Derek Allard15dcf492008-05-12 21:37:04 +0000895
Derek Jones01f72ca2007-05-04 18:19:17 +0000896 /**
897 * JS Image Removal
898 *
899 * Callback function for xss_clean() to sanitize image tags
900 * This limits the PCRE backtracks, making it more performance friendly
901 * and prevents PREG_BACKTRACK_LIMIT_ERROR from being triggered in
902 * PHP 5.2+ on image tag heavy strings
903 *
904 * @access private
905 * @param array
906 * @return string
907 */
908 function _js_img_removal($match)
909 {
Derek Jones7aae9052008-06-25 16:02:06 +0000910 $attributes = $this->_filter_attributes(str_replace(array('<', '>'), '', $match[1]));
911 return str_replace($match[1], preg_replace("#src=.*?(alert\(|alert&\#40;|javascript\:|charset\=|window\.|document\.|\.cookie|<script|<xss|base64\s*,)#si", "", $attributes), $match[0]);
Derek Jones01f72ca2007-05-04 18:19:17 +0000912 }
Derek Allarda72b60d2007-01-31 23:56:11 +0000913
Derek Jones01f72ca2007-05-04 18:19:17 +0000914 // --------------------------------------------------------------------
Derek Allard15dcf492008-05-12 21:37:04 +0000915
Derek Jones303c9cb2007-07-12 19:12:37 +0000916 /**
917 * Attribute Conversion
918 *
919 * Used as a callback for XSS Clean
920 *
921 * @access public
922 * @param array
923 * @return string
924 */
Derek Jones908ecc62008-05-21 19:53:40 +0000925 function _convert_attribute($match)
Derek Jones303c9cb2007-07-12 19:12:37 +0000926 {
Derek Jones908ecc62008-05-21 19:53:40 +0000927 return str_replace(array('>', '<'), array('&gt;', '&lt;'), $match[0]);
Derek Jones303c9cb2007-07-12 19:12:37 +0000928 }
Derek Allard15dcf492008-05-12 21:37:04 +0000929
Derek Jones303c9cb2007-07-12 19:12:37 +0000930 // --------------------------------------------------------------------
931
932 /**
933 * HTML Entity Decode Callback
934 *
935 * Used as a callback for XSS Clean
936 *
937 * @access public
938 * @param array
939 * @return string
940 */
941 function _html_entity_decode_callback($match)
942 {
Derek Jones6159d1d2007-07-16 13:04:46 +0000943 global $CFG;
944 $charset = $CFG->item('charset');
Derek Jones303c9cb2007-07-12 19:12:37 +0000945
946 return $this->_html_entity_decode($match[0], strtoupper($charset));
947 }
948
949 // --------------------------------------------------------------------
Derek Allard15dcf492008-05-12 21:37:04 +0000950
Derek Allarda72b60d2007-01-31 23:56:11 +0000951 /**
952 * HTML Entities Decode
953 *
954 * This function is a replacement for html_entity_decode()
955 *
956 * In some versions of PHP the native function does not work
957 * when UTF-8 is the specified character set, so this gives us
958 * a work-around. More info here:
959 * http://bugs.php.net/bug.php?id=25670
960 *
961 * @access private
962 * @param string
963 * @param string
964 * @return string
965 */
966 /* -------------------------------------------------
967 /* Replacement for html_entity_decode()
968 /* -------------------------------------------------*/
Derek Allard15dcf492008-05-12 21:37:04 +0000969
Derek Allarda72b60d2007-01-31 23:56:11 +0000970 /*
971 NOTE: html_entity_decode() has a bug in some PHP versions when UTF-8 is the
972 character set, and the PHP developers said they were not back porting the
973 fix to versions other than PHP 5.x.
974 */
Derek Jones303c9cb2007-07-12 19:12:37 +0000975 function _html_entity_decode($str, $charset='UTF-8')
Derek Allarda72b60d2007-01-31 23:56:11 +0000976 {
977 if (stristr($str, '&') === FALSE) return $str;
Derek Allard15dcf492008-05-12 21:37:04 +0000978
Derek Allarda72b60d2007-01-31 23:56:11 +0000979 // The reason we are not using html_entity_decode() by itself is because
980 // while it is not technically correct to leave out the semicolon
981 // at the end of an entity most browsers will still interpret the entity
982 // correctly. html_entity_decode() does not convert entities without
983 // semicolons, so we are left with our own little solution here. Bummer.
Derek Allard15dcf492008-05-12 21:37:04 +0000984
Derek Allarda72b60d2007-01-31 23:56:11 +0000985 if (function_exists('html_entity_decode') && (strtolower($charset) != 'utf-8' OR version_compare(phpversion(), '5.0.0', '>=')))
986 {
987 $str = html_entity_decode($str, ENT_COMPAT, $charset);
Derek Jones63fc5fe2008-05-15 20:13:14 +0000988 $str = preg_replace('~&#x(0*[0-9a-f]{2,5})~ei', 'chr(hexdec("\\1"))', $str);
Derek Allarda72b60d2007-01-31 23:56:11 +0000989 return preg_replace('~&#([0-9]{2,4})~e', 'chr(\\1)', $str);
990 }
Derek Allard15dcf492008-05-12 21:37:04 +0000991
Derek Allarda72b60d2007-01-31 23:56:11 +0000992 // Numeric Entities
Derek Jones63fc5fe2008-05-15 20:13:14 +0000993 $str = preg_replace('~&#x(0*[0-9a-f]{2,5});{0,1}~ei', 'chr(hexdec("\\1"))', $str);
Derek Allarda72b60d2007-01-31 23:56:11 +0000994 $str = preg_replace('~&#([0-9]{2,4});{0,1}~e', 'chr(\\1)', $str);
Derek Allard15dcf492008-05-12 21:37:04 +0000995
Derek Allarda72b60d2007-01-31 23:56:11 +0000996 // Literal Entities - Slightly slow so we do another check
997 if (stristr($str, '&') === FALSE)
998 {
999 $str = strtr($str, array_flip(get_html_translation_table(HTML_ENTITIES)));
1000 }
Derek Allard15dcf492008-05-12 21:37:04 +00001001
Derek Allarda72b60d2007-01-31 23:56:11 +00001002 return $str;
1003 }
1004
Derek Jones7aae9052008-06-25 16:02:06 +00001005 // --------------------------------------------------------------------
1006
1007 /**
1008 * Filter Attributes
1009 *
1010 * Filters tag attributes for consistency and safety
1011 *
1012 * @access public
1013 * @param string
1014 * @return string
1015 */
1016 function _filter_attributes($str)
1017 {
1018 $out = '';
1019
1020 if (preg_match_all('#\s*[a-z\-]+\s*=\s*(\042|\047)([^\\1]*?)\\1#is', $str, $matches))
1021 {
1022 foreach ($matches[0] as $match)
1023 {
1024 $out .= "{$match}";
1025 }
1026 }
1027
1028 return $out;
1029 }
1030
1031 // --------------------------------------------------------------------
1032
Derek Allarda72b60d2007-01-31 23:56:11 +00001033}
1034// END Input class
Derek Jones53437de2008-05-12 18:07:08 +00001035
1036/* End of file Input.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +00001037/* Location: ./system/libraries/Input.php */