<?php
/*
   Created by: C.R. Andrews, Jr.

   Substantially based on "RssFeedDisplay", the Christophe David version modified on 26 Jul 2005
		http://www.pmwiki.org/wiki/Cookbook/RssFeedDisplay

   Adds (:RSS :) markup to PmWiki using SimplePie, a PHP class that performs RSS feed parsing
		http://simplepie.org/

   Download the latest version of SimplePie, extract "simplepie.inc" and place it in the
   same Cookbook directory as this script.

   In config.php, add the following line to match your recipe location:
	include_once('cookbook/rss/simplepiedisplay.php');

   Change four values in this file: SimplePie location, $RSS_Cache, $Cache_Duration,
	and $Order_By_Date; to match your installation

   Add the following mark-up to your PmWiki page:
	(:RSS feed_url; show_items_num; show_chars_num :)
		feed_url = the URL of the feed to display
		show_items_num = number of news items in the feed to display
		show_chars_num = maximum amount of characters of an item's description to display
*/
$RecipeInfo['SimplePieDisplay']['Version'] = '2008-07-28';

Markup('rssdisplay', 'fulltext', '/\(:RSS\s*(.*?)\s*:\)/e',"RSS('\$1')");

function RSS($regex) {

//----- Change the four values below as needed for your installation -----
	include_once('simplepie.inc');			// Location of SimplePie,
							//   currently set to same directory as this script
	$RSS_Cache = '/mnt/data_a/www/cache';		// Location of RSS cache directory
	$Cache_Duration = '1800';			// Minimum time in seconds before next cache update
	$Order_By_Date = 'true';			// Order feed items by date?

//----- You generally would not need to change anything below this line ------
	$output = '';
	// 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);
	if ($regex) {
		global $action;
		global $FarmD;
		$parameters = preg_split("/\s+/", $regex);
		if (($action == 'browse') && (isset($parameters[0]))) {
			include_once('simplepie.inc');
			$feed = new SimplePie();
			$feed->set_feed_url($parameters[0]);
			$show_items_num = $parameters[1];	// Show how many items per feed?
			$show_chars_num = $parameters[2];	// Show how many characters per item?
			$feed->set_cache_location($RSS_Cache);
			$feed->set_cache_duration($Cache_Duration);
			$feed->set_timeout(5);
			$feed->enable_order_by_date('$Order_By_Date');
			$feed->strip_htmltags(array_merge($feed->strip_htmltags, array('img', 'pre')));
			$feed->remove_div(true);
			$feed->force_feed(true);
			$feed->set_useragent('Mozilla/4.0 '.SIMPLEPIE_USERAGENT);
			$feed->init();
			$feed->handle_content_type();
			foreach ($feed->get_items(0, $show_items_num) as $item):
				$title = $item->get_title();
				$url = ltrim($item->get_permalink());
				$date = $item->get_date('j F Y');
				$time = $item->get_date('g:i a');
				$description = trim($item->get_description());
				$description = substr($description, 0, $show_chars_num);
				$description = rtrim($description, '<p></p>');
				$output .= "<span class='rss_title'>" .$title. "</span>\\\\\n";
				if ($date != '') $output .= "<span class='rss_date'>Posted: " .$date. "&nbsp; | &nbsp;" .$time. "</span>\\\\\n";
				if ($description == '')	$output .= "<span class='rss_description'>[[ " .$url. " | more...]]</span>\\\\\n";
					else $output .= "<span class='rss_description'>" .$description. "&nbsp;&nbsp;&nbsp;[[ " .$url. " | more...]]</span>\\\\\n";
				$output .= "\\\\\n";
			endforeach;
		} // if (($action == 'browse')
		error_reporting($OriginalError_reportingLevel);
	} // if ($regex)
return $output;
} // function
?>