#!/usr/local/bin/perl
#<a href='/index.html' target='main'>Section One</a> link to make main page react not admin window

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

&parse_return;

exit;

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{'admin_action'} eq login) {
		&check_login;
		}
	else {
		&html_login(0);
		}
}

sub check_login
{
	&read_password;

	if (!$config_file_read) {
		$pass = "2908phubw";
		$user = "Admin";
		}
		
	if (($FORM{'password'} eq $pass) & ($FORM{'userid'} eq $user)) {

		if ($FORM{'proc'} eq rm) {
			&html_delete(0);
			}
		elsif ($FORM{'proc'} eq rt) {
			&html_delete(1);
			}
		elsif ($FORM{'proc'} eq ipc) {
			&html_ip_check;
			}
		elsif ($FORM{'proc'} eq el) {
			&html_email_check;
			}
		elsif ($FORM{'proc'} eq edt) {
			&edit_redirect;
			}
		elsif ($FORM{'proc'} eq edtc) {
			&html_edit;
			}
		elsif ($FORM{'proc'} eq edtf) {
			&edit_file;
			}
		elsif ($FORM{'proc'} eq mn) {
			&html_admin_menu;
			}
		elsif ($FORM{'proc'} eq chpwd) {
			&html_change_password;
			}
		elsif ($FORM{'proc'} eq rstrbk) {
			&html_confirm_restore;
			}
		elsif ($FORM{'proc'} eq bkconf) {
			&undo_last;
			}
		else {
			&html_header('WWW - Message Board Admin - ERROR');
			print "<center><span class=\"bold\">Sorry an unexpected error has occured please retry from main message board\n</span></center></body>\n</html>";
			}
		}

	else {
		&html_login(1);
		}
}

sub read_password
{
	if(!open(config_file,"$config_name")) {
		$config_file_read = 0;
		}
	else { 
		@config_list = <config_file>;
		close(config_file);	

		foreach $config (@config_list) {
			($variable, $value) = split(/=/, $config);
			$value =~ s/\n//g;
			if ($variable eq userid) {
				$user = $value;
				$userid_read = 1;
				}
			elsif ($variable eq password) {
				$pass = $value;
				$pass_read = 1;
				}
			}

		if (($pass_read) & ($userid_read)) {
			$config_file_read = 1;
			}
		else {
			$config_file_read = 0;
			}
		}
}

sub html_confirm_restore
{
	&html_header('WWW - Message Board Admin - Backup Restore');
	print<<confirm_restore;
<table width="50%" align="center">
<tr>
	<th bgcolor="#99CCFF" class="bold">
		WARNING:
	</th>
</tr>
<tr>
	<td bgcolor="#CCCCCC" class="regular">
		Please confirm backup restoration - Note restoring the backup will remove any posts made to the message board after the backup was made. This will be the last time an administrative change was made to the board i.e. Message delete, Thread delete or message edit<br> Please only use if you are certain of its effects.
	</td>
</tr>
<tr>
	<td bgcolor="#99CCFF" align="center">
		<form method="post" action="/$base_dir$admin_name">
		<input type="submit" value="Confirm">
		<input type="button" value="Cancel" onClick="javascript: window.refresh('/$base_dir$board_name')">
	</td>
</tr>
<tr>
	<td>
		<input type="hidden" name="admin_action" value="login">
		<input type="hidden" name="password" value="$FORM{'password'}">
		<input type="hidden" name="userid" value="$FORM{'userid'}">
		<input type="hidden" name="proc" value="bkconf">
		</form>
	</td>
</tr>
</table>
<body>
</html>
confirm_restore
}

sub undo_last
{
	rename("$file_name.bak", "$file_name.tmp");
	rename("$file_name", "$file_name.bak");
	rename("$file_name.tmp", "$file_name");

	&html_header('Restoring Backup Redirect');
	print "<body onLoad=\"javascript: window.refresh('/$base_dir$board_name')\">\n</body>\n</html>";
}

sub html_change_password
{
	&html_header('WWW - Message Board Admin - Change Password');
	print "<body><center><span class=\"bold\">Sorry this function is not working, please edit config file manually</span></center></body></html>";
}

