Guest User

Untitled

a guest
Feb 23rd, 2011
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.24 KB | None | 0 0
  1. <?php
  2. /*
  3.  *      Q3Master Quake 3 Master Server Query Class (supports also regular servers queries)
  4.  *      
  5.  *      Copyright 2009 linkboss <[email protected]>
  6.  *      
  7.  *      This program is free software; you can redistribute it and/or modify
  8.  *      it under the terms of the GNU General Public License as published by
  9.  *      the Free Software Foundation; either version 2 of the License, or
  10.  *      (at your option) any later version.
  11.  *      
  12.  *      This program is distributed in the hope that it will be useful,
  13.  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  *      GNU General Public License for more details.
  16.  *      
  17.  *      You should have received a copy of the GNU General Public License
  18.  *      along with this program; if not, write to the Free Software
  19.  *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  20.  *      MA 02110-1301, USA.
  21.  */
  22. class Q3Master
  23. {
  24.     private $_socket;
  25.     private $_port = '27950';
  26.     private $_host;
  27.    
  28.     public function __construct($masterHost)
  29.     {
  30.         $this->_socket = fsockopen('udp://'.$masterHost,27950);
  31.         stream_set_blocking($this->_socket,0);
  32.     }
  33.    
  34.     public function master_listServers($timeout = '1')
  35.     {
  36.         fputs($this->_socket,str_repeat(chr(255),4).'getservers 68 empty full demo'."\n");
  37.        
  38.         $time=time()+$timeout;
  39.         while($time > time() || strpos($return,'EOT') === FALSE)
  40.         {
  41.             $return .= fgets($this->_socket);
  42.         }
  43.         $return = explode('\\',$return);
  44.         unset($return[0]);
  45.         unset($return[count($return)]);
  46.         $iplist = array();
  47.         foreach($return as $server)
  48.         {
  49.             for($i = 0;$i < 4;$i++)
  50.                 $addr[] = ord($server[$i]);
  51.                
  52.             for($i = 4;$i < 6;$i++)
  53.                 $port .= dechex(ord($server[$i]));
  54.             $port = hexdec($port);
  55.             $iplist[] = array('ip' => join('.',$addr),'port' => $port);
  56.             unset($addr);
  57.             unset($port);
  58.         }
  59.        
  60.         return $iplist;
  61.     }
  62.    
  63.     function server_getInfo($adresse, $port)
  64.     {
  65.         if($port != 0)
  66.         {
  67.             $cmd = "\xFF\xFF\xFF\xFF\x02getstatus\x0a\x00";
  68.             $f = fsockopen('udp://'.$adresse, $port);
  69.            
  70.             socket_set_timeout ($f, 1);
  71.             fwrite ($f, $cmd);
  72.             $data = fread ($f, 10000);
  73.             fclose ($f);
  74.            
  75.          if($data)
  76.          {
  77.             $temp = explode("\x0a",$data);
  78.            
  79.             $list3 = explode("\\",substr($temp[1],1,strlen($temp[1])));
  80.             for ($i = 0;$i <= count($list3);$i = $i + 2) {
  81.                $list[@$list3[$i]] = @$list3[$i + 1];
  82.             }
  83.             array_pop($list);//efface la derniere entrée vide
  84.            
  85.             $players = array();
  86.             foreach($temp as $id => $player)
  87.             {
  88.                if($id != 0 AND $id != 1)
  89.                {
  90.                   $infos = explode(' ', $player, 3);
  91.                   $name = explode('"', $infos[2]);
  92.                   $players[] = array('score' => $infos[0], 'ping' => $infos[1], 'name' => $name[1]);
  93.                }
  94.             }
  95.             array_pop($players);//efface la derniere entré vide
  96.            
  97.             $infos = array();
  98.             $infos = $list;
  99.             $infos['players'] = $players;
  100.            
  101.             return $infos;
  102.          }
  103.          else
  104.             return FALSE;
  105.         }
  106.         else
  107.             return FALSE;
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment