forked from xjflyttp/yii2-oauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQqExmailAuth.php
399 lines (369 loc) · 11.1 KB
/
QqExmailAuth.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
<?php
namespace xj\oauth;
use xj\oauth\exception\QqExmailException;
use yii\authclient\InvalidResponseException;
use yii\authclient\OAuth2;
use yii\authclient\OAuthToken;
/**
* Tencent Exmail OAuth
* 腾讯企业邮箱API OAUTH
* @version 1.4
* @author xjflyttp <[email protected]>
* @see PDF
* @see http://exmail.qq.com/cgi-bin/download?path=bizopenapidoc&filename=%cc%da%d1%b6%c6%f3%d2%b5%d3%ca%cf%e4OpenApi%d0%ad%d2%e9v1.4.pdf
*/
class QqExmailAuth extends OAuth2
{
//Action
const ACTION_DEL = 1;
const ACTION_ADD = 2;
const ACTION_MOD = 3;
//Gender
const GENDER_MALE = 1;
const GENDER_FEMALE = 2;
//OpenType
const OPEN_TYPE_UNSET = 0;
const OPEN_TYPE_ENABLE = 1;
const OPEN_TYPE_DISABLE = 2;
//Md5
const MD5_PLAINTEXT = 0;
const MD5_ENCYPT = 1;
//EmailAvailable
const ACCOUNT_TYPE_UNAVAILABLE = -1; //帐号名无效
const ACCOUNT_TYPE_AVAILABLE = 0; //帐号名没被占用
const ACCOUNT_TYPE_MAIN = 1; //主帐号名
const ACCOUNT_TYPE_ALIAS = 2; //别名账户
const ACCOUNT_TYPE_MAILGROUP = 3; //群组账户
//GroupStatus
const GROUP_STATUS_ALL = 'all';
const GROUP_STATUS_INNER = 'inner';
const GROUP_STATUS_GROUP = 'group';
const GROUP_STATUS_LIST = 'list';
public $authUrl = '';
public $tokenUrl = 'https://exmail.qq.com/cgi-bin/token';
public $apiBaseUrl = 'http://openapi.exmail.qq.com:12211';
public $templateOneKeyLoginUrl = 'https://exmail.qq.com/cgi-bin/login?fun=bizopenssologin&method=bizauth&agent=<agent>&user=<email>&ticket=<ticket>';
protected function initUserAttributes()
{
return [];
}
/**
* Fetches access token
* @param string $authCode ignore in this time
* @param array $params additional request params.
* @return OAuthToken access token.
* @throws InvalidResponseException
*/
public function fetchAccessToken($authCode = null, array $params = [])
{
$defaultParams = [
'grant_type' => 'client_credentials',
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
];
$response = $this->sendRequest('POST', $this->tokenUrl, array_merge($defaultParams, $params));
$token = $this->createToken(['params' => $response]);
$this->setAccessToken($token);
return $token;
}
/**
* @return OAuthToken auth token instance.
* @throws QqExmailException
*/
public function getAccessToken()
{
$accessToken = parent::getAccessToken();
if (null === $accessToken || !$accessToken->getIsValid()) {
$accessToken = $this->fetchAccessToken();
}
if (null === $accessToken) {
throw new QqExmailException('getAccessToken Fail.');
}
return $accessToken;
}
/**
*
* @param string $email MemberEmail
* @return string Member Auth Key
* @throws QqExmailException
*/
public function getMemberAuthKey($email)
{
$result = $this->api('openapi/mail/authkey', 'GET', ['alias' => $email]);
if (isset($result['errcode']) || isset($result['error'])) {
throw new QqExmailException($result['error'], $result['errcode']);
}
return $result['auth_key'];
}
/**
*
* @param string $email login EMAIL
* @param string $ticket getAuthKey()
* @return string Login Web Url
* @throws QqExmailException
*/
public function getOneKeyLogin($email, $ticket = null)
{
$urlTemplate = $this->templateOneKeyLoginUrl;
$agent = $this->clientId;
if (null === $ticket) {
$ticket = $this->getMemberAuthKey($email);
}
$requestUrl = str_replace([
'<agent>', '<email>', '<ticket>'
], [
$agent, $email, $ticket
], $urlTemplate);
return $requestUrl;
}
/**
*
* @param string $email MemberEmail
* @return string MemberInfo
* @throws QqExmailException
*/
public function getMemberInfo($email)
{
$result = $this->api('openapi/user/get', 'POST', ['alias' => $email]);
if (isset($result['errcode']) || isset($result['error'])) {
throw new QqExmailException($result['error'], $result['errcode']);
}
return $result;
}
/**
* Add Mod Del Member
* @param string $email
* @param [] $options
* @return []
* @throws QqExmailException
*/
public function syncMember($email, $options)
{
$options['alias'] = $email;
$result = $this->api('openapi/user/sync', 'POST', $options);
if (isset($result['errcode']) || isset($result['error'])) {
throw new QqExmailException($result['error'], $result['errcode']);
}
return true;
}
/**
*
* @param string $email
* @return boolean
*/
public function delMember($email)
{
try {
$this->syncMember($email, ['action' => self::ACTION_DEL]);
} catch (QqExmailException $ex) {
return false;
}
return true;
}
/**
* Add Mod Del Patry
* @param [] $options Del/Add only DstPath , Mod need SrcPath & DstPath
* @return []
* @throws QqExmailException
*/
public function syncParty($options)
{
$result = $this->api('openapi/party/sync', 'POST', $options);
if (isset($result['errcode']) || isset($result['error'])) {
throw new QqExmailException($result['error'], $result['errcode']);
}
return true;
}
/**
*
* @param string $partyPath
* @return []
* @throws QqExmailException
*/
public function getPartList($partyPath)
{
$options = [
'partypath' => $partyPath,
];
$result = $this->api('openapi/party/list', 'POST', $options);
if (isset($result['errcode']) || isset($result['error'])) {
throw new QqExmailException($result['error'], $result['errcode']);
}
return $result;
}
/**
*
* @param string $partyPath
* @return []
* @throws QqExmailException
*/
public function getMemberListByPartyPath($partyPath)
{
$options = [
'partypath' => $partyPath,
];
$result = $this->api('openapi/partyuser/list', 'POST', $options);
if (isset($result['errcode']) || isset($result['error'])) {
throw new QqExmailException($result['error'], $result['errcode']);
}
return $result;
}
/**
*
* @param string $email
* @return bool
* @throws QqExmailException
*/
public function getMemberStatus($email)
{
$options = [
'email' => $email,
];
$result = $this->api('openapi/user/check', 'POST', $options);
if (isset($result['errcode']) || isset($result['error']) || !isset($result['List'])) {
throw new QqExmailException($result['error'], $result['errcode']);
}
return intval($result['List'][0]['Type']);
}
/**
*
* @param string $email
* @return bool
*/
public function getMemberStatusAvailable($email)
{
$type = $this->getMemberStatus($email);
return $type === self::ACCOUNT_TYPE_AVAILABLE;
}
/**
*
* @param int $ver 0=all
* @return []
* @throws QqExmailException
*/
public function getMemberListByVersion($ver)
{
$options = [
'Ver' => $ver,
];
$result = $this->api('openapi/user/list', 'POST', $options);
if (isset($result['errcode']) || isset($result['error'])) {
throw new QqExmailException($result['error'], $result['errcode']);
}
return $result;
}
/**
*
* @param string $email
* @return int
* @throws QqExmailException
*/
public function getMailNewCount($email)
{
$options = [
'alias' => $email,
];
$result = $this->api('openapi/mail/newcount', 'POST', $options);
if (isset($result['errcode']) || isset($result['error']) || !isset($result['NewCount'])) {
throw new QqExmailException($result['error'], $result['errcode']);
}
return intval($result['NewCount']);
}
/**
*
* @param string $groupName 组名
* @param string $groupAdmin 组管理员(需要使用一个域中不存在的邮箱地址)
* @param string $status 组状态
* @param string $members 成员列表
* @return bool
* @throws QqExmailException
*/
public function addGroup($groupName, $groupAdmin, $status, $members)
{
$options = [
'group_name' => $groupName,
'group_admin' => $groupAdmin,
'status' => $status,
'members' => $members,
];
$result = $this->api('openapi/group/add', 'POST', $options);
if (isset($result['errcode']) || isset($result['error'])) {
throw new QqExmailException($result['error'], $result['errcode']);
}
return true;
}
/**
*
* @param string $groupAlias AdminEmail
* @return boolean
* @throws QqExmailException
*/
public function delGroup($groupAlias)
{
$options = [
'group_alias' => $groupAlias,
];
$result = $this->api('openapi/group/delete', 'POST', $options);
if (isset($result['errcode']) || isset($result['error'])) {
throw new QqExmailException($result['error'], $result['errcode']);
}
return true;
}
/**
*
* @param string $groupAlias AdminEmail
* @param string $members MemberEmail
* @return boolean
* @throws QqExmailException
*/
public function addGroupMember($groupAlias, $members)
{
$options = [
'group_alias' => $groupAlias,
'members' => $members,
];
$result = $this->api('openapi/group/addmember', 'POST', $options);
if (isset($result['errcode']) || isset($result['error'])) {
throw new QqExmailException($result['error'], $result['errcode']);
}
return true;
}
/**
*
* @param string $groupAlias
* @param string $members
* @return boolean
* @throws QqExmailException
*/
public function deleteGroupMember($groupAlias, $members)
{
$options = [
'group_alias' => $groupAlias,
'members' => $members,
];
$result = $this->api('openapi/group/deletemember', 'POST', $options);
if (isset($result['errcode']) || isset($result['error'])) {
throw new QqExmailException($result['error'], $result['errcode']);
}
var_dump($result);
return true;
}
/**
*
* @param int $ver
* @return []
* @throws QqExmailException
* @see PDF
*/
public function listen($ver)
{
$options = [
'Ver' => $ver,
];
$result = $this->api('openapi/listen', 'POST', $options);
if (isset($result['errcode']) || isset($result['error'])) {
throw new QqExmailException($result['error'], $result['errcode']);
}
return $result;
}
}