Files
Minecraft-Server-Status/minecraft_status.php
2025-08-16 09:03:01 -04:00

66 lines
2.5 KiB
PHP

<?php
function ping($host, $port, $timeout){return fSockOpen($host, $port, $errno, $errstr, $timeout);}
class Server{
public $NAME; public $HOST; public $PORT; public $PING;
public function __construct($NAME,$HOST,$PORT) {
$this->NAME = $NAME; $this->HOST = $HOST; $this->PORT = $PORT; $this->PING = ping($HOST, $PORT, 0.5);
}
function ping($host, $port, $timeout){return fSockOpen($host, $port, $errno, $errstr, $timeout);}
}
class ServerStatus {
private $status = array();
public function __construct($status) {$this->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);
}
}
};
?>