<?php

/**
*	@version	0.1.2
*	@date		2009-04-07
*	@author		Ruud (Ruud Eisinga)
*	@package	Websitebaker - Last modiefied RSS feed
*	@state		@dev
*
*   Put this page in the root of your website. Use your favorite RSS reader to check on the last updates of your customers websites.
*
*   History:
*
*	v 0.1.0: Initial release
*   v 0.1.1: Filled pubDate field. Limit to 15 pages reported.
*   v 0.1.2: Filled Author field
*   v 0.1.3: Modified by GMastaP
*/	

require_once(dirname(__FILE__).'/config.php');
if(!defined('WB_PATH')) {die();}

$limit = 15;  //Change this if you need more or less items returned

$output_array = array();
$result = $database->query("SELECT * FROM " . TABLE_PREFIX . "pages WHERE visibility = 'public' ORDER BY modified_when DESC LIMIT 0 , $limit");

if ($result && $result->numRows() > 0) {
	while($page = $result->fetchRow()){
		$ptitle = $page['page_title'];
		$pdesc  = $page['description'];
		$lastchange = date("d-m-y, H:i", $page['modified_when']);
		$pubDate = date('r', $page['modified_when']);
		$url = WB_URL.PAGES_DIRECTORY.$page['link'].PAGE_EXTENSION;
		$lin = '<item>'; 
		$lin .=  "\n\t\t".'<title><![CDATA['.$lastchange.' / '.$ptitle.']]></title>'; 
		$lin .=  "\n\t\t".'<link>'.$url.'</link>'; 
		$lin .=  "\n\t\t".'<description><![CDATA['.$pdesc.']]></description>'; 
		$lin .=  "\n\t\t".'<category>Sitepages</category>'; 
		$lin .=  "\n\t\t".'<author>'.GetUserName($page['modified_by']).'</author>'; 
		$lin .=  "\n\t\t".'<pubDate>'.$pubDate.'</pubDate>'; 
		$lin .=  "\n\t".'</item>';
		$output_array[] = $lin;
	} 
}

header("Content-type: text/xml");
echo '<?xml version="1.0" encoding="ISO-UTF-8"?>';
echo "\n".'<rss version="2.0">';
echo "\n".'<channel>'; 
echo "\n\t".'<title><![CDATA['.WEBSITE_TITLE.']]></title>';    
echo "\n\t".'<link>'.WB_URL.'</link>';   
echo "\n\t".'<description>Last Updated</description>'; 
foreach ($output_array as $o){
  echo $o;
}
echo "\n".'</channel>';
echo "\n".'</rss>';	

Function GetUserName ($userId) {
	global $database;
	$result = $database->query("SELECT display_name	FROM " . TABLE_PREFIX . "users WHERE `user_id` = $userId");
	if ($result && $result->numRows() == 1) {
		$udata = $result->fetchRow();
		return $udata['display_name'];
	} else {
		return "unknown";
	}
}
?>

