php security filter function code

  • 2020-05-05 11:02:22
  • OfStack

 
// Secure filtered input [jb]
function check_str($string, $isurl = false)
{
$string = preg_replace('/[\\x00-\\x08\\x0B\\x0C\\x0E-\\x1F]/','',$string);
$string = str_replace(array("\0","%00","\r"),'',$string);
empty($isurl) && $string = preg_replace("/&(?!(#[0-9]+|[a-z]+);)/si",'&',$string);
$string = str_replace(array("%3C",'<'),'<',$string);
$string = str_replace(array("%3E",'>'),'>',$string);
$string = str_replace(array('"',"'","\t",' '),array(' " ',' ' ',' ',' '),$string);
return trim($string);
}


Here are some filters for you:


/**
* Security filter class - filter javascript,css,iframes,object And other unsafe parameters High filtration level
*  Controller Method of use: $this->controller->fliter_script($value)
* @param  string $value Values to be filtered
* @return string
*/
function fliter_script($value) {
$value = preg_replace("/(javascript:)?on(click|load|key|mouse|error|abort|move|unload|change|dblclick|move|reset|resize|submit)/i","&111n\\2",$value);
$value = preg_replace("/(.*?)<\/script>/si","",$value);
$value = preg_replace("/(.*?)<\/iframe>/si","",$value);
$value = preg_replace ("//iesU", '', $value);
return $value;
} /**
* Security filter class - filter HTML The label
*  Controller Method of use: $this->controller->fliter_html($value)
* @param  string $value Values to be filtered
* @return string
*/
function fliter_html($value) {
if (function_exists('htmlspecialchars')) return htmlspecialchars($value);
return str_replace(array("&", '"', "'", "<", ">"), array("&", "\"", "'", "<", ">"), $value);
} /**
* Security filter class - Underline the incoming data To prevent SQL injection
*  Controller Method of use: $this->controller->fliter_sql($value)
* @param  string $value Values to be filtered
* @return string
*/
function fliter_sql($value) {
$sql = array("select", 'insert', "update", "delete", "\'", "\/\*",
     "\.\.\/", "\.\/", "union", "into", "load_file", "outfile");
$sql_re = array("","","","","","","","","","","","");
return str_replace($sql, $sql_re, $value);
} /**
* Security filter class - General data filtering
*  Controller Method of use: $this->controller->fliter_escape($value)
* @param string $value Variables that need to be filtered
* @return string|array
*/
function fliter_escape($value) {
if (is_array($value)) {
  foreach ($value as $k => $v) {
   $value[$k] = self::fliter_str($v);
  }
} else {
  $value = self::fliter_str($value);
}
return $value;
} /**
* Security filter class - String filtering Filter special harmful characters
*  Controller Method of use: $this->controller->fliter_str($value)
* @param  string $value Values to be filtered
* @return string
*/
function fliter_str($value) {
$badstr = array("\0", "%00", "\r", '&', ' ', '"', "'", "<", ">", "   ", "%3C", "%3E");
$newstr = array('', '', '', '&', ' ', '"', ''', "<", ">", "   ", "<", ">");
$value  = str_replace($badstr, $newstr, $value);
$value  = preg_replace('/&((#(\d{3,5}|x[a-fA-F0-9]{4}));)/', '&\\1', $value);
return $value;
} /**
* Private road safety conversion
*  Controller Method of use: $this->controller->filter_dir($fileName)
* @param string $fileName
* @return string
*/
function filter_dir($fileName) {
$tmpname = strtolower($fileName);
$temp = array(':/',"\0", "..");
if (str_replace($temp, '', $tmpname) !== $tmpname) {
  return false;
}
return $fileName;
} /**
* Filter directory
*  Controller Method of use: $this->controller->filter_path($path)
* @param string $path
* @return array
*/
public function filter_path($path) {
$path = str_replace(array("'",'#','=','`','$','%','&',';'), '', $path);
return rtrim(preg_replace('/(\/){2,}|(\\\){1,}/', '/', $path), '/');
} /**
* filter PHP The label
*  Controller Method of use: $this->controller->filter_phptag($string)
* @param string $string
* @return string
*/
public function filter_phptag($string) {
return str_replace(array(''), array('<?', '?>'), $string);
} /**
* Security filter class - Returns the function
*  Controller Method of use: $this->controller->str_out($value)
* @param  string $value Values to be filtered
* @return string
*/
public function str_out($value) {
$badstr = array("<", ">", "%3C", "%3E");
$newstr = array("<", ">", "<", ">");
$value  = str_replace($newstr, $badstr, $value);
return stripslashes($value); // The underline
}


Related articles: