tagsArray = (array) $tagsArray; $this->attrArray = (array) $attrArray; $this->tagsMethod = $tagsMethod; $this->attrMethod = $attrMethod; $this->xssAuto = $xssAuto; } /** * Method to be called by another php script. Processes for XSS and specified bad code. * @access public * @param Mixed $source - input string/array-of-string to be 'cleaned' * @return String $source - 'cleaned' version of input parameter */ function process($source) { // clean all elements in this array if (is_array($source)) { foreach($source as $key => $value) // filter element for XSS and other 'bad' code etc. if (is_string($value)) $source[$key] = $this->remove($this->decode($value)); return $source; // clean this string } else if (is_string($source)) { // filter source for XSS and other 'bad' code etc. return $this->remove($this->decode($source)); // return parameter as given } else return $source; } /** * Internal method to iteratively remove all unwanted tags and attributes * @access protected * @param String $source - input string to be 'cleaned' * @return String $source - 'cleaned' version of input parameter */ function remove($source) { $loopCounter=0; // provides nested-tag protection while($source != $this->filterTags($source)) { $source = $this->filterTags($source); $loopCounter++; } return $source; } /** * Internal method to strip a string of certain tags * @access protected * @param String $source - input string to be 'cleaned' * @return String $source - 'cleaned' version of input parameter */ function filterTags($source) { return strip_tags($source); } /** * Internal method to strip a tag of certain attributes * @access protected * @param Array $attrSet * @return Array $newSet */ function filterAttr($attrSet) { $newSet = array(); // process attributes for ($i = 0; $i xssAuto) && ((in_array(strtolower($attrSubSet[0]), $this->attrBlacklist)) || (substr($attrSubSet[0], 0, 2) == 'on')))) continue; // xss attr value filtering if ($attrSubSet[1]) { // strips unicode, hex, etc $attrSubSet[1] = str_replace('&#', '', $attrSubSet[1]); // strip normal newline within attr value $attrSubSet[1] = preg_replace('/\s+/', '', $attrSubSet[1]); // strip double quotes $attrSubSet[1] = str_replace('"', '', $attrSubSet[1]); // [requested feature] convert single quotes from either side to doubles (Single quotes shouldn't be used to pad attr value) if ((substr($attrSubSet[1], 0, 1) == "'") && (substr($attrSubSet[1], (strlen($attrSubSet[1]) - 1), 1) == "'")) $attrSubSet[1] = substr($attrSubSet[1], 1, (strlen($attrSubSet[1]) - 2)); // strip slashes $attrSubSet[1] = stripslashes($attrSubSet[1]); } // auto strip attr's with "javascript: if ( ((strpos(strtolower($attrSubSet[1]), 'expression') !== false) && (strtolower($attrSubSet[0]) == 'style')) || (strpos(strtolower($attrSubSet[1]), 'javascript:') !== false) || (strpos(strtolower($attrSubSet[1]), 'behaviour:') !== false) || (strpos(strtolower($attrSubSet[1]), 'vbscript:') !== false) || (strpos(strtolower($attrSubSet[1]), 'mocha:') !== false) || (strpos(strtolower($attrSubSet[1]), 'livescript:') !== false) ) continue; // if matches user defined array $attrFound = in_array(strtolower($attrSubSet[0]), $this->attrArray); // keep this attr on condition if ((!$attrFound && $this->attrMethod) || ($attrFound && !$this->attrMethod)) { // attr has value if ($attrSubSet[1]) $newSet[] = $attrSubSet[0] . '="' . $attrSubSet[1] . '"'; // attr has decimal zero as value else if ($attrSubSet[1] == "0") $newSet[] = $attrSubSet[0] . '="0"'; // reformat single attributes to XHTML else $newSet[] = $attrSubSet[0] . '="' . $attrSubSet[0] . '"'; } } return $newSet; } /** * Try to convert to plaintext * @access protected * @param String $source * @return String $source */ function decode($source) { // url decode $source = html_entity_decode($source, ENT_QUOTES, "ISO-8859-1"); // convert decimal $source = preg_replace('/&#(\d+);/me',"chr(\\1)", $source); // decimal notation // convert hex $source = preg_replace('/&#x([a-f0-9]+);/mei',"chr(0x\\1)", $source); // hex notation return $source; } /** * Method to be called by another php script. Processes for SQL injection * @access public * @param Mixed $source - input string/array-of-string to be 'cleaned' * @return String $source - 'cleaned' version of input parameter */ function safeSQL($source) { // clean all elements in this array if (is_array($source)) { foreach($source as $key => $value) // filter element for SQL injection if (is_string($value)) $source[$key] = $this->quoteSmart($this->decode($value)); return $source; // clean this string } else if (is_string($source)) { // filter source for SQL injection if (is_string($source)) return $this->quoteSmart($this->decode($source)); // return parameter as given } else return $source; } /** * @author Chris Tobin * @author Daniel Morris * @access protected * @param String $source * @return String $source */ function quoteSmart($source) { // strip slashes if (get_magic_quotes_gpc()) $source = stripslashes($source); // quote both numeric and text $source = $this->escapeString($source); return $source; } /** * @author Chris Tobin * @author Daniel Morris * @access protected * @param String $source * @return String $source */ function escapeString($string) { // depreciated function if (version_compare(phpversion(),"4.3.0", "<")) mysql_escape_string($string); // current function else mysql_real_escape_string($string); return $string; } /** * @author Sidnei Augusto Drovetto Jr. - drovetto@gmail.com * @param mixed $source A string or array of strings to be escaped * @return mixed $source An object of the same type of the input, only escaped * @access public */ function escapeHTML($source) { $output = $source; if (is_string($output)) $output = htmlentities($output, ENT_QUOTES, 'ISO-8859-1'); else if (is_array($output)) foreach ($output as &$element) $element = $this->escapeHTML($element); return $output; } } ?>