Location: PHPKode > projects > Buzzword > buzzword-0.8.12/planet/magpierss.inc
<?php

/**
 * MagpieRSS: a simple RSS integration tool
 * - Author: Kellan Elliott-McCrea <hide@address.com>
 * - Version: 0.51
 * - License: GPL
 *
 * This is a heavily modified (stripped down) version of MagpieRSS for 
 * use within buzzword only.
 *
 * The lastest version of MagpieRSS can be obtained from:
 * http://magpierss.sourceforge.net
 */
 
// change the name to planet_magpierss to avoid conflicts with existing installs of MagpieRSS
class planet_magpierss {
    var $parser;
    var $current_item = array();
    var $items = array();
    var $channel = array();
    var $textinput = array();
    var $image = array();
    var $parent_field = array('RDF');
    var $current_field = '';
    var $current_namespace = false;
    var $ERROR = "";

    function planet_magpierss($source) {
        // if PHP xml isn't compiled in, die
        if (!function_exists('xml_parser_create'))
            return;

        $this->parser = @xml_parser_create();
        if (!is_resource($this->parser))
            return;

        // pass in parser and a reference to this object
        xml_set_object($this->parser, $this);

        // setup handlers
        xml_set_element_handler($this->parser, 'start_element', 'end_element');
        xml_set_character_data_handler($this->parser, 'cdata'); 

        // parse the rdf
        if ( (!xml_parse($this->parser, $source)) && (xml_get_error_code($this->parser) != XML_ERROR_NONE) )
            return;

        // free memory
        xml_parser_free($this->parser);
    }
    
    function start_element($p, $element, &$attrs) {
        $element = strtolower($element);
                
        // check for a namespace, and split if found
        $namespace = false;
        if (strpos($element, ':'))
            list($namespace, $element) = split( ':', $element, 2); 

        $this->current_field = $element;
        if ( ($namespace) && ($namespace != 'rdf') )
            $this->current_namespace = $namespace;

        switch($element) {
            case 'channel':
            case 'items':
            case 'item':
            case 'textinput':
            case 'image':
            array_unshift($this->parent_field, $element);
            break;
        }
    }

    function end_element($p, $element) {
        $element = strtolower($element);
                            
        switch($element) {
            case 'item':
            $this->items[] = $this->current_item;
            $this->current_item = array();
            array_shift($this->parent_field);
            break;

            case 'channel':
            case 'items':
            case 'textinput':
            case 'image':
            array_shift($this->parent_field);
            break;
        }

        $this->current_field = '';
        $this->current_namespace = false;
    }

    function cdata($p, $text) {
        // skip item, channel, items first time we see them
        if ( ($this->parent_field[0] == $this->current_field) || (!$this->current_field) )
            return;

        if ($this->parent_field[0] == 'channel') {
            if ($this->current_namespace) {
                $this->append($this->channel[$this->current_namespace][$this->current_field], $text);
            } else {
                $this->append($this->channel[$this->current_field], $text);
            }
        } else if ($this->parent_field[0] == 'item') {
            if ($this->current_namespace) {
                $this->append($this->current_item[$this->current_namespace][$this->current_field], $text);
            } else {
                $this->append($this->current_item[$this->current_field], $text);
            }
        } else if ($this->parent_field[0] == 'textinput') {
            if ($this->current_namespace) {
                $this->append($this->textinput[$this->current_namespace][$this->current_field], $text);
            } else {
                $this->append($this->textinput[$this->current_field], $text);
            }
        } else if ($this->parent_field[0] == 'image') {
            if ($this->current_namespace) {
                $this->append($this->image[$this->current_namespace][$this->current_field], $text);
            } else {
                $this->append($this->image[$this->current_field], $text);
            }
        }
    }

    function append (&$str1, $str2='') {
        if (!isset($str1))
            $str1='';
        $str1 .= $str2;
    }
}

?>
Return current item: Buzzword