<?php
/*
URI 安全 严格限制 URI 中所能包含的字符，以此帮助你设计的程序减少被恶意数据入侵的可能。
URI 一般只包含下列内容： 字母和数字、波浪符： ~、句号： . 、冒号： : 、下划线： _ 、破折号： - 
*/
class Input
{
	var $get 	= array();
	var $post 	= array();
	var $req 	= array();
	//var $post 	= array();
	//var $reqval 	= array();
	//var $serverval 	= array();
	function __construct()
	{
		//var_dump($_POST);
		//parent::__construct();
		//var_dump($_GET);
		$this -> get  	= $this->del_get_extent($_GET);
		//var_dump($this->get);
		$this -> post  	= $_POST;
		//var_dump($this->post);
		$this -> req  = $_REQUEST;
		//unset($_GET);
		//unset($_POST);
		//unset($_REQUEST);
		
		//var_dump($this -> input['get']);
		//$this -> input['post'] 		= $this -> funReq($_POST);
		//$this -> input['request'] 	= $this -> funReq($_REQUEST);
	}
	//对输入$GET处理
	function del_get_extent(&$val,$length = 500)
	{
		//var_dump($val);
		if(is_array($val))
		{
			foreach( $val as $k => $v )
			{

				$val[$k] =  $this -> del_get_extent($v);
			}
		}else{
			 $delstr=array('*','^','%','$','#', '`','\'',"\"",'+','=','/','\\','&','!','?','<','>','(',')','[',']',' ', '\x0d','\x0a','\x20');
		     $val = str_replace($delstr,'',trim($val));
			 $val = substr($val,0,$length);
		}
		return  $val ;
	}

	function only_int($value){
		return (int)$value;
	
	}
	
	function del_post_extent(&$val,$length = 500,$del_arr =  array('~','*','^','%','$','#', '`',',','\'',"\"",'@','+','=','/','\\','&','!','?','<','>','(',')','[',']',' ', '\x0d','\x0a','\x20') )
	{
		//var_dump($val);
		if(is_array($val))
		{
			foreach( $val as $k => $v )
			{

				$val[$k] =  $this -> del_post_extent($v,$length,$del_arr);
			}
		}else{
			 //$del_arr = array('-','_','~','*','^','%','$','#', '`',',','\'',"\"",'@','+','=','/','\\','&','!','?','<','>','(',')','[',']',' ', '\x0d','\x0a','\x20');
		     $val = str_replace($del_arr,'',trim($val));
			 $val = substr($val,0,$length);
		}
		return  $val ;
	}

	function del_ext_post($key_str,$length = 500){
		return $this -> del_post_extent($this -> post[$key_str]);
	}

	function abbr_post( $keystr )
	{
		if($keystr)
		{
			return $this -> delExtstr( $_POST[$keystr] );
		}else{
			return false;
		}
	}
	
	function abbr_get( $keystr )
	{
		if($keystr)
		{
			return $this -> delExtstr( $_GET[$keystr] );
		}else{
			return false;
		}
	}
	function abbr_req( $keystr )
	{
		if($keystr)
		{
			return $this -> delExtstr( $_REQUEST[$keystr] );
		}else{
			return false;
		}
	}

	
	function funpost( $keystr = '' )
	{
		if($keystr)
		{
			if(is_array( $this -> post[$keystr]) )
			{
				return $this -> post[$keystr];
			}else{
			
				return trim( $this -> post[$keystr] );
			}
		}else{
			return false;
		}
	}
	
	
	
	function funReq( $getArray = array() )
	{
		//
		foreach( $getArray as $gkey => $gval )
		{
			$getArray[$gkey] = $this -> delExtstr($gval) ;
		}
		return $getArray;
	}
	
	function delExtstr(&$val)
	{
		//var_dump($val);
		if(is_array($val))
		{
			foreach( $val as $k => $v )
			{

				$val[$k] =  $this -> delExtstr($v);
			}
		}else{
			$val = $this -> delStrWord( $val );
		}
		//return  $val ;
	}

	//非法字符删除（适用keyword性）
	function delKeyword( $strVal )
	{
		$delstr=array('*','^','%','$', '`','~','\'',"\"",'+','\\','!','<','>','(',')','[',']',';', '\x0d','\x0a','\x20');
		return str_replace($delstr,'',trim($strVal));
	}

	//非法字符删除（适用URL地址性）
	function delUrl( $strVal )
	{
		$delstr=array('*','^','%','$', '`','\'',"\"",'+','\\','!','<','>','(',')','[',']',' ','&nbsp;',';', '\x0d','\x0a','\x20');
		return str_replace($delstr,'',trim($strVal));
	}
	
