<?php
ini_set('display_errors', '0');
foreach ($_REQUEST as $k => $v)
$$k = $v;
$l=(isset($_GET['l'])?$_GET['l']:'bg');
include 'functions/db_mysql.php';
require('rss_class/rss_fetch.inc');
include 'functions/feed_functions.php';
include 'languages/lang_'.$l.'.php';
$db = new DB_Sql;
$db1 = new DB_Sql;
$sql = 'SELECT encoding, `encoding` FROM feed_languages WHERE short_name = "'.$l.'"';
$db->query($sql);
$db->next_record();
$target_encoding = $db->f('encoding');
header("Content-Type: application/rss+xml; charset=$target_encoding");
echo '<'.'?xml version="1.0" encoding="'.$target_encoding.'"?'.'>
';
?><rss version="2.0">
   <channel>
      <title>enews.bg</title>
      <link>http://www.enews.bg/?l=<?=$l ?></link>
      <description>the news place</description>
      <language><?=$l ?></language>
      <lastBuildDate>Fri, 02 Mar 2007 20:52:10 +0200</lastBuildDate>
      <generator>enews.bg RSS Generator</generator>
	  <copyright>2007 enews.bg</copyright>
	  <image>
		  <title>enews.bg</title>
		  <url>http://www.enews.bg/images/logo4-1.gif</url>
		  <link>http://www.enews.bg/?l=<?=$l ?></link>
	   </image><?php
if($c == 'c'){
	//news by category
	$sql = 'SELECT id FROM feed_categories_'.$l.' WHERE parent = '.$d.' AND active = 1';
	$db->query($sql);
	while($db->next_record()){
		$subs .= ' OR f.category = '.$db->f('id');
	}
	$sql = 'SELECT i.guid AS guid, i.feed_id AS feed_id, c.name AS category
				FROM feed_items_'.$l.' i 
				LEFT JOIN feed_feeds_'.$l.' f ON i.feed_id = f.id
				LEFT JOIN feed_categories_'.$l.' c ON f.category = c.id ';
	$sql .= ($o=='c'?' LEFT JOIN feed_clicks_'.$l.' l ON l.guid = i.guid AND l.feed_id = f.id ':'');			
	$sql .= '		 WHERE ( f.category = '.$d.' '.$subs.' ) '.($o=='c'?' AND ((UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP(l.published))< '.(isset($r)? $r:604800 ).') ':'').' GROUP BY guid ORDER BY '.($o=='c'?' l.clicks ':'i.published').'  DESC LIMIT 20';
}elseif($c=='d'){
	//news by date
	$sql = 'SELECT f.guid AS guid, f.feed_id AS feed_id, DATE_FORMAT(f.published, "%d-%m-%Y") AS display_date FROM feed_items_'.$l.' f
			WHERE 
			'.($o=='c'?' LEFT JOIN feed_clicks_'.$l.' c ON f.feed_id = c.feed_id AND f.guid = c.guid ':'').'
			'.(isset($t)?' LEFT JOIN feed_feeds_'.$l.' t c ON t.id = f.feed_id':'').'
				DATE_FORMAT(f.published, "%Y-%m-%d") = "'.$r.'"';
	$sql .= (isset($t)? ' AND t.category = '.$t.' ' :'');
	$sql .= ' GROUP BY guid ';
	$sql .= ($o=='c'?' ORDER BY c.clicks DESC ':' ORDER BY f.published DESC ');
	$sql .= 'LIMIT 20';
}elseif($c=='s'){
	//news by search string
	$search = rawurldecode($s);
	$sql = 'SELECT i.guid AS guid, i.feed_id AS feed_id, c.name AS category, f.name AS name,
					MATCH (i.content) AGAINST ("'.$search.'") AS score, l.clicks AS clicks
				FROM feed_items_'.$l.' i 
				LEFT JOIN feed_feeds_'.$l.' f ON i.feed_id = f.id
				LEFT JOIN feed_categories_'.$l.' c ON f.category = c.id
				LEFT JOIN feed_clicks_'.$l.' l ON l.guid = i.guid AND l.feed_id = f.id
			 WHERE MATCH (i.content) AGAINST ("'.$search.'") GROUP BY guid ORDER BY '.($o!='r'?(o=='a'?'clicks':'i.published'):'score').' DESC LIMIT 20';
}elseif($c=='r'){
	//news by source
	$sql = 'SELECT i.guid AS guid, i.feed_id AS feed_id, c.name AS category, f.name AS name
				FROM feed_items_'.$l.' i 
				LEFT JOIN feed_feeds_'.$l.' f ON i.feed_id = f.id
				LEFT JOIN feed_categories_'.$l.' c ON f.category = c.id
			 WHERE f.id = '.$d.' GROUP BY guid ORDER BY published DESC LIMIT 20 ';
}elseif($c=='p'){
	$r = (isset($r)?$r:604800);
	$sql = 'SELECT guid, feed_id FROM feed_clicks_'.$l.' 
			WHERE 
				((UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP(published))< '.$r.') ';
	$sql .= ' ORDER BY clicks DESC LIMIT 20';
}else{
	$sql = 'SELECT guid, feed_id FROM feed_items_'.$l.' WHERE 1 GROUP BY guid ORDER BY published DESC LIMIT 20';
}
$db->query($sql);
while($db->next_record()){
	$n = get_news($db->f('guid'), $db->f('feed_id')  );
?>
	  	<item>
			<title><?=$n['title'] ?></title>
			<link>http://www.enews.bg/index.php?c=w&amp;l=<?=$l ?>&amp;d=<?=$n['feedbg_id'] ?></link>
			<description><?=$n['description'] ?></description>
			<?php
	 if(isset($n['image/jpeg'])){
	 	echo '<enclosure url="'.$n['image/jpeg'].'" type="image/jpeg" />
			';
	 }
	 ?><pubDate><?=$n['pubdate'] ?></pubDate>
			<category><?=htmlspecialchars($n['feedbg_category']) ?></category>
			<guid>http://www.enews.bg/index.php?c=w&amp;l=<?=$l ?>&amp;d=<?=$n['feedbg_id'] ?></guid>
		</item>
<?php	


}	  	   
	   
	   ?>		</channel>
	</rss>