Client.php 10 KB

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