<?php

/**
 *      【TuntronJxt v4.0】 (C)2010-2012 Tuntron Inc.
 *      
 *    	http://www.tuntron.com   QQ：1121251729
 *		
 *      &检测||过滤函数文件&
 */

if (!defined('IS_RUN')) die('Forbidden');

//取得$_GET里的变量
function _get($var, $default = null, $convert_fun='') {
	Filter($_GET);
    if(isset($_GET[$var])) {
        if($convert_fun) return $convert_fun($_GET[$var]);
        return $_GET[$var];
    }
    return $default;
}
//取得$_POST里的变量
function _post($var, $default = null, $convert_fun='') {
	Filter($_POST);
    if(isset($_POST[$var])) {
        if($convert_fun) return $convert_fun($_POST[$var]);
        return $_POST[$var];
    }
	
    return $default;
}

//过滤SQL
function strip_sql($string) {
    $pattern_arr = array("/ union /i", "/ select /i", "/ update /i", "/ outfile /i", "/ or /i");
    $replace_arr = array('&nbsp;union&nbsp;', '&nbsp;select&nbsp;', '&nbsp;update&nbsp;','&nbsp;outfile&nbsp;', '&nbsp;or&nbsp;');
    return is_array($string) ? array_map('strip_sql', $string) : preg_replace($pattern_arr, $replace_arr, $string);
}

//过滤HTML，用于Text
function _T($string) {
    if(is_array($string))
        foreach($string as $key => $val) $string[$key] = _T($val); 
    else
        $string = is_string($string) ? htmlspecialchars(trim($string), ENT_QUOTES) : $string;
    $wu = array('&amp;copy;','&amp;reg;','&amp;trade;');
    $rp = array('&copy;','&reg;','&trade;');
    return str_replace($wu, $rp, $string);
}

//支持HTML格式,过滤可能引起安全问题的HTML标记
function _HTML($string) {
    $search_arr = array("/(javascript|jscript|js|vbscript|vbs|about):/i","/on(mouse|exit|error|click|dblclick|key|load|unload|change|move|submit|reset|cut|copy|select|start|stop)/i","/<script([^>]*)>/i","/<iframe([^>]*)>/i","/<frame([^>]*)>/i","/<link([^>]*)>/i","/@import/i");
    $replace_arr = array("\\1\n:","on\n\\1","&lt;script\\1&gt;","&lt;iframe\\1&gt;","&lt;frame\\1&gt;","&lt;link\\1&gt;","@\nimport");
    $string = preg_replace($search_arr, $replace_arr, $string);
    $string = str_replace("&#", "&\n#", $string);
    return $string;
}

//处理过滤
function Filter(&$array){
	if (is_array($array)) {        
		foreach ($array as $key => $value) {             
			if (!is_array($value)) {                 
				$array[$key] = addslashes($value);
			} else {
				Filter($array[$key]);
			} 
		}
	} 
}

?>