oauth2client.class.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. abstract class OAuth2Client {
  7. protected $ak;
  8. protected $sk;
  9. protected $login_type;
  10. protected $user_type = USER_TYPE_COMMON;
  11. protected $stateParam = array(
  12. 'state' => '',
  13. 'from' => '',
  14. 'mode' => '',
  15. );
  16. public function __construct($ak, $sk) {
  17. $this->ak = $ak;
  18. $this->sk = $sk;
  19. }
  20. public function stateParam() {
  21. global $_W;
  22. $this->stateParam['state'] = $_W['token'];
  23. if (!empty($_W['user'])) {
  24. $this->stateParam['mode'] = 'bind';
  25. } else {
  26. $this->stateParam['mode'] = 'login';
  27. }
  28. return base64_encode(http_build_query($this->stateParam, '', '&'));
  29. }
  30. public function getLoginType($login_type) {
  31. $this->login_type = $login_type;
  32. }
  33. public function setUserType($user_type) {
  34. $this->user_type = $user_type;
  35. return $this;
  36. }
  37. public static function supportLoginType() {
  38. return array('system', 'qq', 'wechat', 'mobile', 'console');
  39. }
  40. public static function supportThirdLoginType() {
  41. return array('qq', 'wechat');
  42. }
  43. public static function supportBindTypeInfo($type = '') {
  44. $data = array(
  45. 'qq' => array(
  46. 'type' => 'qq',
  47. 'title' => 'QQ',
  48. ),
  49. 'wechat' => array(
  50. 'type' => 'wechat',
  51. 'title' => '微信',
  52. ),
  53. 'mobile' => array(
  54. 'type' => 'mobile',
  55. 'title' => '手机号',
  56. ),
  57. 'console' => array(
  58. 'type' => 'console',
  59. 'title' => '控制台',
  60. ),
  61. );
  62. if (!empty($type)) {
  63. return $data[$type];
  64. } else {
  65. return $data;
  66. }
  67. }
  68. public static function supportThirdLoginBindType() {
  69. return array('qq', 'wechat');
  70. }
  71. public static function supportThirdMode() {
  72. return array('bind', 'login');
  73. }
  74. public static function supportParams($state) {
  75. $state = urldecode($state);
  76. $param = array();
  77. if (!empty($state)) {
  78. $state = base64_decode($state);
  79. parse_str($state, $third_param);
  80. $modes = self::supportThirdMode();
  81. $types = self::supportThirdLoginType();
  82. if (in_array($third_param['mode'], $modes) && in_array($third_param['from'], $types)) {
  83. return $third_param;
  84. }
  85. }
  86. return $param;
  87. }
  88. public static function create($type, $appid = '', $appsecret = '') {
  89. $types = self::supportLoginType();
  90. if (in_array($type, $types)) {
  91. load()->classs('oauth2/' . $type);
  92. $type_name = ucfirst($type);
  93. $obj = new $type_name($appid, $appsecret);
  94. $obj->getLoginType($type);
  95. return $obj;
  96. }
  97. return null;
  98. }
  99. abstract public function showLoginUrl($calback_url = '');
  100. abstract public function user();
  101. abstract public function login();
  102. abstract public function bind();
  103. abstract public function unbind();
  104. abstract public function isbind();
  105. abstract public function register();
  106. public function user_register($register) {
  107. global $_W;
  108. load()->model('user');
  109. if (is_error($register)) {
  110. return $register;
  111. }
  112. $member = $register['member'];
  113. $profile = $register['profile'];
  114. $member['type'] = $this->user_type;
  115. $member['status'] = !empty($_W['setting']['register']['verify']) ? 1 : 2;
  116. $member['remark'] = '';
  117. $group = user_group_detail_info(intval($_W['setting']['register']['groupid']));
  118. $timelimit = intval($group['timelimit']);
  119. if ($timelimit > 0) {
  120. $member['endtime'] = strtotime($timelimit . ' days');
  121. }
  122. $member['starttime'] = TIMESTAMP;
  123. $user_id = user_register($member, $this->stateParam['from']);
  124. if (in_array($member['register_type'], array(USER_REGISTER_TYPE_QQ, USER_REGISTER_TYPE_WECHAT))) {
  125. pdo_update('users', array('username' => $member['username'] . $user_id . rand(100, 999)), array('uid' => $user_id));
  126. }
  127. if ($user_id > 0) {
  128. unset($member['password']);
  129. $member['uid'] = $user_id;
  130. if (!empty($profile)) {
  131. $profile['uid'] = $user_id;
  132. $profile['createtime'] = TIMESTAMP;
  133. pdo_insert('users_profile', $profile);
  134. }
  135. if (in_array($member['register_type'], array(USER_REGISTER_TYPE_QQ, USER_REGISTER_TYPE_WECHAT, USER_REGISTER_TYPE_MOBILE))) {
  136. pdo_insert('users_bind', array('uid' => $user_id, 'bind_sign' => $member['openid'], 'third_type' => $member['register_type'], 'third_nickname' => $member['username']));
  137. }
  138. if (in_array($member['register_type'], array(USER_REGISTER_TYPE_QQ, USER_REGISTER_TYPE_WECHAT))) {
  139. return $user_id;
  140. }
  141. $message = '注册成功';
  142. if (USER_STATUS_CHECK == $member['status']) {
  143. $message .= ',请等待管理员审核!';
  144. } elseif (USER_TYPE_CLERK != $member['type']) {
  145. $message .= ',请重新登录!';
  146. }
  147. return array(
  148. 'errno' => 0,
  149. 'message' => $message,
  150. 'uid' => $user_id,
  151. );
  152. }
  153. return error(-1, '增加用户失败,请稍候重试或联系网站管理员解决!');
  154. }
  155. }