-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
122 lines (89 loc) · 3.36 KB
/
index.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
<?php
require __DIR__ . '/config.php';
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/discordHelperClass.php';
session_start();
#########################
#testing OAuth user stuff
#########################
$provider = new \Wohali\OAuth2\Client\Provider\Discord([
'clientId' => CLIENT_ID,
'clientSecret' => CLIENT_SECRET,
'redirectUri' => REDIRECT_URI
]);
$options = [
'state' => 'OPTIONAL_CUSTOM_CONFIGURED_STATE',
'scope' => ['identify'] // array or string
];
if (!isset($_GET['code'])) {
// Step 1. Get authorization code
$authUrl = $provider->getAuthorizationUrl($options);
$_SESSION['oauth2state'] = $provider->getState();
header('Location: ' . $authUrl);
// Check given state against previously stored one to mitigate CSRF attack
} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {
unset($_SESSION['oauth2state']);
exit('Invalid state');
} else {
// Step 2. Get an access token using the provided authorization code
try
{
$token = $provider->getAccessToken('authorization_code', [
'code' => $_GET['code']
]);
}
catch (Exception $e)
{
exit('Invalid authorization code.');
}
// testing
#echo '<h2>Token details:</h2>';
#echo 'Token: ' . $token->getToken() . "<br/>";
#echo 'Refresh token: ' . $token->getRefreshToken() . "<br/>";
#echo 'Expires: ' . $token->getExpires() . " - ";
#echo ($token->hasExpired() ? 'expired' : 'not expired') . "<br/>";
try {
#set this to see their guilds instead of user info
#$provider->setResourceUrl("/users/@me/guilds");
$user = $provider->getResourceOwner($token);
#echo '<h2>Resource owner details:</h2>';
#printf('Hello %s#%s!<br/><br/>', $user->getUsername(), $user->getDiscriminator());
#var_export($user->toArray());
} catch (Exception $e) {
// Failed to get user details
exit('Failed to get user details from Discord.');
}
}
###################
#testing bot stuff
###################
echo "<html>";
$discordHelperClass = new discordHelperClass();
use RestCord\DiscordClient;
$discord = new DiscordClient(['token' => BOT_TOKEN]); // Token is required
#get all the roles in the guild
$roles_array = $discord->guild->getGuildRoles(['guild.id' => intval(RCHAIN_GUILD_ID)]);
#Find the required role name, and get its id
$role_id_required = $discordHelperClass->getRoleIdFromString($roles_array, DISCORD_COOP_ROLE);
#Get a list of members, along with their role id's
$guild_members = $discord->guild->listGuildMembers(['guild.id' => intval(RCHAIN_GUILD_ID), 'limit' => 1000]);
#get the roles of the authorized member/user
if(!$user_roles = $discordHelperClass->getRolesOfUser($guild_members, $user->getUsername(), $user->getDiscriminator()))
{
echo "That user is not in this guild";
}
echo "<br>role id's that the authenticated user has<br>";
print_r($user_roles);
echo "<br>required role id = ".$role_id_required . "<br>";
#now check the member has the required role
$is_user_coop_member = $discordHelperClass->checkIfUserHasRoleId($user_roles, $role_id_required);
if($is_user_coop_member == true)
{
echo "user is valid";
}
else
{
echo "user is invalid";
}
echo "</html>";
?>