forked from Zyahko/RealDeviceMap-RaidBillBoard
-
Notifications
You must be signed in to change notification settings - Fork 15
/
discord-callback.php
73 lines (63 loc) · 2.55 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
<?php
session_start();
include './vendor/autoload.php';
require_once './config.php';
require_once './includes/DiscordAuth.php';
require_once './includes/utils.php';
use RestCord\DiscordClient;
try {
if (isset($_GET['code'])) {
$auth = new DiscordAuth();
$auth->handleAuthorizationResponse($_GET);
$user = json_decode($auth->get("/api/v6/users/@me"));
$guilds = json_decode($auth->get("/api/v6/users/@me/guilds"));
$session_lifetime = 60;
$in_trusted = in_trusted_guild($config['discord']['guildIds'], $guilds);
if (!$in_trusted) {
die("You must join Discord server " . $config['discord']['inviteLink']);
}
$valid = false;
$roles = [];
if (!empty($config['discord']['botToken'])) {
$discord = new DiscordClient(['token' => $config['discord']['botToken']]); // Token is required
$count = count($config['discord']['guildIds']);
for ($i = 0; $i < $count; $i++) {
$member = $discord->guild->getGuildMember(['guild.id' => $config['discord']['guildIds'][$i], 'user.id' => (int)$user->id]);
if ($member === null)
continue;
$has_trusted_role = hasDiscordRole($member->roles, $config['discord']['roleIds']);
if (!$has_trusted_role)
continue;
if ($config['discord']['logUsers']) { //Debug
file_put_contents('./discord_users.txt', print_r($user, true), FILE_APPEND);
}
setcookie("LoginCookie", session_id(), time()+$session_lifetime);
array_push($roles, $member->roles);
$valid = true;
break;
}
if (!$has_trusted_role) {
die("You do not have the required permissions to access this page.");
}
}
if ($valid) {
$_SESSION['user'] = ["username" => $user->username, "roles" => $roles[0]];
$path = isset($_SESSION['redirect']) && !empty($_SESSION['redirect']) ? $_SESSION['redirect'] : ".";
header("Location: $path");
}
}
} catch (Exception $e) {
file_put_contents('error.log', $e->getMessage(), FILE_APPEND);
header("Location: ./discord-login.php");
}
function in_trusted_guild($trusted_guild_ids, $guilds) {
if (count($trusted_guild_ids) == 0) {
return true;
}
foreach ($guilds as $key=>$value) {
if (in_array($value->id, $trusted_guild_ids)) {
return true;
}
}
return false;
}