ValidateExtend.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\library;
  12. use think\Validate;
  13. /**
  14. * thinkphp 框架表单验证Validate的扩展类
  15. * @author setsu
  16. *
  17. */
  18. class ValidateExtend extends Validate
  19. {
  20. public function __construct(array $rules = [], array $message = [], array $field = [])
  21. {
  22. parent::__construct($rules, $message, $field);
  23. parent::$typeMsg['isEmpty'] = ':attribute不能为空值';
  24. }
  25. /**
  26. * 重构父类Validate的is方法,增加验证规则
  27. * @access protected
  28. * @param mixed $value 字段值
  29. * @param string $rule 验证规则
  30. * @param array $data 验证数据
  31. * @return bool
  32. */
  33. public function is($value, $rule, $data = [])
  34. {
  35. switch ($rule) {
  36. case 'mobile': // 手机号码验证
  37. $result = $this->regex($value, '/^(13[0-9]|14[0-9]|15[0-9]|16[0-9]|17[0-9]|18[0-9]|19[0-9])[0-9]{8}$/');
  38. break;
  39. case 'positiveInteger': //正整数
  40. $result = $this->regex($value, '/^[1-9][0-9]*$/');
  41. break;
  42. default:
  43. $result = parent::is($value, $rule, $data);
  44. }
  45. return $result;
  46. }
  47. /**
  48. * 身份证号码格式验证
  49. */
  50. protected function identityNumber($value)
  51. {
  52. $vCity = [
  53. '11',
  54. '12',
  55. '13',
  56. '14',
  57. '15',
  58. '21',
  59. '22',
  60. '23',
  61. '31',
  62. '32',
  63. '33',
  64. '34',
  65. '35',
  66. '36',
  67. '37',
  68. '41',
  69. '42',
  70. '43',
  71. '44',
  72. '45',
  73. '46',
  74. '50',
  75. '51',
  76. '52',
  77. '53',
  78. '54',
  79. '61',
  80. '62',
  81. '63',
  82. '64',
  83. '65',
  84. '71',
  85. '81',
  86. '82',
  87. '91',
  88. ];
  89. if (!preg_match('/^([\d]{17}[xX\d]|[\d]{15})$/', $value)) {
  90. return false;
  91. }
  92. if (!in_array(substr($value, 0, 2), $vCity)) {
  93. return false;
  94. } else {
  95. $value = preg_replace('/[xX]$/i', 'a', $value);
  96. }
  97. $vLength = strlen($value);
  98. if ($vLength == 18) {
  99. $vBirthday = substr($value, 6, 4) . '-' . substr($value, 10, 2) . '-' . substr($value, 12, 2);
  100. } else {
  101. $vBirthday = '19' . substr($value, 6, 2) . '-' . substr($value, 8, 2) . '-' . substr($value, 10, 2);
  102. }
  103. if (date('Y-m-d', strtotime($vBirthday)) != $vBirthday) {
  104. return false;
  105. }
  106. if ($vLength == 18) {
  107. $vSum = 0;
  108. for ($i = 17; $i >= 0; $i--) {
  109. $vSubStr = substr($value, 17 - $i, 1);
  110. $vSum += (pow(2, $i) % 11) * (($vSubStr == 'a') ? 10 : intval($vSubStr, 11));
  111. }
  112. if ($vSum % 11 != 1) {
  113. return false;
  114. }
  115. }
  116. return true;
  117. }
  118. /**
  119. * 密码强度检测
  120. * Password规则调整:至少$length位,密码至少要包含字母、数字、符号中的两种,
  121. * ******************规则结果:密码强度大于等于2的通过
  122. *
  123. * @param $value 密码
  124. * @param $rule 要求的最小密码强度
  125. *
  126. * @return boolean
  127. *
  128. */
  129. public function passwordStrength($value, $rule)
  130. {
  131. if (empty($value)) {
  132. return false;
  133. }
  134. $strong = 0;
  135. if (preg_match('/[0-9]+/', $value)) {
  136. $strong++;
  137. }
  138. if (preg_match('/[a-zA-Z]+/', $value)) {
  139. $strong++;
  140. }
  141. if (preg_match('/[^A-Za-z0-9]+/', $value)) {
  142. $strong++;
  143. }
  144. return $strong >= $rule;
  145. }
  146. /**
  147. * 判断值是否为空
  148. * @param $value
  149. * @return bool
  150. */
  151. public function isEmpty($value)
  152. {
  153. if (is_string($value)) {
  154. $value = trim($value);
  155. }
  156. if (empty($value)) {
  157. return false;
  158. }
  159. return true;
  160. }
  161. /**
  162. * 递归验证数组数据
  163. * @param $value
  164. * @param $rule
  165. * @return bool
  166. */
  167. public function recursiveCheck($value, $rule)
  168. {
  169. $flag = true;
  170. array_walk_recursive($value, function ($item, $key) use ($rule, &$flag) {
  171. if (true !== parent::is($item, $rule)) {
  172. $flag = false;
  173. var_dump($item, $rule);
  174. }
  175. });
  176. return $flag;
  177. }
  178. /**
  179. * 是否有效的私钥
  180. * @param $private_key string
  181. * @return bool
  182. */
  183. public function isValidPrivateKey($private_key)
  184. {
  185. if (empty($private_key)) {
  186. return false;
  187. }
  188. $resource_id = openssl_pkey_get_private($private_key);
  189. if (empty($resource_id)) {
  190. return false;
  191. }
  192. return true;
  193. }
  194. /**
  195. * 是否有效的公钥
  196. * @param $public_key string
  197. * @return bool
  198. */
  199. public function isValidPublicKey($public_key)
  200. {
  201. if (empty($public_key)) {
  202. return false;
  203. }
  204. $resource_id = openssl_pkey_get_public($public_key);
  205. if (empty($resource_id)) {
  206. return false;
  207. }
  208. return true;
  209. }
  210. /**
  211. * 是否有效的密钥对(判断是否能否互相加密解密)
  212. * @param $private_key string
  213. * @param $public_key string
  214. * @return bool
  215. */
  216. public function isValidRsaKey($private_key, $public_key)
  217. {
  218. if (!$this->isValidPublicKey($public_key)) {
  219. return false;
  220. }
  221. if (!$this->isValidPrivateKey($private_key)) {
  222. return false;
  223. }
  224. //数据原文
  225. $data = time();
  226. // 使用公钥加密
  227. openssl_public_encrypt($data, $encrypted, openssl_pkey_get_public($public_key));
  228. $encrypted = base64_encode($encrypted);
  229. // 使用秘钥看是否能够解密,能的话,说明就是配对的
  230. openssl_private_decrypt(base64_decode($encrypted), $decrypted, openssl_pkey_get_private($private_key));
  231. if ($data != $decrypted) {
  232. return false;
  233. }
  234. return true;
  235. }
  236. /**
  237. * 身份证验证
  238. * @param $idCard
  239. *
  240. * @return bool
  241. */
  242. function validateIDCard($idCard) {
  243. // 正则表达式验证格式
  244. if (!preg_match('/^\d{17}[\dXx]$/', $idCard)) {
  245. return false;
  246. }
  247. // 加权因子
  248. $weight = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
  249. // 校验码对应值
  250. $validate = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
  251. $sum = 0;
  252. // 计算前17位的加权和
  253. for ($i = 0; $i < 17; $i++) {
  254. $sum += intval($idCard[$i]) * $weight[$i];
  255. }
  256. // 计算校验码
  257. $mod = $sum % 11;
  258. $expectedValidateCode = $validate[$mod];
  259. // 校验最后一位
  260. $lastChar = strtoupper($idCard[17]);
  261. return $lastChar === $expectedValidateCode;
  262. }
  263. }