|
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; |
|
|
|
|
|
|
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) |
|
|
|
|
|
|
50
|
|
|
{ |
|
51
|
|
|
$var = computeUnsignedCRC32($cs); |
|
52
|
|
|
$x = ('0x'); |
|
|
|
|
|
|
53
|
|
|
$a = substr($var, 0, 2); |
|
|
|
|
|
|
54
|
|
|
$a = $x . $a; |
|
55
|
|
|
$b = substr($var, 2, 2); |
|
|
|
|
|
|
56
|
|
|
$b = $x . $b; |
|
57
|
|
|
$c = substr($var, 4, 2); |
|
|
|
|
|
|
58
|
|
|
$c = $x . $c; |
|
59
|
|
|
$d = substr($var, 6, 2); |
|
|
|
|
|
|
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) { |
|
|
|
|
|
|
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) { |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
$cmdhead = chr(0xFF) . chr(0x01) . chr(0x00); |
|
116
|
|
|
//$cmd = "Players"; |
|
|
|
|
|
|
117
|
|
|
$cmd = $cmdhead . $cmd; |
|
118
|
|
|
$checksum = get_checksum($cmd); |
|
119
|
|
|
$cmdmsg = $head . $checksum . $cmd; |
|
120
|
|
|
$hlen = strlen($head . $checksum . chr(0xFF) . chr(0x01)); |
|
|
|
|
|
|
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
|
|
|
} |
|
|
|
|
|
|
141
|
|
|
|
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.