Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class MinecraftQueryException extends Exception
- {
- // Exception thrown by MinecraftQuery class
- }
- class MinecraftQuery
- {
- /*
- * Class written by xPaw
- *
- * Website: http://xpaw.ru
- * GitHub: https://github.com/xPaw/PHP-Minecraft-Query
- */
- private $Socket;
- private $Challenge;
- private $Players;
- private $Info;
- public function Connect( $Ip = '88.191.242.16', $Port = 10252, $Timeout = 3 )
- {
- if( $this->Socket = FSockOpen( 'udp://' . $Ip, (int)$Port ) )
- {
- Socket_Set_TimeOut( $this->Socket, $Timeout );
- if( !$this->GetChallenge( ) )
- {
- FClose( $this->Socket );
- throw new MinecraftQueryException( "Failed to receive challenge." );
- }
- if( !$this->GetStatus( ) )
- {
- FClose( $this->Socket );
- throw new MinecraftQueryException( "Failed to receive status." );
- }
- FClose( $this->Socket );
- }
- else
- {
- throw new MinecraftQueryException( "Can't open connection." );
- }
- }
- public function GetInfo( )
- {
- return isset( $this->Info ) ? $this->Info : false;
- }
- public function GetPlayers( )
- {
- return isset( $this->Players ) ? $this->Players : false;
- }
- private function GetChallenge( )
- {
- $Data = $this->WriteData( "\x09" );
- if( !$Data )
- {
- return false;
- }
- $this->Challenge = Pack( 'N', $Data );
- return true;
- }
- private function GetStatus( )
- {
- $Data = $this->WriteData( "\x00", $this->Challenge . "\x01\x02\x03\x04" );
- if( !$Data )
- {
- return false;
- }
- $Last = "";
- $Info = Array( );
- $Data = SubStr( $Data, 11 ); // splitnum + 2 int
- $Data = Explode( "\x00\x00\x01player_\x00\x00", $Data );
- $Players = SubStr( $Data[ 1 ], 0, -2 );
- $Data = Explode( "\x00", $Data[ 0 ] );
- // Array with known keys in order to validate the result
- // It can happen that server sends custom strings containing bad things (who can know!)
- $Keys = Array(
- 'hostname' => 'HostName',
- 'gametype' => 'GameType',
- 'version' => 'Version',
- 'map' => 'Map',
- 'numplayers' => 'Players',
- 'maxplayers' => 'MaxPlayers',
- 'hostport' => 'HostPort',
- 'hostip' => 'HostIp'
- );
- foreach( $Data as $Key => $Value )
- {
- if( ~$Key & 1 )
- {
- if( !Array_Key_Exists( $Value, $Keys ) )
- {
- $Last = false;
- continue;
- }
- $Last = $Keys[ $Value ];
- $Info[ $Last ] = "";
- }
- else if( $Last != false )
- {
- $Info[ $Last ] = $Value;
- }
- }
- // Ints
- $Info[ 'Players' ] = IntVal( $Info[ 'Players' ] );
- $Info[ 'MaxPlayers' ] = IntVal( $Info[ 'MaxPlayers' ] );
- $Info[ 'HostPort' ] = IntVal( $Info[ 'HostPort' ] );
- // Parse "plugins", if any
- if( $Info[ 'Plugins' ] )
- {
- $Data = Explode( ": ", $Info[ 'Plugins' ], 2 );
- $Info[ 'RawPlugins' ] = $Info[ 'Plugins' ];
- $Info[ 'Software' ] = $Data[ 0 ];
- if( Count( $Data ) == 2 )
- {
- $Info[ 'Plugins' ] = Explode( "; ", $Data[ 1 ] );
- }
- }
- else
- {
- $Info[ 'Software' ] = 'Vanilla';
- }
- $this->Info = $Info;
- if( $Players )
- {
- $this->Players = Explode( "\x00", $Players );
- }
- return true;
- }
- private function WriteData( $Command, $Append = "" )
- {
- $Command = "\xFE\xFD" . $Command . "\x01\x02\x03\x04" . $Append;
- $Length = StrLen( $Command );
- if( $Length !== FWrite( $this->Socket, $Command, $Length ) )
- {
- return false;
- }
- $Data = FRead( $this->Socket, 1440 );
- if( StrLen( $Data ) < 5 || $Data[ 0 ] != $Command[ 2 ] )
- {
- return false;
- }
- return SubStr( $Data, 5 );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement