<?php

$rss
= new RSS();
$rss->output();

///////////////////////////////////////////////////////////////////////////////
// PHP Class to add RSS content to your dynamic web page.
//
// To reformat the output contents simply use the syntax:
//
//    $rss->output(... formatted string ...)
//
// where the string contains html and template variables to be replaced with
// data from each item of the RSS feed: {link}, {title}, and {description}.
//
// See the class definition below  for an example of the default format string.
//
///////////////////////////////////////////////////////////////////////////////
class RSS
{
    var
$insideitem = false;
    var
$tag = "";
    var
$title = "";
    var
$description = "";
    var
$link = "";
    var
$format = "<p><b><a href='{link}'>{title}</a></b><br><blockquote>{description}</blockquote></p>";
    var
$url = "http://www.warnerbrosrecords.com/rss.xml";

    function
RSS($url = "")
    {
        if (
$url != "") $this->set_url($url);

        
$this->parser = xml_parser_create();
    }

    function
set_url($url)
    {
        
$this->url = $url;
    }

    function
set_format($format)
    {
        
$this->format = $format;
    }

    function
output($format = "")
    {
        if (
$format != "") $this->set_format($format);

        
xml_set_object($this->parser, &$this);
        
xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, true);
        
xml_set_element_handler($this->parser, "_startElementHandler", "_endElementHandler");
        
xml_set_character_data_handler($this->parser, "_cDataHandler");
        
$fp = fopen($this->url,"r")
            or die(
"Error reading RSS data.");
        while (
$data = fread($fp, 4096))
            
xml_parse($this->parser, $data, feof($fp))
                or die(
sprintf("RSS error: %s at line %d",
                    
xml_error_string(xml_get_error_code($this->parser)),
                    
xml_get_current_line_number($this->parser)));
        
fclose($fp);
        
xml_parser_free($this->parser);
    }

    function
_startElementHandler($parser, $name, $attrs) {
        if (
$this->insideitem) {
            
$this->tag = $name;
        } elseif (
$name == "ITEM") {
            
$this->insideitem = true;
        }
    }

    function
_endElementHandler($parser, $name) {
        if (
$name == "ITEM") {
            
$tokens = array(
                
"{link}",
                
"{title}",
                
"{description}"
            
);
            
$values = array(
                
trim($this->link),
                
htmlspecialchars(trim($this->title)),
                
htmlspecialchars(trim($this->description))
            );
            echo
str_replace($tokens, $values, $this->format) . "\n";

            
$this->title = "";
            
$this->description = "";
            
$this->link = "";
            
$this->insideitem = false;
        }
    }

    function
_cDataHandler($parser, $data) {
        if (
$this->insideitem) {
            switch (
$this->tag) {
                case
"TITLE":
                    
$this->title .= $data;
                    break;
                case
"DESCRIPTION":
                    
$this->description .= $data;
                    break;
                case
"LINK":
                    
$this->link .= $data;
                    break;
            }
        }
    }
}

?>