Open source system source code analysis (filter.class.php)

<?php

        class baseValidater

        {

              //Maximum number of parameters

const MAX_ARGS=3;

public static function checkBool($var)
{
  return filter_var($var,FILTER_VALIDATE_BOOLEAN);
}


public static function checkInt($var)
{
  $args=func_get_args();

  if($var!=0) $var=ltrim($var,0);

  if(isset($args[1]))
  {
    if(isset($args[2]))
    {
      $options=array('options'=>array('min_range'=>$args[1],'max_range'=>$args[2]));
    }else{
      $options=array('options'=>array('min_range'=>$args[1]));
    }

    return filter_var ($ var, FILTER_VALIDATE_INT, $ options);
  } else {
    return filter_var ($ var, FILTER_VALIDATE_INT);
  }
}


public static function checkNotInt ($ var)
{
  return! self :: checkInt ($ var);
}

public static function checkFloat($var,$decimal=''){
  return filter_var($var,FILTER_VALIDATE_FLOAT,array('options'=>array('decimal'=>$decimal)));
}

public static function checkEmail($var)
{
  return filter_var($var,FILTER_VALIDATE_EMAIL);
}

public static function checkPhone($var)
{
  return (validater::checkTel($var) or validater::checkMobile($var));
}

public static function checkTel($var)
{
  //? Match the atom before 0 or 1 This is the configuration is the fixed phone
  return preg_match("/^([0-9]{3,4}-?)?[0 -9]{7,8}$/",$var)
}


public static function checkMobile($var)
{
  return preg_match("/^1[3-5,7,8]{1}[0-9]{9}$/",$var);
}

 

public static function checkURL($var)

{

  return filter_var ($ var, FILTER_VALIDATE_URL);

}

 

public static function checkDomain($var)

{

  return preg_match('/^([a-z0-9-]+\.[a-z]{2,15})$/',$var);

}

 

public static function checkIP($var,$range='all')

{

  if($range=='all') return filter_var($var,FILTER_VALIDATE_IP);

       if($range=='public static')  return filter_var($var,FILTER_VALIDATE_IP,FILTER_FLAG_NO_PRIV_RANGE);

       if($range=='private')

  {

    if ($ var == '127.0.0.1' or filter_var ($ var, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANG) == false) return true

    return false

  }

}

//ID card

public  static function checkIdcard($idcard)

{

  if(strlen($idcard)!=18) return false;

  $idcard=strtoupper($idcard);

  $cityList=array(

      '11','12','13','14','15','21','22',
      '23','31','32','33','34','35','36',
      '37','41','42','43','44','45','46',
      '50','51','52','53','54','61','62',
      '63','64','65','71','81','82','91'

  );

  if(!preg_match('/^([\d]{17}[xX\d]|[\d]{15})$/')) return false;

  if(!in_array(substr($idcard,0,6),$cityList)) return false;

  $baseCode=substr($idcard,0,17);

  $verifyCode=substr($idcard,17,1);

  $interference=array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);

  $verifyConfig=array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');

  $total=0;

  for($i=0;$i<17;$i++) $total+=substr($baseCode,$i,1)*$interference[$i]';

  

  $mod=$total%11;

  return $verifyCode==$verifyConfig[$mod];

}

//substr(string,start,length) 

public static function checkDate($date)

{

  if($date=='0000-00-00') return true;

  $stamp=strtotime($date);

  if(!is_numeric($stamp)) return false;

  return checkdate(date('m',$stamp),date('d',$stamp),date('Y',$stamp));

}

// Check if some date is a valid Gregorian date: checkdate(month, day, year)

//date('m', timestamp)

 

public static function checkREG($var,$reg)

{

  return filter_var($var,FILTER_VALIDATE_REGEXP,array('options'=>array('regexp'=>$reg)));

}

// FILTER_VALIDATE_REGEXP regular expression

public static function checkLength($var,$max,$min=0)

{

  $length=function_exists('mb_strlen') ? mb_strlen($var,'utf-8'):strlen($var);

  return self::checkInt($length,$min,$max);

}

