oauth2client.class.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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');
  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. );
  58. if (!empty($type)) {
  59. return $data[$type];
  60. } else {
  61. return $data;
  62. }
  63. }
  64. public static function supportThirdLoginBindType() {
  65. return array('qq', 'wechat');
  66. }
  67. public static function supportThirdMode() {
  68. return array('bind', 'login');
  69. }
  70. public static function supportParams($state) {
  71. $state = urldecode($state);
  72. $param = array();
  73. if (!empty($state)) {
  74. $state = base64_decode($state);
  75. parse_str($state, $third_param);
  76. $modes = self::supportThirdMode();
  77. $types = self::supportThirdLoginType();
  78. if (in_array($third_param['mode'],$modes) && in_array($third_param['from'],$types)) {
  79. return $third_param;
  80. }
  81. }
  82. return $param;
  83. }
  84. public static function create($type, $appid = '', $appsecret = '') {
  85. $types = self::supportLoginType();
  86. if (in_array($type, $types)) {
  87. load()->classs('oauth2/' . $type);
  88. $type_name = ucfirst($type);
  89. $obj = new $type_name($appid, $appsecret);
  90. $obj->getLoginType($type);
  91. return $obj;
  92. }
  93. return null;
  94. }
  95. abstract function showLoginUrl($calback_url = '');
  96. abstract function user();
  97. abstract function login();
  98. abstract function bind();
  99. abstract function unbind();
  100. abstract function isbind();
  101. abstract function register();
  102. public function user_register($register) {
  103. global $_W;
  104. load()->model('user');
  105. if (is_error($register)) {
  106. return $register;
  107. }
  108. $member = $register['member'];
  109. $profile = $register['profile'];
  110. $member['type'] = $this->user_type;
  111. if ($member['type'] == USER_TYPE_CLERK) {
  112. $member['status'] = !empty($_W['setting']['register']['clerk']['verify']) ? 1 : 2;
  113. } else {
  114. $member['status'] = !empty($_W['setting']['register']['verify']) ? 1 : 2;
  115. }
  116. $member['remark'] = '';
  117. $member['groupid'] = intval($_W['setting']['register']['groupid']);
  118. if (empty($member['groupid'])) {
  119. $member['groupid'] = pdo_fetchcolumn('SELECT id FROM '.tablename('users_group').' ORDER BY id ASC LIMIT 1');
  120. $member['groupid'] = intval($member['groupid']);
  121. }
  122. $group = user_group_detail_info($member['groupid']);
  123. $timelimit = intval($group['timelimit']);
  124. if($timelimit > 0) {
  125. $member['endtime'] = strtotime($timelimit . ' days');
  126. }
  127. $member['starttime'] = TIMESTAMP;
  128. $user_id = user_register($member, $this->stateParam['from']);
  129. if (in_array($member['register_type'], array(USER_REGISTER_TYPE_QQ, USER_REGISTER_TYPE_WECHAT))) {
  130. pdo_update('users', array('username' => $member['username'] . $user_id . rand(100,999)), array('uid' => $user_id));
  131. }
  132. if($user_id > 0) {
  133. unset($member['password']);
  134. $member['uid'] = $user_id;
  135. if (!empty($profile)) {
  136. $profile['uid'] = $user_id;
  137. $profile['createtime'] = TIMESTAMP;
  138. pdo_insert('users_profile', $profile);
  139. }
  140. if (in_array($member['register_type'], array(USER_REGISTER_TYPE_QQ, USER_REGISTER_TYPE_WECHAT, USER_REGISTER_TYPE_MOBILE))) {
  141. pdo_insert('users_bind', array('uid' => $user_id, 'bind_sign' => $member['openid'], 'third_type' => $member['register_type'], 'third_nickname' => $member['username']));
  142. }
  143. if (in_array($member['register_type'], array(USER_REGISTER_TYPE_QQ, USER_REGISTER_TYPE_WECHAT))) {
  144. return $user_id;
  145. }
  146. return error(0, '注册成功'.(!empty($_W['setting']['register']['verify']) ? ',请等待管理员审核!' : ',请重新登录!'));
  147. }
  148. return error(-1, '增加用户失败,请稍候重试或联系网站管理员解决!');
  149. }
  150. }