PageRenderTime 48ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/ipserver/FLEA/FLEA/Helper/Verifier.php

http://ipmanger.googlecode.com/
PHP | 373 lines | 195 code | 24 blank | 154 comment | 71 complexity | a524d5ac628f18570f819032a543b3e4 MD5 | raw file
  1. <?php
  2. /////////////////////////////////////////////////////////////////////////////
  3. // FleaPHP Framework
  4. //
  5. // Copyright (c) 2005 - 2008 QeeYuan China Inc. (http://www.qeeyuan.com)
  6. //
  7. // ??????????????? LICENSE.txt ???
  8. // ???? http://www.fleaphp.org/ ???????
  9. /////////////////////////////////////////////////////////////////////////////
  10. /**
  11. * ?? FLEA_Helper_Verifier ?
  12. *
  13. * @copyright Copyright (c) 2005 - 2008 QeeYuan China Inc. (http://www.qeeyuan.com)
  14. * @author ???? (www.qeeyuan.com)
  15. * @package Core
  16. * @version $Id: Verifier.php,v 1.1 2008/09/10 03:00:12 jiang Exp $
  17. */
  18. /**
  19. * FLEA_Helper_Verifier ?????????????????????
  20. *
  21. * ?????????????????????????
  22. *
  23. * ???????????????
  24. * name: ???
  25. * type: ????
  26. * simpleType: ??????
  27. * maxLength: ????
  28. * notNull: ??????? NULL ?
  29. * notEmpty: ??????????
  30. * binary: ????????
  31. * unsigned: ????????
  32. * hasDefault: ??????
  33. * defaultValue: ???
  34. *
  35. * ?? notNull ? true?? hasDefault ? false?????????????
  36. *
  37. * simpleType ?????????
  38. * C - ?????? 250 ????
  39. * X - ???? 250 ????
  40. * B - ?????
  41. * N - ???????
  42. * D - ??
  43. * T - TimeStamp
  44. * L - ?????
  45. * I - ??
  46. * R - ????????
  47. *
  48. * ??????? SDBO::metaColumns() ??????????????
  49. * ??????? metaColumns() ????????????
  50. *
  51. * ????????????????????????
  52. *
  53. * complexType: ??????
  54. * min: ?????????????
  55. * max: ?????????????
  56. * minLength: ??????????????????
  57. * maxLength: ??????????????????
  58. *
  59. * ?? complexType ??????????
  60. * NUMBER - ??????????
  61. * INT - ??
  62. * ASCII - ASCII ???????????? 127 ????
  63. * EMAIL - Email ??
  64. * DATE - ????? GNU Date Input Formats??? yyyy/mm/dd?yyyy-mm-dd?
  65. * TIME - ????? GNU Date Input Formats??? hh:mm:ss?
  66. * IPv4 - IPv4 ?????? a.b.c.h?
  67. * OCTAL - ?????
  68. * BINARY - ?????
  69. * HEX - ??????
  70. * DOMAIN - Internet ??
  71. * ANY - ????
  72. * STRING - ????????????
  73. * ALPHANUM - ??????26????0?9?
  74. * ALPHA - ???26????
  75. * ALPHANUMX - 26????10????? _ ??
  76. *
  77. * ???????? verifier.js ???????????????
  78. * ????????????????????
  79. *
  80. * notNull, hasDefault, min, max, minLength, maxLength
  81. *
  82. *
  83. * @package Core
  84. * @author ???? (www.qeeyuan.com)
  85. * @version 1.0
  86. */
  87. class FLEA_Helper_Verifier
  88. {
  89. /**
  90. * ?????????????
  91. *
  92. * @param array $data
  93. * @param array $rules
  94. * @param mixed $skip
  95. *
  96. * @return array
  97. */
  98. function checkAll(& $data, & $rules, $skip = 0)
  99. {
  100. $result = array();
  101. foreach ($rules as $rule) {
  102. $name = $rule['name'];
  103. if ($skip === 1 || $skip === true || $skip === 'empty') {
  104. if (!isset($data[$name]) || empty($data[$name])) { continue; }
  105. } elseif ($skip === 2 || $skip === 'noset') {
  106. if (!isset($data[$name])) { continue; }
  107. }
  108. do {
  109. // ?? notNull ? true?? hasDefault ? false????????????
  110. if (isset($rule['notNull'])) {
  111. if ($rule['notNull']) {
  112. if (isset($rule['hasDefault']) && $rule['hasDefault']) { break; }
  113. if (isset($rule['simpleType']) && $rule['simpleType'] == 'R') {
  114. break;
  115. }
  116. if (!isset($data[$name]) || is_null($data[$name])) {
  117. $result[$name] = array('check' => 'notNull', 'rule' => $rule);
  118. break;
  119. }
  120. } else {
  121. if (!isset($data[$name]) || is_null($data[$name]) || $data[$name] === '') { break; }
  122. }
  123. }
  124. if (isset($rule['notEmpty'])) {
  125. if ($rule['notEmpty']) {
  126. if (isset($rule['hasDefault']) && $rule['hasDefault']) { break; }
  127. if (isset($rule['simpleType']) && $rule['simpleType'] == 'R') {
  128. break;
  129. }
  130. if (!isset($data[$name]) || $data[$name] == '') {
  131. $result[$name] = array('check' => 'notEmpty', 'rule' => $rule);
  132. break;
  133. }
  134. } else {
  135. if (!isset($data[$name]) || $data[$name] == '') {
  136. break;
  137. }
  138. }
  139. }
  140. $ret = $this->check($data[$name], $rule);
  141. if ($ret !== true) {
  142. $result[$name] = array('check' => $ret, 'rule' => $rule);
  143. }
  144. } while (false);
  145. }
  146. return $result;
  147. }
  148. /**
  149. * ??????????????? ture???????????????
  150. *
  151. * @param mixed $value
  152. * @param array $rule
  153. *
  154. * @return boolean
  155. */
  156. function check($value, & $rule)
  157. {
  158. // ???? simpleType ?????? simpleType ?????
  159. $checkLength = false;
  160. $checkMinMax = false;
  161. $ret = 'simpleType';
  162. if (isset($rule['simpleType'])) {
  163. switch ($rule['simpleType']) {
  164. case 'C': // ?????? 250 ????
  165. if (strlen($value) > 250) { return $ret; }
  166. $checkLength = true;
  167. break;
  168. case 'N': // ???????
  169. if (!is_numeric($value)) { return $ret; }
  170. $checkMinMax = true;
  171. break;
  172. case 'D': // ??
  173. $test = @strtotime($value);
  174. if ($test === false || $test === -1) { return $ret; }
  175. break;
  176. case 'I': // ??
  177. if (!is_numeric($value)) { return $ret; }
  178. if (intval($value) != $value) { return $ret; }
  179. $checkMinMax = true;
  180. break;
  181. case 'X': // ???? 250 ????
  182. case 'B': // ?????
  183. $checkLength = true;
  184. break;
  185. case 'T': // TimeStamp
  186. case 'L': // ?????
  187. break;
  188. case 'R': // ????????
  189. $checkMinMax = true;
  190. break;
  191. default:
  192. }
  193. } else {
  194. $checkLength = true;
  195. $checkMinMax = true;
  196. }
  197. // ???? complexType ?????? complexType ?????
  198. $ret = 'complexType';
  199. if (isset($rule['complexType'])) {
  200. $func = 'is' . $rule['complexType'];
  201. if (!method_exists($this, $func)) {
  202. FLEA::loadClass('FLEA_Exception_InvalidArguments');
  203. __THROW(new FLEA_Exception_InvalidArguments('$rule[\'complexType\']',
  204. $rule['complexType']));
  205. return null;
  206. }
  207. if (!$this->{$func}($value)) { return $ret; }
  208. }
  209. // min/max/minLength/maxLength ??
  210. if ($checkMinMax) {
  211. $ret = 'min';
  212. if (isset($rule['min']) && $value < $rule['min']) { return $ret; }
  213. $ret = 'max';
  214. if (isset($rule['max']) && $value > $rule['max']) { return $ret; }
  215. }
  216. $ret = 'length';
  217. if ($checkLength) {
  218. $ret = 'minLength';
  219. if (isset($rule['minLength']) && $rule['minLength'] > 0 &&
  220. strlen($value) < $rule['minLength']) {
  221. return $ret;
  222. }
  223. $ret = 'maxLength';
  224. if (isset($rule['maxLength']) && $rule['maxLength'] > 0 &&
  225. strlen($value) > $rule['maxLength']) {
  226. return $ret;
  227. }
  228. }
  229. $ret = null;
  230. return true;
  231. }
  232. /**
  233. * ??
  234. */
  235. function isNUMBER($value)
  236. {
  237. return is_numeric($value);
  238. }
  239. /**
  240. * ??
  241. */
  242. function isINT($value)
  243. {
  244. return strlen(intval($value)) == strlen($value) && is_numeric($value);
  245. }
  246. /**
  247. * ASCII ???????????? 127 ????
  248. */
  249. function isASCII($value)
  250. {
  251. $ar = array();
  252. $count = preg_match_all('/[\x20-\x7f]/', $value, $ar);
  253. return $count == strlen($value);
  254. }
  255. /**
  256. * Email ??
  257. */
  258. function isEMAIL($value)
  259. {
  260. 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;
  261. }
  262. /**
  263. * ????? GNU Date Input Formats??? yyyy/mm/dd?yyyy-mm-dd?
  264. */
  265. function isDATE($value)
  266. {
  267. $test = @strtotime($value);
  268. return $test !== -1 && $test !== false;
  269. }
  270. /**
  271. * ????? GNU Date Input Formats??? hh:mm:ss?
  272. */
  273. function isTIME($value)
  274. {
  275. $test = strtotime($value);
  276. return $test !== -1 && $test !== false;
  277. }
  278. /**
  279. * IPv4 ?????? a.b.c.h?
  280. */
  281. function isIPv4($value)
  282. {
  283. $test = ip2long($value);
  284. return $test !== -1 && $test !== false;
  285. }
  286. /**
  287. * ?????
  288. */
  289. function isOCTAL($value)
  290. {
  291. return preg_match('/0[0-7]+/', $value) != 0;
  292. }
  293. /**
  294. * ?????
  295. */
  296. function isBINARY($value)
  297. {
  298. return preg_match('/[01]+/', $value) != 0;
  299. }
  300. /**
  301. * ??????
  302. */
  303. function isHEX($value)
  304. {
  305. return preg_match('/[0-9a-f]+/i', $value) != 0;
  306. }
  307. /**
  308. * Internet ??
  309. */
  310. function isDOMAIN($value)
  311. {
  312. return preg_match('/[a-z0-9\.]+/i', $value) != 0;
  313. }
  314. /**
  315. * ????
  316. */
  317. function isANY()
  318. {
  319. return true;
  320. }
  321. /**
  322. * ????????????
  323. */
  324. function isSTRING()
  325. {
  326. return true;
  327. }
  328. /**
  329. * ??????26????0?9?
  330. */
  331. function isALPHANUM($value)
  332. {
  333. return ctype_alnum($value);
  334. }
  335. /**
  336. * ???26????
  337. */
  338. function isALPHA($value)
  339. {
  340. return ctype_alpha($value);
  341. }
  342. /**
  343. * 26????10???
  344. */
  345. function isALPHANUMX($value)
  346. {
  347. return preg_match('/^[A-Za-z0-9_]+$/', $value) != 0;
  348. }
  349. }