Client.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <?php
  2. # Oauth Client
  3. namespace Oauth\Lib;
  4. use Dever;
  5. use Dever\Session\Oper as Save;
  6. use Passport\Lib\Base;
  7. class Client extends Base
  8. {
  9. /**
  10. * @desc prefix
  11. * @var string
  12. */
  13. private $prefix = 'v3_';
  14. /**
  15. * @desc account
  16. * @var int
  17. */
  18. private $account = 1;
  19. /**
  20. * @desc save
  21. * @var object
  22. */
  23. protected $session = null;
  24. /**
  25. * @desc passport
  26. * @var object
  27. */
  28. protected $passport = null;
  29. /**
  30. * @desc request
  31. * @var array
  32. */
  33. private $request = null;
  34. /**
  35. * @desc config
  36. * @var array
  37. */
  38. private $config = null;
  39. /**
  40. * @desc refer_param
  41. * @var array
  42. */
  43. private $refer_param = array();
  44. public function __construct()
  45. {
  46. parent::__construct();
  47. $this->initPassport();
  48. $this->initSave();
  49. $this->initRequest();
  50. $this->initAccount();
  51. $this->initSource();
  52. $this->initGet();
  53. $this->initInvite();
  54. $this->initSystem();
  55. $this->initSourceType();
  56. $this->initSystemSource();
  57. $this->initRefer();
  58. $this->initConfig();
  59. }
  60. private function initPassport()
  61. {
  62. $this->passport = Dever::load('passport/lib/base');
  63. }
  64. private function initSave()
  65. {
  66. $this->session = new Save(DEVER_PROJECT, 'session');
  67. }
  68. private function initRequest()
  69. {
  70. $this->request = Dever::input();
  71. }
  72. private function initAccount()
  73. {
  74. $this->account = (isset($this->request['account']) && $this->request['account']) ? $this->request['account'] : $this->session->get($this->prefix . 'oauth_account');
  75. $this->session->add($this->prefix . 'oauth_account', $this->account);
  76. }
  77. private function initSystem()
  78. {
  79. $this->system = (isset($this->request['system']) && $this->request['system']) ? $this->request['system'] : $this->session->get($this->prefix . 'oauth_system');
  80. $this->session->add($this->prefix . 'oauth_system', $this->system);
  81. }
  82. private function initSourceType()
  83. {
  84. $this->source_type = (isset($this->request['source_type']) && $this->request['source_type']) ? $this->request['source_type'] : $this->session->get($this->prefix . 'oauth_source_type');
  85. $this->session->add($this->prefix . 'oauth_source_type', $this->source_type);
  86. }
  87. private function initSystemSource()
  88. {
  89. $this->system_source = (isset($this->request['system_source']) && $this->request['system_source']) ? $this->request['system_source'] : $this->session->get($this->prefix . 'oauth_system_source');
  90. $this->session->add($this->prefix . 'oauth_system_source', $this->system_source);
  91. }
  92. private function initSource()
  93. {
  94. $this->source = (isset($this->request['source']) && $this->request['source']) ? $this->request['source'] : $this->session->get($this->prefix . 'oauth_source');
  95. $this->session->add($this->prefix . 'oauth_source', $this->source);
  96. }
  97. private function initGet()
  98. {
  99. $this->get = (isset($this->request['get']) && $this->request['get']) ? $this->request['get'] : $this->session->get($this->prefix . 'oauth_get');
  100. $this->session->add($this->prefix . 'oauth_get', $this->get);
  101. }
  102. private function initInvite()
  103. {
  104. $this->invite = (isset($this->request['invite']) && $this->request['invite']) ? $this->request['invite'] : $this->session->get($this->prefix . 'oauth_invite');
  105. $this->session->add($this->prefix . 'oauth_invite', $this->invite);
  106. }
  107. private function initRefer()
  108. {
  109. $this->refer = (isset($this->request['refer']) && $this->request['refer']) ? $this->request['refer'] : $this->session->get($this->prefix . 'oauth_refer');
  110. $this->session->add($this->prefix . 'oauth_refer', $this->refer);
  111. }
  112. private function initConfig()
  113. {
  114. $this->config = Dever::db('oauth/account')->one($this->account);
  115. if (!$this->config) {
  116. Dever::alert('账户错误');
  117. }
  118. $this->config += Dever::config($this->config['type'])->cAll;
  119. }
  120. private function location($result = '')
  121. {
  122. if ($this->refer) {
  123. $refer = base64_decode($this->refer);
  124. if (!strstr($refer, 'http')) {
  125. return $result;
  126. }
  127. if ($this->refer_param) {
  128. $param = http_build_query($this->refer_param);
  129. if (strstr($refer, '?')) {
  130. $refer .= '&' . $param;
  131. } else {
  132. $refer .= '?' . $param;
  133. }
  134. }
  135. Dever::location($refer);
  136. } else {
  137. return $result;
  138. }
  139. }
  140. /**
  141. * @desc oauth请求
  142. */
  143. public function auth()
  144. {
  145. $info = $this->info();
  146. if ($info) {
  147. return $this->location(true);
  148. }
  149. $id = Dever::id();
  150. $this->session->add($this->prefix . 'oauth_id', $id);
  151. $uid = $this->passport->check(false);
  152. $this->session->add($this->prefix . 'oauth_uid', $uid);
  153. $this->param('auth', 'appid', $this->config['appid']);
  154. $this->param('auth', 'redirect_uri', Dever::url('request.callback?account=' . $this->account, 'oauth'));
  155. $this->param('auth', 'state', $id);
  156. $this->param('auth', 'response_type');
  157. $this->param('auth', 'scope');
  158. //print_r($this->config['auth']);die;
  159. $url = $this->config['auth']['url'] . '?' . http_build_query($this->config['auth']['param']);
  160. Dever::location($url);
  161. }
  162. /**
  163. * @desc oauth请求 callback
  164. */
  165. public function callback($url = '')
  166. {
  167. $this->js = true;
  168. if ((isset($this->request['js']) && $this->request['js'])) {
  169. $this->js = false;
  170. }
  171. if ($this->js == true && $url) {
  172. return $this->output($url);
  173. } else {
  174. $id = $this->session->get($this->prefix . 'oauth_id');
  175. if (isset($this->config['token']['param'])) {
  176. if (!$this->session->get($this->prefix . 'oauth_refresh')) {
  177. $this->param('token', 'code');
  178. $this->param('token', 'appid', $this->config['appid']);
  179. $this->param('token', 'secret', $this->config['appsecret']);
  180. $this->param('token', 'grant_type');
  181. $result = Dever::curl($this->config['token']['url'], $this->config['token']['param']);
  182. $result = Dever::json_decode($result);
  183. if (isset($result['errmsg'])) {
  184. Dever::alert($result['errmsg']);
  185. }
  186. parse_str(http_build_query($result), $this->request);
  187. $this->response('token', 'access_token');
  188. $this->response('token', 'expires_in');
  189. $this->response('token', 'refresh_token');
  190. $this->response('token', 'openid');
  191. $this->response('token', 'unionid');
  192. $this->response('token', 'scope');
  193. $this->response('token', 'errcode');
  194. $this->response('token', 'errmsg');
  195. } else {
  196. # 由于refresh token是长期有效的,所以这里无需再次获取了。之后通过这个refresh获取access token就行了
  197. return;
  198. }
  199. }
  200. if (isset($this->config['token']['response']) && $this->config['token']['response']) {
  201. # 进入绑定流程吧
  202. return $this->bind();
  203. }
  204. }
  205. Dever::alert('登录失败');
  206. }
  207. /**
  208. * @desc 重新获取token 暂时不用
  209. * @author leo(suwi.bin)
  210. * @date 2012-08-27
  211. */
  212. protected function refresh()
  213. {
  214. $data = $this->request;
  215. $state = false;
  216. if(isset($data['token_refresh']) && $data['token_refresh'])
  217. {
  218. $this->param('refresh', 'refresh_token', $data['token_refresh']);
  219. $this->param('refresh', 'client_id', $this->_config['id']);
  220. $this->param('refresh', 'client_secret', $this->_config['key']);
  221. $return = json_decode($this->_curl('post', $this->param['refresh']), true);
  222. if(isset($return['error']))
  223. {
  224. Dever::alert('参数错误');
  225. }
  226. if(isset($return['access_token']) && $return['access_token'])
  227. {
  228. $update['token_code'] = $return['access_token'];
  229. $update['token_type'] = $return['token_type'];
  230. $update['token_time'] = $return['expires_in'];
  231. $state = $this->update($update, $data['id']);
  232. }
  233. }
  234. return $state;
  235. }
  236. /**
  237. * @desc 绑定数据
  238. */
  239. private function bind()
  240. {
  241. $data = $this->config['token']['response'];
  242. if (!$data) {
  243. Dever::alert('错误的数据');
  244. }
  245. $get = $this->request;
  246. $id = false;
  247. if (isset($get['id']) && $get['id'] > 0) {
  248. $id = $get['id'];
  249. }
  250. $user = $this->update($data, $id);
  251. return $this->location($user);
  252. }
  253. /**
  254. * @desc 绑定数据
  255. */
  256. private function update($data, $id)
  257. {
  258. if ($this->get && isset($data[$this->get])) {
  259. $key = $this->get;
  260. $get = $data[$key];
  261. $this->get = array();
  262. $this->get[$key] = $data[$key];
  263. }
  264. $this->param('user', 'access_token', $data['access_token']);
  265. $this->param('user', 'openid', $data['openid']);
  266. $userinfo = Dever::json_decode(Dever::curl($this->config['user']['url'], $this->config['user']['param']));
  267. if (!$userinfo) {
  268. return;
  269. }
  270. //$user['bind'] = 1;
  271. $user['temp'] = 2;
  272. //$user['username'] = Dever::emoji($userinfo['nickname']);
  273. $user['username'] = $userinfo['nickname'];
  274. if ($userinfo['headimgurl']) {
  275. //$update['set_avatar'] = $this->sessionAvatar($pic);
  276. $user['avatar'] = $userinfo['headimgurl'];
  277. }
  278. if ($userinfo['city']) {
  279. $user['city'] = $userinfo['city'];
  280. }
  281. if ($userinfo['province']) {
  282. $user['province'] = $userinfo['province'];
  283. }
  284. if ($userinfo['country']) {
  285. $user['country'] = $userinfo['country'];
  286. }
  287. $uid = $this->session->get($this->prefix . 'oauth_uid');
  288. $user = $this->passport->wechat($data, $user, $this->account, $this->system, $this->source_type, $this->system_source, $this->source, $this->invite, $uid);
  289. return $user;
  290. }
  291. /**
  292. * @desc 输出js内容
  293. */
  294. private function output($url)
  295. {
  296. $html =
  297. '<script>
  298. var params = {}, queryString = location.hash.substring(1),
  299. regex = /([^&=]+)=([^&]*)/g, m;
  300. while (m = regex.exec(queryString))
  301. {
  302. params[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
  303. }
  304. location.href="'.$url.'&js=false&" + queryString;
  305. </script>';
  306. echo $html;die;
  307. }
  308. /**
  309. * @desc 请求参数
  310. */
  311. private function param($type, $key, $value = false)
  312. {
  313. $this->compatible($this->config[$type]['param'], $key, $value);
  314. }
  315. /**
  316. * @desc 响应参数
  317. */
  318. private function response($type, $key, $value = false)
  319. {
  320. $this->compatible($this->config[$type]['response'], $key, $value);
  321. }
  322. /**
  323. * @desc 兼容处理
  324. */
  325. private function compatible(&$param, $key, $value = false)
  326. {
  327. $default = false;
  328. if (isset($param[$key]) && is_array($param[$key])) {
  329. $nkey = $param[$key][0];
  330. $default = $param[$key][1];
  331. unset($param[$key]);
  332. $key = $nkey;
  333. } else {
  334. $default = $param[$key];
  335. }
  336. return $param[$key] = ($value ? $value : (isset($this->request[$key]) ? $this->request[$key] : $default));
  337. }
  338. }