sub html_admin_menu
{
	&html_header('WWW - Message Board Admin - Main');
	print<<admin_main0;
<table width="35%" align="center" border="0" cellpadding="7" cellspacing="3">
<tr>
	<td bgcolor="#99CCFF" class="bold" colspan="2" align="center">
		Administration Main Menu<br>
		Welcome: $FORM{'userid'}
	</td>
</tr>
<tr>
	<td class="regular" colspan="2">
		Please choose operation
	</td>
</tr>
<tr>
<tr>
	<td width="10%">
		<form method="POST" action="/$base_dir$admin_name">
		<input type="radio" name="proc" value="chpwd">
	</td>
	<td class="bold">
		Change Password
		<input type="hidden" name="admin_action" value="login">
		<input type="hidden" name="password" value="$FORM{'password'}">
		<input type="hidden" name="userid" value="$FORM{'userid'}">
	</td>
</tr>
<tr>
	<td>
		<input type="radio" name="proc" value="rstrbk">
	</td>
	<td class="bold">
		Restore Backup
	</td>
</tr>
<tr>
	<td colspan="2" align="center"><br>
		<input type="submit" value="Procede">
		<form> <input type="button" value="Return" onClick="javascript: window.refresh('/$base_dir$board_name')"> </form> 
		</form>
	</td>
</tr>
</table>
</body>
</html>
admin_main0
}

sub edit_redirect
{
	&html_header('Editing Redirect');

	print "<body onLoad=\"opener.refresh('/$base_dir$admin_name?proc=edtc&password=$FORM{'password'}&userid=$FORM{'userid'}&admin_action=login&id=$FORM{'id'}'); javascript: self.close()\">";
}

sub html_edit
{
	$edit_reference = $FORM{'id'};

	&html_header('WWW - Message Board Admin - Edit Message');

	&read_file;

	foreach $one (@board_list) {
		($reference, $title, $message, $starter, $date, $ip_address, $email_address) = split(/\s*\|\s*/, $one);
		if ($edit_reference eq $reference) {
			$FORM{'name'} = $starter;
			$FORM{'email'} = $email_address;
			$FORM{'title'} = $title;
			$message =~ s/<\*R\*\>//g;
			$FORM{'message'} = $message;		
			}
		}

	&html_edit_form;
}

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

	print<<EOF1;
<body>
<table width="75%" align="center" border="0" cellpadding="7" cellspacing="3">
	<form method="POST" action="/$base_dir$admin_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" class=\"regular\">
EOF1
		print "\n\t\t$FORM{'name'}";
#		print "\n\t\t<input type=\"text\" name=\"name\" value=\"$FORM{'name'}\" size=\"30\">";

print<<EOF10;
		</td>
	</tr>
	<tr>
		<td bgcolor="#99CCFF" class=\"bold\">
			Email:
		</td>
		<td bgcolor="#CCCCCC" colspan="2" class=\"regular\">
EOF10
		print "\n\t\t$FORM{'email'}";
#		print "\n\t\t<input type=\"text\" name=\"email\" value=\"$FORM{'email'}\" size=\"30\">";
print<<EOF11;
		</td>
	</tr>
	<tr>
		<td bgcolor="#99CCFF" class=\"bold\">
			Title:
		</td>
		<td bgcolor="#CCCCCC" colspan="2" class=\"regular\">
EOF11
		print "\n\t\t$FORM{'title'}";
#		print "\n\t\t<input type=\"text\" name=\"title\" value=\"$FORM{'title'}\" size=\"30\">";
# Select line if you want to be able to edit all fields instead of just message.
print<<EOF12;
		</td>
	</tr>
	<tr>
		<td valign="top" bgcolor="#99CCFF" class=\"bold\">
			Message:
		</td>
		<td bgcolor="#CCCCCC" colspan="2">
EOF12
		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">
		</td>
		<td bgcolor="#99CCFF" align="center">
			<input type="submit" value="Edit Message">
		</td>
		<td bgcolor="#99CCFF" align="center">
			<input type="reset" value="Reset Form">
			<input type="hidden" name="proc" value="edtf">
EOF13
	print "\t\t\t<input type=\"hidden\" name=\"edit\" value=\"$edit_reference\">\n";
	print "\t\t\t<input type=\"hidden\" name=\"admin_action\" value=\"login\">\n";
	print "\t\t\t<input type=\"hidden\" name=\"password\" value=\"$FORM{'password'}\">\n";
	print "\t\t\t<input type=\"hidden\" name=\"userid\" value=\"$FORM{'userid'}\")>\n";
	print "\t\t</td>\n\t</tr>\n</form>\n</table>\n<SCRIPT LANGUAGE=\"JavaScript\"><!--\ndocument.forms[0].elements[0].focus();\n//--></SCRIPT>\n</body>\n</html>";
}