//Note: strlen and mb_strlen are functions to find the length of a string. When calculating strlen, the length of a UTF8 Chinese character is 3. When calculating mb_strlen, if the internal code is UTF8, a Chinese character will be treated as Calculated as length 1

public static function checkNotEmpty ($ var)

{

  return !empty($var);

}

//Note: Returns TRUE if the variable does not exist
// * If the variable exists and its value is "", 0, "0", NULL, , FALSE, array(), $var; and an object without any properties, then Returns TRUE
// * Returns FALSE if the variable exists and the value is not "", 0, "0", NULL, , FALSE, array(), $var; and an object without any properties

public static function checkEmpty($var)

{

  return empty($var);

}

 

public static function checkAccount($var)

{

  global $config;

  $accoutRule=empty($config->accountRule)?'|^[a-zA-Z0-9_]{1}[a-zA-Z0-9_\.]{1,}[a-zA-Z0-9_]{1}$|' : $config->accountRule;

  return self::checkREG($var,$accountRule);

}

public static function checkCode($var)

{

  return self::checkREG($var,'|^[A-Za-z0-9]+$|');

}

public static function checkCaptcha($var)

{

  if(!isset($_SESSION['captcha'])) return false;

  return $var==$_SESSION['captcha'];

}

public static function checkEqual($var,$value)

{

  return $var==$value;

}

public static function checkNoEqual($var,$value)

{

  return $var!=$value;

}

public static function checkGT($var,$value)

{

  return $var>$value;

}

 

public static function checkLT($var,$value)

{

  return $var<$value;

}

public static function checkGE($var,$value)

{

  return $var>=$value;

}

public static function checkLE($var,$value)

{

  return $var<=$value;

}

public static funciton checkIn($var,$value)

{

  if(!is_array($value)) $value=explode(',',$value);

  return in_array($var,$value);

}

public static function checkFileName($var)

{

  return !preg_match('/>+|:+|<+/',$var);

}

//Note: The preg_match() function is used for regular expression matching, and returns 1 if successful, otherwise it returns 0.

public static function checkSensitive($vars,$dicts)

{

  foreach ($ vars as $ var)

  {

    if(!$var) continue;

    foreach($dicts as $dict)

    {

      if(strpos($var,$dist)==false) continue;

      if(strpos($var,$dist)!==false) return false;

    }

  }

  return true;

}

//Note: The strpos() function finds the first occurrence of the string in another string, returns the first occurrence of the string in another string, and returns FALSE if the string is not found.

public static function filterFiles()

{

  global $config;

  if(empty($_FILES)) return $_FILES;

  foreach($_FILES as $varName=$files)

  {

    if(is_array($files['name']))

    {

      foreach($files['name'] as $i=>$filename)

      {

        $extension=ltrim(strrchr($filename,'.'),'.');

        if(stripos(",{$config->file->dangers},",",{$extension},")!==false)

        {

          unset($_FILES);

          return array();

        }

      }

    }else{

      $extension=ltrim(strrchr($files['name'],'.'),'.');

      if(stripos(",{$config->file-dangers},",",{$extension},")!==false)

      {

        unset($_FILES);

        return array();

      }

    }

  }

  return $_FILES;

}

//Note: strrchr searches for the position of a character in the string and returns all characters from that position to the end of the string

//strpos() function finds the first occurrence of a string in another string (case sensitive)

//stripos() - finds the first occurrence of a string within another string (case-insensitive)

//stripos() - finds the last occurrence of a string within another string (case-insensitive)

//strrpos() - Find the last occurrence of a string within another string (case sensitive)

 

public static function filterSuper($super)

{

  if(!is_array($supper)) return $supper;

  

  $super=self::filterBadKeys($super);

  foreach($super as $key => $item)

  {

    if(is_array($item))

    {

      $item=self::filterBadKeys($item);

      foreach($item as $subkey=>$subItem)

      {

        if(is_array($subItem)) continue;

        $subItem=self::filterTrojan($subItem);

        $super[$key][$subkey]=self::filterXSS($subItem);

      }

    }else{

      $item=self::filterTrojan($item);

      $super[$key][$subkey]=self::filterXSS($item);

    }

  }

  return $super;

}

 

