<?php
header("Content-type: text/xml");
include_once ("/usr/local/plesk/apache/vhosts/northcountrypublicradio.org/httpdocs/newsdb/config.php");
include_once ("/usr/local/plesk/apache/vhosts/northcountrypublicradio.org/httpdocs/newsdb/functions.php");

$storiestoday = get_stories_by_date(array(date("Y-m-d",$corrtime),date("Y-m-d",$corrtime)));

function displaynewslinks($newsday,$displaydate) {
		include ("/usr/local/plesk/apache/vhosts/northcountrypublicradio.org/httpdocs/newsdb/config.php");
		$connect = dbconnect();

		$query = "SELECT * FROM news WHERE 1 and storydate LIKE '$newsday' ORDER BY ord_id ASC";
		if (DISP_BY_NUM > 0) $query = "SELECT * FROM news WHERE 1 and storydate <= '$newsday' ORDER BY storydate DESC, ord_id ASC LIMIT 0, ".(DISP_BY_NUM);
		$result = mysql_query ($query);

		if ($story = mysql_fetch_array($result)) {
				do {
					$storyid = $story[story_id];
					$title = $story[title];
					$storydate = $story[storydate];
					$storydate = strtotime($storydate);
					$storyyear = date( "y", $storydate );
					$storymonth = date( "n", $storydate );
					$storyday = date( "j", $storydate );
					$description = $story[description];
					$reporter_id = $story[reporter_id];
					$operator = $story[operator];
					$reportername = mysql_fetch_array(mysql_query("SELECT reporter FROM reporters WHERE reporter_id = '$reporter_id'"));

                                        echo "<item>\n";
                                        echo "<title>".htmlentities(strip_tags($title))."</title>\n";

                                        if (($reportername[reporter] != "NCPR Online") && ($reportername[reporter] != "Promo")) {
				                echo '<link>http://www.northcountrypublicradio.org/news.php?getnewsfordate=1&amp;mm='.date("m", $storydate).'&amp;dd='.date("d", $storydate).'&amp;yyyy='.date("Y", $storydate)."#$storyid</link>\n";
					}
                                        elseif ($reportername[reporter] == "NCPR Online") {
                                                echo '<link>http://www.northcountrypublicradio.org/news.php?getnewsfordate=1&amp;mm='.date("m", $storydate).'&amp;dd='.date("d", $storydate).'&amp;yyyy='.date("Y", $storydate)."#$storyid</link>\n";
                                        }
                                        elseif ($reportername[reporter] == "Promo") {
                                                echo '<link>http://www.northcountrypublicradio.org/news.php?getnewsfordate=1&amp;mm='.date("m", $storydate).'&amp;dd='.date("d", $storydate).'&amp;yyyy='.date("Y", $storydate)."#$storyid</link>\n";
                                        }

                                        echo "<description>".htmlentities(strip_tags($description))."</description>\n";
                                        echo "</item>\n";
				} 
				while ($story = mysql_fetch_array($result));
				echo "\n";
		}
		else {
			echo "<p>No items listed for $displaydate - please check back again later</p><br>\n";
		}
		mysql_close ($link);
}
function displaystorylinks($story_id,$datereq=null,$headlines=null) {
	include ("/usr/local/plesk/apache/vhosts/northcountrypublicradio.org/httpdocs/newsdb/config.php");
	$connect = dbconnect();
	$query = "SELECT * FROM news WHERE 1 and story_id = '$story_id'";
	$result = mysql_query ($query);
	if ($story = mysql_fetch_array($result)) {
			do {
				include "/usr/local/plesk/apache/vhosts/northcountrypublicradio.org/httpdocs/newsdb/dbfields.php";
				$reportername = mysql_fetch_array(mysql_query("SELECT reporter FROM reporters WHERE reporter_id = '$story[reporter_id]'"));
				echo "<item>\n";
				echo "<title>".htmlentities(strip_tags($title))."</title>\n";
				echo "<link>http://www.northcountrypublicradio.org/news.php#$storyid</link>\n";
				echo "<description>".htmlentities(strip_tags($description))."</description>\n";
				echo "</item>\n";
			} 
			while ($story = mysql_fetch_array($result));
	}
	else {
		echo "<p>No items listed for $displaydate - please check back again later</p><br>\n";
	}
	mysql_close ($link);
}
echo '<?';?>xml version="1.0" encoding="ISO-8859-1"?>
<rss version="0.91">

<channel>
<title>North Country Public Radio Newsroom</title> 
<link>http://www.northcountrypublicradio.org
<description>NCPR provides locally-produced news stories from around the Adirondack and North Country regions of New York State, as well as Western Vermont, and Ontario and Quebec in Canada.</description>
<language>en-us</language>
<copyright>Copyright 2006 North Country Public Radio.</copyright>
<managingEditor>radio@ncpr.org</managingEditor>
<webMaster>radio@ncpr.org</webMaster>


<image>
<title>North Country Public Radio Newsroom</title>
<url>http://www.northcountrypublicradio.org/images/ncprorgsm.gif</url>
<link>http://www.northcountrypublicradio.org
<width>51</width>
<height>12</height>
<description>NCPR provides locally-produced news stories from around the Adirondack and North Country regions of New York State, as well as Western Vermont, and Ontario and Quebec in Canada.</description> 
</image>

<?php
if ( (date("w",$corrtime)==6) || (date("w",$corrtime)==0) || ((date("w",$corrtime)==1) && ($storiestoday==false)) ) {
	$weekend = true;
	$today=strtotime(date("Y-m-d"));
	if (date("w", $today)==0) {
		$monday = date("Y-m-d", ($today-518400));
		$friday = date("Y-m-d", ($today-172800));
	}
	else {
		$monday = date("Y-m-d", ($today+((1-date("w",$today))*86400)));
		$friday = date("Y-m-d", ($today+((5-date("w",$today))*86400)));
	}
	$buckets = get_buckets();
	foreach ($buckets as $key=>$val) {
		unset($ids);
		$stories = get_weekend_headlines($buckets[$key][id]);
		if (is_array($stories)) {
			foreach ($stories as $k=>$v) {
				$ids[] = $stories[$k][id];
			}
			$i=0;
			foreach ($ids as $id) {
				$datereq = 1;
				displaystorylinks($id,$datereq,$i);
				if ($weekend==true) $i++;
			}
		}
	}
}
else {
if ( str_replace('-', '', $_GET['nd']) != '' ) $newstoday = $_GET['nd'];
$newsday = newsday($newstoday);
$displaydate = date("l, F d, Y", strtotime($newsday));
$anchordate = date("Ymd", strtotime($newsday));
echo displaynewslinks($newsday, $displaydate);
}?>

</channel>
</rss>