人人商城

oauth2client.class.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 $stateParam = array(
  11. 'state' => '',
  12. 'from' => '',
  13. 'mode' => ''
  14. );
  15. public function __construct($ak, $sk) {
  16. $this->ak = $ak;
  17. $this->sk = $sk;
  18. }
  19. public function stateParam() {
  20. global $_W;
  21. $this->stateParam['state'] = $_W['token'];
  22. if (!empty($_W['user'])) {
  23. $this->stateParam['mode'] = 'bind';
  24. } else {
  25. $this->stateParam['mode'] = 'login';
  26. }
  27. return base64_encode(http_build_query($this->stateParam, '', '&'));
  28. }
  29. public function getLoginType($login_type) {
  30. $this->login_type = $login_type;
  31. }
  32. public static function supportLoginType(){
  33. return array('system', 'qq', 'wechat', 'mobile');
  34. }
  35. public static function supportThirdLoginType() {
  36. return array('qq', 'wechat');
  37. }
  38. public static function supportThirdMode() {
  39. return array('bind', 'login');
  40. }
  41. public static function supportParams($state) {
  42. $state = urldecode($state);
  43. $param = array();
  44. if (!empty($state)) {
  45. $state = base64_decode($state);
  46. parse_str($state, $third_param);
  47. $modes = self::supportThirdMode();
  48. $types = self::supportThirdLoginType();
  49. if (in_array($third_param['mode'],$modes) && in_array($third_param['from'],$types)) {
  50. return $third_param;
  51. }
  52. }
  53. return $param;
  54. }
  55. public static function create($type, $appid = '', $appsecret = '') {
  56. $types = self::supportLoginType();
  57. if (in_array($type, $types)) {
  58. load()->classs('oauth2/' . $type);
  59. $type_name = ucfirst($type);
  60. $obj = new $type_name($appid, $appsecret);
  61. $obj->getLoginType($type);
  62. return $obj;
  63. }
  64. return null;
  65. }
  66. abstract function showLoginUrl($calback_url = '');
  67. abstract function user();
  68. abstract function login();
  69. abstract function bind();
  70. abstract function unbind();
  71. abstract function register();
  72. public function user_register($register) {
  73. global $_W;
  74. load()->model('user');
  75. if (is_error($register)) {
  76. return $register;
  77. }
  78. $member = $register['member'];
  79. $profile = $register['profile'];
  80. $member['status'] = !empty($_W['setting']['register']['verify']) ? 1 : 2;
  81. $member['remark'] = '';
  82. $member['groupid'] = intval($_W['setting']['register']['groupid']);
  83. if (empty($member['groupid'])) {
  84. $member['groupid'] = pdo_fetchcolumn('SELECT id FROM '.tablename('users_group').' ORDER BY id ASC LIMIT 1');
  85. $member['groupid'] = intval($member['groupid']);
  86. }
  87. $group = user_group_detail_info($member['groupid']);
  88. $timelimit = intval($group['timelimit']);
  89. if($timelimit > 0) {
  90. $member['endtime'] = strtotime($timelimit . ' days');
  91. }
  92. $member['starttime'] = TIMESTAMP;
  93. if (!empty($owner_uid)) {
  94. $member['owner_uid'] = pdo_getcolumn('users', array('uid' => $owner_uid, 'founder_groupid' => ACCOUNT_MANAGE_GROUP_VICE_FOUNDER), 'uid');
  95. }
  96. $user_id = user_register($member);
  97. if (in_array($member['register_type'], array(USER_REGISTER_TYPE_QQ, USER_REGISTER_TYPE_WECHAT, USER_REGISTER_TYPE_MOBILE))) {
  98. pdo_update('users', array('username' => $member['username'] . $user_id . rand(100,999)), array('uid' => $user_id));
  99. }
  100. if($user_id > 0) {
  101. unset($member['password']);
  102. $member['uid'] = $user_id;
  103. if (!empty($profile)) {
  104. $profile['uid'] = $user_id;
  105. $profile['createtime'] = TIMESTAMP;
  106. pdo_insert('users_profile', $profile);
  107. }
  108. if (in_array($member['register_type'], array(USER_REGISTER_TYPE_QQ, USER_REGISTER_TYPE_WECHAT, USER_REGISTER_TYPE_MOBILE))) {
  109. pdo_insert('users_bind', array('uid' => $user_id, 'bind_sign' => $member['openid'], 'third_type' => $member['register_type'], 'third_nickname' => $member['username']));
  110. }
  111. if (in_array($member['register_type'], array(USER_REGISTER_TYPE_QQ, USER_REGISTER_TYPE_WECHAT))) {
  112. return $user_id;
  113. }
  114. return error(0, '注册成功'.(!empty($_W['setting']['register']['verify']) ? ',请等待管理员审核!' : ',请重新登录!'));
  115. }
  116. return error(-1, '增加用户失败,请稍候重试或联系网站管理员解决!');
  117. }
  118. }