#!/usr/local/bin/perl	#have to start with this line for Unix servers

# Format - Messages are stored as single line | delimited with reference number in form 1 indicates message 1, 1,1 indicates first follow up to message 1...
#    if ($s =~ /if by la/) {print "YES"} - example of pattern matching condition
#    else {print "NO"}
# Add words to swear dictionary??
##############################################################################

$base_dir = 	"cgi-bin/";
$file_name = 	"messages.txt";
$board_name =	"board4.pl";
$admin_name =	"webadmin.pl";

&read_file;

&parse_return;

exit;

sub html_main
{
	&html_header('Message Board Main');

	print<<EOF0;
<table width="100%">
<tr>
	<th class="bold">
		Message Board
	</th>
</tr>
<tr>
	<td class="small" colspan="4">
		<a href="/$base_dir$admin_name?proc=mn">Administration</a>
	</td>
</tr>
<tr>
	<td>
		<table width="95%" align="center" cellpadding="6">
		<tr>
			<td width="40%" bgcolor="#99CCFF" class="bold" align="center">
				Message Title
			</td>
			<td width="10%" bgcolor="#99CCFF" class="bold" align="center">
				Replies
			</td>
			<td width="20%" bgcolor="#99CCFF" class="bold" align="center">
				Date Started
			</td>
			<td width="30%" bgcolor="#99CCFF" class="bold" align="center">
				Topic Starter
			</td>
		</tr>
		<tr>
EOF0
	foreach $one (@message_list) {
		($reference, $title, $message, $starter, $date) = split(/\s*\|\s*/, $one);
		if ($reference =~ m/,/) {
			$reply_count[$reference]++;
			}	
		}

	for ($n=($text_lines-1); $n >= 0; $n--) {

		($reference, $title, $message, $starter, $date) = split(/\s*\|\s*/, $message_list[$n]);

		if ($reply_count[$reference] eq "") {
			$reply_count[$reference] = 0;
			}

		if ($reference !~ (m/,/)) {
			print "<tr>";
			print "<td width=\"40%\" bgcolor=\"#CCCCCC\" class=\"regular\" align=\"center\">\n\t<a href=/$base_dir$board_name?reference=$reference&board_action=view  onmouseover=\"window.status='View Message';return true\" onmouseout=\"window.status=' ';return true;\">$title</a>\n</td>";
			print "<td width=\"10%\" bgcolor=\"#CCCCCC\" class=\"regular\" align=\"center\">\n\t$reply_count[$reference]\n</td>";
			print "<td width=\"20%\" bgcolor=\"#CCCCCC\" class=\"regular\" align=\"center\">\n\t$date\n</td>";
			print "<td width=\"30%\" bgcolor=\"#CCCCCC\" class=\"regular\" align=\"center\">\n\t$starter\n</td>";
			print "</tr>";
			}
		}

print<<EOF2;
		<tr>
			<td colspan="4" align="center">
				<a href="/$base_dir$board_name?board_action=post" onmouseover=\"window.status='Start New Thread';return true\" onmouseout=\"window.status=' ';return true;\">Post New Message</a>
			</td>
		</tr>
		</table>
	</td>
</tr>

</table>
</body>
</html>
EOF2
}

sub html_post
{
	&html_header('Message Board Post');
	&html_post_form('new_thread');
print<<EOF4
		</td>
	</tr>
</form>
</table>
<br>
</body>
</html>
EOF4
}

sub html_post_reply
{
	&html_header('Message Board Reply');
	&html_post_form('reply');
	print "\t\t\t<input type=\"hidden\" name=\"reply_to\" value=\"$FORM{'reply_to'}\">\n";

print<<EOF5
		</td>
	</tr>
</form>
</table>
<br>
</body>
</html>
EOF5
}

sub html_post_form
{
	local($html_post_to) = @_;

	if ($FORM{'reply_to'} =~ m/e/g) {
		foreach $one (@message_list) {
		($reference, $title, $message, $starter, $date, $ip_address, $email_address) = split(/\s*\|\s*/, $one);		
			if ($reference eq $FORM{'reply_to'}) {
				$FORM{'name'} = $starter;
				$FORM{'email'} = $email_address;
				$FORM{'title'} = $title;
				$FORM{'message'} = $message;
				}
			}
		}

	print<<EOF1;
<body>

<table width="75%" align="center" border="0" cellpadding="7" cellspacing="3">
	<form method="POST" action="/$base_dir$board_name">
	<tr>
		<th bgcolor="#99CCFF" colspan="3" class=\"bold\">
			Message Board
		</th>
	<tr>

		<td bgcolor="#99CCFF" class=\"bold\" width=\"15%\">
			Name:
		</td>
		<td bgcolor="#CCCCCC" colspan="2">
EOF1
	print "\n\t\t<input type=\"text\" name=\"name\" size=\"30\"";

	if ($name_blank == 1) {
		print "><span class=\"regular\"> *  Name Required</span>";
		}
	else {
		if ($FORM{'name'} eq "") {
			print "><span class=\"regular\"> (* required)</span>";
			}
		else {
			print " value=\"$FORM{'name'}\">";
			}
		}
print<<EOF10;
		</td>
	</tr>
	<tr>
		<td bgcolor="#99CCFF" class=\"bold\">
			Email:
		</td>
		<td bgcolor="#CCCCCC" colspan="2">
EOF10
	print "\n\t\t<input type=\"text\" name=\"email\" size=\"30\"";

	if ($email_blank == 1) {
		print "><span class=\"regular\"> * VALID Email Required</span>";
		}
	else {
		if ($FORM{'email'} eq "") {
			print "><span class=\"regular\"> (* required)</span>";
			}
		else {
			print " value=\"$FORM{'email'}\">";
			}
		}
print<<EOF11;
		</td>
	</tr>
	<tr>
		<td bgcolor="#99CCFF" class=\"bold\">
			Title:
		</td>
		<td bgcolor="#CCCCCC" colspan="2">
EOF11
	print "\n\t\t<input type=\"text\" name=\"title\" size=\"30\"";

	if (($html_post_to eq reply) & ($FORM{'re_title'} ne "")) {
		print " value=\"RE: $FORM{'re_title'}\">";
		}

	elsif ($title_blank == 1) {
		print "><span class=\"regular\"> *  Title Required</span>";
		}
	else {
		if ($FORM{'title'} eq "") {
			print "><span class=\"regular\"> (* required)</span>";
			}
		else {
			print " value=\"$FORM{'title'}\">";
			}
		}

print<<EOF12;
		</td>
	</tr>
	<tr>
		<td valign="top" bgcolor="#99CCFF" class=\"bold\">
			Message:
		</td>
		<td bgcolor="#CCCCCC" colspan="2">
EOF12
	if ($message_blank == 1) {
		print "\n\t\t<textarea name=\"message\" cols=\"50\" rows=\"10\" wrap=virtual class=\"regular\">* Message Required</textarea>";
		}

	else {
		print "\n\t\t<textarea name=\"message\" cols=\"50\" rows=\"10\" wrap=virtual class=\"regular\">$FORM{'message'}</textarea>";
		}
print<<EOF13;
		</td>
	</tr>
	<tr>
		<td bgcolor="#99CCFF" class=\"bold\">
			Do You:
		</td>
		<td bgcolor="#CCCCCC" colspan="2">
			<input type="checkbox" name="CheckBox1" value="1">Service
		</td>
	</tr>
	<tr>
		<td bgcolor="#99CCFF">
		</td>
		<td bgcolor="#99CCFF" align="center">
			<input type="submit" value="Post Message">
		</td>
		<td bgcolor="#99CCFF" align="center">
			<input type="reset" value="Reset Form">
			<input type="hidden" name="board_action" value="post_file">
EOF13
	print "\t\t\t<input type=\"hidden\" name=\"post_to\" value=\"$html_post_to\">\n";

}

sub file_post
{
	$blank = 0;
	if ($FORM{'message'} eq "") {
		$message_blank = 1;
		$blank = 1;
		}

	if ($FORM{'name'} eq "") {
		$name_blank = 1;
		$blank = 1
		}

	if ($FORM{'title'} eq "") {
		$title_blank = 1;
		$blank = 1;
		}

	if (($FORM{'email'} eq "") | ($FORM{'email'} !~ m/@\s*/) | ($FORM{'email'} !~ m/\./)){
		$email_blank = 1;
		$blank = 1;				# add back in if want to check for valid email
		}

	if ($blank == 0) {

		&html_header('Message Posted');
		print "<META HTTP-EQUIV=\"refresh\" CONTENT=\"3; url=\"/$base_dir$board_name\">\n"; 	
		print "<center><h2>\nThank you for posting message, please wait for page refresh</h2></center>\n";
		print "</body></html>";

		$current_time = time;
		$time_zone_calibration = 0;
		local($sec,$min,$hour,$mday,$mon,$yday,$wday,$isdst) = localtime($current_time - $time_zone_calibration);

		$min = "0" . $min if ($min < 10);
		$hour = "0" . $hour if ($hour < 10);
		$mon++;
		$sec = "0" . $sec if ($sec < 10);
		$mon = "0" . $mon if ($mon < 10);
		$mday = "0" . $mday if ($mday < 10);
		$yday = "0" . $yday if ($yday < 10);
   
		$yday = 1900 + $yday;

		$msgdate = "$mon/$mday/$yday $hour:$min";

		$FORM{'message'} =~ s/\n/<\*R\*\>/g;

		if ($FORM{'reply_to'} =~ s/e//g) {
			$next_reference = $FORM{'reply_to'};
			}

		else {
			if ($FORM{'post_to'} eq new_thread) {
				foreach $search2 (@message_list) {					# better function would be to look straight at last entry (like EOF in C)
					($reference, $title, $message, $starter, $date) = split(/\s*\|\s*/, $search2);
					if ($reference =~ /,/g) {
						$last_reference_number =~ s/,*//g;
						} 
					else {
						$last_reference_number = $reference;
						}
					}
				$next_reference_number = ++$last_reference_number;
				}

			elsif ($FORM{'post_to'} eq reply) {
				foreach $search2 (@message_list) {					# better function would be to look straight at last entry (like EOF in C)
					($reference, $title, $message, $starter, $date) = split(/\s*\|\s*/, $search2);
					if ($FORM{'reply_to'} =~ (m/[$reference$reference,*]/)) {
						($last_reference_number, $sub_reference_number) = split(/,/, $reference);
						}
					}
				$next_sub_reference = ++$sub_reference_number;
				$next_reference_number = "$FORM{'reply_to'},$next_sub_reference";
				}
			}

		$FORM{'name'} = &remove_bad($FORM{'name'});
		$FORM{'title'} = &remove_bad($FORM{'title'});
		$FORM{'message'} = &remove_bad($FORM{'message'});

		open(FILE1,">>$file_name");
		print FILE1 "$next_reference_number|$FORM{'title'}|$FORM{'message'}|$FORM{'name'}|$msgdate|$ENV{'REMOTE_ADDR'}|$FORM{'email'}\n";
		close(FILE1);
		}

	else {
		if ($FORM{'post_to'} eq new_thread) {
			&html_post;
			}
		elsif ($FORM{'post_to'} eq reply) {
			&html_post_reply;
			}
		}
}

sub remove_bad
{
	local($st) = @_;

	$swearwords = "shit|prick|fuck|fu.ck|fuc.k|f.uck|fucker|fucking|bitch|cunt|c.unt|cu.nt|cun.t|cunting|tits|nigga|nigger|bastard|cock|dick|shite";
	# Can't have '*' in swearword string else matches all values and posts nothing!!
	$st =~ s/\b$swearwords\b//gi;						# remove swearwords

	$st =~ s/\|//g;								# remove dangerous characters ('|')
	return($st);
}

sub parse_return
{
	read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});				# Read from STDIN (passed by server), up to enviromental variable 'CONTENT_LENGTH', into variable $buffer

	if(length($buffer) < 1) {
		$buffer = $ENV{QUERY_STRING};
	}		

	@pairs = split(/&/, $buffer);						# Data is sent by POST method, as "first=1&second=2...", so split into name=value pairs.

	foreach $pair (@pairs) {
		($name, $value) = split(/=/, $pair);				# For each name=value pair further split into name and value
		$value =~ tr/+/ /;						# URL decoding (change '+' back to ' ')
		$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;	# URL decoding (change encoded characters back to ASCII)
		$FORM{$name} = $value;						# Save pairs in $FORM hash under name elements
	}	

	if ($FORM{'board_action'} eq view) {
		&message_read($FORM{'reference'});
		}
	elsif ($FORM{'board_action'} eq post) {
		&html_post;
		}
	elsif ($FORM{'board_action'} eq post_reply) {
		&html_post_reply;
		}
	elsif ($FORM{'board_action'} eq post_file) {
		&file_post($FORM);
		}
	elsif ($FORM{'board_action'} eq list) {
		&html_main;
		}
	else {
		&html_main;
		}
}

sub message_read
{
	local($message_reference) = @_;

	($message_reference, $sub_reference_number) = split(/,/, $message_reference);	

	&html_header('View Message Thread');

	print<<EOF3;
<table width="95%" align="center" border="0" cellpadding="5" cellspacing="3">
<tr>
EOF3
	foreach $one (@message_list) {
		($reference, $title, $message, $starter, $date) = split(/\s*\|\s*/, $one);
		$full_reference = $reference;	
		$message =~ s/<\*R\*\>/<br>\n/g;

		$sub_message = 0;

		if ($reference =~ m/,/) {
			($reference, $sub_reference_number) = split(/,/, $reference);
			$sub_message = 1;			
			}

		if ($message_reference == $reference) {
			$reply_title = $title;		
			print "\t<td bgcolor=\"#99CCFF\" width=\"70%\" class=\"bold\">\n\t\tAuthor: $starter\n\t</td>\n\t<td bgcolor=\"#99CCFF\" class=\"regular\" width=\"30%\">\n\t\tTopic: $title</td>";
			print "\n</tr>\n<tr>\n\t<td bgcolor=\"#CCCCCC\" colspan=\"2\" width=\"75%\">\n\t\t<table cellpadding=\"3\" cellspacing=\"1\" width=\"100%\"><tr><td width=\"75%\" class=\"small\">Posted: $date <a href=\"#\" onClick=\"open_page('/$base_dir$admin_name?proc=ipc&id=$full_reference')\"; onmouseover=\"window.status='Administration - Display Logged IP Address';return true\" onmouseout=\"window.status=' ';return true;\">IP Address</a> <a href=\"#\" onClick=\"open_page('/$base_dir$admin_name?proc=el&id=$full_reference')\"; onmouseover=\"window.status='Administration - Display Email Address of Author';return true\" onmouseout=\"window.status=' ';return true;\">Email Address</a> <a href=\"#\" onClick=\"open_page('/$base_dir$admin_name?proc=edt&id=$full_reference')\"; onmouseover=\"window.status='Administration - Edit Message';return true\" onmouseout=\"window.status=' ';return true;\">Edit</a></td>\n\t\t<td class=\"small\">";
			if ($sub_message == 1) {
				print "Delete <a href=\"#\" onClick=\"open_page('/$base_dir$admin_name?proc=rm&id=$full_reference')\"; onmouseover=\"window.status='Administration - Deletes Message';return true\" onmouseout=\"window.status=' ';return true;\">Message</a> ";
 				}
			else {
				print "Delete";
				}
			print " | <a href=\"#\" onClick=\"open_page('/$base_dir$admin_name?proc=rt&id=$full_reference')\"; onmouseover=\"window.status='Administration - Deletes Entire Thread';return true\" onmouseout=\"window.status=' ';return true;\">Thread</a>";
			print "</td></tr>\n\t<tr>\n\t\t<td colspan=\"2\" class=\"regular\"><hr>$message\n\t</td></tr></table>\n</tr>\n<tr>";
			}
		}
	print "\n\t<td align=\"center\" colspan=\"3\">\n\t\t<a href=\"/$base_dir$board_name?board_action=post_reply&reply_to=$message_reference&re_title=$reply_title\" onmouseover=\"window.status='Post Reply';return true\" onmouseout=\"window.status=' ';return true;\">Post Reply</a>\n\t<a href=\"/$base_dir$board_name\" onmouseover=\"window.status='List Posts - Main Index';return true\" onmouseout=\"window.status=' ';return true;\">List Posts</a></td>";
	print "\n</tr>\n</table>\n</body>\n</html>";
}

sub read_file 
{
	if(!open(FILE2,"$file_name")) {
		$new_message_board_file = 1;
		if (open(new_board, ">$file_name")) {
			print new_board "1|Welcome|Welcome to your new message board courtesy of APGatt Soft&#169\; (wellies\@yahoo.com). Help can be found in the original zip file, called README. Please use and enjoy.|Administrator|06/18/2002 01:00|0.0.0.0|wellies\@yahoo.com\n";
			close(new_board);
			open(FILE2, "$file_name");
			}
		else {
			&html_header('Error Creating New Board');
			print "<h2><center>\nThere has been an error creating the new board. ERROR: $!</center>\n</h2></body></html>";
			}
		}

	@message_list = <FILE2>;
	close(FILE2);
	$text_lines = @message_list;						# Returns number of lines
}

sub html_header
{
	local($html_title) = @_;
	print "Content-type: text/html\n\n";
	print "<!- This message board is copyright (c) of Andrew Gatt (wellies\@yahoo.com)\n-->\n";
	print "<html><head><title>$html_title</title>";

print<<header_print;
<style>
	.header_link {
		font-family : Verdana,Georgia;
		font-size : 12px;
		color : #000000
	}
	.regular {
		font-family : Verdana,Georgia;
		font-size : 13px;
		color : #000000;
	}
	.menu {
		text-decoration : none;
	}
	.small {
		font-family : Verdana,Georgia;
		font-size : 10px;
		color : #000000;
	}
	.bold {
		font-family : Verdana,Georgia;
		font-size : 13px;
		color : #000000;
		font-weight : bold;
	}
	.normal_link {
		font-family : Arial;
		font-size : 16px;
		color : #800080;
	}
	.title {
		font-family : Verdana,Georgia;
		font-size : 38px;
		color : #800080;
	}
	.important {
		font-family : Verdana
		font-size : 12px;
		color : #FF2020;
	}
</style>

<script language="JavaScript">

function open_page(page)
{
open_admin=window.open(page,'open_admin','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=400,height=210,left=20,top=20');
self.name="main"
}

function refresh(to)
{
window.location.href=to;
}

</script>
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="-1">
</head><body>
header_print
}

sub html_footer
{
	print "</body>\n</html>\n"; 
}