<?php header('Content-type: application/rss+xml');

	require('cdbmysql.php');

	$db = new CDBMySQL("mysql.ludd.ltu.se", "pyrot", "xahhohx.ae", "pyrot");
	
	$useragent = $_SERVER['HTTP_USER_AGENT'];

	echo "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n";
	echo "<rss version=\"2.0\">\n";
	echo "  <channel>\n";
	echo "    <title>Pyrotekniska Sällskapet LTU</title>\n";
	echo "    <link>http://www.pyrot.com</link>\n";
	echo "    <description>Nyheter fran pyrotekniska sällskapet vid LTU</description>\n";
	echo "    <language>sv</language>\n";
	echo "    <copyright>Copyright 2005-" . date('Y', time()) . ", Styf Software Solutions</copyright>\n";
	echo "    <managingEditor>info@pyrot.com</managingEditor>\n";
	echo "    <webMaster>isak@styfsoftware.se</webMaster>\n";
	
	# Find the most recent post and use its date as the pub date ...
	$db->Query("SELECT datum FROM tbNews ORDER BY datum DESC LIMIT 1");
	$db->ReadRow();
	$db->SeekRow();
	
	$lastdate = $db->RowData["datum"];
	$dt = explode(" ", $lastdate);
	
	$thedate = $dt[0];
	$thetime = $dt[1];

	$dparts = explode("-", $thedate);
	$tparts = explode(":", $thetime);
	
	date_default_timezone_set('GMT');

	$thepubdate = date('D, d M Y H:i:s T', mktime($tparts[0], $tparts[1], $tparts[2], $dparts[1], $dparts[2], $dparts[0]));

	echo "    <pubDate>" . $thepubdate . "</pubDate>\n";
	echo "    <lastBuildDate>" . $thepubdate . "</lastBuildDate>\n";
	echo "    <category>Pyroteknik</category>\n";
	echo "    <generator>In house</generator>\n";
	echo "    <docs>http://cyber.law.harvard.edu/rss/rss.html</docs>\n";
	
	$query = "SELECT col_id, datum, rubrik, info FROM news ORDER BY datum DESC LIMIT 30";
	
	if (strstr($useragent, 'Apple-')) {
		$invchars = array("<", ">");
		$valchars = array("&lt;", "&gt;");
	} else {
		$invchars = array("<", ">", "&");
		$valchars = array("&lt;", "&gt;", "&amp;");
	}

	if ($db->Query($query) && $db->Size() > 0) {
		$db->SeekRow();

		while ($db->ReadRow()) {
			echo "    <item>\n";
			echo "      <title>" . $db->RowData['rubrik'] . "</title>\n";
			
			$rawinfo = $db->RowData['info'];
			$validinfo = str_replace($invchars, $valchars, $rawinfo);
			
			echo "      <description>" . $validinfo . "</description>\n";
			echo "      <author>info@pyrot.com</author>\n";
			
			$dt = explode(" ", $db->RowData["datum"]);
			$thedate = $dt[0];
			$thetime = $dt[1];

			$dparts = explode("-", $thedate);
			$tparts = explode(":", $thetime);

			$thepubdate = date('D, d M Y H:i:s T', mktime($tparts[0], $tparts[1], $tparts[2], $dparts[1], $dparts[2], $dparts[0]));

			//echo "      <guid>com.pyrot.news.id" . $db->RowData['col_id'] . "</guid>\n";
			echo "      <pubDate>" . $thepubdate . "</pubDate>\n";
			echo "      <source url=\"http://www.ludd.ltu.se/users/pyrot/rss.php\">Pyrot LTU</source>\n";
			echo "    </item>\n";
		}
	}

	echo "  </channel>\n";
	echo "</rss>\n";

?>