#!/bin/perl
$| = 1;
#####################################
#                                   #
# RobBoard v1.0                     #
# Copyright 1999 by Rob Eisler      #
# rob@robplanet.com                 #
# http://www.robplanet.com          #
#                                   #
# Last modified on Jan 13, '99      #
#                                   #
#####################################
#
# Copyright Notice:
# Copyright 1999 Robert S. Eisler.  All Rights Reserved.
#
# This code may be used and modified by anyone so long as this header and
# copyright information remains intact.  By using this code you agree to 
# indemnify Robert S. Eisler from any liability that might arise from its 
# use.  You must obtain written consent before selling or redistributing 
# this code.
#
#####################################

# declare the variables

$boarddir = '../';
$boardurl = 'http://pvprm.zesoi.fer.hr/dbabic/seminar/board/RobBoard';
$cgi = 'http://pvprm.zesoi.fer.hr/dbabic/seminar/board/RobBoard/cgi/robboard.cgi';
$indexcgi = 'http://pvprm.zesoi.fer.hr/dbabic/seminar/board/RobBoard/cgi/index.cgi';
$queryswitch = '?';

$style = 0;
$threaded = 1;

$maximum = 0;
$maxdisplay = 50;

$fontface = 'arial';
$fontsize = 2;
$tablecolor1 = 'black';
$tablecolor2 = 'white';
$tablecolor3 = 'white';
$fontcolor1 = 'white';
$fontcolor2 = 'black';
$fontcolor3 = 'black';

#########################################################################
# Don't change anything below here unless you know what you're doing :) #
#########################################################################

$datadir = "$boarddir/data";
$datafile = "$boarddir/data.txt";
$index_t = "$boarddir/index_t.html";
$page_t = "$boarddir/page_t.html";
$use_cgi = 1;
$LOCK_EX = 2;
$LOCK_UN = 8;

&readform;

print "Content-type: text/html\n\n";

open (FILE, "$datafile") || &fatal_error("Unable to open $datafile");
&lock(FILE);
@messages = <FILE>;
&unlock(FILE);
close(FILE);

$messagecount = 0;
$threadcount = 0;

for ($a = 0; $a < @messages; $a++) {

	($mnum[$a],$msub[$a],$mfollow[$a],$mname[$a],$memail[$a],$mdate[$a],$mip[$a],$mbrowser[$a],$chop) = split(/``/,$messages[$a]);

	$messagecount++;
	if ($mfollow[$a] == 1) {
		$threadcount++;
	}

}

open (FILE, "$index_t") || &fatal_error("Unable to open $index_t");
&lock(FILE);
@template = <FILE>;
&unlock(FILE);
close(FILE);

for ($a = 0; $a < @template; $a++) {

	$_ = $template[$a];

	if (/<!--messages-->/) {

		if ($style == 1) {

			$displaycounter = 0;
			$itzapizza = 0;
			$remember = $mnum[0];
			print "<table border=0 cellpadding=2>\n";

			for ($b = 0; $b < @messages; $b++) {

				if ($mfollow[$b] > 1) {

					$itzapizza++;

				}

				else {

					unless ($b == 0 || $threaded == 0) {

						print "<tr><td align=right><font face=\"$fontface\" size=\"$fontsize\" color=\"$fontcolor3\"><b>Thread Info:</b></font></td>\n";

						if ($itzapizza == 0) {

							print "<td><font face=\"$fontface\" size=\"$fontsize\" color=\"$fontcolor3\">No more messages in this thread.\n";

						}

						else {

							print "<td><font face=\"$fontface\" size=\"$fontsize\" color=\"$fontcolor3\">$itzapizza more message(s) in this thread.\n";
							$itzapizza = 0;

						}

						print "<a href=\"$cgi$queryswitch";
						print "action=viewthread\&num=$remember\">Post / View this thread</a>.</font></td></tr>\n";
						$remember = $mnum[$b];

					}

					$displaycounter++;

					open (FILE, "$datadir/$mnum[$b]\.txt") || &fatal_error("Unable to open $mnum[$b]\.txt");
					&lock(FILE);
					@body = <FILE>;
					&unlock(FILE);
					close(FILE);

					print "<tr bgcolor=\"$tablecolor1\"><td colspan=2 align=right><font face=\"$fontface\" size=0 color=\"$fontcolor1\">$mdate[$b]</font></td></tr>\n";
					print "<tr><td align=right><font face=\"$fontface\" size=\"$fontsize\" color=\"$fontcolor2\"><b>Posted By:</b></font></td>\n";
					print "<td><font face=\"$fontface\" size=\"$fontsize\" color=\"$fontcolor3\"><a href=\"mailto:$memail[$b]\">$mname[$b]</a></font></td></tr>\n";
					print "<tr><td align=right><font face=\"$fontface\" size=\"$fontsize\" color=\"$fontcolor2\"><b>Subject:</b></font></td>\n";
					print "<td><font face=\"$fontface\" size=\"$fontsize\" color=\"$fontcolor3\">$msub[$b]</font></td></tr>\n";
					print "<tr><td align=right valign=top><font face=\"$fontface\" size=\"$fontsize\" color=\"$fontcolor2\"><b>Message:</b></font></td>\n";
					print "<td><font face=\"$fontface\" size=\"$fontsize\" color=\"$fontcolor3\">@body</font></td></tr>\n";

				}

				if ($displaycounter >= $maxdisplay && $maximum == 1 && $action ne 'showall') { $b = @messages; }

			}

			unless (@messages == 0 || $threaded == 0) {

				print "<tr><td></td>\n";

				if ($itzapizza == 0) {

					print "<td><font face=\"$fontface\" size=\"$fontsize\" color=\"$fontcolor3\">No more messages in this thread.\n";

				}

				else {

					print "<td><font face=\"$fontface\" size=\"$fontsize\" color=\"$fontcolor3\">$itzapizza more message(s) in this thread.\n";

				}

				print "<a href=\"$cgi$queryswitch";
				print "action=viewthread\&num=$remember\">Post / View this thread</a>.</font></td></tr>\n";

			}

			print "</table>\n";

		}

		else {

			$last = 1;
			print "<ul>\n";

			for ($b = 0; $b < @messages; $b++) {
	
				if ($mfollow[$b] > $last) {
					print "<ul>\n";
				}
				else {
					if ($mfollow[$b] < $last) {
						for ($c = 0; $c < $last - $mfollow[$b]; $c++) {
							print "</ul>\n";
						}
					}
				}

				$last = $mfollow[$b];

				print "<li>\n";
				print "<a href=\"$cgi$queryswitch";
				print "action=display\&num=$mnum[$b]\">$msub[$b]</a> ";
				print "- <b>$mname[$b]</b>, $mdate[$b]</li>\n";

				if ($b >= $maxdisplay && $maximum == 1 && $action ne 'showall') { $b = @messages; }

			}

			for ($b = 0; $b < $last; $b++) {

				print "</ul>\n";
	
			}

		}

	}

	elsif (/<!--form-->/) {

		print "<form action=\"$cgi$queryswitch";
		print "action=post&follow=no\" method=post>\n";

		print "<table border=0>\n";

		print "<tr><td align=right>Name: </td>\n";
		print "<td><input type=text name=name size=50></td></tr>\n\n";

		print "<tr><td align=right>E-Mail: </td>\n";
		print "<td><input type=text name=email size=50></td></tr>\n\n";

		print "<tr><td align=right>Subject: </td>\n";
		print "<td><input type=text name=subject size=50></td></tr>\n\n";

		print "<tr><td align=right valign=top>Message: </td>\n";
		print "<td><textarea name=message rows=10 cols=50 wrap=virtual></textarea></td></tr>\n\n";

		print "<tr><td align=center colspan=2>";
		print "<input type=submit value=\"Post this Message\"> ";
		print "<input type=reset value=\"Reset Form\"></td></tr>\n";

		print "</table></form>\n\n";

	}

	elsif (/<!--messagecount-->/) {

		print "$messagecount\n";

	}

	elsif (/<!--threadcount-->/) {

		print "$threadcount\n";

	}

	elsif (/<!--maximum-->/) {

		if ($maximum == 1 && $action ne 'showall' && @messages > $maxdisplay) {

			print "$maxdisplay\n";

		} else {

			print "all\n";

		}			

	}

	elsif (/<!--showall-->/) {

		if ($maximum == 1 && $action ne 'showall' && @messages > $maxdisplay) {

			print "<a href=\"$indexcgi$queryswitch";
			print "action=showall\">Show All Messages</a>\n";

		}

	}

	else {

		print "$_";

	}

}


sub fatal_error {
	local($e) = @_;

	print "<html>\n";
	print "<head><title> RobBoard Fatal Error </title></head>\n";
	print "<body bgcolor=white text=black link=blue vlink=blue>\n";
	print "<font face=arial><center>\n";
	print "<h2>RobMail Fatal Error</h2>\n";

	print "<blockquote>\n";
	print "RobBoard experienced an unrecoverable error. The error seems\n";
	print "to be:<p>\n";

	print "<b>$e</b><p>\n\n";

	print "If this error continues, you should contact the administrator.<p>\n";

	print "<a href=\"$mailurl\">RobBoard Main</a>\n";
	print "</blockquote>\n";
	print "</center>";
	print "</font></body></html>\n";
	exit;
}

sub readform {

	read(STDIN, $input, $ENV{'CONTENT_LENGTH'});
	@pairs = split(/&/, $input);
	foreach $pair (@pairs) {

	        ($name, $value) = split(/=/, $pair);
	        $name =~ tr/+/ /;
	        $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
	        $value =~ tr/+/ /;
	        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
	        $value =~ s/<!--(.|\n)*-->//g;
	        $FORM{$name} = $value;
	}

	$what = $FORM{'what'};

	@vars = split(/&/, $ENV{QUERY_STRING});
	foreach $var (@vars) {
	        ($v,$i) = split(/=/, $var);
	        $v =~ tr/+/ /;
	        $v =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
	        $i =~ tr/+/ /;
	        $i =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
	        $i =~ s/<!--(.|\n)*-->//g;
	        $INFO{$v} = $i;
	}

	$action = $INFO{'action'};
	$startat = $INFO{'startat'};

}

sub lock {
  local($file)=@_;
  flock($file, $LOCK_EX);
}

sub unlock {
  local($file)=@_;
  flock($file, $LOCK_UN);
}
