<?php
/**
 * @author     Thomas Meyer
 * @licence    http://creativecommons.org/licenses/by-nc-sa/2.5/
 * @package    yana
 * @subpackage plugins
 */

/**
 * @access     public
 * @package    yana
 * @subpackage plugins
 */
class plugin_rss extends plugin
{

    /* private string[] */ var $rss;
    /* "file" should be a valid path to an existing XML-File */
    /* private string */ var $file;
    /* maximum number of entries to show */
    /* private integer */ var $max;
    /* private string */ var $current_tag = "";
    /* private int */ var $current_entry = -1;

    /* constructor */ function plugin_rss($name)
    {
        settype($name,"string");
        $this->name = $name;
        
        global $YANA;
        $this->file = $YANA->getVar("PROFILE.RSS.FILE");
        $this->max  = $YANA->getVar("PROFILE.RSS.MAX");
        settype($this->file, "string");
        settype($this->max, "integer");
        
        if (!$this->max>0 ) {
            $this->max  = 5;
        }
        if (!$this->file>0) {
            $this->file = 'plugins/rss/test.rss';
        }
    
    }

    /* public boolean */ function _default(/* String */ $event, /* Hashtable */ $ARGS) 
    {
        /* nothing to do here */
        return true;
    }

    /* public boolean */ function get_news(/* Hashtable */ $ARGS) 
    {
        settype($ARGS, "array");

        global $YANA;
        $xml_parser = xml_parser_create();
        xml_set_element_handler($xml_parser, array(&$this,"_startElement"), array(&$this,"_endElement"));
        xml_set_character_data_handler($xml_parser, array(&$this,"_characterData"));
        if (!($fp = @fopen($this->file, "r"))) {
            die("<H1>ERROR 500</H1><P>Could not open XML input. Check if &quot;".$this->file."&quot; is a correct path to a valid XML file!</P>");
        }

        while ($data = fread($fp, 4096)) 
        {
            if (!xml_parse($xml_parser, $data, feof($fp))) {
                sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser));
                return false;
            }
        }
        xml_parser_free($xml_parser);
        $YANA->setVar("RSS", array_slice($this->rss,0,$this->max));
        $YANA->setVar("FILE", $this->file);
        return true;

    }

    /* private */ function _startElement($parser, $name, $attrs)
    {
        if (preg_match("/title|pubDate|description|link|author|category|comments/i", $name)) {
           $this->current_tag = strtoupper($name);
        }
        if (strtoupper($name) == "ITEM" && $this->current_entry == -1) {
            $this->current_entry = 0;
        }
    }

    /* private */ function _endElement($parser, $name)
    {
        if (strtoupper($name) == "ITEM") {
            $this->current_entry++;
        }
    }

    /* private */ function _characterData($parser, $data)
    {

        if ( trim($data) && $this->current_entry > -1) {
            if ($this->current_tag != "LINK") {
                $data = preg_replace("/'/", "\\'", utf8_decode($data));
            }
            settype($this->rss[$this->current_entry][$this->current_tag], "string");
            $this->rss[$this->current_entry][$this->current_tag] .= $data;
        }
    }

}

// © 2006, Thomas Meyer www.All-Community.de
// Copyright Hinweis nicht entfernen!
?>