Passed
Push — master ( f1cc19...f13458 )
by Bob
09:09 queued 04:24
created

authCheck::tick()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 2
eloc 5
c 3
b 1
f 0
nc 2
nop 0
dl 0
loc 10
rs 9.4285
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
26
use Discord\Parts\Channel\Channel;
27
use Discord\Parts\User\Client;
28
29
/**
30
 * Class fileAuthCheck
31
 * @property int nextCheck
32
 */
33
class authCheck
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...
34
{
35
    /**
36
     * @var
37
     */
38
    var $config;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $config.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
39
    /**
40
     * @var
41
     */
42
    var $db;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $db.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
43
    /**
44
     * @var
45
     */
46
    var $discord;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $discord.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
47
    /**
48
     * @var
49
     */
50
    var $channelConfig;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $channelConfig.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
51
    /**
52
     * @var int
53
     */
54
    var $lastCheck = 0;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $lastCheck.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
55
    /**
56
     * @var
57
     */
58
    var $logger;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $logger.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
59
60
    /**
61
     * @param $config
62
     * @param $discord
63
     * @param $logger
64
     */
65
    function init($config, $discord, $logger)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
66
    {
67
        $this->config = $config;
68
        $this->discord = $discord;
69
        $this->logger = $logger;
70
        $this->nextCheck = 0;
71
        $lastCheck = getPermCache("authLastChecked");
72
        if ($lastCheck == NULL) {
73
            // Schedule it for right now if first run
74
            setPermCache("authLastChecked", time() - 5);
75
        }
76
    }
77
78
    /**
79
     * @return array
80
     */
81
    function information()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
82
    {
83
        return array(
84
            "name" => "",
85
            "trigger" => array(),
86
            "information" => ""
87
        );
88
    }
89
    function tick()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
90
    {
91
        $lastChecked = getPermCache("authLastChecked");
92
93
        if ($lastChecked <= time()) {
94
            $this->logger->addInfo("Checking authed users for changes....");
95
            $this->checkAuth();
96
        }
97
98
    }
99
100
    /**
101
     *
102
     */
103
    function checkAuth()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
104
    {
105
        if ($this->config["plugins"]["auth"]["periodicCheck"] == "true") {
106
            $db = $this->config["database"]["host"];
107
            $dbUser = $this->config["database"]["user"];
108
            $dbPass = $this->config["database"]["pass"];
109
            $dbName = $this->config["database"]["database"];
110
            $clientId = $this->config["bot"]["clientID"];
0 ignored issues
show
Unused Code introduced by
$clientId is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
111
            $id = $this->config["bot"]["guild"];
112
            $allyID = $this->config["plugins"]["auth"]["allianceID"];
113
            $corpID = $this->config["plugins"]["auth"]["corpID"];
114
            $toDiscordChannel = $this->config["plugins"]["auth"]["alertChannel"];
115
            $conn = new mysqli($db, $dbUser, $dbPass, $dbName);
116
117
            //get bot ID so we don't remove out own roles
118
            $botID = $this->discord->id;
119
120
            //Remove members who have roles but never authed
121
            $guild = $this->discord->guilds->get('id', $id);
122
            foreach($guild->members as $member) {
123
                $notifier = null;
0 ignored issues
show
Unused Code introduced by
$notifier is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
124
                $id = $member->id;
125
                $username = $member->username;
126
                $roles = $member->roles;
127
128
                $sql = "SELECT * FROM authUsers WHERE discordID='$id' AND active='yes'";
129
130
                $result = $conn->query($sql);
131
                if($result->num_rows == 0) {
132
                    foreach ($roles as $role) {
133
                        if(!isset($role->name)){
134
                            if($id != $botID){
135
                                $member->removeRole($role);
136
                                $member->save();
137
                                // Send the info to the channel
138
                                $msg = "{$username} has been removed from the {$role->name} role as they never authed (Someone manually assigned them roles).";
139
                                $channelID = $toDiscordChannel;
140
                                $channel = Channel::find($channelID);
141
                                $channel->sendMessage($msg, false);
142
                                $this->logger->addInfo("{$username} has been removed from the {$role->name} role as they never authed.");
143
                            }
144
                        }
145
                    }
146
                }
147
            }
148
149
            $sql = "SELECT characterID, discordID, eveName FROM authUsers WHERE active='yes'";
150
151
            $result = $conn->query($sql);
152
            $num_rows = $result->num_rows;
153
154
            if ($num_rows >= 2) {
155
                while ($rows = $result->fetch_assoc()) {
156
                    $charID = $rows['characterID'];
157
                    $discordID = $rows['discordID'];
158
                    $guild = $this->discord->guilds->first();
159
                    $member = $guild->members->get("id", $discordID);
160
                    $eveName = $rows['eveName'];
161
                    $roles = $member->roles;
162
                    $url = "https://api.eveonline.com/eve/CharacterAffiliation.xml.aspx?ids=$charID";
163
                    $xml = makeApiRequest($url);
164
                    if ($xml->result->rowset->row[0]) {
165
                        foreach ($xml->result->rowset->row as $character) {
166
                            if ($character->attributes()->allianceID != $allyID && $character->attributes()->corporationID != $corpID) {
167
                                foreach ($roles as $role) {
168
                                    $member->removeRole($role);
169
                                    $member->save();
170
                                }
171
172
                                $statsURL = "https://beta.eve-kill.net/api/corpInfo/corporationID/" . urlencode($corpID) . "/";
173
                                $stats = json_decode(downloadData($statsURL), true);
174
175
                                if (empty($stats)) {
176
                                    $stats["corporationName"] = "Unknown";
177
                                }
178
179
                                $corporationName = @$stats["corporationName"];
180
181
                                // Send the info to the channel
182
                                $msg = "{$eveName} roles have been removed, user is now a member of **{$corporationName}**.";
183
                                $channelID = $toDiscordChannel;
184
                                $channel = Channel::find($channelID);
185
                                $channel->sendMessage($msg, false);
186
                                $this->logger->addInfo("{$eveName} roles ({$role}) have been removed, user is now a member of **{$corporationName}**.");
0 ignored issues
show
Bug introduced by
The variable $role 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...
187
188
                                $sql = "UPDATE authUsers SET active='no' WHERE discordID='$discordID'";
189
                                $conn->query($sql);
190
191
                            }
192
                        }
193
                    }
194
                }
195
                $this->logger->addInfo("All users successfully authed.");
196
                $nextCheck = time() + 7200;
197
                setPermCache("authLastChecked", $nextCheck);
198
                if ($this->config["plugins"]["auth"]["nameEnforce"] == "true") {
199
                    while ($rows = $result->fetch_assoc()) {
200
                        $discordID = $rows['discordID'];
201
                        $eveName = $rows['eveName'];
202
                        $guild = $this->discord->guilds->first();
203
                        $member = $guild->members->get("id", $discordID);
204
                        $discordName = $member->user->username;
205
                        if ($discordName != $eveName) {
206
                            foreach ($roles as $role) {
0 ignored issues
show
Bug introduced by
The variable $roles 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...
207
                                $member->removeRole($role);
208
                            }
209
210
                            // Send the info to the channel
211
                            $msg = $discordName . " roles have been removed because their name no longer matches their ingame name.";
212
                            $channelID = $toDiscordChannel;
213
                            $channel = Channel::find($channelID);
214
                            $channel->sendMessage($msg, false);
215
                            $this->logger->addInfo($discordName . " roles have been removed because their name no longer matches their ingame name.");
216
217
                            $sql = "UPDATE authUsers SET active='no' WHERE discordID='$discordID'";
218
                            $conn->query($sql);
219
                        }
220
                    }
221
                    $this->logger->addInfo("All users names have been checked.");
222
                    $cacheTimer = gmdate("Y-m-d H:i:s", $nextCheck);
223
                    $this->logger->addInfo("Next auth and name check at {$cacheTimer} EVE");
224
                    return null;
225
                }
226
                $cacheTimer = gmdate("Y-m-d H:i:s", $nextCheck);
227
                $this->logger->addInfo("Next auth and name check at {$cacheTimer} EVE");
228
                return null;
229
            }
230
            $this->logger->addInfo("No users found in database.");
231
            $nextCheck = time() + 7200;
232
            setPermCache("authLastChecked", $nextCheck);
233
            $cacheTimer = gmdate("Y-m-d H:i:s", $nextCheck);
234
            $this->logger->addInfo("Next auth and name check at {$cacheTimer} EVE");
235
            return null;
236
        }
237
        return null;
238
    }
239
240
    /**
241
     * @param $msgData
242
     */
243
    function onMessage($msgData)
0 ignored issues
show
Unused Code introduced by
The parameter $msgData is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
244
    {
245
    }
246
}
247