f (!file_exists('online.cache') || filemtime('online.cache')+$onlinecache < time())
{
$serverconn = @fsockopen($serverhost, $serverport, $errno, $errstr, 2.0);
$tpl->online = $online = (bool)$serverconn;
$onlinelist = array();
if ($online)
{
$request_online = chr(5).chr(254).chr(1).chr(22).chr(254).chr(255);
fwrite($serverconn, $request_online);
$raw = fread($serverconn, 1024*256); // Read up to 256KB of data
fclose($serverconn);
$raw = substr($raw, 5); // length, ID, replycode
$chars = Number(ord($raw[0]), ord($raw[1])); $raw = substr($raw, 2); // Number of characters
$raw = substr($raw, 1); // separator
for ($i = 0; $i < $chars; ++$i)
{
$newchar = array(
'name' => '',
'title' => '',
'admin' => '',
'class' => '',
'guild' => '',
);
$pos = strpos($raw, chr(255));
$newchar['name'] = substr($raw, 0, $pos);
$raw = substr($raw, $pos+1);
$pos = strpos($raw, chr(255));
$newchar['title'] = substr($raw, 0, $pos);
$raw = substr($raw, $pos+1);
$raw = substr($raw, 1); // ?
$newchar['admin'] = Number(ord(substr($raw, 0, 1)));
$newchar['admin'] = ($newchar['admin'] == 4 || $newchar['admin'] == 5 || $newchar['admin'] == 9 || $newchar['admin'] == 10);
$raw = substr($raw, 1);
$newchar['class'] = Number(ord(substr($raw, 0, 1)));
$raw = substr($raw, 1);
$newchar['guild'] = trim(substr($raw, 0, 3));
$raw = substr($raw, 3);
$raw = substr($raw, 1); // separator
$onlinelist[] = $newchar;
}
ksort($onlinelist);
file_put_contents('online.cache', serialize($onlinelist));
}
else
{
file_put_contents('online.cache', 'OFFLINE');
}
}