PHP Classes

Update to query

Recommend this page to a friend!

      Counter-Strike Source Query  >  All threads  >  Update to query  >  (Un) Subscribe thread alerts  
Subject:Update to query
Summary:This updated cssource_class.php works
Messages:5
Author:John Doe
Date:2007-02-17 11:58:31
Update:2009-09-26 22:53:49
 

  1. Update to query   Reply   Report abuse  
Picture of John Doe John Doe - 2007-02-17 11:58:31
Valve updated the query protocol and the author of this script hasn't updated his files, so here is an updated version I put together. Just overwrite your cssource_class.php file with this code:

<?php
class cssource {
var $ip;
var $port;
var $timeout;
var $sock;
var $data;
var $output = array();
var $sortBy;

function cssource($ip, $port="", $timeout=200) {
$this->ip = $ip;
$this->port = (empty($port)) ? 27015 : $port;
$this->timeout = $timeout;
}

function get_challenge()
{
socket_set_timeout($this->sock, 2);
// get challenge number from server
fwrite($this->sock, "\xFF\xFF\xFF\xFF\x57");
$challenge = fread($this->sock, 4096);
$challenge = substr($challenge, 5, 4);
return $challenge;
}

function status() {
if(!$this->sock = @fsockopen("udp://".$this->ip, $this->port, $errno, $errstr, 30)) {
die("Could not connect to the socket.");
}
else {
$querys[] = "\xFF\xFF\xFF\xFFTSource Engine Query\x00";
$challenge = $this->get_challenge($this->sock);
$querys[] = "\xFF\xFF\xFF\xFF\x55" . $challenge;
$querys[] = "\xFF\xFF\xFF\xFF\x56" . $challenge;

$wait = 0;
foreach($querys AS $querystring) {
fwrite($this->sock, $querystring);
while($wait < $this->timeout) {
$string = fread($this->sock, 4096);
$wait += 10;

if(!empty($string)) {
$this->data[] = $string;
if(strlen($string) < 4096)
break;
}
}
}
@fclose($this->sock);

if(empty($this->data[0]))
die("No data was returned from the query.");

$this->parseStatus();
$this->parsePlayers();
$this->parseRules();

return $this->output;
}
}

function parseStatus() {
$status = $this->data[0];
$i = 5;

$this->output['net_protocol'] = ord($status{$i++});
$this->output['ip'] = $this->ip;
$this->output['port'] = $this->port;
$this->output['hostname'] = $this->readString($status, $i);
$this->output['map'] = $this->readString($status, $i);
$this->output['game_dir'] = $this->readString($status, $i);
$this->output['game_type'] = $this->readString($status, $i);
$this->output['appid'] = ord($status{$i++}.$status{$i++});
$this->output['num_players'] = ord($status{$i++});
$this->output['max_players'] = ord($status{$i++});
$this->output['bot_players'] = ord($status{$i++});
$this->output['dedicated'] = $status{$i++};
$this->output['server_os'] = $status{$i++};
$this->output['needpass'] = ord($status{$i++});
$this->output['secure'] = ord($status{$i++});

return TRUE;
}

function parsePlayers() {
$players = $this->data[1];
$i = 4;
$num_players = ord($players{$i++});

$this->output['players'] = array();
if($num_players > 0) {
for($p = 0; $p < $num_players-1; $p++) {
if(!empty($players{$i+1})) {
$this->output['players'][$p]['index'] = ord($players{$i++});
$this->output['players'][$p]['name'] = $this->readString($players, $i);

$frags = unpack("L", substr($players, $i, $i+4)); $i+=4;
$this->output['players'][$p]['frags'] = $frags[1];

$time = unpack("f", substr($players, $i, $i+4)); $i+=4;
$time = mktime(0, 0, $time[1]);
//$time = date("H:i:s", $time);
$this->output['players'][$p]['time'] = $time;
}
}
}

$this->sortPlayers($this->sortBy);

return TRUE;
}

function parseRules() {
$rules = $this->data[2];
$rule = explode("\x00", $rules);
$num_rules = count($rule);

for($i = 2; $i < $num_rules; $i+=2)
$this->output['rules'][$rule[$i-1]] = $rule[$i];

return TRUE;
}

function readString($string, &$i) {
$begin = $i;
$strlen = strlen($string);
for ($i; ($i < $strlen) && ($string{$i} != chr(0)); $i++);
$result = substr($string, $begin, $i-$begin);
$i++;

return $result;
}

function sortPlayers($sortvar="index") {
$players = $this->output['players'];
$num_players = count($players);

for($i = 0; $i != $num_players; $i++) {
$a = $i;
$b = $num_players-1;
while ($a != $b){
if ($players[$a][$sortvar] > $players[$b][$sortvar])
$b--;
else
$a++;
}
$h = $players[$i];
$players[$i] = $players[$a];
$players[$a] = $h;
}
$this->output['players'] = $players;

return TRUE;
}

function setSortBy($sortvar) {
$this->sortBy = $sortvar;
}
}
?>

  2. Re: Update to query   Reply   Report abuse  
Picture of cata cata - 2007-03-12 12:55:56 - In reply to message 1 from John Doe
hi there!
thanks for update!

can u help me to include Counter Strike(HL) querying too?

sorry for my bad english! i'm waiting for a reply...:|

cheers

  3. Re: Update to query   Reply   Report abuse  
Picture of Adam B Adam B - 2008-07-29 07:55:36 - In reply to message 1 from John Doe
Thank you very much for your working update :)

-Adam

  4. Re: Update to query   Reply   Report abuse  
Picture of Ilja Ilja - 2009-02-03 15:45:34 - In reply to message 1 from John Doe
Excuse, I do not know English language...
Try a server 77.220.180.17:27015
Errors...

  5. Re: Update to query   Reply   Report abuse  
Picture of shigs shigs - 2009-09-26 22:53:49 - In reply to message 1 from John Doe
nice woik