<?php 
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Content-type: application/rss+xml");

require_once ('incs/db.inc.php');

function prepareString($str) {
    return mb_convert_encoding(str_replace(array('&gt;','&lt;', '&quot;'), array('>', '<', '"') , htmlspecialchars(html_entity_decode(str_replace(array('src="/',"src='/"), array('src="http://song.lutsk.ua/', "src='http://song.lutsk.ua/"), str_replace('&mdash;', '-', $str))))), 'UTF-8', 'CP-1251');
}


print '<?xml version="1.0"?>'; ?>

<rss version="2.0">
   <channel>
      <title><?php print prepareString("������� ������������ ������");?></title>
      <link>http://song.lutsk.ua/</link>
      <description><?php print prepareString("������ ������� ���� ������������ ������. ��������������� �������� ������������ �����.");?></description>
      <image>
         <url>http://song.lutsk.ua/images/song88.gif</url>
         <title><?php print prepareString("������� ������������ ������");?></title>
         <link>http://song.lutsk.ua</link>
      </image>
      <language>ru-ru</language>
      <copyright><?php print prepareString("Copyright 2006-2009, ������ ������������ �����");?></copyright>
      <managingEditor>roskoalexey@gmail.com</managingEditor>
      <webMaster>roskoalexey@gmail.com</webMaster>
      <docs>http://blogs.law.harvard.edu/tech/rss</docs>
      <generator>Song.lutsk.ua</generator>
<?php

//      <image>http://song.lutsk.ua/favicon.ico</image>

 $res = mysql_query("
            (select n.*, 'news' as 'i_type' from news n where n.verify=1 and (n.news_enddate='0000-00-00' or n.news_enddate >= CURDATE() ) and (n.news_date <= NOW() ) order by n.news_date desc,n.news_id desc limit 10)
            union
            (select a.*, 'article' as 'i_type' from articles a where a.verify=1 and (a.article_enddate='0000-00-00' or a.article_enddate >= CURDATE() ) and (a.article_date <= NOW() ) order by a.article_date desc,a.article_id desc limit 10)
            order by 2 desc
 ");
 $i = 0;
 while ($row = mysql_fetch_array($res)) {
    $i++;
    if ($i == 1) {
        print "      <pubDate>" . date("r", strtotime($row['news_date'])) . "</pubDate>\n";
        print "      <lastBuildDate>" . date("r") . "</lastBuildDate>\n";
    }
    print "      <item>\n";
    print "         <title>" . prepareString($row['news_title']) . "</title>\n";
    if ($row['i_type'] == 'news') {
        print "         <link>http://song.lutsk.ua/news.html?news_id=$row[news_id]</link>\n";
    } elseif ($row['i_type'] == 'article') {
        print "         <link>http://song.lutsk.ua/articles.html?article_id=$row[news_id]</link>\n";
    }
    print "         <description>" . prepareString($row['news_text']) . "</description>\n";
    if ($row['news_author'] != '') {
//        print "         <author>" . prepareString($row['news_author']) . "</author>\n";
    }
    if ($row['news_source'] != '') {
//        print "         <enclosure>" . prepareString($row['news_source']) . "</enclosure>\n";
    }
    if ($row['i_type'] == 'news') {
        print "         <comments>http://song.lutsk.ua/news.html?news_id=$row[news_id]#comments</comments>\n";
    } elseif ($row['i_type'] == 'article') {
        print "         <comments>http://song.lutsk.ua/articles.html?article_id=$row[news_id]#comments</comments>\n";
    }
    print "         <pubDate>" . date("r", strtotime($row['news_date'])) . "</pubDate>\n";
    if ($row['i_type'] == 'news') {
        print "         <guid isPermaLink=\"true\">http://song.lutsk.ua/news.html?news_id=$row[news_id]</guid>\n";
    } elseif ($row['i_type'] == 'article') {
        print "         <guid isPermaLink=\"true\">http://song.lutsk.ua/articles.html?article_id=$row[news_id]</guid>\n";
    }
    print "      </item>\n";
 }
 
 mysql_close($_link);
?>
   </channel>
</rss>