<?php
/*
* @package LabCMS
* @copyright (c) 2006 Alexander Levin aka Loki (loki_angel@mail.ru)
* @support http://labcms.ru
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/
	if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']))
	{
		
		if (strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])>=
				Db::i()->selectCell("SELECT MAX(UNIX_TIMESTAMP(news_date)) 
										FROM ?_news 
										WHERE news_date<=NOW() 
										AND lang_id=?
										ORDER BY news_date DESC LIMIT ?d", LabCMS::i()->lang['id'],intval(LabConfig::$mcfg['news_rss'])))
		header('HTTP/1.1 304 Not Modified');
		exit();
	}

	LabCMS::i()->setModuleTemplate('string:{$xml}');
	LabVars::$break=true;
	LabVars::$template='blank.tpl';
	
	sendXmlHeaders();
	if (LabCMS::i()->is_cached('string:{$xml}', 10*60, 'news'.LabCMS::i()->lang['id'])) return;

	$url=getServerName();
	$rss=array('title'=>LabConfig::get('site_name'),
				'link'=>$url,
				'description'=>LabConfig::$cfg['site_desc']);
	$result=Db::i()->select("SELECT UNIX_TIMESTAMP(news_date) as news_date, news_title, news_text, news_id 
										FROM ?_news 
										WHERE news_date<=NOW() 
										AND lang_id=?
										ORDER BY news_date DESC LIMIT ?d", LabCMS::i()->lang['id'],intval(LabConfig::$mcfg['news_rss']));
	foreach ($result as $item)
	{
		//если присутствует тег [more]
		if ($anonce=mb_stristr($item['news_text'], '[more]', true))
		{
			$item['news_text']=$anonce;
		}
		elseif (LabConfig::$mcfg['news_anonce_length'])
		{
			$text_wo_tags=strip_tags($new['news_text']);
			if (strlen($text_wo_tags)>LabConfig::$mcfg['news_anonce_length'])
			{
				preg_match('/^(.{1,'.LabConfig::$mcfg['news_anonce_length'].'})\s/s', $text_wo_tags, $anonce);
				$item['news_text']=$anonce[1];
			}
		}
		$rss['feed'][]=array('title'=>$item['news_title'],
							'description'=>$item['news_text'],
							'link'=>$url.LabCMS::i()->url($item['news_id'], 'item'),
							'pubDate'=>$item['news_date']);
	}
	
	$xml=genereteFeed($rss);
	Tpl::i()->assign('xml', $xml);