-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdiscord_verification.php
More file actions
921 lines (795 loc) · 29.1 KB
/
Copy pathdiscord_verification.php
File metadata and controls
921 lines (795 loc) · 29.1 KB
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
<?php
/*
* WHMCS Discord Client Verifcation Module
* Copyright (C) 2025 26bz (https://26bz.online/)
* Licensed under GNU GPLv3 or later. See LICENSE file.
*/
if (!defined("WHMCS")) {
die("This file cannot be accessed directly");
}
use WHMCS\Authentication\CurrentUser;
use WHMCS\Database\Capsule;
require_once __DIR__ . '/admin.php';
if (!function_exists('discord_verification_config')) {
function discord_verification_config()
{
return [
'name' => 'Discord Client Verification',
'description' => 'Automatically verify WHMCS clients with Discord roles based on their account status. Clients with active products receive one role, while verified clients without active products receive a different role.',
'version' => '1.5.2',
'author' => '<a href="https://26bz.online/" target="_blank">26BZ</a>',
'language' => 'english',
'fields' => [
'client_id' => [
'FriendlyName' => 'Discord Client ID',
'Type' => 'text',
'Size' => '30',
'Description' => 'Your Discord Application Client ID',
'Default' => '',
],
'secret_id' => [
'FriendlyName' => 'Discord Client Secret',
'Type' => 'password',
'Size' => '30',
'Description' => 'Your Discord Application Client Secret',
'Default' => '',
],
'bot_token' => [
'FriendlyName' => 'Discord Bot Token',
'Type' => 'password',
'Size' => '30',
'Description' => 'Your Discord Bot Token (for role management)',
'Default' => '',
],
'guild_id' => [
'FriendlyName' => 'Discord Server ID',
'Type' => 'text',
'Size' => '25',
'Description' => 'Your Discord Server (Guild) ID',
'Default' => '',
],
'active_role_id' => [
'FriendlyName' => 'Active Client Role ID',
'Type' => 'text',
'Size' => '25',
'Description' => 'Discord Role ID for clients with active products',
'Default' => '',
],
'default_role_id' => [
'FriendlyName' => 'Default Role ID',
'Type' => 'text',
'Size' => '25',
'Description' => 'Discord role ID for verified clients without active products',
'Default' => '',
],
'enable_client_widget' => [
'FriendlyName' => 'Enable Client Widget',
'Type' => 'yesno',
'Default' => 'yes',
'Description' => 'Show Discord verification status widget in client area sidebar',
],
'force_verification' => [
'FriendlyName' => 'Force Discord Verification',
'Type' => 'yesno',
'Default' => 'no',
'Description' => 'Force clients to link their Discord account to access the client area (similar to email verification)',
],
'auto_join_guild' => [
'FriendlyName' => 'Auto Join Server',
'Type' => 'yesno',
'Default' => 'yes',
'Description' => 'Automatically add users to your Discord server when they verify (requires bot with MANAGE_GUILD permission)',
],
]
];
}
}
if (!function_exists('discord_verification_activate')) {
function discord_verification_activate()
{
try {
$existingField = Capsule::table('tblcustomfields')
->where('fieldname', 'discord')
->where('type', 'client')
->first();
if (!$existingField) {
Capsule::table('tblcustomfields')->insert([
'type' => 'client',
'fieldname' => 'discord',
'fieldtype' => 'text',
'description' => 'Discord User ID',
'fieldoptions' => '',
'regexpr' => '',
'adminonly' => 'on',
'required' => '',
'showorder' => 'on',
'showinvoice' => '',
'sortorder' => 0,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
]);
}
if (Capsule::schema()->hasTable('mod_discord_verification_backup')) {
$backedUpSettings = Capsule::table('mod_discord_verification_backup')->get();
if ($backedUpSettings->count() > 0) {
sleep(1);
foreach ($backedUpSettings as $setting) {
Capsule::table('tbladdonmodules')
->where('module', 'discord_verification')
->where('setting', $setting->setting)
->update(['value' => $setting->value]);
}
Capsule::table('mod_discord_verification_backup')->truncate();
logActivity('Discord Verification: Settings restored from backup during activation');
return [
'status' => 'success',
'description' => 'Discord Verification module activated successfully! Your previous settings have been restored.',
];
}
}
return [
'status' => 'success',
'description' => 'Discord Verification module activated successfully! The required custom field "discord" has been created. Configure your Discord application settings in the module configuration.',
];
} catch (\Exception $e) {
logActivity('Discord Verification: Error during activation - ' . $e->getMessage());
return [
'status' => 'error',
'description' => 'Unable to activate module: ' . $e->getMessage(),
];
}
}
}
if (!function_exists('discord_verification_deactivate')) {
function discord_verification_deactivate()
{
try {
$currentSettings = Capsule::table('tbladdonmodules')
->where('module', 'discord_verification')
->get();
if ($currentSettings->count() > 0) {
if (!Capsule::schema()->hasTable('mod_discord_verification_backup')) {
Capsule::schema()->create('mod_discord_verification_backup', function ($table) {
$table->increments('id');
$table->string('setting');
$table->text('value');
$table->timestamp('created_at')->useCurrent();
});
}
Capsule::table('mod_discord_verification_backup')->truncate();
foreach ($currentSettings as $setting) {
Capsule::table('mod_discord_verification_backup')->insert([
'setting' => $setting->setting,
'value' => $setting->value,
'created_at' => date('Y-m-d H:i:s')
]);
}
logActivity('Discord Verification: Settings backed up before deactivation');
}
return [
'status' => 'success',
'description' => 'Discord Verification module deactivated successfully. Settings have been backed up and will be restored on reactivation.',
];
} catch (\Exception $e) {
logActivity('Discord Verification: Error during deactivation - ' . $e->getMessage());
return [
'status' => 'error',
'description' => 'Error during deactivation: ' . $e->getMessage(),
];
}
}
}
if (!function_exists('discord_verification_upgrade')) {
function discord_verification_upgrade($vars)
{
$currentlyInstalledVersion = $vars['version'];
try {
logActivity("Discord Verification module upgraded from version {$currentlyInstalledVersion} to 1.5");
return [
'status' => 'success',
'description' => 'Discord Verification module upgraded successfully to version 1.5',
];
} catch (\Exception $e) {
return [
'status' => 'error',
'description' => 'Error during upgrade: ' . $e->getMessage(),
];
}
}
}
if (!function_exists('discord_verification_output')) {
function discord_verification_output($vars)
{
return discord_verification_admin_output($vars);
}
}
if (!function_exists('discord_verification_clientarea')) {
function discord_verification_clientarea($vars)
{
$clientId = getenv('DISCORD_CLIENT_ID') ?: $vars['client_id'];
$secretId = getenv('DISCORD_SECRET_ID') ?: $vars['secret_id'];
$botToken = getenv('DISCORD_BOT_TOKEN') ?: $vars['bot_token'];
$guildId = $vars['guild_id'];
$activeRoleId = $vars['active_role_id'];
$defaultRoleId = $vars['default_role_id'];
if (empty($clientId) || empty($secretId) || empty($botToken) || empty($guildId)) {
return [
'pagetitle' => 'Discord Verification',
'breadcrumb' => ['index.php?m=discord_verification' => 'Discord Verification'],
'templatefile' => 'verification',
'requirelogin' => true,
'forcessl' => true,
'vars' => [
'error' => true,
'message' => 'Service temporarily unavailable. Please contact support.',
],
];
}
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
$currentUser = new CurrentUser();
if (!$currentUser->isAuthenticatedUser()) {
return [
'pagetitle' => 'Discord Verification',
'breadcrumb' => ['index.php?m=discord_verification' => 'Discord Verification'],
'templatefile' => 'verification',
'requirelogin' => true,
'forcessl' => true,
'vars' => [
'error' => true,
'message' => 'You must be logged in to verify your Discord account.',
],
];
}
$client = $currentUser->client();
if (!$client) {
return [
'pagetitle' => 'Discord Verification',
'breadcrumb' => ['index.php?m=discord_verification' => 'Discord Verification'],
'templatefile' => 'verification',
'requirelogin' => true,
'forcessl' => true,
'vars' => [
'error' => true,
'message' => 'Unable to retrieve client information.',
],
];
}
$redirectUri = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http') .
'://' . $_SERVER['HTTP_HOST'] . '/index.php?m=discord_verification';
if (isset($_GET['action']) && $_GET['action'] === 'verify') {
$csrfToken = bin2hex(random_bytes(32));
$_SESSION['csrf_token'] = $csrfToken;
$config = getDiscordConfig();
$autoJoinGuild = isset($config['auto_join_guild']) && $config['auto_join_guild'] === 'on';
$scope = $autoJoinGuild ? 'identify email guilds.join' : 'identify email';
$authorizationUrl = 'https://discord.com/oauth2/authorize?' . http_build_query([
'response_type' => 'code',
'client_id' => $clientId,
'redirect_uri' => $redirectUri,
'scope' => $scope,
'state' => $csrfToken,
]);
header('Location: ' . $authorizationUrl);
exit();
}
if (isset($_GET['code'])) {
// Verify CSRF
if (!isset($_GET['state']) || !isset($_SESSION['csrf_token']) || $_GET['state'] !== $_SESSION['csrf_token']) {
return [
'pagetitle' => 'Discord Verification',
'breadcrumb' => ['index.php?m=discord_verification' => 'Discord Verification'],
'templatefile' => 'verification',
'requirelogin' => true,
'forcessl' => true,
'vars' => [
'error' => true,
'message' => 'Invalid security token. Please try again.',
],
];
}
try {
$tokenData = exchangeAuthorizationCodeForAccessToken($_GET['code'], $clientId, $secretId, $redirectUri);
$userInfo = getUserInfo($tokenData->access_token);
if (!isset($userInfo->id)) {
throw new Exception("VERIFICATION_FAILED:Unable to complete verification. Please try again.");
}
$existingClient = checkDuplicateDiscordAccount($userInfo->id, $client->id);
if ($existingClient) {
throw new Exception("DUPLICATE_ACCOUNT:This Discord account is already linked to another WHMCS account. Please contact support if you believe this is an error.");
}
$discordUsername = $userInfo->username;
if (isset($userInfo->discriminator) && $userInfo->discriminator !== '0') {
$discordUsername .= '#' . $userInfo->discriminator;
}
updateClientDiscordId($userInfo->id, $client->id, $discordUsername);
try {
$config = getDiscordConfig();
$autoJoinGuild = isset($config['auto_join_guild']) && $config['auto_join_guild'] === 'on';
if ($autoJoinGuild) {
try {
addUserToGuild($userInfo->id, $tokenData->access_token, $guildId, $botToken);
} catch (Exception $e) {
logActivity("Failed to add Discord user {$userInfo->id} to guild - " . $e->getMessage());
}
}
assignRoleToUser($userInfo->id, $client->id, $guildId, $activeRoleId, $defaultRoleId, $botToken);
logActivity("Discord successfully linked for Client ID: " . $client->id);
$avatarUrl = isset($userInfo->avatar) ?
"https://cdn.discordapp.com/avatars/{$userInfo->id}/{$userInfo->avatar}.png" :
"https://cdn.discordapp.com/embed/avatars/0.png";
$username = htmlspecialchars($userInfo->username);
$discriminator = isset($userInfo->discriminator) && $userInfo->discriminator !== '0' ?
"#{$userInfo->discriminator}" : '';
return [
'pagetitle' => 'Discord Verification',
'breadcrumb' => ['index.php?m=discord_verification' => 'Discord Verification'],
'templatefile' => 'verification',
'requirelogin' => true,
'forcessl' => true,
'vars' => [
'verified' => true,
'avatar' => $avatarUrl,
'username' => $username,
'discriminator' => $discriminator,
'message' => 'Successfully linked your Discord account!',
'modulelink' => $vars['modulelink'],
],
];
} catch (Exception $e) {
logActivity("Failed to assign Discord role for Client ID: " . $client->id . " - " . $e->getMessage());
$errorMessage = parseDiscordRoleError($e->getMessage());
return [
'pagetitle' => 'Discord Verification',
'breadcrumb' => ['index.php?m=discord_verification' => 'Discord Verification'],
'templatefile' => 'verification',
'requirelogin' => true,
'forcessl' => true,
'vars' => [
'error' => true,
'message' => $errorMessage,
],
];
}
} catch (Exception $e) {
logActivity("Discord linking error for Client ID: " . $client->id . " - " . $e->getMessage());
$errorMessage = parseDiscordError($e->getMessage());
return [
'pagetitle' => 'Discord Verification',
'breadcrumb' => ['index.php?m=discord_verification' => 'Discord Verification'],
'templatefile' => 'verification',
'requirelogin' => true,
'forcessl' => true,
'vars' => [
'error' => true,
'message' => $errorMessage,
],
];
}
}
$existingDiscord = getExistingDiscordInfo($client->id, $botToken);
if ($existingDiscord) {
return [
'pagetitle' => 'Discord Verification',
'breadcrumb' => ['index.php?m=discord_verification' => 'Discord Verification'],
'templatefile' => 'verification',
'requirelogin' => true,
'forcessl' => true,
'vars' => [
'verified' => true,
'avatar' => $existingDiscord['avatar'],
'username' => $existingDiscord['username'],
'discriminator' => $existingDiscord['discriminator'],
'message' => 'Your Discord account is linked',
'modulelink' => $vars['modulelink'],
],
];
} else {
$config = getDiscordConfig();
$forceVerification = isset($config['force_verification']) && $config['force_verification'] === 'on';
return [
'pagetitle' => 'Discord Verification',
'breadcrumb' => ['index.php?m=discord_verification' => 'Discord Verification'],
'templatefile' => 'verification',
'requirelogin' => true,
'forcessl' => true,
'vars' => [
'verified' => false,
'message' => 'Link your Discord account to your client area',
'modulelink' => $vars['modulelink'],
'force_verification' => $forceVerification,
],
];
}
}
function exchangeAuthorizationCodeForAccessToken($code, $client_id, $secret_id, $redirect_uri)
{
$ch = curl_init('https://discord.com/api/oauth2/token');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query([
'client_id' => $client_id,
'client_secret' => $secret_id,
'grant_type' => 'authorization_code',
'code' => $code,
'redirect_uri' => $redirect_uri
]),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => true
]);
$response = curl_exec($ch);
if ($response === false) {
throw new Exception('OAUTH_ERROR:Authentication failed. Please try again.');
}
$data = json_decode($response);
if (!isset($data->access_token)) {
throw new Exception('OAUTH_ERROR:Authentication failed. Please try again.');
}
curl_close($ch);
return $data;
}
function getUserInfo($accessToken)
{
$ch = curl_init('https://discord.com/api/users/@me');
curl_setopt_array($ch, [
CURLOPT_HTTPHEADER => ['Authorization: Bearer ' . $accessToken],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => true
]);
$response = curl_exec($ch);
if ($response === false) {
throw new Exception('USER_INFO_ERROR:Unable to retrieve account information. Please try again.');
}
$data = json_decode($response);
if (!isset($data->id)) {
throw new Exception('USER_INFO_ERROR:Unable to retrieve account information. Please try again.');
}
curl_close($ch);
return $data;
}
function updateClientDiscordId($discordId, $clientId, $discordUsername = null)
{
try {
$discordFieldId = Capsule::table('tblcustomfields')
->where('fieldname', 'discord')
->value('id');
if (!$discordFieldId) {
throw new Exception('SYSTEM_ERROR:Configuration error. Please contact support.');
}
// Store data as JSON if username is available otherwise just ID
$discordData = $discordUsername ?
json_encode(['id' => $discordId, 'username' => $discordUsername]) :
$discordId;
Capsule::table('tblcustomfieldsvalues')
->updateOrInsert(
[
'fieldid' => $discordFieldId,
'relid' => $clientId,
],
[
'value' => $discordData
]
);
return true;
} catch (Exception $e) {
throw new Exception('SYSTEM_ERROR:Unable to save verification data. Please contact support.');
}
}
function assignRoleToUser($userId, $clientId, $guildId, $activeRoleId, $defaultRoleId, $botToken)
{
$activeProducts = Capsule::table('tblhosting')
->where('userid', $clientId)
->where('domainstatus', 'Active')
->count();
$roleId = $activeProducts > 0 ? $activeRoleId : $defaultRoleId;
$requestData = "PUT /guilds/{$guildId}/members/$userId/roles/$roleId";
logActivity("Assigning role to Discord user $userId (Client ID: $clientId) - Active products: $activeProducts - Role ID: $roleId");
$url = "https://discord.com/api/guilds/{$guildId}/members/$userId/roles/$roleId";
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_HTTPHEADER => [
'Authorization: Bot ' . $botToken,
'Content-Type: application/json',
],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_TIMEOUT => 10
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
logModuleCall(
'discord_verification',
'assign_role',
$requestData,
$response,
$httpCode,
[$botToken] // Scrub
);
if ($response === false) {
throw new Exception('CURL_ERROR:Failed to connect to Discord API. Please check your internet connection and try again.');
}
if (!in_array($httpCode, [204, 200])) {
$errorData = json_decode($response, true);
switch ($httpCode) {
case 403:
if (isset($errorData['code']) && $errorData['code'] === 50013) {
throw new Exception('BOT_PERMISSIONS:Bot permissions issue - please contact support. The bot may not have permission to manage roles.');
}
throw new Exception('NOT_IN_GUILD:Please join our Discord server first before verifying your account.');
case 404:
throw new Exception('NOT_IN_GUILD:Please join our Discord server first before verifying your account.');
case 400:
throw new Exception('INVALID_REQUEST:Invalid request to Discord API. Please contact support.');
case 429:
throw new Exception('RATE_LIMITED:Too many requests. Please wait a moment and try again.');
case 500:
case 502:
case 503:
throw new Exception('DISCORD_DOWN:Discord API is currently unavailable. Please try again later.');
default:
throw new Exception('ROLE_ASSIGNMENT_FAILED:Role assignment failed - please try again or contact admin if the problem persists.');
}
}
return true;
}
function getDiscordConfig()
{
try {
$config = Capsule::table('tbladdonmodules')
->where('module', 'discord_verification')
->pluck('value', 'setting')
->toArray();
return [
'client_id' => getenv('DISCORD_CLIENT_ID') ?: ($config['client_id'] ?? ''),
'secret_id' => getenv('DISCORD_SECRET_ID') ?: ($config['secret_id'] ?? ''),
'bot_token' => getenv('DISCORD_BOT_TOKEN') ?: ($config['bot_token'] ?? ''),
'guild_id' => $config['guild_id'] ?? '',
'active_role_id' => $config['active_role_id'] ?? '',
'default_role_id' => $config['default_role_id'] ?? '',
'enable_client_widget' => $config['enable_client_widget'] ?? 'yes',
'force_verification' => $config['force_verification'] ?? 'no',
'auto_join_guild' => $config['auto_join_guild'] ?? 'yes',
];
} catch (Exception $e) {
return [
'client_id' => '',
'secret_id' => '',
'bot_token' => '',
'guild_id' => '',
'active_role_id' => '',
'default_role_id' => '',
'enable_client_widget' => 'yes',
'force_verification' => 'no',
'auto_join_guild' => 'yes',
];
}
}
function fetchDiscordUsername($discordId)
{
try {
$config = getDiscordConfig();
if (!$config['bot_token']) {
return null;
}
$url = "https://discord.com/api/v10/users/{$discordId}";
$headers = [
'Authorization: Bot ' . $config['bot_token'],
'Content-Type: application/json'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode === 200 && $response) {
$userData = json_decode($response, true);
if (isset($userData['username'])) {
$username = $userData['username'];
if (isset($userData['discriminator']) && $userData['discriminator'] !== '0') {
$username .= '#' . $userData['discriminator'];
}
return $username;
}
}
return null;
} catch (Exception $e) {
// Silently fail
return null;
}
}
function removeAllDiscordRoles($discordId, $guildId, $activeRoleId, $defaultRoleId, $botToken)
{
$rolesToRemove = array_filter([$activeRoleId, $defaultRoleId]);
foreach ($rolesToRemove as $roleId) {
try {
$url = "https://discord.com/api/v10/guilds/{$guildId}/members/{$discordId}/roles/{$roleId}";
$headers = [
'Authorization: Bot ' . $botToken,
'Content-Type: application/json'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode === 204 || $httpCode === 404) {
logActivity("Discord Verification: Role {$roleId} removed from user {$discordId}");
} else {
logActivity("Discord Verification: Failed to remove role {$roleId} from user {$discordId} - HTTP {$httpCode}");
}
} catch (Exception $e) {
logActivity("Discord Verification: Error removing role {$roleId} from user {$discordId} - " . $e->getMessage());
}
}
}
function getDiscordUserInfo($userId, $botToken)
{
$url = "https://discord.com/api/users/$userId";
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_HTTPHEADER => [
'Authorization: Bot ' . $botToken,
'Content-Type: application/json',
],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_TIMEOUT => 10
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($response === false) {
throw new Exception('API_ERROR:Unable to retrieve user information. Please contact support.');
}
if ($httpCode !== 200) {
throw new Exception('API_ERROR:Unable to retrieve user information. Please contact support.');
}
return json_decode($response, true);
}
function getExistingDiscordInfo($clientId, $botToken)
{
try {
$discordId = Capsule::table('tblcustomfields')
->join('tblcustomfieldsvalues', 'tblcustomfields.id', '=', 'tblcustomfieldsvalues.fieldid')
->where('tblcustomfields.fieldname', 'LIKE', '%discord%')
->where('tblcustomfieldsvalues.relid', $clientId)
->value('tblcustomfieldsvalues.value');
if ($discordId && is_numeric($discordId)) {
$userInfo = getDiscordUserInfo($discordId, $botToken);
if ($userInfo) {
return [
'id' => $discordId,
'username' => $userInfo['username'],
'discriminator' => isset($userInfo['discriminator']) && $userInfo['discriminator'] !== '0' ?
"#{$userInfo['discriminator']}" : '',
'avatar' => isset($userInfo['avatar']) ?
"https://cdn.discordapp.com/avatars/{$discordId}/{$userInfo['avatar']}.png" :
"https://cdn.discordapp.com/embed/avatars/0.png"
];
}
}
} catch (Exception $e) {
logActivity("Failed to fetch existing Discord info - Client ID: {$clientId} - " . $e->getMessage());
}
return null;
}
function checkDuplicateDiscordAccount($discordId, $currentClientId)
{
try {
$existingClient = Capsule::table('tblcustomfields')
->join('tblcustomfieldsvalues', 'tblcustomfields.id', '=', 'tblcustomfieldsvalues.fieldid')
->where('tblcustomfields.fieldname', 'discord')
->where('tblcustomfieldsvalues.value', $discordId)
->where('tblcustomfieldsvalues.relid', '!=', $currentClientId)
->value('tblcustomfieldsvalues.relid');
return $existingClient;
} catch (Exception $e) {
logActivity("Failed to check duplicate Discord account - Discord ID: {$discordId} - " . $e->getMessage());
return null;
}
}
function parseDiscordError($errorMessage)
{
if (strpos($errorMessage, 'DUPLICATE_ACCOUNT:') === 0) {
return substr($errorMessage, 17);
}
if (strpos($errorMessage, 'CURL_ERROR:') === 0) {
return substr($errorMessage, 11);
}
if (strpos($errorMessage, 'NOT_IN_GUILD:') === 0) {
return substr($errorMessage, 13);
}
if (strpos($errorMessage, 'BOT_PERMISSIONS:') === 0) {
return substr($errorMessage, 16);
}
if (strpos($errorMessage, 'INVALID_REQUEST:') === 0) {
return substr($errorMessage, 16);
}
if (strpos($errorMessage, 'RATE_LIMITED:') === 0) {
return substr($errorMessage, 13);
}
if (strpos($errorMessage, 'DISCORD_DOWN:') === 0) {
return substr($errorMessage, 13);
}
if (strpos($errorMessage, 'GUILD_JOIN_FAILED:') === 0) {
return substr($errorMessage, 17);
}
return 'An unexpected error occurred during Discord verification. Please try again or contact support if the problem persists.';
}
function parseDiscordRoleError($errorMessage)
{
if (strpos($errorMessage, 'NOT_IN_GUILD:') === 0) {
return substr($errorMessage, 13);
}
if (strpos($errorMessage, 'BOT_PERMISSIONS:') === 0) {
return substr($errorMessage, 16);
}
if (strpos($errorMessage, 'ROLE_ASSIGNMENT_FAILED:') === 0) {
return substr($errorMessage, 23);
}
if (strpos($errorMessage, 'RATE_LIMITED:') === 0) {
return substr($errorMessage, 13);
}
if (strpos($errorMessage, 'DISCORD_DOWN:') === 0) {
return substr($errorMessage, 13);
}
if (strpos($errorMessage, 'CURL_ERROR:') === 0) {
return substr($errorMessage, 11);
}
if (strpos($errorMessage, 'INVALID_REQUEST:') === 0) {
return substr($errorMessage, 16);
}
return 'Discord linked successfully, but role assignment failed. Please try again or contact admin if the problem persists.';
}
function addUserToGuild($userId, $accessToken, $guildId, $botToken)
{
$url = "https://discord.com/api/v10/guilds/{$guildId}/members/{$userId}";
$data = json_encode(['access_token' => $accessToken]);
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => $data,
CURLOPT_HTTPHEADER => [
'Authorization: Bot ' . $botToken,
'Content-Type: application/json',
],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_TIMEOUT => 10
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
logModuleCall(
'discord_verification',
'add_to_guild',
"PUT /guilds/{$guildId}/members/{$userId}",
$response,
$httpCode,
[$botToken, $accessToken]
);
if ($response === false) {
throw new Exception('CURL_ERROR:Failed to connect to Discord API.');
}
if (!in_array($httpCode, [201, 204])) {
if ($httpCode === 403) {
throw new Exception('NOT_IN_GUILD:Failed to add you to the Discord server.');
} else {
throw new Exception('GUILD_JOIN_FAILED:Failed to add you to the Discord server.');
}
}
return true;
}
}