<?php

/**
 * @package eggblog
 * @author eggblog.net, tenfourzero@users.sourceforge.net
 * @version $Id: v4.0RC3 2008/01/21 11:25:04 tenfourzero Exp $
 * @copyright Copyright (c) 2005, http://www.eggblog.net
 * @license http://www.gnu.org/licenses/gpl.html GNU Lesser General Public License
 */


/**
* return xml formatted string
* @param string $input string to be encoded
* @return string
*/
function eb_xmlencode($string) {
	return str_replace(array('&','"',"'","’", '<','>','£','&amp;amp;','&amp;#'),array('&amp;','&quot;','&apos;','&apos;','&lt;','&gt;','&#163;','&amp;','&#'),html_entity_decode($string));
}


/**
* return rss xml
* @param int $id 0 for news, 1 for forum topics
* @return string
*/
function eb_rss($id) {
	global $config,$lang;
	$output ="<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n";
	$output.="<rss version=\"2.0\">\r\n";
	$output.="<channel>\r\n";
	$output.="\t<ttl>15</ttl>\r\n";
	$output.="\t<title>".eb_xmlencode($config['title']);
	 if($id==1) $output.=" | ".eb_xmlencode($lang['forums']);
	 $output.="</title>\r\n";
	$output.="\t<link>".eb_xmlencode($config['uri'])."</link>\r\n";
	$output.="\t<description>".eb_xmlencode($config['subtitle'])."</description>\r\n";
	$output.="\t<pubDate>".date("r")."</pubDate>\r\n";
	$output.="\t<lastBuildDate>".date("r")."</lastBuildDate>\r\n";
	$output.="\t<generator>www.eggblog.net</generator>\r\n";
	if($id==1) {
		foreach(eb_recentforum(10,0) as $row) {
			$output.="\t<item>\r\n";
			$output.="\t\t<title>".eb_xmlencode($row['topic_title'])."</title>\r\n";
			$output.="\t\t<link>".eb_xmlencode($config['uri'])."forum.php?topic=".$row['topic_id']."</link>\r\n";
			$output.="\t\t<description><p>".eb_xmlencode(str_replace("\r\n","<br />",str_replace("\r\n\r\n","</p><p>",$row['post_body'])))."</p></description>\r\n";
			$output.="\t\t<guid>".eb_xmlencode($config['uri'])."forum.php?topic=".$row['topic_id']."</guid>\r\n";
			$output.="\t\t<pubDate>".date("r",$row['post_date'])."</pubDate>\r\n";
			$output.="\t</item>\r\n";
		}
	}
	else {
		$sql="SELECT article_id,article_title,article_body,article_date FROM eb_articles WHERE article_flag!=0 ORDER BY article_flag DESC,article_date DESC LIMIT 10";
		$query=mysql_query($sql);
		while($row=mysql_fetch_array($query)) {
			$output.="\t<item>\r\n";
			$output.="\t\t<title>".eb_xmlencode($row[1])."</title>\r\n";
			$output.="\t\t<link>".eb_xmlencode($config['uri'])."news.php?id=".$row[0]."</link>\r\n";
			$output.="\t\t<description>".eb_xmlencode($row[2])."</description>\r\n";
			$output.="\t\t<guid>".eb_xmlencode($config['uri'])."news.php?id=".$row[0]."</guid>\r\n";
			$output.="\t\t<pubDate>".date("r",$row[3])."</pubDate>\r\n";
			$output.="\t</item>\r\n";
		}
	}
	$output.="</channel>\r\n";
	$output.="</rss>";
	return $output;
}

?>