<?php
class XsltRss {
    var
$timeout = 10;
    var
$cache_time = 3600;
    var
$cache_dir = '';
    var
$charset = 'ISO-8859-15';

    
// Reads an URL checkin error conditions and timeout
    // gallir at uib dot es
    //
    
function ReadUrl ($rss_url) {
        
$parsed_url =   parse_url($rss_url);
        
$host = $parsed_url["host"];
        
$path = $parsed_url["path"];
        
$port = $parsed_url["port"];
        
$query = $parsed_url["query"];
        if(!
$port > 0) $port = 80;
        
$filename = $path;
        if(
strlen($query)>0) $filename .= "?$query";

        
$contents = '';
        if (!(
$fpread = @fsockopen ($host, $port, $errno, $errstr, $this->timeout)))
            return
false;
        
socket_set_timeout($fpread, $this->timeout);
        
// Send HTTP petition accepting gziped content.
        
@fputs ($fpread,
                
"GET $filename HTTP/1.0\r\n".
            
"Host: $host\r\n".
                
"User-Agent: Bulma v2.0 (http://bulma.net)\r\n".
            
"Accept-Encoding: gzip\r\n".
            
"\r\n");
            
        
$endheaders = FALSE;
        
$sstatus = socket_get_status($fpread);
        while (!
feof($fpread) && !$endheaders && !$sstatus["timed_out"]) {
            
$temp=trim(@fgets($fpread, 256));
            
$headers .= $temp;
            
$endheaders = (strlen($temp) == 0);
            
$sstatus = socket_get_status($fpread);
        }
        while(!
feof($fpread) && !$sstatus["timed_out"]) {
            
$contents .= @fgets($fpread, 1024);
            
$sstatus = socket_get_status($fpread);
        }
        
fclose($fpread);

        
// If content is gziped, pass the stream to the gzinflate function stripping
        // the first 10 chars, that are the gzip file header. The gzinflate function
        // wants a string without this header.
        
if (preg_match('/Content-Encoding: gzip/', $headers))
            
$contents = gzinflate(substr($contents,10));

        if(!
$sstatus["timed_out"])
            return
$contents;
        else
            return
false;
    }

    
    function
Get ($rss_url) {
        
// If CACHE ENABLED
        
if ($this->cache_dir != '') {
            
$cache_file = $this->cache_dir . '/rsscache_' . md5($rss_url);
            
$last_try_file = $cache_file . ".last_try";
            
$timedif = @(time() - filemtime($cache_file));
            
$lasttimedif = @(time() - filemtime($last_try_file));
            
$cache_time_rnd = rand(-120, 120);
            if (
file_exists($last_try_file) &&
                ((
$timedif + $cache_time_rnd) < $this->cache_time || $lasttimedif < 600)) {
                
// cached file is fresh enough, return cached HTML
                
if ($fd = @fopen($cache_file, 'r')) {
                    
$result = fread($fd, filesize($cache_file));
                    
fclose($fd);
                }
                else {
                    
$result = "Unable to read cache contents.";
                }
            } else {
                
// cached file is too old, create new
                
if ($f = @fopen($last_try_file, 'w')) {
                    
fwrite ($f, $rss_url, strlen($rss_url));
                    
fclose($f);
                }
                if (
$result = $this->Parse($rss_url)) {
                    if (
$f = @fopen($cache_file, 'w')) {
                        
fwrite ($f, $result, strlen($result));
                        
fclose($f);
                    }
                }
            }
        }
        
// If CACHE DISABLED >> load and parse the file directly
        
else {
            
$result = $this->Parse($rss_url);
        }
        
// return result
        
return $result;
    }

    
// -------------------------------------------------------------------
    // Parse() is private method used by Get() to load and parse RSS file.
    // Don't use Parse() in your scripts - use Get($rss_file) instead.
    // -------------------------------------------------------------------
    
function Parse ($rss_url) {
        
// Open and load RSS file
        
if(($rss_content = $this->ReadUrl($rss_url)) && strlen($rss_content) > 5) {
            
$xsltproc = xslt_create();
            
xslt_set_encoding($xsltproc, $this->charset);

            
$arguments['/_xml'] = $rss_content;
            
$parameters['datetime'] = date('j/n/Y G:i:s');
            
            
$html = xslt_process($xsltproc, 'arg:/_xml', 'xsl/rdf-rss.xsl', NULL, $arguments, $parameters);

            if (empty(
$html))
                return(
'XSLT processing error: '. xslt_error($xsltproc));
            
xslt_free($xsltproc);
            return(
$html);
        }
        else
// Error in opening return False
        
{
            return
False;
        }
    }
}

?>