mobile.class.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. class Mobile extends OAuth2Client {
  8. public function __construct($ak, $sk) {
  9. parent::__construct($ak, $sk);
  10. $this->stateParam['from'] = 'mobile';
  11. }
  12. public function showLoginUrl($calback_url = '') {
  13. }
  14. public function user() {
  15. global $_GPC, $_W;
  16. $mobile = safe_gpc_string($_GPC['username']);
  17. $password = safe_check_password($_GPC['password']);
  18. if (is_error($password)) {
  19. return error(-1, $password['message']);
  20. }
  21. $member['password'] = $password;
  22. pdo_delete('users_failed_login', array('lastupdate <' => TIMESTAMP - 3600));
  23. $failed = pdo_get('users_failed_login', array('username' => $mobile, 'ip' => $_W['clientip']));
  24. if ($failed['count'] >= 5) {
  25. return error('-1', '输入密码错误次数超过5次,请在1小时后再登录');
  26. }
  27. if (!empty($_W['setting']['copyright']['verifycode'])) {
  28. $verify = safe_gpc_string($_GPC['verify']);
  29. if (empty($verify)) {
  30. return error('-1', '请输入验证码');
  31. }
  32. $result = checkcaptcha($verify);
  33. if (empty($result)) {
  34. return error('-1', '输入验证码错误');
  35. }
  36. }
  37. if (empty($mobile)) {
  38. return error('-1', '请输入要登录的手机号');
  39. }
  40. if (!preg_match(REGULAR_MOBILE, $mobile)) {
  41. return error(-1, '手机号格式不正确');
  42. }
  43. if (empty($member['password'])) {
  44. return error('-1', '请输入密码');
  45. }
  46. $user_profile = table('users_profile')->getByMobile($mobile);
  47. if (empty($user_profile)) {
  48. return error(-1, '手机号未注册');
  49. }
  50. $member['uid'] = $user_profile['uid'];
  51. $member['type'] = $this->user_type;
  52. return $member;
  53. }
  54. public function validateMobile() {
  55. global $_GPC;
  56. $mobile = safe_gpc_string($_GPC['mobile'], '', 'mobile');
  57. if (empty($mobile)) {
  58. return error(-1, '手机号不能为空或手机号格式不正确');
  59. }
  60. $mobile_exists = table('users_profile')->getByMobile($mobile);
  61. if (!empty($mobile_exists)) {
  62. return error(-1, '手机号已存在');
  63. }
  64. return true;
  65. }
  66. public function register() {
  67. global $_GPC;
  68. load()->model('user');
  69. $member = array();
  70. $profile = array();
  71. $smscode = safe_gpc_string($_GPC['smscode']);
  72. $mobile = safe_gpc_string($_GPC['mobile'], '', 'mobile');
  73. if (empty($mobile)) {
  74. return error(-1, '手机号不能为空或手机号格式不正确');
  75. }
  76. $password = safe_check_password($_GPC['password']);
  77. if (is_error($password)) {
  78. return error(-1, $password['message']);
  79. }
  80. $member['password'] = $password;
  81. if (empty($smscode)) {
  82. return error(-1, '短信验证码不能为空');
  83. }
  84. load()->model('utility');
  85. $verify_info = utility_smscode_verify(0, $mobile, $smscode);
  86. if (is_error($verify_info)) {
  87. return error(-1, $verify_info['message']);
  88. }
  89. if (istrlen($member['password']) < 8) {
  90. return error(-1, '必须输入密码,且密码长度不得低于8位。');
  91. }
  92. $member['username'] = $mobile;
  93. $member['openid'] = $mobile;
  94. $member['register_type'] = USER_REGISTER_TYPE_MOBILE;
  95. $member['owner_uid'] = intval($_GPC['owner_uid']);
  96. $profile['mobile'] = $mobile;
  97. $register = array(
  98. 'member' => $member,
  99. 'profile' => $profile,
  100. );
  101. return parent::user_register($register);
  102. }
  103. public function login() {
  104. return $this->user();
  105. }
  106. public function bind() {
  107. global $_GPC, $_W;
  108. $mobile = safe_gpc_string($_GPC['mobile'], '', 'mobile');
  109. if (empty($mobile)) {
  110. return error(-1, '手机号不能为空或手机号格式不正确');
  111. }
  112. $user = table('users')->getById($_W['uid']);
  113. if (empty($user)) {
  114. return error(-1, '请先登录');
  115. }
  116. $user_profile = table('users_profile')->getByUid($_W['uid']);
  117. $user_bind = table('users_bind')->getByTypeAndUid(USER_REGISTER_TYPE_MOBILE, $_W['uid']);
  118. $need_checkcaptcha = true;
  119. if (isset($_GPC['need_checkcaptcha']) && empty($_GPC['need_checkcaptcha'])) {
  120. $need_checkcaptcha = false;
  121. }
  122. if (!$need_checkcaptcha && !empty($user_bind)) {
  123. $check_authcode = safe_gpc_string($_GPC['check_authcode']);
  124. if (empty($check_authcode)) {
  125. return error(-1, '请先验证旧手机!');
  126. }
  127. $old_mobile = authcode($check_authcode);
  128. if ($old_mobile != $user_bind['bind_sign']) {
  129. return error(-1, '请先验证旧手机!');
  130. }
  131. }
  132. $param_validate = $this->paramValidate();
  133. if (is_error($param_validate)) {
  134. return $param_validate;
  135. }
  136. if (empty($user_profile)) {
  137. pdo_insert('users_profile', array('uid' => $_W['uid'], 'mobile' => $mobile));
  138. } else {
  139. pdo_update('users_profile', array('mobile' => $mobile), array('id' => $user_profile['id']));
  140. }
  141. if (empty($user_bind)) {
  142. pdo_insert('users_bind', array('uid' => $_W['uid'], 'bind_sign' => $mobile, 'third_type' => USER_REGISTER_TYPE_MOBILE, 'third_nickname' => $mobile));
  143. } else {
  144. table('users_bind')->where(array('uid' => $_W['uid'], 'third_type' => USER_REGISTER_TYPE_MOBILE))->fill(array('bind_sign' => $mobile, 'third_nickname' => $mobile))->save();
  145. }
  146. return error(0, '绑定成功');
  147. }
  148. public function unbind() {
  149. global $_GPC, $_W;
  150. $mobile = safe_gpc_string($_GPC['mobile'], '', 'mobile');
  151. if (empty($mobile)) {
  152. return error(-1, '手机号不能为空或手机号格式不正确');
  153. }
  154. $user_profile = table('users_profile')->getByUid($_W['uid']);
  155. $param_validate = $this->paramValidate();
  156. if (is_error($param_validate)) {
  157. return $param_validate;
  158. }
  159. pdo_update('users', array('openid' => ''), array('uid' => $_W['uid']));
  160. pdo_update('users_profile', array('mobile' => ''), array('id' => $user_profile['id']));
  161. pdo_delete('users_bind', array('uid' => $_W['uid'], 'bind_sign' => $mobile, 'third_type' => USER_REGISTER_TYPE_MOBILE));
  162. return error(0, '解除绑定成功');
  163. }
  164. public function isbind() {
  165. global $_W;
  166. $bind_info = table('users_bind')->getByTypeAndUid(USER_REGISTER_TYPE_MOBILE, $_W['uid']);
  167. return !empty($bind_info['bind_sign']);
  168. }
  169. public function paramValidate() {
  170. global $_GPC;
  171. $mobile = safe_gpc_string($_GPC['mobile'], '', 'mobile');
  172. if (empty($mobile)) {
  173. return error(-1, '手机号不能为空或手机号格式不正确');
  174. }
  175. $image_code = safe_gpc_string($_GPC['imagecode']);
  176. $sms_code = safe_gpc_string($_GPC['smscode']);
  177. $need_checkcaptcha = true;
  178. if (isset($_GPC['need_checkcaptcha']) && empty($_GPC['need_checkcaptcha'])) {
  179. $need_checkcaptcha = false;
  180. }
  181. if (empty($sms_code)) {
  182. return error(-1, '短信验证码不能为空');
  183. }
  184. if ($need_checkcaptcha && empty($image_code)) {
  185. return error(-1, '图形验证码不能为空');
  186. }
  187. if ($need_checkcaptcha) {
  188. $captcha = checkcaptcha($image_code);
  189. if (empty($captcha)) {
  190. return error(-1, '图形验证码错误,请重新获取');
  191. }
  192. }
  193. load()->model('utility');
  194. $verify_info = utility_smscode_verify(0, $mobile, $sms_code);
  195. if (is_error($verify_info)) {
  196. return error(-1, $verify_info['message']);
  197. }
  198. }
  199. }