Hmm, Det bliver en længere smører..
----
counterstrikeclass.php
----
<?
function fragsort ($a, $b) {
if ($a["frags"] == $b["frags"]) return 0;
if ($a["frags"] > $b["frags"]) {
return -1;
} else {
return 1;
}
}
Class CounterStrike {
var $m_playerinfo ="";
var $m_servervars ="";
var $m_serverrules ="";
function timenow() {
return doubleval(ereg_replace('^0\.([0-9]*) ([0-9]*)$','\\2.\\1',microtime()));
}
function getServerData($command,$serveraddress,$portnumber,$waittime) {
$serverdata ="";
$serverdatalen=0;
if ($waittime< 500) $waittime= 500;
if ($waittime>2000) $waittime=2000;
$waittime=doubleval($waittime/1000.0);
if (!$cssocket=fsockopen("
udp://".$serveraddress,$portnumber,$errnr)) {
$this->errmsg="No connection";
return "";
}
socket_set_blocking($cssocket,true);
fwrite($cssocket,$command,strlen($command));
$starttime=$this->timenow();
do {
$serverdata.=fgetc($cssocket);
$serverdatalen++;
$socketstatus=socket_get_status($cssocket);
if ($this->timenow()>($starttime+$waittime)) {
$this->errmsg="Connection timed out";
fclose($cssocket);
return "";
}
} while ($socketstatus["unread_bytes"] );
fclose($cssocket);
return $serverdata;
}
function getnextstring(&$data) {
$temp="";
$counter=0;
while (ord($data[$counter++])!=0) $temp.=$data[$counter-1];
$data=substr($data,strlen($temp)+1);
return $temp;
}
function getnextbytevalue(&$data) {
$temp=ord($data[0]);
$data=substr($data,1);
return $temp;
}
function getnextfragvalue(&$data) {
$frags=ord($data[0])+(ord($data[1])<<8)+(ord($data[2])<<16)+(ord($data[3])<<24);
if ($frags>=4294967294) $frags-=4294967296;
$data=substr($data,4);
return $frags;
}
function getnextplaytime(&$data) {
$decnumber=ord($data[0])+(ord($data[1])<<8)+(ord($data[2])<<16)+(ord($data[3])<<24);
$binnumber=base_convert($decnumber,10,2);
while (strlen($binnumber) < 32) $binnumber="0".$binnumber;
$exp=abs(base_convert(substr($binnumber,1,8),2,10))-127;
if (substr($binnumber,0,1)=="1") $exp=0-$exp;
$man=1;$manadd=0.5;
for ($counter=9;$counter<32;$counter++) {
if (substr($binnumber,$counter,1)=="1") $man+=$manadd;
$manadd=$manadd/2;
}
$time=round(pow(2,$exp)*$man);
$playtime="";
if ($time>3600) {
$playtime=sprintf("%2dh",$time/3600);
}
$time%=3600;
$playtime=$playtime.sprintf("%2dm",$time/60);
$time%=60;
$playtime=$playtime.sprintf("%2ds",$time);
$data=substr($data,5);
return $playtime;
}
function getServerRules($serveraddress,$portnumber,$waittime) {
$cmd="\xFF\xFF\xFF\xFFrules\x00";
$serverdata=$this->getServerData($cmd,$serveraddress,$portnumber,$waittime) ;
if (strlen($serverdata)<5) return false;
$rules=(ord($serverdata[5]))+(ord($serverdata[6])*256);
if ($rules!=0) {
$serverdata=substr($serverdata,7);
for ($i=1;$i<=$rules;$i++) {
$rulename =$this->getnextstring($serverdata);
$rulevalue =$this->getnextstring($serverdata);
$this->m_serverrules[$rulename]=$rulevalue;
}
return true;
} else {
return false;
}
}
function getServerInfo($serveraddress,$portnumber,$waittime) {
$cmd="\xFF\xFF\xFF\xFFinfo\x00";
$serverdata=$this->getServerData($cmd,$serveraddress,$portnumber,$waittime) ;
if (strlen($serverdata)<5) return false;
$serverdata=substr($serverdata,5);
$this->m_servervars["serveraddress"] =$this->getnextstring($serverdata);
$this->m_servervars["servername"] =$this->getnextstring($serverdata);
$this->m_servervars["mapname"] =$this->getnextstring($serverdata);
$this->m_servervars["game"] =$this->getnextstring($serverdata);
$this->m_servervars["gamename"] =$this->getnextstring($serverdata);
$this->m_servervars["currentplayers"] =$this->getnextbytevalue($serverdata);
$this->m_servervars["maxplayers"] =$this->getnextbytevalue($serverdata);
return true;
}
function getServerPlayers($serveraddress,$portnumber,$waittime) {
$cmd="\xFF\xFF\xFF\xFFplayers\x00";
$serverdata=$this->getServerData($cmd,$serveraddress,$portnumber,$waittime);
if (strlen($serverdata)<5) return false;
$players=ord($serverdata[5]);
$serverdata=substr($serverdata,7);
for ($i=1;$i<=$players;$i++) {
$playername =htmlspecialchars($this->getnextstring($serverdata));
$frags =$this->getnextfragvalue($serverdata);
$playtime =$this->getnextplaytime($serverdata);
$this->m_playerinfo[$i] =array("name"=>$playername,"frags"=>$frags,"time"=>$playtime);
}
if ($players>1) usort($this->m_playerinfo,"fragsort");
return true;
}
}
?>
----
serverlist.php
----
<? require("cs/counterstrikeClass.php");?>
<?
$fp = fopen("cs/klist.lst", r);
$line = file("cs/klist.lst");
$server = array();
for($i=0;$i<count($line);$i++){
$server[$i] = new CounterStrike;
$serveradr =substr($line[$i],0,strpos($line[$i],":"));
$serverport=substr($line[$i],strpos($line[$i],":")+1);
$status=$server[$i]->getServerInfo($serveradr,$serverport,1000);
if ($status) {
?>
<?
}
}
?>