-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSampQuery.class.php
282 lines (237 loc) · 7.54 KB
/
SampQuery.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<?php
/**
* @author Edward McKnight (EM-Creations.co.uk)
*/
/* *****************************************************************
// SampQuery.class.php
// Version 1.1
// This class connects to a specific SA-MP server via sockets.
// Copyright 2014 Edward McKnight (EM-Creations.co.uk)
// Creative Commons Attribution-NoDerivs 3.0 Unported License
// http://creativecommons.org/licenses/by-nd/3.0/
// Credits to Westie for the original PHP SA-MP API and inspiration for this API.
* *****************************************************************/
class SampQuery {
private $sock = null;
private $server = null;
private $port = null;
/**
* Creates a new SampQuery object.
* @param $server server hostname
* @param $port port of the server
*/
public function __construct($server, $port=7777) {
// <editor-fold defaultstate="collapsed" desc="Constructor">
$this->server = $server;
$this->port = $port;
$this->sock = fsockopen("udp://".$this->server, $this->port, $errorNum, $errorString, 2);
socket_set_timeout($this->sock, 2);
// </editor-fold>
}
/**
* Returns an array of server information.
* @return Array[]
* (
* [password] => 0 or 1
* [players] => players
* [maxplayers] => maxPlayers
* [hostname] => hostName
* [gamemode] => gamemode
* [map] => map
* )
*/
public function getInfo() {
// <editor-fold defaultstate="collapsed" desc="Get Info">
@fwrite($this->sock, $this->assemblePacket("i"));
fread($this->sock, 11);
$serverInfo = array();
$serverInfo['password'] = (integer) ord(fread($this->sock, 1));
$serverInfo['players'] = (integer) $this->toInt(fread($this->sock, 2));
$serverInfo['maxplayers'] = (integer) $this->toInt(fread($this->sock, 2));
$strLen = ord(fread($this->sock, 4));
if(!$strLen) return -1;
$serverInfo['hostname'] = (string) fread($this->sock, $strLen);
$strLen = ord(fread($this->sock, 4));
$serverInfo['gamemode'] = (string) fread($this->sock, $strLen);
$strLen = ord(fread($this->sock, 4));
$serverInfo['map'] = (string) fread($this->sock, $strLen);
return $serverInfo;
// </editor-fold>
}
/**
* Returns a multidimensional array of basic player information.
* @return Array[]
* (
* [0] => Array[]
* (
* [name] => playerName
* [score] => score
* )
* ...
* )
* @see getDetailedPlayers()
*/
public function getBasicPlayers() {
// <editor-fold defaultstate="collapsed" desc="Get Basic Players">
@fwrite($this->sock, $this->assemblePacket("c"));
fread($this->sock, 11);
$playerCount = ord(fread($this->sock, 2));
$players = array();
if($playerCount > 0) {
for($i = 0; $i < $playerCount; ++$i) {
$strLen = ord(fread($this->sock, 1));
$players[$i] = array
(
"name" => (string) fread($this->sock, $strLen),
"score" => (integer) $this->toInt(fread($this->sock, 4)),
);
}
}
return $players;
// </editor-fold>
}
/**
* Returns a multidimensional array of detailed player information.
* @return Array[]
* (
* [0] => Array
* (
* [playerid] => playerid
* [nickname] => playername
* [score] => score
* [ping] => pinh
* )
* ...
* )
* @see getBasicPlayers()
*/
public function getDetailedPlayers() {
// <editor-fold defaultstate="collapsed" desc="Get Detailed Players">
@fwrite($this->sock, $this->assemblePacket("d"));
fread($this->sock, 11);
$playerCount = ord(fread($this->sock, 2));
$players = array();
for($i = 0; $i < $playerCount; ++$i) {
$player['playerid'] = (integer) ord(fread($this->sock, 1));
$strLen = ord(fread($this->sock, 1));
$player['nickname'] = (string) fread($this->sock, $strLen);
$player['score'] = (integer) $this->toInt(fread($this->sock, 4));
$player['ping'] = (integer) $this->toInt(fread($this->sock, 4));
$players[$i] = $player;
unset($player);
}
return $players;
// </editor-fold>
}
/**
* Returns an array of server rules.
* @return Array[]
* (
* [gravity] => gravity
* [mapname] => map
* [version] => version
* [weather] => weather
* [weburl] => weburl
* [worldtime] => worldtime
* )
*/
public function getRules() {
// <editor-fold defaultstate="collapsed" desc="Get Rules">
@fwrite($this->sock, $this->assemblePacket("r"));
fread($this->sock, 11);
$ruleCount = ord(fread($this->sock, 2));
$rules = array();
for($i = 0; $i< $ruleCount; ++$i) {
$strLen = ord(fread($this->sock, 1));
$rule = (string) fread($this->sock, $strLen);
$strLen = ord(fread($this->sock, 1));
$rules[$rule] = (string) fread($this->sock, $strLen);
}
return $rules;
// </editor-fold>
}
/**
* Returns the server's ping.
* @return integer
*/
public function getPing() {
// <editor-fold defaultstate="collapsed" desc="Get Ping">
$ping = 0;
$beforeSend = microtime(true);
@fwrite($this->sock, $this->assemblePacket("r"));
fread($this->sock, 15);
$afterReceive = microtime(true);
$ping = ($afterReceive - $beforeSend) * 1000;
return round($ping);
// </editor-fold>
}
/**
* Converts a string to an integer
*
* @param String $string
* @return int|null
*/
private function toInt($string) {
// <editor-fold defaultstate="collapsed" desc="To Int">
if($string === "") {
return null;
}
$int = 0;
$int += (ord($string[0]));
if(isset($string[1])) {
$int += (ord($string[1]) << 8);
}
if(isset($string[2])) {
$int += (ord($string[2]) << 16);
}
if(isset($string[3])) {
$int += (ord($string[3]) << 24);
}
if($int >= 4294967294) {
$int -= 4294967296;
}
return $int;
// </editor-fold>
}
/**
* Assembles a packet, ready for sending
*
* @param char $type
* @return String
*/
private function assemblePacket($type) {
// <editor-fold defaultstate="collapsed" desc="Assemble Packet">
$packet = "SAMP";
$packet .= chr(strtok($this->server, "."));
$packet .= chr(strtok("."));
$packet .= chr(strtok("."));
$packet .= chr(strtok("."));
$packet .= chr($this->port & 0xFF);
$packet .= chr($this->port >> 8 & 0xFF);
$packet .= $type;
return $packet;
// </editor-fold>
}
/**
* Attempts to connect to the server and returns whether it was successful.
* @return boolean
*/
public function connect() {
// <editor-fold defaultstate="collapsed" desc="Connect">
$connected = false;
fwrite($this->sock, $this->assemblePacket("p0101"));
if(fread($this->sock, 10)) {
if(fread($this->sock, 5) == 'p0101') {
$connected = true;
}
}
return $connected;
// </editor-fold>
}
/**
* Closes the connection
*/
public function close() {
@fclose($this->sock);
}
}