<?php

error_reporting
(0);                         //error reporting = off for production use
//error_reporting(E_ALL | E_STRICT);          //error reporting = on for testing

define('RSS_TTL', 180);                     //maximum age in minutes of locally cached feed
define('RSS_FILE', './slug.xml');           //local filename of cached feed
define('RSS_CHARSET', 'UTF-8');             //character set used for caching and serving the feed
define('RSS_HEADER', 'Content-type: application/xml; charset='.RSS_CHARSET);

clearstatcache();                                     //make sure we get up-to-date file information
if ( file_exists(RSS_FILE)                            //if the cached feed file exists
    
&& time() - filemtime(RSS_FILE) < RSS_TTL * 60 )  //and is not older than RSS_TTL minutes
{                                                     //then local copy is good
    
header(RSS_HEADER);                               //output appropriate header to the client
    
readfile(RSS_FILE);                               //pass the cached file
    
exit();                                           //end script here
}

define('RSS_SOURCE', 'http://www.twrc.rowing.org.uk/slug/lower.htm');  //where to get the html source for building the feed
define('RSS_DATEFORMAT', 'D, d M Y H:i:s T');  //standard rss 2.0 date format
define('RSS_TITLELENGTH', 65);                 //item title abbreviation length
define('RSS_DESCRLENGTH', 255);                //item description abbreviation length
define('RSS_ITEMS', 15);                       //number of items used for the feed

/**
* Remove html code from text, collapse whitespace, abbreviate to predefined length and encode special chars
* In : (string)html code/text
* Out: (string)abbreviated plain text
*/
function abbr($text, $maxlength)
{
    
$t = trim(preg_replace('/\s+/', ' ', strip_tags($text)));
    if (
strlen($t) > $maxlength )
        
$t = rtrim(substr($t, 0, $maxlength - 3)).'...';
    return
htmlspecialchars($t);
}

ob_start();                                  //start output buffering because output needs to be captured to file
echo '<?xml version="1.0" encoding="'.RSS_CHARSET.'" ?>'."\n";  //echo to circumvent possible php "short open tag" setting

?>
<rss version="2.0">
    <channel>
        <title>The Tideway Slug</title>
        <link><?php echo RSS_SOURCE; ?></link>
        <description>Trawl the depths of the 'Lower' Thames with The Tideway Slug.</description>
        <language>en-GB</language>
        <pubDate><?php echo date(RSS_DATEFORMAT); ?></pubDate>
        <lastBuildDate><?php echo date(RSS_DATEFORMAT); ?></lastBuildDate>
        <ttl><?php echo RSS_TTL; ?></ttl>
        <docs>http://blogs.law.harvard.edu/tech/rss</docs>
        <managingEditor>lizwray@aol.com</managingEditor>
        <webMaster>webmaster@twrc.rowing.org.uk</webMaster>
        <copyright>Copyright 1996 - <?php echo date('Y'); ?>, The Tideway Slug</copyright>
<?php

$src
= mb_convert_encoding(file_get_contents(RSS_SOURCE), RSS_CHARSET);  //grab remote page and convert character set
$src = str_replace("\r", "\n", str_replace("\r\n", "\n", $src));         //remove DOS and Mac line endings

$len = strlen($src);                                                 //string length of page
$offset = 0;                                                         //starting position for item search
$items = 0;                                                          //count number of processed items
while ( $items < RSS_ITEMS && $offset < $len )                       //check complete source for maximum number of items
{
    
$item = array();
    if (
preg_match('/\n<hr.*?title="([0-9-]+)".*?<b>(.*?)<\/b>.*?<blockquote>(.*?)<\/blockquote>/s', $src, $item, PREG_OFFSET_CAPTURE, $offset) )
    {
        
$day = strtotime($item[1][0]);                                   //interpret textual date to unix time value
        
$pubdate = date(RSS_DATEFORMAT, $day);                           //standard rss date format for this day
        
$anchor = date('Ymd', $day);                                     //date format used as html anchor and for guid
        
$link = RSS_SOURCE.'#'.$anchor;                                  //all items from one day get the same html link
        
$title = abbr($item[2][0], RSS_TITLELENGTH);
        
$descr = wordwrap(abbr($item[3][0], RSS_DESCRLENGTH), 70, "\n\t\t\t\t", FALSE)."\n";
        
$offset = $item[0][1] + strlen($item[0][0]);
?>
        <item>
            <title><?php echo $title; ?></title>
            <link><?php echo $link; ?></link>
            <description>
                <?php echo $descr; ?>
            </description>
            <pubDate><?php echo $pubdate; ?></pubDate>
            <guid isPermaLink="false">co.uk.tidewayslug.<?php echo $anchor.'.'.$items; ?></guid>
        </item>
<?php
        
++$items;
    }
    else
        
$items = RSS_ITEMS;
}

?>
    </channel>
</rss>
<?php

$rss
= ob_get_clean();              //stop output buffering and get all text from buffer
if ( $fp = fopen(RSS_FILE, 'wb') )  //open locally cached feed file for writing
{
    
fwrite($fp, $rss);              //write all output to the file
    
fclose($fp);                    //close the file
    
chmod(RSS_FILE, 0666);          //make sure file is also writable for vhost owner
}

header(RSS_HEADER);  //output appropriate header to the client
echo $rss;           //pass the saved output

?>