Completed
Push — master ( 280e63...1ffdb5 )
by Sam
03:06
created

gfunctions.php ➔ serverConnect()   C

Complexity

Conditions 7
Paths 18

Size

Total Lines 37
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 2 Features 0
Metric Value
cc 7
eloc 23
c 3
b 2
f 0
nc 18
nop 1
dl 0
loc 37
rs 6.7272
1
<?php
2
function encrypt($text)
3
{
4
    $settings = require('config/settings.php');
5
    return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $settings['key'], $text, MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))));
6
}
7
8
function decrypt($text)
9
{
10
    $settings = require('config/settings.php');
11
    return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $settings['key'], base64_decode($text), MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND)));
12
}
13
14
function masterConnect()
15
{
16
    $settings = require('config/settings.php');
17
18
    if (isset($settings['db']['port'])) {
19
        $db_connection = new mysqli(decrypt($settings['db']['host']), decrypt($settings['db']['user']), decrypt($settings['db']['pass']), decrypt($settings['db']['name']), decrypt($settings['db']['port']));
20
    } else {
21
        $db_connection = new mysqli(decrypt($settings['db']['host']), decrypt($settings['db']['user']), decrypt($settings['db']['pass']), decrypt($settings['db']['name']));
22
    }
23
    if (!$db_connection->set_charset("utf8")) {
24
        $db_connection->errors[] = $db_connection->error;
0 ignored issues
show
Bug introduced by
The property errors does not seem to exist. Did you mean error?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
25
    }
26
27
    return $db_connection;
28
}
29
30
function serverConnect($dbid = NULL)
31
{
32
    if (isset($_SESSION['dbid']) && empty($dbid)) {
33
        $dbid = $_SESSION['dbid'];
34
    }
35
    $settings = require('config/settings.php');
0 ignored issues
show
Unused Code introduced by
$settings 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...
36
    $db_connection = masterConnect();
37
38
    $sql = "SELECT `sql_host`,`sql_name`,`sql_pass`,`sql_user` FROM `db` WHERE `dbid` = '" . $dbid . "';";
39
    $server = $db_connection->query($sql);
40
41
    if ($server->num_rows === 1) {
42
        $server = $server->fetch_object();
43
        $host = decrypt($server->sql_host);
44
45
        if (strpos($host, ":")) {
46
            $SQL = explode(":", $host);
47
            $host = $SQL['0'];
48
            $port = $SQL['1'];
49
        }
50
51
        if (isset($port)) {
52
            $db_link = new mysqli($host, decrypt($server->sql_user), decrypt($server->sql_pass), decrypt($server->sql_name), $port);
53
        } else {
54
            $db_link = new mysqli($host, decrypt($server->sql_user), decrypt($server->sql_pass), decrypt($server->sql_name));
55
        }
56
57
        if (!$db_link->set_charset("utf8")) {
58
            $db_link->errors[] = $db_link->error;
0 ignored issues
show
Bug introduced by
The property errors does not seem to exist. Did you mean error?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
59
        }
60
61
        return $db_link;
62
    } else {
63
        return false;
64
    }
65
66
}
67
68
function carType($car, $lang)
69
{
70
    switch ($car) {
71
        case 'Car':
72
            return $lang['car'];
73
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
74
        case 'Air':
75
            return $lang['air'];
76
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
77
        case 'Ship':
78
            return $lang['ship'];
79
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
80
    }
81
}
82
83
function yesNo($input, $lang)
84
{
85
    if ($input == 1) {
86
        return $lang['yes'];
87
    } else if ($input == 0) {
88
        return $lang['no'];
89
    } else {
90
        return $lang['error'];
91
    }
92
}
93
94
function select($val, $row)
95
{
96
    if ($row == $val) {
97
        return 'selected';
98
    }
99
}
100
101 View Code Duplication
function nameID($pId, $db_link)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in 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...
102
{
103
    $sql = "SELECT `name` FROM `players` WHERE `playerid` LIKE '" . $pId . "' ";
104
    $result_of_query = $db_link->query($sql);
105
106
    if ($result_of_query->num_rows > 0) {
107
        while ($row = mysqli_fetch_assoc($result_of_query)) {
108
            return $row['name'];
109
        }
110
    } else {
111
        return $pId;
112
    }
113
}
114
115 View Code Duplication
function uID($pId, $db_link)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in 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...
116
{
117
    $sql = "SELECT `uid` FROM `players` WHERE `playerid` = '" . $pId . "' ";
118
    $result_of_query = $db_link->query($sql);
119
    if ($result_of_query->num_rows > 0) {
120
        while ($row = mysqli_fetch_assoc($result_of_query)) {
121
            return $row['uid'];
122
        }
123
    } else {
124
        return $pId;
125
    }
126
}
127
128 View Code Duplication
function uIDname($uID, $db_link)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in 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...
129
{
130
    $sql = "SELECT `name` FROM `players` WHERE `uid` = '" . $uID . "' ";
131
    $result_of_query = $db_link->query($sql);
132
    if ($result_of_query->num_rows > 0) {
133
        while ($row = mysqli_fetch_assoc($result_of_query)) {
134
            return $row['name'];
135
        }
136
    } else {
137
        return $uID;
138
    }
139
}
140
141
function IDname($name, $db_link)
142
{
143
    $sql = "SELECT `name`,`playerid` FROM `players` WHERE `name` LIKE '%" . $name . "%' ";
144
    $result_of_query = $db_link->query($sql);
145
146
    if ($result_of_query->num_rows > 0) {
147
        while ($row = mysqli_fetch_array($result_of_query)) {
0 ignored issues
show
Unused Code introduced by
$row 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...
Unused Code introduced by
This while loop is empty and can be removed.

This check looks for while loops that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

Consider removing the loop.

Loading history...
148
        }
149
    } else {
150
        return $name;
151
    }
152
}
153
154
/**
155
 * @param string $action
156
 * @param integer $level
157
 */
158
function logAction($user, $action, $level)
159
{
160
    $settings = require('config/settings.php');
161
162
    if ($settings['logging']) {
163
        $db_connection = masterConnect();
164
        $sql = "INSERT INTO `logs` (`user`, `action`, `level`) VALUES ('" . $user . "', '" . $action . "', '" . $level . "');";
165
        $db_connection->query($sql);
166
    }
167
}
168
169
function message($text)
170
{
171
    echo "<br><div class='row'><div class='col-lg-12'>";
172
    echo "<div class='alert alert-danger alert-dismissable'>";
173
    echo "<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>";
174
    echo "<i class='fa fa-info-circle'></i> " . $text . "</div></div></div>";
175
}
176
177
function error($errno, $errstr, $errfile, $errline)
178
{
179
    echo '<h4><b>PHP ERROR ' . $errno . '</b> ' . $errstr . ' - ' . $errfile . ':' . $errline . '</h4>';
180
}
181
182
/**
183
 * @param integer $code
184
 */
185
function errorMessage($code, $lang)
186
{
187
    switch ($code)
188
    {
189
        case 1:
190
            return $lang['lowVersion']; //Version too low
191
        case 2:
192
            return $lang['dbConnect']; //Db Connection
193
        case 3:
194
            return $lang['noRes']; //No Results
195
        case 4:
196
            return $lang['404']; //404 Not Found
197
        case 5:
198
            return $lang['noPerm']; //No Permissions
199
        case 6:
200
            return $lang['banned']; //User Banned
201
        case 7:
202
            return $lang['pluginNF']; //Pulgin Not Found
203
        case 8:
204
            return $lang['noID']; //No ID
205
        case 9:
206
            return $lang['noPlayers']; // RCON no players online
207
        case 10:
208
            return $lang['selDB']; // Select A DB
209
        case 11:
210
            return $lang['noServer']; // Select A DB
211
        case 31:
212
            return $lang['noHouse']; //No House
213
        case 32:
214
            return $lang['noVeh']; //No Vehicle
215
        case 33:
216
            return $lang['noGang']; //No Gang
217
        case 34:
218
            return $lang['noCrimes']; //No Crimes
219
        case 35:
220
            return $lang['noCrimes']; //No Crimes
221
        case 36:
222
            return $lang['noPlayer']; //No Player
223
        case 37:
224
            return $lang['noLic']; //No License
225 View Code Duplication
        case 371:
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...
226
            return $lang['no'] . ' ' . $lang['civil'] . ' ' . $lang['licenses']; //No Civillian Licenses
227 View Code Duplication
        case 372:
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...
228
            return $lang['no'] . ' ' . $lang['medic'] . ' ' . $lang['licenses']; //No Medic Licenses
229 View Code Duplication
        case 373:
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...
230
            return $lang['no'] . ' ' . $lang['police'] . ' ' . $lang['licenses']; //No Police Licenses
231
        case 38:
232
            return $lang['no'] . ' ' . $lang['gear']; //No License
233 View Code Duplication
        case 381:
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...
234
            return $lang['no'] . ' ' . $lang['civil'] . ' ' . $lang['gear']; //No Civillian Licenses
235 View Code Duplication
        case 382:
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...
236
            return $lang['no'] . ' ' . $lang['medic'] . ' ' . $lang['gear']; //No Medic Licenses
237 View Code Duplication
        case 383:
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...
238
            return $lang['no'] . ' ' . $lang['police'] . ' ' . $lang['gear']; //No Police Licenses
239
    }
240
}
241
242 View Code Duplication
function random($length)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in 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...
243
{
244
    $max = ceil($length / 40);
245
    $random = '';
246
    for ($i = 0; $i < $max; $i++) {
247
        $random .= sha1(microtime(true) . mt_rand(10000, 90000));
248
    }
249
    return substr($random, 0, $length);
250
}
251
252
function steamBanned($PID)
253
{
254
    $settings = require('config/settings.php');
255
    if (!empty($settings['steamAPI'])) {
256
        $api = "http://api.steampowered.com/ISteamUser/GetPlayerBans/v1/?key=" . $settings['steamAPI'] . "&steamids=" . $PID;
257
        $bans = json_decode(file_get_contents($api), true);
258
        if ($bans['players']['0']['VACBanned']) {
259
            return '<h4><span class="label label-danger" style="margin-left:3px; line-height:2;">VAC BANNED</span></h4>';
260
        }
261
        //todo:formatting
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
262
    }
263
}
264
265
function multiDB()
266
{
267
    $db_connection = masterConnect();
268
269
    $sql = "SELECT `sid`,`dbid`,`type` FROM `servers`;";
270
    $db = $db_connection->query($sql);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $db. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
271
    if ($db->num_rows == 1) {
272
        $iamDB = $db->fetch_object();
273
        $_SESSION['multiDB'] = false;
274
        $_SESSION['server_type'] = $iamDB->type;
275
        $_SESSION['dbid'] = $iamDB->dbid;
276
    } else {
277
        $_SESSION['multiDB'] = true;
278
    }
279
}
280
281
function tokenGen($length)
282
{
283
    return substr(str_shuffle('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'), 0, $length);
284
}
285
286
function stripArray($input, $type)
287
{
288
    $array = array();
289
290
    switch ($type) {
291 View Code Duplication
        case 0:
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...
292
            $array = explode("],[", $input);
293
            $array = str_replace('"[[', "", $array);
294
            $array = str_replace(']]"', "", $array);
295
            $array = str_replace('`', "", $array);
296
            break;
297 View Code Duplication
        case 1:
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...
298
            $array = explode(",", $input);
299
            $array = str_replace('"[', "", $array);
300
            $array = str_replace(']"', "", $array);
301
            $array = str_replace('`', "", $array);
302
            break;
303 View Code Duplication
        case 2:
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...
304
            $array = explode(",", $input);
305
            $array = str_replace('"[', "", $array);
306
            $array = str_replace(']"', "", $array);
307
            $array = str_replace('`', "", $array);
308
            break;
309
        case 3:
310
            $input = str_replace('[`', "", $input);
311
            $input = str_replace('`]', "", $input);
312
            $array = explode("`,`", $input);
313
            break;
314
    }
315
316
    return $array;
317
}
318
319
function clean($input, $type)
320
{
321
    if ($type == 'string') {
322
        return filter_var(htmlspecialchars(trim($input)), FILTER_SANITIZE_STRING);
323
    } elseif ($type == 'int') {
324
        $input = filter_var(htmlspecialchars(trim($input)), FILTER_SANITIZE_NUMBER_INT);
325
        if ($input < 0) {
326
            return 0;
327
        } else {
328
            return $input;
329
        }
330
    } elseif ($type == 'url') {
331
        return filter_var(htmlspecialchars(trim($input)), FILTER_SANITIZE_URL);
332
    } elseif ($type == 'email') {
333
        return filter_var(htmlspecialchars(trim($input)), FILTER_SANITIZE_EMAIL);
334
    } elseif ($type == 'boolean') {
335
        return ($input === 'true');
336
    } elseif ($type == 'intbool') {
337
        if ($input == 1 || $input == 0) return $input;
338
    } else {
339
        return 0;
340
    }
341
    }
342
343
/**
344
 * @param string $this
345
 * @param string|null $inthat
346
 */
347
function before($this, $inthat)
348
{
349
    return substr($inthat, 0, strpos($inthat, $this));
350
}
351
352
353
/**
354
 * @param string $this
355
 */
356
function after($this, $inthat)
357
{
358
    if (!is_bool(strpos($inthat, $this))) {
359
            return substr($inthat, strpos($inthat, $this) + strlen($this));
360
    }
361
}
362
363
function communityBanned($GUID)
364
{
365
    $settings = require('config/settings.php');
366
    $data = json_decode(file_get_contents("http://bans.itsyuka.tk/api/bans/player/id/" . $GUID . "/format/json/key/" . $settings['communityBansAPI']), true);
367
368
    if ($data['level'] >= 1) {
369
        return true;
370
    } else {
371
        return false;
372
    }
373
}
374
375
/**
376
 * Get either a Gravatar URL or complete image tag for a specified email address.
377
 *
378
 * @param string $email The email address
379
 * @param string $s Size in pixels, defaults to 80px [ 1 - 2048 ]
380
 * @param string $d Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ]
381
 * @param string $r Maximum rating (inclusive) [ g | pg | r | x ]
382
 * @param boole $img True to return a complete IMG tag False for just the URL
383
 * @param array $atts Optional, additional key/value attributes to include in the IMG tag
384
 * @return String containing either just a URL or a complete image tag
385
 * @source http://gravatar.com/site/implement/images/php/
386
 */
387
function get_gravatar( $email, $s = 80, $d = 'mm', $r = 'x', $img = false, $atts = array() ) {
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $s. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
Comprehensibility introduced by
Avoid variables with short names like $d. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
Comprehensibility introduced by
Avoid variables with short names like $r. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
388
    $url = 'https://www.gravatar.com/avatar/';
389
    $url .= md5( strtolower( trim( $email ) ) );
390
    $url .= "?s=$s&d=$d&r=$r";
391
    if ( $img ) {
392
        $url = '<img src="' . $url . '"';
393
        foreach ( $atts as $key => $val )
394
            $url .= ' ' . $key . '="' . $val . '"';
395
        $url .= ' />';
396
    }
397
    return $url;
398
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
399