# Checkbox entry - allow email??
#	<tr>
#		<td bgcolor="#99CCFF" class=\"bold\">
#			Do You:
#		</td>
#		<td bgcolor="#CCCCCC" colspan="2">
#			<input type="checkbox" name="CheckBox1" value="1">Service
#		</td>
#	</tr>

sub edit_file
{
# open file search for message line, pull out date and ip, delete line
# then insert new edited one

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

	open(FILE_SOURCE, "$file_name");
	flock(FILE_SOURCE, 2);
	@message_list=<FILE_SOURCE>;

	open(FILE_DEST,">destination.txt");
	foreach $one (@message_list) {
		($reference, $title, $message, $starter, $date, $ip_address, $email_address) = split(/\s*\|\s*/, $one);
		if ($FORM{'edit'} ne $reference) {
			print FILE_DEST "$one";
			}
		else {
			print FILE_DEST "$FORM{'edit'}|$title|$FORM{'message'}|$starter|$date|$ip_address|$email_address";
			}
		}

	close(FILE_DEST);
	close(FILE_SOURCE);

	rename("$file_name", "$file_name.bak");
	rename("destination.txt", "$file_name");

	&html_header;
	print "<body onLoad=\"window.refresh('/$base_dir$board_name?reference=$FORM{'edit'}&board_action=view')\">";
}

sub html_ip_check
{
	$ip_reference = $FORM{'id'};

	&html_header('WWW - Message Board Admin - IP Address');
	print<<ip_check;
<body>
<center>
<span class="bold">
ip_check

	&read_file;

	foreach $one (@board_list) {
		($reference, $title, $message, $starter, $date, $ip_address) = split(/\s*\|\s*/, $one);
		if ($ip_reference eq $reference) {
			print "Title: $title<br>\nAuthor: $starter<br>\nLogged IP Address: $ip_address";
			}
		}

	print<<ip_check2;
</span>
<form>
<input type="button" value="Close" onClick=\"javascript: self.close()\">
</form>
</center>
<SCRIPT LANGUAGE="JavaScript"><!--
document.forms[0].elements[0].focus();
//--></SCRIPT>
</body>
</html>
ip_check2
}

sub html_email_check
{
	$ip_reference = $FORM{'id'};

	&html_header('WWW - Message Board Admin - Email Address');

	print "<body>\n<center>\n<span class=\"bold\">";

	&read_file;

	foreach $one (@board_list) {
		($reference, $title, $message, $starter, $date, $ip_address, $email_address) = split(/\s*\|\s*/, $one);
		if ($ip_reference eq $reference) {
			print "Title: $title<br>\nAuthor: $starter<br>\nEmail Address: $email_address";
			}
		}

	print<<email_check;
</span>
<form>
<input type="button" value="Close" onClick=\"javascript: self.close()\">
</form>
</center>
<SCRIPT LANGUAGE="JavaScript"><!--
document.forms[0].elements[0].focus();
//--></SCRIPT>
</body>
</html>
email_check
}

sub read_file 
{
	open(FILE_IP,"$file_name"); 
	@board_list = <FILE_IP>;
	close(FILE_IP);

	$text_lines = @board_list;						# Returns number of lines
}

sub html_delete
{
	local ($delete_type) = @_;

	&html_header('WWW - Message Board Admin - Delete');

	if ($delete_type == 0) {
		&message_delete($FORM{'id'});
		print "<body onLoad=\"opener.refresh('/$base_dir$board_name?reference=$FORM{'id'}&board_action=view'); javascript: self.close()\">";
		}
	elsif ($delete_type == 1) {
		&thread_delete($FORM{'id'});
		print "<body onLoad=\"opener.refresh('/$base_dir$board_name'); javascript: self.close()\">";
		}	
}

