<?php
/*
     http://www.brambring.nl

     $Id: rssdisplay.php,v 1.30 2005/10/11 09:29:14 pts00065 Exp $

    Syntax
    [[$RSS http://url.to.rss.feed/feed.rss <what> <parameters>]]

    Examples:
    displays the feed of the pmwiki pages with the defauft layout and number of lines:
    [[$RSS http://www.pmichaud.com/wiki/Pm/AllRecentChanges?action=rss]]  
    
    displays the pmwiki feed in long layout and  the top 5 lines 
    [[$RSS http://www.pmichaud.com/wiki/Pm/AllRecentChanges?action=rss long 5]]  

    displays the pmwiki feed in short layout and  all lines in the feed
    [[$RSS http://www.pmichaud.com/wiki/Pm/AllRecentChanges?action=rss short -1]]  
*/

Markup('rssdisplay', 'fulltext', '/\(:RSS\s*(.*?)\s*:\)/e',"RSS('\$1')");
/* just some example 
$HTMLStylesFmt['rss']= "
.rss a {color:blue;font-weight:normal;text-decoration:none}
.rss ul {padding-left:6px; list-style-type:none; }
.rss  {color:red; }
.rss {border:pink 1px dashed;}
";
*/

define('MAGPIE_CACHE_AGE', 2*60*60);
define('MAGPIE_CACHE_DIR', "$FarmD/cache");
define('MAGPIE_FETCH_TIME_OUT', 15);
define('MAGPIE_GZIP', true);


function RSS($regex) {
global $action;
global $FarmD;
$OriginalError_reportingLevel = error_reporting();
error_reporting(0);
# If you make a call to your own site
# and you have any kind of WritePage action ( Lock(2) ) for example
# because you do some kind of logging in a wiki-page.
# the whole thing will deadlock, so remove any locks.
# ReadPage will request a lock again when needed
Lock(-1);
$line="";
if ( $regex == '' ) {
 $line.="Empty parameter in RSS ";
} else {
  $tokens=preg_split("/\s+/", $regex);

  $url      =$tokens[0];
  $what     =$tokens[1];
  $num_items=$tokens[2];

  if ( $what == '' ) {
    $what='short';
  }

  if ( $num_items == 0 ) {
    $num_items=10;
  }
  if ( $action && (  $action != 'browse'  ) ) {
    # this is important
    #pmwiki processes DoubleBrackets in the rss module
    #recursion untill the server blows
    $line.= "RSS Feed : $url";
  } else {
    include_once("$FarmD/local/magpie/rss_fetch.inc");
    $rss = fetch_rss($url);
    if ($rss) {
      $title = htmlspecialchars($rss->channel['title']);
      $link = htmlspecialchars($rss->channel['link']);
      $line .= "<h2 class='rss'>Rss feed van <a href='$link'>$title</a></h2>\n";
      if ( $num_items > 0 ) { 
        $items = array_slice($rss->items, 0, $num_items);
      } else {
        $items = $rss->items;
      }
      foreach ($items as $item) {
        #print_r ($item);
        $href = htmlspecialchars($item['link']);
        $title = htmlspecialchars($item['title']);	
        if ( isset ($item['description']) ) {
          $description = $item['description'];	
        }
        if ( isset ($item['atom_content']) ) {
          $description = $item['atom_content'];	
        }
        $description=htmlspecialchars($description);
        $link="<a class='urllink' href='$href'>$title</a>";
        $line .= "<h3>$link</h3>\n";
        if ( $what == 'long' ) { 
          $line .=  $description . "<br />\n";
        }
      }
    }
  }
}
error_reporting($OriginalError_reportingLevel);
$line="<div class='rss'>$line</div>";
return Keep($line);
}
?>