Passed
Branch dev (4bd34d)
by Bob
02:19
created

auth::information()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
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
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $corpInfo is correct as getCorpInfo($corpID) (which targets getCorpInfo()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
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) {
0 ignored issues
show
Duplication introduced by
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...
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) {
0 ignored issues
show
Duplication introduced by
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...
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);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $allianceContacts is correct as getContacts($allianceID) (which targets getContacts()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
177
                        $corpContacts = getContacts($corpID);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $corpContacts is correct as getContacts($corpID) (which targets getContacts()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
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']) {
0 ignored issues
show
Duplication introduced by
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...
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']) {
0 ignored issues
show
Duplication introduced by
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...
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']) {
0 ignored issues
show
Duplication introduced by
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...
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']) {
0 ignored issues
show
Duplication introduced by
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...
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);
0 ignored issues
show
Bug introduced by
The variable $nick does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
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