/
Baidu.php
385 lines (344 loc) · 9.74 KB
/
Baidu.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<?php
/***************************************************************************
*
* Copyright (c) 2011 Baidu.com, Inc. All Rights Reserved
*
**************************************************************************/
require_once('BaiduStore.php');
require_once('BaiduOAuth2.php');
require_once('BaiduApiClient.php');
require_once('BaiduUtils.php');
/**
*
* @package Baidu
* @author zhujianting(zhujianting@baidu.com)
* @version v2.0.0
*/
class Baidu
{
/**
* The client_id of the app or access_key of the developer.
* @var string
*/
protected $clientId;
/**
* The client_secret of the app or secret_key of the developer.
* @var string
*/
protected $clientSecret;
/**
* Redirect uri of the app, where we will redirect to after user authorization.
* @var string
*/
protected $redirectUri;
/**
* Storage for the user session related datas, like state, authorization code,
* access token and so on.
*
* @var BaiduStore
*/
protected $store = null;
/**
* @var string
*/
protected $state = null;
/**
* User session info.
* @var array
*/
protected $session = null;
/**
* @var BaiduOAuth2
*/
protected $oauth2 = null;
/**
* Constructor
*
* @param string $clientId The client_id of the app or access_key of the developer.
* @param string $clientSecret The client_secret of the app or secret_key of the developer.
* @param string $redirectUri Redirect uri of the app.
* @param BaiduStore $store Storage for the user session related datas.
*/
public function __construct()
{
$this->clientId = C('BaiduOpenid.clientId');
$this->clientSecret = C('BaiduOpenid.clientSecret');
$this->redirectUri = C('BaiduOpenid.redirectUri');
$this->setStore($store ? $store : new BaiduCookieStore($clientId));
}
/**
* Get an instance of BaiduOAuth2 class.
*
* @return BaiduOAuth2
*/
public function getBaiduOAuth2Service()
{
if (!$this->oauth2) {
$this->oauth2 = new BaiduOAuth2();
}
return $this->oauth2;
}
/**
* Get an instance of BaiduApiClient class.
*
* @param string $accessToken Access token for api calls.
* @return BaiduApiClient
*/
public function getBaiduApiClientService()
{
return new BaiduApiClient($this->clientId, $this->getAccessToken());
}
/**
* Get access token for openapi calls.
*
* @return string|false Returns access token if user has authorized the app, or false if not.
*/
public function getAccessToken()
{
$session = $this->getSession();
if (isset($session['access_token'])) {
return $session['access_token'];
} else {
return false;
}
}
/**
* Get refresh token.
*
* @return string|false Returns refresh token if app has, or false if not.
*/
public function getRefreshToken()
{
$session = $this->getSession();
if (isset($session['refresh_token'])) {
return $session['refresh_token'];
} else {
return false;
}
}
/**
* Get currently logged in user's uid.
*
* @return uint|false Return uid of the loggedin user, or return
* false if user isn't loggedin.
*/
public function getLoggedInUser()
{
// Get user from cached data or from access token
$user = $this->getUser();
p($user);die;
// If there's bd_sig & bd_user parameter in query parameters,
// it must be an inside web app(app on baidu) loading request,
// then we must check whether the uid passed from baidu is the
// same as we get from persistent data or from access token,
// if it's not, we should clear all the persistent data and to
// get an access token again.
if (isset($_REQUEST['bd_sig']) && isset($_REQUEST['bd_user'])) {
$params = array('bd_user' => $_REQUEST['bd_user']);
$sig = BaiduUtils::generateSign($params, $this->clientSecret, 'bd_sig');
if ($sig != $_REQUEST['bd_sig'] || $user['uid'] != $_REQUEST['bd_user']) {
$this->store->remove('session');
return false;
}
}
return $user;
}
/**
* Get a Login URL for use with redirects. By default, full page redirect is
* assumed. If you are using the generated URL with a window.open() call in
* JavaScript, you can pass in display=popup as part of the $params.
*
* @param string $scope blank space separated list of requested extended perms
* @param string $display Authorization page style, 'page', 'popup', 'touch' or 'mobile'
* @return string the URL for the login flow
*/
public function getLoginUrl($scope = '', $display = 'page')
{
$oauth2 = $this->getBaiduOAuth2Service();
return $oauth2->getAuthorizeUrl('code', $scope, $this->state, $display);
}
/**
* Get the Logout URL suitable for use with redirects.
*
* @param string $next Url to go to after a successful logout.
*
* @return string
*/
public function getLogoutUrl($next)
{
$oauth2 = $this->getBaiduOAuth2Service();
return $oauth2->getLogoutUrl($this->getAccessToken(), $next);
}
/**
* Get user session info.
*
* @return array
*/
public function getSession()
{
if ($this->session === null) {
$this->session = $this->doGetSession();
}
return $this->session;
}
/**
* Set user session.
*
* @param array $session User session info.
* @return Baidu
*/
public function setSession($session)
{
$this->session = $session;
if ($session) {
$this->store->set('session', $session);
} else {
$this->store->remove('session');
}
return $this;
}
/**
* Get current user's uid and uname.
*
* @return array|false array('uid' => xx, 'uname' => xx)
*/
protected function getUser()
{
$session = $this->getSession();
if (is_array($session) && isset($session['uid']) && isset($session['uname'])) {
return array('uid' => $session['uid'], 'uname' => $session['uname']);
} else {
return false;
}
}
/**
* Set the session data storage instance.
*
* @param BaiduStore $store
* @return Baidu
*/
protected function setStore($store)
{
$this->store = $store;
if ($this->store) {
$state = $this->store->get('state');
if (!empty($state)) {
$this->state = $state;
}
//as the storage engine is changed, we need to get the session again.
$this->session = null;
$this->getSession();
$this->establishCSRFTokenState();
}
return $this;
}
/**
* Get session info from Baidu server or from the store in app server side.
*
* @return array|false
*/
protected function doGetSession()
{
// get authorization code from query parameters
$code = $this->getCode();
// check whether it is a CSRF attack request
if ($code && $code != $this->store->get('code')) {
$oauth2 = $this->getBaiduOAuth2Service();
$session = $oauth2->getAccessTokenByAuthorizationCode($code);
if ($session) {
$this->store->set('code', $code);
$this->setSession($session);
$apiClient = new BaiduApiClient($this->clientId, $session['access_token']);
$user = $apiClient->api('passport/users/getLoggedInUser');
if ($user) {
$session = array_merge($session, $user);
$this->setSession($session);
}
return $session;
}
// code was bogus, so everything based on it should be invalidated.
$this->store->removeAll();
return false;
}
// as a fallback, just return whatever is in the storage
$session = $this->store->get('session');
$this->setSession($session);
if ($session && !isset($session['uid'])) {
$apiClient = new BaiduApiClient($this->clientId, $session['access_token']);
$user = $apiClient->api('passport/users/getLoggedInUser');
if ($user) {
$session = array_merge($session, $user);
$this->setSession($session);
}
}
return $session;
}
/**
* [getUserInfo 获取用户信息]
* @return [type] [description]
*/
public function getUserInfo(){
// get authorization code from query parameters
$code = $this->getCode();
// check whether it is a CSRF attack request
if ($code && $code != $this->store->get('code')) {p(1);die;
$oauth2 = $this->getBaiduOAuth2Service();
$session = $oauth2->getAccessTokenByAuthorizationCode($code);
if ($session) {
$this->store->set('code', $code);
$this->setSession($session);
$apiClient = new BaiduApiClient($this->clientId, $session['access_token']);
$user = $apiClient->api('passport/users/getInfo');
if ($user) {
$session = array_merge($session, $user);
$this->setSession($session);
}
return $session;
}
$this->store->removeAll();
return false;
}
// as a fallback, just return whatever is in the storage
$session = $this->store->get('session');
$this->setSession($session);
if ($session) {
$apiClient = new BaiduApiClient($this->clientId, $session['access_token']);
$user = $apiClient->api('passport/users/getInfo');
$user['sex']=$user['sex']==1?'男':'女';
$user['nikename']=$user['username'];
$user['head']='http://tb.himg.baidu.com/sys/portraitn/item/'.$user['portrait'];
// if ($user) {
// $session = array_merge($session,$user);
// $this->setSession($session);
// }
}
return $user;
}
/**
* Get the authorization code from the query parameters, if it exists,
* otherwise return false to signal no authorization code was discoverable.
*
* @return mixed Returns the authorization code, or false if the authorization
* code could not be determined.
*/
protected function getCode()
{
if (isset($_GET['code'])) {
$this->state = null;
$this->store->remove('state');
return $_GET['code'];
}
}
/**
* Lays down a CSRF state token for this process.
*
* @return void
*/
protected function establishCSRFTokenState()
{
if ($this->state === null) {
$this->state = md5(uniqid(mt_rand(), true));
$this->store->set('state', $this->state);
}
}
}