Code
function cached_fopen_url($url, $file_mode, $timeout_seconds = 90, $cache_path = "/var/www/dragonsfury/pnTemp", $fsocket_timeout = 10)
{
$debug = false;
clearstatcache();
$cache_filename=$cache_path . "/" . urlencode($url) .".cached";
if ($debug) {
print "local_cache creation_time =" .
@filemtime($cache_filename) .
" actual time = " . time() .
" timeout = " .
timeout_seconds ."<p>";
}
if ( ( @file_exists($cache_filename ) and ( ( @filemtime($cache_filename) + $timeout_seconds) > ( time() ) ) ) ) {
// ok, file is already cached and young enouth
if ($debug) {
print "using cached file ($cache_filename) <p>";
}
} else {
if ($debug) {
print "cacheing file ($url) to local ($cache_filename)<p>";
}
$urlParts = parse_url($url);
$host = $urlParts['host'];
$port = (isset($urlParts['port'])) ? $urlParts['port'] : 80;
if( !$fp = @fsockopen( $host, $port, $errno, $errstr, $fsocket_timeout )) {
// Server not responding
} else {
if( !fputs( $fp, "GET $url HTTP/1.0\r\nHost:$host\r\n\r\n" )) {
die( "unable to send get request" );
}
$data = null;
stream_set_timeout($fp, $fsocket_timeout);
$status = socket_get_status($fp);
while( !feof($fp) && !$status['timed_out'])
{
$data .= fgets ($fp,8192);
$status = socket_get_status($fp);
}
fclose ($fp);
// strip headers
$sData = split("\r\n\r\n", $data, 2);
$data = $sData[1];
// save to cache file
$f2 = fopen($cache_filename,"w+");
fwrite($f2,$data);
fclose($f2);
}
}
// ok, point to (fresh) cached file
if ( @file_exists($cache_filename )) {
$handle = fopen($cache_filename, $file_mode);
return $handle;
}
return false;
}
function smarty_function_ranking($params, &$smarty)
{
extract($params);
unset($params);
$url = "http://ladder.guildwars.com/ladder.dll?name=Dragons+Fury";
$fp = cached_fopen_url($url, "rb",600);
$ladderpage = null;
if($fp) {
while(!feof($fp)) {
$ladderpage = $ladderpage . fgets($fp, 8192);
}
fclose($fp);
if(preg_match("/>([0-9]{3})<SUP>/S", $ladderpage, $regs)) {
$ranking = $regs[1];
} else {
$ranking = "unavailable";
}
} else {
$ranking = "Connection Error";
}
$response = "Current guild ranking: <a href=\"".$url."\" target=\"new\" style=\"color: ffffff;\"><strong>".$ranking."</strong></a>";
return $response;
}
{
$debug = false;
clearstatcache();
$cache_filename=$cache_path . "/" . urlencode($url) .".cached";
if ($debug) {
print "local_cache creation_time =" .
@filemtime($cache_filename) .
" actual time = " . time() .
" timeout = " .
timeout_seconds ."<p>";
}
if ( ( @file_exists($cache_filename ) and ( ( @filemtime($cache_filename) + $timeout_seconds) > ( time() ) ) ) ) {
// ok, file is already cached and young enouth
if ($debug) {
print "using cached file ($cache_filename) <p>";
}
} else {
if ($debug) {
print "cacheing file ($url) to local ($cache_filename)<p>";
}
$urlParts = parse_url($url);
$host = $urlParts['host'];
$port = (isset($urlParts['port'])) ? $urlParts['port'] : 80;
if( !$fp = @fsockopen( $host, $port, $errno, $errstr, $fsocket_timeout )) {
// Server not responding
} else {
if( !fputs( $fp, "GET $url HTTP/1.0\r\nHost:$host\r\n\r\n" )) {
die( "unable to send get request" );
}
$data = null;
stream_set_timeout($fp, $fsocket_timeout);
$status = socket_get_status($fp);
while( !feof($fp) && !$status['timed_out'])
{
$data .= fgets ($fp,8192);
$status = socket_get_status($fp);
}
fclose ($fp);
// strip headers
$sData = split("\r\n\r\n", $data, 2);
$data = $sData[1];
// save to cache file
$f2 = fopen($cache_filename,"w+");
fwrite($f2,$data);
fclose($f2);
}
}
// ok, point to (fresh) cached file
if ( @file_exists($cache_filename )) {
$handle = fopen($cache_filename, $file_mode);
return $handle;
}
return false;
}
function smarty_function_ranking($params, &$smarty)
{
extract($params);
unset($params);
$url = "http://ladder.guildwars.com/ladder.dll?name=Dragons+Fury";
$fp = cached_fopen_url($url, "rb",600);
$ladderpage = null;
if($fp) {
while(!feof($fp)) {
$ladderpage = $ladderpage . fgets($fp, 8192);
}
fclose($fp);
if(preg_match("/>([0-9]{3})<SUP>/S", $ladderpage, $regs)) {
$ranking = $regs[1];
} else {
$ranking = "unavailable";
}
} else {
$ranking = "Connection Error";
}
$response = "Current guild ranking: <a href=\"".$url."\" target=\"new\" style=\"color: ffffff;\"><strong>".$ranking."</strong></a>";
return $response;
}
FYI - I got the caching fopen code from the fopen man page on php.net in the user contributed comments. I added a line to my master.htm, admin.htm, and home.htm modules to include the function in my theme. As you can see, every time someone clicks a link on the page, this function runs. Is this stupid? Is it secure? Should I learn how to write a module and implement it that way? I don't know. Looking for advice on the best way to do this. Keep in mind, I want to implement this as a Smarty function no matter what I do. I don't want another block.
http://www.mydragonsfury.net/
Thanks
