diff --git a/minecraft_status.php b/minecraft_status.php new file mode 100644 index 0000000..62fffae --- /dev/null +++ b/minecraft_status.php @@ -0,0 +1,55 @@ +status = $status;} + public function getMOTD() {return $this->status["motd"];} + public function getVersion() {return $this->status["version"];} + public function getCurrentPlayers() {return $this->status["players"];} + public function getAddress() {return $this->status["address"];} + } + + class ServerStatusQuery { + public function __construct() {} + + public function getServerInfo($address = "127.0.0.1", $port = 25565, $timeout = 5) { + if(!filter_var($address, FILTER_VALIDATE_IP)) {$address = gethostbyname($address);} + $ping_start = microtime(true); + $socket = stream_socket_client("tcp://" . $address . ":" . $port, $errorNumber, $errorString, $timeout ); + + if (!$socket) {return false;} + else { + stream_set_timeout($socket, $timeout ); + fwrite ($socket, "\XFE\01"); + $data = fread( $socket, 2048); + fclose($socket); + $ping = microtime(true) - $ping_start; + if($data == null) {return false;} + + if (substr((String) $data, 3, 5) == "\00\xa7\x00\x31\x00") { + $content = explode( "\x00", mb_convert_encoding(substr((String)$data, 15), 'UTF-8', 'UCS-2' ) ); + $content[1] = preg_replace("/(§.)/", "",$content[1]); + } + else { + $content = explode('§', mb_convert_encoding(substr((String)$data, 3), 'UTF-8', 'UCS-2')); + $content[1] = ""; + foreach ($content as $key => $string) { + if($key != sizeof($content)-1 && $key != sizeof($content)-2 && $key != 0) {$content[1] .= '§'.$string;} + } + $content[1] = preg_replace("/(§.)/", "", $content[1]); + } + $content[1] = preg_replace("/[^[:alnum:][:punct:] ]/", "", $content[1]); + + $info = array( + "version" => $content[1], + "motd" => $content[2], + "players" => $content[3], + "address" => $address + ); + + return new ServerStatus($info); + } + } + }; +?>