/
minequery.class.php
149 lines (117 loc) · 3.87 KB
/
minequery.class.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
/*
* Minequery PHP
* Copyright (C) 2011 Vex Software LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* The Minequery PHP class.
*/
class Minequery {
/**
* @var string name of itself for testing
*/
public static $classname = 'Minequery';
/**
* Queries a Minequery server.
*
* @static
* @param string $address The address to the Minequery server.
* @param int $port The port of the Minequery server.
* @param int $timeout The time given before the connection attempt gives up.
* @return array|bool An array on success, FALSE on failure.
*/
public static function query($address, $port = 25566, $timeout = 30) {
$query = array();
$write = "QUERY\n";
$class = self::$classname;
$response = $class::read($address, $port, $errno, $errstr, $timeout, $write, $latency);
if (false === $response) {
return false;
}
$response = explode("\n", $response);
// Server port
$query['serverPort'] = explode(" ", $response[0], 2);
$query['serverPort'] = $query['serverPort'][1];
// Player count
$query['playerCount'] = explode(" ", $response[1], 2);
$query['playerCount'] = $query['playerCount'][1];
// Max players
$query['maxPlayers'] = explode(" ", $response[2], 2);
$query['maxPlayers'] = $query['maxPlayers'][1];
// Player list
list(, $buffer) = explode(" ", $response[3], 2);
$buffer = trim($buffer, "[] ");
$query['playerList'] = $buffer ? explode(", ", $buffer) : array();
$query['latency'] = $latency;
return $query;
}
/**
* Queries a Minequery server using JSON.
*
* @static
* @param string $address The address to the Minequery server.
* @param int $port The port of the Minequery server.
* @param int $timeout The time given before the connection attempt gives up.
* @return object|bool A stdClass object on success, FALSE on failure.
*/
public static function query_json($address, $port = 25566, $timeout = 30) {
$beginning_time = microtime(true);
$socket = @fsockopen($address, $port, $errno, $errstr, $timeout);
if (!$socket) {
// Could not establish a connection to the server.
return false;
}
$end_time = microtime(true);
fwrite($socket, "QUERY_JSON\n");
$response = "";
while (!feof($socket)) {
$response .= fgets($socket, 1024);
}
$query = json_decode($response);
$query->latency = ($end_time - $beginning_time) * 1000;
return $query;
}
/**
* Queries a Remote Socket
*
* @static
* @param string $address The address to the Minequery server.
* @param int $port The port of the Minequery server.
* @param int $errno
* @param string $errstr
* @param int $timeout The time given before the connection attempt gives up.
* @param string $write
* @param int $latency
* @return string|bool An string on success, FALSE on failure.
*/
protected static function read($address, $port, &$errno, &$errstr, $timeout, $write, &$latency) {
$latency = NULL;
$beginning_time = microtime(true);
$socket = @fsockopen($address, $port, $errno, $errstr, $timeout);
if (!$socket) {
// Could not establish a connection to the server.
return false;
}
$end_time = microtime(true);
$latency = ($end_time - $beginning_time) * 1000;
fwrite($socket, $write);
$response = "";
while (!feof($socket)) {
$response .= fgets($socket, 1024);
}
return $response;
}
}