php 表单敏感字符过滤代码 |
时间:2015-01-23 来源:西部数据 作者:西部数据 |
|
-
-
-
- $_form = new formHtmlFind();
- class formHtmlFind{
-
-
-
-
-
-
- public function formHtml($array,$infoArray='')
- {
-
- if(emptyempty($array))return false;
- $newform = null;
-
- $this->infoArray = !emptyempty($infoArray)?$infoArray:array();
- $this->array['class'] = get_class_methods(get_class());
- foreach ($array as $key =>$arr)
- {
-
- $key = preg_replace("/[^a-z]/i",'',$key);
-
- $newform .= $this->outputForm($arr,$key);
- }
-
- return $newform.$this->jsError();
- }
-
-
-
- private function outputForm($arr,$key)
- {
- $value = null;
- if(emptyempty($arr))return false;
-
- $type = $key;
-
- $name = trim($arr[0]);
-
- $value = (!emptyempty($this->infoArray[$name]))? trim($this->infoArray[$name]):trim($arr[1]);
- $value = emptyempty($this->post[$name])? $value :trim($this->post[$name]);
-
- $title = trim($arr[2]);
-
- $style = trim($arr[3]);
- if($key!=="hidden")
- {
- $dt = "<dt>{$title}</dt><dd>";
-
- $dd = "<tt id="J{$name}"></tt></dd>rn";
- }
- return (!preg_match("/checkbox|select|radio/i",$key))?
- $dt.$this->newInput($type,$name,$value,$style,$title).$dd:
- $this->formSelect($type,$name,$arr[1],$title,$style);
- }
-
-
-
- public function postForm($array)
- {
-
- if(emptyempty($array)||emptyempty($_POST))return false;
- $this->post = $_POST;
- $this->array['class'] = get_class_methods(get_class());
- foreach ($array as $key =>$arr)
- {
-
- $key = preg_replace("/[^a-z]/i",'',$key);
-
- if (!emptyempty($arr)&&'file' != $key)$newData[trim($arr[0])] = $this->postFind($arr,$key);
- }
-
- if(!emptyempty($this->error))
- {
- return false;
- }
- else return $newData;
- }
-
-
-
- private function newInput($type,$name,$value,$style,$title)
- {
- switch ($type)
- {
- case 'text':
-
- return "<input type="text" name="{$name}" value="{$value}" {$style}/>";
- break;
- case 'password':
-
- return "<input type="password" name="{$name}" {$style}/>";
- break;
- case '':
-
- return "<textarea name="{$name}" {$style}/>{$value}</textarea>";
- break;
- case 'hidden':
-
- return "<input type="hidden" name="{$name}" value="{$value}" {$style}/>";
- break;
- case 'file':
-
- return "<input type= "file"name="{$name}" {$style}/>";
- break;
- case 'submit':
-
- return "<input type="submit" name="{$name}" value="$value" $style}/>";
- break;
- default:
- return "{$type}类型错误!!!";
- break;
- }
- }
-
-
-
-
- private function postFind($arr,$key)
- {
- if(emptyempty($arr))return false;
- $name = $title =$error =$find =$standard =null;
-
- $name = trim($arr[0]);
-
- $title = trim($arr[2]);
-
- $error = trim($arr[4]);
-
- $find = trim($arr[5]);
-
- $standard = trim($arr[6]);
-
- if(!emptyempty($standard))$this->error .=$this->ck_split($standard,$name,$title,$find,$error);
-
- if(is_array($this->post[$name]))$this->post[$name] = implode(",",$this->post[$name]);
-
- $KKarray = array();
- if(preg_match("/Y|N/is",$find))
- {
- $KKarray = split("_", $find);
-
- $escape_filter = (!emptyempty($KKarray[1]))?'ck_'.$KKarray[1]:'';
-
- $data = ($escape_filter)?$this->$escape_filter($this->post[$name]):$this->post[$name];
-
- }
- else $data = "";
-
- return $data;
- }
-
-
-
- private function formSelect($type,$name,$value,$title,$style)
- {
- $outform = null;
-
- $nowvalue = (!emptyempty($this->post[$name]))?$this->post[$name]:$this->infoarray[$name];
-
- if(!emptyempty($nowvalue))$valueArray = explode(",",$nowvalue);
-
- if(is_array($title))
- {
- array_unshift($title,'选择');
- $titarray = array_values($title);
- }else $titarray = explode("|",$title);
-
- if(is_array($value))
- {
- array_unshift($value,'选择');
- $valarray = array_keys($value);
- if(emptyempty($title))$titarray = array_values($value);
- }
- else $valarray = explode("|",$value);
-
- if(!emptyempty($this->post)&&!emptyempty($this->infoArray))$value = preg_replace("/Y_/i",'',$value);
-
- foreach ($valarray as $key =>$varl)
- {
-
- if(!emptyempty($valueArray))$select = (in_array($varl,$valueArray))?'Y':'';
-
- else $select = (eregi("Y_",$varl))? 'Y':'';
-
- if($key >'0')
- {
- $_title=($titarray[$key])? $titarray[$key]:$title;
- switch ($type)
- {
- case 'select':
- if('Y' == $select)$select = 'selected';
- $outform .= sprintf("<option %s value="%s"/>%s</option>rn"
- ,$select,preg_replace("/Y_/i",'',$varl),$_title);
- break;
- case 'radio':
- if('Y' == $select)$select = 'checked';
- $outform .= sprintf("<label>%s<input %s type="radio" name="%s" value="%s" %s/></label>rn",
- $_title,$select,$name,$varl,$style);
- break;
- case 'checkbox':
- if('Y' == $select)$select = 'checked';
- $outform .= sprintf("<label>%s<input %s type="checkbox" name="%s[]" value="%s" %s/></label>rn",$_title,$select,$name,$varl,$style);
- break;
- }
- $select =null;
- }
- }
-
- if($type =='select')$outform = sprintf('<select name="%s" %s>%s</select>',$name,$style,$outform);
- return sprintf("<dt>%s</dt><dd>%s<tt id="J%s"></tt></dd>rn",$titarray[0],$outform,$name);
- }
-
-
-
- private function ck_split($standard,$name,$title,$find,$error)
- {
-
- if(eregi('N',$find) && emptyempty($this->post[$name]))return false;
-
- if(eregi('Y',$find) && emptyempty($this->post[$name]))return "["J{$name}","$error"],";
- $t_error = null;
-
- $arr = explode(',',$standard);
-
- if(!emptyempty($arr))foreach ($arr as $var)
- {
- if(trim($var)!='')
- {
- switch ($this->post)
- {
- case is_array($this->post[$name]):
-
- foreach ($this->post[$name] as $_var)
- {
- $t_error.= ($this->ck_open($_var,trim($var)))?"":$error;
- if($t_error)break;
- }
- break;
- default:
- $t_error.= ($this->ck_open($this->post[$name],trim($var)))?"":$error;
- break;
- }
- if($t_error)break;
- }
- }
- return ($t_error)? "["J{$name}","$t_error"],":"";
- }
-
- private function ck_open($string,$str)
- {
- $functi = $this->ck_detected($str);
- return ($this->$functi($string,$str))? true:false;
- }
-
- private function ck_detected($str)
- {
- $detect = (eregi("^[a-zA-Z]*$",$str))? "{$str}Detect":'lengthDetect';
- if(!in_array($detect,$this->array['class']))
- {
- location('index.php',$ck,' Lack of function !!!');
- }
- return $detect;
- }
-
-
- public function lengthDetect($string,$str){
- $len = split('-',trim($str));
- return (strlen($string) > ($len[0]-1) && strlen($string) < ($len[1]+1))? true:false;
- }
-
- public function moneyDetect($str){
- return preg_match("/^(-|+)?d+(.d+)?$/",$str);
- }
-
- public function emailDetect($str){
- return preg_match("/^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$/", $str);
- }
-
- public function urlDetect($str){
- return preg_match("/^http://[A-Za-z0-9]+.[A-Za-z0-9]+[/=?%-&_~`@[]':+!]*([^<>"])*$/", $str);
- }
-
- public function numDetect($str){
- return is_numeric($str);
- }
-
- public function cnDetect($str){
- return preg_match("/^[x7f-xff]+$/", $str);
- }
-
- public function enDetect($str){
- return preg_match("/^[A-Za-z]+$/", $str);
- }
-
- public function numenDetect($str){
- return preg_match("/^([a-zA-Z0-9_-])+$/",$str);
- }
-
- public function telDetect($str){
- return ereg("^[+]?[0-9]+([xX-][0-9]+)*$", $str);
- }
-
- public function keyDetect($str){
- return (!preg_match("/$badkey/i",$str));
- }
-
-
- public function ck_filter($str){
- $str=(is_array($str))? implode(",",$str):$str;
- $str=nl2br($str);
- $str=htmlspecialchars($str);
-
- return $str;
- }
-
- function ck_escape($str)
- {
- if (!get_magic_quotes_gpc())return addslashes($str);
- return $str;
- }
-
- public function ck_md5($str){
- return MD5($str);
- }
-
- public function ck_base64($str){
- return base64_encode($str);
- }
-
- function ck_time($str){
-
- if(!is_numeric($str))
- {
- return time_r($str);
- }
- else return $str;
- }
-
- public function ck_cancel($str){
- return (!is_numeric($str))? $str:"";
- }
-
- public function ck_delete(){
- return null;
- }
-
- private function jsError()
- {
- if(emptyempty($this->error))return false;
- return "
- <script language=javascript> rn var error = new Array(".trim($this->error,',').");
- rn for (i=0; i < error.length; i++){
- rn document.getElementById(error[0]).innerHTML=error[1];
- }rn </script>
- ";
- }
- }
-
-
- $form[1] =array(
- 'text'=>array('title','','产品名称','size=40','产品名称不可缺少!','Y','cn,1-30'),
- 'text1'=>array('categories','','产品名称','','','Y_base64'),
- 'select'=>array('superiors','||1|2|Y_3','产品类别|选择|1|2|3','','必选项','Y'),
- 'radio'=>array('superiors1','|1|Y_2|3','产品xun|产品1|产品2|产品3','','必选项','Y'),
- 'checkbox'=>array('superiors2',array(1=>'11',2=>'22',3=>'33'),'','','必选项','Y'),
- 'file'=>array('ddd','','文件'),
- );
- $form =array (
- 'login' =>
- array (
- 'text' =>
- array (
- 0 => 'user',
- 1 => '',
- 2 => '用户名',
- 3 => 'size=20',
- 4 => '!',
- 5 => 'Y',
- 6 => 'numen,6-12',
- ),
- 'password' =>
- array (
- 0 => 'pass',
- 1 => '',
- 2 => '密 码',
- 3 => 'size=22',
- 4 => '密码格式错误!',
- 5 => 'Y_md5',
- 6 => 'numen,6-12',
- ),
- 'radio' =>
- array (
- 0 => 'time',
- 1 => '|7200|3600|1800',
- 2 => 'cookies有效时间|2小时|1小时|30分钟',
- 3 => '',
- 4 => '',
- 5 => 'N_delete',
- 6 => '',
- ),
- ),
- );
-
-
- $past = $_form->postForm($form['login']);
- $dd = array('title'=>'标题','categories'=>'类别');
-
- if(!emptyempty($past))
- {
- echo "<pre>";
- print_r($past);
- echo"</pre>";
- }
- echo '<form method="POST" NAME="PostTopic" action="" enctype="multipart/form-data" style="margin:0px;">';
- echo $_form->formHtml($form['login'],$dd);
- echo '<input type="submit" value="Y" name="B1"></form>';
|
|
|
|