-
Notifications
You must be signed in to change notification settings - Fork 10
/
query.php
72 lines (67 loc) · 2.03 KB
/
query.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
67
68
69
70
71
72
<?php
// Query code for PocketMine Banners
define('SERVEROK', 0);
define('SERVER_DOWN', 1);
define('SERVERINVALID', 2);
define('UNKNOWN_ERROR', 3);
define('TIMEOUT', 1);
function UT3QueryServer($host,$port,&$return)
{
$sock = @fsockopen( "udp://" . $host, $port );
if ( !$sock ) return SERVERINVALID;
socket_set_timeout( $sock, 0, 500000 );
if ( !@fwrite( $sock, "\xFE\xFD\x09\x10\x20\x30\x40\xFF\xFF\xFF\x01" ) )
return SERVER_DOWN;
$challenge = fread( $sock, 1400 );
if ( !$challenge )
return UNKNOWN_ERROR;
$challenge = substr( preg_replace( "/[^0-9\-]/si", "", $challenge ), 1 );
$query = sprintf(
"\xFE\xFD\x00\x10\x20\x30\x40%c%c%c%c\xFF\xFF\xFF\x01",
( $challenge >> 24 ),
( $challenge >> 16 ),
( $challenge >> 8 ),
( $challenge >> 0 )
);
if ( !@fwrite( $sock, $query ) )
return UNKNOWN_ERROR;
$response = array();
for ($x = 0; $x < 2; $x++)
{
$response[] = @fread($sock,2048);
}
$response = implode($response);
$response = substr($response,16);
$response = explode("\0",$response);
array_pop($response);
array_pop($response);
array_pop($response);
array_pop($response);
$return = array();
$type = 0;
foreach ($response as $key)
{
if ($type == 0) $val = $key;
if ($type == 1) $return[$val] = $key;
$type == 0 ? $type = 1 : $type = 0;
}
return SERVEROK;
}
$host = $_GET['host'];
$port = $_GET['port'];
if (empty($_GET['port'])) {
$port = 19132;
}
$values = '';
$returnvalue = UT3QueryServer($host,$port,$values);
if($returnvalue != SERVEROK){
$ostatus = 2;
}else{
$currentplayers = $values['numplayers'];
$maxplayers = $values['maxplayers'];
$hostname = $values['hostname'];
$percent = (((int)$currentplayers / (int)$maxplayers)*100);
// echo $percent;
$ostatus = 1;
// echo 'Players: '.$currentplayers.'/'.$maxplayers.'<br><br>';
}