PageRenderTime 47ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/upload/src/service/user/validator/PwUserValidator.php

https://gitlab.com/wuhang2003/phpwind
PHP | 261 lines | 135 code | 15 blank | 111 comment | 67 complexity | da53e4314f1993a5f9abcac7450c51b3 MD5 | raw file
  1. <?php
  2. /**
  3. * 用户的相关验证方法
  4. *
  5. * @author xiaoxia.xu <xiaoxia.xuxx@aliyun-inc.com>
  6. * @copyright ©2003-2103 phpwind.com
  7. * @license http://www.phpwind.com
  8. * @version $Id: PwUserValidator.php 24943 2013-02-27 03:52:21Z jieyin $
  9. * @package src.service.user.validator
  10. */
  11. class PwUserValidator {
  12. /**
  13. * 检测用户名的合法性
  14. *
  15. * @param string $username
  16. * @return boolean|PwError
  17. */
  18. public static function isUsernameHasIllegalChar($username) {
  19. //匹配用户名只能含有中文、数字、大小写字母、'.'、_
  20. if (0 >= preg_match('/^[\x7f-\xff\dA-Za-z\.\_]+$/', $username)) {
  21. return new PwError('USER:error.username');
  22. }
  23. return false;
  24. }
  25. /**
  26. * 检查用户的手机号码是否合法
  27. *
  28. * @param string $password 用户密码
  29. * @return PwError|boolean
  30. */
  31. public static function isMobileValid($mobile) {
  32. if (0 >= preg_match('/^1\d{10}$/', $mobile)) return new PwError('USER:mobile.error.formate');
  33. return true;
  34. }
  35. /**
  36. * 检测固定电话号码是否正确
  37. *
  38. * @param string $telPhone
  39. * @return true|PwError
  40. */
  41. public static function isTelPhone($telPhone) {
  42. if (0 >= preg_match('/^[0-9][-\d]*\d*$/', $telPhone)) {
  43. return new PwError('USER:error.telphone');
  44. }
  45. return true;
  46. }
  47. /**
  48. * 验证支付宝帐号
  49. *
  50. * @param string $alipay 待检查的支付宝帐号
  51. * @param string $username 排除的用户名
  52. * @return true|PwError
  53. */
  54. public static function isAlipayValid($alipay, $username = '') {
  55. /* @var $userDs PwUser */
  56. // $userDs = Wekit::load('user.PwUser');
  57. //TODO【用户数据验证】支付宝帐号唯一验证
  58. return true;
  59. }
  60. /**
  61. * 检查用户的邮箱
  62. *
  63. * @param string $email 待检查的用户邮箱
  64. * @param string $username 待检查的用户名
  65. * @return boolean|PwError
  66. */
  67. public static function isEmailValid($email, $username = '', $uid = 0) {
  68. $result = self::_getWindid()->checkUserInput($email, 3, $username, $uid);
  69. if ($result < 1) {
  70. return new PwError('USER:user.error.' . $result);
  71. }
  72. return true;
  73. }
  74. /**
  75. * 验证用户名
  76. *
  77. * @param string $username 验证的用户名
  78. * @param int $uid 排除的用户ID
  79. * @return PwError|boolean
  80. */
  81. public static function isUsernameValid($username, $uid = 0) {
  82. if (!$username) return new PwError('USER:user.error.-1');
  83. $result = self::_getWindid()->checkUserInput($username, 1, '', $uid);
  84. if ($result < 1) {
  85. if ($result == -2) {
  86. $config = WindidApi::C('reg');
  87. return new PwError('WINDID:code.-2', array('{min}' => $config['security.username.min'], '{max}' => $config['security.username.max']));
  88. }
  89. return new PwError('WINDID:code.' . $result);
  90. }
  91. if (false !== ($r = self::isUsernameHasIllegalChar($username))) {
  92. return $r;
  93. }
  94. return true;
  95. }
  96. /**
  97. * 检查用户的username是否存在
  98. *
  99. * @param string $username 待检查的用户名
  100. * @param int $exceptUid 排除的用户ID
  101. * @return boolean
  102. */
  103. public static function checkUsernameExist($username, $exceptUid = 0) {
  104. $result = self::_getWindid()->checkUserInput($username, 1, '', $exceptUid);
  105. if ($result < 1) return new PwError('WINDID:code.'. $result);
  106. /* @var $userDs PwUser */
  107. /* $userDs = Wekit::load('user.PwUser');
  108. $info = $userDs->getUserByName($username, PwUser::FETCH_MAIN);
  109. if (!$info) return false;
  110. $exceptUid = intval($exceptUid);
  111. if ($exceptUid && $info['uid'] == $exceptUid) return false;*/
  112. return true;
  113. }
  114. /**
  115. * 检查用户的密码是否合法
  116. *
  117. * @param string $password 用户密码
  118. * @param string $username 用户名
  119. * @return PwError|boolean
  120. */
  121. public static function isPwdValid($password, $username) {
  122. $result = self::_getWindid()->checkUserInput($password, 2, $username);
  123. if ($result < 1) {
  124. $config = WindidApi::C('reg');
  125. $var = array('{min}' => $config['security.password.min'], '{max}' => $config['security.password.max']);
  126. return new PwError('WINDID:code.'. $result, $var);
  127. }
  128. $result = self::checkPwdComplex($password, $username);
  129. if ($result instanceof PwError) return $result;
  130. return true;
  131. }
  132. /**
  133. * 验证密码的复杂度是否符合后台设置要求
  134. * 检查密码复杂度
  135. * 检查用户名和密码是否允许相同
  136. * 如果设置不允许相同而相同则返回PwError
  137. * 其余返回true
  138. *
  139. * @param string $password 用户密码
  140. * @param string $username 用户名
  141. * @return boolean|PwError
  142. */
  143. public static function checkPwdComplex($password, $username) {
  144. $register = WindidApi::C('reg');
  145. if (!($pwdConfig = $register['security.password'])) return true;
  146. $config = array_sum($pwdConfig);
  147. if (in_array(9, $pwdConfig)) {
  148. $config = $config - 9;
  149. if ($username == $password) return new PwError('USER:pwd.error.equalUsername');
  150. }
  151. if ($config == 0) return true;
  152. if (self::_complexCaculate($password, $config)) return new PwError('USER:pwd.error.complex', array('{type}' => self::buildPwdComplexMsg($pwdConfig)));
  153. return true;
  154. }
  155. /**
  156. * 显示用户密码的支持信息
  157. *
  158. * @return array(string, args)
  159. */
  160. public static function buildPwdShowMsg() {
  161. $config = WindidApi::C('reg');
  162. $_min = $config['security.password.min'];
  163. $_max = $config['security.password.max'];
  164. $_complex = $config['security.password'];
  165. $_length = $_min || $_max;
  166. $type = self::buildPwdComplexMsg($_complex);
  167. $var = array();
  168. $_key = 'USER:pwd.require';
  169. if ($_length && $_complex) {
  170. $_key = 'USER:pwd.format.require';
  171. $var = array('{type}' => $type, '{min}' => $_min, '{max}' => $_max);
  172. } elseif (!$_complex && $_length) {
  173. $_key = 'USER:pwd.format.length.require';
  174. $var = array('{min}' => $_min, '{max}' => $_max);
  175. } elseif (!$_length && $_complex) {
  176. $_key = 'USER:pwd.error.complex';
  177. $var = array('{type}' => $type);
  178. }
  179. return array($_key, $var);
  180. }
  181. /**
  182. * 显示用户名的验证支持信息
  183. *
  184. * @return array(string, args)
  185. */
  186. public static function buildNameShowMsg() {
  187. $config = WindidApi::C('reg');
  188. $_name = 'USER:user.error.username';
  189. $_min = $config['security.username.min'];
  190. $_max = $config['security.username.max'];
  191. return array('USER:user.error.username', array('{min}' => $_min, '{max}' => $_max));
  192. }
  193. /**
  194. * 构造用户密码复杂度的校验规则
  195. *
  196. * @param array $config 复杂规则的配置
  197. * @return string
  198. */
  199. private static function buildPwdComplexMsg($config) {
  200. if (!$config) return '';
  201. $complex = array(1 => '小写字母', 2 => '大写字母', 4 => '数字', 8 => '非空白符号', 9 => '不能和用户名相同');
  202. return implode('、', array_intersect_key($complex, array_flip($config)));
  203. }
  204. /**
  205. * 复杂度判断
  206. *
  207. * @param string $password 密码
  208. * @param int $config 配置
  209. * @return boolean
  210. */
  211. private static function _complexCaculate($password, $config) {
  212. $pwdLen = strlen($password);
  213. $complex = 0;
  214. for ($i = 0; $i < $pwdLen; $i ++) {
  215. $ascii = ord($password[$i]);
  216. //必须含有小写字母 97-122
  217. if (1 == ($config & 1) && $ascii >= 97 && $ascii <= 122) {
  218. if (0 == $complex || 1 != ($complex & 1)) $complex += 1;
  219. continue;
  220. }
  221. //必须含有大写字母 65-90
  222. if (2 == ($config & 2) && $ascii >= 65 && $ascii <= 90) {
  223. if (0 == $complex || 2 != ($complex & 2)) $complex += 2;
  224. continue;
  225. }
  226. //必须含有数字 48-57
  227. if (4 == ($config & 4) && $ascii >= 48 && $ascii <= 57) {
  228. if (0 == $complex || 4 != ($complex & 4)) $complex += 4;
  229. continue;
  230. }
  231. //必须含有符号 33-47/58-64/91-96/123-126
  232. if (8 == ($config & 8) &&
  233. (($ascii >= 33 && $ascii <=47) || ($ascii >= 58 && $ascii <= 64) || ($ascii >= 91 && $ascii <= 96) || ($ascii >= 123 && $ascii <= 126))) {
  234. if (0 == $complex || 8 != ($complex & 8)) $complex += 8;
  235. continue;
  236. }
  237. //已经达到设置复杂度则跳出
  238. if ($config == $complex) break;
  239. }
  240. return $config != $complex;
  241. }
  242. private static function _getWindid() {
  243. return WindidApi::api('user');
  244. }
  245. }
  246. ?>