|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* The MIT License (MIT) |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) 2016 Robert Sardinia |
|
6
|
|
|
* |
|
7
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
9
|
|
|
* in the Software without restriction, including without limitation the rights |
|
10
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
11
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
12
|
|
|
* furnished to do so, subject to the following conditions: |
|
13
|
|
|
* |
|
14
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
15
|
|
|
* copies or substantial portions of the Software. |
|
16
|
|
|
* |
|
17
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
18
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
19
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
20
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
21
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
22
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
23
|
|
|
* SOFTWARE. |
|
24
|
|
|
*/ |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Class fileAuthCheck |
|
29
|
|
|
* @property int nextCheck |
|
30
|
|
|
*/ |
|
31
|
|
|
class authCheck |
|
|
|
|
|
|
32
|
|
|
{ |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var |
|
36
|
|
|
*/ |
|
37
|
|
|
private $active; |
|
38
|
|
|
private $config; |
|
39
|
|
|
private $discord; |
|
40
|
|
|
private $logger; |
|
41
|
|
|
private $db; |
|
42
|
|
|
private $dbUser; |
|
43
|
|
|
private $dbPass; |
|
44
|
|
|
private $dbName; |
|
45
|
|
|
private $guildID; |
|
46
|
|
|
private $exempt; |
|
47
|
|
|
private $corpTickers; |
|
48
|
|
|
private $nameEnforce; |
|
49
|
|
|
private $standingsBased; |
|
50
|
|
|
private $apiKey; |
|
51
|
|
|
private $authGroups; |
|
52
|
|
|
private $alertChannel; |
|
53
|
|
|
private $nameCheck; |
|
54
|
|
|
private $dbusers; |
|
55
|
|
|
private $characterCache; |
|
56
|
|
|
private $corpCache; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param $config |
|
60
|
|
|
* @param $discord |
|
61
|
|
|
* @param $logger |
|
62
|
|
|
*/ |
|
63
|
|
|
public function init($config, $discord, $logger) |
|
64
|
|
|
{ |
|
65
|
|
|
$this->active = true; |
|
66
|
|
|
$this->config = $config; |
|
67
|
|
|
$this->discord = $discord; |
|
68
|
|
|
$this->logger = $logger; |
|
69
|
|
|
$this->db = $config['database']['host']; |
|
70
|
|
|
$this->dbUser = $config['database']['user']; |
|
71
|
|
|
$this->dbPass = $config['database']['pass']; |
|
72
|
|
|
$this->dbName = $config['database']['database']; |
|
73
|
|
|
$this->guildID = $config['bot']['guild']; |
|
74
|
|
|
$this->exempt = $config['plugins']['auth']['exempt']; |
|
75
|
|
|
$this->corpTickers = $config['plugins']['auth']['corpTickers']; |
|
76
|
|
|
$this->nameEnforce = $config['plugins']['auth']['nameEnforce']; |
|
77
|
|
|
$this->standingsBased = $config['plugins']['auth']['standings']['enabled']; |
|
78
|
|
|
$this->apiKey = $config['eve']['apiKeys']; |
|
79
|
|
|
$this->authGroups = $config['plugins']['auth']['authGroups']; |
|
80
|
|
|
$this->alertChannel = $config['plugins']['auth']['alertChannel']; |
|
81
|
|
|
$this->nextCheck = 0; |
|
82
|
|
|
$this->characterCache = array(); |
|
83
|
|
|
$this->corpCache = array(); |
|
84
|
|
|
|
|
85
|
|
|
//Set name check to happen if corpTicker or nameEnforce is set |
|
86
|
|
|
if ($this->nameEnforce === 'true' || $this->corpTickers === 'true') { |
|
87
|
|
|
$this->nameCheck = true; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
//check if cache has been set |
|
91
|
|
|
$permsChecked = getPermCache('permsLastChecked'); |
|
92
|
|
|
$namesChecked = getPermCache('nextRename'); |
|
93
|
|
|
if ($namesChecked === NULL) { |
|
94
|
|
|
setPermCache('nextRename', time()); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
//if not set set for now (30 minutes from now for role removal) |
|
98
|
|
|
if ($permsChecked === NULL) { |
|
99
|
|
|
setPermCache('permsLastChecked', time() - 1); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
// If config is outdated |
|
103
|
|
|
if (null === $this->authGroups) { |
|
104
|
|
|
$msg = '**Auth Failure:** Please update the bots config to the latest version.'; |
|
105
|
|
|
queueMessage($msg, $this->alertChannel, $this->guildID); |
|
106
|
|
|
$this->logger->addInfo($msg); |
|
107
|
|
|
$this->active = false; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function tick() |
|
112
|
|
|
{ |
|
113
|
|
|
// What was the servers last reported state |
|
114
|
|
|
$lastStatus = getPermCache('serverState'); |
|
115
|
|
|
if ($this->active && $lastStatus === 'online') { |
|
116
|
|
|
$permsChecked = getPermCache('permsLastChecked'); |
|
117
|
|
|
$standingsChecked = getPermCache('nextStandingsCheck'); |
|
118
|
|
|
|
|
119
|
|
|
if ($permsChecked <= time()) { |
|
120
|
|
|
$this->logger->addInfo('AuthCheck: Checking for users who have left corp/alliance....'); |
|
121
|
|
|
$this->checkPermissions(); |
|
122
|
|
|
$this->logger->addInfo('AuthCheck: Corp/alliance check complete.'); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
if ($this->standingsBased === 'true' && $standingsChecked <= time()) { |
|
126
|
|
|
$this->logger->addInfo('AuthCheck: Updating Standings'); |
|
127
|
|
|
$this->standingsUpdate(); |
|
128
|
|
|
$this->logger->addInfo('AuthCheck: Standings Updated'); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
// remove user roles |
|
134
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
private function checkPermissions() |
|
137
|
|
|
{ |
|
138
|
|
|
$this->characterCache = array(); |
|
139
|
|
|
$this->corpCache = array(); |
|
140
|
|
|
// Load database users |
|
141
|
|
|
$dbh = new PDO("mysql:host={$this->db};dbname={$this->dbName}", $this->dbUser, $this->dbPass); |
|
142
|
|
|
$this->dbusers = array_column($dbh->query("SELECT discordID, characterID, eveName, role FROM authUsers where active='yes'")->fetchAll(), null, 'discordID'); |
|
143
|
|
|
if (!$this->dbusers) { |
|
144
|
|
|
return; |
|
145
|
|
|
} |
|
146
|
|
|
foreach ($this->discord->guilds->get('id', $this->guildID)->members as $member) { |
|
147
|
|
|
$discordNick = $member->nick ?: $member->user->username; |
|
148
|
|
|
if ($this->discord->id == $member->id || $member->getRolesAttribute()->isEmpty()) { |
|
149
|
|
|
continue; |
|
150
|
|
|
} |
|
151
|
|
|
$this->logger->addDebug("AuthCheck: Username: $discordNick"); |
|
152
|
|
|
try { |
|
153
|
|
|
if (!($this->isMemberInValidCorp($member) || $this->isMemberInValidAlliance($member) || $this->isMemberCorpOrAllianceContact($member))) { |
|
154
|
|
|
$this->deactivateMember($member); |
|
155
|
|
|
} |
|
156
|
|
|
if ($this->nameCheck) { |
|
157
|
|
|
$this->resetMemberNick($member); |
|
158
|
|
|
} |
|
159
|
|
|
} catch (Exception $e) { |
|
160
|
|
|
$this->logger->addError("AuthCheck: " . $e->getMessage()); |
|
161
|
|
|
if ($e->getMessage() == 'The datasource tranquility is temporarily unavailable') { |
|
162
|
|
|
return; |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
setPermCache('permsLastChecked', time() + 1800); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
private function isMemberInValidCorp($member) |
|
170
|
|
|
{ |
|
171
|
|
|
$corpArray = array_column($this->authGroups, 'corpID'); |
|
172
|
|
|
$discordID = $member->id; |
|
173
|
|
|
$return = false; |
|
174
|
|
|
if (isset($this->dbusers[$discordID])) { |
|
175
|
|
|
$character = $this->getCharacterDetails($this->dbusers[$discordID]['characterID']); |
|
176
|
|
|
$return = in_array($character['corporation_id'], $corpArray); |
|
177
|
|
|
} |
|
178
|
|
|
return $return; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
View Code Duplication |
private function getCharacterDetails($charID, $retries = 3) |
|
|
|
|
|
|
182
|
|
|
{ |
|
183
|
|
|
if (isset($this->characterCache[$charID])) { |
|
184
|
|
|
return $this->characterCache[$charID]; |
|
185
|
|
|
} |
|
186
|
|
|
$character = null; |
|
187
|
|
|
for ($i = 0; $i < $retries; $i++) { |
|
188
|
|
|
$character = characterDetails($charID); |
|
189
|
|
|
if (!is_null($character)) { |
|
190
|
|
|
break; |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
if (!$character || isset($character['error'])) { |
|
194
|
|
|
$this->logger->addInfo('AuthCheck: characterDetails lookup failed.'); |
|
195
|
|
|
$msg = isset($character['error']) ? $character['error'] : 'characterDetails lookup failed'; |
|
196
|
|
|
throw new Exception($msg); |
|
197
|
|
|
} |
|
198
|
|
|
$this->characterCache[$charID] = $character; |
|
199
|
|
|
return $character; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
private function isMemberInValidAlliance($member) |
|
203
|
|
|
{ |
|
204
|
|
|
$allianceArray = array_column($this->authGroups, 'allianceID'); |
|
205
|
|
|
$discordID = $member->id; |
|
206
|
|
|
$return = false; |
|
207
|
|
|
// get charID |
|
208
|
|
|
if (isset($this->dbusers[$discordID])) { |
|
209
|
|
|
$character = $this->getCharacterDetails($this->dbusers[$discordID]['characterID']); |
|
210
|
|
|
$corp = $this->getCorpDetails($character['corporation_id']); |
|
211
|
|
|
$return = isset($corp['alliance_id']) && in_array($corp['alliance_id'], $allianceArray); |
|
212
|
|
|
} |
|
213
|
|
|
return $return; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
View Code Duplication |
private function getCorpDetails($corpID, $retries = 3) |
|
|
|
|
|
|
217
|
|
|
{ |
|
218
|
|
|
if (isset($this->corpCache[$corpID])) { |
|
219
|
|
|
return $this->corpCache[$corpID]; |
|
220
|
|
|
} |
|
221
|
|
|
$corporationDetails = null; |
|
222
|
|
|
for ($i = 0; $i < $retries; $i++) { |
|
223
|
|
|
$corporationDetails = corpDetails($corpID); |
|
224
|
|
|
if (!is_null($corporationDetails)) { |
|
225
|
|
|
break; |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
if (!$corporationDetails || isset($corporationDetails['error'])) { |
|
229
|
|
|
$this->logger->addInfo('AuthCheck: corpDetails lookup failed.'); |
|
230
|
|
|
$msg = isset($corporationDetails['error']) ? $corporationDetails['error'] : 'corpDetails lookup failed'; |
|
231
|
|
|
throw new Exception($msg); |
|
232
|
|
|
} |
|
233
|
|
|
$this->corpCache[$corpID] = $corporationDetails; |
|
234
|
|
|
return $corporationDetails; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
private function isMemberCorpOrAllianceContact($member) |
|
238
|
|
|
{ |
|
239
|
|
|
$return = false; |
|
240
|
|
|
$discordID = $member->id; |
|
241
|
|
|
if (isset($this->dbusers[$discordID])) { |
|
242
|
|
|
$role = $this->dbusers[$discordID]['role']; |
|
243
|
|
|
if ($role === 'blue' || 'neut' || 'red') { |
|
244
|
|
|
$character = $this->getCharacterDetails($this->dbusers[$discordID]['characterID']); |
|
245
|
|
|
$corporationDetails = $this->getCorpDetails($character['corporation_id']); |
|
246
|
|
|
$allianceContacts = getContacts($corporationDetails['alliance_id']); |
|
|
|
|
|
|
247
|
|
|
$corpContacts = getContacts($character['corporation_id']); |
|
|
|
|
|
|
248
|
|
|
if ($role === 'blue' && ((int)$allianceContacts['standing'] === 5 || 10 || (int)$corpContacts['standing'] === 5 || 10)) { |
|
249
|
|
|
$return = true; |
|
250
|
|
|
} |
|
251
|
|
|
if ($role === 'red' && ((int)$allianceContacts['standing'] === -5 || -10 || (int)$corpContacts['standing'] === -5 || -10)) { |
|
252
|
|
|
$return = true; |
|
253
|
|
|
} |
|
254
|
|
|
if ($role === 'neut' && ((int)$allianceContacts['standing'] === 0 || (int)$corpContacts['standing'] === 0 || (@(int)$allianceContacts['standings'] === null || '' && @(int)$corpContacts['standings'] === null || ''))) { |
|
255
|
|
|
$return = true; |
|
256
|
|
|
} |
|
257
|
|
|
} |
|
258
|
|
|
} |
|
259
|
|
|
return $return; |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
private function deactivateMember($member) |
|
263
|
|
|
{ |
|
264
|
|
|
$discordID = $member->id; |
|
265
|
|
|
$discordNick = $member->nick ?: $member->user->username; |
|
266
|
|
|
$this->logger->addInfo("AuthCheck: User [$discordNick] not found in database."); |
|
267
|
|
|
$dbh = new PDO("mysql:host={$this->db};dbname={$this->dbName}", $this->dbUser, $this->dbPass); |
|
268
|
|
|
$sql = "UPDATE authUsers SET active='no' WHERE discordID='$discordID'"; |
|
269
|
|
|
$dbh->query($sql); |
|
270
|
|
|
unset($this->dbusers[$member->id]); |
|
271
|
|
|
$this->removeRoles($member); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
private function removeRoles($member) |
|
275
|
|
|
{ |
|
276
|
|
|
$discordNick = $member->nick ?: $member->user->username; |
|
277
|
|
|
$roleRemoved = false; |
|
278
|
|
|
$guild = $this->discord->guilds->get('id', $this->guildID); |
|
279
|
|
|
if (!is_null($member->roles)) { |
|
280
|
|
|
if (!isset($this->dbusers[$member->id])) { |
|
281
|
|
|
foreach ($member->roles as $role) { |
|
282
|
|
|
if (!in_array($role->name, $this->exempt, true)) { |
|
283
|
|
|
$roleRemoved = true; |
|
284
|
|
|
$member->removeRole($role); |
|
285
|
|
|
$guild->members->save($member); |
|
286
|
|
|
break; //temp to see if an issue lies in removing multiple roles from the same person too quickly |
|
287
|
|
|
} |
|
288
|
|
|
} |
|
289
|
|
|
} |
|
290
|
|
|
if ($roleRemoved) { |
|
291
|
|
|
$this->logger->addInfo("AuthCheck: Roles removed from $discordNick"); |
|
292
|
|
|
$msg = "Discord roles removed from $discordNick"; |
|
293
|
|
|
queueMessage($msg, $this->alertChannel, $this->guildID); |
|
294
|
|
|
} |
|
295
|
|
|
} |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
/** |
|
299
|
|
|
* @return null |
|
300
|
|
|
*/ |
|
301
|
|
|
|
|
302
|
|
|
//Check user corp/alliance affiliation |
|
303
|
|
|
//Remove members who have roles but never authed |
|
304
|
|
|
private function resetMemberNick($member) |
|
305
|
|
|
{ |
|
306
|
|
|
$discordID = $member->id; |
|
307
|
|
|
$discordNick = $member->nick ?: $member->user->username; |
|
308
|
|
|
if (!isset($this->dbusers[$discordID])) { |
|
309
|
|
|
return false; |
|
310
|
|
|
} |
|
311
|
|
|
$character = $this->getCharacterDetails($this->dbusers[$discordID]['characterID']); |
|
312
|
|
|
$corpTicker = ''; |
|
313
|
|
|
if ($this->corpTickers === 'true') { |
|
314
|
|
|
$corporationDetails = $this->getCorpDetails($character['corporation_id']); |
|
315
|
|
|
if ($corporationDetails && isset($corporationDetails['ticker']) && $corporationDetails['ticker'] !== 'U') { |
|
316
|
|
|
$corpTicker = "[{$corporationDetails['ticker']}] "; |
|
317
|
|
|
} |
|
318
|
|
|
} |
|
319
|
|
|
if ($this->nameEnforce) { |
|
320
|
|
|
$newNick = $corpTicker . $this->dbusers[$discordID]['eveName']; |
|
321
|
|
|
} else { |
|
322
|
|
|
$newNick = $corpTicker . $discordNick; |
|
323
|
|
|
} |
|
324
|
|
|
if ($newNick !== $discordNick) { |
|
325
|
|
|
queueRename($discordID, $newNick, $this->guildID); |
|
326
|
|
|
} |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
|
|
private function standingsUpdate() |
|
330
|
|
|
{ |
|
331
|
|
|
foreach ($this->apiKey as $apiKey) { |
|
332
|
|
|
if ((string)$apiKey['keyID'] === (string)$this->config['plugins']['auth']['standings']['apiKey']) { |
|
333
|
|
|
$url = "https://api.eveonline.com/char/ContactList.xml.aspx?keyID={$apiKey['keyID']}&vCode={$apiKey['vCode']}&characterID={$apiKey['characterID']}"; |
|
334
|
|
|
$xml = makeApiRequest($url); |
|
335
|
|
|
if (empty($xml)) { |
|
336
|
|
|
return null; |
|
337
|
|
|
} |
|
338
|
|
|
foreach ($xml->result->rowset as $contactType) { |
|
339
|
|
|
if ((string)$contactType->attributes()->name === 'corporateContactList' || 'allianceContactList') { |
|
340
|
|
|
foreach ($contactType->row as $contact) { |
|
341
|
|
|
if (null !== $contact['contactID'] && $contact['contactName'] && $contact['standing']) { |
|
342
|
|
|
addContactInfo($contact['contactID'], $contact['contactName'], $contact['standing']); |
|
343
|
|
|
} |
|
344
|
|
|
} |
|
345
|
|
|
} |
|
346
|
|
|
} |
|
347
|
|
|
} |
|
348
|
|
|
} |
|
349
|
|
|
$nextCheck = time() + 86400; |
|
350
|
|
|
setPermCache('nextStandingsCheck', $nextCheck); |
|
351
|
|
|
} |
|
352
|
|
|
} |
|
353
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.