<?php

header("Content-Type: application/rss+xml; charset=ISO-8859-1");

$cat = $_GET['cat'];

include ('config.php');
	
	$sitequery = 'select * from settings;';
	$siteresult = mysql_query($sitequery,$connection) or die(mysql_error());
	
	//Create site settings variables		
	$siteinfo = mysql_fetch_array($siteresult);
	$sitetitle = $siteinfo['title'];
	$siteurl = $siteinfo['url'];
	$logo = $siteinfo['logourl'];
	$statcode = $siteinfo['statcode'];

	if($cat) {
	$query = "select * from categories where id = ".$cat;
	$results = mysql_query($query,$connection) or die(mysql_error());
    $info = mysql_fetch_assoc($results);
    $cattitle = htmlspecialchars($info['name']);
    
    $rssfeed = '<?xml version="1.0" encoding="ISO-8859-1"?>';
    $rssfeed .= '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">';
    $rssfeed .= '<channel>';
    $rssfeed .= '<title>'.$cattitle.' - '.$sitetitle.'</title>';
    $rssfeed .= '<link>'.$siteurl.'</link>';
    $rssfeed .= '<description>'.$sitetitle.' RSS FEED</description>';
    $rssfeed .= '<language>en-us</language>';
    $rssfeed .= '<copyright></copyright>';

    } else {

    $rssfeed = '<?xml version="1.0" encoding="ISO-8859-1"?>';
    $rssfeed .= '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">';
    $rssfeed .= '<channel>';
    $rssfeed .= '<title>'.$sitetitle.'</title>';
    $rssfeed .= '<link>'.$siteurl.'</link>';
    $rssfeed .= '<description>'.$sitetitle.' RSS FEED</description>';
    $rssfeed .= '<language>en-us</language>';
    $rssfeed .= '<copyright></copyright>';
 	
 	}
	
	if (!$cat){
		$query = "select * from articles where status=0	order by date desc LIMIT 0, 20";
	} else {
		$query = "select * from articles where status=0 and categoryid = ".$cat." OR parentid =  ".$cat." order by date desc LIMIT 0, 20";
	}
	$results = mysql_query($query,$connection) or die(mysql_error());
	$num_results = mysql_num_rows($results);
 
    for ($i=0; $i <$num_results; $i++) { 
       $row = mysql_fetch_assoc($results); 
 	   $date = strtotime($row['date']);
	   $artdate = date('D, d M Y H:i:s O', $date);

        $rssfeed .= '<item>';
        $rssfeed .= '<title>' . $row['title'] . '</title>';
        $rssfeed .= '<description>' . htmlspecialchars($row['body']) . '</description>';
        $rssfeed .= '<link>'. $siteurl.'/article.php?id='.$row['id'] . '</link>';
        $rssfeed .= '<guid>'. $siteurl.'/article.php?id='.$row['id'] . '</guid>';
        $rssfeed .= '<pubDate>' . $artdate . '</pubDate>';
        $rssfeed .= '</item>';
    }
 
    $rssfeed .= '</channel>';
    $rssfeed .= '</rss>';
 
    echo $rssfeed;
?>
