<?
	function on_utf8handler($buffer) { return iconv('windows-1251', 'UTF-8', $buffer);	}

	ob_start('ob_gzhandler');
	ob_start('on_utf8handler');

	require '../core.php';

	header ('Content-Type: application/rss+xml');
//	header ('Content-Type: text/xml');

	$noComments = array_key_exists('woc', $_GET);

	$res = mysql_query('select max(story_ts) from mmr_story');
	$lstModified = 0;
	if ($res !== FALSE) {
		$row = mysql_fetch_row($res);
		$lastModified = date ('r', max(strtotime($row[0]), getlastmod()));
    	header('Last-Modified: '.$lastModified);
    	mysql_free_result($res);
	}
	
	echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
?><rss version="2.0">
<channel>
	<title>Motivate me right!</title>
	<description>���� � ���������, ����� � ������������ "Motivate me right!" ("��������� ���� ���������!")</description>
	<link></link>
	<copyright>���� ������� and Outcorp 2007-2009</copyright>
	<managingEditor> (���� �������)</managingEditor>
	<pubDate></pubDate>
	<generator>moti.vate.me</generator>
	<image>
		<url>mmrlogosmall.png</url>
		<title>Motivate me right!</title>
		<link></link>
		<width>96</width>
		<height>99</height>
	</image>
<?
	$items = array();

	$today = getdate();

	if($noComments)
		$sql = 
			"select story_id, story_ts as t, author_name, story_text, author_email, 1, story_id  
			 from mmr_story
			 order by t desc";
	else
		$sql = 
			"select story_id, story_ts as t, author_name, story_text, author_email, 1, story_id  
			 from mmr_story
			 union 
			 select story_id, comment_ts as t, author_name, comment_text, author_email, 2, comment_id 
			 from mmr_story_comment
			 order by t desc";

	$res = mysql_query($sql)
		or die (mysql_error());

	$MAX_ROWS = 20;
	$rowCount = 0;

	while($row = mysql_fetch_row($res)) {
		$sortkey = strtotime($row[1]);

		$title = strip_tags($row[3]);
		if(($len = strlen($title)) > 80) {
			$title = wordwrap($title, 80, "\n");
			$titleParts = preg_split('/\\n/', $title);
			$title = array_shift($titleParts)."...";
		}
		
		$item = array(
			"sortkey" => $sortkey,
			"guid" => $row[5] == 1 ? $WWW_HOST.$WWW."feedback/".$row[0]."/" : $WWW_HOST.$WWW."feedback/".$row[0]."/#".$row[6],
			"link" => $row[5] == 1 ? $WWW_HOST.$WWW."feedback/".$row[0]."/" : $WWW_HOST.$WWW."feedback/".$row[0]."/#".$row[6],
			"title" => $title,
			"description" => str_replace("\n", "<br/>", $row[3]),
			"author" => ($row[2] == '' ? 
				"info.moti@vate.me (�����������)" : 
				"info.moti@vate.me ".'('.$row[2].')'),
			"domain" => $WWW_HOST.$WWW."feedback/",
			"category" => "������, �������, �������",
			"pubDate" => date("D, d M Y H:i:s \G\M\T", $sortkey),
			"comments" => $WWW_HOST.$WWW."feedback/".$row[0]."/",
		);
		$items[] = $item;

		if (++$rowCount >= $MAX_ROWS)
			break;
	}
	mysql_free_result($res);
 	
 	function itemcmp($a, $b) {
 		return $b["sortkey"]-$a["sortkey"];
 	}
 	usort($items, "itemcmp");
 	$maxCount = 1;
 	foreach($items as $item) {
 		if ($maxCount++ > 30)
 			break;
?><item>
	<title><![CDATA[]]></title>
	<description><![CDATA[<div class='mybookmarks' id='mybookmarkssite1203742075831'></div><style type='text/css'>@import url('http://bookmark.motivateme.ru/bookmarks.css');</style><script type='text/javascript' charset='windows-1251' src="http://bookmark.motivateme.ru/codes/27e4a7153ce232760a42514cc8226773.js"></script>
	]]></description>
	<link></link>
	<pubDate></pubDate>
	<guid></guid>
	<author></author>
<?
  	if (array_key_exists("comments", $item)) {
?>	<comments></comments>
<?
  	}
?>
</item>
<?

 	}
?></channel></rss>
<?
	ob_end_flush();
	ob_end_flush();
?>