1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* SmartQQ Library |
4
|
|
|
* @author Tao <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
namespace Slince\SmartQQ; |
7
|
|
|
|
8
|
|
|
use GuzzleHttp\Client as HttpClient; |
9
|
|
|
use GuzzleHttp\Cookie\CookieJar; |
10
|
|
|
use GuzzleHttp\Psr7\Request; |
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
12
|
|
|
use Slince\SmartQQ\Entity\Discuss; |
13
|
|
|
use Slince\SmartQQ\Entity\DiscussDetail; |
14
|
|
|
use Slince\SmartQQ\Entity\Friend; |
15
|
|
|
use Slince\SmartQQ\Entity\Group; |
16
|
|
|
use Slince\SmartQQ\Entity\GroupDetail; |
17
|
|
|
use Slince\SmartQQ\Entity\Profile; |
18
|
|
|
use Slince\SmartQQ\Exception\InvalidArgumentException; |
19
|
|
|
use Slince\SmartQQ\Exception\RuntimeException; |
20
|
|
|
use Slince\SmartQQ\Message\Request\FriendMessage; |
21
|
|
|
use Slince\SmartQQ\Message\Request\GroupMessage; |
22
|
|
|
use Slince\SmartQQ\Message\Request\Message as RequestMessage; |
23
|
|
|
use Slince\SmartQQ\Message\Response\Message as ResponseMessage; |
24
|
|
|
use Slince\SmartQQ\Request\GetLnickRequest; |
25
|
|
|
use Slince\SmartQQ\Request\RequestInterface; |
26
|
|
|
use Slince\SmartQQ\Request\GetCurrentUserRequest; |
27
|
|
|
use Slince\SmartQQ\Request\GetDiscussDetailRequest; |
28
|
|
|
use Slince\SmartQQ\Request\GetDiscussesRequest; |
29
|
|
|
use Slince\SmartQQ\Request\GetFriendDetailRequest; |
30
|
|
|
use Slince\SmartQQ\Request\GetFriendsOnlineStatusRequest; |
31
|
|
|
use Slince\SmartQQ\Request\GetFriendsRequest; |
32
|
|
|
use Slince\SmartQQ\Request\GetGroupDetailRequest; |
33
|
|
|
use Slince\SmartQQ\Request\GetGroupsRequest; |
34
|
|
|
use Slince\SmartQQ\Request\GetPtWebQQRequest; |
35
|
|
|
use Slince\SmartQQ\Request\GetQQRequest; |
36
|
|
|
use Slince\SmartQQ\Request\GetQrCodeRequest; |
37
|
|
|
use Slince\SmartQQ\Request\GetRecentListRequest; |
38
|
|
|
use Slince\SmartQQ\Request\GetUinAndPsessionidRequest; |
39
|
|
|
use Slince\SmartQQ\Request\GetVfWebQQRequest; |
40
|
|
|
use Slince\SmartQQ\Request\PollMessagesRequest; |
41
|
|
|
use Slince\SmartQQ\Request\SendDiscusMessageRequest; |
42
|
|
|
use Slince\SmartQQ\Request\SendFriendMessageRequest; |
43
|
|
|
use Slince\SmartQQ\Request\SendGroupMessageRequest; |
44
|
|
|
use Slince\SmartQQ\Request\SendMessageRequest; |
45
|
|
|
use Slince\SmartQQ\Request\VerifyQrCodeRequest; |
46
|
|
|
|
47
|
|
|
class Client |
48
|
|
|
{ |
49
|
|
|
/** |
50
|
|
|
* @var Credential |
51
|
|
|
*/ |
52
|
|
|
protected $credential; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* 客户端id(固定值) |
56
|
|
|
* @var int |
57
|
|
|
*/ |
58
|
|
|
protected static $clientId = 53999199; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* 获取ptwebqq的地址 |
62
|
|
|
* @var string |
63
|
|
|
*/ |
64
|
|
|
protected $certificationUrl; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var HttpClient |
68
|
|
|
*/ |
69
|
|
|
protected $httpClient; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var CookieJar |
73
|
|
|
*/ |
74
|
|
|
protected $cookies; |
75
|
|
|
|
76
|
|
|
public function __construct(Credential $credential = null, HttpClient $httpClient = null) |
77
|
|
|
{ |
78
|
|
|
if (!is_null($credential)) { |
79
|
|
|
$this->setCredential($credential); |
80
|
|
|
} |
81
|
|
|
if (is_null($httpClient)) { |
82
|
|
|
$httpClient = new HttpClient([ |
83
|
|
|
'verify' => false |
84
|
|
|
]); |
85
|
|
|
} |
86
|
|
|
$this->httpClient = $httpClient; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* 开启登录流程自行获取凭证 |
91
|
|
|
* @param string $loginQRImage 二维码图片位置 |
92
|
|
|
* @return Credential |
93
|
|
|
*/ |
94
|
|
|
public function login($loginQRImage) |
95
|
|
|
{ |
96
|
|
|
//如果登录则重置cookie |
97
|
|
|
$this->cookies = new CookieJar(); |
98
|
|
|
$qrSign = $this->makeQrCodeImage($loginQRImage); |
99
|
|
|
$ptQrToken = Utils::hash33($qrSign); |
100
|
|
|
while (true) { |
101
|
|
|
$status = $this->verifyQrCodeStatus($ptQrToken); |
102
|
|
|
if ($status == VerifyQrCodeRequest::STATUS_EXPIRED) { |
103
|
|
|
$qrSign = $this->makeQrCodeImage($loginQRImage); |
104
|
|
|
$ptQrToken = Utils::hash33($qrSign); |
105
|
|
|
} elseif ($status == VerifyQrCodeRequest::STATUS_CERTIFICATION) { |
106
|
|
|
//授权成功跳出状态检查 |
107
|
|
|
break; |
108
|
|
|
} |
109
|
|
|
sleep(1); |
110
|
|
|
} |
111
|
|
|
$ptWebQQ = $this->getPtWebQQ($this->certificationUrl); |
112
|
|
|
$vfWebQQ = $this->getVfWebQQ($ptWebQQ); |
113
|
|
|
list($uin, $pSessionId) = $this->getUinAndPSessionId($ptWebQQ); |
114
|
|
|
$this->credential = new Credential($ptWebQQ, $vfWebQQ, $pSessionId, $uin, static::$clientId, $this->cookies); |
115
|
|
|
return $this->credential; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* 创建登录所需的二维码 |
120
|
|
|
* @param string $loginQRImage |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
protected function makeQrCodeImage($loginQRImage) |
124
|
|
|
{ |
125
|
|
|
$response = $this->sendRequest(new GetQrCodeRequest()); |
126
|
|
|
Utils::getFilesystem()->dumpFile($loginQRImage, $response->getBody()); |
127
|
|
|
foreach ($this->getCookies() as $cookie) { |
128
|
|
|
if (strcasecmp($cookie->getName(), 'qrsig') == 0) { |
129
|
|
|
return $cookie->getValue(); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
throw new RuntimeException("Can not find parameter [qrsig]"); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* 验证二维码状态 |
137
|
|
|
* @param int $ptQrToken qr token |
138
|
|
|
* @return int |
139
|
|
|
*/ |
140
|
|
|
protected function verifyQrCodeStatus($ptQrToken) |
141
|
|
|
{ |
142
|
|
|
$request = new VerifyQrCodeRequest($ptQrToken); |
143
|
|
|
$response = $this->sendRequest($request); |
144
|
|
|
if (strpos($response->getBody(), '未失效') !== false) { |
145
|
|
|
$status = VerifyQrCodeRequest::STATUS_UNEXPIRED; |
146
|
|
|
} elseif (strpos($response->getBody(), '已失效') !== false) { |
147
|
|
|
$status = VerifyQrCodeRequest::STATUS_EXPIRED; |
148
|
|
|
} elseif (strpos($response->getBody(), '认证中') !== false) { |
149
|
|
|
$status = VerifyQrCodeRequest::STATUS_ACCREDITATION; |
150
|
|
|
} else { |
151
|
|
|
$status = VerifyQrCodeRequest::STATUS_CERTIFICATION; |
152
|
|
|
//找出认证url |
153
|
|
|
if (preg_match("#'(http.+)'#U", strval($response->getBody()), $matches)) { |
154
|
|
|
$this->certificationUrl = trim($matches[1]); |
155
|
|
|
} else { |
156
|
|
|
throw new RuntimeException("Can not find certification url"); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
return $status; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* 获取ptwebqq的参数值 |
164
|
|
|
* @param string $certificationUrl |
165
|
|
|
* @return string |
166
|
|
|
*/ |
167
|
|
|
protected function getPtWebQQ($certificationUrl) |
168
|
|
|
{ |
169
|
|
|
$request = new GetPtWebQQRequest(); |
170
|
|
|
$request->setUri($certificationUrl); |
171
|
|
|
$this->sendRequest($request); |
172
|
|
|
foreach ($this->getCookies() as $cookie) { |
173
|
|
|
if (strcasecmp($cookie->getName(), 'ptwebqq') == 0) { |
174
|
|
|
return $cookie->getValue(); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
throw new RuntimeException("Can not find parameter [ptwebqq]"); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param string $ptWebQQ |
182
|
|
|
* @return string |
183
|
|
|
*/ |
184
|
|
|
protected function getVfWebQQ($ptWebQQ) |
185
|
|
|
{ |
186
|
|
|
$request = new GetVfWebQQRequest($ptWebQQ); |
187
|
|
|
$response = $this->sendRequest($request); |
188
|
|
|
return GetVfWebQQRequest::parseResponse($response); |
|
|
|
|
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* 获取pessionid和uin |
193
|
|
|
* @param string $ptWebQQ |
194
|
|
|
* @return array |
195
|
|
|
*/ |
196
|
|
|
protected function getUinAndPSessionId($ptWebQQ) |
197
|
|
|
{ |
198
|
|
|
$request = new GetUinAndPsessionidRequest([ |
199
|
|
|
'ptwebqq' => $ptWebQQ, |
200
|
|
|
'clientid' => static::$clientId, |
201
|
|
|
'psessionid' => '', |
202
|
|
|
'status' => 'online' |
203
|
|
|
]); |
204
|
|
|
$response = $this->sendRequest($request); |
205
|
|
|
return GetUinAndPsessionidRequest::parseResponse($response); |
|
|
|
|
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @param Credential $credential |
210
|
|
|
*/ |
211
|
|
|
public function setCredential(Credential $credential) |
212
|
|
|
{ |
213
|
|
|
$this->cookies = $credential->getCookies(); |
214
|
|
|
$this->credential = $credential; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @return Credential |
219
|
|
|
*/ |
220
|
|
|
public function getCredential() |
221
|
|
|
{ |
222
|
|
|
if (!$this->credential) { |
223
|
|
|
throw new InvalidArgumentException("Please login first or set a credential"); |
224
|
|
|
} |
225
|
|
|
return $this->credential; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @return CookieJar |
230
|
|
|
*/ |
231
|
|
|
public function getCookies() |
232
|
|
|
{ |
233
|
|
|
return $this->cookies; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @param HttpClient $httpClient |
238
|
|
|
*/ |
239
|
|
|
public function setHttpClient($httpClient) |
240
|
|
|
{ |
241
|
|
|
$this->httpClient = $httpClient; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @return HttpClient |
246
|
|
|
*/ |
247
|
|
|
public function getHttpClient() |
248
|
|
|
{ |
249
|
|
|
return $this->httpClient; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* 获取所有的群 |
254
|
|
|
* @return EntityCollection |
255
|
|
|
*/ |
256
|
|
|
public function getGroups() |
257
|
|
|
{ |
258
|
|
|
$request = new GetGroupsRequest($this->getCredential()); |
259
|
|
|
$response = $this->sendRequest($request); |
260
|
|
|
return GetGroupsRequest::parseResponse($response, $this); |
|
|
|
|
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* 获取群详细信息 |
265
|
|
|
* @param Group $group |
266
|
|
|
* @return GroupDetail |
267
|
|
|
*/ |
268
|
|
|
public function getGroupDetail(Group $group) |
269
|
|
|
{ |
270
|
|
|
$request = new GetGroupDetailRequest($group, $this->getCredential()); |
271
|
|
|
$response = $this->sendRequest($request); |
272
|
|
|
return GetGroupDetailRequest::parseResponse($response); |
|
|
|
|
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* 获取所有讨论组 |
277
|
|
|
* @return EntityCollection |
278
|
|
|
*/ |
279
|
|
|
public function getDiscusses() |
280
|
|
|
{ |
281
|
|
|
$request = new GetDiscussesRequest($this->getCredential()); |
282
|
|
|
$response = $this->sendRequest($request); |
283
|
|
|
return GetDiscussesRequest::parseResponse($response); |
|
|
|
|
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* 获取讨论组详情 |
288
|
|
|
* @param Discuss $discuss |
289
|
|
|
* @return DiscussDetail |
290
|
|
|
*/ |
291
|
|
|
public function getDiscussDetail(Discuss $discuss) |
292
|
|
|
{ |
293
|
|
|
$request = new GetDiscussDetailRequest($discuss, $this->getCredential()); |
294
|
|
|
$response = $this->sendRequest($request); |
295
|
|
|
return GetDiscussDetailRequest::parseResponse($response); |
|
|
|
|
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* 获取所有的好友 |
300
|
|
|
* @return EntityCollection |
301
|
|
|
*/ |
302
|
|
|
public function getFriends() |
303
|
|
|
{ |
304
|
|
|
$request = new GetFriendsRequest($this->getCredential()); |
305
|
|
|
$response = $this->sendRequest($request); |
306
|
|
|
return GetFriendsRequest::parseResponse($response); |
|
|
|
|
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* 获取好友的详细信息 |
311
|
|
|
* @param Friend $friend |
312
|
|
|
* @return Profile |
313
|
|
|
*/ |
314
|
|
|
public function getFriendDetail(Friend $friend) |
315
|
|
|
{ |
316
|
|
|
$request = new GetFriendDetailRequest($friend, $this->getCredential()); |
317
|
|
|
$response = $this->sendRequest($request); |
318
|
|
|
return GetFriendDetailRequest::parseResponse($response); |
|
|
|
|
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* 获取好友的QQ号 |
323
|
|
|
* @param Friend $friend |
324
|
|
|
* @return int |
325
|
|
|
*/ |
326
|
|
|
public function getFriendQQ(Friend $friend) |
327
|
|
|
{ |
328
|
|
|
$request = new GetQQRequest($friend, $this->getCredential()); |
329
|
|
|
$response = $this->sendRequest($request); |
330
|
|
|
$qq = GetQQRequest::parseResponse($response); |
|
|
|
|
331
|
|
|
$friend->setQq($qq); |
332
|
|
|
return $qq; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* 获取好友的个性签名 |
337
|
|
|
* @param Friend $friend |
338
|
|
|
* @return string |
339
|
|
|
*/ |
340
|
|
|
public function getFriendLnick(Friend $friend) |
341
|
|
|
{ |
342
|
|
|
$request = new GetLnickRequest($friend, $this->getCredential()); |
343
|
|
|
$response = $this->sendRequest($request); |
344
|
|
|
return GetLnickRequest::parseResponse($response, $friend); |
|
|
|
|
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* 获取好友在线状态 |
349
|
|
|
* @return EntityCollection |
350
|
|
|
*/ |
351
|
|
|
public function getFriendsOnlineStatus() |
352
|
|
|
{ |
353
|
|
|
$request = new GetFriendsOnlineStatusRequest($this->getCredential()); |
354
|
|
|
$response = $this->sendRequest($request); |
355
|
|
|
return GetFriendsOnlineStatusRequest::parseResponse($response); |
|
|
|
|
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* 获取最近的会话 |
360
|
|
|
* @return EntityCollection |
361
|
|
|
*/ |
362
|
|
|
public function getRecentList() |
363
|
|
|
{ |
364
|
|
|
$request = new GetRecentListRequest($this->getCredential()); |
365
|
|
|
$response = $this->sendRequest($request); |
366
|
|
|
return GetRecentListRequest::parseResponse($response); |
|
|
|
|
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
/** |
370
|
|
|
* 获取当前登录用户信息 |
371
|
|
|
* @return Profile |
372
|
|
|
*/ |
373
|
|
|
public function getCurrentUserInfo() |
374
|
|
|
{ |
375
|
|
|
$request = new GetCurrentUserRequest(); |
376
|
|
|
$response = $this->sendRequest($request); |
377
|
|
|
return GetCurrentUserRequest::parseResponse($response); |
|
|
|
|
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* 轮询消息, |
382
|
|
|
* client并不会组装信息,只是将接口返回的信息完整抽象并返回 |
383
|
|
|
* 如果需要查询信息对应的数据,如发送人、发送群,请自行获取 |
384
|
|
|
* @return ResponseMessage[] |
385
|
|
|
*/ |
386
|
|
|
public function pollMessages() |
387
|
|
|
{ |
388
|
|
|
$request = new PollMessagesRequest($this->getCredential()); |
389
|
|
|
$response = $this->sendRequest($request); |
390
|
|
|
return PollMessagesRequest::parseResponse($response); |
|
|
|
|
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* 发送消息,包括好友消息,群消息,讨论组消息 |
395
|
|
|
* @param RequestMessage $message |
396
|
|
|
* @return bool |
397
|
|
|
*/ |
398
|
|
|
public function sendMessage(RequestMessage $message) |
399
|
|
|
{ |
400
|
|
|
if ($message instanceof FriendMessage) { |
401
|
|
|
$request = new SendFriendMessageRequest($message, $this->getCredential()); |
402
|
|
|
} elseif ($message instanceof GroupMessage) { |
403
|
|
|
$request = new SendGroupMessageRequest($message, $this->getCredential()); |
404
|
|
|
} else { |
405
|
|
|
$request = new SendDiscusMessageRequest($message, $this->getCredential()); |
|
|
|
|
406
|
|
|
} |
407
|
|
|
$response = $this->sendRequest($request); |
408
|
|
|
return SendMessageRequest::parseResponse($response); |
|
|
|
|
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
/** |
412
|
|
|
* @param RequestInterface $request |
413
|
|
|
* @return ResponseInterface |
414
|
|
|
*/ |
415
|
|
|
protected function sendRequest(RequestInterface $request) |
416
|
|
|
{ |
417
|
|
|
$options = [ |
418
|
|
|
'cookies' => $this->getCookies() |
419
|
|
|
]; |
420
|
|
|
if ($parameters = $request->getParameters()) { |
421
|
|
|
if ($request->getMethod() == RequestInterface::REQUEST_METHOD_GET) { |
422
|
|
|
$options['query'] = $parameters; |
423
|
|
|
} else { |
424
|
|
|
$options['form_params'] = $parameters; |
425
|
|
|
} |
426
|
|
|
} |
427
|
|
|
//如果有referer需要伪造该信息 |
428
|
|
|
if ($referer = $request->getReferer()) { |
429
|
|
|
$options['headers'] = [ |
430
|
|
|
'Referer' => $referer |
431
|
|
|
]; |
432
|
|
|
} |
433
|
|
|
$response = $this->httpClient->send(new Request($request->getMethod(), $request->getUri()), $options); |
434
|
|
|
return $response; |
435
|
|
|
} |
436
|
|
|
} |
437
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.