PageRenderTime 35ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/app/account/openid/google.php

https://gitlab.com/Ltaimao/wecenter
PHP | 210 lines | 153 code | 45 blank | 12 comment | 30 complexity | 69d52149d579235c872f697a72686a12 MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------------
  4. | WeCenter [#RELEASE_VERSION#]
  5. | ========================================
  6. | by WeCenter Software
  7. | © 2011 - 2014 WeCenter. All Rights Reserved
  8. | http://www.wecenter.com
  9. | ========================================
  10. | Support: WeCenter@qq.com
  11. |
  12. +---------------------------------------------------------------------------
  13. */
  14. if (!defined('IN_ANWSION'))
  15. {
  16. die;
  17. }
  18. class openid_google extends AWS_CONTROLLER
  19. {
  20. public function get_access_rule()
  21. {
  22. $rule_action['rule_type'] = 'white';
  23. $rule_action['actions'] = array(
  24. 'bind'
  25. );
  26. return $rule_action;
  27. }
  28. public function setup()
  29. {
  30. HTTP::no_cache_header();
  31. if (get_setting('google_login_enabled') != 'Y' OR !get_setting('google_client_id') OR !get_setting('google_client_secret'))
  32. {
  33. H::redirect_msg(AWS_APP::lang()->_t('本站未开通 Google 登录'), '/');
  34. }
  35. }
  36. public function bind_action()
  37. {
  38. if (AWS_APP::session()->google_user)
  39. {
  40. $google_user_info = AWS_APP::session()->google_user;
  41. unset(AWS_APP::session()->google_user);
  42. }
  43. if ($_GET['error'] == 'access_denied')
  44. {
  45. H::redirect_msg(AWS_APP::lang()->_t('授权失败'), '/account/login/');
  46. }
  47. if ($this->user_id)
  48. {
  49. $google_user = $this->model('openid_google')->get_google_user_by_uid($this->user_id);
  50. if ($google_user)
  51. {
  52. H::redirect_msg(AWS_APP::lang()->_t('此账号已绑定 Google 账号'), '/account/login/');
  53. }
  54. }
  55. if ($_GET['code'])
  56. {
  57. if ($_GET['code'] != $google_user_info['authorization_code'])
  58. {
  59. $this->model('openid_google')->authorization_code = $_GET['code'];
  60. $this->model('openid_google')->redirect_url = '/account/openid/google/bind/';
  61. if (!$this->model('openid_google')->oauth2_login())
  62. {
  63. H::redirect_msg($this->model('openid_google')->error_msg, '/account/login/');
  64. }
  65. $google_user_info = $this->model('openid_google')->user_info;
  66. }
  67. if (!$google_user_info)
  68. {
  69. H::redirect_msg(AWS_APP::lang()->_t('Google 登录失败,用户信息不存在'), '/account/login/');
  70. }
  71. $google_user = $this->model('openid_google')->get_google_user_by_id($google_user_info['id']);
  72. if ($this->user_id)
  73. {
  74. if ($google_user)
  75. {
  76. H::redirect_msg(AWS_APP::lang()->_t('此 Google 账号已被绑定'), '/account/login/');
  77. }
  78. $this->model('openid_google')->bind_account($google_user_info, $this->user_id);
  79. if (!$this->model('integral')->fetch_log($this->user_id, 'BIND_OPENID'))
  80. {
  81. $this->model('integral')->process($this->user_id, 'BIND_OPENID', round((get_setting('integral_system_config_profile') * 0.2)), '绑定 OPEN ID');
  82. }
  83. HTTP::redirect('/account/setting/openid/');
  84. }
  85. else
  86. {
  87. if ($google_user)
  88. {
  89. $user = $this->model('account')->get_user_info_by_uid($google_user['uid']);
  90. if (!$user)
  91. {
  92. $this->model('openid_google')->unbind_account($google_user['uid']);
  93. H::redirect_msg(AWS_APP::lang()->_t('本地用户不存在'), '/account/login/');
  94. }
  95. $this->model('openid_google')->update_user_info($google_user['id'], $google_user_info);
  96. if (get_setting('register_valid_type') == 'approval' AND $user['group_id'] == 3)
  97. {
  98. $redirect_url = '/account/valid_approval/';
  99. }
  100. else
  101. {
  102. if ($_GET['state'])
  103. {
  104. $state = base64_url_decode($_GET['state']);
  105. }
  106. if (get_setting('ucenter_enabled') == 'Y')
  107. {
  108. $redirect_url = '/account/sync_login/';
  109. if ($state['return_url'])
  110. {
  111. $redirect_url .= 'url-' . base64_encode($state['return_url']);
  112. }
  113. }
  114. else if ($state['return_url'])
  115. {
  116. $redirect_url = $state['return_url'];
  117. }
  118. else
  119. {
  120. $redirect_url = '/';
  121. }
  122. HTTP::set_cookie('_user_login', get_login_cookie_hash($user['user_name'], $user['password'], $user['salt'], $user['uid'], false));
  123. if (get_setting('register_valid_type') == 'email' AND !$user['valid_email'])
  124. {
  125. AWS_APP::session()->valid_email = $user['email'];
  126. }
  127. }
  128. HTTP::redirect($redirect_url);
  129. }
  130. else
  131. {
  132. switch (get_setting('register_type'))
  133. {
  134. case 'close':
  135. H::redirect_msg(AWS_APP::lang()->_t('本站目前关闭注册'), '/account/login/');
  136. break;
  137. case 'invite':
  138. H::redirect_msg(AWS_APP::lang()->_t('本站只能通过邀请注册'), '/account/login/');
  139. break;
  140. case 'weixin':
  141. H::redirect_msg(AWS_APP::lang()->_t('本站只能通过微信注册'), '/account/login/');
  142. break;
  143. }
  144. AWS_APP::session()->google_user = $google_user_info;
  145. $this->crumb(AWS_APP::lang()->_t('完善资料'), '/account/login/');
  146. TPL::assign('register_url', 'account/ajax/google/register/');
  147. TPL::assign('user_name', AWS_APP::session()->google_user['name']);
  148. TPL::assign('email', AWS_APP::session()->google_user['email']);
  149. TPL::import_css('css/register.css');
  150. TPL::output('account/openid/callback');
  151. }
  152. }
  153. }
  154. else
  155. {
  156. $state = ($_GET['return_url']) ? base64_url_encode(array('return_url' => base64_decode($_GET['return_url']))) : null;
  157. HTTP::redirect($this->model('openid_google')->get_redirect_url('/account/openid/google/bind/', $state));
  158. }
  159. }
  160. public function unbind_action()
  161. {
  162. $this->model('openid_google')->unbind_account($this->user_id);
  163. HTTP::redirect('/account/setting/openid/');
  164. }
  165. }