<?php
/**
 * @version $Id: rss.php v2.0 $
 * @package PBDigg
 * @copyright Copyright (C) 2007 - 2008 PBDigg.com. All Rights Reserved.
 * @license PBDigg is free software and use is subject to license terms
 */

define('PB_CSCRIPT', 'rss');
require_once('include/common.inc.php');

$pagetitle = $sql = '';
if (!$action) $action = 'index';
if (!in_array($action, array('index', 'cate', 'user', 'tag')))
{
	exit('Access Denied!');
}
if ($action == 'cate' && is_numeric($cid))
{
	$cid = intval($cid);
	$sql .= ' AND cid = '.$cid;
	$pagetitle = $categories[$cid]['name'];
}
elseif ($action == 'tag' && isset($tagname))
{
	$tag = intval($tag);
	$tid = '';
	$tagname = str_replace(array("\\","&","'","\"","/","*",",","<",">","\r","\t","\n","#","$","(",")","%","@","+","?",";","^"), '', trim($_GET['tagname']));
	$SQL = "SELECT tc.tid 
			FROM {$db_prefix}tagcache tc, {$db_prefix}tags t 
			WHERE tc.tagid = t.tagid 
			AND t.tagname = '$tagname'";
	$DB->db_query($SQL);
	while ($result = $DB->db_fetch_array())
	{
		$tid .= $result['tid'].',';
	}
	if ($tid)
	{
		$tid = substr($tid, 0, -1);
	}
	$sql .= ' AND tid IN ('.$tid.')';
	$pagetitle = $tagname;
}
elseif ($action == 'user' && is_numeric($uid))
{
	$uid = intval($uid);
	$sql .= ' AND uid = '.$uid;
	$rs = $DB->db_fetch_one_array("SELECT username FROM {$db_prefix}members WHERE uid = $uid");
	$pagetitle = $rs['username'];
}
$rss = $rs = array();
$SQL = "SELECT tid, cid, author, uid, subject, content, postdate 
		FROM {$db_prefix}threads 
		WHERE ifcheck = 1".$sql." 
		ORDER BY postdate DESC 
		LIMIT 0, ".intval($pb_aperpage);
$DB->db_query($SQL);
if ($DB->db_num())
{
	$content_replacement = array('[strike]', '[/strike]', '[br]', '[pre]', '[/pre]', '[acronym]', '[/acronym]', '[address]', '[/address]', '[ul]', '[ol]', '[li]', '[/ul]', '[/ol]', '[/li]', '[div]','[/div]','[p]','[/p]','[span]','[/span]','[tr]','[/tr]','[/td]','[/table]','[code]','[/code]','[b]','[/b]','[i]','[/i]','[u]','[/u]','[img]','[/img]','[/font]','[/size]','[/color]','[/align]','[/url]','[/quote]','[/media]','[/flash]','[/email]');
	$content_preg_replacement = array('#\[font(.*?)\]#is','#\[table(.*?)\]#is','#\[td(.*?)\]#is','#\[size(.*?)\]#is','#\[color(.*?)\]#is','#\[align(.*?)\]#is','#\[url(.*?)\]#is','#\[quote(.*?)\]#is','#\[media(.*?)\]#is','#\[flash(.*?)\]#is','#\[email(.*?)\]#is');
	$subject_replacement = array('[b]','[/b]','[/color]');
	$subject_preg_replacement = array('#\[color(.*?)\]#is',);
	while ($rs = $DB->db_fetch_array())
	{
		$rs['postdate'] = gdate($rs['postdate']);
		$rs['content'] = str_replace($content_replacement, '', preg_replace($content_preg_replacement, '', $rs['content']));
		$rs['subject'] = str_replace($subject_preg_replacement, '', preg_replace($subject_replacement, '', $rs['subject']));
		$rs['cate'] = $categories[$rs['cid']]['name'];
		$rss[] = $rs;
	}
}
header("Content-Type: application/xml");
$rssTitle = "<?xml version=\"1.0\" encoding=\"$db_charset\"?>\n" .
			"<rss version=\"2.0\">\n" .
			"<channel>\n" .
			"<title>$pb_sitename</title>\n" .
			"<link>$pb_siteurl</link>\n" .
			"<copyright>Copyright (C) $pb_sitename </copyright>\n" .
			"<generator>PBDIGG Version ".$_PBENV['PB_VERSION'][0]." Build ".$_PBENV['PB_VERSION'][1]."</generator>\n" .
			"<lastBuildDate>".gdate($timestamp, 'r')."</lastBuildDate>\n";
			"<description>".$pagetitle.$u_message['rss']."</description>\n";
			"<ttl>".$pb_cachetime."</ttl>\n";
echo $rssTitle;
foreach ($rss as $key => $value)
{
	$rsscontent = "<item id=\"$key\">\r\n<title><![CDATA[".$value['subject']."]]></title>\r\n<link>".$_PBENV['PB_URL']."show.php?tid=".$value['tid']."</link>\r\n<description><![CDATA[\n".$value['content']."\n]]></description>\r\n<pubDate>".$value['postdate']."</pubDate>\r\n</item>\r\n";
	echo $rsscontent;
}
echo "</channel>\r\n</rss>";
exit;
?>