Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 2.43 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?
  2. //* Made by Kofel (kofels@gmail.com)
  3. //* Under GPL
  4. //* Writed in PHP5 and used SimpleXML
  5.  
  6. //well, this version is refactored
  7.  
  8. class ServerStatus
  9. {
  10.     protected $IP, $port;
  11.    
  12.     public $rawData;
  13.    
  14.     public function __construct( $IP, $port = 7171 )
  15.     {
  16.         $this->IP = $IP;
  17.         $this->port = (int)$port;
  18.     }
  19.    
  20.     public function update()
  21.     {
  22.                 $this->rawData = "";
  23.         $socket = fsockopen( $this->IP, $this->port, $e, $ee, 2 );
  24.        
  25.         if( !$socket ) return false;
  26.        
  27.         fwrite( $socket, chr(6) . chr(0) . chr(255) . chr(255) . 'info' );
  28.        
  29.         while( !feof( $socket ) )
  30.         {
  31.             $this->rawData .= fgets( $socket, 64 );
  32.         }
  33.        
  34.         fclose( $socket );
  35.        
  36.         return true;
  37.     }
  38.  
  39.         public function parse()
  40.         {
  41.                 if( empty( $this->rawData ) )
  42.                         throw new BadMethodCallException('No raw data found. Please call update before. Make sure, that you are not doing too much requests, because OTS have to refuse often requests from same IP');
  43.                
  44.                 $xml=new SimpleXMLElement($this->rawData);
  45.                 $tmp=array();
  46.                 $tmp['serverinfo']['uptime']=(int)$xml->serverinfo->attributes()->uptime;
  47.                 $tmp['serverinfo']['ip']=(string)$xml->serverinfo->attributes()->ip;
  48.                 $tmp['serverinfo']['name']=(string)$xml->serverinfo->attributes()->servername;
  49.                 $tmp['serverinfo']['port']=(int)$xml->serverinfo->attributes()->port;
  50.                 $tmp['serverinfo']['location']=(string)$xml->serverinfo->attributes()->location;
  51.                 $tmp['serverinfo']['site']=(string)$xml->serverinfo->attributes()->url;
  52.                 $tmp['serverinfo']['server']=(string)$xml->serverinfo->attributes()->server;
  53.                 $tmp['serverinfo']['version']=(int)$xml->serverinfo->attributes()->version;
  54.                 $tmp['serverinfo']['client']=(int)$xml->serverinfo->attributes()->client;
  55.                 $tmp['owner']['name']=(string)$xml->owner->attributes()->name;
  56.                 $tmp['owner']['email']=(string)$xml->owner->attributes()->email;
  57.                 $tmp['players']['online']=(int)$xml->players->attributes()->online;
  58.                 $tmp['players']['max']=(int)$xml->players->attributes()->max;
  59.                 $tmp['players']['peak']=(int)$xml->players->attributes()->peak;
  60.                 $tmp['monsters']['total']=(int)$xml->monsters->attributes()->total;
  61.                 $tmp['map']['name']=(string)$xml->map->attributes()->name;
  62.                 $tmp['map']['author']=(string)$xml->map->attributes()->author;
  63.                 $tmp['map']['width']=(int)$xml->map->attributes()->width;
  64.                 $tmp['map']['height']=(int)$xml->map->attributes()->height;
  65.                 $tmp['motd']=(string)$xml->motd;
  66.                 return $tmp;
  67.         }
  68. }