发布一个 PHP操作MYSQL数据库类 带过滤XSS危险脚本函数
[code]<?phpclass SFun{var $Fun="now()";function SFun($Date=""){if($Date!="")$this->Fun=$Date;}}
class MySqlDB
{
var $dbConn;
var $dbHost;
var $dbUID;
var $dbPWD;
var $dbName;
var $dbEncode;
var $IsOp;
function MySqlDB($Host='localhost',$UID='root',$Pwd='',$Name='',$Encode='utf8')
{
$this->dbHost = $Host;
$this->dbUID = $UID;
$this->dbPWD = $Pwd;
$this->dbName = $Name;
$this->dbEncode = $Encode;
$this->IsOp=false;
}
//打开数据库
function Open()
{
if (!$this->dbConn)
{
@$this->dbConn = mysql_connect($this->dbHost, $this->dbUID, $this->dbPWD) or die("数据库连接错误!...");
}
mysql_query("SET NAMES '" . $this->dbEncode . "'");
mysql_select_db($this->dbName);
$this->IsOp=true;
}
//关闭数据库
function Close()
{
if ($this->dbConn&&$this->IsOp)
{
mysql_close($this->dbConn);
$this->IsOp=false;
}
}
function htmlrsp($str)
{
$str=str_replace("<","<",$str);
$str=str_replace(">",">",$str);
return $str;
}
//过滤XSS危险脚本
function RemoveXSS($val) {
if(strpos($val,"<")===false)return $val;
$val = preg_replace('/([\x00-\x08][\x0b-\x0c][\x0e-\x20])/', '', $val);
$search = 'abcdefghijklmnopqrstuvwxyz';
$search .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$search .= '1234567890!@#$%^&*()';
$search .= '~`";:?+/={}[]-_|\'\\';
for ($i = 0; $i < strlen($search); $i++) {
$val = preg_replace('/(&#[x|X]0{0,8}'.dechex(ord($search[$i])).';?)/i', $search[$i], $val); // with a ;
$val = preg_replace('/(�{0,8}'.ord($search[$i]).';?)/', $search[$i], $val); // with a ;
}
$ra1 = array('javascript', 'vbscript', 'expression', 'applet');
$ra2 = 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($ra1, $ra2);
$val=preg_replace("'<script[^>]*?>.*?</script>'si","",$val);
$val=preg_replace("'<style[^>]*?>.*?</style>'si","",$val);
$val=preg_replace("/<(script|iframe|expression|applet|meta|xml|blink|style|frame|frameset|ilayer|layer|bgsound|title|base|link)(|[^>]+)>/i","",$val);
$val=preg_replace("/<\\/(script|iframe|expression|applet|meta|xml|blink|style|frame|frameset|ilayer|layer|bgsound|title|base)>/i","",$val);
$newval="";
$idx=0;
$edx=0;
do
{
$idx=strpos($val,"<",$edx);
if($idx!==false)
{
if($idx>$edx){
$str=substr($val,$edx,$idx-$edx);
$newval.=$this->htmlrsp($str);
}
$edx=strpos($val,">",$idx);
if($edx!==false&&$edx>$idx)
{
$edx++;
$tag=substr($val,$idx,$edx-$idx);
$ridx=strrpos($tag,"<",1);
if($ridx!==false){
$str=substr($val,$idx,$ridx);
$newval.=$this->htmlrsp($str);
$idx=$idx+$ridx;
$tag=substr($val,$idx,$edx-$idx);
}
if(stripos($tag,"on")===false)
{$newval.=$tag;}
else
{
for($j=0;$j<count($ra1);$j++){if(stripos($tag,$ra1[$j])!==false){$tag=str_ireplace($ra1[$j],"",$tag);}}
for($j=0;$j<count($ra2);$j++){
if(stripos($tag,$ra2[$j])!==false){
$tag=preg_replace('/'.$ra2[$j].'="[^"]+"/i',"",$tag);
$tag=preg_replace("/".$ra2[$j]."='[^']+'/i","",$tag);
$tag=preg_replace("/".$ra2[$j]."=[^ ]+>/i",">",$tag);
$tag=preg_replace("/".$ra2[$j]."=[^ ]+ /i","",$tag);
$tag=str_ireplace($ra2[$j],"",$tag);
}}
$newval.=$tag;
}
}
else
{
$str=substr($val,$idx);
$newval.=$this->htmlrsp($str);
}
}
else
{
$str=substr($val,$edx);
$newval.=$this->htmlrsp($str);
}
}while($idx!==false&&$edx!==false);
return $newval;
}
//执行SQL语句
function ExeSql($sql)
{
mysql_query($sql, $this->dbConn) or die("执行SQL语句错误...".$sql);
}
function HGexecute($SqlArr)
{
mysql_query("SET AUTOCOMMIT=0");//设置为不自动提交,因为MYSQL默认立即执行
mysql_query("BEGIN");//开始事务定义
for($i=0;$i<count($SqlArr);$i++)
{
$sql=$SqlArr[$i];
if(!mysql_query($sql, $this->dbConn))
{
mysql_query("ROLLBACK");//判断执行失败回滚
mysql_query("SET AUTOCOMMIT=1");
return false;
}
}
mysql_query("SET AUTOCOMMIT=1");
mysql_query("COMMIT");//执行事务
return true;
}
//取SQL数据
function GetData($sql)
{
$result = mysql_query($sql, $this->dbConn) or die("查询SQL语句错误...".$sql);
$records=array();
//while($record = mysql_fetch_array($result))
while($record = mysql_fetch_object($result))
{
$records[] = $record;
}
return $records;
}
function _T($str){
$str=$this->RemoveXSS($str);
$str=str_replace('\\','\\\\',$str);
$str=str_replace('\'','\\\'',$str);
//$str=str_replace('"','\\"',$str);
return $str;}
function GetOne($sql)
{
$records=$this->GetData($sql);
return $records[0];
}
function Like($str)
{
$str=$this->_T($str);
$str=str_replace('%','\\%',$str);
return $str;
}
//插入记录
function Add($Tb,$A,$IsId=false)
{
$SqlKey=array();
$SqlArr=array();
foreach ($A as $key=>$value)
{
$SqlKey[]="`".$key."`";
if($value===NULL)
$SqlArr[]="NULL";
else if(gettype($value)=="integer"||gettype($value)=="boolean"||gettype($value)=="double"||gettype($value)=="float")
$SqlArr[]=$value;
else if(gettype($value)=="object")
$SqlArr[]=$value->Fun;
else
$SqlArr[]="'".$this->_T($value)."'";
}
$Sql="INSERT INTO ".$Tb."(".join(",",$SqlKey).") VALUES (".join(",",$SqlArr).")";
mysql_query($Sql, $this->dbConn) or die("执行SQL语句错误...".$Sql);
if($IsId)
return mysql_insert_id($this->dbConn);
else
return 0;
}
function Update($Tb,$A,$Id)
{
$Id=intval($Id);
$SqlArr=array();
foreach ($A as $key=>$value)
{
if($value===NULL)
$SqlArr[]="`".$key."`=NULL";
else if(gettype($value)=="integer"||gettype($value)=="boolean"||gettype($value)=="double"||gettype($value)=="float")
$SqlArr[]="`".$key."`=".$value;
else if(gettype($value)=="object")
$SqlArr[]="`".$key."`=".$value->Fun;
else
$SqlArr[]="`".$key."`='".$this->_T($value)."'";
}
$Sql="update ".$Tb." set ".join(",",$SqlArr)." where id=".$Id;
mysql_query($Sql, $this->dbConn) or die("执行SQL语句错误...".$Sql);
}
function Del($Tb,$Id)
{
$Id=intval($Id);
$Sql="delete from ".$Tb." where id=".$Id;
mysql_query($Sql, $this->dbConn) or die("执行SQL语句错误...".$Sql);
}
}
/*
$DB=new MySqlDB('127.0.0.1','root','123','test');
$DB->Open();
$A["key1"]=$_POST["FCKeditor4"];
$A["key2"]=NULL;
$A["key3"]=new SFun("now()");
$A["key4"]=NULL;
$id=$DB->Add("test",$A,1);
echo $id;
$Row=$DB->GetOne("select * from test where id=".$id);
print_r($Row);
$DB->Close();*/
?>
[/code]
页:
[1]