/ipserver/FLEA/FLEA/Helper/Verifier.php
PHP | 373 lines | 195 code | 24 blank | 154 comment | 71 complexity | a524d5ac628f18570f819032a543b3e4 MD5 | raw file
- <?php
- /////////////////////////////////////////////////////////////////////////////
- // FleaPHP Framework
- //
- // Copyright (c) 2005 - 2008 QeeYuan China Inc. (http://www.qeeyuan.com)
- //
- // ??????????????? LICENSE.txt ???
- // ???? http://www.fleaphp.org/ ???????
- /////////////////////////////////////////////////////////////////////////////
-
- /**
- * ?? FLEA_Helper_Verifier ?
- *
- * @copyright Copyright (c) 2005 - 2008 QeeYuan China Inc. (http://www.qeeyuan.com)
- * @author ???? (www.qeeyuan.com)
- * @package Core
- * @version $Id: Verifier.php,v 1.1 2008/09/10 03:00:12 jiang Exp $
- */
-
- /**
- * FLEA_Helper_Verifier ?????????????????????
- *
- * ?????????????????????????
- *
- * ???????????????
- * name: ???
- * type: ????
- * simpleType: ??????
- * maxLength: ????
- * notNull: ??????? NULL ?
- * notEmpty: ??????????
- * binary: ????????
- * unsigned: ????????
- * hasDefault: ??????
- * defaultValue: ???
- *
- * ?? notNull ? true?? hasDefault ? false?????????????
- *
- * simpleType ?????????
- * C - ?????? 250 ????
- * X - ???? 250 ????
- * B - ?????
- * N - ???????
- * D - ??
- * T - TimeStamp
- * L - ?????
- * I - ??
- * R - ????????
- *
- * ??????? SDBO::metaColumns() ??????????????
- * ??????? metaColumns() ????????????
- *
- * ????????????????????????
- *
- * complexType: ??????
- * min: ?????????????
- * max: ?????????????
- * minLength: ??????????????????
- * maxLength: ??????????????????
- *
- * ?? complexType ??????????
- * NUMBER - ??????????
- * INT - ??
- * ASCII - ASCII ???????????? 127 ????
- * EMAIL - Email ??
- * DATE - ????? GNU Date Input Formats??? yyyy/mm/dd?yyyy-mm-dd?
- * TIME - ????? GNU Date Input Formats??? hh:mm:ss?
- * IPv4 - IPv4 ?????? a.b.c.h?
- * OCTAL - ?????
- * BINARY - ?????
- * HEX - ??????
- * DOMAIN - Internet ??
- * ANY - ????
- * STRING - ????????????
- * ALPHANUM - ??????26????0?9?
- * ALPHA - ???26????
- * ALPHANUMX - 26????10????? _ ??
- *
- * ???????? verifier.js ???????????????
- * ????????????????????
- *
- * notNull, hasDefault, min, max, minLength, maxLength
- *
- *
- * @package Core
- * @author ???? (www.qeeyuan.com)
- * @version 1.0
- */
- class FLEA_Helper_Verifier
- {
- /**
- * ?????????????
- *
- * @param array $data
- * @param array $rules
- * @param mixed $skip
- *
- * @return array
- */
- function checkAll(& $data, & $rules, $skip = 0)
- {
- $result = array();
- foreach ($rules as $rule) {
- $name = $rule['name'];
- if ($skip === 1 || $skip === true || $skip === 'empty') {
- if (!isset($data[$name]) || empty($data[$name])) { continue; }
- } elseif ($skip === 2 || $skip === 'noset') {
- if (!isset($data[$name])) { continue; }
- }
-
- do {
- // ?? notNull ? true?? hasDefault ? false????????????
- if (isset($rule['notNull'])) {
- if ($rule['notNull']) {
- if (isset($rule['hasDefault']) && $rule['hasDefault']) { break; }
- if (isset($rule['simpleType']) && $rule['simpleType'] == 'R') {
- break;
- }
- if (!isset($data[$name]) || is_null($data[$name])) {
- $result[$name] = array('check' => 'notNull', 'rule' => $rule);
- break;
- }
- } else {
- if (!isset($data[$name]) || is_null($data[$name]) || $data[$name] === '') { break; }
- }
- }
- if (isset($rule['notEmpty'])) {
- if ($rule['notEmpty']) {
- if (isset($rule['hasDefault']) && $rule['hasDefault']) { break; }
- if (isset($rule['simpleType']) && $rule['simpleType'] == 'R') {
- break;
- }
- if (!isset($data[$name]) || $data[$name] == '') {
- $result[$name] = array('check' => 'notEmpty', 'rule' => $rule);
- break;
- }
- } else {
- if (!isset($data[$name]) || $data[$name] == '') {
- break;
- }
- }
- }
-
- $ret = $this->check($data[$name], $rule);
- if ($ret !== true) {
- $result[$name] = array('check' => $ret, 'rule' => $rule);
- }
- } while (false);
- }
- return $result;
- }
-
- /**
- * ??????????????? ture???????????????
- *
- * @param mixed $value
- * @param array $rule
- *
- * @return boolean
- */
- function check($value, & $rule)
- {
- // ???? simpleType ?????? simpleType ?????
- $checkLength = false;
- $checkMinMax = false;
- $ret = 'simpleType';
- if (isset($rule['simpleType'])) {
- switch ($rule['simpleType']) {
- case 'C': // ?????? 250 ????
- if (strlen($value) > 250) { return $ret; }
- $checkLength = true;
- break;
- case 'N': // ???????
- if (!is_numeric($value)) { return $ret; }
- $checkMinMax = true;
- break;
- case 'D': // ??
- $test = @strtotime($value);
- if ($test === false || $test === -1) { return $ret; }
- break;
- case 'I': // ??
- if (!is_numeric($value)) { return $ret; }
- if (intval($value) != $value) { return $ret; }
- $checkMinMax = true;
- break;
- case 'X': // ???? 250 ????
- case 'B': // ?????
- $checkLength = true;
- break;
- case 'T': // TimeStamp
- case 'L': // ?????
- break;
- case 'R': // ????????
- $checkMinMax = true;
- break;
- default:
- }
- } else {
- $checkLength = true;
- $checkMinMax = true;
- }
-
- // ???? complexType ?????? complexType ?????
- $ret = 'complexType';
- if (isset($rule['complexType'])) {
- $func = 'is' . $rule['complexType'];
- if (!method_exists($this, $func)) {
- FLEA::loadClass('FLEA_Exception_InvalidArguments');
- __THROW(new FLEA_Exception_InvalidArguments('$rule[\'complexType\']',
- $rule['complexType']));
- return null;
- }
- if (!$this->{$func}($value)) { return $ret; }
- }
-
- // min/max/minLength/maxLength ??
- if ($checkMinMax) {
- $ret = 'min';
- if (isset($rule['min']) && $value < $rule['min']) { return $ret; }
- $ret = 'max';
- if (isset($rule['max']) && $value > $rule['max']) { return $ret; }
- }
- $ret = 'length';
- if ($checkLength) {
- $ret = 'minLength';
- if (isset($rule['minLength']) && $rule['minLength'] > 0 &&
- strlen($value) < $rule['minLength']) {
- return $ret;
- }
- $ret = 'maxLength';
- if (isset($rule['maxLength']) && $rule['maxLength'] > 0 &&
- strlen($value) > $rule['maxLength']) {
- return $ret;
- }
- }
- $ret = null;
-
- return true;
- }
-
- /**
- * ??
- */
- function isNUMBER($value)
- {
- return is_numeric($value);
- }
-
- /**
- * ??
- */
- function isINT($value)
- {
- return strlen(intval($value)) == strlen($value) && is_numeric($value);
- }
-
- /**
- * ASCII ???????????? 127 ????
- */
- function isASCII($value)
- {
- $ar = array();
- $count = preg_match_all('/[\x20-\x7f]/', $value, $ar);
- return $count == strlen($value);
- }
-
- /**
- * Email ??
- */
- function isEMAIL($value)
- {
- return preg_match('/^[A-Za-z0-9]+([._\-\+]*[A-Za-z0-9]+)*@([A-Za-z0-9]+[-A-Za-z0-9]*[A-Za-z0-9]+\.)+[A-Za-z0-9]+$/', $value) != 0;
- }
-
- /**
- * ????? GNU Date Input Formats??? yyyy/mm/dd?yyyy-mm-dd?
- */
- function isDATE($value)
- {
- $test = @strtotime($value);
- return $test !== -1 && $test !== false;
- }
-
- /**
- * ????? GNU Date Input Formats??? hh:mm:ss?
- */
- function isTIME($value)
- {
- $test = strtotime($value);
- return $test !== -1 && $test !== false;
- }
-
- /**
- * IPv4 ?????? a.b.c.h?
- */
- function isIPv4($value)
- {
- $test = ip2long($value);
- return $test !== -1 && $test !== false;
- }
-
- /**
- * ?????
- */
- function isOCTAL($value)
- {
- return preg_match('/0[0-7]+/', $value) != 0;
- }
-
- /**
- * ?????
- */
- function isBINARY($value)
- {
- return preg_match('/[01]+/', $value) != 0;
- }
-
- /**
- * ??????
- */
- function isHEX($value)
- {
- return preg_match('/[0-9a-f]+/i', $value) != 0;
- }
-
- /**
- * Internet ??
- */
- function isDOMAIN($value)
- {
- return preg_match('/[a-z0-9\.]+/i', $value) != 0;
- }
-
- /**
- * ????
- */
- function isANY()
- {
- return true;
- }
-
- /**
- * ????????????
- */
- function isSTRING()
- {
- return true;
- }
-
- /**
- * ??????26????0?9?
- */
- function isALPHANUM($value)
- {
- return ctype_alnum($value);
- }
-
- /**
- * ???26????
- */
- function isALPHA($value)
- {
- return ctype_alpha($value);
- }
-
- /**
- * 26????10???
- */
- function isALPHANUMX($value)
- {
- return preg_match('/^[A-Za-z0-9_]+$/', $value) != 0;
- }
- }