Location: PHPKode > scripts > San Andreas - Multiplayer Server Query > san-andreas-multiplayer-server-query/san-andreas-class.php
<?

// ------------------------------------------------------------------
// SAN ANDREAS: MULTIPLAYER GAME SERVER QUERY CLASS
// ------------------------------------------------------------------
// Written by: Bogdan Zarchievici <hide@address.com>
// Version: 1.0.0.a
// Revision: Thursday, Friday, May 19, 2006 at 5:46:45 PM
// ------------------------------------------------------------------
// Comments: works with any PHP version greater than 4.1.0.
// ------------------------------------------------------------------
// Special thanks to: Alexandru Burdulea and all of you who have been
// actively interested in the development of this class.
// ------------------------------------------------------------------
// For more details on MTA:SA please visit:
// http://www.sa-mp.com
// http://www.sa-mp.com/blog.php
// http://www.sanandreas.ro
// ------------------------------------------------------------------

class san_andreas
{ var $timeout=3; // packet TTL in seconds

  //
  // MAIN FUNCTION OF THE CLASS (CONSTRUCTOR)
  //

  function san_andreas($ip,$port)
  { $player_count=0;

    # (begin) create packet
    $packet="SAMP";
    $tokens=explode(".",$ip);
    foreach($tokens as $token) $packet=$packet.chr($token);
    $packet=$packet.chr($port&0xFF).chr($port>>8&0xFF);
    # (end) create packet

    # (begin) socket code
    $socket=socket_create(AF_INET,SOCK_DGRAM,SOL_UDP);
    socket_set_option($socket,SOL_SOCKET,SO_RCVTIMEO,array('sec'=>$this->timeout,'usec'=>0));
    socket_connect($socket,$ip,$port);
    if (!socket_write($socket,$packet.'i',strlen($packet.'i'))) return false;
    if (!socket_recvfrom($socket,$raw['i'],1024,0,$client['ip'],$client['port'])) return false;
    if (!socket_write($socket,$packet.'c',strlen($packet.'c'))) return false;
    if (!socket_recvfrom($socket,$raw['c'],1024,0,$client['ip'],$client['port'])) return false;
    socket_close($socket);
    # (end) socket code

    # (begin) fill server array with results
    for ($i=0; $i<strlen($raw['i']); $i++) $server[]=ord(substr($raw['i'],$i,1));
    $this->results['server']['players']=ord(chr($server[12]).chr($server[13]));
    $this->results['server']['max_players']=ord(chr($server[14]).chr($server[15]));
    $this->results['server']['password']=($server[11]?'true':'false');
    $this->results['server']['hostname']=null; for ($i=20; $i<count($server); $i++) if ($server[$i]>12 && $server[$i]<16) { $id=$i+4; break; } else $this->results['server']['hostname'].=chr($server[$i]);
    $this->results['server']['gamemod']=null; for ($i=$id; $i<count($server); $i++) if ($server[$i]==11) { $id=$i+4; break; } else $this->results['server']['gamemod'].=chr($server[$i]);
    $this->results['server']['gametype']=null; for ($i=$id; $i<count($server); $i++) $this->results['server']['gametype'].=chr($server[$i]);
    # (end) fill server array with results

    # (begin) fill players array with results
    for ($i=0; $i<strlen($raw['c']); $i++) $players[]=ord(substr($raw['c'],$i,1));
    array_splice($players,0,13);
    $i=0; $name=null;
    while($i<count($players))
    { if ($players[$i]>=0 && $players[$i]<16)
      { if (strlen($name)>0)
        { $score=null; for($s=$i; $s<$i+4; $s++) $score.=chr($players[$s]);
          $this->results['players'][]=array('player'=>$name,'score'=>ord($score));
          unset($name); $name=null;
        }
      }
      else { $name.=chr($players[$i]); }
      $i++;
    }
    # (end) fill players array with results

    unset($this->timeout);
  }
}

?>
Return current item: San Andreas - Multiplayer Server Query