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

rcon.php ➔ fixAnswer()   D

Complexity

Conditions 9
Paths 9

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 2 Features 0
Metric Value
cc 9
eloc 18
c 3
b 2
f 0
nc 9
nop 1
dl 0
loc 31
rs 4.909
1
<?php
2
function strToHex($string)
3
{
4
    $hex = '';
5
    for ($i = 0; $i < strlen($string); $i++)
6
    {
7
        $hex .= dechex(ord($string[$i]));
8
    }
9
    return $hex;
10
}
11
12
function hexToStr($hex)
13
{
14
    $string = '';
15
    for ($i = 0; $i < strlen($hex) - 1; $i += 2)
16
    {
17
        $string .= chr(hexdec($hex[$i] . $hex[$i + 1]));
18
    }
19
    return $string;
20
}
21
22
function computeUnsignedCRC32($str) {
23
    sscanf(crc32($str), "%u", $var);
24
    $var = dechex($var + 0);
25
    return $var;
26
}
27
28
function dec_to_hex($dec)
29
{
30
    $sign = ""; // suppress errors
31
    $h = null;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $h. 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...
32
    if ($dec < 0) { $sign = "-"; $dec = abs($dec); }
33
34
    $hex = Array(0 => 0, 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5,
35
                    6 => 6, 7 => 7, 8 => 8, 9 => 9, 10 => 'a',
36
                    11 => 'b', 12 => 'c', 13 => 'd', 14 => 'e',
37
                    15 => 'f');
38
39
    do
40
    {
41
        $h = $hex[($dec % 16)] . $h;
42
        $dec /= 16;
43
    }
44
    while ($dec >= 1);
45
46
    return $sign . $h;
47
}
48
49
function get_checksum($cs)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $cs. 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...
50
{
51
    $var = computeUnsignedCRC32($cs);
52
    $x = ('0x');
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $x. 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...
53
    $a = substr($var, 0, 2);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $a. 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...
54
    $a = $x . $a;
55
    $b = substr($var, 2, 2);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $b. 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...
56
    $b = $x . $b;
57
    $c = substr($var, 4, 2);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $c. 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...
58
    $c = $x . $c;
59
    $d = substr($var, 6, 2);
0 ignored issues
show
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...
60
    $d = $x . $d;
61
    return chr($d) . chr($c) . chr($b) . chr($a);
62
}
63
64
function fixAnswer($answer)
65
{
66
67
    if ($answer == '') {
68
        return;
69
    }
70
    $answer = substr($answer, strpos($answer, 'Players on server'));
71
    $start_symbol = 0;
72
73
    while ($start_defect = strpos($answer, 'BE', $start_symbol)) {
74
        $last_symbol_defect = false;
75
        $seven_symbols = substr($answer, $start_defect, 7);
76
        $start_symbol = $start_defect + 7;
77
        if (mb_detect_encoding($seven_symbols, 'ASCII', true) == false) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing mb_detect_encoding($seven_symbols, 'ASCII', true) of type string to the boolean false. If you are specifically checking for an empty string, consider using the more explicit === '' instead.
Loading history...
78
            for ($i = 2; $i < 7; $i++) {
79
                $symbol = substr($seven_symbols, $i, 1);
80
                $ord = ord($symbol);
81
                if (mb_detect_encoding($symbol) == 'ASCII' && $ord < 33 OR $ord > 126) {
82
                    $last_symbol_defect = $i;
83
                }
84
            }
85
86
87
            if ($last_symbol_defect) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $last_symbol_defect of type integer|false is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
88
                    $answer = str_replace(substr($seven_symbols, 0, $last_symbol_defect + 1), '', $answer);
89
            }
90
        }
91
    }
92
93
    return $answer;
94
}
95
96
function rcon($serverip, $serverport, $rconpassword, $cmd)
97
{
98
    $passhead = chr(0xFF) . chr(0x00);
99
    $head = chr(0x42) . chr(0x45);
100
    $pass = $passhead . $rconpassword;
101
    $answer = "";
102
    $checksum = get_checksum($pass);
103
104
    $loginmsg = $head . $checksum . $pass;
105
106
    $rcon = fsockopen("udp://" . $serverip, $serverport, $errno, $errstr, 1);
107
    stream_set_timeout($rcon, 1);
108
109
    if (!$rcon) {
110
        echo "ERROR: $errno - $errstr<br />\n";
111
    } else {
112
        fwrite($rcon, $loginmsg);
113
        $res = fread($rcon, 16);
0 ignored issues
show
Unused Code introduced by
$res 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...
114
115
        $cmdhead = chr(0xFF) . chr(0x01) . chr(0x00);
116
        //$cmd = "Players";
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
117
        $cmd = $cmdhead . $cmd;
118
        $checksum = get_checksum($cmd);
119
        $cmdmsg = $head . $checksum . $cmd;
120
        $hlen = strlen($head . $checksum . chr(0xFF) . chr(0x01));
0 ignored issues
show
Unused Code introduced by
$hlen 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...
121
122
        fwrite($rcon, $cmdmsg);
123
        $answer = fread($rcon, 102400);
124
125
        if (strToHex(substr($answer, 9, 1)) == "0") {
126
            $count = strToHex(substr($answer, 10, 1));
127
            for ($i = 0; $i < $count - 1; $i++) {
128
                $answer .= fread($rcon, 102400);
129
            }
130
        }
131
        $cmd = "Exit";
132
        $cmd = $cmdhead . $cmd;
133
        $checksum = get_checksum($cmd);
134
        $cmdmsg = $head . $checksum . $cmd;
135
        fwrite($rcon, $cmdmsg);
136
    }
137
138
    $answer = fixAnswer($answer);
139
    return $answer;
140
}
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...
141