Advertisement
dead__

Untitled

Jun 17th, 2012
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.98 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.             'mapname' => map name
  75.             'version' => server version string
  76.             'weburl' => server weburl (specified in config)
  77.         */
  78.         public function getInfo($info = '') {
  79.             if(!$this->isOnline()) return false;
  80.             if(!$this->sInfo) {
  81.                 if(!$r = $this->sendPacket('M2MPi')) return false;
  82.    
  83.                 $i = explode('@', $r);
  84.                
  85.                 if(sizeof($i) <= 5) $this->sInfo = array('name' => $i[0], 'players' => (int)$i[1], (int)'maxplayers' => $i[2], 'gamemode' => $i[3], 'password' => (int)$i[4]);
  86.                 else $this->sInfo = array('name' => $i[0], 'players' => (int)$i[1], 'maxplayers' => (int)$i[2], 'gamemode' => $i[3], 'password' => $i[4], 'mapname' => $i[5], 'version' => $i[6], 'weburl' => $i[7]);
  87.             }
  88.            
  89.             return ($info === '') ? ($this->sInfo) : ($this->sInfo[$info]);
  90.         }
  91.        
  92.         /* get ping */
  93.         public function getPing() {
  94.             if(!$this->sPing) {
  95.                 $start = microtime(true);
  96.                 if(!$this->sendPacket('M2MPp')) return 0;
  97.                 $this->sPing = round((microtime(true)-$start) * 1000, 0);
  98.             }
  99.            
  100.             return ($this->sPing > 0 && $this->sPing < 2000) ? ($this->sPing) : (0);
  101.         }
  102.        
  103.         /* used for sending packets, you can send custom packets directly from your object */
  104.         public function sendPacket($packet) {
  105.             fputs($this->sSocket, $packet);
  106.             if(!$r = fread($this->sSocket, 1024)) return false;
  107.            
  108.             return trim(str_replace($packet.'@', '', $r), '\x00');
  109.         }
  110.  
  111.         /*
  112.             SINGLETON
  113.                         */
  114.                        
  115.         public static function getInstance($ip, $port = 27015, $timeout = 2) {
  116.             if($port = -1) $port = 27015;
  117.            
  118.             if(!self::$instance) self::$instance = new self($ip, $port, $timeout);
  119.                
  120.             return self::$instance;
  121.         }
  122.        
  123.         public static function info($info = '', $ip = -1, $port = 27015) {
  124.             if($port === -1) $port = 27015;
  125.            
  126.             if(!self::$instance) {
  127.                 if($ip === -1) return false;
  128.                 self::$instance = new self($ip, $port);
  129.             }
  130.            
  131.             return self::$instance->getInfo($info);
  132.         }
  133.        
  134.         public static function players($ip = -1, $port = 27015) {
  135.             if($port = -1) $port = 27015;
  136.            
  137.             if(!self::$instance) {
  138.                 if($ip === -1) return false;
  139.                 self::$instance = new self($ip, $port);
  140.             }
  141.            
  142.             return self::$instance->getPlayers();
  143.         }
  144.        
  145.         public static function online($ip = -1, $port = 27015) {
  146.             if($port = -1) $port = 27015;
  147.            
  148.             if(!self::$instance) {
  149.                 if($ip === -1) return false;
  150.                 self::$instance = new self($ip, $port);
  151.             }
  152.            
  153.             return self::$instance->isOnline();
  154.         }
  155.        
  156.         public static function ping($ip = -1, $port = 27015) {
  157.             if($port = -1) $port = 27015;
  158.            
  159.             if(!self::$instance) {
  160.                 if($ip === -1) return false;
  161.                 self::$instance = new self($ip, $port);
  162.             }
  163.            
  164.             return self::$instance->getPing();
  165.         }
  166.        
  167.         public static function playerOnline($name, $ip = -1, $port = 27015) {
  168.             if($port = -1) $port = 27015;
  169.            
  170.             if(!self::$instance) {
  171.                 if($ip === -1) return false;
  172.                 self::$instance = new self($ip, $port);
  173.             }
  174.            
  175.             return self::$instance->isPlayerOnline($name);
  176.         }
  177.     }
  178. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement