|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* The MIT License (MIT) |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) 2016 Robert Sardinia |
|
6
|
|
|
* |
|
7
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
9
|
|
|
* in the Software without restriction, including without limitation the rights |
|
10
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
11
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
12
|
|
|
* furnished to do so, subject to the following conditions: |
|
13
|
|
|
* |
|
14
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
15
|
|
|
* copies or substantial portions of the Software. |
|
16
|
|
|
* |
|
17
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
18
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
19
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
20
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
21
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
22
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
23
|
|
|
* SOFTWARE. |
|
24
|
|
|
*/ |
|
25
|
|
|
use discord\discord; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Class auth |
|
29
|
|
|
*/ |
|
30
|
|
|
class auth |
|
|
|
|
|
|
31
|
|
|
{ |
|
32
|
|
|
public $config; |
|
33
|
|
|
public $discord; |
|
34
|
|
|
public $logger; |
|
35
|
|
|
private $excludeChannel; |
|
36
|
|
|
private $nameEnforce; |
|
37
|
|
|
private $db; |
|
38
|
|
|
private $dbUser; |
|
39
|
|
|
private $dbPass; |
|
40
|
|
|
private $dbName; |
|
41
|
|
|
private $ssoUrl; |
|
42
|
|
|
private $corpTickers; |
|
43
|
|
|
private $authGroups; |
|
44
|
|
|
private $standingsBased; |
|
45
|
|
|
private $guild; |
|
46
|
|
|
private $triggers; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param $config |
|
50
|
|
|
* @param $discord |
|
51
|
|
|
* @param $logger |
|
52
|
|
|
*/ |
|
53
|
|
|
public function init($config, $discord, $logger) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->config = $config; |
|
56
|
|
|
$this->discord = $discord; |
|
57
|
|
|
$this->logger = $logger; |
|
58
|
|
|
$this->db = $config['database']['host']; |
|
59
|
|
|
$this->dbUser = $config['database']['user']; |
|
60
|
|
|
$this->dbPass = $config['database']['pass']; |
|
61
|
|
|
$this->dbName = $config['database']['database']; |
|
62
|
|
|
$this->corpTickers = $config['plugins']['auth']['corpTickers']; |
|
63
|
|
|
$this->nameEnforce = $config['plugins']['auth']['nameEnforce']; |
|
64
|
|
|
$this->ssoUrl = $config['plugins']['auth']['url']; |
|
65
|
|
|
$this->excludeChannel = $this->config['bot']['restrictedChannels']; |
|
66
|
|
|
$this->authGroups = $config['plugins']['auth']['authGroups']; |
|
67
|
|
|
$this->standingsBased = $config['plugins']['auth']['standings']['enabled']; |
|
68
|
|
|
$this->guild = $config['bot']['guild']; |
|
69
|
|
|
$this->triggers[] = $this->config['bot']['trigger'] . 'auth'; |
|
70
|
|
|
$this->triggers[] = $this->config['bot']['trigger'] . 'Auth'; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param $msgData |
|
75
|
|
|
* @param $message |
|
76
|
|
|
* @return null |
|
77
|
|
|
*/ |
|
78
|
|
|
public function onMessage($msgData, $message) |
|
79
|
|
|
{ |
|
80
|
|
|
$channelID = (int) $msgData['message']['channelID']; |
|
81
|
|
|
|
|
82
|
|
|
if (in_array($channelID, $this->excludeChannel, true)) { |
|
83
|
|
|
return null; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$this->message = $message; |
|
87
|
|
|
$userID = $msgData['message']['fromID']; |
|
88
|
|
|
$userName = $msgData['message']['from']; |
|
89
|
|
|
$message = $msgData['message']['message']; |
|
90
|
|
|
$channelInfo = $this->message->channel; |
|
91
|
|
|
$guildID = $channelInfo[@guild_id]; |
|
92
|
|
|
$data = command($message, $this->information()['trigger'], $this->config['bot']['trigger']); |
|
93
|
|
|
if (isset($data['trigger'])) { |
|
94
|
|
|
if (isset($this->config['bot']['primary'])) { |
|
95
|
|
|
if ($guildID != $this->config['bot']['primary']) { |
|
96
|
|
|
$this->message->reply('**Failure:** The auth code your attempting to use is for another discord server'); |
|
97
|
|
|
return null; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
// If config is outdated |
|
102
|
|
|
if (null === $this->authGroups) { |
|
103
|
|
|
$this->message->reply('**Failure:** Please update the bots config to the latest version.'); |
|
104
|
|
|
return null; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$code = $data['messageString']; |
|
108
|
|
|
$result = selectPending($this->db, $this->dbUser, $this->dbPass, $this->dbName, $code); |
|
109
|
|
|
|
|
110
|
|
|
if (strlen($code) < 12) { |
|
111
|
|
|
$this->message->reply('Invalid Code, check ' . $this->config['bot']['trigger'] . 'help auth for more info.'); |
|
112
|
|
|
return null; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
while ($rows = $result->fetch_assoc()) { |
|
116
|
|
|
$charID = (int) $rows['characterID']; |
|
117
|
|
|
$corpID = (int) $rows['corporationID']; |
|
118
|
|
|
$allianceID = (int) $rows['allianceID']; |
|
119
|
|
|
|
|
120
|
|
|
//If corp is new store in DB |
|
121
|
|
|
$corpInfo = getCorpInfo($corpID); |
|
|
|
|
|
|
122
|
|
|
if (null === $corpInfo) { |
|
123
|
|
|
$corpDetails = corpDetails($corpID); |
|
124
|
|
|
if (null === $corpDetails) { // Make sure it's always set. |
|
125
|
|
|
$this->message->reply('**Failure:** Unable to auth at this time, ESI is down. Please try again later.'); |
|
126
|
|
|
return null; |
|
127
|
|
|
} |
|
128
|
|
|
$corpTicker = $corpDetails['ticker']; |
|
129
|
|
|
$corpName = (string)$corpDetails['corporation_name']; |
|
130
|
|
|
if (null !== $corpTicker) { |
|
131
|
|
|
addCorpInfo($corpID, $corpTicker, $corpName); |
|
132
|
|
|
} |
|
133
|
|
|
} else { |
|
134
|
|
|
$corpTicker = $corpInfo['corpTicker']; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
//Add corp ticker to name |
|
138
|
|
|
if ($this->corpTickers === 'true') { |
|
139
|
|
|
$setTicker = 1; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
//Set eve name if nameCheck is true |
|
143
|
|
|
if ($this->nameEnforce === 'true') { |
|
144
|
|
|
$nameEnforce = 1; |
|
145
|
|
|
} |
|
146
|
|
|
$role = null; |
|
147
|
|
|
|
|
148
|
|
|
$roles = @$this->message->channel->guild->roles; |
|
149
|
|
|
$member = @$this->message->channel->guild->members->get('id', $userID); |
|
150
|
|
|
$eveName = characterName($charID); |
|
151
|
|
|
if (null === $eveName) { |
|
152
|
|
|
$this->message->reply('**Failure:** Unable to auth at this time, ESI is down. Please try again later.'); |
|
153
|
|
|
return null; |
|
154
|
|
|
} |
|
155
|
|
|
foreach ($this->authGroups as $authGroup) { |
|
156
|
|
|
//Check if corpID matches |
|
157
|
|
|
if ($corpID === $authGroup['corpID']) { |
|
158
|
|
View Code Duplication |
foreach ($roles as $role) { |
|
|
|
|
|
|
159
|
|
|
if ((string)$role->name === (string)$authGroup['corpMemberRole']) { |
|
160
|
|
|
$member->addRole($role); |
|
161
|
|
|
$role = 'corp'; |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
//Check if allianceID matches |
|
166
|
|
|
if ($allianceID === $authGroup['allianceID'] && $authGroup['allianceID'] != 0) { |
|
167
|
|
View Code Duplication |
foreach ($roles as $role) { |
|
|
|
|
|
|
168
|
|
|
if ((string)$role->name === (string)$authGroup['allyMemberRole']) { |
|
169
|
|
|
$member->addRole($role); |
|
170
|
|
|
$role = 'ally'; |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
//check for standings based roles |
|
175
|
|
|
if ($this->standingsBased === 'true' && $role === null) { |
|
176
|
|
|
$allianceContacts = getContacts($allianceID); |
|
|
|
|
|
|
177
|
|
|
$corpContacts = getContacts($corpID); |
|
|
|
|
|
|
178
|
|
|
foreach ($roles as $role) { |
|
179
|
|
View Code Duplication |
if ((@(int)$allianceContacts['standings'] === 5 || @(int)$corpContacts['standings'] === 5) && (string)$role->name === (string)$this->config['plugins']['auth']['standings']['plus5Role']) { |
|
|
|
|
|
|
180
|
|
|
$member->addRole($role); |
|
181
|
|
|
$role = 'blue'; |
|
182
|
|
|
} |
|
183
|
|
View Code Duplication |
if ((@(int)$allianceContacts['standings'] === 10 || @(int)$corpContacts['standings'] === 10) && (string)$role->name === (string)$this->config['plugins']['auth']['standings']['plus10Role']) { |
|
|
|
|
|
|
184
|
|
|
$member->addRole($role); |
|
185
|
|
|
$role = 'blue'; |
|
186
|
|
|
} |
|
187
|
|
|
if ((@(int)$allianceContacts['standings'] === 0 || @(int)$corpContacts['standings'] === 0 || (@(int)$allianceContacts['standings'] && @(int)$corpContacts['standings'] === null || '')) && (string)$role->name === (string)$this->config['plugins']['auth']['standings']['neutralRole']) { |
|
188
|
|
|
$member->addRole($role); |
|
189
|
|
|
$role = 'neut'; |
|
190
|
|
|
} |
|
191
|
|
View Code Duplication |
if ((@(int)$allianceContacts['standings'] === -5 || @(int)$corpContacts['standings'] === -5) && (string)$role->name === (string)$this->config['plugins']['auth']['standings']['minus5Role']) { |
|
|
|
|
|
|
192
|
|
|
$member->addRole($role); |
|
193
|
|
|
$role = 'red'; |
|
194
|
|
|
} |
|
195
|
|
View Code Duplication |
if ((@(int)$allianceContacts['standings'] === -10 || @(int)$corpContacts['standings'] === -10) && (string)$role->name === (string)$this->config['plugins']['auth']['standings']['minus10Role']) { |
|
|
|
|
|
|
196
|
|
|
$member->addRole($role); |
|
197
|
|
|
$role = 'red'; |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
if (null !== $role) { |
|
202
|
|
|
$guild = $this->discord->guilds->get('id', $guildID); |
|
203
|
|
|
insertUser($this->db, $this->dbUser, $this->dbPass, $this->dbName, $userID, $charID, $eveName, $role); |
|
204
|
|
|
disableReg($this->db, $this->dbUser, $this->dbPass, $this->dbName, $code); |
|
205
|
|
|
$msg = ":white_check_mark: **Success:** {$userName} has been successfully authed."; |
|
206
|
|
|
$this->logger->addInfo("auth: {$eveName} authed"); |
|
207
|
|
|
$this->message->reply($msg); |
|
208
|
|
|
//Add ticker if set and change name if nameEnforce is on |
|
209
|
|
|
if (isset($setTicker) || isset($nameEnforce)) { |
|
210
|
|
|
if (isset($setTicker) && isset($nameEnforce)) { |
|
211
|
|
|
$nick = "[{$corpTicker}] {$eveName}"; |
|
212
|
|
|
} elseif (null === $setTicker && isset($nameEnforce)) { |
|
213
|
|
|
$nick = "{$eveName}"; |
|
214
|
|
|
} elseif (isset($setTicker) && !isset($nameEnforce)) { |
|
215
|
|
|
$nick = "[{$corpTicker}] {$userName}"; |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
if (null !== $nick) { |
|
219
|
|
|
queueRename($userID, $nick, $this->guild); |
|
|
|
|
|
|
220
|
|
|
} |
|
221
|
|
|
$guild->members->save($member); |
|
222
|
|
|
return null; |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
$this->message->reply('**Failure:** There are no roles available for your corp/alliance.'); |
|
226
|
|
|
$this->logger->addInfo('Auth: User was denied due to not being in the correct corp or alliance ' . $eveName); |
|
227
|
|
|
return null; |
|
228
|
|
|
} |
|
229
|
|
|
$this->message->reply('**Failure:** There was an issue with your code.'); |
|
230
|
|
|
$this->logger->addInfo('Auth: User was denied due to the code being invalid ' . $userName); |
|
231
|
|
|
return null; |
|
232
|
|
|
} |
|
233
|
|
|
return null; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* @return array |
|
238
|
|
|
*/ |
|
239
|
|
|
public function information() |
|
240
|
|
|
{ |
|
241
|
|
|
return array( |
|
242
|
|
|
'name' => 'auth', |
|
243
|
|
|
'trigger' => $this->triggers, |
|
244
|
|
|
'information' => 'SSO based auth system. ' . $this->ssoUrl . ' Visit the link and login with your main EVE account, select the correct character, and put the !auth <string> you receive in chat.' |
|
245
|
|
|
); |
|
246
|
|
|
} |
|
247
|
|
|
} |
|
248
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.