<?
include ($_SERVER['DOCUMENT_ROOT']."/caching_functions.php");

$no_micro_optims = True;

$cachefile = start_caching("feed.xml");

include ($_SERVER['DOCUMENT_ROOT']."/functions.php");

if (!blog_enabled) {
	header('HTTP/1.1 404 Not Found');
	exit();
}

$sitewideID = "tag:$url,2010:feed";

$postInfo = $mysqli->query("SELECT posts.id, title, content, categories.name as category, users.username, UNIX_TIMESTAMP(created) as created, UNIX_TIMESTAMP(modified) as modified FROM posts, categories, users WHERE posts.category = categories.id AND posts.author = users.id ORDER BY modified DESC LIMIT 10");

$recentPostInfo = $mysqli->query("SELECT UNIX_TIMESTAMP(modified) as modified FROM posts ORDER BY modified DESC LIMIT 1");
$recentPostObject = $recentPostInfo->fetch_object();

header("Content-type: text/xml");

echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>  <?xml-stylesheet type=\"text/xsl\" href=\"/atom2html.xslt\"?> <feed xmlns=\"http://www.w3.org/2005/Atom\" xmlns:media=\"http://search.yahoo.com/mrss/\">";
?>

	<title><?= str_replace("&", "&amp;", str_replace("&amp;","&",$business_name)) ?> News Feed</title>
	<subtitle>News and information about <?=str_replace("&", "&amp;", str_replace("&amp;","&",$business_name)) ?>.</subtitle>
	<link href="http://<?= $url ?>/feed.xml" rel="self" />
	<link href="http://<?= $url ?>/" />
	<id><?= $sitewideID ?></id>
	
	<updated><?= date(DATE_ATOM, $recentPostObject->modified )?></updated>
	
<? while($postObject = $postInfo->fetch_object()) {

	$opengraphImage = img_urls($postObject->content);
?>
	<entry>
		<author>
			<name><?= ucwords($postObject->username) ?></name>
		</author>
				
		<title type="html"><![CDATA[<?= htmlentities($postObject->title, null, 'utf-8') ?>]]></title>
		<? if(function_exists("custom_blog_url")) { ?>
			<link rel="alternate" type="text/html" href="http://<?= $url . htmlentities(custom_blog_url($postObject->id, $postObject->title), null, 'utf-8') ?>" />		
		<? } else { ?>
			<link rel="alternate" type="text/html" href="http://<?= $url . htmlentities(blog_url($postObject->id, $postObject->title), null, 'utf-8') ?>" />		
		<? } ?>

		<id>tag:<?= $url?>,2010:<?=$postObject->id?></id>
		
		<updated><?= date(DATE_ATOM, $postObject->modified) ?></updated>
		<published><?= date(DATE_ATOM, $postObject->created) ?></published>		
		
		<content type="html"><![CDATA[<?= $postObject->content ?>]]></content>

		<? if (isset($opengraphImage) && count($opengraphImage[1]) > 0) { ?>
		<media:thumbnail url="http://<?= $url ?><?= htmlentities(str_replace(" ","%20",$opengraphImage[1][0]), null, 'utf-8') ?>" />
		<? } ?>
	</entry>
	
<? } ?>
</feed>

<? end_caching($cachefile); ?>