Passed
Push — master ( fb6ce1...7c7d88 )
by Bob
02:59
created

src/plugins/onMessage/auth.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
use Discord\Parts\Channel\Message;
27
28
/**
29
 * Class auth.
30
 *
31
 * @property  message
32
 */
33
class auth
34
{
35
    /*
36
     * @var
37
     */
38
    public $config;
39
    /*
40
     * @var
41
     */
42
    public $discord;
43
    /*
44
     * @var
45
     */
46
    public $logger;
47
    public $solarSystems;
48
    public $triggers = [];
49
    public $guildID;
50
    public $roleName;
51
    public $corpID;
52
    public $db;
53
    public $dbUser;
54
    public $dbPass;
55
    public $dbName;
56
    public $forceName;
57
    public $ssoUrl;
58
    public $nameEnforce;
59
    public $allianceID;
60
    public $allyroleName;
61
62
    /**
63
     * @param $config
64
     * @param $discord
65
     * @param $logger
66
     */
67
    public function init($config, $discord, $logger)
68
    {
69
        $this->config = $config;
70
        $this->discord = $discord;
71
        $this->logger = $logger;
72
        $this->db = $config['database']['host'];
73
        $this->dbUser = $config['database']['user'];
74
        $this->dbPass = $config['database']['pass'];
75
        $this->dbName = $config['database']['database'];
76
        $this->corpID = (int) $config['plugins']['auth']['corpID'];
77
        $this->allianceID = (int) $config['plugins']['auth']['allianceID'];
78
        $this->guildID = $config['plugins']['auth']['guildID'];
79
        $this->roleName = $config['plugins']['auth']['corpMemberRole'];
80
        $this->allyroleName = $config['plugins']['auth']['allyMemberRole'];
81
        $this->nameEnforce = $config['plugins']['auth']['nameEnforce'];
82
        $this->ssoUrl = $config['plugins']['auth']['url'];
83
    }
84
85
86
    public function tick()
87
    {
88
    }
89
90
    /**
91
     * @param $msgData
92
     * @param $message
93
     *
94
     * @return null
95
     */
96
    public function onMessage($msgData, $message)
97
    {
98
        $this->message = $message;
99
        $userID = $msgData['message']['fromID'];
100
        $userName = $msgData['message']['from'];
101
        $message = $msgData['message']['message'];
102
        $data = command($message, $this->information()['trigger'], $this->config['bot']['trigger']);
103
        if (isset($data['trigger'])) {
104
            if (isset($this->config['bot']['primary'])) {
105
                $userID = $msgData['message']['fromID'];
106
                $channelInfo = $this->message->getFullChannelAttribute();
107
                $guildID = $channelInfo[@guild_id];
108
                if ($guildID != $this->config['bot']['primary']) {
109
                    $this->message->reply('**Failure:** The auth code your attempting to use is for another discord server');
110
111
                    return;
112
                }
113
            }
114
            $code = $data['messageString'];
115
            $result = selectPending($this->db, $this->dbUser, $this->dbPass, $this->dbName, $code);
116
117
            if (strlen($code) < 12) {
118
                $this->message->reply('Invalid Code, check '.$this->config['bot']['trigger'].'help auth for more info.');
119
120
                return;
121
            }
122
123
            while ($rows = $result->fetch_assoc()) {
124
                $charid = (int) $rows['characterID'];
125
                $corpid = (int) $rows['corporationID'];
126
                $allianceid = (int) $rows['allianceID'];
127
                $url = "https://api.eveonline.com/eve/CharacterName.xml.aspx?ids=$charid";
128
                $xml = makeApiRequest($url);
129
130
131
132
                // We have an error, show it it
133
                if ($xml->error) {
134
                    $this->message->reply('**Failure:** Eve API error, please try again in a little while.');
135
136
                    return;
137
                }
138
139
                if (!isset($xml->result->rowset->row)) {
140
                    $this->message->reply('**Failure:** Eve API error, please try again in a little while.');
141
142
                    return;
143
                } elseif ($this->nameEnforce == 'true') {
144
                    foreach ($xml->result->rowset->row as $character) {
145
                        if ($character->attributes()->name != $userName) {
146
                            $this->message->reply('**Failure:** Your discord name must match your character name.');
147
                            $this->logger->addInfo('User was denied due to not having the correct name '.$character->attributes()->name);
148
149
                            return;
150
                        }
151
                    }
152
                }
153
                foreach ($xml->result->rowset->row as $character) {
154
                    $eveName = $character->attributes()->name;
155 View Code Duplication
                    if ($corpid === $this->corpID) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
156
                        $roles = $this->message->getFullChannelAttribute()->getGuildAttribute()->getRolesAttribute();
157
                        $member = $this->message->getFullChannelAttribute()->getGuildAttribute()->getMembersAttribute()->get('id', $userID);
158
                        foreach ($roles as $role) {
159
                            $roleName = $role->name;
160
                            if ($roleName == $this->roleName) {
161
                                $member->addRole($role);
162
                                $member->save();
163
                                insertUser($this->db, $this->dbUser, $this->dbPass, $this->dbName, $userID, $charid, $eveName, 'corp');
164
                                disableReg($this->db, $this->dbUser, $this->dbPass, $this->dbName, $code);
165
                                $this->message->reply('**Success:** You have now been added to the '.$this->roleName.' group. To get more roles, talk to the CEO / Directors');
166
                                $this->logger->addInfo('User authed and added to corp group '.$eveName);
167
168
                                return;
169
                            }
170
                        }
171
                    }
172 View Code Duplication
                    if ($allianceid === $this->allianceID) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
173
                        $roles = $this->message->getFullChannelAttribute()->getGuildAttribute()->getRolesAttribute();
174
                        $member = $this->message->getFullChannelAttribute()->getGuildAttribute()->getMembersAttribute()->get('id', $userID);
175
                        foreach ($roles as $role) {
176
                            $roleName = $role->name;
177
                            if ($roleName == $this->allyroleName) {
178
                                $member->addRole($role);
179
                                $member->save();
180
                                insertUser($this->db, $this->dbUser, $this->dbPass, $this->dbName, $userID, $charid, $eveName, 'ally');
181
                                disableReg($this->db, $this->dbUser, $this->dbPass, $this->dbName, $code);
182
                                $this->message->reply('**Success:** You have now been added to the '.$this->allyroleName.' group. To get more roles, talk to the CEO / Directors');
183
                                $this->logger->addInfo('User authed and added to the alliance group '.$eveName);
184
185
                                return;
186
                            }
187
                        }
188
                    }
189
                    $this->message->reply('**Failure:** There are no roles available for your corp/alliance.');
190
                    $this->logger->addInfo('User was denied due to not being in the correct corp or alliance '.$eveName);
191
192
                    return;
193
                }
194
            }
195
            $this->message->reply('**Failure:** There was an issue with your code.');
196
            $this->logger->addInfo('User was denied due to not being in the correct corp or alliance '.$userName);
197
198
            return;
199
        }
200
    }
201
202
    /**
203
     * @return array
204
     */
205 View Code Duplication
    public function information()
206
    {
207
        return [
208
            'name'        => 'auth',
209
            'trigger'     => [$this->config['bot']['trigger'].'auth'],
210
            '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.',
211
        ];
212
    }
213
214
    /**
215
     * @param $msgData
216
     */
217
    public function onMessageAdmin($msgData)
218
    {
219
    }
220
}
221