-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCoDQuery.php
66 lines (53 loc) · 1.52 KB
/
CoDQuery.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
namespace Promod\CoDQuery;
require 'ServerinfoFactory.php';
class CoDServer
{
private $address, $port, $rcon_password;
function __construct($address, $port = 28960, $rcon_password = '')
{
$this->address = $address;
$this->port = $port;
$this->rcon_password = $rcon_password;
}
private function command($commands = '')
{
if(empty($commands))
{
return 'Error: empty command';
}
if(!is_array($commands))
{
$commands = array($commands);
}
$responses = [];
$socket = fsockopen('udp://' . $this->address, $this->port);
stream_set_timeout($socket, 2);
foreach($commands as $command)
{
$response = '';
fwrite($socket, str_repeat(chr(255), 4) . $command);
$bytes_left = 1;
do
{
$response .= fread($socket, $bytes_left);
} while(($bytes_left = stream_get_meta_data($socket)['unread_bytes']) > 0);
$responses[] = $response;
}
fclose($socket);
return $responses;
}
function rcon($command)
{
if(empty($rcon_command))
{
return 'Error: RCON password not set.';
}
return $this->command('rcon ' . $rcon_password . ' ' . $command);
}
public function info()
{
$responses = $this->command(['getInfo', 'getStatus']);
return ServerinfoFactory::generateInfo($responses[0], $responses[1]);
}
}