<?php

error_reporting
(0);                         //error reporting = off for production use

define('RSS_TTL', 180);                     //maximum age in minutes of locally cached feed
define('RSS_FILE', './rowingservice.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.rowingservice.com/new.html');  //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_DAYS', 5);                         //number of days used for the feed

/**
* Remove html code from text, collapse whitespace and abbreviate to predefined length
* In : (string)html code/text
* Out: (string)abbreviated plain text
*/
function abbr($text)
{
    
$t = trim(preg_replace('/\s+/', ' ', strip_tags($text)));
    if (
strlen($t) > RSS_TITLELENGTH )
        
$t = rtrim(substr($t, 0, RSS_TITLELENGTH - 3)).'...';
    return
$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 php "short open tag" setting

?>
<rss version="2.0">
    <channel>
        <title>The Rowing Service</title>
        <link><?php echo RSS_SOURCE; ?></link>
        <description>New rowing information from around the world.</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>rq@rowingservice.com</managingEditor>
        <webMaster>webmaster@rowingservice.com</webMaster>
        <copyright>Copyright 1994 - <?php echo date('Y'); ?>, The Rowing Service</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
$src = preg_replace('/^.*?\n\n<b>/s', '<b>', $src, 1);            //strip header (search for first <b> after blank line)
$src = preg_replace('/\A(.*)<P>.*?\z/s', '\1', $src, 1);          //strip footer (search for last <P>)
$daily = preg_split('/\n+<P>\n+/', $src);                         //split all news to daily news units

for ( $i = 0; $i < RSS_DAYS; ++$i )                                  //loop through predefined number of days
{
    
$items = array_map('abbr', preg_split('/\n<LI>/', $daily[$i]));  //split daily news to single items and clean up
    
$day = strtotime($items[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

    
for ( $j = 1; $j < count($items); ++$j )  //loop through all items from this day
    
{                                         //generate standard rss item data
?>
        <item>
            <title><?php echo htmlspecialchars($items[$j]); ?></title>
            <link><?php echo $link; ?></link>
            <description>Visit <a href="<?php echo RSS_SOURCE; ?>">The Rowing Service</a> for more information.</description>
            <pubDate><?php echo $pubdate; ?></pubDate>
            <guid isPermaLink="false">com.rowingservice.<?php echo $anchor.'.'.$j; ?></guid>
        </item>
<?php
    
}
}

?>
    </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

?>