<?
	/*
		news.php
		
		SQL-based news reading library

		Copyright (C) 2000,2001 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');

	function newsItem ($date, $user, $text)
	{
		echo '<TR>' .
			'	<TD>' .
			'		<DL>' .
			'			<DT><FONT SIZE="-1">' . "<EM>Posted on </EM>$date<EM> by </EM><STRONG>$user</STRONG></FONT></DT>" .
			"			<DD><P>$text</DD>" .
			'		</DL>' .
			'	</TD>' .
			'</TR>';
	}

	function newsEntry ($array)
	{
		need ("date");
		
		return '<TR>' .
			'	<TD>' .
			'		<DL>' .
			'			<DT><FONT SIZE="-1"><EM>Posted on </EM>' . dateFromSQLDateTime ($array[n_date]) . '<EM> by </EM><STRONG>' . $array[n_user] . '</STRONG></FONT></DT>' .
			'			<DD><P>' . StripSlashes ($array[n_news]) . '</DD>' .
			'		</DL>' .
			'	</TD>' .
			'</TR>';
	}

	function monthForm ($month, $year)
	{
		?><FORM name="bymonth" method="get" action="/old_news.php"><?
		tableBoxHeader ('black', tableHeadColor);
		tableTitle ("Search by Month", 1, tableHeadColor);
		?><TD align="center"><?
		tableHeader("100%", featureBgColor);
		?>
		<TR width="100%" vAlign="middle">
			<TD class="inside" align="center">
				<STRONG>Month:</STRONG>
			</TD>
			<TD class="inside" align="center">
				<SELECT name="month">
				<?
					for ( $i = 1 ; $i < 13 ; $i++ ) {
						printf ("<OPTION%s>%02d</OPTION>", $i == $month ? " selected" : "", $i);
					}
				?>
				</SELECT>
			</TD>
			<TD class="inside" align="center">
				<STRONG>Year:</STRONG>
			</TD>
			<TD class="inside" align="center">
				<SELECT name="year">
				<?
					if ($year == "")
						$year = date ('Y');
					for ($i = 2000; $i <= date ('Y'); $i++ ) {
						printf ("<OPTION%s>%04d</OPTION>", $i == $year ? " selected" : "", $i);
					}
				?>
				</SELECT>
			</TD>
		</TR>
		<TR width="100%" vAlign="middle">
			<TD class="inside" align="center" colSpan="4">
				<INPUT type="submit" value="Search">
			</TD>
		</TR>
		<?
		tableFooter ();
		tableBoxFooter ();
		?>
		</FORM>
		<?
	}

	function keywordForm ($string)
	{
		?><FORM name="bystring" method="get" action="/old_news.php"><?
		tableBoxHeader ('black', tableHeadColor);
		tableTitle ("Search by String", 1, tableHeadColor);
		?><TD align="center"><?		
		tableHeader("100%", featureBgColor);
		?>
		<TR vAlign="middle">
			<TD class="inside" align="center">
				<STRONG>String:</STRONG>
			</TD>
			<TD class="inside" align="center">
			<?
				printf ("<INPUT name=\"string\" type=\"text\" size=\"25\" value=\"%s\">", $string);
			?>
			</TD>
		</TR>
		<TR vAlign="middle">
			<TD class="inside" align="center" colSpan="2">
				<INPUT TYPE="submit" VALUE="Search">
			</TD>
		</TR>
		<?
		tableFooter ();
		tableBoxFooter ();
		?>
		</FORM>
		<?
	}

	function fetchNewsEntry ($number)
	{
		need('date');

			$conn = @mysql_pconnect (sqlHost, sqlUser, sqlPass);
			if ($conn) {
				$query = 'SELECT n_date, n_user, n_news FROM news_main' .
						" ORDER BY n_date DESC LIMIT $number";
				$result = @mysql_db_query (sqlDB, $query, $conn);
				if ($result) {
					$numRows = @mysql_num_rows ($result);
					if ($numRows) {
						for ($i = 0 ; $i < $numRows ; $i++) {
							list ($n_date, $n_user, $n_news) = mysql_fetch_row ($result);
						}
					} else {
						return 0;
					}
				} else {
					return 0;
				}
				mysql_close ($conn);
			} else {
				return 0;
			}
	//		return (list (dateFromSQLDateTime ($n_date), $n_user, StripSlashes($n_news)));
		return 0;
	}

	function latestNews ($max)
	{
		if (sqlAvail) {
			$conn = mysql_pconnect (sqlHost, sqlUser, sqlPass);
			if ($conn) {
				$query = 'SELECT n_date, n_user, n_news FROM news_main' .
						' ORDER BY n_date DESC';
				$result = mysql_db_query (sqlDB, $query, $conn);
				if ($result) {
					$numRows = mysql_num_rows ($result);
					if ($numRows) {
						for ($i = 0 ; $i < $numRows  && $i < $max; $i++) {
							$news[$i] = mysql_fetch_array ($result);
							echo newsEntry ($news[$i]);
						}
					} else {
						newsItem (date ('d M Y'), 'Web Server', '<P>No current news!');
					}
				} else {
					newsItem (date ('d M Y'), 'Web Server', '<P>No news in database!');
				}
				mysql_close ($conn);
			} else {
				include 'newscache.php';
				return 1;
			}
			return 0;
		} else {
			newsItem (date ('d M Y'), 'Web Server', '<P>No SQL server available!');
			return 0;
		}
	}
	
	function monthlyNews ($month, $year)
	{
		if (($year < 1999) || ($year > date ('Y'))) {	// Sanity checking
			$year = date ('Y');
		}
		
		if (($month < 1) || ($month > 12)) {
			$month = date ('m');
		}

		$date = sprintf ("%04d-%02d", $year, $month);

		if (sqlAvail) {
			$conn = mysql_pconnect (sqlHost, sqlUser, sqlPass);
			if ($conn) {
				$query = 'SELECT n_date, n_user, n_news FROM news_main' .
						" WHERE n_date BETWEEN '$date-00 00:00:00'" .
						" AND '$date-31 23:59:59'" .
						' ORDER BY n_date DESC';
				$result = @mysql_db_query (sqlDB, $query, $conn);
				if ($result) {
					$numRows = @mysql_num_rows ($result);
					if ($numRows) {
						for ($i = 0 ; $i < $numRows ; $i++) {
							$news[$i] = mysql_fetch_array ($result);
							echo newsEntry ($news[$i]);
						}
					} else {
						newsItem (date ('d M Y'), 'Web Server', '<P>No news for this month!');
					}
				} else {
					newsItem (date ('d M Y'), 'Web Server', '<P>No news in database!');
				}
				mysql_close ($conn);
			} else {
				newsItem (date ('d M Y'), 'Web Server', '<STRONG>SQL error!</STRONG> Please contact the <A href="mailto:deek@quakeforge.net">Webmaster</A>.');
			}
		} else {
			newsItem (date ('d M Y'), 'Web Server', '<P>No SQL server available');
		}
	}

	function searchNews ($string)
	{
		if (sqlAvail) {
			$search = AddSlashes ("%$string%");
			$conn = mysql_pconnect (sqlHost, sqlUser, sqlPass);
			if ($conn) {
				$query = 'SELECT n_date, n_user, n_news FROM news_main' .
						" WHERE n_news LIKE '$search'" .
						' ORDER BY n_date DESC';
				$result = mysql_db_query (sqlDB, $query, $conn);
				if ($result) {
					$numRows = mysql_num_rows ($result);
					if ($numRows) {
						for ($i = 0 ; $i < $numRows ; $i++) {
							$news[$i] = mysql_fetch_array ($result);
							echo newsEntry ($news[$i]);
						}
					} else {
						newsItem (date ('d M Y'), 'Web Server', "No news found matching '$string'");
					}
				} else {
					newsItem (date ('d M Y'), 'Web Server', "No news found matching '$string'");
				}
				mysql_close ($conn);
			} else { 
				newsItem (date ('d M Y'), 'Web Server', '<STRONG>SQL error!</STRONG> Please contact the <A href="mailto:deek@quakeforge.net">Webmaster</A>.');
			}
		} else {
			newsItem (date ('d M Y'), 'Web Server', '<P>No SQL server available');
		}
	}

?>
