This repository has been archived by the owner on Dec 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
cron.php
434 lines (352 loc) · 11.6 KB
/
cron.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
<?php
define('BASEDIR', __DIR__);
ini_set('display_errors', 1);
error_reporting(E_ALL);
require_once BASEDIR . '/config/config.php';
require_once BASEDIR . '/vendor/autoload.php';
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;
use RestCord\DiscordClient;
$log = new Logger('DScan');
$log->pushHandler(new RotatingFileHandler(__DIR__ . '/log/KeepstarCron.log', Logger::NOTICE));
$restcord = new DiscordClient([
'token' => $config['discord']['botToken']
]);
// Loading our own libs
foreach(glob(BASEDIR . '/libraries/*.php') as $lib) {
require_once $lib;
}
// Start Auth
$log->notice('AUTHCHECK INITIATED');
// Make sure bots nick is set
if(isset($config['discord']['botNick'])) {
/**
* Since the restcord library changes this all the damn time,
* we have to add a workaround ...
*/
try {
$restcord->guild->modifyCurrentUserNick([
'guild.id' => (int) $config['discord']['guildId'],
'nick' => $config['discord']['botNick']
]);
} catch(Exception $e) {
$restcord->guild->modifyCurrentUsersNick([
'guild.id' => (int) $config['discord']['guildId'],
'nick' => $config['discord']['botNick']
]);
}
}
// Ensure DB Is Created
createAuthDb();
// get authed users
$users = getUsers();
// get TQ server status
$status = serverStatus();
// Downtime probably ...
if(!$status || $status['players'] === null || (int) $status['players'] < 100) {
die();
}
if((int) $config['discord']['logChannel'] !== 0) {
$restcord->channel->createMessage([
'channel.id' => (int) $config['discord']['logChannel'],
'content' => 'Starting cron job to check member access and roles ...'
]);
}
// get discord members
$members = $restcord->guild->listGuildMembers([
'guild.id' => $config['discord']['guildId'],
'limit' => 1000
]);
// get discord roles
$roles = $restcord->guild->getGuildRoles([
'guild.id' => $config['discord']['guildId']
]);
// get discord server informations
$currentGuild = $restcord->guild->getGuild([
'guild.id' => (int) $config['discord']['guildId']
]);
/**
* Walk through the users that are registered in the auth database
* 5 at a time, than wait 10 seconds to not run into a rate limit
*/
foreach(array_chunk($users, 5, true) as $userSet) {
foreach($userSet as $user) {
$characterID = $user['characterID'];
$characterData = characterDetails($characterID);
$discordID = (int) $user['discordID']; // this has to be casted to int
$type = json_decode($user['groups'], true);
$id = $user['id'];
$corporationID = $characterData['corporation_id'];
$corporationData = corporationDetails($corporationID);
if(!isset($characterData['alliance_id'])) {
$allianceID = 1;
$allianceTicker = null;
} else {
$allianceID = $characterData['alliance_id'];
$allianceData = allianceDetails($allianceID);
$allianceTicker = $allianceData['ticker'];
}
$eveName = $characterData['name'];
$exists = false;
foreach($members as $member) {
if($member->user->id === $discordID) {
$exists = true;
break;
}
}
// Additional ESI Check
if(!(int) $characterData['corporation_id'] || (int) $characterData['corporation_id'] === null) {
continue;
}
if($exists === false) {
$log->notice("$eveName has been removed from the database as they are no longer a member of the server.");
deleteUser($id);
continue;
}
/**
* Set EVE Name and Corp Ticker
* Server owner will not be touched
*/
// To keep compatible with older config files
if(!isset($config['discord']['addCorpTicker'])) {
$config['discord']['addCorpTicker'] = $config['discord']['addTicker'];
}
if(($config['discord']['enforceInGameName'] || $config['discord']['addCorpTicker']) && (int) $currentGuild->owner_id !== (int) $discordID) {
if($config['discord']['enforceInGameName'] && $config['discord']['addCorpTicker']) {
if(!empty($corporationData['ticker'])) {
$newNick = '[' . $corporationData['ticker'] . '] ' . $eveName;
if(isset($config['discord']['addAllianceTicker']) && $config['discord']['addAllianceTicker'] === true && !is_null($allianceTicker)) {
$newNick = $allianceTicker . ' [' . $corporationData['ticker'] . '] ' . $eveName;
}
if (strlen($newNick) >= 32) {
$newNick = mb_strimwidth($newNick, 0, 32);
}
$restcord->guild->modifyGuildMember([
'guild.id' => (int) $config['discord']['guildId'],
'user.id' => (int) $discordID,
'nick' => $newNick
]);
}
} else if(!$config['discord']['enforceInGameName'] && $config['discord']['addCorpTicker']) {
$memberDetails = $restcord->guild->getGuildMember([
'guild.id' => (int) $config['discord']['guildId'],
'user.id' => (int) $discordId
]);
if($memberDetails->nick) {
$searchstring = '[' . $corporationData['ticker'] . ']';
if(isset($config['discord']['addAllianceTicker']) && $config['discord']['addAllianceTicker'] === true && !is_null($allianceTicker)) {
$searchstring = $allianceTicker . ' [' . $corporationData['ticker'] . ']';
}
$discordNick = str_replace($searchstring, '', $memberDetails->nick);
$cleanNick = trim($discordNick);
$newNick = '[' . $corporationData['ticker'] . '] ' . $cleanNick;
} else {
$newNick = '[' . $corporationData['ticker'] . '] ' . trim($memberDetails->user->username);
}
if (strlen($newNick) >= 32) {
$newNick = mb_strimwidth($newNick, 0, 32);
}
$restcord->guild->modifyGuildMember([
'guild.id' => (int) $config['discord']['guildId'],
'user.id' => (int) $discordId,
'nick' => $newNick
]);
} else {
if (strlen($eveName) >= 32) {
$eveName = mb_strimwidth($eveName, 0, 32);
}
$restcord->guild->modifyGuildMember([
'guild.id' => (int) $config['discord']['guildId'],
'user.id' => (int) $discordID,
'nick' => $eveName
]);
}
}
/**
* Modify user roles
*
* @todo Check if a user already has a role
*/
$access = [];
foreach($config['groups'] as $authGroup) {
if(is_array($authGroup['id'])) {
$id = $authGroup['id'];
} else {
$id = [];
$id[] = $authGroup['id'];
}
$role = null;
// General "Authenticated" Role
if(in_array('1234', $id)) {
foreach($roles as $role) {
if($role->name == $authGroup['role']) {
break;
}
}
$restcord->guild->addGuildMemberRole([
'guild.id' => (int) $config['discord']['guildId'],
'user.id' => (int) $discordID,
'role.id' => (int) $role->id
]);
$access[] = 'character';
continue;
}
// Authentication by characterID
if(in_array($characterID, $id)) {
foreach($roles as $role) {
if($role->name == $authGroup['role']) {
break;
}
}
$restcord->guild->addGuildMemberRole([
'guild.id' => (int) $config['discord']['guildId'],
'user.id' => (int) $discordID,
'role.id' => (int) $role->id
]);
$access[] = 'character';
continue;
}
// Autnetification by allianceID
if(in_array($allianceID, $id)) {
foreach($roles as $role) {
if($role->name == $authGroup['role']) {
break;
}
}
$restcord->guild->addGuildMemberRole([
'guild.id' => (int) $config['discord']['guildId'],
'user.id' => (int) $discordID,
'role.id' => (int) $role->id
]);
$access[] = 'alliance';
continue;
}
// Authentification by corporationID
if(in_array($corporationID, $id)) {
foreach($roles as $role) {
if($role->name == $authGroup['role']) {
break;
}
}
$restcord->guild->addGuildMemberRole([
'guild.id' => (int) $config['discord']['guildId'],
'user.id' => (int) $discordID,
'role.id' => (int) $role->id
]);
$access[] = 'corp';
continue;
}
// This rate limit is annoying -.-
usleep(1000000);
}
// Make the json access list
$accessList = json_encode($access);
// Insert it all into the db
insertUser($characterID, (int) $discordID, $accessList);
/**
* Removing roles in case
*/
try {
$removeTheseRoles = [];
$removeTheseRolesName = [];
foreach($config['groups'] as $authGroup) {
if(is_array($authGroup['id'])) {
$id = $authGroup['id'];
} else {
$id = [];
$id[] = $authGroup['id'];
}
foreach($roles as $role) {
if($role->name === $authGroup['role']) {
if(((isset($characterData['corporation_id']) && !in_array($characterData['corporation_id'], $id)) && ((isset($characterData['alliance_id']) && !in_array($characterData['alliance_id'], $id)) || !isset($characterData['alliance_id'])) && !in_array($characterID, $id) && !in_array('1234', $id)) && in_array($role->id, $member->roles)) {
$removeTheseRoles[] = (int) $role->id;
if((int) $config['discord']['logChannel'] !== 0) {
$removeTheseRolesName[] = $role->name;
}
$log->notice($eveName . ' has been removed from the role ' . $role->name);
continue;
}
if(in_array($role->id, $removeTheseRoles, true)) {
unset($removeTheseRoles[array_search($role->id, $removeTheseRoles, true)]);
}
}
}
}
} catch(Exception $e) {
// Check if we're being rate limited
if(strpos($error, 'rate limited') !== false) {
break;
}
$log->error('ERROR: ' . $error);
}
if(count($removeTheseRoles) > 0) {
foreach($removeTheseRoles as $removeRole) {
try {
$restcord->guild->removeGuildMemberRole([
'guild.id' => (int) $config['discord']['guildId'],
'user.id' => (int) $discordID,
'role.id' => (int) $removeRole
]);
} catch(Exception $e) {
$error = $e->getMessage();
// Check if error is user left server and if so remove them
if(strpos($error, '10007') !== false) {
deleteUser($id);
continue 2;
}
// Check if we're being rate limited
if(strpos($error, 'rate limited') !== false) {
break 2;
}
$log->error('ERROR: ' . $error);
}
}
if((int) $config['discord']['logChannel'] !== 0) {
$removedRoles = implode(', ', $removeTheseRolesName);
$restcord->channel->createMessage([
'channel.id' => (int) $config['discord']['logChannel'],
'content' => $eveName . ' has been removed from the following roles: ' . $removedRoles
]);
}
if(!isset($config['discord']['removeUser'])) {
$config['discord']['removeUser'] = false;
}
if($config['discord']['removeUser'] === true) {
$restcord->guild->removeGuildMember([
'guild.id' => (int) $config['discord']['guildId'],
'user.id' => (int) $discordID
]);
}
if(isset($config['discord']['removedRole']) && $config['discord']['removedRole'] !== false) {
foreach ($roles as $role) {
if ($role->name == $config['discord']['removedRole']) {
break;
}
}
$restcord->guild->addGuildMemberRole([
'guild.id' => (int)$config['discord']['guildId'],
'user.id' => (int)$_SESSION['user_id'],
'role.id' => (int)$role->id
]);
}
}
if(count($type) === 0) {
$log->notice("2 $type");
deleteUser($id);
}
} // END DB User Check
// Don't run into a rate limit, just wait 10 seconds
usleep(10000000);
} // END Auth DB User Check
/**
* @todo Check for users on the server that are NOT ni the auth DB
* and remove all roles they might have. Just to be absolutely sure.
* Don't touch the bot here!
*/
if((int) $config['discord']['logChannel'] !== 0) {
$restcord->channel->createMessage([
'channel.id' => (int) $config['discord']['logChannel'],
'content' => 'Finished cron job'
]);
}
$log->notice('AUTHCHECK FINISHED');