	//非法字符删除（适用与非文本）
	function delStrWord( $keyWord )
	{
		$delstr=array('*','^','%','$','#', '`','~',',','\'',"\"",'+','=','\\','!','?','<','>','(',')','[',']', '\x0d','\x0a','\x20');
		return str_replace($delstr,'',trim($keyWord));
	}
	
	//非法字符删除（用户登录适用）
	function delStrlogin( $keyWord )
	{
		 $delstr=array('*','^','%','$','#', '`','~',',','\'',"\"",'@','+','-','=','/','\\','_','&','!','?','<','>','(',')','[',']',' ', '\x0d','\x0a','\x20');
		 return str_replace($delstr,'',trim($keyWord)); 
	}
	
	function delart( $content )
	{
			
		$search  = array ("'<script[^>]*?>.*?</script>'si");  // 去掉 javascript
		$replace = array ("");
		$content = preg_replace ($search, $replace, $content);
	
		$search  = array ("'<iframe[^>]*?>.*?</iframe>'si");  // 去掉 iframe
		$replace = array ("");
		$content = preg_replace ($search, $replace, $content);
			
		
		return $content;		
	}
	
	
	function delContent( $content )
	{
		if($content !=''){
			 
			 
			$content=preg_replace('/<img\s+src=[\\\'| \\\"](.*?(?:[\.gif|\.jpg]))[\\\'|\\\"].*?[\/]?>/',"",$content);
			
			// 去掉 img 连接	
			$content = preg_replace("'<img[^>]*?>.*?</img>'si", "", $content);	
			
			// 去掉 javascript
			$content = preg_replace ("'<script[^>]*?>.*?</script>'si", "", $content);

			// 去掉 style css
			$content = preg_replace ("'<style[^>]*?>.*?</style>'si", "", $content);
				
			// 去掉 iframe
			$content = preg_replace ("'<iframe[^>]*?>.*?</iframe>'si", "", $content);
				
			// 去掉 ul 连接
			$content = preg_replace ("'<ul[^>]*?>.*?</ul>'si", "", $content);
			
			$content = preg_replace("/<a[^>]*href=[^>]*>|<\/[^a]*a[^>]*>/i","",$content);
			
			//$content = preg_replace( "@<a[^>]*>(.*)</a>@is","\$1",$content);
			}
		return $content;
	}
	
	//新策略函数
	
	//非法字符删除（适用一般性输入变量处理）
	function delStr( $value )
	{
		$delstr=array('*','^','%','$','#', '`','~',',','\'',"\"",'+','=','\\','!','?','<','>','(',')','[',']', '\x0d','\x0a','\x20');
		return str_replace($delstr,'',trim($value));
	}
	
	//('~','*','^','%','$','#', '`',',','\'',"\"",'@','+','=','/','\\','&','!','?','<','>','(',')','[',']',' ', '\x0d','\x0a','\x20')
	// |~|^|#|$|%|&|?|\[|\]
    function check_illegal_str($key_str){ 
        if(function_exists('preg_match')){
            return preg_match('/select|insert|and|or|update|delete|union|into|\'|\/\*|\*|\.\.\/|\.\/|load_file|outfile|\[|\]|\?|\~|\^|\#|\$|\%|\&/i', $this -> post[$key_str]);
        }else{
            return eregi('select|insert|and|or|update|delete|union|into|\'|\/\*|\*|\.\.\/|\.\/|load_file|outfile|\[|\]|\?|\~|\^|\#|\$|\%|\&', $this -> post[$key_str]);
        }
    } 
     
    function verify_id($id=null) { 
        if(!$id) {
            exit('没有提交参数！'); 
        }elseif(inject_check($id)) { 
            exit('提交的参数非法！');
        } elseif(!is_numeric($id)) { 
            exit('提交的参数非法！'); 
        } 
        $id = intval($id); 
         
        return $id; 
    } 
     
     
    function str_check( $str ) { 
        if(!get_magic_quotes_gpc()) { 
            $str = addslashes($str); // 进行过滤 
        } 
        $str = str_replace("_", "\_", $str); 
        $str = str_replace("%", "\%", $str); 
         
       return $str; 
    } 
     
     
    function post_check($post) { 
        if(!get_magic_quotes_gpc()) { 
            $post = addslashes($post);
        } 
        $post = str_replace("_", "\_", $post); 
        $post = str_replace("%", "\%", $post); 
        $post = nl2br($post); 
        $post = htmlspecialchars($post); 
         
        return $post; 
    }
	
	
	
	
	
	
}