Test Setup Failed
Push — master ( 3d9945...bd93ea )
by Bob
03:44
created

authCheck::tick()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
cc 2
eloc 6
c 4
b 1
f 0
nc 2
nop 0
dl 0
loc 11
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\discord;
27
28
/**
29
 * Class fileAuthCheck
30
 * @property int nextCheck
31
 */
32
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...
33
{
34
    /**
35
     * @var
36
     */
37
    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...
38
    /**
39
     * @var
40
     */
41
    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...
42
    /**
43
     * @var
44
     */
45
    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...
46
    /**
47
     * @var
48
     */
49
    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...
50
    /**
51
     * @var int
52
     */
53
    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...
54
    /**
55
     * @var
56
     */
57
    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...
58
59
    /**
60
     * @param $config
61
     * @param $discord
62
     * @param $logger
63
     */
64
    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...
65
    {
66
        $this->config = $config;
67
        $this->discord = $discord;
68
        $this->logger = $logger;
69
        $this->nextCheck = 0;
70
        $lastCheck = getPermCache("authLastChecked");
71
        if ($lastCheck == NULL) {
72
            // Schedule it for right now if first run
73
            setPermCache("authLastChecked", time() - 5);
74
        }
75
    }
76
77
    /**
78
     * @return array
79
     */
80
    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...
81
    {
82
        return array(
83
            "name" => "",
84
            "trigger" => array(),
85
            "information" => ""
86
        );
87
    }
88
    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...
89
    {
90
        $lastChecked = getPermCache("authLastChecked");
91
        $discord = $this->discord;
92
93
        if ($lastChecked <= time()) {
94
            $this->logger->addInfo("Checking authed users for changes....");
95
            $this->checkAuth($discord);
96
        }
97
98
    }
99
100
    /**
101
     * @param $discord
102
     * @return null
103
     */
104
    function checkAuth($discord)
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...
105
    {
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
        $id = $this->config["bot"]["guild"];
111
        $allyID = $this->config["plugins"]["auth"]["allianceID"];
112
        $corpID = $this->config["plugins"]["auth"]["corpID"];
113
        $exempt = $this->config["plugins"]["auth"]["exempt"];
114
        if (is_null($exempt)) {
115
            $exempt = "0";
116
        }
117
        $toDiscordChannel = $this->config["plugins"]["auth"]["alertChannel"];
118
        $conn = new mysqli($db, $dbUser, $dbPass, $dbName);
119
120
        //get bot ID so we don't remove out own roles
121
        $botID = $this->discord->id;
122
123
        //Remove members who have roles but never authed
124
        $guild = $discord->guilds->get('id', $id);
125
        foreach($guild->members as $member) {
126
            $id = $member->id;
127
            $username = $member->username;
128
            $roles = $member->roles;
129
130
            $sql = "SELECT * FROM authUsers WHERE discordID='$id' AND active='yes'";
131
132
            $result = $conn->query($sql);
133
            if ($result->num_rows == 0) {
134
                foreach ($roles as $role) {
135
                    if (!isset($role->name)) {
136
                        if ($id != $botID && !in_array($role->name, $exempt, true)) {
137
                            $member->removeRole($role);
138
                            $guild->members->save($member);
139
                            // Send the info to the channel
140
                            $msg = "{$username} has been removed from the {$role->name} role as they never authed (Someone manually assigned them roles).";
141
                            $channelID = $toDiscordChannel;
142
                            $channel = $guild->channels->get('id', $channelID);
143
                            $channel->sendMessage($msg, false);
144
                            $this->logger->addInfo("{$username} has been removed from the {$role->name} role as they never authed.");
145
                        }
146
                    }
147
                }
148
            }
149
        }
150
151
        $sql = "SELECT characterID, discordID, eveName FROM authUsers WHERE active='yes'";
152
153
        $result = $conn->query($sql);
154
        $num_rows = $result->num_rows;
155
156
        if ($num_rows >= 1) {
157
            while ($rows = $result->fetch_assoc()) {
158
                $charID = $rows['characterID'];
159
                $discordID = $rows['discordID'];
160
                $member = $guild->members->get("id", $discordID);
161
                $eveName = $rows['eveName'];
162
                $roles = $member->roles;
163
164
                if ($this->config["plugins"]["auth"]["nameEnforce"] == "true" && !is_null($member)) {
165
                    $nick = $eveName;
166
                    $member->setNickname($nick);
167
                }
168
169
                $url = "https://api.eveonline.com/eve/CharacterAffiliation.xml.aspx?ids=$charID";
170
                $xml = makeApiRequest($url);
171
                // Stop the process if the api is throwing an error
172
                if (is_null($xml)) {
173
                    $this->logger->addInfo("{$eveName} cannot be authed, API issues detected.");
174
                    return null;
175
                }
176
                if ($xml->result->rowset->row[0]) {
177
                    foreach ($xml->result->rowset->row as $character) {
178
179
                        if ($character->attributes()->allianceID != $allyID && $character->attributes()->corporationID != $corpID) {
180
                            foreach ($roles as $role) {
181
                                $member->removeRole($role);
182
                                $guild->members->save($member);
183
                            }
184
185
                            $statsURL = "https://api.eveonline.com/eve/CharacterName.xml.aspx?ids=" . urlencode($character->attributes()->corporationID) . "/";
186
                            $stats = makeApiRequest($statsURL);
187
                            foreach ($stats->result->rowset->row as $corporation) {
188
                                $corporationName = $corporation->attributes()->name;
189
                            }
190
191
                            if (!isset($corporationName)) { // Make sure it's always set.
192
                                $corporationName = "Unknown";
193
                            }
194
195
                            if (!isset($role)) { // Make sure it's always set.
196
                                $role = "Unknown";
197
                            }
198
199
                            // Send the info to the channel
200
                            $msg = "{$eveName} roles have been removed, user is now a member of **{$corporationName}**.";
201
                            $channelID = $toDiscordChannel;
202
                            $channel = $guild->channels->get('id', $channelID);
203
                            $channel->sendMessage($msg, false);
204
                            $this->logger->addInfo("{$eveName} roles ({$role}) have been removed, user is now a member of **{$corporationName}**.");
205
206
                            $sql = "UPDATE authUsers SET active='no' WHERE discordID='$discordID'";
207
                            $conn->query($sql);
208
209
                        }
210
                    }
211
                }
212
            }
213
            $this->logger->addInfo("All users successfully authed.");
214
            $nextCheck = time() + 7200;
215
            setPermCache("authLastChecked", $nextCheck);
216
            $cacheTimer = gmdate("Y-m-d H:i:s", $nextCheck);
217
            $this->logger->addInfo("Next auth and name check at {$cacheTimer} EVE");
218
            return null;
219
        }
220
        $this->logger->addInfo("No users found in database.");
221
        $nextCheck = time() + 7200;
222
        setPermCache("authLastChecked", $nextCheck);
223
        $cacheTimer = gmdate("Y-m-d H:i:s", $nextCheck);
224
        $this->logger->addInfo("Next auth and name check at {$cacheTimer} EVE");
225
        return null;
226
    }
227
228
    function onMessage()
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...
229
    {
230
    }
231
}
232