Advertisement
Darkwater

Minecraft RCon Class

Sep 16th, 2012
2,535
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.50 KB | None | 0 0
  1. <?php
  2. class MinecraftRconException extends Exception
  3. {
  4.     // Exception thrown by MinecraftRcon class
  5. }
  6.  
  7. class MinecraftRcon
  8. {
  9.     /*
  10.      * Class written by xPaw
  11.      *
  12.      * Website: http://xpaw.ru
  13.      * GitHub: https://github.com/xPaw/PHP-Minecraft-Query
  14.      *
  15.      * Protocol: https://developer.valvesoftware.com/wiki/Source_RCON_Protocol
  16.      */
  17.    
  18.     // Sending
  19.     const SERVERDATA_EXECCOMMAND    = 2;
  20.     const SERVERDATA_AUTH           = 3;
  21.    
  22.     // Receiving
  23.     const SERVERDATA_RESPONSE_VALUE = 0;
  24.     const SERVERDATA_AUTH_RESPONSE  = 2;
  25.    
  26.     private $Socket;
  27.     private $RequestId;
  28.    
  29.     public function __destruct( )
  30.     {
  31.         $this->Disconnect( );
  32.     }
  33.    
  34.     public function Connect( $Ip, $Port = 25575, $Password, $Timeout = 3 )
  35.     {
  36.         $this->RequestId = 0;
  37.        
  38.         if( $this->Socket = FSockOpen( $Ip, (int)$Port ) )
  39.         {
  40.             Socket_Set_TimeOut( $this->Socket, $Timeout );
  41.            
  42.             if( !$this->Auth( $Password ) )
  43.             {
  44.                 $this->Disconnect( );
  45.                
  46.                 throw new Exception( "Authorization failed." );
  47.             }
  48.         }
  49.         else
  50.         {
  51.             throw new Exception( "Can't open socket." );
  52.         }
  53.     }
  54.    
  55.     public function Disconnect( )
  56.     {
  57.         if( $this->Socket )
  58.         {
  59.             FClose( $this->Socket );
  60.            
  61.             $this->Socket = null;
  62.         }
  63.     }
  64.    
  65.     public function Command( $String )
  66.     {
  67.         if( !$this->WriteData( self :: SERVERDATA_EXECCOMMAND, $String ) )
  68.         {
  69.             return false;
  70.         }
  71.        
  72.         $Data = $this->ReadData( );
  73.        
  74.         if( $Data[ 'RequestId' ] < 1 || $Data[ 'Response' ] != self :: SERVERDATA_RESPONSE_VALUE )
  75.         {
  76.             return false;
  77.         }
  78.        
  79.         return $Data[ 'String' ];
  80.     }
  81.    
  82.     private function Auth( $Password )
  83.     {
  84.         if( !$this->WriteData( self :: SERVERDATA_AUTH, $Password ) )
  85.         {
  86.             return false;
  87.         }
  88.        
  89.         $Data = $this->ReadData( );
  90.        
  91.         return $Data[ 'RequestId' ] > -1 && $Data[ 'Response' ] == self :: SERVERDATA_AUTH_RESPONSE;
  92.     }
  93.    
  94.     private function ReadData( )
  95.     {
  96.         $Packet = Array( );
  97.        
  98.         $Size = FRead( $this->Socket, 4 );
  99.         $Size = UnPack( 'V1Size', $Size );
  100.         $Size = $Size[ 'Size' ];
  101.        
  102.         // TODO: Add multiple packets (Source)
  103.        
  104.         $Packet = FRead( $this->Socket, $Size );
  105.         $Packet = UnPack( 'V1RequestId/V1Response/a*String/a*String2', $Packet );
  106.        
  107.         return $Packet;
  108.     }
  109.    
  110.     private function WriteData( $Command, $String = "" )
  111.     {
  112.         // Pack the packet together
  113.         $Data = Pack( 'VV', $this->RequestId++, $Command ) . $String . "\x00\x00\x00";
  114.        
  115.         // Prepend packet length
  116.         $Data = Pack( 'V', StrLen( $Data ) ) . $Data;
  117.        
  118.         $Length = StrLen( $Data );
  119.        
  120.         return $Length === FWrite( $this->Socket, $Data, $Length );
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement