<?php

/*
||==========================================================
||		Fusion News 3.x
||----------------------------------------------------------
|| File Version	: $Id: rss.php 187 2007-11-18 21:02:57Z xycaleth $
|| Contact	: xycaleth@gmail.com
|| Copyright:	: (c) 2006 - 2007, FusionNews.net
|| License Info	: http://www.gnu.org/copyleft/gpl.html
||==========================================================
*/

if ( !defined ('FNEWS_ROOT_PATH') )
{
	define ('FNEWS_ROOT_PATH', str_replace ('\\', '/', dirname (__FILE__)) . '/');
	include_once FNEWS_ROOT_PATH . 'common.php';
}

if ( !$enable_rss )
{
	exit;
}

$fn_category = ( isset ($_GET['fn_category']) ) ? intval ($_GET['fn_category']) : 0;

$file = arrange_by_date();

$total_posts = sizeof ($file);
if ( $total_posts <= 0 )
{
	exit;
}

$current_date = date ('r');

header ('Content-Type: text/xml');

$rss_url = $furl . '/rss.php';
if ( $fn_category > 0 )
{
	$rss_url .= '?fn_category=' . $fn_category;
}

echo <<< rss
<?xml version="1.0" encoding="$rss_encoding"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">

<channel>
	<title>$rss_title</title>
	<link>$hurl</link>
	<description>$rss_description</description>
	<pubDate>$current_date</pubDate>
	<lastBuildDate>$current_date</lastBuildDate>
	<generator>Fusion News</generator>
	<atom:link href="$rss_url" type="application/rss+xml" rel="self" />

rss;

if ( $fn_category > 0 )
{
	$file = file (FNEWS_ROOT_PATH . 'categories.php');
	array_shift ($file);

	$category_exists = false;
	foreach ( $file as $value )
	{
		$category = explode ('|<|', $value);
		if ( $category[0] == $fn_category )
		{
			$category_exists = true;
			echo "\t" . '<category>' . $category[1] . '</category>' . "\n";
		}
	}
	
	if ( !$category_exists )
	{
		// Fall back
		$fn_category = 0;
	}
}

$file = arrange_by_date();
foreach ( $file as $newsdata )
{
	list ($id, $timestamp, $writer, $subject, $categories) = explode ('|<|', $newsdata);
	
	if ( file_exists (FNEWS_ROOT_PATH . 'news/news.' . $id . '.php') )
	{
		$news_file = file (FNEWS_ROOT_PATH . 'news/news.' . $id . '.php');
		array_shift ($news_file);
		
		list ($shortnews, $fullnews, $writer, $subject, $description, $categories, $timestamp, $num_comments, $id) = explode ('|<|', $news_file[0]);
		
		if ( $fn_category === 0 || in_array ($fn_category, explode (',', $categories)) )
		{
			$news_info = parse_news_to_view ($news_file[0]);
			$writer = findwriter ($writer);
			$news_info['date'] = date ('r', $timestamp);
			$news_info['subject'] = strip_tags ($news_info['subject']);
			$news_info['subject'] = unhtmlentities ($news_info['subject']);
			$author = ( $news_info['email'] ) ? "\n\t\t" . '<author>' . $news_info['email'] . ' (' . $writer['nick'] . ')</author>' : '';
			$description = ( empty ($description) ) ? strip_tags (array_shift (explode ("\n", $news_info['news']))) : $description;
			
			echo <<< rss
	<item>
		<title>{$news_info['subject']}</title>
		<link>{$hurl}?fn_mode=fullnews&amp;fn_id={$id}</link>
		<description><![CDATA[$description]]></description>$author
		<pubDate>{$news_info['date']}</pubDate>
		<guid isPermaLink="false">fus_{$id}-$timestamp</guid>
	</item>

rss;
		}
	}
}

echo <<< rss
</channel>

</rss>
rss;

?>