Advertisement
dead__

Untitled

Jun 12th, 2012
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.23 KB | None | 0 0
  1. <?php
  2.     /* (c) deadinat0r */
  3.     class m2mpQuery {
  4.         protected $sIP, $sPort;
  5.         private $sSocket, $sPing = 0, $sOnline = false;
  6.        
  7.         /* returns ping if connected, otherwise false */
  8.         public function __construct($ip, $port = 27015, $timeout = 2) {
  9.             $this->sIP = $ip;
  10.             $this->sPort = (int)$port+1;
  11.             $this->sSocket = @fsockopen('udp://'.$this->sIP, $this->sPort, $erNo, $erStr);
  12.             if($this->sSocket) {
  13.                 $this->sOnline = true;
  14.                 socket_set_timeout($this->sSocket, $timeout);
  15.                 return $this->getPing();
  16.             }
  17.             return false;
  18.         }
  19.    
  20.         public function __destruct() {
  21.             @fclose($this->sSocket);
  22.         }
  23.        
  24.         public function isOnline() {
  25.             return $this->sOnline;
  26.         }
  27.        
  28.         /* get players, use foreach for listing (see example) */
  29.         public function getPlayers() {
  30.             $r = $this->sendPacket('M2MPl');
  31.            
  32.             $r = str_replace('M2MPl@', '', $r);
  33.             $r = str_replace($this->getInfo('players').'@', '', $r); // not the best solution, but works
  34.             $i = explode('|', $r);
  35.            
  36.             $return = array();
  37.             if(sizeof($i) > 0) {
  38.                 $maxPlayers = $this->getInfo('maxplayers');
  39.                 for($x = 0; $x < $maxPlayers; $x++) {
  40.                     if(@$i[$x]) {
  41.                         $ex = explode('/', $i[$x]);
  42.                         $return[] = array('id' => $ex[0], 'name' => $ex[1], 'level' => $ex[2]);
  43.                     }
  44.                 }
  45.             }
  46.            
  47.             return $return;
  48.         }
  49.        
  50.        
  51.        
  52.         /* possible infos:
  53.             'name' => servername
  54.             'players' => plaayercount
  55.             'maxplayers' => maxplayercount
  56.             'gamemode' => gamemode name
  57.             'password' => password (yes = 1/no = 0)
  58.         */
  59.         public function getInfo($info = '') {
  60.             $r = $this->sendPacket('M2MPi');
  61.             if(!$r) return false;
  62.            
  63.             $r = str_replace('M2MPi@', '', $r);
  64.             $i = explode('@', $r);
  65.  
  66.             $return = array('name' => $i[0], 'players' => $i[1], 'maxplayers' => $i[2], 'gamemode' => $i[3], 'password' => $i[4]);
  67.            
  68.             return ($info === '') ? ($return) : ($return[$info]);
  69.         }
  70.        
  71.         /* get ping */
  72.         public function getPing() {
  73.             if($this->sPing === 0) {
  74.                 $start = microtime(true);
  75.                 $this->sendPacket('M2MPp');
  76.                 $this->sPing = round((microtime(true)-$start) * 1000, 0);
  77.             }
  78.             return $this->sPing;
  79.         }
  80.        
  81.         /* used for sending packets, you can send custom packets directly from your object */
  82.         public function sendPacket($packet) {
  83.             fputs($this->sSocket, $packet);
  84.             $r = fread($this->sSocket, 1024);
  85.             if(!@$r || @$r === '') return false;
  86.             else return $r;
  87.         }
  88.     }
  89.    
  90.     echo '<h3>Some Infos (Server: 176.65.141.8)</h3>';
  91.    
  92.     $query = new m2mpQuery('176.65.141.8'); // Testserver
  93.     if($query !== false) {
  94.         echo 'The server is ';
  95.         if(!$query->isOnline()) echo 'offline';
  96.         else {
  97.             echo 'online';
  98.             echo '<br />';
  99.            
  100.             $info = $query->getInfo();
  101.        
  102.             echo 'Servername: '.$info['name'];
  103.             echo '<br />';
  104.        
  105.             echo 'Gamemode: '.$query->getInfo('gamemode');
  106.             echo '<br />';
  107.        
  108.             echo 'Ping: '.$query->getPing();
  109.             echo '<br /><br />';
  110.  
  111.             echo '<h3>Players</h3>';
  112.             if($info['players'] > 0) {
  113.                 $players = $query ->getPlayers();
  114.                 echo '<table><thead><tr><td>ID</td><td>Name</td><td>Level</td></thead><tbody>';
  115.                 foreach($players as $p) echo '<tr><td>'.$p['id'].'</td><td>'.$p['name'].'</td><td>'.$p['level'].'</td></tr>';
  116.                 echo '</table></tbody>';
  117.             }
  118.             else echo 'No players online.';
  119.         }
  120.     }
  121. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement