/common/system/validate.php
PHP | 251 lines | 144 code | 4 blank | 103 comment | 23 complexity | 8fed9ea71025d97db5a8c9fff6ad6572 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1
- <?php
- /**
- * +---------------------------------------------------------------------------+
- * | LL LLLL LL L LLLL LLLL |
- * | LL LL L LLL LL LL L LL LL |
- * | LL LLLL LLLLL LL LL LL LLLL LLL LL LL LL LL |
- * | LL LL LL LL LL LL L LLL LL LLLLL LL LL LL |
- * | LL LLLLL LL LLLL LL L L LL LLLLL LL LL LL |
- * | LL LL LL LL LLLL LL L LL LL LLLL LL |
- * | LL LL LL LL LL LL L L LL L LL LLLL LL |
- * | LLLLLL LLLLL LLLLL LL LLLL L LL LLLL LL LLLLLL |
- * | LL |
- * | LL |
- * +---------------------------------------------------------------------------+
- * | Copyright (C) 2007-2010 LazyCMS.com All rights reserved. |
- * +---------------------------------------------------------------------------+
- * | LazyCMS is free software. See LICENSE for copyright notices and details. |
- * +---------------------------------------------------------------------------+
- */
- defined('COM_PATH') or die('Restricted access!');
- // ??
- define('VALIDATE_IS_NUMERIC','IS_NUMERIC');
- // ??
- define('VALIDATE_IS_LETTERS','IS_LETTERS');
- //????
- define('VALIDATE_IS_EMAIL','IS_EMAIL');
- // ???
- define('VALIDATE_IS_URL','IS_URL');
- // ?????????
- define('VALIDATE_IS_LIST','IS_LIST');
- // ???????????
- define('VALIDATE_IS_CNUS','IS_CNUS');
- // ???????????????[?]
- define('VALIDATE_IS_CNUSO','IS_CNUSO');
- // ????
- define('VALIDATE_IS_PATH','IS_PATH');
- // ????
- define('VALIDATE_EMPTY','EMPTY');
- // ????
- define('VALIDATE_LENGTH','LENGTH');
- // ???????
- define('VALIDATE_EQUAL','EQUAL');
- /**
- * LazyCMS ?????
- *
- * @author Lukin <my@lukin.cn>
- * @version $Id: validate.php 722 2011-01-17 16:28:17Z mylukin $
- */
- class Validate {
- // private
- var $_error = array();
- var $_isVal = false;
- /**
- * ????????
- *
- * @return bool
- */
- function post(){
- return $_SERVER['REQUEST_METHOD']=='POST';
- }
- /**
- * ????????
- *
- * @return bool
- */
- function check(){
- $args = func_get_args();
- /**
- * 1.array('field',VALIDATE_TYPE,__('alert text'),params)
- * 2.array(
- * array('field',VALIDATE_TYPE,__('alert text'),params),
- * array('field',VALIDATE_TYPE,__('alert text'),params)
- * )
- */
- if (is_array($args[0])) {
- // Use method 2 rule.
- if (is_array($args[0][0])) {
- foreach ($args[0] as $rule) {
- if (!$this->check($rule)) break;
- }
- }
- // Use method 1 rule.
- else {
- return call_user_func_array(array(&$this,'check'),$args[0]);
- }
- } else {
- // Validate single rule.
- $error = false;
- $value = isset($_POST[$args[0]]) ? rawurldecode(trim($_POST[$args[0]])) : null; // POST?
- $type = $args[1]; // ??
- $text = $args[2]; // ????
- switch ($type) {
- case VALIDATE_EMPTY: case 'IS_EMPTY':
- if (empty($value)) $error = $text;
- break;
- case VALIDATE_LENGTH: case 'LENGTH_LIMIT':
- if (!is_numeric($args[3]) && strpos($args[3],'-')!==false) {
- $as = explode('-',$args[3]);
- $args[3] = $as[0];
- $args[4] = $as[1];
- }
- if (mb_strlen($value,'UTF-8') < (int)$args[3]
- || mb_strlen($value,'UTF-8') > (int)$args[4]) {
- if ($args[3]) {
- $error = sprintf($text,$args[3],$args[4]);
- } else {
- $error = sprintf($text,$args[4]);
- }
- }
- break;
- case VALIDATE_EQUAL: case 'IS_EQUAL':
- $value1 = isset($_POST[$args[3]]) ? trim($_POST[$args[3]]) : null;
- if ($value != $value1) $error = $text;
- break;
- case false: case 'FALSE':
- $error = $text;
- break;
- default:
- if (!$this->is($value,$type)) $error = $text;
- break;
- }
- // ??????
- if (!$error) return true;
- $this->_set_error($args[0],$error);
- return false;
- }
- }
- /**
- * ??????
- *
- * @param string $str ????????
- * @param mixed $type ??????????????
- * @return bool
- */
- function is($str,$type){
- if ($str === null) return $str;
- switch ($type) {
- case VALIDATE_IS_NUMERIC: case 'IS_NUMERIC':
- $pattern = '/^\d+$/';
- break;
- case VALIDATE_IS_LETTERS: case 'IS_LETTERS':
- $pattern = '/^[a-z]+$/i';
- break;
- case VALIDATE_IS_EMAIL: case 'IS_EMAIL':
- $pattern = '/^\w+([\-\+\.]\w+)*@\w+([\-\.]\w+)*\.\w+([\-\.]\w+)*$/i';
- break;
- case VALIDATE_IS_URL: case 'IS_URL':
- $pattern = '/^(http|https|ftp)\:(\/\/|\\\\)(([\w\/\\\+\-~`@\:%])+\.)+([\w\/\\\.\=\?\+\-~`@\'\:!%#]|(&)|&)+/i';
- break;
- case VALIDATE_IS_LIST: case 'IS_LIST':
- $pattern = '/^[\d\,\.]+$/i';
- break;
- case VALIDATE_IS_CNUS: case 'IS_CNUS':
- $pattern = '/^[\w\-]+$/i';
- break;
- case VALIDATE_IS_CNUSO: case 'IS_CNUSO':
- $pattern = '/^[\w\,\/\-\[\]]+$/i';
- break;
- case VALIDATE_IS_PATH: case 'IS_PATH':
- $pattern = '/^[^\:\*\<\>\|\\\\]+$/';
- break;
- default: // ?????
- $pattern = $type;
- break;
- }
- return preg_match($pattern,$str);
- }
- /**
- * ????????????
- *
- * @param bool $is_echo ??????
- * @return bool
- */
- function is_error($is_echo=true){
- if ($this->_error){
- if ($is_echo) {
- if (is_ajax()) {
- ajax_echo('Validate',$this->_error);
- }
- }
- return $this->_error;
- }
- return false;
- }
- /**
- * ??????
- *
- * @param string $id
- * @param string $text
- */
- function _set_error($id,$text){
- static $i = 0;
- $this->_error[$i]['id'] = $id;
- $this->_error[$i]['text'] = $text;
- $i++;
- }
- }
- /**
- * ??????
- *
- * @return Validate
- */
- function &_validate_get_object() {
- static $validate;
- if ( is_null($validate) )
- $validate = new Validate();
- return $validate;
- }
- /**
- * ?????post??
- *
- * @return bool
- */
- function validate_is_post() {
- $validate = _validate_get_object();
- return $validate->post();
- }
- /**
- * ??????
- *
- * @return bool
- */
- function validate_check() {
- $args = func_get_args();
- $validate = _validate_get_object();
- return $validate->check($args);
- }
- /**
- * ??????
- *
- * @return bool
- */
- function validate_is_ok() {
- $validate = _validate_get_object();
- return !$validate->is_error();
- }
- /**
- * ????
- *
- * @param $str ????????
- * @param $type ??????????????
- * @return bool
- */
- function validate_is($str,$type) {
- $validate = _validate_get_object();
- return $validate->is($str,$type);
- }