Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* 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.
- (c) deadinat0r
- */
- class m2mpQuery {
- protected $sIP, $sPort;
- private $sSocket, $sPing = 0, $sOnline = false, $sInfo = array();
- protected static $instance = NULL;
- /* returns ping if connected, otherwise false */
- public function __construct($ip, $port = 27015, $timeout = 2) {
- $this->sIP = $ip;
- $this->sPort = (int)$port;
- $this->sSocket = @fsockopen('udp://'.$this->sIP, $this->sPort+1, $erNo, $erStr, $timeout);
- if($this->sSocket) {
- if($this->getPing() > 0) {
- $this->sOnline = true;
- socket_set_timeout($this->sSocket, $timeout);
- return $this->getPing();
- self::$instance = self;
- }
- }
- return false;
- }
- public function __destruct() {
- @fclose($this->sSocket);
- }
- public function isOnline() {
- return $this->sOnline;
- }
- /* gets players, use foreach for listing (see example) // returns false if no players are online*/
- public function getPlayers() {
- if(!$this->isOnline()) return false;
- if(!$this->getInfo('players')) return false;
- if(!$r = $this->sendPacket('M2MPl')) return false;
- $r = str_replace($this->getInfo('players').(($this->checkVersion($r)) ? '<info>' : '@'), '', $r); // not the best solution, but works
- $i = explode(($this->checkVersion($r)) ? ('<player>') : ('|'), $r);
- $return = array();
- if(sizeof($i) > 0) {
- $maxPlayers = $this->getInfo('maxplayers');
- for($x = 0; $x < $maxPlayers; $x++) {
- if(@$i[$x]) {
- $ex = explode(($this->checkVersion($r)) ? ('<data>') : ('/'), $i[$x]);
- $return[] = array('id' => $ex[0], 'name' => $ex[1], 'ping' => $ex[2]);
- }
- }
- }
- return $return;
- }
- /* idea by Lordkire (http://www.m2-multiplayer.com/topic/367-php-server-query-class/) */
- public function isPlayerOnline($name) {
- if(!$players = $this->getPlayers()) return false;
- foreach($players as $p) {
- if($p['name'] === $name) return true;
- }
- return false;
- }
- /* possible infos:
- 'name' => servername
- 'players' => plaayercount
- 'maxplayers' => maxplayercount
- 'gamemode' => gamemode name
- 'password' => password (yes = 1/no = 0)
- 'mapname' => map name
- 'version' => server version string
- 'weburl' => server weburl (specified in config)
- */
- public function getInfo($info = '') {
- if(!$this->isOnline()) return false;
- if(!$this->sInfo) {
- if(!$r = $this->sendPacket('M2MPi')) return false;
- $i = explode(($this->checkVersion($r)) ? ('<info>') : ('@'), $r);
- 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' => '');
- 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]);
- }
- return ($info === '') ? ($this->sInfo) : ($this->sInfo[$info]);
- }
- /* get ping */
- public function getPing($rePing = false) {
- if(!$this->sPing || $rePing) {
- $start = microtime(true);
- if(!$this->sendPacket('M2MPp')) return 0;
- $this->sPing = round((microtime(true)-$start) * 1000, 0);
- }
- return ($this->sPing > 0 && $this->sPing < 2000) ? ($this->sPing) : (0);
- }
- /* used for sending packets, you can send custom packets directly from your object */
- public function sendPacket($packet) {
- fputs($this->sSocket, $packet);
- if(!$r = fread($this->sSocket, 1024)) return false;
- return trim(str_replace($packet.(($this->checkVersion($r)) ? '<info>' : '@'), '', $r), '\x00');
- }
- private function checkVersion($string) {
- if(strpos($string, '<info>') !== false || strpos($string, '<player>') !== false || strpos($string, '<data>') !== false) return 1;
- else return 0;
- }
- /*
- SINGLETON
- */
- public static function getInstance($ip, $port = 27015, $timeout = 2) {
- if($port = -1) $port = 27015;
- if(!self::$instance) self::$instance = new self($ip, $port, $timeout);
- return self::$instance;
- }
- public static function info($info = '', $ip = -1, $port = 27015) {
- if($port === -1) $port = 27015;
- if(!self::$instance) {
- if($ip === -1) return false;
- self::$instance = new self($ip, $port);
- }
- return self::$instance->getInfo($info);
- }
- public static function players($ip = -1, $port = 27015) {
- if($port = -1) $port = 27015;
- if(!self::$instance) {
- if($ip === -1) return false;
- self::$instance = new self($ip, $port);
- }
- return self::$instance->getPlayers();
- }
- public static function online($ip = -1, $port = 27015) {
- if($port = -1) $port = 27015;
- if(!self::$instance) {
- if($ip === -1) return false;
- self::$instance = new self($ip, $port);
- }
- return self::$instance->isOnline();
- }
- public static function ping($ip = -1, $port = 27015) {
- if($port = -1) $port = 27015;
- if(!self::$instance) {
- if($ip === -1) return false;
- self::$instance = new self($ip, $port);
- }
- return self::$instance->getPing();
- }
- public static function playerOnline($name, $ip = -1, $port = 27015) {
- if($port = -1) $port = 27015;
- if(!self::$instance) {
- if($ip === -1) return false;
- self::$instance = new self($ip, $port);
- }
- return self::$instance->isPlayerOnline($name);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement