<?
/*
	news.php

	SQL-based news reading

	Copyright (C) 2000-2007 Jeff Teunissen <deek@quakeforge.net>
	Copyright (C) 2000 Daniel David Olson <theoddone33@users.sourceforge.net>

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  

	See the GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to:

	Free Software Foundation, Inc.
	59 Temple Place - Suite 330
	Boston, MA  02111-1307, USA.
*/

have ('news');
									// SQL definition for news table
$createQuery="CREATE TABLE news_main ("
			."	n_id	INT NOT NULL PRIMARY KEY AUTO_INCREMENT,"
			."	n_user	TINYTEXT NOT NULL,"
			."	n_date	TIMESTAMP NOT NULL DEFAULT NOW(),"
			."	n_news	TEXT NOT NULL"
			.")";

function formatNews ($date, $user, $text)
{
	return "<DL>\n"
		.'	<DT>'
		. 		"<EM>Posted on </EM>$date<EM> by </EM><STRONG>$user</STRONG>"
		.	"</DT>\n"
		."	<DD>$text</DD>\n"
		."</DL>\n";
}


function formatNewsArray ($a)
{
	need ('date');

	return formatNews (dateFromSQLDateTime ($a['n_date']), $a['n_user'], stripSlashes($a['n_news']));
}


function printNews ($date, $user, $text)
{
	echo formatNews ($date, $user, $text);
}


function printNewsArray ($a)
{
	echo formatNewsArray ($a);
}


function latestNews ($max=5)
{
	need ('sql');

	$query = 'SELECT n_date, n_user, n_news FROM news_main'
			." ORDER BY n_date DESC LIMIT $max";

	return sqlReadQuery ($query);
}


function printLatestNews ($max=5)
{
	$filler = date ('d M Y');
	$entries = latestNews ($max);

	if ($entries === true) {	// success, but no data
		printNews ($filler, 'Web Server', 'Nobody has posted news yet, so I got nothing to say right now.');
	} elseif ($entries === false) {	// bad query
		printNews ($filler, 'Web Server', 'Got an error while finding news. Bug a project administrator, eh?');
	} elseif ($entries === null) {	// boom
		printNews ($filler, 'Web Server', 'Couldn\'t connect to the project news server...Sorry.');
	} else for ($i = 0; $i < count ($entries); $i++) {
		printNewsArray ($entries[$i]);
	}
}


function monthlyNews ($month, $year)
{
	need ('sql');

	if (($year < 1999) || ($year > date ('Y'))) {	// Sanity checking
		$year = date ('Y');
	}

	if (($month < 1) || ($month > 12)) {
		$month = date ('m');
	}

	$filler = date ('d M Y');
	$date1 = sprintf ("%04d-%02d", $year, $month);
	$date2 = sprintf ("%04d-%02d", $year, $month+1);
	$query = 'SELECT n_date, n_user, n_news FROM news_main'
			." WHERE n_date BETWEEN '$date1-01 00:00:00'"
			." AND '$date2-01 00:00:00'"
			.' ORDER BY n_date DESC';

	$entries = sqlReadQuery ($query);
	if ($entries === true) {	// success, but no data
		printNews ($filler, 'Web Server', 'Nobody posted news in the month you asked for.');
	} elseif ($entries === false) {	// bad query
		printNews ($filler, 'Web Server', 'Got an error while finding news. Bug a project administrator, eh?');
	} elseif ($entries === null) {	// boom
		printNews ($filler, 'Web Server', 'Couldn\'t connect to the project news server...Sorry.');
	} else for ($i = 0; $i < count ($entries); $i++) {
		printNewsArray ($entries[$i]);
	}
}


function searchNews ($string)
{
	need ('sql');

	$filler = date ('d M Y');
	$search = addSlashes ("%$string%");
	$query = 'SELECT n_date, n_user, n_news FROM news_main'
			." WHERE n_news LIKE '$search'"
			.' ORDER BY n_date DESC';

	$entries = sqlReadQuery ($query);
	if ($entries === true) {	// success, but no data
		printNews ($filler, 'Web Server', "No news found matching '$string'");
	} elseif ($entries === false) {	// bad query
		printNews ($filler, 'Web Server', 'Got an error while finding news. Bug a project administrator, eh?');
	} elseif ($entries === null) {	// boom
		printNews ($filler, 'Web Server', 'Couldn\'t connect to the project news server...Sorry.');
	} else for ($i = 0; $i < count ($entries); $i++) {
		printNewsArray ($entries[$i]);
	}
}
?>