sub message_delete
{
	local ($reference_delete) = @_;

	open(FILE_SOURCE, "$file_name");
	flock(FILE_SOURCE, 2);
	@message_list=<FILE_SOURCE>;

	open(FILE_DEST,">destination.txt");
	foreach $one (@message_list) {
		($reference, $title, $message, $starter, $date) = split(/\s*\|\s*/, $one);
		if ($reference_delete ne $reference) {
			print FILE_DEST "$one";
			}
		}

	close(FILE_DEST);
	close(FILE_SOURCE);

	rename("$file_name", "$file_name.bak");
	rename("destination.txt", "$file_name");
}

sub thread_delete
{
	local ($reference_delete) = @_;

	open(FILE_SOURCE, "$file_name");
	flock(FILE_SOURCE, 2);
	@message_list=<FILE_SOURCE>;

	open(FILE_DEST,">destination.txt");
	foreach $one (@message_list) {
		($reference, $title, $message, $starter, $date) = split(/\s*\|\s*/, $one);
		if ($reference =~ m/,/) {	# Matches all messages with correct first digit of reference
			($reference, $sub_reference_number) = split(/,/, $reference);			
			}
		if ($reference_delete ne $reference) {
			print FILE_DEST"$one";
			}
		}

	close(FILE_DEST);
	close(FILE_SOURCE);

	rename("$file_name", "$file_name.bak");
	rename("destination.txt", "$file_name");
}

sub html_header
{
	local($html_title) = @_;
	print "Content-type: text/html\n\n";
	print "<!- This message board is (c) copyright of Andrew Gatt (phubw\@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;
	}
	.heading {
		font-family : Verdana,Georgia;
		font-size : 15px;
		color : #000000;
		font-weight : bold;
	}		
	.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;
	}
</style>
<script language="JavaScript">

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

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

sub html_login
{
	local ($login_incorrect) = @_;

	&html_header('Message Board Administrator Login');

	if ($FORM{'proc'} eq rm) {
			$operation = "Delete Message";
			}
		elsif ($FORM{'proc'} eq rt) {
			$operation = "Delete Thread";
			}
		elsif ($FORM{'proc'} eq ipc) {
			$operation = "Check IP Address";
			}
		elsif ($FORM{'proc'} eq el) {
			$operation = "Check Email Address";
			}
		elsif ($FORM{'proc'} eq edt) {
			$operation = "Edit Message";
			}
		elsif ($FORM{'proc'} eq mn) {
			$operation = 0;
			}
		else {
			$dont_show_operation = 1;
			$operation = 1;
			}


	print "<body><table width=\"30%\" align=\"center\">";

	print<<EOF0;
<form method="POST" action="/$base_dir$admin_name">
<tr>
	<td colspan="2" class="heading" align="center">Administration Login<br><br></td>
</tr>
<tr>
	<td bgcolor="#99CCFF" class="bold" width="45%"  align="center">User ID:</td>
	<td  width="55%" align="left"><input type="text" name="userid" size="15" class="regular"></td>
</tr>
<tr>
	<td bgcolor="#99CCFF" class="bold" align="center">Password:</td>

	<td align="left"><input type="password" name="password" size="15" class="regular">
EOF0
	print "<input type=\"hidden\" name=\"admin_action\" value=\"login\">";
	print "<input type=\"hidden\" name=\"id\" value=\"$FORM{'id'}\">\n";
	print "<input type=\"hidden\" name=\"proc\" value=\"$FORM{'proc'}\"></td>\n";

	if (!$dont_show_operation) {
		if ($operation) {
			print "<tr>\n\t<td colspan=\"2\" class=\"small\" align=\"center\">\n\t\t<b>Login</b> to confirm, <b>Close</b> to cancel<br>Pending: <b>$operation</b>\n\t</td>\n</tr>";
			}
		}

	print<<EOF1;
</tr>
<tr>
	<td colspan="2" align="center" class="bold">
EOF1
	if ($login_incorrect == 1) {
		print "* Invalid Login, Try Again";
		}
	print "<br><br><input type=\"submit\" value=\"Login\"><form> ";

	if ($operation) {
		print "<input type=\"button\" value=\"Close\" onClick=\"javascript: self.close()\"> </form> </td>";
		}
	else {
		print "<input type=\"button\" value=\"Return\" onClick=\"javascript: window.refresh('/$base_dir$board_name')\"> </form> </td>";
		}
	print<<EOF2;
</tr>
</table>
</form>
<SCRIPT LANGUAGE="JavaScript"><!--
document.forms[0].elements[0].focus();
//--></SCRIPT>
</body>
</html>
EOF2
}