* change for lib can extend with base.

This commit is contained in:
wangyidong
2016-03-10 09:30:15 +08:00
parent e617014b65
commit dffd808378
8 changed files with 4765 additions and 4633 deletions
File diff suppressed because it is too large Load Diff
+952
View File
@@ -0,0 +1,952 @@
<?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.
*/
/**
* validater类,检查数据是否符合规则。
* The validater class, checking data by rules.
*
* @package framework
*/
class baseValidater
{
/**
* 最大参数个数。
* The max count of args.
*/
const MAX_ARGS = 3;
/**
* 是否是Bool类型。
* Bool checking.
*
* @param bool $var
* @static
* @access public
* @return bool
*/
public static function checkBool($var)
{
return filter_var($var, FILTER_VALIDATE_BOOLEAN);
}
/**
* 是否是Int类型。
* Int checking.
*
* @param int $var
* @static
* @access public
* @return bool
*/
public static function checkInt($var)
{
$args = func_get_args();
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. */
if(isset($args[1]))
{
/* 如果最大的整数也设置了。 And Max is setted. */
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);
}
}
/**
* 检查不是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
* @param string $decimal
* @static
* @access public
* @return bool
*/
public static function checkFloat($var, $decimal = '.')
{
return filter_var($var, FILTER_VALIDATE_FLOAT, array('options' => array('decimal' => $decimal)));
}
/**
* 检查Email。
* Email checking.
*
* @param string $var
* @static
* @access public
* @return bool
*/
public static function checkEmail($var)
{
return filter_var($var, FILTER_VALIDATE_EMAIL);
}
/**
* 检查电话或手机号码
* 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
* @static
* @access public
* @return bool
*/
public static function checkURL($var)
{
return filter_var($var, FILTER_VALIDATE_URL);
}
/**
* 检查域名,不支持中文。
* 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
* @param string $range all|public|static|public
* @static
* @access public
* @return bool
*/
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 == 'public')
{
if($var == '127.0.0.1' or filter_var($var, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE) === false) return true;
return false;
}
}
/**
* 日期检查。注意,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
* @static
* @access public
* @return bool
*/
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));
}
/**
* 检查正则表达式。
* REG checking.
*
* @param string $var
* @param string $reg
* @static
* @access public
* @return bool
*/
public static function checkREG($var, $reg)
{
return filter_var($var, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => $reg)));
}
/**
* 检查长度。
* Length checking.
*
* @param string $var
* @param string $max
* @param int $min
* @static
* @access public
* @return bool
*/
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);
}
/**
* 检查不为空。
* Not empty checking.
*
* @param mixed $var
* @static
* @access public
* @return bool
*/
public static function checkNotEmpty($var)
{
return !empty($var);
}
/**
* 检查为空。
* Empty checking.
*
* @param mixed $var
* @static
* @access public
* @return bool
*/
public static function checkEmpty($var)
{
return empty($var);
}
/**
* 检查用户名。
* Account checking.
*
* @param string $var
* @static
* @access public
* @return bool
*/
public static function checkAccount($var)
{
global $config;
$accountRule = 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);
}
/**
* 检查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
* @static
* @access public
* @return bool
*/
public static function checkCaptcha($var)
{
if(!isset($_SESSION['captcha'])) return false;
return $var == $_SESSION['captcha'];
}
/**
* 是否等于给定的值。
* Must equal a value.
*
* @param mixed $var
* @param mixed $value
* @static
* @access public
* @return bool
*/
public static function checkEqual($var, $value)
{
return $var == $value;
}
/**
* 检查不等于给定的值
* 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
* @param mixed $value
* @static
* @access public
* @return bool
*/
public static function checkGT($var, $value)
{
return $var > $value;
}
/**
* 检查小于给定的值
* Must less than a value.
*
* @param mixed $var
* @param mixed $value
* @static
* @access public
* @return bool
*/
public static function checkLT($var, $value)
{
return $var < $value;
}
/**
* 检查大于等于给定的值
* Must greater than a value or equal a value.
*
* @param mixed $var
* @param mixed $value
* @static
* @access public
* @return bool
*/
public static function checkGE($var, $value)
{
return $var >= $value;
}
/**
* 检查小于等于给定的值
* Must less than a value or equal a value.
*
* @param mixed $var
* @param mixed $value
* @static
* @access public
* @return bool
*/
public static function checkLE($var, $value)
{
return $var <= $value;
}
/**
* 检查是否在给定的列表里面。
* Must in value list.
*
* @param mixed $var
* @param mixed $value
* @static
* @access public
* @return bool
*/
public static function checkIn($var, $value)
{
if(!is_array($value)) $value = explode(',', $value);
return in_array($var, $value);
}
/**
* 检查文件名。
* Check file name.
*
* @param string $var
* @static
* @access public
* @return bool
*/
public static function checkFileName($var)
{
return !preg_match('/>+|:+|<+/', $var);
}
/**
* 检查敏感词。
* 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
* @param string $func
* @static
* @access public
* @return bool
*/
public static function call($var, $func)
{
return filter_var($var, FILTER_CALLBACK, array('options' => $func));
}
}
/**
* fixer类,处理数据。
* fixer class, to fix data types.
*
* @package framework
*/
class baseFixer
{
/**
* 处理的数据。
* The data to be fixed.
*
* @var ojbect
* @access public
*/
public $data;
/**
* 跳过处理的字段。
* The fields to striped.
*
* @var array
* @access public
*/
public $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
* @access public
* @return void
*/
public 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;
default:
die('scope not supported, should be post|get|server|session|cookie|env');
}
}
/**
* 工厂方法。
* The factory function.
*
* @param string $scope
* @access public
* @return object fixer object.
*/
public static function input($scope)
{
return new fixer($scope);
}
/**
* 处理Email。
* Email fix.
*
* @param string $fieldName
* @access public
* @return object fixer object.
*/
public function cleanEmail($fieldName)
{
$fields = $this->processFields($fieldName);
foreach($fields as $fieldName) $this->data->$fieldName = filter_var($this->data->$fieldName, FILTER_SANITIZE_EMAIL);
return $this;
}
/**
* url编码。
* urlencode.
*
* @param string $fieldName
* @access public
* @return object fixer object.
*/
public function encodeURL($fieldName)
{
$fields = $this->processFields($fieldName);
$args = func_get_args();
foreach($fields as $fieldName)
{
$this->data->$fieldName = isset($args[1]) ? filter_var($this->data->$fieldName, FILTER_SANITIZE_ENCODED, $args[1]) : filter_var($this->data->$fieldName, FILTER_SANITIZE_ENCODED);
}
return $this;
}
/**
* 清理网址。
* Clean the url.
*
* @param string $fieldName
* @access public
* @return object fixer object.
*/
public function cleanURL($fieldName)
{
$fields = $this->processFields($fieldName);
foreach($fields as $fieldName) $this->data->$fieldName = filter_var($this->data->$fieldName, FILTER_SANITIZE_URL);
return $this;
}
/**
* 处理Float类型。
* Float fixer.
*
* @param string $fieldName
* @access public
* @return object fixer object.
*/
public function cleanFloat($fieldName)
{
$fields = $this->processFields($fieldName);
foreach($fields as $fieldName) $this->data->$fieldName = filter_var($this->data->$fieldName, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION|FILTER_FLAG_ALLOW_THOUSAND);
return $this;
}
/**
* 处理Int类型。
* Int fixer.
*
* @param string $fieldName
* @access public
* @return object fixer object.
*/
public function cleanINT($fieldName = '')
{
$fields = $this->processFields($fieldName);
foreach($fields as $fieldName) $this->data->$fieldName = filter_var($this->data->$fieldName, FILTER_SANITIZE_NUMBER_INT);
return $this;
}
/**
* 将字符串转换为可以在浏览器查看的编码。
* Special chars.
*
* @param string $fieldName
* @access public
* @return object fixer object
*/
public function specialChars($fieldName)
{
$fields = $this->processFields($fieldName);
foreach($fields as $fieldName)
{
if(empty($this->stripedFields) or !in_array($fieldName, $this->stripedFields)) $this->data->$fieldName = $this->specialArray($this->data->$fieldName);
}
return $this;
}
/**
* Special array
*
* @param mix $data
* @access public
* @return mix
*/
public function specialArray($data)
{
if(!is_array($data)) return htmlspecialchars($data, ENT_QUOTES);
foreach($data as &$value) $value = $this->specialArray($value);
return $data;
}
/**
* 忽略该标签。
* Strip tags
*
* @param string $fieldName
* @param string $allowableTags
* @access public
* @return object fixer object
*/
public function stripTags($fieldName, $allowedTags = '')
{
global $app, $config;
if(empty($allowedTags) and isset($config->allowedTags)) $allowedTags = $config->allowedTags;
$usePurifier = isset($config->framework->purifier) ? $config->framework->purifier : false;
if($usePurifier)
{
$app->loadClass('purifier', true);
$purifierConfig = HTMLPurifier_Config::createDefault();
$purifierConfig->set('Filter.YouTube', 1);
/* Disable caching. */
$purifierConfig->set('Cache.DefinitionImpl', null);
$purifier = new HTMLPurifier($purifierConfig);
$def = $purifierConfig->getHTMLDefinition(true);
$def->addAttribute('a', 'target', 'Enum#_blank,_self,_target,_top');
}
$fields = $this->processFields($fieldName);
foreach($fields as $fieldName)
{
if(version_compare(phpversion(), '5.4', '<') and get_magic_quotes_gpc()) $this->data->$fieldName = stripslashes($this->data->$fieldName);
if(!in_array($fieldName, $this->stripedFields))
{
if(!defined('RUN_MODE') or RUN_MODE != 'admin')
{
/*
* purifier会把&nbsp;替换空格,kindeditor在会吧行首的空格去掉。
* purifier will change &nbsp; to ' ', and edit it will no space in line head use kindeditor.
**/
if($usePurifier) $this->data->$fieldName = str_replace('&nbsp;', '&spnb;', $this->data->$fieldName);
$this->data->$fieldName = $usePurifier ? $purifier->purify($this->data->$fieldName) : strip_tags($this->data->$fieldName, $allowedTags);
if($usePurifier) $this->data->$fieldName = str_replace('&amp;spnb;', '&nbsp;', $this->data->$fieldName);
}
}
$this->stripedFields[] = $fieldName;
}
return $this;
}
/**
* 忽略处理给定的字段。
* Skip special chars check.
*
* @param string $filename
* @access public
* @return object fixer object
*/
public function skipSpecial($fieldName)
{
$fields = $this->processFields($fieldName);
foreach($fields as $fieldName) $this->stripedFields[] = $fieldName;
return $this;
}
/**
* 给字段添加引用,防止字符与关键字冲突。
* Quote
*
* @param string $fieldName
* @access public
* @return object fixer object
*/
public function quote($fieldName)
{
$fields = $this->processFields($fieldName);
foreach($fields as $fieldName) $this->data->$fieldName = filter_var($this->data->$fieldName, FILTER_SANITIZE_MAGIC_QUOTES);
return $this;
}
/**
* 设置字段的默认值。
* Set default value of some fileds.
*
* @param string $fields
* @param mixed $value
* @access public
* @return object fixer object
*/
public function setDefault($fields, $value)
{
$fields = strpos($fields, ',') ? explode(',', str_replace(' ', '', $fields)) : array($fields);
foreach($fields as $fieldName)if(!isset($this->data->$fieldName) or empty($this->data->$fieldName)) $this->data->$fieldName = $value;
return $this;
}
/**
* 如果条件为真,则为字段赋值。
* Set value of a filed on the condition is true.
*
* @param bool $condition
* @param string $fieldName
* @param string $value
* @access public
* @return object fixer object
*/
public function setIF($condition, $fieldName, $value)
{
if($condition) $this->data->$fieldName = $value;
return $this;
}
/**
* 强制给字段赋值。
* Set the value of a filed in force.
*
* @param string $fieldName
* @param mixed $value
* @access public
* @return object fixer object
*/
public function setForce($fieldName, $value)
{
$this->data->$fieldName = $value;
return $this;
}
/**
* 移除一个字段。
* Remove a field.
*
* @param string $fieldName
* @access public
* @return object fixer object
*/
public function remove($fieldName)
{
$fields = $this->processFields($fieldName);
foreach($fields as $fieldName) unset($this->data->$fieldName);
return $this;
}
/**
* 如果条件为真,移除该字段。
* Remove a filed on the condition is true.
*
* @param bool $condition
* @param string $fields
* @access public
* @return object fixer object
*/
public function removeIF($condition, $fields)
{
$fields = $this->processFields($fields);
if($condition) foreach($fields as $fieldName) unset($this->data->$fieldName);
return $this;
}
/**
* 为数据添加新的项。
* Add an item to the data.
*
* @param string $fieldName
* @param mixed $value
* @access public
* @return object fixer object
*/
public function add($fieldName, $value)
{
$this->data->$fieldName = $value;
return $this;
}
/**
* 如果条件为真,则为数据添加新的项。
* Add an item to the data on the condition if true.
*
* @param bool $condition
* @param string $fieldName
* @param mixed $value
* @access public
* @return object fixer object
*/
public function addIF($condition, $fieldName, $value)
{
if($condition) $this->data->$fieldName = $value;
return $this;
}
/**
* 为指定字段增加值。
* Join the field.
*
* @param string $fieldName
* @param string $value
* @access public
* @return object fixer object
*/
public function join($fieldName, $value)
{
if(!isset($this->data->$fieldName) or !is_array($this->data->$fieldName)) return $this;
$this->data->$fieldName = join($value, $this->data->$fieldName);
return $this;
}
/**
* 调用一个方法来处理数据。
* Call a function to fix it.
*
* @param string $fieldName
* @param string $func
* @access public
* @return object fixer object
*/
public function callFunc($fieldName, $func)
{
$fields = $this->processFields($fieldName);
foreach($fields as $fieldName) $this->data->$fieldName = filter_var($this->data->$fieldName, FILTER_CALLBACK, array('options' => $func));
return $this;
}
/**
* 处理完成后返回数据。
* Get the data after fixing.
*
* @param string $fieldName
* @access public
* @return object
*/
public function get($fields = '')
{
$fields = str_replace(' ', '', trim($fields));
foreach($this->data as $field => $value) $this->specialChars($field);
if(empty($fields)) return $this->data;
if(strpos($fields, ',') === false) return $this->data->$fields;
$fields = array_flip(explode(',', $fields));
foreach($this->data as $field => $value)
{
if(!isset($fields[$field])) unset($this->data->$field);
if(!in_array($field, $this->stripedFields)) $this->data->$field = $this->specialChars($this->data->field);
}
return $this->data;
}
/**
* 处理字段,如果字段中含有',',拆分成数组。如果字段不在$data中,删除掉。
* Process fields, if contains ',', split it to array. If not in $data, remove it.
*
* @param string $fields
* @access public
* @return array
*/
public function processFields($fields)
{
$fields = strpos($fields, ',') ? explode(',', str_replace(' ', '', $fields)) : array($fields);
foreach($fields as $key => $fieldName) if(!isset($this->data->$fieldName)) unset($fields[$key]);
return $fields;
}
}
File diff suppressed because it is too large Load Diff
+620
View File
@@ -0,0 +1,620 @@
<?php
/**
* ZenTaoPHP的分页类。
* The pager 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.
*/
/**
* pager类.
* Pager class.
*
* @package framework
*/
class basePager
{
/**
* 每页的默认显示记录数。
* The default counts of per page.
*
* @public int
*/
const DEFAULT_REC_PER_PAGE = 20;
/**
* 总个数。
* The total counts.
*
* @var int
* @access public
*/
public $recTotal;
/**
* 每页的记录数。
* Record count per page.
*
* @var int
* @access public
*/
public $recPerPage;
/**
* The cookie name of recPerPage.
*
* @var string
* @access public
*/
public $pageCookie;
/**
* 总页面数。
* Page count.
*
* @var string
* @access public
*/
public $pageTotal;
/**
* 当前页码。
* Current page id.
*
* @var string
* @access public
*/
public $pageID;
/**
* 全局变量$app。
* The global $app.
*
* @var object
* @access public
*/
public $app;
/**
* 全局变量$lang。
* The global $lang.
*
* @var object
* @access public
*/
public $lang;
/**
* 当前的模块名。
* Current module name.
*
* @var string
* @access public
*/
public $moduleName;
/**
* 当前的方法名。
* Current method.
*
* @var string
* @access public
*/
public $methodName;
/**
* 参数信息。
* The params.
*
* @public array
*/
public $params;
/**
* 构造方法。
* The construct function.
*
* @param int $recTotal
* @param int $recPerPage
* @param int $pageID
* @access public
* @return void
*/
public function __construct($recTotal = 0, $recPerPage = 20, $pageID = 1)
{
$this->setApp();
$this->setLang();
$this->setModuleName();
$this->setMethodName();
$this->setRecTotal($recTotal);
$this->setRecPerPage($recPerPage);
$this->setPageTotal();
$this->setPageID($pageID);
}
/**
* 构造方法。
* The factory function.
*
* @param int $recTotal
* @param int $recPerPage
* @param int $pageID
* @access public
* @return object
*/
public static function init($recTotal = 0, $recPerPage = 20, $pageID = 1)
{
return new pager($recTotal, $recPerPage, $pageID);
}
/**
* 设置总记录数。
* Set the recTotal property.
*
* @param int $recTotal
* @access public
* @return void
*/
public function setRecTotal($recTotal = 0)
{
$this->recTotal = (int)$recTotal;
}
/**
* 设置每页记录数。
* Set the recPerPage property.
*
* @param int $recPerPage
* @access public
* @return void
*/
public function setRecPerPage($recPerPage)
{
/* Set the cookie name. */
$this->pageCookie = 'pager' . ucfirst($this->app->getModuleName()) . ucfirst($this->app->getMethodName());
if(isset($_COOKIE[$this->pageCookie])) $recPerPage = $_COOKIE[$this->pageCookie];
$this->recPerPage = ($recPerPage > 0) ? $recPerPage : PAGER::DEFAULT_REC_PER_PAGE;
}
/**
* 设置总页数。
* Set the pageTotal property.
*
* @access public
* @return void
*/
public function setPageTotal()
{
$this->pageTotal = ceil($this->recTotal / $this->recPerPage);
}
/**
* 设置页码。
* Set the page id.
*
* @param int $pageID
* @access public
* @return void
*/
public function setPageID($pageID)
{
if($pageID > 0 and ($this->pageTotal == 0 or $pageID <= $this->pageTotal))
{
$this->pageID = $pageID;
}
else
{
$this->pageID = 1;
}
}
/**
* 设置全局变量$app。
* Set the $app property;
*
* @access public
* @return void
*/
public function setApp()
{
global $app;
$this->app = $app;
}
/**
* 设置全局变量$lang。
* Set the $lang property.
*
* @access public
* @return void
*/
public function setLang()
{
global $lang;
$this->lang = $lang;
}
/**
* 设置模块名。
* Set the $moduleName property.
*
* @access public
* @return void
*/
public function setModuleName()
{
$this->moduleName = $this->app->getModuleName();
}
/**
* 设置方法名。
* Set the $methodName property.
*
* @access public
* @return void
*/
public function setMethodName()
{
$this->methodName = $this->app->getMethodName();
}
/**
* 从请求网址中获取记录总数、每页记录数、页码。
* Get recTotal, recPerpage, pageID from the request params, and add them to params.
*
* @access public
* @return void
*/
public function setParams()
{
$this->params = $this->app->getParams();
foreach($this->params as $key => $value)
{
if(strtolower($key) == 'rectotal') $this->params[$key] = $this->recTotal;
if(strtolower($key) == 'recperpage') $this->params[$key] = $this->recPerPage;
if(strtolower($key) == 'pageid') $this->params[$key] = $this->pageID;
}
parse_str(strip_tags(urldecode($_SERVER['QUERY_STRING'])), $query);
unset($query['m']);
unset($query['f']);
unset($query['t']);
$this->params = array_merge($this->params, $query);
}
/**
* 创建limit语句。
* Create the limit string.
*
* @access public
* @return string
*/
public function limit()
{
$limit = '';
if($this->pageTotal > 1) $limit = ' limit ' . ($this->pageID - 1) * $this->recPerPage . ", $this->recPerPage";
return $limit;
}
/**
* 向页面显示分页信息。
* Print the pager's html.
*
* @param string $align
* @param string $type
* @access public
* @return void
*/
public function show($align = 'right', $type = 'full')
{
if($align === 'justify')
{
echo $this->getJustify($type);
}
else
{
echo $this->get($align, $type);
}
}
/**
* 获取优化后的分页。
* Get the justify pager html string
*
* @access public
* @return [type] [description]
*/
public function getJustify()
{
if($this->recTotal <= 0) return '';
$this->setParams();
$pager = '';
$pager .= "<li class='previous" . ($this->pageID == 1 ? ' disabled' : '') . "'>";
$this->params['pageID'] = 1;
$pager .= $this->createLink('« ' . $this->lang->pager->previousPage) . '</li>';
$pager .= "<li class='caption'>";
$firstId = $this->recPerPage * ($this->pageID - 1) + 1;
$pager .= sprintf($this->lang->pager->summery, $firstId, max(min($this->recPerPage * $this->pageID, $this->recTotal), $firstId), $this->recTotal);
$pager .= '</li>';
$pager .= "<li class='next" . (($this->pageID == $this->pageTotal || $this->pageTotal <= 1) ? ' disabled' : '') . "'>";
$this->params['pageID'] = min($this->pageTotal, $this->pageID + 1);
$pager .= $this->createLink($this->lang->pager->nextPage . ' »') . '</li>';
return "<ul class='pager pager-justify'>{$pager}</ul>";
}
/**
* 设置分页信息的样式。
* Get the pager html string.
*
* @param string $align
* @param string $type the pager type, full|short|shortest
* @access public
* @return string
*/
public function get($align = 'right', $type = 'full')
{
/* 如果记录个数为0,返回没有记录。 */
/* If the RecTotal is zero, return with no record. */
if($this->recTotal == 0) return $type == 'mobile' ? '' : "<div style='float:$align; clear:none;' class='page'>{$this->lang->pager->noRecord}</div>";
/* Set the params. */
$this->setParams();
/* 创建前一页和后一页链接。 */
/* Create the prePage and nextpage, all types have them. */
$pager = $this->createPrePage($type);
$pager .= $this->createNextPage($type);
/* 简单和完全模式。 The short and full type. */
if($type !== 'shortest' and $type !== 'mobile')
{
$pager = $this->createFirstPage() . $pager;
$pager .= $this->createLastPage();
}
if($type == 'mobile')
{
$position = $this->pageTotal == 1 ? '' : $this->pageID . '/' . $this->pageTotal;
$pager = $pager . ' ' . $position;
}
else if($type != 'full')
{
$pager = $this->pageID . '/' . $this->pageTotal . ' ' . $pager;
}
/* 只是完全模式。 Only the full type . */
if($type == 'full')
{
$pager = $this->createDigest() . $pager;
$pager .= $this->createGoTo();
$pager .= $this->createRecPerPageJS();
}
return "<div style='float:$align; clear:none;' class='pager form-inline'>$pager</div>";
}
/**
* 生成分页摘要信息。
* Create the digest code.
*
* @access public
* @return string
*/
public function createDigest()
{
return sprintf($this->lang->pager->digest, $this->recTotal, $this->createRecPerPageList(), $this->pageID, $this->pageTotal);
}
/**
* 创建首页链接。
* Create the first page.
*
* @access public
* @return string
*/
public function createFirstPage()
{
if($this->pageID == 1) return $this->lang->pager->first . ' ';
$this->params['pageID'] = 1;
return $this->createLink($this->lang->pager->first);
}
/**
* 创建前一页链接。
* Create the pre page html.
*
* @param string $type
* @access public
* @return string
*/
public function createPrePage($type = 'full')
{
if($type == 'mobile')
{
if($this->pageID == 1) return '';
$this->params['pageID'] = $this->pageID - 1;
return $this->createLink($this->lang->pager->pre);
}
else
{
if($this->pageID == 1) return $this->lang->pager->pre . ' ';
$this->params['pageID'] = $this->pageID - 1;
return $this->createLink($this->lang->pager->pre);
}
}
/**
* 创建下一页链接。
* Create the next page html.
*
* @param string $type
* @access public
* @return string
*/
public function createNextPage($type = 'full')
{
if($type == 'mobile')
{
if($this->pageID == $this->pageTotal) return '';
$this->params['pageID'] = $this->pageID + 1;
return $this->createLink($this->lang->pager->next);
}
else
{
if($this->pageID == $this->pageTotal) return $this->lang->pager->next . ' ';
$this->params['pageID'] = $this->pageID + 1;
return $this->createLink($this->lang->pager->next);
}
}
/**
* 创建最后一页链接。
* Create the last page
*
* @access public
* @return string
*/
public function createLastPage()
{
if($this->pageID == $this->pageTotal) return $this->lang->pager->last . ' ';
$this->params['pageID'] = $this->pageTotal;
return $this->createLink($this->lang->pager->last);
}
/**
* 创建每页显示记录数的select标签。
* Create the select object of record perpage.
*
* @access public
* @return string
*/
public function createRecPerPageJS()
{
/*
* 替换recTotal, recPerPage, pageID为特殊的字符串,然后用js代码替换掉。
* Replace the recTotal, recPerPage, pageID to special string, and then replace them with values by JS.
**/
$params = $this->params;
foreach($params as $key => $value)
{
if(strtolower($key) == 'rectotal') $params[$key] = '_recTotal_';
if(strtolower($key) == 'recperpage') $params[$key] = '_recPerPage_';
if(strtolower($key) == 'pageid') $params[$key] = '_pageID_';
}
$vars = '';
foreach($params as $key => $value) $vars .= "$key=$value&";
$vars = rtrim($vars, '&');
$js = <<<EOT
<script language='Javascript'>
vars = '$vars';
pageCookie = '$this->pageCookie';
function submitPage(mode, perPage)
{
pageTotal = parseInt(document.getElementById('_pageTotal').value);
pageID = document.getElementById('_pageID').value;
recPerPage = document.getElementById('_recPerPage').getAttribute('data-value');
recTotal = document.getElementById('_recTotal').value;
if(mode == 'changePageID')
{
if(pageID > pageTotal) pageID = pageTotal;
if(pageID < 1) pageID = 1;
}
else if(mode == 'changeRecPerPage')
{
recPerPage = perPage;
pageID = 1;
}
$.cookie(pageCookie, recPerPage, {expires:config.cookieLife, path:config.webRoot});
vars = vars.replace('_recTotal_', recTotal)
vars = vars.replace('_recPerPage_', recPerPage)
vars = vars.replace('_pageID_', pageID);
location.href=createLink('$this->moduleName', '$this->methodName', vars);
}
</script>
EOT;
return $js;
}
/**
* 生成每页显示记录数的select列表。
* Create the select list of RecPerPage.
*
* @access public
* @return string
*/
public function createRecPerPageList()
{
for($i = 5; $i <= 50; $i += 5) $range[$i] = $i;
$range[100] = 100;
$range[200] = 200;
$range[500] = 500;
$range[1000] = 1000;
$range[2000] = 2000;
$html = "<div class='dropdown dropup'><a href='javascript:;' data-toggle='dropdown' id='_recPerPage' data-value='{$this->recPerPage}'>" . (sprintf($this->lang->pager->recPerPage, $this->recPerPage)) . "<span class='caret'></span></a><ul class='dropdown-menu'>";
foreach ($range as $key => $value)
{
$html .= '<li' . ($this->recPerPage == $value ? " class='active'" : '') .'>' . "<a href='javascript:submitPage(\"changeRecPerPage\", $value)'>{$value}</a>" . '</li>';
}
$html .= '</ul></div>';
return $html;
}
/**
* 生成跳转到指定页码的部分。
* Create the goto part html.
*
* @access public
* @return string
*/
public function createGoTo()
{
$goToHtml = "<input type='hidden' id='_recTotal' value='$this->recTotal' />\n";
$goToHtml .= "<input type='hidden' id='_pageTotal' value='$this->pageTotal' />\n";
$goToHtml .= "<input type='text' id='_pageID' value='$this->pageID' style='text-align:center;width:30px;' class='form-control' /> \n";
$goToHtml .= "<input type='button' id='goto' value='{$this->lang->pager->locate}' onclick='submitPage(\"changePageID\");' class='btn'/>";
return $goToHtml;
}
/**
* 创建链接。
* Create link.
*
* @param string $title
* @access public
* @return string
*/
public function createLink($title)
{
global $config;
if(helper::inSeoMode() && method_exists('uri', 'create' . $this->moduleName . $this->methodName))
{
$link = strip_tags(urldecode($_SERVER['REQUEST_URI']));
if($this->params['pageID'] == 1) return html::a(preg_replace('/\/p\d+\./', '.', $link), $title);
if(preg_match('/\/p\d+/', $link)) return html::a(preg_replace('/\/p\d+\./', '/p' . $this->params['pageID'] . '.', $link), $title);
if($config->requestType == 'PATH_INFO2') $link = str_replace('index.php/', 'index_php/', $link);
$link = str_replace('.', "/p{$this->params['pageID']}.", $link);
if($config->requestType == 'PATH_INFO2') $link = str_replace('index_php/', 'index.php/', $link);
return html::a($link, $title);
}
return html::a(helper::createLink($this->moduleName, $this->methodName, $this->params), $title);
}
}
+3 -1972
View File
File diff suppressed because it is too large Load Diff
+3 -922
View File
@@ -11,489 +11,15 @@
* May you share freely, never taking more than you give.
*/
helper::import(dirname(dirname(__FILE__)) . '/base/filter/filter.class.php');
/**
* validater类,检查数据是否符合规则。
* The validater class, checking data by rules.
*
* @package framework
*/
class validater
class validater extends baseValidater
{
/**
* 最大参数个数。
* The max count of args.
*/
const MAX_ARGS = 3;
/**
* 是否是Bool类型。
* Bool checking.
*
* @param bool $var
* @static
* @access public
* @return bool
*/
public static function checkBool($var)
{
return filter_var($var, FILTER_VALIDATE_BOOLEAN);
}
/**
* 是否是Int类型。
* Int checking.
*
* @param int $var
* @static
* @access public
* @return bool
*/
public static function checkInt($var)
{
$args = func_get_args();
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. */
if(isset($args[1]))
{
/* 如果最大的整数也设置了。 And Max is setted. */
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);
}
}
/**
* 检查不是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
* @param string $decimal
* @static
* @access public
* @return bool
*/
public static function checkFloat($var, $decimal = '.')
{
return filter_var($var, FILTER_VALIDATE_FLOAT, array('options' => array('decimal' => $decimal)));
}
/**
* 检查Email。
* Email checking.
*
* @param string $var
* @static
* @access public
* @return bool
*/
public static function checkEmail($var)
{
return filter_var($var, FILTER_VALIDATE_EMAIL);
}
/**
* 检查电话或手机号码
* 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
* @static
* @access public
* @return bool
*/
public static function checkURL($var)
{
return filter_var($var, FILTER_VALIDATE_URL);
}
/**
* 检查域名,不支持中文。
* 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
* @param string $range all|public|static|private
* @static
* @access public
* @return bool
*/
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_RANGE) === false) return true;
return false;
}
}
/**
* 日期检查。注意,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
* @static
* @access public
* @return bool
*/
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));
}
/**
* 检查正则表达式。
* REG checking.
*
* @param string $var
* @param string $reg
* @static
* @access public
* @return bool
*/
public static function checkREG($var, $reg)
{
return filter_var($var, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => $reg)));
}
/**
* 检查长度。
* Length checking.
*
* @param string $var
* @param string $max
* @param int $min
* @static
* @access public
* @return bool
*/
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);
}
/**
* 检查不为空。
* Not empty checking.
*
* @param mixed $var
* @static
* @access public
* @return bool
*/
public static function checkNotEmpty($var)
{
return !empty($var);
}
/**
* 检查为空。
* Empty checking.
*
* @param mixed $var
* @static
* @access public
* @return bool
*/
public static function checkEmpty($var)
{
return empty($var);
}
/**
* 检查用户名。
* Account checking.
*
* @param string $var
* @static
* @access public
* @return bool
*/
public static function checkAccount($var)
{
global $config;
$accountRule = 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);
}
/**
* 检查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
* @static
* @access public
* @return bool
*/
public static function checkCaptcha($var)
{
if(!isset($_SESSION['captcha'])) return false;
return $var == $_SESSION['captcha'];
}
/**
* 是否等于给定的值。
* Must equal a value.
*
* @param mixed $var
* @param mixed $value
* @static
* @access public
* @return bool
*/
public static function checkEqual($var, $value)
{
return $var == $value;
}
/**
* 检查不等于给定的值
* 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
* @param mixed $value
* @static
* @access public
* @return bool
*/
public static function checkGT($var, $value)
{
return $var > $value;
}
/**
* 检查小于给定的值
* Must less than a value.
*
* @param mixed $var
* @param mixed $value
* @static
* @access public
* @return bool
*/
public static function checkLT($var, $value)
{
return $var < $value;
}
/**
* 检查大于等于给定的值
* Must greater than a value or equal a value.
*
* @param mixed $var
* @param mixed $value
* @static
* @access public
* @return bool
*/
public static function checkGE($var, $value)
{
return $var >= $value;
}
/**
* 检查小于等于给定的值
* Must less than a value or equal a value.
*
* @param mixed $var
* @param mixed $value
* @static
* @access public
* @return bool
*/
public static function checkLE($var, $value)
{
return $var <= $value;
}
/**
* 检查是否在给定的列表里面。
* Must in value list.
*
* @param mixed $var
* @param mixed $value
* @static
* @access public
* @return bool
*/
public static function checkIn($var, $value)
{
if(!is_array($value)) $value = explode(',', $value);
return in_array($var, $value);
}
/**
* 检查文件名。
* Check file name.
*
* @param string $var
* @static
* @access public
* @return bool
*/
public static function checkFileName($var)
{
return !preg_match('/>+|:+|<+/', $var);
}
/**
* 检查敏感词。
* 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
* @param string $func
* @static
* @access public
* @return bool
*/
public static function call($var, $func)
{
return filter_var($var, FILTER_CALLBACK, array('options' => $func));
}
}
/**
@@ -502,451 +28,6 @@ class validater
*
* @package framework
*/
class fixer
class fixer extends baseFixer
{
/**
* 处理的数据。
* The data to be fixed.
*
* @var ojbect
* @access private
*/
private $data;
/**
* 跳过处理的字段。
* 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
* @access private
* @return void
*/
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;
default:
die('scope not supported, should be post|get|server|session|cookie|env');
}
}
/**
* 工厂方法。
* The factory function.
*
* @param string $scope
* @access public
* @return object fixer object.
*/
public static function input($scope)
{
return new fixer($scope);
}
/**
* 处理Email。
* Email fix.
*
* @param string $fieldName
* @access public
* @return object fixer object.
*/
public function cleanEmail($fieldName)
{
$fields = $this->processFields($fieldName);
foreach($fields as $fieldName) $this->data->$fieldName = filter_var($this->data->$fieldName, FILTER_SANITIZE_EMAIL);
return $this;
}
/**
* url编码。
* urlencode.
*
* @param string $fieldName
* @access public
* @return object fixer object.
*/
public function encodeURL($fieldName)
{
$fields = $this->processFields($fieldName);
$args = func_get_args();
foreach($fields as $fieldName)
{
$this->data->$fieldName = isset($args[1]) ? filter_var($this->data->$fieldName, FILTER_SANITIZE_ENCODED, $args[1]) : filter_var($this->data->$fieldName, FILTER_SANITIZE_ENCODED);
}
return $this;
}
/**
* 清理网址。
* Clean the url.
*
* @param string $fieldName
* @access public
* @return object fixer object.
*/
public function cleanURL($fieldName)
{
$fields = $this->processFields($fieldName);
foreach($fields as $fieldName) $this->data->$fieldName = filter_var($this->data->$fieldName, FILTER_SANITIZE_URL);
return $this;
}
/**
* 处理Float类型。
* Float fixer.
*
* @param string $fieldName
* @access public
* @return object fixer object.
*/
public function cleanFloat($fieldName)
{
$fields = $this->processFields($fieldName);
foreach($fields as $fieldName) $this->data->$fieldName = filter_var($this->data->$fieldName, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION|FILTER_FLAG_ALLOW_THOUSAND);
return $this;
}
/**
* 处理Int类型。
* Int fixer.
*
* @param string $fieldName
* @access public
* @return object fixer object.
*/
public function cleanINT($fieldName = '')
{
$fields = $this->processFields($fieldName);
foreach($fields as $fieldName) $this->data->$fieldName = filter_var($this->data->$fieldName, FILTER_SANITIZE_NUMBER_INT);
return $this;
}
/**
* 将字符串转换为可以在浏览器查看的编码。
* Special chars.
*
* @param string $fieldName
* @access public
* @return object fixer object
*/
public function specialChars($fieldName)
{
$fields = $this->processFields($fieldName);
foreach($fields as $fieldName)
{
if(empty($this->stripedFields) or !in_array($fieldName, $this->stripedFields)) $this->data->$fieldName = $this->specialArray($this->data->$fieldName);
}
return $this;
}
/**
* Special array
*
* @param mix $data
* @access public
* @return mix
*/
public function specialArray($data)
{
if(!is_array($data)) return htmlspecialchars($data, ENT_QUOTES);
foreach($data as &$value) $value = $this->specialArray($value);
return $data;
}
/**
* 忽略该标签。
* Strip tags
*
* @param string $fieldName
* @param string $allowableTags
* @access public
* @return object fixer object
*/
public function stripTags($fieldName, $allowedTags = '')
{
global $app, $config;
if(empty($allowedTags) and isset($config->allowedTags)) $allowedTags = $config->allowedTags;
$usePurifier = isset($config->framework->purifier) ? $config->framework->purifier : false;
if($usePurifier)
{
$app->loadClass('purifier', true);
$purifierConfig = HTMLPurifier_Config::createDefault();
$purifierConfig->set('Filter.YouTube', 1);
/* Disable caching. */
$purifierConfig->set('Cache.DefinitionImpl', null);
$purifier = new HTMLPurifier($purifierConfig);
$def = $purifierConfig->getHTMLDefinition(true);
$def->addAttribute('a', 'target', 'Enum#_blank,_self,_target,_top');
}
$fields = $this->processFields($fieldName);
foreach($fields as $fieldName)
{
if(version_compare(phpversion(), '5.4', '<') and get_magic_quotes_gpc()) $this->data->$fieldName = stripslashes($this->data->$fieldName);
if(!in_array($fieldName, $this->stripedFields))
{
if(!defined('RUN_MODE') or RUN_MODE != 'admin')
{
/*
* purifier会把&nbsp;替换空格,kindeditor在会吧行首的空格去掉。
* purifier will change &nbsp; to ' ', and edit it will no space in line head use kindeditor.
**/
if($usePurifier) $this->data->$fieldName = str_replace('&nbsp;', '&spnb;', $this->data->$fieldName);
$this->data->$fieldName = $usePurifier ? $purifier->purify($this->data->$fieldName) : strip_tags($this->data->$fieldName, $allowedTags);
if($usePurifier) $this->data->$fieldName = str_replace('&amp;spnb;', '&nbsp;', $this->data->$fieldName);
}
}
$this->stripedFields[] = $fieldName;
}
return $this;
}
/**
* 忽略处理给定的字段。
* Skip special chars check.
*
* @param string $filename
* @access public
* @return object fixer object
*/
public function skipSpecial($fieldName)
{
$fields = $this->processFields($fieldName);
foreach($fields as $fieldName) $this->stripedFields[] = $fieldName;
return $this;
}
/**
* 给字段添加引用,防止字符与关键字冲突。
* Quote
*
* @param string $fieldName
* @access public
* @return object fixer object
*/
public function quote($fieldName)
{
$fields = $this->processFields($fieldName);
foreach($fields as $fieldName) $this->data->$fieldName = filter_var($this->data->$fieldName, FILTER_SANITIZE_MAGIC_QUOTES);
return $this;
}
/**
* 设置字段的默认值。
* Set default value of some fileds.
*
* @param string $fields
* @param mixed $value
* @access public
* @return object fixer object
*/
public function setDefault($fields, $value)
{
$fields = strpos($fields, ',') ? explode(',', str_replace(' ', '', $fields)) : array($fields);
foreach($fields as $fieldName)if(!isset($this->data->$fieldName) or empty($this->data->$fieldName)) $this->data->$fieldName = $value;
return $this;
}
/**
* 如果条件为真,则为字段赋值。
* Set value of a filed on the condition is true.
*
* @param bool $condition
* @param string $fieldName
* @param string $value
* @access public
* @return object fixer object
*/
public function setIF($condition, $fieldName, $value)
{
if($condition) $this->data->$fieldName = $value;
return $this;
}
/**
* 强制给字段赋值。
* Set the value of a filed in force.
*
* @param string $fieldName
* @param mixed $value
* @access public
* @return object fixer object
*/
public function setForce($fieldName, $value)
{
$this->data->$fieldName = $value;
return $this;
}
/**
* 移除一个字段。
* Remove a field.
*
* @param string $fieldName
* @access public
* @return object fixer object
*/
public function remove($fieldName)
{
$fields = $this->processFields($fieldName);
foreach($fields as $fieldName) unset($this->data->$fieldName);
return $this;
}
/**
* 如果条件为真,移除该字段。
* Remove a filed on the condition is true.
*
* @param bool $condition
* @param string $fields
* @access public
* @return object fixer object
*/
public function removeIF($condition, $fields)
{
$fields = $this->processFields($fields);
if($condition) foreach($fields as $fieldName) unset($this->data->$fieldName);
return $this;
}
/**
* 为数据添加新的项。
* Add an item to the data.
*
* @param string $fieldName
* @param mixed $value
* @access public
* @return object fixer object
*/
public function add($fieldName, $value)
{
$this->data->$fieldName = $value;
return $this;
}
/**
* 如果条件为真,则为数据添加新的项。
* Add an item to the data on the condition if true.
*
* @param bool $condition
* @param string $fieldName
* @param mixed $value
* @access public
* @return object fixer object
*/
public function addIF($condition, $fieldName, $value)
{
if($condition) $this->data->$fieldName = $value;
return $this;
}
/**
* 为指定字段增加值。
* Join the field.
*
* @param string $fieldName
* @param string $value
* @access public
* @return object fixer object
*/
public function join($fieldName, $value)
{
if(!isset($this->data->$fieldName) or !is_array($this->data->$fieldName)) return $this;
$this->data->$fieldName = join($value, $this->data->$fieldName);
return $this;
}
/**
* 调用一个方法来处理数据。
* Call a function to fix it.
*
* @param string $fieldName
* @param string $func
* @access public
* @return object fixer object
*/
public function callFunc($fieldName, $func)
{
$fields = $this->processFields($fieldName);
foreach($fields as $fieldName) $this->data->$fieldName = filter_var($this->data->$fieldName, FILTER_CALLBACK, array('options' => $func));
return $this;
}
/**
* 处理完成后返回数据。
* Get the data after fixing.
*
* @param string $fieldName
* @access public
* @return object
*/
public function get($fields = '')
{
$fields = str_replace(' ', '', trim($fields));
foreach($this->data as $field => $value) $this->specialChars($field);
if(empty($fields)) return $this->data;
if(strpos($fields, ',') === false) return $this->data->$fields;
$fields = array_flip(explode(',', $fields));
foreach($this->data as $field => $value)
{
if(!isset($fields[$field])) unset($this->data->$field);
if(!in_array($field, $this->stripedFields)) $this->data->$field = $this->specialChars($this->data->field);
}
return $this->data;
}
/**
* 处理字段,如果字段中含有',',拆分成数组。如果字段不在$data中,删除掉。
* Process fields, if contains ',', split it to array. If not in $data, remove it.
*
* @param string $fields
* @access private
* @return array
*/
private function processFields($fields)
{
$fields = strpos($fields, ',') ? explode(',', str_replace(' ', '', $fields)) : array($fields);
foreach($fields as $key => $fieldName) if(!isset($this->data->$fieldName)) unset($fields[$key]);
return $fields;
}
}
+4 -1139
View File
File diff suppressed because it is too large Load Diff
+3 -600
View File
@@ -10,611 +10,14 @@
* May you find forgiveness for yourself and forgive others.
* May you share freely, never taking more than you give.
*/
helper::import(dirname(dirname(__FILE__)) . '/base/pager/pager.class.php');
/**
* pager类.
* Pager class.
*
* @package framework
*/
class pager
class pager extends basePager
{
/**
* 每页的默认显示记录数。
* The default counts of per page.
*
* @public int
*/
const DEFAULT_REC_PER_PAGE = 20;
/**
* 总个数。
* The total counts.
*
* @var int
* @access public
*/
public $recTotal;
/**
* 每页的记录数。
* Record count per page.
*
* @var int
* @access public
*/
public $recPerPage;
/**
* The cookie name of recPerPage.
*
* @var string
* @access public
*/
public $pageCookie;
/**
* 总页面数。
* Page count.
*
* @var string
* @access public
*/
public $pageTotal;
/**
* 当前页码。
* Current page id.
*
* @var string
* @access public
*/
public $pageID;
/**
* 全局变量$app。
* The global $app.
*
* @var object
* @access private
*/
private $app;
/**
* 全局变量$lang。
* The global $lang.
*
* @var object
* @access private
*/
private $lang;
/**
* 当前的模块名。
* Current module name.
*
* @var string
* @access private
*/
private $moduleName;
/**
* 当前的方法名。
* Current method.
*
* @var string
* @access private
*/
private $methodName;
/**
* 参数信息。
* The params.
*
* @private array
*/
private $params;
/**
* 构造方法。
* The construct function.
*
* @param int $recTotal
* @param int $recPerPage
* @param int $pageID
* @access public
* @return void
*/
public function __construct($recTotal = 0, $recPerPage = 20, $pageID = 1)
{
$this->setApp();
$this->setLang();
$this->setModuleName();
$this->setMethodName();
$this->setRecTotal($recTotal);
$this->setRecPerPage($recPerPage);
$this->setPageTotal();
$this->setPageID($pageID);
}
/**
* 构造方法。
* The factory function.
*
* @param int $recTotal
* @param int $recPerPage
* @param int $pageID
* @access public
* @return object
*/
public static function init($recTotal = 0, $recPerPage = 20, $pageID = 1)
{
return new pager($recTotal, $recPerPage, $pageID);
}
/**
* 设置总记录数。
* Set the recTotal property.
*
* @param int $recTotal
* @access public
* @return void
*/
public function setRecTotal($recTotal = 0)
{
$this->recTotal = (int)$recTotal;
}
/**
* 设置每页记录数。
* Set the recPerPage property.
*
* @param int $recPerPage
* @access public
* @return void
*/
public function setRecPerPage($recPerPage)
{
/* Set the cookie name. */
$this->pageCookie = 'pager' . ucfirst($this->app->getModuleName()) . ucfirst($this->app->getMethodName());
if(isset($_COOKIE[$this->pageCookie])) $recPerPage = $_COOKIE[$this->pageCookie];
$this->recPerPage = ($recPerPage > 0) ? $recPerPage : PAGER::DEFAULT_REC_PER_PAGE;
}
/**
* 设置总页数。
* Set the pageTotal property.
*
* @access public
* @return void
*/
public function setPageTotal()
{
$this->pageTotal = ceil($this->recTotal / $this->recPerPage);
}
/**
* 设置页码。
* Set the page id.
*
* @param int $pageID
* @access public
* @return void
*/
public function setPageID($pageID)
{
if($pageID > 0 and ($this->pageTotal == 0 or $pageID <= $this->pageTotal))
{
$this->pageID = $pageID;
}
else
{
$this->pageID = 1;
}
}
/**
* 设置全局变量$app。
* Set the $app property;
*
* @access private
* @return void
*/
private function setApp()
{
global $app;
$this->app = $app;
}
/**
* 设置全局变量$lang。
* Set the $lang property.
*
* @access private
* @return void
*/
private function setLang()
{
global $lang;
$this->lang = $lang;
}
/**
* 设置模块名。
* Set the $moduleName property.
*
* @access private
* @return void
*/
private function setModuleName()
{
$this->moduleName = $this->app->getModuleName();
}
/**
* 设置方法名。
* Set the $methodName property.
*
* @access private
* @return void
*/
private function setMethodName()
{
$this->methodName = $this->app->getMethodName();
}
/**
* 从请求网址中获取记录总数、每页记录数、页码。
* Get recTotal, recPerpage, pageID from the request params, and add them to params.
*
* @access private
* @return void
*/
private function setParams()
{
$this->params = $this->app->getParams();
foreach($this->params as $key => $value)
{
if(strtolower($key) == 'rectotal') $this->params[$key] = $this->recTotal;
if(strtolower($key) == 'recperpage') $this->params[$key] = $this->recPerPage;
if(strtolower($key) == 'pageid') $this->params[$key] = $this->pageID;
}
parse_str(strip_tags(urldecode($_SERVER['QUERY_STRING'])), $query);
unset($query['m']);
unset($query['f']);
unset($query['t']);
$this->params = array_merge($this->params, $query);
}
/**
* 创建limit语句。
* Create the limit string.
*
* @access public
* @return string
*/
public function limit()
{
$limit = '';
if($this->pageTotal > 1) $limit = ' limit ' . ($this->pageID - 1) * $this->recPerPage . ", $this->recPerPage";
return $limit;
}
/**
* 向页面显示分页信息。
* Print the pager's html.
*
* @param string $align
* @param string $type
* @access public
* @return void
*/
public function show($align = 'right', $type = 'full')
{
if($align === 'justify')
{
echo $this->getJustify($type);
}
else
{
echo $this->get($align, $type);
}
}
/**
* 获取优化后的分页。
* Get the justify pager html string
*
* @access public
* @return [type] [description]
*/
public function getJustify()
{
if($this->recTotal <= 0) return '';
$this->setParams();
$pager = '';
$pager .= "<li class='previous" . ($this->pageID == 1 ? ' disabled' : '') . "'>";
$this->params['pageID'] = 1;
$pager .= $this->createLink('« ' . $this->lang->pager->previousPage) . '</li>';
$pager .= "<li class='caption'>";
$firstId = $this->recPerPage * ($this->pageID - 1) + 1;
$pager .= sprintf($this->lang->pager->summery, $firstId, max(min($this->recPerPage * $this->pageID, $this->recTotal), $firstId), $this->recTotal);
$pager .= '</li>';
$pager .= "<li class='next" . (($this->pageID == $this->pageTotal || $this->pageTotal <= 1) ? ' disabled' : '') . "'>";
$this->params['pageID'] = min($this->pageTotal, $this->pageID + 1);
$pager .= $this->createLink($this->lang->pager->nextPage . ' »') . '</li>';
return "<ul class='pager pager-justify'>{$pager}</ul>";
}
/**
* 设置分页信息的样式。
* Get the pager html string.
*
* @param string $align
* @param string $type the pager type, full|short|shortest
* @access public
* @return string
*/
public function get($align = 'right', $type = 'full')
{
/* 如果记录个数为0,返回没有记录。 */
/* If the RecTotal is zero, return with no record. */
if($this->recTotal == 0) return $type == 'mobile' ? '' : "<div style='float:$align; clear:none;' class='page'>{$this->lang->pager->noRecord}</div>";
/* Set the params. */
$this->setParams();
/* 创建前一页和后一页链接。 */
/* Create the prePage and nextpage, all types have them. */
$pager = $this->createPrePage($type);
$pager .= $this->createNextPage($type);
/* 简单和完全模式。 The short and full type. */
if($type !== 'shortest' and $type !== 'mobile')
{
$pager = $this->createFirstPage() . $pager;
$pager .= $this->createLastPage();
}
if($type == 'mobile')
{
$position = $this->pageTotal == 1 ? '' : $this->pageID . '/' . $this->pageTotal;
$pager = $pager . ' ' . $position;
}
else if($type != 'full')
{
$pager = $this->pageID . '/' . $this->pageTotal . ' ' . $pager;
}
/* 只是完全模式。 Only the full type . */
if($type == 'full')
{
$pager = $this->createDigest() . $pager;
$pager .= $this->createGoTo();
$pager .= $this->createRecPerPageJS();
}
return "<div style='float:$align; clear:none;' class='pager form-inline'>$pager</div>";
}
/**
* 生成分页摘要信息。
* Create the digest code.
*
* @access private
* @return string
*/
private function createDigest()
{
return sprintf($this->lang->pager->digest, $this->recTotal, $this->createRecPerPageList(), $this->pageID, $this->pageTotal);
}
/**
* 创建首页链接。
* Create the first page.
*
* @access private
* @return string
*/
private function createFirstPage()
{
if($this->pageID == 1) return $this->lang->pager->first . ' ';
$this->params['pageID'] = 1;
return $this->createLink($this->lang->pager->first);
}
/**
* 创建前一页链接。
* Create the pre page html.
*
* @param string $type
* @access private
* @return string
*/
private function createPrePage($type = 'full')
{
if($type == 'mobile')
{
if($this->pageID == 1) return '';
$this->params['pageID'] = $this->pageID - 1;
return $this->createLink($this->lang->pager->pre);
}
else
{
if($this->pageID == 1) return $this->lang->pager->pre . ' ';
$this->params['pageID'] = $this->pageID - 1;
return $this->createLink($this->lang->pager->pre);
}
}
/**
* 创建下一页链接。
* Create the next page html.
*
* @param string $type
* @access private
* @return string
*/
private function createNextPage($type = 'full')
{
if($type == 'mobile')
{
if($this->pageID == $this->pageTotal) return '';
$this->params['pageID'] = $this->pageID + 1;
return $this->createLink($this->lang->pager->next);
}
else
{
if($this->pageID == $this->pageTotal) return $this->lang->pager->next . ' ';
$this->params['pageID'] = $this->pageID + 1;
return $this->createLink($this->lang->pager->next);
}
}
/**
* 创建最后一页链接。
* Create the last page
*
* @access private
* @return string
*/
private function createLastPage()
{
if($this->pageID == $this->pageTotal) return $this->lang->pager->last . ' ';
$this->params['pageID'] = $this->pageTotal;
return $this->createLink($this->lang->pager->last);
}
/**
* 创建每页显示记录数的select标签。
* Create the select object of record perpage.
*
* @access private
* @return string
*/
private function createRecPerPageJS()
{
/*
* 替换recTotal, recPerPage, pageID为特殊的字符串,然后用js代码替换掉。
* Replace the recTotal, recPerPage, pageID to special string, and then replace them with values by JS.
**/
$params = $this->params;
foreach($params as $key => $value)
{
if(strtolower($key) == 'rectotal') $params[$key] = '_recTotal_';
if(strtolower($key) == 'recperpage') $params[$key] = '_recPerPage_';
if(strtolower($key) == 'pageid') $params[$key] = '_pageID_';
}
$vars = '';
foreach($params as $key => $value) $vars .= "$key=$value&";
$vars = rtrim($vars, '&');
$js = <<<EOT
<script language='Javascript'>
vars = '$vars';
pageCookie = '$this->pageCookie';
function submitPage(mode, perPage)
{
pageTotal = parseInt(document.getElementById('_pageTotal').value);
pageID = document.getElementById('_pageID').value;
recPerPage = document.getElementById('_recPerPage').getAttribute('data-value');
recTotal = document.getElementById('_recTotal').value;
if(mode == 'changePageID')
{
if(pageID > pageTotal) pageID = pageTotal;
if(pageID < 1) pageID = 1;
}
else if(mode == 'changeRecPerPage')
{
recPerPage = perPage;
pageID = 1;
}
$.cookie(pageCookie, recPerPage, {expires:config.cookieLife, path:config.webRoot});
vars = vars.replace('_recTotal_', recTotal)
vars = vars.replace('_recPerPage_', recPerPage)
vars = vars.replace('_pageID_', pageID);
location.href=createLink('$this->moduleName', '$this->methodName', vars);
}
</script>
EOT;
return $js;
}
/**
* 生成每页显示记录数的select列表。
* Create the select list of RecPerPage.
*
* @access private
* @return string
*/
private function createRecPerPageList()
{
for($i = 5; $i <= 50; $i += 5) $range[$i] = $i;
$range[100] = 100;
$range[200] = 200;
$range[500] = 500;
$range[1000] = 1000;
$range[2000] = 2000;
$html = "<div class='dropdown dropup'><a href='javascript:;' data-toggle='dropdown' id='_recPerPage' data-value='{$this->recPerPage}'>" . (sprintf($this->lang->pager->recPerPage, $this->recPerPage)) . "<span class='caret'></span></a><ul class='dropdown-menu'>";
foreach ($range as $key => $value)
{
$html .= '<li' . ($this->recPerPage == $value ? " class='active'" : '') .'>' . "<a href='javascript:submitPage(\"changeRecPerPage\", $value)'>{$value}</a>" . '</li>';
}
$html .= '</ul></div>';
return $html;
}
/**
* 生成跳转到指定页码的部分。
* Create the goto part html.
*
* @access private
* @return string
*/
private function createGoTo()
{
$goToHtml = "<input type='hidden' id='_recTotal' value='$this->recTotal' />\n";
$goToHtml .= "<input type='hidden' id='_pageTotal' value='$this->pageTotal' />\n";
$goToHtml .= "<input type='text' id='_pageID' value='$this->pageID' style='text-align:center;width:30px;' class='form-control' /> \n";
$goToHtml .= "<input type='button' id='goto' value='{$this->lang->pager->locate}' onclick='submitPage(\"changePageID\");' class='btn'/>";
return $goToHtml;
}
/**
* 创建链接。
* Create link.
*
* @param string $title
* @access private
* @return string
*/
private function createLink($title)
{
global $config;
if(helper::inSeoMode() && method_exists('uri', 'create' . $this->moduleName . $this->methodName))
{
$link = strip_tags(urldecode($_SERVER['REQUEST_URI']));
if($this->params['pageID'] == 1) return html::a(preg_replace('/\/p\d+\./', '.', $link), $title);
if(preg_match('/\/p\d+/', $link)) return html::a(preg_replace('/\/p\d+\./', '/p' . $this->params['pageID'] . '.', $link), $title);
if($config->requestType == 'PATH_INFO2') $link = str_replace('index.php/', 'index_php/', $link);
$link = str_replace('.', "/p{$this->params['pageID']}.", $link);
if($config->requestType == 'PATH_INFO2') $link = str_replace('index_php/', 'index.php/', $link);
return html::a($link, $title);
}
return html::a(helper::createLink($this->moduleName, $this->methodName, $this->params), $title);
}
}