forked from pmsf/PMSF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
discord-callback.php
101 lines (95 loc) · 3.77 KB
/
discord-callback.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
<?php
require_once("config/config.php");
require_once("DiscordAuth.php");
if ($noDiscordLogin === false) {
try {
if (isset($_GET['code'])) {
$auth = new DiscordAuth();
$auth->handleAuthorizationResponse($_GET);
$user = json_decode($auth->get("/api/users/@me"));
$guilds = json_decode($auth->get("/api/users/@me/guilds"));
if (in_array($user->{'id'}, $userBlacklist)) {
header("Location: ./access-denied.php");
$granted = false;
} else {
if (in_array($user->{'id'}, $userWhitelist)) {
header("Location: .?login=true");
$granted = true;
} else {
foreach ($guilds as $guild) {
$uses = $guild->id;
$guildName = $guild->name;
if (in_array($uses, $serverBlacklist)) {
if ($logFailedLogin) {
logFailure($user->{'username'} . "#" . $user->{'discriminator'} . " has been blocked for being a member of " . $guildName . "\n");
}
header("Location: .?login=false");
die();
} else {
if (in_array($uses, $serverWhitelist)) {
header("Location: .?login=true");
$granted = true;
}
}
}
}
}
if ($granted !== true) {
header("Location: .?login=false");
die();
}
$count = $manualdb->count("users", [
"id" => $user->{'id'},
"login_system" => 'discord'
]);
if ($count === 0) {
if ($manualAccessLevel) {
$manualdb->insert("users", [
"id" => $user->{'id'},
"user" => $user->{'username'} . "#" . $user->{'discriminator'},
"expire_timestamp" => time(),
"login_system" => 'discord'
]);
} else {
$manualdb->insert("users", [
"id" => $user->{'id'},
"user" => $user->{'username'} . "#" . $user->{'discriminator'},
"expire_timestamp" => time() + 60 * 60 * 24 * 7,
"login_system" => 'discord'
]);
}
}
setcookie("LoginCookie", session_id(), time() + 60 * 60 * 24 * 7);
if ($manualAccessLevel) {
$manualdb->update("users", [
"session_id" => session_id(),
"user" => $user->{'username'} . "#" . $user->{'discriminator'}
], [
"id" => $user->{'id'},
"login_system" => 'discord'
]);
} else {
$manualdb->update("users", [
"session_id" => session_id(),
"expire_timestamp" => time() + 60 * 60 * 24 * 7,
"user" => $user->{'username'} . "#" . $user->{'discriminator'}
], [
"id" => $user->{'id'},
"login_system" => 'discord'
]);
}
}
die();
} catch (Exception $e) {
error_log("Unexpected error after Discord Auth!");
error_log($e);
header("Location: .?exception=yes");
}
} else {
header("Location: .");
}
function logFailure($logFailure)
{
global $logFailedLogin;
file_put_contents($logFailedLogin, $logFailure, FILE_APPEND);
}