PageRenderTime 80ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/common/system/validate.php

http://lazycms.googlecode.com/
PHP | 251 lines | 144 code | 4 blank | 103 comment | 23 complexity | 8fed9ea71025d97db5a8c9fff6ad6572 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * +---------------------------------------------------------------------------+
  4. * | LL LLLL LL L LLLL LLLL |
  5. * | LL LL L LLL LL LL L LL LL |
  6. * | LL LLLL LLLLL LL LL LL LLLL LLL LL LL LL LL |
  7. * | LL LL LL LL LL LL L LLL LL LLLLL LL LL LL |
  8. * | LL LLLLL LL LLLL LL L L LL LLLLL LL LL LL |
  9. * | LL LL LL LL LLLL LL L LL LL LLLL LL |
  10. * | LL LL LL LL LL LL L L LL L LL LLLL LL |
  11. * | LLLLLL LLLLL LLLLL LL LLLL L LL LLLL LL LLLLLL |
  12. * | LL |
  13. * | LL |
  14. * +---------------------------------------------------------------------------+
  15. * | Copyright (C) 2007-2010 LazyCMS.com All rights reserved. |
  16. * +---------------------------------------------------------------------------+
  17. * | LazyCMS is free software. See LICENSE for copyright notices and details. |
  18. * +---------------------------------------------------------------------------+
  19. */
  20. defined('COM_PATH') or die('Restricted access!');
  21. // ??
  22. define('VALIDATE_IS_NUMERIC','IS_NUMERIC');
  23. // ??
  24. define('VALIDATE_IS_LETTERS','IS_LETTERS');
  25. //????
  26. define('VALIDATE_IS_EMAIL','IS_EMAIL');
  27. // ???
  28. define('VALIDATE_IS_URL','IS_URL');
  29. // ?????????
  30. define('VALIDATE_IS_LIST','IS_LIST');
  31. // ???????????
  32. define('VALIDATE_IS_CNUS','IS_CNUS');
  33. // ???????????????[?]
  34. define('VALIDATE_IS_CNUSO','IS_CNUSO');
  35. // ????
  36. define('VALIDATE_IS_PATH','IS_PATH');
  37. // ????
  38. define('VALIDATE_EMPTY','EMPTY');
  39. // ????
  40. define('VALIDATE_LENGTH','LENGTH');
  41. // ???????
  42. define('VALIDATE_EQUAL','EQUAL');
  43. /**
  44. * LazyCMS ?????
  45. *
  46. * @author Lukin <my@lukin.cn>
  47. * @version $Id: validate.php 722 2011-01-17 16:28:17Z mylukin $
  48. */
  49. class Validate {
  50. // private
  51. var $_error = array();
  52. var $_isVal = false;
  53. /**
  54. * ????????
  55. *
  56. * @return bool
  57. */
  58. function post(){
  59. return $_SERVER['REQUEST_METHOD']=='POST';
  60. }
  61. /**
  62. * ????????
  63. *
  64. * @return bool
  65. */
  66. function check(){
  67. $args = func_get_args();
  68. /**
  69. * 1.array('field',VALIDATE_TYPE,__('alert text'),params)
  70. * 2.array(
  71. * array('field',VALIDATE_TYPE,__('alert text'),params),
  72. * array('field',VALIDATE_TYPE,__('alert text'),params)
  73. * )
  74. */
  75. if (is_array($args[0])) {
  76. // Use method 2 rule.
  77. if (is_array($args[0][0])) {
  78. foreach ($args[0] as $rule) {
  79. if (!$this->check($rule)) break;
  80. }
  81. }
  82. // Use method 1 rule.
  83. else {
  84. return call_user_func_array(array(&$this,'check'),$args[0]);
  85. }
  86. } else {
  87. // Validate single rule.
  88. $error = false;
  89. $value = isset($_POST[$args[0]]) ? rawurldecode(trim($_POST[$args[0]])) : null; // POST?
  90. $type = $args[1]; // ??
  91. $text = $args[2]; // ????
  92. switch ($type) {
  93. case VALIDATE_EMPTY: case 'IS_EMPTY':
  94. if (empty($value)) $error = $text;
  95. break;
  96. case VALIDATE_LENGTH: case 'LENGTH_LIMIT':
  97. if (!is_numeric($args[3]) && strpos($args[3],'-')!==false) {
  98. $as = explode('-',$args[3]);
  99. $args[3] = $as[0];
  100. $args[4] = $as[1];
  101. }
  102. if (mb_strlen($value,'UTF-8') < (int)$args[3]
  103. || mb_strlen($value,'UTF-8') > (int)$args[4]) {
  104. if ($args[3]) {
  105. $error = sprintf($text,$args[3],$args[4]);
  106. } else {
  107. $error = sprintf($text,$args[4]);
  108. }
  109. }
  110. break;
  111. case VALIDATE_EQUAL: case 'IS_EQUAL':
  112. $value1 = isset($_POST[$args[3]]) ? trim($_POST[$args[3]]) : null;
  113. if ($value != $value1) $error = $text;
  114. break;
  115. case false: case 'FALSE':
  116. $error = $text;
  117. break;
  118. default:
  119. if (!$this->is($value,$type)) $error = $text;
  120. break;
  121. }
  122. // ??????
  123. if (!$error) return true;
  124. $this->_set_error($args[0],$error);
  125. return false;
  126. }
  127. }
  128. /**
  129. * ??????
  130. *
  131. * @param string $str ????????
  132. * @param mixed $type ??????????????
  133. * @return bool
  134. */
  135. function is($str,$type){
  136. if ($str === null) return $str;
  137. switch ($type) {
  138. case VALIDATE_IS_NUMERIC: case 'IS_NUMERIC':
  139. $pattern = '/^\d+$/';
  140. break;
  141. case VALIDATE_IS_LETTERS: case 'IS_LETTERS':
  142. $pattern = '/^[a-z]+$/i';
  143. break;
  144. case VALIDATE_IS_EMAIL: case 'IS_EMAIL':
  145. $pattern = '/^\w+([\-\+\.]\w+)*@\w+([\-\.]\w+)*\.\w+([\-\.]\w+)*$/i';
  146. break;
  147. case VALIDATE_IS_URL: case 'IS_URL':
  148. $pattern = '/^(http|https|ftp)\:(\/\/|\\\\)(([\w\/\\\+\-~`@\:%])+\.)+([\w\/\\\.\=\?\+\-~`@\'\:!%#]|(&amp;)|&)+/i';
  149. break;
  150. case VALIDATE_IS_LIST: case 'IS_LIST':
  151. $pattern = '/^[\d\,\.]+$/i';
  152. break;
  153. case VALIDATE_IS_CNUS: case 'IS_CNUS':
  154. $pattern = '/^[\w\-]+$/i';
  155. break;
  156. case VALIDATE_IS_CNUSO: case 'IS_CNUSO':
  157. $pattern = '/^[\w\,\/\-\[\]]+$/i';
  158. break;
  159. case VALIDATE_IS_PATH: case 'IS_PATH':
  160. $pattern = '/^[^\:\*\<\>\|\\\\]+$/';
  161. break;
  162. default: // ?????
  163. $pattern = $type;
  164. break;
  165. }
  166. return preg_match($pattern,$str);
  167. }
  168. /**
  169. * ????????????
  170. *
  171. * @param bool $is_echo ??????
  172. * @return bool
  173. */
  174. function is_error($is_echo=true){
  175. if ($this->_error){
  176. if ($is_echo) {
  177. if (is_ajax()) {
  178. ajax_echo('Validate',$this->_error);
  179. }
  180. }
  181. return $this->_error;
  182. }
  183. return false;
  184. }
  185. /**
  186. * ??????
  187. *
  188. * @param string $id
  189. * @param string $text
  190. */
  191. function _set_error($id,$text){
  192. static $i = 0;
  193. $this->_error[$i]['id'] = $id;
  194. $this->_error[$i]['text'] = $text;
  195. $i++;
  196. }
  197. }
  198. /**
  199. * ??????
  200. *
  201. * @return Validate
  202. */
  203. function &_validate_get_object() {
  204. static $validate;
  205. if ( is_null($validate) )
  206. $validate = new Validate();
  207. return $validate;
  208. }
  209. /**
  210. * ?????post??
  211. *
  212. * @return bool
  213. */
  214. function validate_is_post() {
  215. $validate = _validate_get_object();
  216. return $validate->post();
  217. }
  218. /**
  219. * ??????
  220. *
  221. * @return bool
  222. */
  223. function validate_check() {
  224. $args = func_get_args();
  225. $validate = _validate_get_object();
  226. return $validate->check($args);
  227. }
  228. /**
  229. * ??????
  230. *
  231. * @return bool
  232. */
  233. function validate_is_ok() {
  234. $validate = _validate_get_object();
  235. return !$validate->is_error();
  236. }
  237. /**
  238. * ????
  239. *
  240. * @param $str ????????
  241. * @param $type ??????????????
  242. * @return bool
  243. */
  244. function validate_is($str,$type) {
  245. $validate = _validate_get_object();
  246. return $validate->is($str,$type);
  247. }