class post_get{
private $array; #提交表單檢測字段和檢測要求
# $type; 缺省為更新 或$type ="Into";
public function _post_get($array,$type=""){
if(!$array) die(" Error!! ");
foreach ($array as $v)
{
$KK = $this->ck_split($v);
$KKarray = split("_", $KK);
if(preg_match("/Yes|No/is",$KKarray[1]))
{
$escape_filter = ($KKarray[2])? 'ck_'.$KKarray[2]:"";#需要轉義和過濾
$data = ($escape_filter)? $this->$escape_filter($_POST[$KKarray[0]]):$_POST[$KKarray[0]]; #輸出合法數據
if($data)
{
if(is_array($data))$data = implode(",",$data);
$Into_data .=",'$data'";
$Into_Field.=",`$KKarray[0]`";
$Updat_array[] ="`$KKarray[0]` = '$data'";
}
}
else $error .= ($KK)? "[".$KK."],":"";
}
$_data=(!$type)? $Updat_array:array("$Into_Field","$Into_data");
return (!$error)? $_data:$this->ck_script($error); # 數組為合法表單驗證結果,字符串為錯誤信息提示
}
public function ck_split($str){
$strarray = explode(",",$str);
foreach ($strarray as $i=> $var)
if(0 ===$i)
$name = trim($var);
elseif(1===$i)
{
if(eregi("No",trim($var))&&!$_POST[$name])break; #缺省忽略,否則...
else $_type = (trim($var))? $name.'_'.trim($var):""; #檢測後動作
}
elseif(2===$i)
{
$error = trim($var);
if(!$_POST[$name])
{
$_error = $error;
break;
}
}
else # 檢測部分
{
if(!trim($var))break;
if(is_array($_POST[$name]))
{
foreach ($_POST[$name] as $_var)
{
$_error= ($this->ck_open($_var,trim($var)))?"":$error;
if($_error)break;
}
}
else
{
$_error= ($this->ck_open($_POST[$name],trim($var)))?"":$error;
if($_error)break;
}
}
return ($_error)? """.$name."","".$_error.""":$_type;
}
# 窗口js提示
public function ck_script($str){
$str= trim($str,",");
$str = "
<script>
var str = new Array( $str );
for (i=0; i < str.length; i++)
{
document.getElementById('J'+str[0]+'').innerHTML= str[1];
}
</script>
";
return $str;
}
# 類型判斷
public function ck_detected($str){
return (eregi("^[a-zA-Z]*$",$str))? 'ck_'.$str:'ck_Length';
}
# 函數調用
public function ck_open($string,$str){
$functi = $this->ck_detected($str);
return ($this->$functi($string,$str))? true:false;
}
#-------------------------------------以下為檢測函數
# 長度
public function ck_Length($string,$str){
$len = split('-',trim($str));
return (strlen($string) > ($len[0]-1) && strlen($string) < ($len[1]+1))? true:false;
}
# 價格
public function ck_money($str){
return preg_match("/^(-|+)?d+(.d+)?$/",$str);
}
# 郵件
public function ck_email($str){
return preg_match("/^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$/", $str);
}
# 網址
public function ck_url($str){
return preg_match("/^http://[A-Za-z0-9]+.[A-Za-z0-9]+[/=?%-&_~`@[]':+!]*([^<>"])*$/", $str);
}
# 數字型
public function ck_num($str){
return is_numeric($str);
}
# 中文
public function ck_cn($str){
return preg_match("/^[x7f-xff]+$/", $str);
}
# 字母
public function ck_en($str){
return preg_match("/^[A-Za-z]+$/", $str);
}
# 數字字母混合
public function ck_numen($str){
return preg_match("/^([a-zA-Z0-9_-])+$/",$str);
}
# 電話號碼
public function ck_tel($str){
return ereg("^[+]?[0-9]+([xX-][0-9]+)*$", $str);
}
# 敏感詞
public function ck_key($str){
Global $badkey;
return (!preg_match("/$badkey/i",$str));
}
#-----------------------------------------------------輸出
# 字符替換
public function ck_filter($str){
$str=(is_array($str))? implode(",",$str):$str;
$str=HTMLSpecialChars($str); //將特殊字元轉成 HTML 格式。
$str=nl2br($str); //將回車替換為<br>
$str=str_replace(array(" ",'<? '),array(" ",'< ?'),$str); //替換空格替換為
return $str;
}
# MD5加密
public function ck_md($str){
return MD5($str);
}
# 轉義
public function ck_escape($str){
return (is_array($str))? implode(",",$str):$str;
}
# 有條件注銷(數字)
public function ck_cancel($str){
return (!is_numeric($str))? $str:"";
}
# 無條件注銷(數字)
public function ck_delete(){
return null;
}
}