<?php class MagpieRSS { var $parser; var $current_item = array(); var $items = array(); var $channel = array(); var $parent_field = array('RDF'); var $current_field = ''; var $current_namespace = false; function MagpieRSS ($source) { $this->parser = xml_parser_create( ); xml_set_object( $this->parser, &$this ); xml_set_element_handler($this->parser, 'start_element', 'end_element'); xml_set_character_data_handler( $this->parser, 'cdata' ); xml_parse( $this->parser, $source ); xml_parser_free( $this->parser ); } function start_element ($p, $element, &$attrs) { $element = strtolower( $element ); $namespace = false; if ( strpos( $element, ':' ) ) { list($namespace, $element) = split( ':', $element, 2); } $this->current_field = $element; if ( $namespace and $namespace != 'rdf' ) { $this->current_namespace = $namespace; } if ( $element == 'channel' ) { array_unshift( $this->parent_field, 'channel' ); } elseif ( $element == 'items' ) { array_unshift( $this->parent_field, 'items' ); } elseif ( $element == 'item' ) { array_unshift( $this->parent_field, 'item' ); } } function end_element ($p, $element) { $element = strtolower($element); if ( $element == 'item' ) { $this->items[] = $this->current_item; $this->current_item = array(); array_shift( $this->parent_field ); } elseif ( $element == 'channel' or $element == 'items' ) { array_shift( $this->parent_field ); } $this->current_field = ''; $this->current_namespace = false; } function cdata ($p, $text) { if ( $this->parent_field[0] == $this->current_field or ! $this->current_field ) { return; } elseif ( $this->parent_field[0] == 'channel') { if ( $this->current_namespace ) { $this->channel[ $this->current_namespace ][ $this->current_field ] .= $text; } else { $this->channel[ $this->current_field ] .= $text; } } elseif ( $this->parent_field[0] == 'item' ) { if ( $this->current_namespace ) { $this->current_item[ $this->current_namespace ][ $this->current_field ] .= $text; } else { $this->current_item[ $this->current_field ] .= $text; } } } function show_list () { echo "<ol>\n"; foreach ($this->items as $item) { echo "<li>", $this->show_item( $item ); } echo "</ol>"; } function show_channel () { echo "channel:<br>"; echo "<ul>"; while ( list($key, $value) = each( $this->channel ) ) { echo "<li> $key: $value"; } echo "</ul>"; } function show_item ($item) { echo "item: $item[title]"; echo "<ul>"; while ( list($key, $value) = each($item) ) { if ( is_array($value) ) { echo "<br><b>$key</b>"; echo "<ul>"; while ( list( $ns_key, $ns_value) = each( $value ) ) { echo "<li>$ns_key: $ns_value"; } echo "</ul>"; } else { echo "<li> $key: $value"; } } echo "</ul>"; } } # end class RSS class RSSCache { var $BASE_CACHE = './cache'; function RSSCache ($base) { if ( $base ) { $this->BASE_CACHE = $base; } if ( ! file_exists( $this->BASE_CACHE ) ) { mkdir( $this->BASE_CACHE, 0755 ); } } function set ($url, $rss) { $cache_file = $this->file_name( $url ); $fp = @fopen( $cache_file, 'w' ); if ( ! $fp ) { return array(0, "Unable to open cache file for writing: $cache_file"); } $data = $this->serialize( $rss ); fwrite( $fp, $data ); fclose( $fp ); return array(1, $cache_file); } function get ($url) { $cache_file = $this->file_name( $url ); if ( ! file_exists( $cache_file ) ) { return array(0, '', "Cache file does not exists: $cache_file"); } $fp = @fopen($cache_file, 'r'); if ( ! $fp ) { return array(0, '', "Failed to open cache file for reading: $cache_file"); } $data = fread( $fp, filesize($cache_file) ); $rss = $this->unserialize( $data ); return array(1, $rss, 'cache.success'); } function check_cache ( $url ) { $filename = $this->file_name( $url ); if ( file_exists( $filename ) ) { $mtime = filemtime( $filename ); return array(1, $filename, $mtime); } return array(0); } function serialize ( $rss ) { return serialize( $rss ); } function unserialize ( $data ) { return unserialize( $data ); } function file_name ($url) { $filename = md5( $url ); return join( '/', array( $this->BASE_CACHE, $filename ) ); } } $CACHE_BASE = './cache'; $CACHE_AGE = 60*60; // one hour function fetch_rss ($url, $options=array() ) { global $CACHE_BASE, $CACHE_AGE; if ( $options['cache.no'] ) { list($remote_status, $rss, $msg) = fetch_remote_rss( $url ); if ( $remote_status and $rss ) { return array($rss, 1, 'success.remote'); } else { return array($msg, 0, 'fail.no_cache'); } } else { $base = ( $options['cache.base'] ) ? $options['cache.base'] : $CACHE_BASE; $cache_age = ( $options['cache.age'] ) ? $options['cache.age'] : $CACHE_AGE; $cache = new RSSCache( $base ); list( $cache_status, $filename, $mtime) = $cache->check_cache( $url ); $age = time() - $mtime; if ( $cache_status and $cache_age > $age ) { list($status, $rss, $msg) = $cache->get( $url ); if ( $status and $rss ) { return array($rss, 1, 'success.cache'); } } list($remote_status, $rss, $remote_msg) = fetch_remote_rss( $url ); if ( $remote_status and $rss ) { $cache->set( $url, $rss ); return array($rss, 1, 'success.remote'); } if ( $cache_status ) { $rss = $cache->get( $url ); if ( $rss ) { return array($rss, 1, 'success.cache.stale' ); } } return array($remote_msg, 0, 'fail'); } } function fetch_remote_rss ($url) { list($status, $data) = fetch_remote_file( $url ); if ( $status ) { $rss = new MagpieRSS( $data ); if ( $rss ) { return array(1, $rss, 'success'); } else { return array(0, '', "Failed to parse RSS file: $url"); } } else { return array(0, '', $data); } } function fetch_remote_file ($url) { $fp = @fopen($url, 'r'); if ( ! $fp ) { return array(0, "Unable to open remote file: $url"); } while ( ! feof($fp) ) { $data .= fread( $fp, 1024 ); } fclose($fp); return array(1, $data); } $url = $_GET['url']; if ( ! $url ) { // $url einfach anpassen $url = 'http://www.php-space.info/script_feed.xml'; } list( $rss, $status, $msg) = fetch_rss( $url ); function slashbox ($rss) { // $trenner einfach anpssen $trenner=';'; $title = $rss->channel['title']; $link = $rss->channel['link']; echo 'link'.$trenner.'title'.$trenner.'description'.$trenner; $description = $rss->channel['description']; foreach ($rss->items as $item ) { echo $item["link"].$trenner; echo $item["title"].$trenner; echo $item["description"].$trenner; } } slashbox ($rss); ?> |