<?php

header('Content-type: application/rss+xml; charset=utf-8');

# Determine test or production dir
if (!file_exists("../../test/")) {$test = "test/";} else {$test = "";}

#include siteconfig
include("../../siteconfig.inc");

# include functions
include("../../functions/database.inc");

function fixoutput($str){
    $good[] = 9;  #tab
    $good[] = 10; #nl
    $good[] = 13; #cr
    for($a=32;$a<127;$a++){
        $good[] = $a;
    }   
    $len = strlen($str);
    for($b=0;$b < $len+1; $b++){
        if(in_array(ord($str[$b]), $good)){
            $newstr .= $str[$b];
        }//fi
    }//rof
    return $newstr;
}

$page1 = explode("/pages/", $PHP_SELF);
$page2 = $page1[1];
$page3 = explode("/", $page2);
$page = $page3[0];


# Determine current page & key
$RSS_KEY = array_search($page, $PAGE_DIR);

echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
echo "<rss version=\"2.0\" xmlns:content=\"http://purl.org/rss/1.0/modules/content/\">";
echo "
<channel>
	<title>$SITE_NAME $page</title>
	<link>http://$SITE_URL"."/$test</link>
	<description>$SITE_NAME</description>
	<generator>$SITE_NAME</generator>
	";
	
$query[get_current_blog] = "SELECT * FROM tbl".$PAGE_TABLELABEL[$RSS_KEY]."_Entries
							ORDER BY EntryDateTime DESC
							LIMIT 0, 5";
$query[gcb_result] = mysql_query($query[get_current_blog]);
while($query[gcb_row] = mysql_fetch_array($query[gcb_result])) {
	extract($query[gcb_row]);

		$EntryContent = nl2br($EntryContent);

		$EntrySubject = str_replace("&","&amp;",$EntrySubject);
		$EntryContent = str_replace("&","&amp;",$EntryContent);
		
		$EntrySubject = str_replace("<","&lt;",$EntrySubject);
		$EntryContent = str_replace("<","&lt;",$EntryContent);

		$EntrySubject = str_replace(">","&gt;",$EntrySubject);
		$EntryContent = str_replace(">","&gt;",$EntryContent);
		
		$EntrySubject = str_replace("'","&apos;",$EntrySubject);
		$EntryContent = str_replace("'","&apos;",$EntryContent);

		echo "
	<item>
		<title>$EntrySubject</title>
		<link>http://$SITE_URL"."/$test"."index.php?page=$page&amp;screen=show_entry&amp;entry=$EntryRowID</link>
		<author>$SITE_EMAIL ($EntryAuthor)</author>
		<pubDate>".date("D, d M Y H:i:s", strtotime($EntryDateTime))." EST</pubDate>
		<guid>http://$SITE_URL"."/$test"."index.php?page=$page&amp;screen=show_entry&amp;entry=$EntryRowID</guid>
		<description>";
		
			if (file_exists("images/entries/$EntryRowID.jpg")) 
		{echo "&lt;img  src=\"http://$SITE_URL/$test"."pages/$page/images/entries/$EntryRowID.jpg\"&gt; &lt;BR&gt;&lt;BR&gt;";}
		
		$EntryContent = fixoutput($EntryContent);
		
		echo "$EntryContent
		&lt;BR&gt;&lt;BR&gt;";
		
		if ($EntryTags != "") {
	echo "tags: ";
	$tags = explode(",", "$EntryTags");
	
	foreach($tags AS $t_key => $t_id) { 
		 $t_id = trim($t_id); 
		if ($tg == "") {$tg = "1"; echo "&lt;a href=\"http://$SITE_URL"."/$test"."index.php?page=$page&amp;tag=$t_id\"&gt;$t_id&lt;/a&gt;";}
		else 
		{echo ", &lt;a href=\"http://$SITE_URL"."/$test"."index.php?page=$page&amp;tag=$t_id\"&gt;$t_id&lt;/a&gt;";}
	
	}
	$tg = "";
	}
		
		echo "
		</description>

	</item>			
			";
					
			
}
	
echo "			
</channel>
</rss>";

?>
