-
Notifications
You must be signed in to change notification settings - Fork 1
/
MinecraftStatus.class.php
executable file
·95 lines (86 loc) · 2.81 KB
/
MinecraftStatus.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
<?php
/**
* Minecraft Server Status Class
* @copyright © 2011 Nox Nebula - Patrick Kleinschmidt
* @website https://github.com/NoxNebula/MC-Server-Status
* @license GNU Public Licence - Version 3
* @author Nox Nebula - Patrick Kleinschmidt
**/
class MinecraftStatus {
private $Socket, $Info;
public $Online, $MOTD, $CurPlayers, $MaxPlayers, $IP, $Port, $Error;
public function __construct($IP, $Port = '25565') {
$this->IP = $IP;
$this->Port = $Port;
// Remove any protocols from serveraddress
if(preg_match('/(.*):\/\//', $this->IP)) {
$this->IP = preg_replace('/(.*):\/\//', '', $this->IP);
}
if(strpos($this->IP, '/') !== false) {
$this->IP = rtrim($this->IP, '/');
if(strpos($this->IP, '/') !== false) {
$this->Failed();
$this->Error = 'Unsupported IP/Domain format, no \'/\'s allowed';
return;
}
}
if(preg_match_all('/:/', $this->IP, $matches) > 1) {
unset($matches);
// IP6
if(strpos($this->IP, '[') === false && strpos($this->IP, ']') === false)
$this->IP = '['.$this->IP.']';
} else if(strpos($this->IP, ':') !== false) {
$this->Failed();
$this->Error = 'Unsupported IP/Domain format';
return;
}
if($this->Socket = @stream_socket_client('tcp://'.$this->IP.':'.$Port, $ErrNo, $ErrStr, 1)) {
// If IP6 remove brackets
if(strpos($this->IP, '[') === 0 && strpos($this->IP, ']') === (strlen($this->IP) - 1))
$this->IP = trim($this->IP, '[]');
$this->Online = true;
fwrite($this->Socket, "\xfe");
$Handle = fread($this->Socket, 2048);
$Handle = str_replace("\x00", '', $Handle);
$Handle = substr($Handle, 2);
$this->Info = explode("\xa7", $Handle); // Separate Infos
unset($Handle);
fclose($this->Socket);
if(sizeof($this->Info) == 3) {
$this->MOTD = $this->Info[0];
$this->CurPlayers = (int)$this->Info[1];
$this->MaxPlayers = (int)$this->Info[2];
$this->Error = false;
} else if(sizeof($this->Info) > 3) { // Handle error, Minecraft don't handle this.
$Temp = '';
for($i = 0; $i < sizeof($this->Info) - 2; $i++) {
$Temp .= ($i > 0 ? '§' : '').$this->Info[$i];
}
$this->MOTD = $Temp;
$this->CurPlayers = (int)$this->Info[sizeof($this->Info) - 2];
$this->MaxPlayers = (int)$this->Info[sizeof($this->Info) - 1];
$this->Error = 'Faulty motd or outdated script';
} else {
$this->Failed();
$this->Error = 'Unexpected error, cause may be an outdated script';
}
} else {
$this->Online = false;
$this->Failed();
$this->Error = 'Can not reach the server';
}
}
public function Info() {
return array(
'MOTD' => $this->MOTD,
'CurPlayers' => $this->CurPlayers,
'MaxPlayers' => $this->MaxPlayers
);
}
private function Failed() {
$this->MOTD = false;
$this->CurPlayers = false;
$this->MaxPlayers = false;
}
}
?>