<?php
/*
    Coded by WhiteAcid - built off http://www.sitepoint.com/article/php-xml-parsing-rss-1-0/4
    Security warning: This script assumes that the RSS feed has already been sanitised as it often is. IPB does this.
    
    Usage:
        Pass the url to the RSS feed into the constructor, ie:
            $foobar = new RSSParser('http://newsrss.bbc.co.uk/rss/newsonline_world_edition/front_page/rss.xml');
        Call the create() method to echo all the contents of the feed
    CSS Customization:
        Every entr in the feed is within a <p> tag with class set to rss_post
        Every author (if displayed) is in a span tag with class set to rss_author
        Every feed link is in a h4 (wrapped in <a>) tag with class set to rss_heading
        Knowing that creating CSS should be trivial.
    
    Class diagram of RSSParser:
     _________________________
    |________RSSParser________|
    | -insideitem : boolean   |
    | -tag : string           |
    | -title : string         |
    | -description : string   |
    | -link : string          |
    | -author : string        |
    | -source : string        |
    | -xml_parser : resource  |
    |_________________________|
    | +__construct() : void   |
    | +create() : void        |
    | +__destruct() : void    |
    | -startElement() : void  |
    | -endElement() : void    |
    | -characterData() : void |
    |_________________________|
*/

class RSSParser
{
    
private $insideitem false;
    
private $tag;
    
private $title;
    
private $description;
    
private $link;
    
private $author;
    
private $source;
    
private $xml_parser;

    
public function __construct($url)
    {
        
$this->source $url;
        
        
$this->xml_parser xml_parser_create();
        
xml_set_object($this->xml_parser,$this);
        
xml_set_element_handler($this->xml_parser"startElement""endElement");
        
xml_set_character_data_handler($this->xml_parser"characterData");
    }
    
    
public function create()
    {
        
$xml file_get_contents($this->source);
        
xml_parse($this->xml_parser$xmlstrlen($xml))
            or die(
"XML error: ".xml_error_string(xml_get_error_code($this->xml_parser))." at line ".xml_get_current_line_number($this->xml_parser));
    }
    
    
public function __destruct()
    {
        
xml_parser_free($this->xml_parser);
    }
    
    
private function startElement($parser$tagName$attrs)
    {
        if (
$this->insideitem)
            
$this->tag $tagName;
        elseif (
$tagName == "ITEM")
            
$this->insideitem true;
    }

    
private function endElement($parser$tagName)
    {
        if (
$tagName == "ITEM")
        {
            
$this->description preg_replace("/<!--(.+)?-->/","",$this->description);
            echo 
"<p class='rss_post'>\n\t<h4 class='rss_heading'><a href='".$this->link."'>".$this->title."</h4></a>\n";
            echo 
"\t".$this->description."\n\n";
            
#The author field is rare, so only use if it's there
            
if(!empty($this->author))
                echo 
"\t-<span class='rss_author'>".$this->author."<span>\n";
            echo 
"</p>\n";

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

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


#$rss_parser = new RSSParser('http://newsrss.bbc.co.uk/rss/newsonline_world_edition/front_page/rss.xml');
$rss_parser = new RSSParser('http://127.0.0.1/cs/index.php?act=rssout&id=2');
$rss_parser->create();
?>