<?php if(!defined('master_script')) die();

/**
 * Copyright (C) Nico Wenig
 * All rights reserved
 *
 * @author Nico Wenig <nico.wenig@yellowgreen.de>
 * @filesource
 */



/**
 * Feed file header
 *
 * @category   Extension (feed)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-05-24
 */

	define('feed_script', true);
	global $user_data, $system_data;
	
	header('content-type: text/xml; charset=utf-8');

/**
 * Get basis frontend language
 *
 * @category   Localization
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-05-24
 */

	$frontend_languages = explode(',', $user_data['frontend languages']);
	if(!is_array($frontend_languages)) $frontend_languages = array($frontend_languages);
	
	$_SESSION['language'] = $frontend_languages[0];

/**
 * Create XML for feed
 *
 * @category   Extension (feed)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-11-19
 */
	
	// Basic XML feed structure
	$feed = simplexml_load_string('<?xml version="1.0" encoding="utf-8"?><rss version="2.0"></rss>');
	
	$feed->channel->title = xml_string($user_data['rss title']);
	$feed->channel->description = xml_string($user_data['rss description']);
	$feed->channel->link = xml_string($user_data['base url']);
	$feed->channel->language = xml_string(str_replace('_', '-', $frontend_languages[0]));
	$feed->channel->pubDate = date('r');
	$feed->channel->generator = xml_string('Pagemap ' . $system_data['system']['version']);
	
	// Get latest pages
	$database_latest_file = $system_data['folder']['database'] . 'latest/catalogue.xml';
	$database_latest_xml = is_file($database_latest_file) ? simplexml_load_file($database_latest_file) : simplexml_load_string('<?xml version="1.0" encoding="utf-8"?><pages></pages>');
	
	$j = 0;
	
	for($i = count($database_latest_xml->page) - 1; $i >= 0; $i--) {
		$page_entry = get_page_info($database_latest_xml->page[$i]->alias);
		
		if(($user_data["rss only news"] == 0 || $page_entry->pagetype == 'news') && $page_entry->catalogue != '0000000000' && $page_entry->alias != '') {
			$description = generate_content($system_data['folder']['pages'] . $page_entry->alias . '/' . $system_data['file']['index page'], $page_entry->alias);
			$description = strip_custom_tags($description) == '' ? strval($page_entry->teaser) : strip_custom_tags($description, true, true, true, false, true);
			if($description == '') $description = strval($page_entry->description);
			
			$feed->channel->item[$j]->title = xml_string($page_entry->title);
			if(strip_tags($description) != '') $feed->channel->item[$j]->description = xml_string($description);
			$feed->channel->item[$j]->link = xml_string($user_data['base url'] . $system_data['frontend']['href attribute'] . $page_entry->href);
			$feed->channel->item[$j]->pubDate = date('r', intval($page_entry->timestamp));
			$feed->channel->item[$j]->guid = xml_string($user_data['base url'] . $system_data['frontend']['href attribute'] . $page_entry->href);
			
			$j++;
			if($j == $user_data["rss items"]) break;
		}
	}
	
	echo $feed->asXML();

?>