* change for new framework.
This commit is contained in:
283
lib/filter/filter.class.php
Executable file → Normal file
283
lib/filter/filter.class.php
Executable file → Normal file
@@ -1,28 +1,32 @@
|
||||
<?php
|
||||
/**
|
||||
* ZenTaoPHP的验证和过滤类。
|
||||
* The validater and fixer class file of ZenTaoPHP framework.
|
||||
*
|
||||
* The author disclaims copyright to this source code. In place of
|
||||
* a legal notice, here is a blessing:
|
||||
*
|
||||
*
|
||||
* May you do good and not evil.
|
||||
* May you find forgiveness for yourself and forgive others.
|
||||
* May you share freely, never taking more than you give.
|
||||
*/
|
||||
|
||||
/**
|
||||
* The valida class, checking datas by rules.
|
||||
*
|
||||
* validater类,检查数据是否符合规则。
|
||||
* The validater class, checking data by rules.
|
||||
*
|
||||
* @package framework
|
||||
*/
|
||||
class validater
|
||||
{
|
||||
/**
|
||||
* 最大参数个数。
|
||||
* The max count of args.
|
||||
*/
|
||||
const MAX_ARGS = 3;
|
||||
|
||||
/**
|
||||
* 是否是Bool类型。
|
||||
* Bool checking.
|
||||
*
|
||||
* @param bool $var
|
||||
@@ -36,6 +40,7 @@ class validater
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否是Int类型。
|
||||
* Int checking.
|
||||
*
|
||||
* @param int $var
|
||||
@@ -46,12 +51,13 @@ class validater
|
||||
public static function checkInt($var)
|
||||
{
|
||||
$args = func_get_args();
|
||||
if($var != 0) $var = ltrim($var, 0); // Remove the left 0, filter don't think 00 is an int.
|
||||
if($var != 0) $var = ltrim($var, 0); // 去掉变量左边的0,00不是Int类型
|
||||
// Remove the left 0, filter don't think 00 is an int.
|
||||
|
||||
/* Min is setted. */
|
||||
/* 如果设置了最小的整数。 Min is setted. */
|
||||
if(isset($args[1]))
|
||||
{
|
||||
/* And Max is setted. */
|
||||
/* 如果最大的整数也设置了。 And Max is setted. */
|
||||
if(isset($args[2]))
|
||||
{
|
||||
$options = array('options' => array('min_range' => $args[1], 'max_range' => $args[2]));
|
||||
@@ -70,6 +76,21 @@ class validater
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查不是Int类型。
|
||||
* Not int checking.
|
||||
*
|
||||
* @param int $var
|
||||
* @static
|
||||
* @access public
|
||||
* @return bool
|
||||
*/
|
||||
public static function checkNotInt($var)
|
||||
{
|
||||
return !self::checkInt($var);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查Float类型。
|
||||
* Float checking.
|
||||
*
|
||||
* @param float $var
|
||||
@@ -80,10 +101,11 @@ class validater
|
||||
*/
|
||||
public static function checkFloat($var, $decimal = '.')
|
||||
{
|
||||
return filter_var($var, FILTER_VALIDATE_FLOAT, array('options' => array('decimail' => $decimal)));
|
||||
return filter_var($var, FILTER_VALIDATE_FLOAT, array('options' => array('decimal' => $decimal)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查Email。
|
||||
* Email checking.
|
||||
*
|
||||
* @param string $var
|
||||
@@ -97,8 +119,52 @@ class validater
|
||||
}
|
||||
|
||||
/**
|
||||
* URL checking.
|
||||
* 检查电话或手机号码
|
||||
* Check phone number.
|
||||
*
|
||||
* @param string $var
|
||||
* @static
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public static function checkPhone($var)
|
||||
{
|
||||
return (validater::checkTel($var) or validater::checkMobile($var));
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查电话号码
|
||||
* Check tel number.
|
||||
*
|
||||
* @param int $var
|
||||
* @static
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public static function checkTel($var)
|
||||
{
|
||||
return preg_match("/^([0-9]{3,4}-)?[0-9]{7,8}$/", $var);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查手机号码
|
||||
* Check mobile number.
|
||||
*
|
||||
* @param string $var
|
||||
* @static
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public static function checkMobile($var)
|
||||
{
|
||||
return preg_match("/^1[3-5,8]{1}[0-9]{9}$/", $var);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查网址。
|
||||
* 该规则不支持中文字符的网址。
|
||||
*
|
||||
* URL checking.
|
||||
* The check rule of filter don't support chinese.
|
||||
*
|
||||
* @param string $var
|
||||
@@ -112,6 +178,23 @@ class validater
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查域名,不支持中文。
|
||||
* Domain checking.
|
||||
*
|
||||
* The check rule of filter don't support chinese.
|
||||
*
|
||||
* @param string $var
|
||||
* @static
|
||||
* @access public
|
||||
* @return bool
|
||||
*/
|
||||
public static function checkDomain($var)
|
||||
{
|
||||
return preg_match('/^([a-z0-9-]+\.)+[a-z]{2,15}$/', $var);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查IP地址。
|
||||
* IP checking.
|
||||
*
|
||||
* @param ip $var
|
||||
@@ -132,6 +215,7 @@ class validater
|
||||
}
|
||||
|
||||
/**
|
||||
* 日期检查。注意,2009-09-31是一个合法日期,系统会将它转换为2009-10-01。
|
||||
* Date checking. Note: 2009-09-31 will be an valid date, because strtotime auto fixed it to 10-01.
|
||||
*
|
||||
* @param date $date
|
||||
@@ -148,6 +232,7 @@ class validater
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查正则表达式。
|
||||
* REG checking.
|
||||
*
|
||||
* @param string $var
|
||||
@@ -160,8 +245,9 @@ class validater
|
||||
{
|
||||
return filter_var($var, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => $reg)));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 检查长度。
|
||||
* Length checking.
|
||||
*
|
||||
* @param string $var
|
||||
@@ -178,6 +264,7 @@ class validater
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查不为空。
|
||||
* Not empty checking.
|
||||
*
|
||||
* @param mixed $var
|
||||
@@ -191,6 +278,7 @@ class validater
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查为空。
|
||||
* Empty checking.
|
||||
*
|
||||
* @param mixed $var
|
||||
@@ -204,6 +292,7 @@ class validater
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查用户名。
|
||||
* Account checking.
|
||||
*
|
||||
* @param string $var
|
||||
@@ -219,6 +308,21 @@ class validater
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查Code。
|
||||
* Check code.
|
||||
*
|
||||
* @param string $var
|
||||
* @static
|
||||
* @access public
|
||||
* @return bool
|
||||
*/
|
||||
public static function checkCode($var)
|
||||
{
|
||||
return self::checkREG($var, '|^[A-Za-z0-9]+$|');
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查验证码。
|
||||
* Check captcha.
|
||||
*
|
||||
* @param mixed $var
|
||||
@@ -233,6 +337,7 @@ class validater
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否等于给定的值。
|
||||
* Must equal a value.
|
||||
*
|
||||
* @param mixed $var
|
||||
@@ -247,6 +352,22 @@ class validater
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查不等于给定的值
|
||||
* Must not equal a value.
|
||||
*
|
||||
* @param mixed $var
|
||||
* @param mixed $value
|
||||
* @static
|
||||
* @access public
|
||||
* @return bool
|
||||
*/
|
||||
public static function checkNotEqual($var, $value)
|
||||
{
|
||||
return $var != $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查大于给定的值。
|
||||
* Must greater than a value.
|
||||
*
|
||||
* @param mixed $var
|
||||
@@ -261,6 +382,7 @@ class validater
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查小于给定的值
|
||||
* Must less than a value.
|
||||
*
|
||||
* @param mixed $var
|
||||
@@ -275,6 +397,7 @@ class validater
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查大于等于给定的值
|
||||
* Must greater than a value or equal a value.
|
||||
*
|
||||
* @param mixed $var
|
||||
@@ -289,6 +412,7 @@ class validater
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查小于等于给定的值
|
||||
* Must less than a value or equal a value.
|
||||
*
|
||||
* @param mixed $var
|
||||
@@ -303,6 +427,7 @@ class validater
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否在给定的列表里面。
|
||||
* Must in value list.
|
||||
*
|
||||
* @param mixed $var
|
||||
@@ -316,8 +441,9 @@ class validater
|
||||
if(!is_array($value)) $value = explode(',', $value);
|
||||
return in_array($var, $value);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 检查文件名。
|
||||
* Check file name.
|
||||
*
|
||||
* @param string $var
|
||||
@@ -331,6 +457,31 @@ class validater
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查敏感词。
|
||||
* Check sensitive words.
|
||||
*
|
||||
* @param object $vars
|
||||
* @param array $dicts
|
||||
* @static
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public static function checkSensitive($vars, $dicts)
|
||||
{
|
||||
foreach($vars as $var)
|
||||
{
|
||||
if(!$var) continue;
|
||||
foreach($dicts as $dict)
|
||||
{
|
||||
if(strpos($var, $dict) === false) continue;
|
||||
if(strpos($var, $dict) !== false) return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 调用一个方法进行检查。
|
||||
* Call a function to check it.
|
||||
*
|
||||
* @param mixed $var
|
||||
@@ -346,6 +497,7 @@ class validater
|
||||
}
|
||||
|
||||
/**
|
||||
* fixer类,处理数据。
|
||||
* fixer class, to fix data types.
|
||||
*
|
||||
* @package framework
|
||||
@@ -353,6 +505,7 @@ class validater
|
||||
class fixer
|
||||
{
|
||||
/**
|
||||
* 处理的数据。
|
||||
* The data to be fixed.
|
||||
*
|
||||
* @var ojbect
|
||||
@@ -360,8 +513,17 @@ class fixer
|
||||
*/
|
||||
private $data;
|
||||
|
||||
private $stripedFields = array();
|
||||
/**
|
||||
* 跳过处理的字段。
|
||||
* The fields to striped.
|
||||
*
|
||||
* @var array
|
||||
* @access private
|
||||
*/
|
||||
private $stripedFields = array();
|
||||
|
||||
/**
|
||||
* 构造方法,将超级全局变量转换为对象。
|
||||
* The construction function, according the scope, convert it to object.
|
||||
*
|
||||
* @param string $scope the scope of the var, should be post|get|server|session|cookie|env
|
||||
@@ -370,36 +532,37 @@ class fixer
|
||||
*/
|
||||
private function __construct($scope)
|
||||
{
|
||||
switch($scope)
|
||||
{
|
||||
case 'post':
|
||||
$this->data = (object)$_POST;
|
||||
break;
|
||||
case 'server':
|
||||
$this->data = (object)$_SERVER;
|
||||
break;
|
||||
case 'get':
|
||||
$this->data = (object)$_GET;
|
||||
break;
|
||||
case 'session':
|
||||
$this->data = (object)$_SESSION;
|
||||
break;
|
||||
case 'cookie':
|
||||
$this->data = (object)$_COOKIE;
|
||||
break;
|
||||
case 'env':
|
||||
$this->data = (object)$_ENV;
|
||||
break;
|
||||
case 'file':
|
||||
$this->data = (object)$_FILES;
|
||||
break;
|
||||
switch($scope)
|
||||
{
|
||||
case 'post':
|
||||
$this->data = (object)$_POST;
|
||||
break;
|
||||
case 'server':
|
||||
$this->data = (object)$_SERVER;
|
||||
break;
|
||||
case 'get':
|
||||
$this->data = (object)$_GET;
|
||||
break;
|
||||
case 'session':
|
||||
$this->data = (object)$_SESSION;
|
||||
break;
|
||||
case 'cookie':
|
||||
$this->data = (object)$_COOKIE;
|
||||
break;
|
||||
case 'env':
|
||||
$this->data = (object)$_ENV;
|
||||
break;
|
||||
case 'file':
|
||||
$this->data = (object)$_FILES;
|
||||
break;
|
||||
|
||||
default:
|
||||
die('scope not supported, should be post|get|server|session|cookie|env');
|
||||
}
|
||||
default:
|
||||
die('scope not supported, should be post|get|server|session|cookie|env');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 工厂方法。
|
||||
* The factory function.
|
||||
*
|
||||
* @param string $scope
|
||||
@@ -412,6 +575,7 @@ class fixer
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理Email。
|
||||
* Email fix.
|
||||
*
|
||||
* @param string $fieldName
|
||||
@@ -426,7 +590,8 @@ class fixer
|
||||
}
|
||||
|
||||
/**
|
||||
* urlenocde.
|
||||
* url编码。
|
||||
* urlencode.
|
||||
*
|
||||
* @param string $fieldName
|
||||
* @access public
|
||||
@@ -444,6 +609,7 @@ class fixer
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理网址。
|
||||
* Clean the url.
|
||||
*
|
||||
* @param string $fieldName
|
||||
@@ -458,6 +624,7 @@ class fixer
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理Float类型。
|
||||
* Float fixer.
|
||||
*
|
||||
* @param string $fieldName
|
||||
@@ -472,6 +639,7 @@ class fixer
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理Int类型。
|
||||
* Int fixer.
|
||||
*
|
||||
* @param string $fieldName
|
||||
@@ -486,7 +654,8 @@ class fixer
|
||||
}
|
||||
|
||||
/**
|
||||
* Special chars
|
||||
* 将字符串转换为可以在浏览器查看的编码。
|
||||
* Special chars.
|
||||
*
|
||||
* @param string $fieldName
|
||||
* @access public
|
||||
@@ -519,10 +688,11 @@ class fixer
|
||||
}
|
||||
|
||||
/**
|
||||
* Strip tags
|
||||
*
|
||||
* @param string $fieldName
|
||||
* @param string $allowedTags
|
||||
* 忽略该标签。
|
||||
* Strip tags
|
||||
*
|
||||
* @param string $fieldName
|
||||
* @param string $allowableTags
|
||||
* @access public
|
||||
* @return object fixer object
|
||||
*/
|
||||
@@ -545,14 +715,18 @@ class fixer
|
||||
{
|
||||
if(version_compare(phpversion(), '5.4', '<') and get_magic_quotes_gpc()) $this->data->$fieldName = stripslashes($this->data->$fieldName);
|
||||
|
||||
if(!in_array($fieldName, $this->stripedFields)) $this->data->$fieldName = $purifier->purify($this->data->$fieldName);
|
||||
if(!in_array($fieldName, $this->stripedFields))
|
||||
{
|
||||
if(!defined('RUN_MODE') or RUN_MODE != 'admin') $this->data->$fieldName = $purifier->purify($this->data->$fieldName);
|
||||
}
|
||||
$this->stripedFields[] = $fieldName;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Skip special chars.
|
||||
* 忽略处理给定的字段。
|
||||
* Skip special chars check.
|
||||
*
|
||||
* @param string $filename
|
||||
* @access public
|
||||
@@ -566,6 +740,7 @@ class fixer
|
||||
}
|
||||
|
||||
/**
|
||||
* 给字段添加引用,防止字符与关键字冲突。
|
||||
* Quote
|
||||
*
|
||||
* @param string $fieldName
|
||||
@@ -580,6 +755,7 @@ class fixer
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置字段的默认值。
|
||||
* Set default value of some fileds.
|
||||
*
|
||||
* @param string $fields
|
||||
@@ -595,6 +771,7 @@ class fixer
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果条件为真,则为字段赋值。
|
||||
* Set value of a filed on the condition is true.
|
||||
*
|
||||
* @param bool $condition
|
||||
@@ -610,6 +787,7 @@ class fixer
|
||||
}
|
||||
|
||||
/**
|
||||
* 强制给字段赋值。
|
||||
* Set the value of a filed in force.
|
||||
*
|
||||
* @param string $fieldName
|
||||
@@ -624,6 +802,7 @@ class fixer
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除一个字段。
|
||||
* Remove a field.
|
||||
*
|
||||
* @param string $fieldName
|
||||
@@ -638,6 +817,7 @@ class fixer
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果条件为真,移除该字段。
|
||||
* Remove a filed on the condition is true.
|
||||
*
|
||||
* @param bool $condition
|
||||
@@ -653,6 +833,7 @@ class fixer
|
||||
}
|
||||
|
||||
/**
|
||||
* 为数据添加新的项。
|
||||
* Add an item to the data.
|
||||
*
|
||||
* @param string $fieldName
|
||||
@@ -667,6 +848,7 @@ class fixer
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果条件为真,则为数据添加新的项。
|
||||
* Add an item to the data on the condition if true.
|
||||
*
|
||||
* @param bool $condition
|
||||
@@ -682,6 +864,7 @@ class fixer
|
||||
}
|
||||
|
||||
/**
|
||||
* 为指定字段增加值。
|
||||
* Join the field.
|
||||
*
|
||||
* @param string $fieldName
|
||||
@@ -697,6 +880,7 @@ class fixer
|
||||
}
|
||||
|
||||
/**
|
||||
* 调用一个方法来处理数据。
|
||||
* Call a function to fix it.
|
||||
*
|
||||
* @param string $fieldName
|
||||
@@ -712,14 +896,12 @@ class fixer
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理完成后返回数据。
|
||||
* Get the data after fixing.
|
||||
*
|
||||
* If only one field, return it's value directly.
|
||||
* More fields, remove other fields not in the list and return $data.
|
||||
*
|
||||
* @param string $fields the fields list.
|
||||
* @param string $fieldName
|
||||
* @access public
|
||||
* @return mix
|
||||
* @return object
|
||||
*/
|
||||
public function get($fields = '')
|
||||
{
|
||||
@@ -740,6 +922,7 @@ class fixer
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理字段,如果字段中含有',',拆分成数组。如果字段不在$data中,删除掉。
|
||||
* Process fields, if contains ',', split it to array. If not in $data, remove it.
|
||||
*
|
||||
* @param string $fields
|
||||
|
||||
Reference in New Issue
Block a user