ChainsBoy

m2mpphpquery

Jun 11th, 2013
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.75 KB | None | 0 0
  1. <?php
  2.  
  3. class m2mp {
  4.    
  5.     private $m_socket, $m_data = array();
  6.    
  7.     public function __construct ( $ip, $port, $timeout = 5 )
  8.     {
  9.         // Open the socket
  10.         $this->m_socket = @fsockopen( 'udp://' . $ip, ($port + 1) );
  11.            
  12.         // Did the socket fail to open?
  13.         if( !$this->m_socket )
  14.             die( 'FAILED TO OPEN UDP SOCKET TO ' . $ip . ':' . $port . '!' );
  15.            
  16.         // Set the socket timeout time
  17.         @stream_set_timeout( $this->m_socket, $timeout );
  18.            
  19.         // Store the ip and port
  20.         $this->m_data[ 'ip' ] = $ip;
  21.         $this->m_data[ 'port' ] = $port;
  22.     }
  23.        
  24.     public function __destruct ( )
  25.     {
  26.         // Close the socket
  27.         @fclose( $this->m_socket );
  28.            
  29.         // Clear the array data
  30.         unset( $this->m_data );
  31.     }
  32.        
  33.     private function Unpack ( $data )
  34.     {
  35.         // Set the initial offset
  36.         $offset = 1;
  37.            
  38.         // Unpack all information from binary data
  39.         $binary = unpack( 'C*', $data );
  40.  
  41.         // Get the hostname from the data
  42.         $len = $binary[ $offset ];
  43.         $this->m_data[ 'hostname' ] = substr( $data, 0, $len );
  44.         $offset += $len;
  45.            
  46.         // Get the player count
  47.         $len = ($binary[ $offset ] - 1);
  48.         $this->m_data[ 'playercount' ] = (int)substr( $data, $offset, $len );
  49.         $offset += ($len + 1);
  50.            
  51.         // Get the max player count
  52.         $len = ($binary[ $offset ] - 1);
  53.         $this->m_data[ 'maxplayers' ] = (int)substr( $data, $offset, $len );
  54.         $offset += ($len + 1);
  55.            
  56.         // Get the gamemode name
  57.         $len = ($binary[ $offset ] - 1);
  58.         $this->m_data[ 'gamemode' ] = substr( $data, $offset, $len );
  59.         $offset += ($len + 1);
  60.        
  61.         // Is the server password protected?
  62.         $this->m_data[ 'password' ] = (bool)$binary[ $offset ];
  63.         $offset += 1;
  64.        
  65.         // Do we have any players in the server?
  66.         if( $this->m_data[ 'playercount' ] > 0 )
  67.         {
  68.             // Create the players array
  69.             $this->m_data[ 'players' ] = array();
  70.            
  71.             // Loop over all players
  72.             for( $i = 0; $i < $this->m_data[ 'playercount' ]; $i++ )
  73.             {
  74.                 // Get the player name
  75.                 $len = ($binary[ $offset ] - 1);
  76.                 array_push( $this->m_data[ 'players' ], substr( $data, $offset, $len ) );
  77.                 $offset += ($len + 1);
  78.             }
  79.         }
  80.        
  81.         // Is this server summer?
  82.         $this->m_data[ 'summer' ] = (bool)$binary[ $offset ];
  83.         $offset += 1;
  84.            
  85.         return $this->m_data;
  86.     }
  87.        
  88.     public function Pulse ( )
  89.     {
  90.         if( !$this->m_socket )
  91.             return false;
  92.            
  93.         // Query the server
  94.         @fputs( $this->m_socket, 'M2MP' );
  95.            
  96.         // Did we fail to read any data?
  97.         if( !$result = @fread( $this->m_socket, 1024 ) )
  98.             return false;
  99.                
  100.         // Is this packet not for m2mp?
  101.         if( $result[0] != 'M' || $result[1] != '2' || $result[2] != 'M' || $result[3] != 'P' )
  102.             return false;
  103.                
  104.         // Remove the identifier from the string
  105.         $result = substr( $result, 4 );
  106.  
  107.         // Unpack all the data
  108.         return $this->Unpack( $result );
  109.     }
  110.    
  111. };
  112.  
  113. ?>
Advertisement
Add Comment
Please, Sign In to add comment