validate.init.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. if (!defined('IS_INITPHP')) exit('Access Denied!');
  3. /*********************************************************************************
  4. * InitPHP 3.8.1 国产PHP开发框架 Controller-validate 数据基础验证类
  5. *-------------------------------------------------------------------------------
  6. * 版权所有: CopyRight By initphp.com
  7. * 您可以自由使用该源码,但是在使用过程中,请保留作者信息。尊重他人劳动成果就是尊重自己
  8. *-------------------------------------------------------------------------------
  9. * Author:zhuli Dtime:2014-11-25
  10. ***********************************************************************************/
  11. class validateInit extends requestInit {
  12. /**
  13. * 数据基础验证-检测字符串长度
  14. * 规则结构:
  15. * array(
  16. * $data数组的key|验证的方法名称(例如is_email)|验证错误后的提示|参数
  17. * )
  18. * 例子:
  19. * array(
  20. * 'email|is_email|邮箱地址不正确',
  21. * 'email|is_length|长度在0-111|0|50',
  22. * 'phone|is_phone|手机号码格式不正确'
  23. * )
  24. * Controller中使用方法:$this->controller->validate($data, $rule)
  25. * @param array $data 数据
  26. * @param array $rule 规则模型
  27. * @return bool
  28. */
  29. public function validate($data, $rule) {
  30. if (!is_array($data) || !is_array($rule)) return false;
  31. foreach ($rule as $val) {
  32. $temp = explode('|', $val);
  33. $params = $temp;
  34. unset($params[0], $params[1], $params[2]);
  35. array_unshift($params, $data[$temp[0]]);
  36. $r = call_user_func_array(array($this, $temp[1]), $params);
  37. if (!$r) return $temp[2];
  38. }
  39. return true;
  40. }
  41. /**
  42. * 数据基础验证-检测字符串长度
  43. * Controller中使用方法:$this->controller->is_length($value, $min = 0, $max= 0)
  44. * @param string $value 需要验证的值
  45. * @param int $min 字符串最小长度
  46. * @param int $max 字符串最大长度
  47. * @return bool
  48. */
  49. public function is_length($value, $min = 0, $max= 0) {
  50. $value = trim($value);
  51. if ($min != 0 && strlen($value) < $min) return false;
  52. if ($max != 0 && strlen($value) > $max) return false;
  53. return true;
  54. }
  55. /**
  56. * 数据基础验证-是否必须填写的参数
  57. * Controller中使用方法:$this->controller->is_require($value)
  58. * @param string $value 需要验证的值
  59. * @return bool
  60. */
  61. public function is_require($value) {
  62. return preg_match('/.+/', trim($value));
  63. }
  64. /**
  65. * 数据基础验证-是否是空字符串
  66. * Controller中使用方法:$this->controller->is_empty($value)
  67. * @param string $value 需要验证的值
  68. * @return bool
  69. */
  70. public function is_empty($value) {
  71. if (empty($value) || $value=="") return true;
  72. return false;
  73. }
  74. /**
  75. * 数据基础验证-检测数组,数组为空时候也返回FALSH
  76. * Controller中使用方法:$this->controller->is_arr($value)
  77. * @param string $value 需要验证的值
  78. * @return bool
  79. */
  80. public function is_arr($value) {
  81. if (!is_array($value) || empty($value)) return false;
  82. return true;
  83. }
  84. /**
  85. * 数据基础验证-是否是Email 验证:xxx@qq.com
  86. * Controller中使用方法:$this->controller->is_email($value)
  87. * @param string $value 需要验证的值
  88. * @return bool
  89. */
  90. public function is_email($value) {
  91. return preg_match('/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/', trim($value));
  92. }
  93. /**
  94. * 数据基础验证-是否是IP
  95. * Controller中使用方法:$this->controller->is_ip($value)
  96. * @param string $value 需要验证的值
  97. * @return bool
  98. */
  99. public function is_ip($value) {
  100. return preg_match('/^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$/', trim($value));
  101. }
  102. /**
  103. * 数据基础验证-是否是数字类型
  104. * Controller中使用方法:$this->controller->is_number($value)
  105. * @param string $value 需要验证的值
  106. * @return bool
  107. */
  108. public function is_number($value) {
  109. return preg_match('/^\d{0,}$/', trim($value));
  110. }
  111. /**
  112. * 数据基础验证-是否是身份证
  113. * Controller中使用方法:$this->controller->is_card($value)
  114. * @param string $value 需要验证的值
  115. * @return bool
  116. */
  117. public function is_card($value){
  118. return preg_match("/^(\d{15}|\d{17}[\dx])$/i", $value);
  119. }
  120. /**
  121. * 数据基础验证-是否是电话 验证:0571-xxxxxxxx
  122. * Controller中使用方法:$this->controller->is_mobile($value)
  123. * @param string $value 需要验证的值
  124. * @return bool
  125. */
  126. public function is_mobile($value) {
  127. return preg_match('/^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/', trim($value));
  128. }
  129. /**
  130. * 数据基础验证-是否是移动电话 验证:1385810XXXX
  131. * Controller中使用方法:$this->controller->is_phone($value)
  132. * @param string $value 需要验证的值
  133. * @return bool
  134. */
  135. public function is_phone($value) {
  136. return preg_match('/^((\(\d{2,3}\))|(\d{3}\-))?(13|15)\d{9}$/', trim($value));
  137. }
  138. /**
  139. * 数据基础验证-是否是URL 验证:http://www.easyphp.cc
  140. * Controller中使用方法:$this->controller->is_url($value)
  141. * @param string $value 需要验证的值
  142. * @return bool
  143. */
  144. public function is_url($value) {
  145. return preg_match('/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/', trim($value));
  146. }
  147. /**
  148. * 数据基础验证-是否是邮政编码 验证:311100
  149. * Controller中使用方法:$this->controller->is_zip($value)
  150. * @param string $value 需要验证的值
  151. * @return bool
  152. */
  153. public function is_zip($value) {
  154. return preg_match('/^[1-9]\d{5}$/', trim($value));
  155. }
  156. /**
  157. * 数据基础验证-是否是QQ
  158. * Controller中使用方法:$this->controller->is_qq($value)
  159. * @param string $value 需要验证的值
  160. * @return bool
  161. */
  162. public function is_qq($value) {
  163. return preg_match('/^[1-9]\d{4,12}$/', trim($value));
  164. }
  165. /**
  166. * 数据基础验证-是否是英文字母
  167. * Controller中使用方法:$this->controller->is_english($value)
  168. * @param string $value 需要验证的值
  169. * @return bool
  170. */
  171. public function is_english($value) {
  172. return preg_match('/^[A-Za-z]+$/', trim($value));
  173. }
  174. /**
  175. * 数据基础验证-是否是中文
  176. * Controller中使用方法:$this->controller->is_chinese($value)
  177. * @param string $value 需要验证的值
  178. * @return bool
  179. */
  180. public function is_chinese($value) {
  181. return preg_match("/^([\xE4-\xE9][\x80-\xBF][\x80-\xBF])+$/", trim($value));
  182. }
  183. /**
  184. * 检查对象中是否有可调用函数
  185. * Controller中使用方法:$this->controller->is_method($object, $method)
  186. * @param string $object
  187. * @param string $method
  188. * @return bool
  189. */
  190. public function is_method($object, $method) {
  191. $method = strtolower ( $method );
  192. return method_exists($object, $method) && is_callable (array($object, $method));
  193. }
  194. /**
  195. * 检查是否是安全的账号
  196. * Controller中使用方法:$this->controller->is_safe_account($value)
  197. * @param string $value
  198. * @return bool
  199. */
  200. public function is_safe_account($value) {
  201. return preg_match ("/^[a-zA-Z]{1}[a-zA-Z0-9_\.]{3,31}$/", $value);
  202. }
  203. /**
  204. * 检查是否是安全的昵称
  205. * Controller中使用方法:$this->controller->is_safe_nickname()
  206. * @param string $value
  207. * @return bool
  208. */
  209. public function is_safe_nickname($value) {
  210. return preg_match ("/^[-\x{4e00}-\x{9fa5}a-zA-Z0-9_\.]{2,10}$/u", $value);
  211. }
  212. /**
  213. * 检查是否是安全的密码
  214. * Controller中使用方法:$this->controller->is_safe_password()
  215. * @param string $str
  216. * @return bool
  217. */
  218. public function is_safe_password($str) {
  219. if (preg_match('/[\x80-\xff]./', $str) || preg_match('/\'|"|\"/', $str) || strlen($str) < 6 || strlen($str) > 20 ){
  220. return false;
  221. }
  222. return true;
  223. }
  224. /**
  225. * 检查是否是正确的标识符
  226. * 1. 以字母或下划线开始,后面跟着任何字母,数字或下划线。
  227. * 2. 使用方法【Controller】:
  228. * <code>$this->controller->is_identifier($value)</code>
  229. **/
  230. public function is_identifier($value) {
  231. return preg_match('/^[a-zA-Z_][a-zA-Z0-9_]+$/', trim($value));
  232. }
  233. }