forked from Momenzi/UltimateRPG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SampRcon.class.php
441 lines (385 loc) · 11.8 KB
/
SampRcon.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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
<?php
/**
* @author Edward McKnight (EM-Creations.co.uk)
*/
/* *****************************************************************
// SampRcon.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 SampRcon {
private $sock = null;
private $server = null;
private $port = null;
private $password = null;
/**
* Creates a new SampRcon object.
* @param $server hostname of the server
* @param $port port of the server
* @param $password rcon password for the server
*/
public function __construct($server, $port, $password) {
// <editor-fold defaultstate="collapsed" desc="Constructor">
$this->server = $server;
$this->port = $port;
$this->password = $password;
// </editor-fold>
}
/**
* Returns an array of rcon commands.
* @return Array
* (
* Array[0] => "echo"
* ...
* )
* ...
* @see getServerVariables()
*/
public function getCommandList() {
// <editor-fold defaultstate="collapsed" desc="Get Command List">
$commands = $this->rconSend('cmdlist');
foreach($commands as &$command) {
$command = trim($command);
}
return $commands;
// </editor-fold>
}
/**
* Returns an array of server variables.
* @return Array[]
* (
* ['variableName'] = variableValue
* ...
* )
* @see getCommandList
*/
public function getServerVariables() {
// <editor-fold defaultstate="collapsed" desc="Get Server Variables">
$aVariables = $this->rconSend('varlist');
unset($aVariables[0]);
$aReturn = array();
foreach($aVariables as $sString) {
preg_match('/(.*)=[\s]+(.*)/', $sString, $aMatches);
if($aMatches[2][0] == '"') {
preg_match('/\"(.*)\"[\s]+\(/', $aMatches[2], $aTemp);
$aReturn[trim($aMatches[1])] = $aTemp[1];
} else {
preg_match('/(.*?)\s+\(/', $aMatches[2], $aTemp);
$aReturn[trim($aMatches[1])] = $aTemp[1];
}
}
return $aReturn;
// </editor-fold>
}
/**
* Sets the server's current weather, if no weather ID is given 1 is used.
* @param $weatherID weather ID
*/
public function setWeather($weatherID=1) {
$this->rconSend("weather ".$weatherID, false);
}
/**
* Sets the server's current gravity, of mp gravity is given 0.008 is used.
* @param $gravity gravity double / float
*/
public function setGravity($gravity=0.008) {
$this->rconSend("gravity ".$gravity, false);
}
/**
* Ban a player from the server.
* @param $playerID player's ID
* @return Output from ban
*/
public function ban($playerID) {
return $this->rconSend("ban ".$playerID);
}
/**
* Kick a player from the server.
* @param $playerID player's ID
* @return Output from kick
*/
public function kick($playerID) {
return $this->rconSend("kick ".$playerID);
}
/**
* Ban an IP address from the server.
* @param $address IP address
* @return Output from ban
*/
public function banAddress($address) {
return $this->rconSend("banip ".$address);
}
/**
* Unban an IP address from the server.
* @param $address IP address
* @return Output from unban
*/
public function unbanAddress($address) {
return $this->rconSend("unbanip ".$address);
}
/**
* Reload the server's log file.
*/
public function reloadLog() {
return $this->rconSend("reloadlog");
}
/**
* Reload the server's bans file.
*/
public function reloadBans() {
return $this->rconSend("reloadbans");
}
/**
* Send an admin message to players on the server.
* @param $message message to send to players
*/
public function say($message) {
$this->rconSend("say ".$message, false);
}
/**
* Change the server's current gamemode.
* @param $gameMode game mode to change to
*/
public function changeGameMode($gameMode) {
$this->rconSend("changemode ".$gameMode, false);
}
/**
* Set the server's current displayed game mode text.
* @param $gameModeText game mode text to change to
*/
public function setGameModeText($gameModeText) {
$this->rconSend("gamemodetext ".$gameModeText, false);
}
/**
* Run the server's next gamemode (gmx).
*/
public function nextGameMode() {
$this->rconSend("gmx", false);
}
/**
* Run the server's next gamemode (gmx).
*/
public function gmx() {
$this->nextGamemode();
}
/**
* Execute config file.
* @param $config config file to execute
* @return Output from exec
*/
public function execConfig($config) {
return $this->rconSend("exec ".$config);
}
/**
* Load a filterscript.
* @param $fs filterscript to load
* @return Output from filterscript load
*/
public function loadFilterscript($fs) {
return $this->rconSend("loadfs ".$fs);
}
/**
* Unload a filterscript.
* @param $fs filterscript to unload
* @return Output from filterscript unload
*/
public function unloadFilterscript($fs) {
return $this->rconSend("unloadfs ".$fs);
}
/**
* Reload a filterscript.
* @param $fs filterscript to reload
* @return Output from filterscript reload
*/
public function reloadFilterscript($fs) {
return $this->rconSend("reloadfs ".$fs);
}
/**
* Shutdown the server.
*/
public function exitGame() {
$this->rconSend("exit", false);
}
/**
* Sets the server's host name.
* @param $hostName the host name to set the server to
*/
public function setHostName($hostName) {
$this->rconSend("hostname ".$hostName, false);
}
/**
* Sets the server's map name.
* @param $mapName the map name to set the server to
*/
public function setMapname($mapName) {
$this->rconSend("mapname ".$mapName, false);
}
/**
* Sets the server's time.
* Note: This will not change the weather relative to the time.
* @param $time the time to set the server to
*/
public function setTime($time) {
$this->rconSend("worldtime ".$time, false);
}
/**
* Sets the server's web url.
* @param $url the url to set the server to
*/
public function setURL($url) {
$this->rconSend("weburl ".$url, false);
}
/**
* Sets the server's password.
* @param $password the password to set the server to
*/
public function setPassword($password) {
$this->rconSend("password ".$password, false);
}
/**
* Remove the server's password.
*/
public function removePassword() {
$this->rconSend("password 0", false);
}
/**
* Sets the server's rcon password.
* @param $password the password the rcon password should be set to
*/
public function setRconPassword($password) {
$this->rconSend("rcon_password ".$password, false);
}
/**
* Disables remote rcon to the server.
*/
public function disableRcon() {
$this->rconSend("rcon 0", false);
}
/**
* Enables remote queries to the server.
*/
public function enableQuery() {
$this->rconSend("query 1", false);
}
/**
* Disables remote queries to the server.
*/
public function disableQuery() {
$this->rconSend("query 0", false);
}
/**
* Enables the server's announce.
*/
public function enableAnnounce() {
$this->rconSend("announce 1", false);
}
/**
* Disables the server's announce.
*/
public function disableAnnounce() {
$this->rconSend("announce 0", false);
}
/**
* Sets the server's max number of NPCs.
* @param $maxNPCs the maximum numbers of NPCs
*/
public function setMaxNPCs($maxNPCs) {
$this->rconSend("maxnpc ".$maxNPCs, false);
}
/**
* Execute an rcon command.
* @param $command command to execute
* @param $delay delay time, if you don't expect any data back set this to false
* @return Output from command
*/
public function call($command, $delay=1.0) {
return $this->rconSend($command, $delay);
}
/**
* Send an rcon command
*
* @param String $command
* @param double $delay
* @return String
*/
private function rconSend($command, $delay=1.0) {
// <editor-fold defaultstate="collapsed" desc="Rcon Send">
fwrite($this->sock, $this->assemblePacket($command));
if ($delay === false) {
return;
}
$result = array();
$iMicrotime = microtime(true) + $delay;
while (microtime(true) < $iMicrotime) {
$temp = substr(fread($this->sock, 128), 13);
if (strlen($temp)) {
$result[] = $temp;
} else {
break;
}
}
return $result;
// </editor-fold>
}
/**
* Assembles a packet, ready for sending
*
* @param String $command
* @return String
*/
private function assemblePacket($command) {
// <editor-fold defaultstate="collapsed" desc="Assemble Packet">
$sPacket = "SAMP";
$sPacket .= chr(strtok($this->server, "."));
$sPacket .= chr(strtok("."));
$sPacket .= chr(strtok("."));
$sPacket .= chr(strtok("."));
$sPacket .= chr($this->port & 0xFF);
$sPacket .= chr($this->port >> 8 & 0xFF);
$sPacket .= "x";
$sPacket .= chr(strlen($this->password) & 0xFF);
$sPacket .= chr(strlen($this->password) >> 8 & 0xFF);
$sPacket .= $this->password;
$sPacket .= chr(strlen($command) & 0xFF);
$sPacket .= chr(strlen($command) >> 8 & 0xFF);
$sPacket .= $command;
return $sPacket;
// </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;
$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 .= "p0101";
$this->sock = fsockopen("udp://".$this->server, $this->port, $errorNum, $errorString, 2);
socket_set_timeout($this->sock, 2);
fwrite($this->sock, $packet);
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);
}
}