<?php

/* RSS.php script: created by Richard Bos */

/* Feedcreator class is from : http://www.bitfolge.de/rsscreator-en.html 
  Licensed under the LGPL.*/

include ("feedcreator.class.php");

$rss = new UniversalFeedCreator();
/* Here you can give the main description for your RSS feeds */
$rss->description = "Changes for my school website";

// include database configuration file and connect
include ("../configuration/database.inc.php");

$link = mysql_connect("$db_server", "$db_username", "$db_password");
if (!$link)
{
	die("Could not connect: " . mysql_error());
}

$db_selected = mysql_select_db("$db_name", $link);
if (!$db_selected)
{
	die("Can't use database '$db_name': " . mysql_error());
}

$query = "SELECT config_value FROM $table_configuration WHERE config_key LIKE 'sitename'";
$result = mysql_query($query) or die_script(mysql_error());
$sitename = mysql_result($result, 0);
unset ($query);
unset ($result);

$query = "SELECT config_value FROM $table_configuration WHERE config_key LIKE 'url'";
$result = mysql_query($query) or die_script(mysql_error());
$site = mysql_result($result, 0);
unset ($query);
unset ($result);

$rss->useCached(); // use cached version if age<1 hour
$rss->title = $sitename;

//optional
$rss->descriptionTruncSize = 500;
$rss->descriptionHtmlSyndicated = true;

$rss->link = "$site";
$rss->syndicationURL = "$site/" . $_SERVER["PHP_SELF"];

if (0)
{
	$image = new FeedImage();
	$image->title = "dailyphp.net logo";
	$image->url = "http://www.dailyphp.net/images/logo.gif";
	$image->link = "http://www.dailyphp.net";
	$image->description = "Feed provided by dailyphp.net. Click to visit.";

	//optional
	$image->descriptionTruncSize = 500;
	$image->descriptionHtmlSyndicated = true;

	$rss->image = $image;
}

$res = mysql_query("SELECT id, name, date_format(lastupdate,'%Y-%m-%dT%H:%i:%s+01:00') AS date, section, content, module
                    FROM $table_pages
                    ORDER BY lastupdate DESC
                    LIMIT 20");

while ($data = mysql_fetch_object($res))
{
	$item = new FeedItem();
	$item->title = $data->name;
	$item->link = $site . "index.php?section=$data->section&page=$data->id";

	if ($data->content == "")
	{
		switch ($data->module)
		{
			case 'sn_gbook/gbook.php' :
				$result = mysql_query("SELECT message FROM sn_m_gbook ORDER BY date DESC");
				$message = mysql_result($result, 0);
				$item->description = $message;
				break;
			case 1 :
				echo "i equals 1";
				break;
			case 2 :
				echo "i equals 2";
				break;
		}
	}
	else
	{
		$item->description = substr(strip_tags($data->content), 0, 500);
		//print substr($data->content,0,20);
	}

	// optional
	// $item->descriptionTruncSize = 50;
	// $item->descriptionHtmlSyndicated = true;

	$item->date = $data->date;
	$item->author = $data->author;
	$item->source = $data->source;

	$rss->addItem($item);
}

// valid format strings are: RSS0.91, RSS1.0, RSS2.0, PIE0.1 (deprecated),
// MBOX, OPML, ATOM, ATOM0.3, HTML, JS
//echo $rss->saveFeed("rss2.0", "rss/rss.xml");
echo $rss->outputFeed("rss2.0");
?>

