|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sovereign\Plugins\onMessage; |
|
4
|
|
|
|
|
5
|
|
|
use Discord\Discord; |
|
6
|
|
|
use Discord\Parts\Channel\Message; |
|
7
|
|
|
use Discord\Parts\Guild\Role; |
|
8
|
|
|
use Discord\Parts\User\Member; |
|
9
|
|
|
use Monolog\Logger; |
|
10
|
|
|
use Sovereign\Lib\Config; |
|
|
|
|
|
|
11
|
|
|
use Sovereign\Lib\cURL; |
|
12
|
|
|
use Sovereign\Lib\Db; |
|
13
|
|
|
use Sovereign\Lib\Permissions; |
|
14
|
|
|
use Sovereign\Lib\ServerConfig; |
|
15
|
|
|
use Sovereign\Lib\Settings; |
|
16
|
|
|
use Sovereign\Lib\Users; |
|
17
|
|
|
|
|
18
|
|
|
class auth extends \Threaded implements \Collectable |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var Message |
|
22
|
|
|
*/ |
|
23
|
|
|
private $message; |
|
24
|
|
|
/** |
|
25
|
|
|
* @var Discord |
|
26
|
|
|
*/ |
|
27
|
|
|
private $discord; |
|
28
|
|
|
/** |
|
29
|
|
|
* @var Logger |
|
30
|
|
|
*/ |
|
31
|
|
|
private $log; |
|
32
|
|
|
/** |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
private $channelConfig; |
|
36
|
|
|
/** |
|
37
|
|
|
* @var Config |
|
38
|
|
|
*/ |
|
39
|
|
|
private $config; |
|
40
|
|
|
/** |
|
41
|
|
|
* @var Db |
|
42
|
|
|
*/ |
|
43
|
|
|
private $db; |
|
44
|
|
|
/** |
|
45
|
|
|
* @var cURL |
|
46
|
|
|
*/ |
|
47
|
|
|
private $curl; |
|
48
|
|
|
/** |
|
49
|
|
|
* @var Settings |
|
50
|
|
|
*/ |
|
51
|
|
|
private $settings; |
|
52
|
|
|
/** |
|
53
|
|
|
* @var Permissions |
|
54
|
|
|
*/ |
|
55
|
|
|
private $permissions; |
|
56
|
|
|
/** |
|
57
|
|
|
* @var ServerConfig |
|
58
|
|
|
*/ |
|
59
|
|
|
private $serverConfig; |
|
60
|
|
|
/** |
|
61
|
|
|
* @var Users |
|
62
|
|
|
*/ |
|
63
|
|
|
private $users; |
|
64
|
|
|
/** |
|
65
|
|
|
* @var array |
|
66
|
|
|
*/ |
|
67
|
|
|
private $extras; |
|
68
|
|
|
|
|
69
|
|
View Code Duplication |
public function __construct($message, $discord, $channelConfig, $log, $config, $db, $curl, $settings, $permissions, $serverConfig, $users, $extras) |
|
|
|
|
|
|
70
|
|
|
{ |
|
71
|
|
|
$this->message = $message; |
|
72
|
|
|
$this->discord = $discord; |
|
73
|
|
|
$this->channelConfig = $channelConfig; |
|
74
|
|
|
$this->log = $log; |
|
75
|
|
|
$this->config = $config; |
|
76
|
|
|
$this->db = $db; |
|
77
|
|
|
$this->curl = $curl; |
|
78
|
|
|
$this->settings = $settings; |
|
79
|
|
|
$this->permissions = $permissions; |
|
80
|
|
|
$this->serverConfig = $serverConfig; |
|
81
|
|
|
$this->users = $users; |
|
82
|
|
|
$this->extras = $extras; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function run() |
|
86
|
|
|
{ |
|
87
|
|
|
$explode = explode(" ", $this->message->content); |
|
88
|
|
|
$authString = isset($explode[1]) ? $explode[1] : ""; |
|
89
|
|
|
|
|
90
|
|
|
if ($this->message->getChannelAttribute()->is_private) { |
|
91
|
|
|
return $this->message->reply("**Error** You are trying to send your auth token in private. This won't work because i need the guild information, which i can only get if you post it in a public channel on the server you want to get authed on."); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$authData = $this->db->queryRow("SELECT * FROM authRegs WHERE authString = :authString AND active = 1", array(":authString" => $authString)); |
|
95
|
|
|
|
|
96
|
|
|
if ($authData) { |
|
|
|
|
|
|
97
|
|
|
$groups = json_decode($authData["groups"], true); |
|
98
|
|
|
$characterID = $authData["characterID"]; |
|
99
|
|
|
/** @var Role $roles */ |
|
100
|
|
|
$roles = $this->message->getFullChannelAttribute()->getGuildAttribute()->getRolesAttribute(); |
|
101
|
|
|
/** @var Member $member */ |
|
102
|
|
|
$member = $this->message->getFullChannelAttribute()->getGuildAttribute()->getMembersAttribute()->get("id", $this->message->author->id); |
|
103
|
|
|
$username = $this->message->author->username; |
|
104
|
|
|
$discordID = $this->message->author->id; |
|
105
|
|
|
|
|
106
|
|
|
// @todo Force ingame name |
|
107
|
|
|
$characterName = json_decode($this->curl->get("https://evedata.xyz/api/character/shortinformation/{$characterID}/"))->characterName; |
|
|
|
|
|
|
108
|
|
|
// Doesn't work yet, but it should be something like $member->nick($characterName); |
|
109
|
|
|
//$member->user->setAttribute("username", $characterName); |
|
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
/** @var Role $role */ |
|
112
|
|
|
foreach ($roles as $role) { |
|
113
|
|
|
$roleName = $role->name; |
|
114
|
|
|
if (in_array($roleName, $groups)) { |
|
115
|
|
|
$member->addRole($role); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
// Save the member object, so all the roles are set |
|
120
|
|
|
$member->save(); |
|
121
|
|
|
|
|
122
|
|
|
$this->db->execute("UPDATE authRegs SET discordID = :discordID, active = 0 WHERE authString = :authString", array(":discordID" => $discordID, ":authString" => $authString)); |
|
123
|
|
|
$this->log->addInfo("{$username} authenticated in {$this->message->getChannelAttribute()->name} on {$this->message->getChannelAttribute()->getGuildAttribute()->name}"); |
|
124
|
|
|
$this->message->reply("You have now been added to the following groups: " . implode(", ", $groups)); |
|
125
|
|
|
} else { |
|
126
|
|
|
$this->message->reply("**Error** You are trying to authenticate with an already used (or not existing) auth token.."); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
// Mark this as garbage |
|
130
|
|
|
$this->isGarbage(); |
|
131
|
|
|
} |
|
132
|
|
|
} |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: