Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * Q3Master Quake 3 Master Server Query Class (supports also regular servers queries)
- *
- * Copyright 2009 linkboss <[email protected]>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
- * MA 02110-1301, USA.
- */
- class Q3Master
- {
- private $_socket;
- private $_port = '27950';
- private $_host;
- public function __construct($masterHost)
- {
- $this->_socket = fsockopen('udp://'.$masterHost,27950);
- stream_set_blocking($this->_socket,0);
- }
- public function master_listServers($timeout = '1')
- {
- fputs($this->_socket,str_repeat(chr(255),4).'getservers 68 empty full demo'."\n");
- $time=time()+$timeout;
- while($time > time() || strpos($return,'EOT') === FALSE)
- {
- $return .= fgets($this->_socket);
- }
- $return = explode('\\',$return);
- unset($return[0]);
- unset($return[count($return)]);
- $iplist = array();
- foreach($return as $server)
- {
- for($i = 0;$i < 4;$i++)
- $addr[] = ord($server[$i]);
- for($i = 4;$i < 6;$i++)
- $port .= dechex(ord($server[$i]));
- $port = hexdec($port);
- $iplist[] = array('ip' => join('.',$addr),'port' => $port);
- unset($addr);
- unset($port);
- }
- return $iplist;
- }
- function server_getInfo($adresse, $port)
- {
- if($port != 0)
- {
- $cmd = "\xFF\xFF\xFF\xFF\x02getstatus\x0a\x00";
- $f = fsockopen('udp://'.$adresse, $port);
- socket_set_timeout ($f, 1);
- fwrite ($f, $cmd);
- $data = fread ($f, 10000);
- fclose ($f);
- if($data)
- {
- $temp = explode("\x0a",$data);
- $list3 = explode("\\",substr($temp[1],1,strlen($temp[1])));
- for ($i = 0;$i <= count($list3);$i = $i + 2) {
- $list[@$list3[$i]] = @$list3[$i + 1];
- }
- array_pop($list);//efface la derniere entrée vide
- $players = array();
- foreach($temp as $id => $player)
- {
- if($id != 0 AND $id != 1)
- {
- $infos = explode(' ', $player, 3);
- $name = explode('"', $infos[2]);
- $players[] = array('score' => $infos[0], 'ping' => $infos[1], 'name' => $name[1]);
- }
- }
- array_pop($players);//efface la derniere entré vide
- $infos = array();
- $infos = $list;
- $infos['players'] = $players;
- return $infos;
- }
- else
- return FALSE;
- }
- else
- return FALSE;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment