#!/usr/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 = '/home/panther/public_html/vds';
$boardurl = 'http://www.toppoint.de/~panther/vds';
$cgi = 'http://www.toppoint.de/~panther/cgi-bin/vds/robboard.cgi';
$indexcgi = 'http://www.toppoint.de/~panther/cgi-bin/vds/index.cgi';
$queryswitch = '?';

$style = 0;
$threaded = 1;

$maximum = 0;
$maxdisplay = 10;

$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 (/<!--htmlout-->/) {
                $last = 1;

		print "<ul compact><li>\n";
                for ($b = 0; $b < @messages; $b++) {
                        $last = $mfollow[$b];

                        open (FILE, "$datadir/$mnum[$b]\.txt") || &fatal_error("Unable to open $mnum[$b]\.txt");
                        &lock(FILE);
                        @body = <FILE>;
                        &unlock(FILE);
                        close(FILE);
			
			if ($last <= 1) {
                                ($pl3v, $pl3n) = split(/\(/,$mdate[$b],2);
                                print "<li> $msub[$b] ( $pl3v) - $mname[$b]: @body\n";
                        }
                        else {
                                print "<br> $mname[$b]: @body\n"
                        }
                }
                print "</li></ul>\n";
        }
	
	elsif (/<!--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>Beitrag Info:</b></font></td>\n";

						if ($itzapizza == 0) {

							print "<td><font face=\"$fontface\" size=\"$fontsize\" color=\"$fontcolor3\">noch keine weiteren Beitraee hierzu.\n";

						}

						else {

							print "<td><font face=\"$fontface\" size=\"$fontsize\" color=\"$fontcolor3\">$itzapizza Beitraege hierzu.\n";
							$itzapizza = 0;

						}

						print "<a href=\"$cgi$queryswitch";
						print "action=viewthread\&num=$remember\">Veroeffentlichen / lese diesen Beitrag </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>veroeffentlicht von:</b></font></td>\n";
					print "<td><font face=\"$fontface\" size=\"$fontsize\" color=\"$fontcolor3\">$mname[$b]</font></td></tr>\n";
					print "<tr><td align=right><font face=\"$fontface\" size=\"$fontsize\" color=\"$fontcolor2\"><b>Beitrag:</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>Beitrag:</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\">keine weiteren Beitraege hierzu.\n";

				}

				else {

					print "<td><font face=\"$fontface\" size=\"$fontsize\" color=\"$fontcolor3\">$itzapizza Beitraege hierzu.\n";

				}

				print "<a href=\"$cgi$queryswitch";
				print "action=viewthread\&num=$remember\">veroeffentlichen / lese in diesem Beitrag</a>.</font></td></tr>\n";

			}

			print "</table>\n";

		}

		else {

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

			for ($b = 0; $b < @messages; $b++) {
	
				if ($mfollow[$b] > $last) {
					print "<ul compact> \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>von: </td>\n";
		print "<td><input type=text name=name size=80></td></tr>\n\n";

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

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

		print "<tr><td align=center colspan=2>";
		print "<input type=submit value=\"diesen Beitrag veroeffentlichen\"> ";
		print "<input type=reset value=\"Formularinhalte loeschen\"></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 "alle\n";

		}			

	}

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

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

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

		}

	}

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

                       open (FILE, "footer\.html") || &fatal_error("Unable  to open footer");
                        &lock(FILE);
                        @bddy = <FILE>;
                        &unlock(FILE);
                        close(FILE);

                        print "@bddy";
                }

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

                       open (FILE, "head1\.html") || &fatal_error("Unable  to open head1");
                        &lock(FILE);
                        @bddy = <FILE>;
                        &unlock(FILE);
                        close(FILE);

                        print "@bddy";
                }
               elsif (/<!--head2-->/) {

                       open (FILE, "head2\.html") || &fatal_error("Unable  to open head2");
                        &lock(FILE);
                        @bddy = <FILE>;
                        &unlock(FILE);
                        close(FILE);

                        print "@bddy";
                }
               elsif (/<!--style-->/) {

                       open (FILE, "style\.html") || &fatal_error("Unable  to open style");
                        &lock(FILE);
                        @bddy = <FILE>;
                        &unlock(FILE);
                        close(FILE);

                        print "@bddy";
                }

	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);
}
