-
Notifications
You must be signed in to change notification settings - Fork 0
/
SampQueryAPI.php
350 lines (292 loc) · 7.7 KB
/
SampQueryAPI.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<?php
/**
* This API connects directly to the server, without any need for any
* middlemen connections.
* Your server must have fsockopen enabled in order to access the
* functions that have been made available from this.
*
* @package sampAPI
* @version 1.2
* @author David Weston <westie@typefish.co.uk>
* @copyright 2010; http://www.typefish.co.uk/licences/
*/
class SampQueryAPI
{
/**
* @ignore
*/
private $rSocket = false;
/**
* @ignore
*/
private $aServer = array();
/**
* Creation of the server class.
*
* @param string $sServer Server IP, or hostname.
* @param integer $iPort Server port
*/
public function __construct($sServer, $iPort = 7777)
{
/* Fill some arrays. */
$this->aServer[0] = $sServer;
$this->aServer[1] = $iPort;
/* Start the connection. */
$this->rSocket = fsockopen('udp://'.$this->aServer[0], $this->aServer[1], $iError, $sError, 2);
if(!$this->rSocket)
{
$this->aServer[4] = false;
return;
}
socket_set_timeout($this->rSocket, 2);
$sPacket = 'SAMP';
$sPacket .= chr(strtok($this->aServer[0], '.'));
$sPacket .= chr(strtok('.'));
$sPacket .= chr(strtok('.'));
$sPacket .= chr(strtok('.'));
$sPacket .= chr($this->aServer[1] & 0xFF);
$sPacket .= chr($this->aServer[1] >> 8 & 0xFF);
$sPacket .= 'p4150';
fwrite($this->rSocket, $sPacket);
if(fread($this->rSocket, 10))
{
if(fread($this->rSocket, 5) == 'p4150')
{
$this->aServer[4] = true;
return;
}
}
$this->aServer[4] = false;
}
/**
* @ignore
*/
public function __destruct()
{
@fclose($this->rSocket);
}
/**
* Used to tell if the server is ready to accept queries.
*
* If false is returned, then it is suggested that you remove the
* class from active use, so that you can reload the class if needs
* be.
*
* @return bool true if success, false if failure.
*/
public function isOnline()
{
return isset($this->aServer[4]) ? $this->aServer[4] : false;
}
/**
* This function is used to get the server information.
*
* <code>
* Array
* (
* [password] => 0
* [players] => 9
* [maxplayers] => 500
* [hostname] => Everystuff Tr3s [MAD]oshi (03a Final) [FIXED]
* [gamemode] => Stunt/Race/DM/FR Everystuff
* [mapname] => Everystuff
* )
* </code>
*
* @return array Array of server information.
*/
public function getInfo()
{
@fwrite($this->rSocket, $this->createPacket('i'));
fread($this->rSocket, 11);
$aDetails['password'] = (integer) ord(fread($this->rSocket, 1));
$aDetails['players'] = (integer) $this->toInteger(fread($this->rSocket, 2));
$aDetails['maxplayers'] = (integer) $this->toInteger(fread($this->rSocket, 2));
$iStrlen = ord(fread($this->rSocket, 4));
if(!$iStrlen) return -1;
$aDetails['hostname'] = (string) fread($this->rSocket, $iStrlen);
$iStrlen = ord(fread($this->rSocket, 4));
$aDetails['gamemode'] = (string) fread($this->rSocket, $iStrlen);
/*$iStrlen = ord(fread($this->rSocket, 4));
$aDetails['mapname'] = (string) fread($this->rSocket, $iStrlen);*/
return $aDetails;
}
/**
* This function gets a basic list of all the players on the server.
*
* Note as of 0.3.0, the amount of players that can be retrieved is
* limited to 100. This means if there are more players than 100,
* then no data will be returned, and it will be a blank array.
*
* <code>
* Array
* (
* [0] => Array
* (
* [nickname] => K1nNngO
* [score] => 72
* )
*
* [1] => Array
* (
* [nickname] => [kikOo]
* [score] => 150
* )
*
* [and so on...]
* )
* </code>
*
* @return array Array of player information.
*/
public function getBasicPlayers()
{
@fwrite($this->rSocket, $this->createPacket('c'));
fread($this->rSocket, 11);
$iPlayerCount = ord(fread($this->rSocket, 2));
$aDetails = array();
if($iPlayerCount > 0)
{
for($iIndex = 0; $iIndex < $iPlayerCount; ++$iIndex)
{
$iStrlen = ord(fread($this->rSocket, 1));
$aDetails[] = array
(
"nickname" => (string) fread($this->rSocket, $iStrlen),
"score" => (integer) $this->toInteger(fread($this->rSocket, 4)),
);
}
}
return $aDetails;
}
/**
* This function gets a detailed list of all the players on the server.
*
* Note as of 0.3.0, the amount of players that can be retrieved is
* limited to 100. This means if there are more players than 100,
* then no data will be returned, and it will be a blank array.
*
* <code>
* Array
* (
* [0] => Array
* (
* [playerid] => 0
* [nickname] => K1nNngO
* [score] => 72
* [ping] => 195
* )
*
* [1] => Array
* (
* [playerid] => 1
* [nickname] => [kikOo]
* [score] => 150
* [ping] => 375
* )
*
* [and so on...]
* )
* </code>
*
* @return array Array of player information.
*/
public function getDetailedPlayers()
{
@fwrite($this->rSocket, $this->createPacket('d'));
fread($this->rSocket, 11);
$iPlayerCount = ord(fread($this->rSocket, 2));
$aDetails = array();
for($iIndex = 0; $iIndex < $iPlayerCount; ++$iIndex)
{
$aPlayer['playerid'] = (integer) ord(fread($this->rSocket, 1));
$iStrlen = ord(fread($this->rSocket, 1));
$aPlayer['nickname'] = (string) fread($this->rSocket, $iStrlen);
$aPlayer['score'] = (integer) $this->toInteger(fread($this->rSocket, 4));
$aPlayer['ping'] = (integer) $this->toInteger(fread($this->rSocket, 4));
$aDetails[] = $aPlayer;
unset($aPlayer);
}
return $aDetails;
}
/**
* This function gets all the server rules from the server.
*
* Rules in this context are not player rules, they are client rules,
* like the weather of the server, time, and so on. (Custom rules,
* when supported by a SA-MP plugin, will be included here.)
*
* <code>
* Array
* (
* [gravity] => 0.007900
* [mapname] => Everystuff
* [version] => 0.3a
* [weather] => 0
* [weburl] => samp.madoshi.net
* [worldtime] => 12:00
* )
* </code>
*
* @return array Array of server rules.
*/
public function getRules()
{
@fwrite($this->rSocket, $this->createPacket('r'));
fread($this->rSocket, 11);
$iRuleCount = ord(fread($this->rSocket, 2));
$aReturn = array();
for($iIndex = 0; $iIndex < $iRuleCount; ++$iIndex)
{
$iStrlen = ord(fread($this->rSocket, 1));
$sRulename = (string) fread($this->rSocket, $iStrlen);
$iStrlen = ord(fread($this->rSocket, 1));
$aDetails[$sRulename] = (string) fread($this->rSocket, $iStrlen);
}
return $aDetails;
}
/**
* @ignore
*/
private function toInteger($sData)
{
if($sData === "")
{
return null;
}
$iInteger = 0;
$iInteger += (ord($sData[0]));
if(isset($sData[1]))
{
$iInteger += (ord($sData[1]) << 8);
}
if(isset($sData[2]))
{
$iInteger += (ord($sData[2]) << 16);
}
if(isset($sData[3]))
{
$iInteger += (ord($sData[3]) << 24);
}
if($iInteger >= 4294967294)
{
$iInteger -= 4294967296;
}
return $iInteger;
}
/**
* @ignore
*/
private function createPacket($sPayload)
{
$sPacket = 'SAMP';
$sPacket .= chr(strtok($this->aServer[0], '.'));
$sPacket .= chr(strtok('.'));
$sPacket .= chr(strtok('.'));
$sPacket .= chr(strtok('.'));
$sPacket .= chr($this->aServer[1] & 0xFF);
$sPacket .= chr($this->aServer[1] >> 8 & 0xFF);
$sPacket .= $sPayload;
return $sPacket;
}
}