<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 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 processing of the requested string
/*
$safestep
0 for non-processing,
1 to prohibit unsafe HTML content (javascript, etc.),
2 completely disables HTML content and replaces some unsafe strings (such as: 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 hazard codes
+----------------------------------------------------------
* @access Public
+----------------------------------------------------------
* @param string $text to be processed
* @param mixed $tags allowed list of tags, 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);
Full Filter JS
$text = Preg_replace ('/<script?. */script>/', ', $text);
$text = str_replace (' [', ' [', $text);
$text = str_replace ('] ', '] ', $text);
$text = Str_replace (' | ', ' | ', $text);
Filter line breaks
$text = preg_replace ('/?/', ' ', $text);
Br
$text = preg_replace ('/<br (s/)? '. ' >/i ', '
', $text);
$text = Preg_replace ('/(
S*) {10,}/i ', '
', $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 ', ' [n] ', $text);
Filtering 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 Conversions
$text = Str_replace ("', ' | | ', $text);
$text = str_replace (' "" ', ' | | ', $text);
Filter the wrong single quotation marks
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);
Inverse 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 that you had 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 prevents-like
$search = ' abcdefghijklmnopqrstuvwxyz ';
$search. = ' abcdefghijklmnopqrstuvwxyz ';
$search. = ' [email protected]#$%^&* () ';
$search. = ' ~ ' ";:? +/={}[]-_| ';
For ($i = 0; $i < strlen ($search); $i + +) {
// ;? Matches the; which is optional
0{0,7} matches any padded zeros, which is 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 is, 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 ', ' onre 'Sizeend ', ' 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;
}
?>
How to use Str_replace anti-injection in PHP