<?php
	// -----------------------------------------------------------------------
	// rss092feed.php - generates an RSS 0.92 feed from greymatter files.
	//
	// To add an RSS 0.92 feed to your greymatter weblog, put this file on
	// your server and edit the values below to configure it.
	//
	// This script is written in PHP4, so you will need that. Possibly it
	// will work in older PHP versions.
	// Use this script at your own risk. Also, I do not offer support with
	// it. If it doesn't work, or it's too hard to get it working, bad luck.
	//
	// Written by Stefan Mensink (stefan at mensink dot nl)
	// URL: http://www.mensink.nl/projects/gm-rss/
	// -----------------------------------------------------------------------

	// the directory where the gm-entrylist is found
	$cgidir = "/data/web/stefan/nutteloze.info/cgi-bin/";

	// the greymatter archives directory
	$archivedir = "/data/web/stefan/nutteloze.info/archives/";

	// the base URL for the archives directory on your website
	$archivewebdir = "http://nutteloze.info/archives/";

	// the extension for your archived entries
	$archiveext = ".shtml";

	// the title/name of your weblog
	$log_title = "nutteloze.info";

	// the main URL for your weblog
	$log_link = "http://nutteloze.info";

	// the description for your weblog
	$log_description = "Zooi die u niet eens wilt weten";

	// your weblog's language
	$log_language = "nl";

	// the log editor
	$log_editor = "crusher <crusher@nutteloze.info>";

	// the log webmaster
	$log_webmaster = "crusher <crusher@nutteloze.info>";

	// optionally a logo image for your log, comment this out if you have no logo
	$log_logo = "http://nutteloze.info/img/logo.gif";

	// the maximum amount of characters per post you want in the feed, 0 for no limit
	$summary_length = 1000;

	// the maximum amount of posts you want listed in the feed
	$list_length = 15;

	// -----------------------------------------------------------------------
	// the script starts here
	// DO NOT CHANGE ANYTHING BELOW THIS POINT
	// -----------------------------------------------------------------------

	function xmlentities($data) 
	{
		$position = 0;
		$length = strlen($data);
		$escapeddata = "";
		for ($position=0; $position<$length; $position++) {
			$character = substr($data, $position, 1);
			$code = ord($character);
			switch($code) {
				case 34:
				$character = "&quot;";
				break;

				case 38:
				$character = "&amp;";
				break;

				case 39:
				$character = "&apos;";
				break;

				case 60:
				$character = "&lt;";
				break;

				case 62:
				$character = "&gt;";
				break;

				default:
				if ($code<32 || $code>127)
				$character = ("&#".strval($code).";");
				break;
			}
			$escapeddata .= $character;
		}
		return $escapeddata;
	}

	function writerss($items) {
		global $log_title, $log_link, $log_description, $log_language, $log_editor, $log_webmaster, $log_logo;

		echo "<?xml version=\"1.0\" ?>\n";
		echo "<rss version=\"0.92\">\n";
		echo "	<channel>\n";
		echo "		<title>$log_title</title>\n";
		echo "		<link>$log_link</link>\n";
		echo "		<description>".xmlentities($log_description)."</description>\n";
		echo "		<language>".$log_language."</language>\n";
		echo "		<managingEditor>".xmlentities($log_editor)."</managingEditor>\n";
		echo "		<webMaster>".xmlentities($log_webmaster)."</webMaster>\n";
		echo "		<lastBuildDate>".$items[0]['date']."</lastBuildDate>\n";
		echo "		<docs>http://backend.userland.com/rss092</docs>\n";
		if (isset($log_logo)) {
			echo "		<image>\n";
			echo "			<title>$log_title</title>\n";
			echo "			<url>$log_logo</url>\n";
			echo "			<link>$log_link</link>\n";
			echo "			<description>".xmlentities($log_description)."</description>\n";
			echo "		</image>\n";
		}
		foreach ($items as $item) {
			echo "		<item>\n";
			echo "			<title>".xmlentities($item['title'])."</title>\n";
			echo "			<link>".$item['url']."</link>\n";
			echo "			<description>".xmlentities($item['text'])."</description>\n";
			echo "		</item>\n";
		}
		echo "	</channel>\n";
		echo "</rss>\n";
	}

	function getitem($num) {
		global $archivedir, $archivewebdir, $archiveext, $summary_length;
		$itemfile = "{$archivedir}{$num}.cgi";

		$result['url'] = "{$archivewebdir}{$num}{$archiveext}";

		if (file_exists("{$itemfile}")) {
			$fp1 = fopen("{$itemfile}", "r");
			fgets($fp1, 4096);
			fgets($fp1, 4096);
			$data = fgets($fp1, 4096);
			$inarray = explode("|", $data);
			$text = "";
			foreach ($inarray as $piece) {
				if ($piece == "*") $text .= " ";
				else $text .= $piece;
			}
			$description = strip_tags($text);
			if ($summary_length > 0) {
				$len = strlen($description);
				if ($len > $summary_length) {
					$description = substr($description, 0, $summary_length - 3);
					$description .= "...";
				}
			}
			fclose($fp1);
			$result['text'] = $description;
		}

		return $result;
	}

	header("Content-Type: text/xml");
	if (file_exists("{$cgidir}gm-entrylist.cgi")) {
		$fp = fopen("{$cgidir}gm-entrylist.cgi", "r");
		for ($count = 0; !feof($fp) && $count < $list_length; $count++) {
			$line = trim(fgets($fp, 4096)); // upgrade php to 4.2.0+
			if (! $line) continue;

			$pieces = explode("|", $line);
			$num = sprintf("%08d", $pieces[0]);

			$item = getitem($num);
			$item['title'] = $pieces[2];
			$item['date'] = "{$pieces[3]} {$pieces[4]}";

			$items[] = $item;
		}
		fclose($fp);
		writerss($items);
	}
?>
