<?php
/*

Class RSSParser:    2 October 2002

Author            Duncan Gough

Overview:        An RSS parser that uses PHP and freely available RSS feeds to add fresh news content to your site.

Installation:        Decompress the file into your webroot and include it from whichever pages on which you want
            to display the data, e.g;

            include("rss.php");

Usage:            As above, just include the rss.php file from within your PHP page, and the news will appear.
            You should find the HTML code in the functions endElement(), show_title() and show_list_box() below,
            feel free to modify these to match your site.
*/

class RSSParser    {

    var 
$title        "";
    var 
$link         "";
    var 
$description    "";
    var 
$inside_item    false;

    
// These are just some defaults that I chose to display on my site - http://www.suttree.com/
    // Add new RSS feeds using this format;
    // "http://www.wherever.com/path/to/rss/"    => "Display name",
    
var $all_rss_urls = array(
                    
"http://www.bbc.co.uk/syndication/feeds/news/ukfs_news/front_page/rss091.xml"    => "BBC Front Page",
                    
"http://www.bbc.co.uk/syndication/feeds/news/ukfs_news/technology/rss091.xml"    => "BBC - Technology",
                    
"http://www.bbc.co.uk/syndication/feeds/news/ukfs_news/uk/rss091.xml"        => "BBC - UK News",
                    
"http://www.bbc.co.uk/syndication/feeds/news/ukfs_news/world/rss091.xml"    => "BBC - World News",
                    
"http://www.newsunlimited.co.uk/rss/1,,,00.xml"                 => "The Guardian",
                    
"http://www.w3.org/2001/sw/Overview.rss"                     => "W3C Semantic Web",
                    
"http://www.bsdtoday.com/backend/bt.rdf"                     => "BSD Today",
                    
"http://www.perl.com/pace/news.rss"                         => "Perl.com",
                    
"http://www.mnot.net/bookmarks/Programming/Reference/feed"            => "Programming Reference",
                    
"http://www.wired.com/news_drop/netcenter/netcenter.rdf"             => "Wired News",
                    
"http://export.cnet.com/export/feeds/news/rss/1,11176,,00.xml"             => "CNET",
                    
"http://www.kuro5hin.org/backend.rdf"                         => "Kuro5hin",
                    
"http://slashdot.org/slashdot.rss"                         => "Slashdot",
                    
"http://www.macslash.com/macslash.rdf"                         => "MacSlash",
                    
"http://www.macosxhints.com/backend/geeklog.rdf"                 => "Mac OS X Hints",
                    
"http://www.oreillynet.com/meerkat/index.php?&p=12724&_fl=rss"            => "Mac OS X - Meerkat",
                    
"http://maccentral.macworld.com/mnn.cgi"                     => "Mac Central",
                    
"http://macosx.weblogs.com/xml/rss.xml"                     => "Mac OS X Weblogs",
                    
"http://diveintomark.org/xml/rss.xml"                         => "Dive into Mark",
                    
"http://inmyexperience.com/index.xml"                        => "In My Experience",
                    
"http://www.newsisfree.com/HPE/xml/feeds/39/1439.xml"                 => "Scientific American",
                    
"http://www.quotationspage.com/data/mqotd.rss"                     => "Quotations",
                    
"http://memepool.com/memepool.rss"                         => "Memepool",
                );

    function 
startElement$parser$name$attrs='' ){
        global 
$current_tag;

        
$current_tag $name;

        if( 
$current_tag == "ITEM" )
            
$this->inside_item true;

    } 
// endfunc startElement

    
function endElement$parser$tagName$attrs='' ){
        global 
$current_tag;

        if ( 
$tagName == "ITEM" ) {

            
printf"\t<br><b><a href='%s' target='_blank'>%s</a></b>\n"trim$this->link ), htmlspecialcharstrim$this->title ) ) );
            
printf"\t<br>%s<br>\n"htmlspecialcharstrim$this->description ) ) );

            
$this->title "";
            
$this->description "";
            
$this->link "";
            
$this->inside_item false;

        }

    } 
// endfunc endElement

    
function characterData$parser$data ){
        global 
$current_tag;

        if( 
$this->inside_item ){
            switch(
$current_tag){

                case 
"TITLE":
                    
$this->title .= $data;
                    break;
                case 
"DESCRIPTION":
                    
$this->description .= $data;
                    break;
                case 
"LINK":
                    
$this->link .= $data;
                    break;

                default:
                    break;

            } 
// endswitch

        
// end if

    
// endfunc characterData

    
function parse_results$xml_parser$rss_parser$file )    {

        
xml_set_object$xml_parser, &$rss_parser );
        
xml_set_element_handler$xml_parser"startElement""endElement" );
        
xml_set_character_data_handler$xml_parser"characterData" );

        
$fp fopen("$file","r") or die( "Error reading XML file, $file" );

        while (
$data fread($fp4096))    {

            
// parse the data
            
xml_parse$xml_parser$datafeof($fp) ) or die( sprintf"XML error: %s at line %d"xml_error_stringxml_get_error_code($xml_parser) ), xml_get_current_line_number$xml_parser ) ) );

        } 
// endwhile

        
fclose($fp);

        
xml_parser_free$xml_parser );

    } 
// endfunc parse_results

    
function show_title$rss_url ){
                    
?>
                    <tr height="20%">
                        <td valign="top">
                        <small>

                        <br>
                        Latest news (<? echo $this->all_rss_urls$rss_url ]; ?>):
                        </small>
                        </td>
                    </tr>
                    <tr height="70%">
                        <td valign="top">
                        <small><small>
                    <?
    
// endfunc show_title

    
function show_list_box$rss_url ){

                
?>
                    <br>
                        <form method="post">
                        <select name='rss_url' size='1'>
                <?

                
while (list ($key$val) = each $this->all_rss_urls )) {
                    echo 
"\t\t<option value=\"$key\"";

                    if( 
$rss_url == $key )
                    echo 
" selected";

                    echo 
">$val</option>\n";
                }
            
?>
        </select>
        <input type="Submit" Value="Submit">
        </form>
        <?
    
// end func show_list_box

// endclass RSSParser

global $rss_url;

// Set a default feed
if( $rss_url == "" )
    
$rss_url "http://www.bbc.co.uk/syndication/feeds/news/ukfs_news/front_page/rss091.xml";

$xml_parser xml_parser_create();
$rss_parser = new RSSParser();

$rss_parser->show_title$rss_url );
$rss_parser->parse_results$xml_parser, &$rss_parser$rss_url );
$rss_parser->show_list_box$rss_url );

?>