Advertisement
dead__

Untitled

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