#!/usr/bin/perl
#
# Catch22 (llama@osoal.org.nz)
# One Short Of A Llama [http://web.osoal.org.nz]

# create table news
# (
#    pid      int unsigned not null auto_increment primary key,
#    nickname varchar(40),
#    uid      int unsigned,
#    address  varchar(50),
#    browser  varchar(50),
#    time     int unsigned,
#    subject  varchar(80),
#    email    varchar(50),
#    news     longtext
# );

#
use strict;
use DBI;
use OneShortOfALlama;

#
my($load);

#
$load = 10;

#
sub print_news
{
	my($dbh, $gid, $sth, $pid, $nickname, $address,
	   $browser, $time, $subject, $email, $news, $param);

	#
	$dbh   = shift(@_);
	$gid   = shift(@_);
	$param = shift(@_);

	#
	$sth = $dbh->prepare('select pid, nickname, address, browser,
                          from_unixtime(time, "%W %D of %M %l:%i %p, %Y"), email, subject, news
                          from osoal.news
                          order by pid desc' . ($param->{'load'} > -1 ? ' limit ' . $param->{'load'} : ''));
	$sth->execute();

	unless ($sth->rows())
	{
		# avoid errors about binding to non-existant rows
		return;
	}

	$sth->bind_columns(undef(), \$pid, \$nickname, \$address,
					   \$browser, \$time, \$email, \$subject, \$news);

	while ($sth->fetch())
	{
		print('<a name="', $pid, '">
               <b>', $nickname, '</b> ', $address, ' <small>', $browser, '</small><br>
               <em>', $subject, '</em> on ', $time);

		if ($email)
		{
			print(' (<a href="mailto:', $email, '">', $email, '</a>)');
		}

		print('<br>
               <br>', $news, '<br>
               <br>');

		#
		if ($gid == 1)
		{
			print('[ <a href="', $ENV{'SCRIPT_NAME'}, '?select=Modify&pid=', $pid, '">
                   <small><b>Modify</b></small></a>
                   | <a href="', $ENV{'SCRIPT_NAME'}, '?select=Delete&pid=', $pid, '">
                   <small><b>Delete</b></small></a> ]
                   <br><br>');
		}

		print('<hr noshade><br>', "\n\n");
	}	
}

sub submit_news
{
	my($dbh, $sth, $uid, $param, $address, $browser, $i);

	#
	$dbh   = shift(@_);
	$uid   = shift(@_);
	$param = shift(@_);

	#
	$i = $dbh->selectrow_array('select uid from osoal.users
                                where (nickname = '. $dbh->quote($param->{'nickname'}) . ')');

	unless ($uid == $i)
	{
		print('You may not post comments as this user');
		return ();
	}

	unless ($param->{'nickname'} && $param->{'subject'} && $param->{'news'})
	{
		print('Insufficent arguments to build news article');
		return ();
	}

	#
	$param->{'news'} =~ s/\r//g;
	$address = nslookup($ENV{'REMOTE_ADDR'});
	$browser = quote($ENV{'HTTP_USER_AGENT'});

	#
	$sth = $dbh->prepare('insert into osoal.news (nickname, uid, address, browser, time, subject, email, news)
                          values (?, ?, ?, ?, unix_timestamp(), ?, ?, ?)');

	$sth->execute($param->{'nickname'}, $uid, $address, $browser,
				  $param->{'subject'}, $param->{'email'}, $param->{'news'});
	
}

sub input_news
{
	my($dbh, $uid, $select, $nickname, $email, $subject, $news, $i);

	#
	$dbh    = shift(@_);
	$uid    = shift(@_);
	$select = shift(@_);

	#
	print('<form method="POST" action="', $ENV{'SCRIPT_NAME'}, '">', "\n\n");

	#
	if ($select eq 'News' && $uid)
	{
		($nickname, $email)
			= $dbh->selectrow_array("select nickname, fakeemail 
                                                 from osoal.users where (uid = $uid)");
	}

	elsif ($select eq 'Modify')
	{
		$i = shift(@_);
		print('<input type="hidden" name="pid" value="', $i, '">', "\n\n");

		($nickname, $email, $subject, $news)
			= $dbh->selectrow_array("select nickname, email, subject, news
                                     from osoal.news where (pid = $i)");
	}

	#
	print('<table align="center">
           <tr>
               <td align="left">Nickname</td>
               <td align="right"><input type="text" name="nickname" value="', $nickname, '" size="40"></td>
           </tr>

           <tr>
               <td align="left">Password</td>
               <td align="right"><input type="password" name="passwd" value="" size="40"></td>
           </tr>

           <tr>
               <td align="left">Email</td>
               <td align="right"><input type="text" name="email" value="', $email, '" size="40"></td>
           </tr>

           <tr>
               <td align="left">Subject</td>
               <td align="right"><input type="text" name="subject" value="', $subject, '" size="40"></td>
           </tr>

           <tr>
               <td colspan="2">
               <textarea name="news" rows="15" cols="52" wrap="virtual">', $news, '</textarea>
               </td>
           </tr>
           <tr>
           <td colspan="2">
               <table width="100%" border="0" cellpadding="0" cellspacing="0">
               <tr>
                   <td align="left">
                   <input type="submit" name="select" value="');

	#
	if ($select eq 'News')
	{
		print('Submit');
	}

	elsif ($select eq 'Modify')
	{
		print('Apply');
	}
			  
	print('"> <input type="submit" name="select" value="Preview"></td>
              <td align="right"><img src="/pics/icons/oneshortofallama.png" alt=""></td>
              </tr>
              </table>
              </td>
           </tr>
           </table>
           </form>', "\n\n");

}

sub apply_news
{
	my($dbh, $sth, $param);

	#
	$dbh   = shift(@_);
	$param = shift(@_);

	#
	unless ($param->{'nickname'} && $param->{'subject'} && $param->{'news'})
	{
		print('Insufficent arguments to build news article');
		return ();
	}

	#
	$param->{'news'} =~ s/\r//g;

	#
	$sth = $dbh->prepare('update osoal.news
                          set nickname = ?, subject = ?, email = ?, news = ? where (pid = ?)');

	$sth->execute($param->{'nickname'}, $param->{'subject'},
				  $param->{'email'}, $param->{'news'}, $param->{'pid'});
}

sub delete_news
{
	my($dbh, $pid);

	#
	$dbh = shift(@_);
	$pid = shift(@_);

	#
	$dbh->do("delete from osoal.news where (pid = $pid)");
}

sub preview_news
{
	my($dbh, $param, $time, $address);

	#
	$dbh   = shift(@_);
	$param = shift(@_);

	#
	unless ($param->{'nickname'} && $param->{'subject'} && $param->{'news'})
	{
		print('Insufficent arguments to build news article');
		return ();
	}

	#
	$param->{'news'} =~ s/\r//g;
	$address = nslookup($ENV{'REMOTE_ADDR'});
	$time    = $dbh->selectrow_array('select from_unixtime(unix_timestamp(), "%W %D %M %l:%i %p, %Y")');

	#
	print('<b>', $param->{'nickname'}, '</b> ', $address, ' <small>',
		  quote($ENV{'HTTP_USER_AGENT'}), '</small><br><em>', $param->{'subject'},
		  '</em> on ', $time);

		if ($param->{'email'})
		{
			print(' (<a href="mailto:', $param->{'email'}, '">', $param->{'email'}, '</a>)');
		}

		print('<br>
               <br>', $param->{'news'}, '<br>
               <br>
               <hr noshade>');
}

#
#
#

sub main
{
	my($dbh, $uid, $gid, $param, $cookie);

	#
	$param       = variables();
	$cookie      = cookie('osoal');
	($uid, $gid) = authenticate($cookie->{'nickname'}, $cookie->{'passwd'});

	#
	header('News', 'news.png', '/');
	print('<div align="center">
           <table width="600" cellspacing="0" cellpadding="0" border="0">
           <tr>
           <td>', "\n\n");

	#
	if ($dbh = DBI->connect("dbi:mysql:$MySQLserver;database=osoal",
				$MySQLuser, $MySQLpasswd))
	{
		if ($param->{'select'} eq 'News')
		{
			input_news($dbh, $uid, 'News');
		}

		elsif ($param->{'select'} eq 'Modify')
		{
			input_news($dbh, $uid, 'Modify', $param->{'pid'});
		}

		elsif ($param->{'select'} eq 'Preview')
		{
			preview_news($dbh, $param);
		}

		else
		{
			if ($gid == 1)
			{
				if ($param->{'select'} eq 'Submit')
				{
					submit_news($dbh, $uid, $param);
				}

				elsif ($param->{'select'} eq 'Apply')
				{
					apply_news($dbh, $param);
				}

				elsif ($param->{'select'} eq 'Delete')
				{
					delete_news($dbh, $param->{'pid'});
				}
			}

			#
			unless ($param->{'load'} =~ m/^\d+$/ || $param->{'load'} == -1)
			{
				$param->{'load'} = $load;
			}

			#
			print('<form method="GET" action="', $ENV{'SCRIPT_NAME'}, '">
                   <div align="center">
                  [ <a href="',  $ENV{'SCRIPT_NAME'}, '?select=News"><b>Submit Article</b></a>
                  | <b>Display</b> <input type="text" name="load" value="', $param->{'load'}, '" size="3">
                    <b>Articles</b>
                  | <a href="/search.pl?location=News"><b>Search</b></a> ]
                  </div>
                  </form>', "\n\n");

			#
			print_news($dbh, $gid, $param);
		}

		$dbh->disconnect();
	}

	#
	print('</td>
           </tr>
           </table>
           </div>', "\n\n");
	footer();
}

#
main();