public static function filterBadKeys($var)

{

  global $config;

  if(empty( $config->framework->filterBadKeys)) return $var;

  foreach($var as $key=>$value) if(preg_match('/[^a-zA-Z0-9_\.\-]/',$key)) unset($var[$key]);

  return $ var;

}

public static function filterXSS($var)

{

  global $config;

  if(empty($config->framework->filterXSS)) return $var;

  

  if(stripos($var,'<script')!==false)

  {

    $ var = (string) $ var;    

        $evils = array('appendchild(', 'createElement(', 'xss.re', 'onfocus', 'onclick', 'innerHTML', 'replaceChild(', 'html(', 'append(', 'appendTo(', 'prepend(', 'prependTo(', 'after(', 'insertBefore', 'before(', 'replaceWith(');
        $replaces = array('a p p e n d c h i l d (', 'c r e a t e E l e m e n t (', 'x s s . r e', 'o n f o c u s', 'o n c l i c k', 'i n n e r H T M L', 'r e p l a c e C h i l d (', 'h t m l (', 'a p p e n d (', 'a p p e n d T o (', 'p r e p e n d (', 'p r e p e n d T o (', 'a f t e r (', 'i n s e r t B e f o r e(', 'b e f o r e (', 'r e p l a c e W i t h (');
        $var = str_ireplace($evils, $replaces, $var);

  }

  $var = preg_replace('/j\s*a\s*v\s*a\s*s\s*c\s*r\s*i\s*p\s*t\s*:/Ui', 'j a v a s c r i p t :', $var);

  return $ var;

}

//注:str_ireplace(find,replace,string,count)

public static function filterParam($var,$type)

{

  global $config,$filter,$app;

  $moduleName=$app->getModuleName();

  $methodName=$app->getMethodName();

  $params=$app->getParams();

 

   if(type=='cookie')

  {

    $pagerCookie='pager'.ucfirst($moduleName).ucfirst($methodName);

    $filter->default->cookie[$pagerCookie]='int';

  }

 

  foreach($var as $key=>$value)

  {

    if($config->requestType=='GET' and $type=='get' and isset($params[$key])) continue;

 

    $rules='';

     if(isset($filter->{$moduleName}->{$methodName}->{$type}[$key]))

    {

      $rules=$filter->{$moduleName}->{$methodName}->{$type}[$key];

    }elseif(isset($filter->{$moduleName}->default->{$type}[$key]))

    {

      $rules=$filter->{$moduleName}->default->{$type}[$key];

    }else if(isset($filter->default->{$type}[$key]))

    {

      $rules=$filter->default->{$type}[$key];

    }

 

    if(!self::checkByRule($value,$rules)) unset($var[$key]);

  }

  return $ var;

}

public  static function checkByRule($var,$rule)

{

  if(empty($rule)) return false;

  list($operator,$param)=baseValidator::parseRuleString($rule);

 

  $checkMethod='check'.$operator;

  if(method_exists('baseValidater',$checkMethod))

  {

    if(empty($param) and self::$checkMethod($var)==false) return false;

    if(!empty($param) and self::$checkMethod($var,$param)===false) return false;

  }else if(function_exists('is_'.$operator))

  {

    $checkFunction='is_'.$operator;

    if(!$checkFunction($var)) return false;

  }else{

    return false;

  }

  return true;

}

public static function parseRuleString($rule)

{

  global $filter;

  

  if(strpos($rule,'::')!==false) list($operator,$param)=explode('::',$rule);

  if(strpos($rule,'::')===false) list($operator,$param)=array($rule,'');

  if($operator=='reg' and isset($filter->rules->param)) $param=$filter->rules->$param;

  

  return array($operator,$param);

}

//Note: The list() function is used to assign values ​​to a set of variables in one operation. list(var1,var2...)

public static function call($var,$func)

{

  return filter_var ($ var, FILTER_CALLBACK, array ('options' => $ func));

}

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325370809&siteId=291194637