Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* (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').'@', '', $r); // not the best solution, but works
- $i = explode('|', $r);
- $return = array();
- if(sizeof($i) > 0) {
- $maxPlayers = $this->getInfo('maxplayers');
- for($x = 0; $x < $maxPlayers; $x++) {
- if(@$i[$x]) {
- $ex = explode('/', $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)
- */
- public function getInfo($info = '') {
- if(!$this->isOnline()) return false;
- if(!$this->sInfo) {
- if(!$r = $this->sendPacket('M2MPi')) return false;
- $i = explode('@', $r);
- $this->sInfo = array('name' => $i[0], 'players' => $i[1], 'maxplayers' => $i[2], 'gamemode' => $i[3], 'password' => $i[4]);
- }
- return ($info === '') ? ($this->sInfo) : ($this->sInfo[$info]);
- }
- /* get ping */
- public function getPing() {
- if(!$this->sPing) {
- $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.'@', '', $r), '\x00');
- }
- /*
- SINGLETON
- */
- public static function getInstance($ip, $port = 27015) {
- if($port = -1) $port = 27015;
- if(!self::$instance) self::$instance = new self($ip, $port);
- 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