mobile.class.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. }
  11. public function showLoginUrl($calback_url = '') {
  12. }
  13. public function user() {
  14. global $_GPC, $_W;
  15. $mobile = trim($_GPC['username']);
  16. $member['password'] = $_GPC['password'];
  17. pdo_query('DELETE FROM'.tablename('users_failed_login'). ' WHERE lastupdate < :timestamp', array(':timestamp' => TIMESTAMP-300));
  18. $failed = pdo_get('users_failed_login', array('username' => $mobile, 'ip' => CLIENT_IP));
  19. if ($failed['count'] >= 5) {
  20. return error('-1', '输入密码错误次数超过5次,请在5分钟后再登录');
  21. }
  22. if (!empty($_W['setting']['copyright']['verifycode'])) {
  23. $verify = trim($_GPC['verify']);
  24. if (empty($verify)) {
  25. return error('-1', '请输入验证码');
  26. }
  27. $result = checkcaptcha($verify);
  28. if (empty($result)) {
  29. return error('-1', '输入验证码错误');
  30. }
  31. }
  32. if (empty($mobile)) {
  33. return error('-1', '请输入要登录的手机号');
  34. }
  35. if (!preg_match(REGULAR_MOBILE, $mobile)) {
  36. return error(-1, '手机号格式不正确');
  37. }
  38. if (empty($member['password'])) {
  39. return error('-1', '请输入密码');
  40. }
  41. $user_table = table('users');
  42. $user_profile = $user_table->userProfileMobile($mobile);
  43. if (empty($user_profile)) {
  44. return error(-1, '手机号未注册');
  45. }
  46. $member['uid'] = $user_profile['uid'];
  47. return $member;
  48. }
  49. public function validateMobile() {
  50. global $_GPC;
  51. $mobile = $_GPC['mobile'];
  52. if (empty($mobile)) {
  53. return error(-1, '手机号不能为空');
  54. }
  55. if (!preg_match(REGULAR_MOBILE, $mobile)) {
  56. return error(-1, '手机号格式不正确');
  57. }
  58. $user_table = table('users');
  59. $mobile_exists = $user_table->userProfileMobile($mobile);
  60. if (!empty($mobile_exists)) {
  61. return error(-1, '手机号已存在');
  62. }
  63. return true;
  64. }
  65. public function register() {
  66. global $_GPC;
  67. load()->model('user');
  68. $member = array();
  69. $profile = array();
  70. $smscode = trim($_GPC['smscode']);
  71. $mobile = trim($_GPC['mobile']);
  72. $member['password'] = $_GPC['password'];
  73. if (empty($smscode)) {
  74. return error(-1, '短信验证码不能为空');
  75. }
  76. $user_table = table('users');
  77. $code_info = $user_table->userVerifyCode($mobile, $smscode);
  78. if (empty($code_info)) {
  79. return error(-1, '短信验证码不正确');
  80. }
  81. if ($code_info['createtime'] + 120 < TIMESTAMP) {
  82. return error(-1, '短信验证码已过期,请重新获取');
  83. }
  84. if (!checkcaptcha($_GPC['code'])) {
  85. return error(-1, '你输入的验证码不正确, 请重新输入.');
  86. }
  87. if(istrlen($member['password']) < 8) {
  88. return error(-1, '必须输入密码,且密码长度不得低于8位。');
  89. }
  90. $member['username'] = $mobile;
  91. $member['openid'] = $mobile;
  92. $member['register_type'] = USER_REGISTER_TYPE_MOBILE;
  93. $member['owner_uid'] = intval($_GPC['owner_uid']);
  94. $profile['mobile'] = $mobile;
  95. $register = array(
  96. 'member' => $member,
  97. 'profile' => $profile
  98. );
  99. return parent::user_register($register);
  100. }
  101. public function login() {
  102. return $this->user();
  103. }
  104. public function bind() {
  105. global $_GPC, $_W;
  106. $user_table = table('users');
  107. $password = $_GPC['password'];
  108. $mobile = trim($_GPC['mobile']);
  109. $user = $user_table->usersInfo($_W['uid']);
  110. $user_profile = $user_table->userProfile($_W['uid']);
  111. $param_validate = $this->paramValidate();
  112. if (is_error($param_validate)) {
  113. return $param_validate;
  114. }
  115. pdo_update('users', array('password' => user_hash($password, $user['salt'])), array('uid' => $_W['uid']));
  116. pdo_update('users_profile', array('mobile' => $mobile), array('id' => $user_profile['id']));
  117. pdo_insert('users_bind', array('uid' => $_W['uid'], 'bind_sign' => $mobile, 'third_type' => USER_REGISTER_TYPE_MOBILE, 'third_nickname' => $mobile));
  118. return error(0, '绑定成功');
  119. }
  120. public function unbind() {
  121. global $_GPC, $_W;
  122. $user_table = table('users');
  123. $mobile = trim($_GPC['mobile']);
  124. $user_profile = $user_table->userProfile($_W['uid']);
  125. $param_validate = $this->paramValidate();
  126. if (is_error($param_validate)) {
  127. return $param_validate;
  128. }
  129. pdo_update('users', array('openid' => ''), array('uid' => $_W['uid']));
  130. pdo_update('users_profile', array('mobile' => ''), array('id' => $user_profile['id']));
  131. pdo_delete('users_bind', array('uid' => $_W['uid'], 'bind_sign' => $mobile, 'third_type' => USER_REGISTER_TYPE_MOBILE));
  132. return error(0, '解除绑定成功');
  133. }
  134. public function paramValidate($type = false) {
  135. global $_GPC;
  136. $password = $_GPC['password'];
  137. $repassword = $_GPC['repassword'];
  138. $mobile = trim($_GPC['mobile']);
  139. $image_code =trim($_GPC['imagecode']);
  140. $sms_code = trim($_GPC['smscode']);
  141. $user_table = table('users');
  142. if (empty($sms_code)) {
  143. return error(-1, '短信验证码不能为空');
  144. }
  145. if (empty($image_code)) {
  146. return error(-1, '图形验证码不能为空');
  147. }
  148. $captcha = checkcaptcha($image_code);
  149. if (empty($captcha)) {
  150. return error(-1, '图形验证码错误,请重新获取');
  151. }
  152. if (!empty($type)) {
  153. if ((empty($password) || empty($repassword))) {
  154. return error(-1, '密码不能为空');
  155. }
  156. if ($password != $repassword) {
  157. return error(-1, '两次密码不一致');
  158. }
  159. }
  160. $code_info = $user_table->userVerifyCode($mobile, $sms_code);
  161. if (empty($code_info)) {
  162. return error(-1, '短信验证码不正确');
  163. }
  164. if ($code_info['createtime'] + 120 < TIMESTAMP) {
  165. return error(-1, '短信验证码已过期,请重新获取');
  166. }
  167. }
  168. }