Advertisement
TheStuntman

status.class.php

Mar 18th, 2015
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.42 KB | None | 0 0
  1. <?php
  2.  
  3.     class MinecraftServerStatus {
  4.  
  5.         private $timeout;
  6.  
  7.         public function __construct($timeout = 2) {
  8.             $this->timeout = $timeout;
  9.         }
  10.  
  11.         public function getStatus($host = '127.0.0.1', $port = 25565, $version = '1.7.*') {
  12.  
  13.             if (substr_count($host , '.') != 4) $host = gethostbyname($host);
  14.  
  15.             $serverdata = array();
  16.             $serverdata['hostname'] = $host;
  17.             $serverdata['version'] = false;
  18.             $serverdata['protocol'] = false;
  19.             $serverdata['players'] = false;
  20.             $serverdata['maxplayers'] = false;
  21.             $serverdata['motd'] = false;
  22.             $serverdata['motd_raw'] = false;
  23.             $serverdata['favicon'] = false;
  24.             $serverdata['ping'] = false;
  25.  
  26.             $socket = $this->connect($host, $port);
  27.  
  28.             if(!$socket) {
  29.                 return false;
  30.             }
  31.  
  32.             if(preg_match('/1.7|1.8/',$version)) {
  33.  
  34.                 $start = microtime(true);
  35.  
  36.                 $handshake = pack('cccca*', hexdec(strlen($host)), 0, 0x04, strlen($host), $host).pack('nc', $port, 0x01);
  37.  
  38.                 socket_send($socket, $handshake, strlen($handshake), 0); //give the server a high five
  39.                 socket_send($socket, "\x01\x00", 2, 0);
  40.                 socket_read( $socket, 1 );
  41.  
  42.                 $ping = round((microtime(true)-$start)*1000); //calculate the high five duration
  43.  
  44.                 $packetlength = $this->read_packet_length($socket);
  45.  
  46.                 if($packetlength < 10) {
  47.                     return false;
  48.                 }
  49.  
  50.                 socket_read($socket, 1);
  51.  
  52.                 $packetlength = $this->read_packet_length($socket);
  53.  
  54.                 $data = socket_read($socket, $packetlength, PHP_NORMAL_READ);
  55.  
  56.                 if(!$data) {
  57.                     return false;
  58.                 }
  59.  
  60.                 $data = json_decode($data);
  61.  
  62.                 $serverdata['version'] = $data->version->name;
  63.                 $serverdata['protocol'] = $data->version->protocol;
  64.                 $serverdata['players'] = $data->players->online;
  65.                 $serverdata['maxplayers'] = $data->players->max;
  66.  
  67.                 $motd = $data->description;
  68.                 $motd = preg_replace("/(§.)/", "",$motd);
  69.                 $motd = preg_replace("/[^[:alnum:][:punct:] ]/", "", $motd);
  70.  
  71.                 $serverdata['motd'] = $motd;
  72.                 $serverdata['motd_raw'] = $data->description;
  73.                 $serverdata['favicon'] = $data->favicon;
  74.                 $serverdata['ping'] = $ping;
  75.  
  76.             } else {
  77.  
  78.                 $start = microtime(true);
  79.  
  80.                 socket_send($socket, "\xFE\x01", 2, 0);
  81.                 $length = socket_recv($socket, $data, 512, 0);
  82.  
  83.                 $ping = round((microtime(true)-$start)*1000);//calculate the high five duration
  84.                
  85.                 if($length < 4 || $data[0] != "\xFF") {
  86.                     return false;
  87.                 }
  88.  
  89.                 $motd = "";
  90.                 $motdraw = "";
  91.  
  92.                 //Evaluate the received data
  93.                 if (substr((String)$data, 3, 5) == "\x00\xa7\x00\x31\x00"){
  94.  
  95.                     $result = explode("\x00", mb_convert_encoding(substr((String)$data, 15), 'UTF-8', 'UCS-2'));
  96.                     $motd = $result[1];
  97.                     $motdraw = $motd;
  98.  
  99.                 } else {
  100.  
  101.                     $result = explode('§', mb_convert_encoding(substr((String)$data, 3), 'UTF-8', 'UCS-2'));
  102.                         foreach ($result as $key => $string) {
  103.                             if($key != sizeof($result)-1 && $key != sizeof($result)-2 && $key != 0) {
  104.                                 $motd .= '§'.$string;
  105.                             }
  106.                         }
  107.                         $motdraw = $motd;
  108.                     }
  109.  
  110.                     $motd = preg_replace("/(§.)/", "", $motd);
  111.                     $motd = preg_replace("/[^[:alnum:][:punct:] ]/", "", $motd);
  112.  
  113.                     $serverdata['version'] = $result[0];
  114.                     $serverdata['players'] = $result[sizeof($result)-2];
  115.                     $serverdata['maxplayers'] = $result[sizeof($result)-1];
  116.                     $serverdata['motd'] = $motd;
  117.                     $serverdata['motd_raw'] = $motdraw;
  118.                     $serverdata['ping'] = $ping;
  119.  
  120.             }
  121.  
  122.             $this->disconnect($socket);
  123.  
  124.             return $serverdata;
  125.  
  126.         }
  127.  
  128.         private function connect($host, $port) {
  129.             $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  130.             if (!@socket_connect($socket, $host, $port)) {
  131.                 $this->disconnect($socket);
  132.                 return false;
  133.             }
  134.             return $socket;
  135.         }
  136.  
  137.         private function disconnect($socket) {
  138.             if($socket != null) {
  139.                 socket_close($socket);
  140.             }
  141.         }
  142.  
  143.         private function read_packet_length($socket) {
  144.             $a = 0;
  145.             $b = 0;
  146.             while(true) {
  147.                 $c = socket_read($socket, 1);
  148.                 if(!$c) {
  149.                     return 0;
  150.                 }
  151.                 $c = Ord($c);
  152.                 $a |= ($c & 0x7F) << $b++ * 7;
  153.                 if( $b > 5 ) {
  154.                     return false;
  155.                 }
  156.                 if(($c & 0x80) != 128) {
  157.                     break;
  158.                 }
  159.             }
  160.             return $a;
  161.         }
  162.  
  163.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement