Advertisement
dead__

Untitled

Jun 16th, 2012
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.60 KB | None | 0 0
  1. <?php
  2.     /* (c) deadinat0r */
  3.     class m2mpQuery {
  4.         protected $sIP, $sPort;
  5.         private $sSocket, $sPing = 0, $sOnline = false, $sInfo = array();
  6.        
  7.         protected static $instance = NULL;
  8.        
  9.         /* returns ping if connected, otherwise false */
  10.         public function __construct($ip, $port = 27015, $timeout = 2) {
  11.             $this->sIP = $ip;
  12.             $this->sPort = (int)$port;
  13.             $this->sSocket = @fsockopen('udp://'.$this->sIP, $this->sPort+1, $erNo, $erStr, $timeout);
  14.             if($this->sSocket) {
  15.                 if($this->getPing() > 0) {
  16.                     $this->sOnline = true;
  17.                     socket_set_timeout($this->sSocket, $timeout);
  18.                     return $this->getPing();
  19.                     self::$instance = self;
  20.                 }
  21.             }
  22.             return false;
  23.         }
  24.    
  25.         public function __destruct() {
  26.             @fclose($this->sSocket);
  27.         }
  28.        
  29.         public function isOnline() {
  30.             return $this->sOnline;
  31.         }
  32.        
  33.         /* gets players, use foreach for listing (see example) // returns false if no players are online*/
  34.         public function getPlayers() {
  35.             if(!$this->isOnline()) return false;
  36.             if(!$this->getInfo('players')) return false;
  37.            
  38.             if(!$r = $this->sendPacket('M2MPl')) return false;
  39.            
  40.             $r = str_replace($this->getInfo('players').'@', '', $r); // not the best solution, but works
  41.            
  42.             $i = explode('|', $r);
  43.             $return = array();
  44.             if(sizeof($i) > 0) {
  45.                 $maxPlayers = $this->getInfo('maxplayers');
  46.                 for($x = 0; $x < $maxPlayers; $x++) {
  47.                     if(@$i[$x]) {
  48.                         $ex = explode('/', $i[$x]);
  49.                         $return[] = array('id' => $ex[0], 'name' => $ex[1], 'ping' => $ex[2]);
  50.                     }
  51.                 }
  52.             }
  53.            
  54.             return $return;
  55.         }
  56.        
  57.         /* idea by Lordkire (http://www.m2-multiplayer.com/topic/367-php-server-query-class/) */
  58.         public function isPlayerOnline($name) {
  59.             if(!$players = $this->getPlayers()) return false;
  60.            
  61.             foreach($players as $p) {
  62.                 if($p['name'] === $name) return true;
  63.             }
  64.            
  65.             return false;
  66.         }
  67.        
  68.         /* possible infos:
  69.             'name' => servername
  70.             'players' => plaayercount
  71.             'maxplayers' => maxplayercount
  72.             'gamemode' => gamemode name
  73.             'password' => password (yes = 1/no = 0)
  74.         */
  75.         public function getInfo($info = '') {
  76.             if(!$this->isOnline()) return false;
  77.             if(!$this->sInfo) {
  78.                 if(!$r = $this->sendPacket('M2MPi')) return false;
  79.    
  80.                 $i = explode('@', $r);
  81.                 $this->sInfo = array('name' => $i[0], 'players' => $i[1], 'maxplayers' => $i[2], 'gamemode' => $i[3], 'password' => $i[4]);
  82.             }
  83.            
  84.             return ($info === '') ? ($this->sInfo) : ($this->sInfo[$info]);
  85.         }
  86.        
  87.         /* get ping */
  88.         public function getPing() {
  89.             if(!$this->sPing) {
  90.                 $start = microtime(true);
  91.                 if(!$this->sendPacket('M2MPp')) return 0;
  92.                 $this->sPing = round((microtime(true)-$start) * 1000, 0);
  93.             }
  94.            
  95.             return ($this->sPing > 0 && $this->sPing < 2000) ? ($this->sPing) : (0);
  96.         }
  97.        
  98.         /* used for sending packets, you can send custom packets directly from your object */
  99.         public function sendPacket($packet) {
  100.             fputs($this->sSocket, $packet);
  101.             if(!$r = fread($this->sSocket, 1024)) return false;
  102.            
  103.             return trim(str_replace($packet.'@', '', $r), '\x00');
  104.         }
  105.  
  106.         /*
  107.             SINGLETON
  108.                         */
  109.                        
  110.         public static function getInstance($ip, $port = 27015) {
  111.             if($port = -1) $port = 27015;
  112.            
  113.             if(!self::$instance) self::$instance = new self($ip, $port);
  114.                
  115.             return self::$instance;
  116.         }
  117.        
  118.         public static function info($info = '', $ip = -1, $port = 27015) {
  119.             if($port === -1) $port = 27015;
  120.            
  121.             if(!self::$instance) {
  122.                 if($ip === -1) return false;
  123.                 self::$instance = new self($ip, $port);
  124.             }
  125.            
  126.             return self::$instance->getInfo($info);
  127.         }
  128.        
  129.         public static function players($ip = -1, $port = 27015) {
  130.             if($port = -1) $port = 27015;
  131.            
  132.             if(!self::$instance) {
  133.                 if($ip === -1) return false;
  134.                 self::$instance = new self($ip, $port);
  135.             }
  136.            
  137.             return self::$instance->getPlayers();
  138.         }
  139.        
  140.         public static function online($ip = -1, $port = 27015) {
  141.             if($port = -1) $port = 27015;
  142.            
  143.             if(!self::$instance) {
  144.                 if($ip === -1) return false;
  145.                 self::$instance = new self($ip, $port);
  146.             }
  147.            
  148.             return self::$instance->isOnline();
  149.         }
  150.        
  151.         public static function ping($ip = -1, $port = 27015) {
  152.             if($port = -1) $port = 27015;
  153.            
  154.             if(!self::$instance) {
  155.                 if($ip === -1) return false;
  156.                 self::$instance = new self($ip, $port);
  157.             }
  158.            
  159.             return self::$instance->getPing();
  160.         }
  161.        
  162.         public static function playerOnline($name, $ip = -1, $port = 27015) {
  163.             if($port = -1) $port = 27015;
  164.            
  165.             if(!self::$instance) {
  166.                 if($ip === -1) return false;
  167.                 self::$instance = new self($ip, $port);
  168.             }
  169.            
  170.             return self::$instance->isPlayerOnline($name);
  171.         }
  172.     }
  173. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement