<?php
/**
* Safe Filter function
*
* @param $string
* @return String
*/
function Safe_replace ($string) {
$string = Str_replace ('%20 ', ', ', $string);
$string = Str_replace ('%27 ', ', ', $string);
$string = Str_replace ('%2527 ', ', ', $string);
$string = Str_replace (' * ', ', ', $string);
$string = Str_replace (' "', '" ", $string);
$string = Str_replace ("'", ", $string);
$string = Str_replace (' ",", $string);
$string = Str_replace ('; ', ', ', $string);
$string = Str_replace (' < ', ' < ', $string);
$string = Str_replace (' > ', ' > ', $string);
$string = Str_replace ("{", "", $string);
$string = Str_replace ('} ', ', $string);
$string = Str_replace (', ', $string);
return $string;
}
?>
<?php
/**
* Returns a string or array that has been processed by addslashes
* @param $string a string or array to be processed
* @return Mixed
*/
function New_addslashes ($string) {
if (!is_array ($string)) return addslashes ($string);
foreach ($string as $key => $val) $string [$key] = new_addslashes ($val);
return $string;
}
?>
<?php
Secure handling of the requested string
/*
$safestep
0 for not handling,
1 to prohibit unsafe HTML content (JavaScript, etc.),
2 completely prohibit HTML content and replace partial unsafe strings (e.g. eval (, Union, CONCAT (、--、, etc.)
*/
function Stringsafe ($str, $safestep =-1) {
$safestep = ($safestep >-1)? $safestep: 1;
if ($safestep = = 1) {
$str = Preg_replace ("#script: #i", "Script:", $STR);
$str = Preg_replace ("#<[/]{0,1}" (LINK|META|IFR|FRA|SCR) [^>]*> #isU ",", $str);
$str = Preg_replace ("#[]{1,}#", "", $str);
return $str;
}else if ($safestep = = 2) {
$str = Addslashes (Htmlspecialchars (Stripslashes ($STR)));
$str = Preg_replace ("#eval #i", ' eval ', $str);
$str = Preg_replace ("#union #i", ' Union ', $STR);
$str = Preg_replace ("#concat #i", ' concat ', $str);
$str = Preg_replace ("#--#", '--', $str);
$str = Preg_replace ("#[]{1,}#", "", $str);
return $str;
}else{
return $str;
}
}
?>
<?php
/**
+----------------------------------------------------------
* Output secure HTML for filtering dangerous code
+----------------------------------------------------------
* @access Public
+----------------------------------------------------------
* @param string $text strings to be processed
* @param mixed $tags allowed list of labels, such as TABLE|TD|TH|TD
+----------------------------------------------------------
* @return String
+----------------------------------------------------------
*/
static public Function safehtml ($text, $tags = null)
{
$text = Trim ($text);
Fully filtered annotations
$text = Preg_replace ('/<!--?. *-->/', ', $text);
Fully filter Dynamic code
$text = Preg_replace ('/<?|? ') >/', ', $text);
Completely filter JS
$text = Preg_replace ('/<script?. */script>/', ', $text);
$text = Str_replace (' [', ' [', $text];
$text = Str_replace ('] ', '] ', $text);
$text = Str_replace (' | ', ' | '), $text);
Filter line Feed
$text = Preg_replace ('/?/', ', $text);
Br
$text = Preg_replace ('/<br (s/)? ' >/i ', ' [BR] ', $text);
$text = Preg_replace ('/([br]s*) {10,}/i ', ' [BR] ', $text);
Filter for dangerous properties such as: Filter on event Lang JS
while (Preg_match (<[^><]+) (LANG|ON|ACTION|BACKGROUND|CODEBASE|DYNSRC|LOWSRC) [^><]+/i ', $text, $ MAT)) {
$text =str_replace ($mat [0], $mat [1], $text);
}
while (Preg_match (<[^><]+) (window.| javascript:|js:|about:|file:|document.| Vbs:|cookie) ([^><]*)/I ', $text, $mat)) {
$text =str_replace ($mat [0], $mat [1]. $mat [3], $text);
}
if (empty ($allowTags)) {$allowTags = self:: $htmlTags [' Allow '];}
Allowed HTML tags
$text = Preg_replace ('/< $allowTags. ') ([^><[]]*) >/i ', ' [a] ', $text);
Filter Extra HTML
if (empty ($banTag)) {$banTag = self:: $htmlTags [' ban '];}
$text = Preg_replace ('/</? $banTag. ') [^><]*>/i ', ', $text];
Filter legitimate HTML tags
while (Preg_match ('/< ([a-z]+) [^><[]]*>[^><]*</1>/i ', $text, $mat)) {
$text =str_replace ($mat [0],str_replace (' > ', '] ', Str_replace (' < ', ' [', $mat [0])], $text);
}
Convert quotation marks
while (Preg_match () ([^[]]*=s*) ("|") ([^2=[]]+) 2 ([^[]]*])/I ', $text, $mat)) {
$text =str_replace ($mat [0], $mat [1]. ' | ' $mat [3]. ' | '. $mat [4], $text);
}
Null attribute Conversion
$text = Str_replace (', ', ' | | ', $text);
$text = Str_replace (' "', ', ' | | ', $text);
Single quote for filtering errors
while (Preg_match ('/[[^[]]* | ') [^[]]*]/i ', $text, $mat)})
$text =str_replace ($mat [0],str_replace ($mat [1], ", $mat [0]), $text);
}
Convert all other illegal < >
$text = Str_replace (' < ', ' < ', $text);
$text = Str_replace (' > ', ' > ', $text);
$text = Str_replace (' "', '" ", $text);
Reverse Conversion
$text = Str_replace (' [', ' < ', $text);
$text = Str_replace ('] ', ' > ', $text);
$text = Str_replace (' | ', ' "", $text);
Filter extra spaces
$text = Str_replace (', ', $text);
return $text;
}
?>
<?php
function Removexss ($val) {
Remove all non-printable characters. CR (0a) and LF (0b) and TAB (9) are allowed
This prevents some character re-spacing such as <javascript>
Note this you have to handle splits with,, and later since they *are* allowed in some//inputs
$val = Preg_replace ('/([x00-x08,x0b-x0c,x0e-x19])/', ', $val);
Straight replacements, the user should never need these since they ' re normal characters
This is prevents like
$search = ' abcdefghijklmnopqrstuvwxyz ';
$search. = ' ABCDEFGHIJKLMNOPQRSTUVWXYZ ';
$search. = ' 1234567890!@#$%^&* () ';
$search. = ' ~ ';: +/={}[]-_| ';
for ($i = 0; $i < strlen ($search); $i + +) {
// ;? Matches the;, which is optional
0{0,7} matches any padded zeros, which are optional and go up to 8 chars
@ @ search for the hex values
$val = Preg_replace ('/(&#[xx]0{0,8} '. Dechex (Ord ($search [$i])). /I ', $search [$i], $val);//with A;
@ @ 0{0,7} matches ' 0 ' zero to seven times
$val = Preg_replace ('/({0,8} '. Ord ($search [$i]). /', $search [$i], $val); with A;
}
Now the only remaining whitespace attacks are, and
$ra 1 = Array (' javascript ', ' VBScript ', ' expression ', ' applets ', ' meta ', ' xml ', ' blink ', ' link ', ' style ', ' script ', ' embed ', ' Object ', ' iframe ', ' frame ', ' frameset ', ' ilayer ', ' layer ', ' bgsound ', ' title ', ' base ';
$ra 2 = Array (' onabort ', ' onactivate ', ' onafterprint ', ' onafterupdate ', ' Onbeforeactivate ', ' onbeforecopy ', ' onbeforecut ', ' onbeforedeactivate ', ' onbeforeeditfocus ', ' onbeforepaste ', ' Onbeforeprint ', ' onbeforeunload ', ' onbeforeupdate ', ' onblur ', ' onbounce ', ' oncellchange ', ' onchange ', ' onclick ', ' OnContextMenu ', ' oncontrolselect ', ' oncopy ', ' oncut ', ' ondataavailable ', ' ondatasetchanged ', ' ondatasetcomplete ', ' OnDblClick ', ' ondeactivate ', ' Ondrag ', ' ondragend ', ' ondragenter ', ' ondragleave ', ' ondragover ', ' ondragstart ', ' OnDrop ', ' onerror ', ' onerrorupdate ', ' onfilterchange ', ' onfinish ', ' onfocus ', ' onfocusin ', ' onfocusout ', ' onhelp ', ' OnKeyDown ', ' onkeypress ', ' onkeyup ', ' onlayoutcomplete ', ' onload ', ' onlosecapture ', ' onmousedown ', ' onmouseenter ', ' OnMouseLeave ', ' onmousemove ', ' onmouseout ', ' onmouseover ', ' onmouseup ', ' onmousewheel ', ' onmove ', ' onmoveend ', ' Onmovestart ', ' onpaste ', ' onpropertychange ', ' onreadystatechange ', ' onreset ', ' onresize ', ' onreSizeend ', ' onresizestart ', ' onrowenter ', ' onrowexit ', ' onrowsdelete ', ' onrowsinserted ', ' onscroll ', ' onselect ', ' Onselectionchange ', ' onselectstart ', ' onstart ', ' onstop ', ' onsubmit ', ' onunload ';
$ra = Array_merge ($ra 1, $ra 2);
$found = true; Keep replacing as long as the previous round replaced something
while ($found = = True) {
$val _before = $val;
for ($i = 0; $i < sizeof ($RA); $i + +) {
$pattern = '/';
for ($j = 0; $j < strlen ($ra [$i]); $j + +) {
if ($j > 0) {
$pattern. = ' (';
$pattern. = ' (&#[xx]0{0,8} ([9ab]);) ';
$pattern. = ' | ';
$pattern. = ' | ({0,8} ([9|10|13]);) ';
$pattern. = ') *;
}
$pattern. = $ra [$i] [$j];
}
$pattern. = '/I ';
$replacement = substr ($ra [$i], 0, 2). ' <x> '. substr ($ra [$i], 2); Add in <> to nerf the tag
$val = Preg_replace ($pattern, $replacement, $val); Filter out the hex tags
if ($val _before = = $val) {
No replacements were made, so exit the loop
$found = false;
}
}
}
return $val;
}
?>