Når jeg skal skrive nogle stats fra mysql, så udskrives alle ved mine data for serverID 3 første repeat, men ved mit andet og 3 repeat, så udskrives alle data for serverID 2 og 1 ikke:
fil: binary_funcs
<?php
/*
* HLstats - Real-time player and clan rankings and statistics for Half-Life
* http://sourceforge.net/projects/hlstats/
*
* Copyright (C) 2001 Simon Garner
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
// Binary functions.
// Pass the functions the whole lot of data and the point at which
// the function is to begin working from
// TODO: These functions need signed versions creating ASAP
function GetInt8($data, &$datastart)
{
$temp = '';
$temp = ord($data{$datastart});
$datastart += 1;
return $temp;
}
function GetBoolean($data, &$datastart)
{
$temp = '';
$temp = GetInt8($data, $datastart);
if($temp >= 1)
return true;
else
return false;
}
function GetInt16($data, &$datastart)
{
$temp = '';
$temp = GetInt8($data, $datastart) + (GetInt8($data, $datastart) * 256);
return $temp;
}
function GetInt32($data, &$datastart)
{
$temp = '';
$temp = GetInt8($data, $datastart) + (GetInt8($data, $datastart)<<8) + (GetInt8($data, $datastart)<<16) + (GetInt8($data, $datastart)<<24);
return $temp;
}
// Null-Terminated String
function GetString($data, &$datastart)
{
$temp = '';
$counter = 0;
while (ord($data[$datastart+$counter++]) != 0)
$temp .= $data[$datastart+$counter-1];
$datastart += strlen($temp) + 1;
return $temp;
}
function GetFloat32($data, &$datastart)
{
$decnumber = GetInt32($data, $datastart);
$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;
$mantissa = 1;
$mantadd = 0.5;
for ($counter = 9; $counter < 32; $counter++)
{
if(substr($binnumber, $counter, 1) == 1)
$mantissa += $mantadd;
$mantadd = $mantadd / 2;
}
$temp = round(pow(2, $exp) * $mantissa);
return $temp;
}
?>
fil: db
<?php
/*
* HLstats - Real-time player and clan rankings and statistics for Half-Life
* http://sourceforge.net/projects/hlstats/
*
* Copyright (C) 2001 Simon Garner
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
//
// DB_mysql
//
// Database abstraction class for MySQL databases.
//
class DB_mysql
{
var $db_addr;
var $db_user;
var $db_pass;
var $db_name;
var $link;
var $result;
var $rowdata;
var $insert_id;
var $numrows;
var $query;
function DB_mysql ($db_addr=-1, $db_user=-1, $db_pass=-1, $db_name=-1)
{
if ($db_addr == -1) $db_addr = DB_ADDR;
if ($db_user == -1) $db_user = DB_USER;
if ($db_pass == -1) $db_pass = DB_PASS;
if ($db_name == -1) $db_name = DB_NAME;
$this->db_addr = $db_addr;
$this->db_user = $db_user;
$this->db_pass = $db_pass;
$this->db_name = $db_name;
if (DB_PCONNECT == true)
{
$connectfunc = "mysql_pconnect";
}
else
{
$connectfunc = "mysql_connect";
}
$this->link = $connectfunc($db_addr, $db_user, $db_pass)
or $this->error("Could not connect to database server. Check that the values of DB_ADDR, DB_USER and DB_PASS in hlstats.php are set correctly.");
mysql_select_db($db_name, $this->link)
or $this->error("Could not select database '$db_name'. Check that the value of DB_NAME in hlstats.php is set correctly.");
}
function data_seek ($row_number, $result=-1)
{
if ($result < 0) $result = $this->result;
return mysql_data_seek($result, $row_number);
}
function fetch_array ($result=-1)
{
if ($result < 0) $result = $this->result;
$this->rowdata = mysql_fetch_array($result);
return $this->rowdata;
}
function fetch_row ($result=-1)
{
if ($result < 0) $result = $this->result;
$this->rowdata = mysql_fetch_row($result);
return $this->rowdata;
}
function free_result ($result=-1)
{
if ($result < 0) $result = $this->result;
return mysql_free_result($result);
}
function insert_id ()
{
$this->insert_id = mysql_insert_id($this->link);
return $this->insert_id;
}
function num_rows ($result=-1)
{
if ($result < 0) $result = $this->result;
$this->numrows = mysql_num_rows($result);
return $this->numrows;
}
function query ($query, $showerror=true)
{
global $db_debug;
$this->query = $query;
$this->result = mysql_query($query, $this->link);
if ($db_debug)
{
echo "<p><pre>$query</pre><hr></p>";
}
if (!$this->result)
{
if ($showerror)
{
$this->error("Bad query.");
}
else
{
return 0;
}
}
return $this->result;
}
function result ($row, $field, $result=-1)
{
if ($result < 0) $result = $this->result;
return mysql_result($result, $row, $field);
}
function error ($message, $exit=true)
{
error(
"<b>Database Error</b><br>\n<br>\n" .
"<i>Server Address:</i> $this->db_addr<br>\n" .
"<i>Server Username:</i> $this->db_user<p>\n" .
"<i>Error Diagnostic:</i><br>\n$message<p>\n" .
"<i>Server Error:</i> (" . mysql_errno() . ") " . mysql_error() . "<p>\n" .
"<i>Last SQL Query:</i><br>\n<pre><font size=2>$this->query</font></pre>",
$exit
);
}
function dberror ()
{
return mysql_error();
}
}
?>
fil: hlquery_funcs
<?php
/*
* HLstats - Real-time player and clan rankings and statistics for Half-Life
* http://sourceforge.net/projects/hlstats/
*
* Copyright (C) 2001 Simon Garner
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
// Half-Life Query Functions
// These functions with the use of Detritus' binary functions allow HL servers to be queried
// and data to be returned in a friendly readable format
function GetServerData($command, $ip, $port)
{
$data = '';
if (!$server_connection = fsockopen('udp://'.$ip, $port))
return false;
socket_set_blocking($server_connection, 1);
# Time out after 5 seconds
socket_set_timeout($server_connection, 5);
fwrite($server_connection, $command, strlen($command));
# This must time out first to make sure the right arguement is returned
# The above timeout is just to make sure it doesn't go over 4 seconds
$timeout = microtime();
$timeout += 4;
$formated_data = '';
do
{
# Rough explanation
# First loop is a hack to force packet scanning
# for the Valve split packet bug
$packetsfound = 0;
$packets = 0;
$packet_data = array();
do
{
# Second loop is a loop to keep grabbing packets
# when split packets are not bugged
$data = '';
do
{
# Third loop keeps scanning for data
# once we have found a packet
$data .= fread($server_connection, 8192);
if (microtime() > $timeout)
{
# Connection timed out
fclose($server_connection);
return false;
}
$data_status = socket_get_status($server_connection);
}
while ($data_status['unread_bytes']);
if (GetInt8($data, $tmp = 0) == 254)
{
# We have a split packet!
$datacounter = 0;
# Type
GetInt32($data, $datacounter);
# Packet ID
GetInt32($data, $datacounter);
$split_packet_info = str_pad(decbin(GetInt8($data, $datacounter)), 8, 0, STR_PAD_LEFT);
$packet_num = bindec(substr($split_packet_info, 0, 4));
$packets = bindec(substr($split_packet_info, 4));
$data = substr($data, $datacounter);
$packet_data[$packet_num] = $data;
}
else
{
# Packet hasn't indicitated it's split
# It could be a lone packet or the Valve split packet bug
# If later, pray to God it isn't a split packet that has come in reverse order
# Only cut this off if we can safely assume it's a multipacket and we don't know it is
if (strstr($data, "\xFF\xFF\xFF\xFFl"))
$datacounter = 5;
else
$datacounter = 0;
# Get rid of the crap at the start
$data = substr($data, $datacounter);
$packet_data[] = $data;
}
$packetsfound++;
}
while ($packetsfound < $packets);
ksort($packet_data);
$formated_data .= implode('', $packet_data);
# The above code follows protocol
# To protect against Valve not following protocol
# we must check if the data is :-
# a) an rcon status check
# b) contains the start of the packet
# c) contains of packet
# Future ref:
# May need to actually check the packet to see if the X Users
# actually is equal to amount of users in packet in case a mid section
# goes AWOL
# RCon status packet:
# \xFF\xFF\xFF\xFFrcon [32bit Int] "[rcon password string]" status
# Might want to use regexps here
if (strstr($command, "\xFF\xFF\xFF\xFFrcon"))
{
# check if header part is found
# Hostname: HLstats 0wns \o/
if (strstr($formated_data, 'hostname: '))
{
# Woohoo found
# Now find if it ends with X users
# Might want to use regexps
if (strstr($formated_data, ' users'))
break;
}
elseif (eregi('^Bad challenge.', $formated_data))
break;
elseif (eregi('^Bad rcon_password.', $formated_data))
break;
# We are going to loop again and attempt a further scan
# as we couldn't find users or hostname in the packet
}
else
break;
}
while (1);
fclose($server_connection);
if (strlen($formated_data) == 0)
{
# We got no data?! Something must have gone wrong
return false;
}
return $formated_data;
}
function HalfLife_Rules($ip, $port)
{
$cmd = "\xFF\xFF\xFF\xFFV\x00";
if(!$serverdata = GetServerData($cmd, $ip, $port))
return false;
return Decode_Rule_Packet($serverdata);
}
function HalfLife_Players($ip, $port)
{
$cmd = "\xFF\xFF\xFF\xFFU\x00";
if(!$serverdata = GetServerData($cmd, $ip, $port))
return array();
return Decode_Player_Packet($serverdata);
}
function HalfLife_Details ($ip, $port)
{
$cmd = "\xFF\xFF\xFF\xFFT\x00"; if (!$serverdata = getServerData($cmd, $ip, $port))
return false;
$serverdetails = array();
$datastart = 0;
GetInt32($serverdata, $datastart);
GetInt8($serverdata, $datastart); $serverdetails['address'] = GetString($serverdata, $datastart);
$serverdetails['hostname'] = GetString($serverdata, $datastart);
$serverdetails['map'] = GetString($serverdata, $datastart);
$serverdetails['gamedir'] = GetString($serverdata, $datastart);
$serverdetails['description'] = GetString($serverdata, $datastart);
$serverdetails['players'] = GetInt8($serverdata, $datastart);
$serverdetails['max'] = GetInt8($serverdata, $datastart);
$serverdetails['protocol'] = GetInt8($serverdata, $datastart);
$serverdetails['type'] = chr(GetInt8($serverdata, $datastart));
$serverdetails['os'] = chr(GetInt8($serverdata, $datastart));
$serverdetails['password'] = GetInt8($serverdata, $datastart);
$serverdetails['ismod'] = GetInt8($serverdata, $datastart);
if($serverdetails['ismod'])
{
$serverdetails['mod_website'] = GetString($serverdata, $datastart);
$serverdetails['mod_download'] = GetString($serverdata, $datastart);
$serverdetails['hl_version'] = GetString($serverdata, $datastart);
$serverdetails['mod_version'] = GetInt32($serverdata, $datastart);
$serverdetails['mod_size'] = GetInt32($serverdata, $datastart);
$serverdetails['mod_type'] = GetInt8($serverdata, $datastart);
$serverdetails['mod_dll'] = GetInt8($serverdata, $datastart);
}
$serverdetails['secure'] = GetInt8($serverdata, $datastart);
$serverdetails['botcount'] = GetInt8($serverdata, $datastart);
return $serverdetails;
}
function Source_A2S_Info ($ip, $port)
{
$cmd = "\xFF\xFF\xFF\xFF".'TSource Engine Query'."\x00";
if (!$serverdata = getServerData($cmd, $ip, $port))
return false;
$serverdetails = array();
$datastart = 0;
# Junk
GetInt32($serverdata, $datastart);
$serverdetails['gametype'] = GetInt8($serverdata, $datastart);
# I = 73 = Source
# m = 109 = HL1
if ($serverdetails['gametype'] == 73)
return Decode_Source_Info_Packet($serverdata);
else
return Decode_HL1_Info_Packet($serverdata);
}
function Source_A2S_Player ($ip, $port, $challenge)
{
$cmd = "\xFF\xFF\xFF\xFF\x55".pack('l', $challenge);
if(!$serverdata = GetServerData($cmd, $ip, $port))
return array();
GetInt32($serverdata, $datastart);
$type = GetInt8($packet, $datastart);
if ($type == 65)
{
$challenge = Decode_Challenge_Packet($serverdata, $datastart);
return Source_A2S_Player($ip, $port, $challenge);
}
return Decode_Player_Packet($serverdata);
}
function Source_A2S_Rules($ip, $port, $challenge)
{
$cmd = "\xFF\xFF\xFF\xFF\x56".pack('l', $challenge);
if(!$serverdata = GetServerData($cmd, $ip, $port))
return false;
$datastart = 0;
GetInt32($serverdata, $datastart);
$type = GetInt8($serverdata, $datastart);
if ($type == 65)
{
$challenge = Decode_Challenge_Packet($serverdata, $datastart);
return Source_A2S_Rules($ip, $port, $challenge);
}
return Decode_Rule_Packet($serverdata);
}
function Source_A2S_GetChallenge ($ip, $port)
{
$cmd = "\xFF\xFF\xFF\xFF".'W';
if(!$serverdata = GetServerData($cmd, $ip, $port))
return "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF";
return Decode_Challenge_Packet($serverdata);
}
# Packet decoding functions
function Decode_Challenge_Packet ($packet)
{
$datastart = 0;
GetInt32($packet, $datastart);
$type = GetInt8($packet, $datastart);
$challenge = GetInt32($packet, $datastart);
return $challenge;
}
function Decode_Source_Info_Packet ($packet)
{
$serverdetails = array();
$datastart = 0;
# Junk
GetInt32($packet, $datastart);
$serverdetails['gametype'] = GetInt8($packet, $datastart);
$serverdetails['netversion'] = GetInt8($packet, $datastart);
$serverdetails['hostname'] = GetString($packet, $datastart);
$serverdetails['map'] = GetString($packet, $datastart);
$serverdetails['gamedir'] = GetString($packet, $datastart);
$serverdetails['gamedesc'] = GetString($packet, $datastart);
$serverdetails['appid'] = GetInt16($packet, $datastart);
$serverdetails['numplayers'] = GetInt8($packet, $datastart);
$serverdetails['maxplayers'] = GetInt8($packet, $datastart);
$serverdetails['numbots'] = GetInt8($packet, $datastart);
$serverdetails['servertype'] = chr(GetInt8($packet, $datastart));
$serverdetails['serveros'] = chr(GetInt8($packet, $datastart));
$serverdetails['password'] = GetInt8($packet, $datastart);
$serverdetails['secure'] = GetInt8($packet, $datastart);
$serverdetails['gameversion'] = GetString($packet, $datastart);
return $serverdetails;
}
function Decode_HL1_Info_Packet ($packet)
{
$serverdetails = array();
$datastart = 0;
GetInt32($packet, $datastart); $serverdetails['gametype'] = GetInt8($packet, $datastart);
$serverdetails['address'] = GetString($packet, $datastart);
$serverdetails['hostname'] = GetString($packet, $datastart);
$serverdetails['map'] = GetString($packet, $datastart);
$serverdetails['gamedir'] = GetString($packet, $datastart);
$serverdetails['gamedesc'] = GetString($packet, $datastart);
$serverdetails['numplayers'] = GetInt8($packet, $datastart);
$serverdetails['maxplayers'] = GetInt8($packet, $datastart);
$serverdetails['gameversion'] = GetInt8($packet, $datastart);
$serverdetails['servertype'] = chr(GetInt8($packet, $datastart));
$serverdetails['serveros'] = chr(GetInt8($packet, $datastart));
$serverdetails['password'] = GetInt8($packet, $datastart);
$serverdetails['ismod'] = GetInt8($packet, $datastart);
if($serverdetails['ismod'])
{
$serverdetails['mod_website'] = GetString($packet, $datastart);
$serverdetails['mod_download'] = GetString($packet, $datastart);
$serverdetails['hl_version'] = GetString($packet, $datastart);
$serverdetails['mod_version'] = GetInt32($packet, $datastart);
$serverdetails['mod_size'] = GetInt32($packet, $datastart);
$serverdetails['mod_type'] = GetInt8($packet, $datastart);
$serverdetails['mod_dll'] = GetInt8($packet, $datastart);
}
$serverdetails['secure'] = GetInt8($packet, $datastart);
$serverdetails['numbots'] = GetInt8($packet, $datastart);
return $serverdetails;
}
function Decode_Player_Packet ($packet)
{
$serverplayers = array();
$datastart = 0;
GetInt32($packet, $datastart);
$type = GetInt8($packet, $datastart);
$players = GetInt8($packet, $datastart);
if ($players == 0)
return $serverplayers; for ($i = 0; $i < $players; $i++)
{
$index = GetInt8($packet, $datastart);
$name = GetString($packet, $datastart);
$frags = GetInt32($packet, $datastart);
$time = GetFloat32($packet, $datastart);
$serverplayers[$i] = array('index' => $index, 'name' => $name, 'frags' => $frags, 'time' => $time);
}
return $serverplayers;
}
function Decode_Rule_Packet ($packet)
{
$serverrules = array();
$datastart = 0;
GetInt32($packet, $datastart);
$type = GetInt8($packet, $datastart);
$rules = GetInt16($packet, $datastart);
if ($rules == 0)
return false;
for ($i = 1; $i <= $rules; $i++)
{
$rulename = GetString($packet, $datastart);
$rulevalue = GetString($packet, $datastart);
$serverrules[$rulename] = $rulevalue;
}
return $serverrules;
}
# Rcon functions
function HalfLife_GetChallenge ($ip, $port)
{
$cmd = "\xFF\xFF\xFF\xFF".'challenge rcon'."\n";
if (!$serverdata = getServerData($cmd, $ip, $port))
return false;
$datastart = 0;
GetInt32($serverdata, $datastart);
$challenge = GetString($serverdata, $datastart);
$tmp = explode(' ', $challenge);
$challenge = trim($tmp[2]); return $challenge;
}
function HalfLife_Rcon ($ip, $port, $rcon, $command, &$challenge, $challenged = 0)
{
# No point challenging more than we have too.
if (strlen($challenge) < 5)
{
$challenge = HalfLife_GetChallenge($ip, $port);
$challenged++;
}
$cmd = "\xFF\xFF\xFF\xFFrcon $challenge \"$rcon\" $command";
if (!$serverdata = getServerData($cmd, $ip, $port))
return false;
$serverdata = trim($serverdata);
if (eregi('^Bad challenge.', $serverdata))
{
# Try for the challenge twice, if that fails... fail?
if($challenged < 2)
{
$challenge = '';
$serverdata = HalfLife_Rcon($ip, $port, $rcon, $command, $challenge, $challenged);
}
else
return false;
}
elseif (eregi('^Bad rcon_password.', $serverdata))
return false;
return $serverdata;
}
function Get_A_Packet (&$server_connection)
{
$timeout = microtime();
$timeout += 5;
$data = '';
do
{
$data .= fread($server_connection, 8192);
if (microtime() > $timeout)
{
# Connection timed out
return false;
}
$data_status = socket_get_status($server_connection);
}
while ($data_status['unread_bytes']);
return $data;
}
function Source_Rcon ($ip, $port, $rcon, $string1, $string2 = NULL)
{
$reqid = mt_rand(0, 255);
$reqtype = 3;
$rcon .= "\x00";
$string1 .= "\x00";
$string2 .= "\x00";
if (!$server_connection = fsockopen($ip, $port))
return false;
socket_set_blocking($server_connection, 1);
# Time out after 1 seconds
socket_set_timeout($server_connection, 5);
# First we need to auth
# Auth packet creation
$command = pack('VV', $reqid, $reqtype).$rcon.$string2;
$cmdlen = strlen($command);
$command = pack('V', $cmdlen).$command;
# Request Auth
fwrite($server_connection, $command, strlen($command)); # Get the thingy packet that currently has no use
$data = Get_A_Packet($server_connection);
# Get auth packet
$data = Get_A_Packet($server_connection); $datastart = 0;
$recsize = GetInt32($data, $datastart);
$recid = GetInt32($data, $datastart);
$rectype = GetInt32($data, $datastart);
$recstring1 = GetString($data, $datastart);
$recstring2 = GetString($data, $datastart);
# If we don't get what we expect, we must have failed?
if ($rectype != 2)
{
fclose($server_connection);
return false;
}
# If the packet ID's dont match, auth failed
if ($recid != $reqid)
{
fclose($server_connection);
return false;
}
# Prepare new command
$reqid = mt_rand(0, 255);
$reqtype = 2;
$command = pack('VV', $reqid, $reqtype).$string1.$string2;
$cmdlen = strlen ($command);
$command = pack('V', $cmdlen).$command;
# Send new command
fwrite($server_connection, $command, strlen($command)); $data = '';
# i will increment every loop
# t will only increment when a packet was received.
# should something break, it should.. in theory, kill itself
# but hopefully something will have killed it earlier
$i = 0;
$t = 1;
while ($i <= $t)
{
$i++;
$packet = '';
if ($packet = Get_A_Packet($server_connection))
{
$t++;
}
$datastart = 0;
$recsize = GetInt32($packet, $datastart);
$recid = GetInt32($packet, $datastart);
if ($recid == $reqid)
{
# I wonder how long it will be till this gets triggered
# Maybe? Never? Fear the bugs in the protocol!
# Header packet
$rectype = GetInt32($packet, $datastart);
$data .= GetString($packet, $datastart);
$recstring2 = GetString($packet, $datastart);
}
else
$data .= $packet;
}
# Make sure socket is closed
fclose($server_connection);
return $data;
}
# General formatting functions
function Format_Info_Array ($info)
{
if ($info['servertype'] == 'd')
$info['servertype'] = 'Dedicated';
elseif ($info['servertype'] == 'l')
$info['servertype'] = 'Listen';
elseif ($info['servertype'] == 'p')
$info['servertype'] = 'Proxy';
if ($info['serveros'] == 'w')
$info['serveros'] = 'Windows';
elseif ($info['serveros'] == 'l')
$info['serveros'] = 'Linux';
if ($info['password'] == 0)
$info['password'] = 'No';
elseif ($info['password'] == 1)
$info['password'] = 'Yes';
if ($info['secure'] == 0)
$info['secure'] = 'No';
elseif ($info['secure'] == 1)
$info['secure'] = 'Yes';
return $info;
}
function Format_Time ($temp)
{
$time = sprintf('%s', str_pad(floor($temp/3600), 2, 0, STR_PAD_LEFT));
$temp %= 3600;
$time = sprintf('%s:%s', $time, str_pad(floor($temp/60), 2, 0, STR_PAD_LEFT));
$temp %= 60;
$time = sprintf('%s:%s', $time, str_pad(floor($temp), 2, 0, STR_PAD_LEFT));
return $time;
}
function Deformat_Time ($time)
{
$temp = explode (':', $time);
$temp = array_reverse($temp);
$seconds = 0;
# Seconds
$seconds += $temp[0];
# Minutes
$seconds += $temp[1] * 60;
# Hours
$seconds += $temp[2] * 3600;
return $seconds;
}
function Parse_HL_Status ($status, $source, &$server_players, &$server_hltv)
{
$status = str_replace("\x00", '', $status);
$temp = explode("\n", $status);
$i = count($temp);
unset($temp[0]); # hostname
unset($temp[1]); # version
unset($temp[2]); # tcp/ip
unset($temp[3]); # map
unset($temp[4]); # players
unset($temp[5]); # *blank*
unset($temp[6]); # header
if ($temp[7] == '0 Users')
{
# Server is empty
}
else
{
# Get rid of the "X Users" line
unset($temp[$i-1]);
$i = 0;
foreach ($temp as $players)
{
# Get the name before reformating the string
eregi('"(.*)"', $players, $tmp);
$name = $tmp[1];
# de-spacalise the string
$players = preg_replace("/( +)/", ' ', $players);
# Get the de-spacelised name from the de-spacalised string
eregi('"(.*)"', $players, $tmp);
$name2 = $tmp[1];
# get rid of the hashes, they only cause problems
$players = str_replace('# ', '', $players);
$players = str_replace('#', '', $players);
$player = explode(' ', $players);
$count = substr_count($name2, ' ') + 2;
if ('HLTV' == $player[$count+1])
{
# It is possible for multiple HLTVs on 1 server so lets build it like the players.
$server_hltv[] = array('id' => $player[0],
'name' => $name,
'userid' => $player[$count++],
'uid' => $player[$count++],
'viewers' => substr($player[$count], 5, 1),
'capacity' => substr($player[$count++], 7),
'delay' => substr($player[$count++], 6),
'time' => Deformat_Time($player[$count++]),
'ip' => $player[$count++]);
}
else
{
if ($source)
{
# Source status is different
$server_players[] = array('id' => $player[0],
'name' => $name,
'uid' => $player[$count++],
'time' => Deformat_Time($player[$count++]),
'ping' => $player[$count++],
'loss' => $player[$count++],
'state' => $player[$count++],
'ip' => $player[$count++]);
}
else
{
$server_players[] = array('id' => $player[0],
'name' => $name,
'userid' => $player[$count++],
'uid' => $player[$count++],
'frags' => $player[$count++],
'time' => Deformat_Time($player[$count++]),
'ping' => $player[$count++],
'loss' => $player[$count++],
'ip' => $player[$count++]);
}
}
$i++;
}
}
}
?>
fil: live_stats2
<?php
function error($besked)
{
$besked = Nej;
}
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$start = $time;
include(INCLUDE_PATH.'/binary_funcs.inc');
include(INCLUDE_PATH.'/hlquery_funcs.inc');
$db->query("
SELECT
s.serverId,
s.name,
s.address,
s.port,
s.publicaddress,
s.game,
s.rcon_password,
g.name gamename
FROM
hlstats_Servers s
LEFT JOIN
hlstats_Games g
ON
s.game=g.code
ORDER BY serverId DESC
");
$server = $db->fetch_array();
pageHeader(
array('Server Statistics', $server['name']),
array($server['gamename'] => $g_options['scripturl'] . '?game=' . $server['game'],
'Server Statistics' => $g_options['scripturl'] . '?game=' . $server['game'],
$server['name'] => ''
)
);
if ($server['publicaddress'])
{
# Port maybe different
$temp = explode(':', $server['publicaddress']);
$server_ip = $server['address'];
if (isset($temp[1]))
$server_port = $temp[1];
else
$server_port = $server['port'];
}
else
{
$server_ip = $server['address'];
$server_port = $server['port'];
}
$server_rcon = $server['rcon_password'];
$server_hltv = array();
$server_players = array();
# Get info
if (!$server_details = Source_A2S_Info($server_ip, $server_port))
error("The details for this server couldn't be retrieved, this maybe because the server is currently unavailable.");
if ($server_details['gametype'] == 73)
{
$server['source'] = 1;
$server_details['address'] = $server_ip.':'.$server_port;
}
else
$server['source'] = 0;
# Get challenge
$query_challenge = Source_A2S_GetChallenge($server_ip, $server_port);
# Get packets with challenge number
$server_rules = Source_A2S_Rules($server_ip, $server_port, $query_challenge);
$server_players = Source_A2S_Player($server_ip, $server_port, $query_challenge);
$server_details = Format_Info_Array($server_details);
$server_details['hltvcount'] = count($server_hltv);
$server_details['players_real'] = $server_details['numplayers'];
$server_details['players_real'] -= $server_details['numbots'];
$server_details['players_real'] -= $server_details['hltvcount'];
$server_details['players_connecting'] = $server_details['numplayers'];
$server_details['players_connecting'] -= $server_details['numbots'];
$server_details['players_connecting'] -= count($server_players);
$server_details['players_connecting'] -= $server_details['hltvcount'];
?>
<table width="550" align="center" border="0" cellspacing="0" cellpadding="0" bgcolor="<?php echo $g_options['table_border']; ?>">
<?php do { ?>
<tr>
<td width="15" valign="top">
<p>
<img src="<?php echo $server['game']; ?>.ico" /><? if ($server_details['password'] == 'Yes') { echo "<img src=\"Yes.png\" />"; }?></p></td>
<td valign="middle" class="fontNormal"><?php echo htmlentities($server_details['hostname'], ENT_COMPAT, "UTF-8"); ?><br/>
<?php echo $server_details['address']; ?></td>
<td align="center" valign="middle"><?php echo $server_details['map']; ?></a></td>
<td align="right" valign="middle"><?php echo $server_details['players_real']; ?>/<?php echo $server_details['maxplayers'];?> </td>
</tr>
</td>
</tr>
<?php } while (($server = $db->fetch_array()) && ($server_ip = $server['address']) && ($server_port = $server['port']) &&
($server_details = Source_A2S_Info($server_ip, $server_port)) &&
($server_details['players_real'] -= $server_details['numbots'])&&
($server_details['players_real'] -= $server_details['hltvcount']) &&
($server_details['players_real'] = $server_details['numplayers'])) ?>
<tr>
<td>
<tr>
<td colspan="4" valign="top"><p>Dette script er er oprindeligt lavet af HLStats, men redigeret af data-cms.net</p></td>
</tr>
</table>
fil: live_Stats
<?php
define("DB_NAME", "DB navn");
define("DB_USER", "bruger");
define("DB_PASS", "pass");
define("DB_ADDR", "addrsen");
define("DB_TYPE", "mysql");
define("DB_PCONNECT", 0);
define("INCLUDE_PATH", "./include");
define("DELETEDAYS", 5);
define("MODE", "Normal");
define("PLATFORM", "POSIX");
foreach ($_GET as $key => $value) {
$_GET[$key] = sanitize($value);
}
foreach ($_POST as $key => $value) {
$_GET[$key] = sanitize($value);
}
if ( function_exists('ini_get') ) {
$globals = ini_get('register_globals');
} else {
$globals = get_cfg_var('register_globals');
}
if ($globals != 1) {
@extract($_SERVER, EXTR_SKIP);
@extract($_COOKIE, EXTR_SKIP);
@extract($_POST, EXTR_SKIP);
@extract($_GET, EXTR_SKIP);
@extract($_ENV, EXTR_SKIP);
}
if (version_compare(phpversion(), "4.1.0", "<"))
{
error("HLstats requires PHP version 4.1.0 or newer (you are running PHP version " . phpversion() . ").");
}
if (!get_magic_quotes_gpc())
{
error("HLstats requires <b>magic_quotes_gpc</b> to be <i>enabled</i>. Check your php.ini or refer to the PHP manual for more information.");
}
if (get_magic_quotes_runtime())
{
error("HLstats requires <b>magic_quotes_runtime</b> to be <i>disabled</i>. Check your php.ini or refer to the PHP manual for more information.");
}
error_reporting(E_ALL ^ E_NOTICE);
require(INCLUDE_PATH . "/db.inc");
class Table
{
var $columns;
var $keycol;
var $sort;
var $sortorder;
var $sort2;
var $page;
var $showranking;
var $numperpage;
var $var_page;
var $var_sort;
var $var_sortorder;
var $sorthash;
var $columnlist;
var $startitem;
var $maxpagenumbers = 20;
function Table ($columns, $keycol, $sort_default, $sort_default2,
$showranking=false, $numperpage=50, $var_page="page",
$var_sort="sort", $var_sortorder="sortorder", $sorthash="",
$sort_default_order="desc")
{
global $HTTP_GET_VARS;
$this->columns = $columns;
$this->keycol = $keycol;
$this->showranking = $showranking;
$this->numperpage = $numperpage;
$this->var_page = $var_page;
$this->var_sort = $var_sort;
$this->var_sortorder = $var_sortorder;
$this->sorthash = $sorthash;
$this->sort_default_order = $sort_default_order;
$this->page = intval($HTTP_GET_VARS[$var_page]);
$this->sort = $HTTP_GET_VARS[$var_sort];
$this->sortorder = $HTTP_GET_VARS[$var_sortorder];
if ($this->page < 1) $this->page = 1;
$this->startitem = ($this->page - 1) * $this->numperpage;
foreach ($columns as $col)
{
if ($col->sort != "no")
$this->columnlist[] = $col->name;
}
if (!is_array($this->columnlist) || !in_array($this->sort, $this->columnlist))
{
$this->sort = $sort_default;
}
if ($this->sortorder != "asc" && $this->sortorder != "desc")
{
$this->sortorder = $this->sort_default_order;
}
if ($this->sort == $sort_default2)
{
$this->sort2 = $sort_default;
}
else
{
$this->sort2 = $sort_default2;
}
}
function draw ($result, $numitems, $width=100, $align="center")
{
global $g_options, $game, $db;
$numpages = ceil($numitems / $this->numperpage);
}}
?>
<?php
function makeQueryString($key, $value, $notkeys = array())
{
global $HTTP_GET_VARS;
if (!is_array($notkeys))
$notkeys = array();
foreach ($HTTP_GET_VARS as $k=>$v)
{
if ($k && $k != $key && !in_array($k, $notkeys))
{
$querystring .= urlencode($k) . "=" . urlencode($v) . "&";
}
}
$querystring .= urlencode($key) . "=" . urlencode($value);
return $querystring;
}
function getOptions()
{
global $db;
$result = $db->query("SELECT keyname, value FROM hlstats_Options");
$numrows = $db->num_rows($result);
if ($numrows)
{
while ($rowdata = $db->fetch_row($result))
{
$options[$rowdata[0]] = $rowdata[1];
}
return $options;
}
else
{
error("Warning: Could not find any options in table " .
"<b>hlstats_Options</b>, database <b>" . DB_NAME . "</b>. Check HLstats configuration.");
return array();
}
}
function pageHeader($title, $location)
{
global $g_options, $HTTP_GET_VARS;
}
function pageFooter()
{
global $g_options;
include(INCLUDE_PATH . "/footer.inc");
}
function getSortArrow ($sort, $sortorder, $name, $longname,
$var_sort="sort", $var_sortorder="sortorder",
$sorthash="")
{
global $g_options;
if ($sortorder == "asc")
{
$sortimg = "sort-ascending.gif";
$othersortorder = "desc";
}
else
{
$sortimg = "sort-descending.gif";
$othersortorder = "asc";
}
$arrowstring = $g_options["font_small"]
. "<a href=\"" . $g_options["scripturl"] . "?"
. makeQueryString($var_sort, $name, array($var_sortorder));
if ($sort == $name)
{
$arrowstring .= "&$var_sortorder=$othersortorder";
}
else
{
$arrowstring .= "&$var_sortorder=$sortorder";
}
if ($sorthash)
{
$arrowstring .= "#$sorthash";
}
$arrowstring .= "\" style=\"color: " . $g_options["table_head_text"]
. "\" title=\"Change sorting order\">"
. "<font color=\"" . $g_options["table_head_text"] . "\">"
. "$longname</font></a>";
if ($sort == $name)
{
$arrowstring .= " <img src=\"" . $g_options["imgdir"] . "/$sortimg\""
. "width=7 height=7 hspace=4 border=0 align=\"middle\" alt=\"$sortimg\">";
}
$arrowstring .= $g_options["fontend_small"];
return $arrowstring;
}
function getSelect ($name, $values, $currentvalue="")
{
$select = "<select name=\"$name\">\n";
$gotcval = false;
foreach ($values as $k=>$v)
{
$select .= "\t<option value=\"$k\"";
if ($k == $currentvalue)
{
$select .= " selected";
$gotcval = true;
}
$select .= ">$v\n";
}
if ($currentvalue && !$gotcval)
{
$select .= "\t<option value=\"$currentvalue\" selected>$currentvalue\n";
}
$select .= "</select>";
return $select;
}
function getLink ($url, $maxlength=40, $type="http://", $target="_blank")
{
if ($url && $url != $type)
{
if (ereg("^$type(.+)", $url, $regs))
{
$url = $type . $regs[1];
}
else
{
$url = $type . $url;
}
if (strlen($url) > $maxlength)
{
$url_title = substr($url, 0, $maxlength-3) . "...";
}
else
{
$url_title = $url;
}
$url = str_replace("\"", urlencode("\""), $url);
$url = str_replace("<", urlencode("<"), $url);
$url = str_replace(">", urlencode(">"), $url);
return "<a href=\"$url\" target=\"$target\">"
. htmlentities($url_title, ENT_COMPAT, "UTF-8") . "</a>";
}
else
{
return "";
}
}
function getEmailLink ($email, $maxlength=40)
{
if (ereg("(.+)@(.+)", $email, $regs))
{
if (strlen($email) > $maxlength)
{
$email_title = substr($email, 0, $maxlength-3) . "...";
}
else
{
$email_title = $email;
}
$email = str_replace("\"", urlencode("\""), $email);
$email = str_replace("<", urlencode("<"), $email);
$email = str_replace(">", urlencode(">"), $email);
return "<a href=\"mailto:$email\">"
. htmlentities($email_title, ENT_COMPAT, "UTF-8") . "</a>";
}
else
{
return "";
}
}
function getImage ($filename)
{
global $g_options;
$url = $g_options["imgdir"] . $filename;
if ($g_options["imgpath"])
{
$path = $g_options["imgpath"] . $filename;
}
else
{
if (!ereg("^/", $g_options["imgdir"]))
{
ereg("(.+)/[^/]+$", $_SERVER["SCRIPT_NAME"], $regs);
$path = $regs[1] . "/" . $url;
}
else
{
$path = $url;
}
$path = $_SERVER["DOCUMENT_ROOT"] . $path;
}
if (file_exists($path . ".gif"))
{
$ext = "gif";
}
elseif (file_exists($path . ".jpg"))
{
$ext = "jpg";
}
else
{
$ext = "";
}
if ($ext)
{
$size = getImageSize("$path.$ext");
return array(
"url"=>"$url.$ext",
"path"=>"$path.$ext",
"width"=>$size[0],
"height"=>$size[1],
"size"=>$size[3]
);
}
else
{
return false;
}
}
function sanitize($str)
{
$str = preg_replace("/[^A-Za-z0-9_-]/", '', $str);
return htmlentities(strip_tags($str), ENT_QUOTES, "UTF-8");
}
define("VERSION", "1.35");
$db_classname = "DB_" . DB_TYPE;
$db = new $db_classname;
$g_options = getOptions();
if (!$g_options["scripturl"])
$g_options["scripturl"] = $PHP_SELF;
////
//// Main
////
include(INCLUDE_PATH . "/live_stats2.php");
?>
Koden er lidt lang, men håber nogle kan hjælpe
har selv været lidt igang, men nu kan jeg ikke mere
scriptet er lavet af hlstats, men jeg er ved at modificere lidt, så det passer til min side.