Exemplo n.º 1
0
 /**
  * applies xss checks on string
  * @param  string $string text to check
  * @param  string $allowed_tags allowed tags
  * @return string         safe value
  */
 public static function process_xss($string, $allowed_tags = FORMS_XSS_ALLOWED_TAGS)
 {
     // Only operate on valid UTF-8 strings. This is necessary to prevent cross
     // site scripting issues on Internet Explorer 6.
     if (!cs_form::_validate_utf8($string)) {
         return '';
     }
     // Store the input format
     cs_form::_filter_xss_split($allowed_tags, TRUE);
     // Remove NUL characters (ignored by some browsers)
     $string = str_replace(chr(0), '', $string);
     // Remove Netscape 4 JS entities
     $string = preg_replace('%&\\s*\\{[^}]*(\\}\\s*;?|$)%', '', $string);
     // Defuse all HTML entities
     $string = str_replace('&', '&', $string);
     // Change back only well-formed entities in our whitelist
     // Decimal numeric entities
     $string = preg_replace('/&#([0-9]+;)/', '&#\\1', $string);
     // Hexadecimal numeric entities
     $string = preg_replace('/&#[Xx]0*((?:[0-9A-Fa-f]{2})+;)/', '&#x\\1', $string);
     // Named entities
     $string = preg_replace('/&([A-Za-z][A-Za-z0-9]*;)/', '&\\1', $string);
     return preg_replace_callback('%
   (
   <(?=[^a-zA-Z!/])  # a lone <
   |                 # or
   <[^>]*(>|$)       # a string that starts with a <, up until the > or the end of the string
   |                 # or
   >                 # just a >
   )%x', 'cs_form::_filter_xss_split', $string);
 }