#!/usr/bin/perl
#################################################################
#             Message Board V4.0.1 (Freeware)
#
# This program is distributed as freeware. We are not            	
# responsible for any damages that the program causes	
# to your system. It may be used and modified free of 
# charge, as long as the copyright notice
# in the program that give me credit remain intact.
# If you find any bugs in this program. It would be thankful
# if you can report it to us at cgifactory@cgi-factory.com.  
# However, that email address above is only for bugs reporting. 
# We will not  respond to the messages that are sent to that 
# address. If you have any trouble installing this program. 
# Please feel free to post a message on our CGI Support Forum.
# Selling this script is absolutely forbidden and illegal.
##################################################################
#
#               COPYRIGHT NOTICE:
#
#         Copyright 1999-2000 CGI-Factory.com TM 
#		  A subsidiary of SiliconSoup.com LLC
#
#
#      Web site: http://www.cgi-factory.com
#      E-Mail: cgifactory@cgi-factory.com
#      Released Date: April 08, 2000
#	
#   Message Board V4.0 is protected by the copyright 
#   laws and international copyright treaties, as well as other 
#   intellectual property laws and treaties.
###################################################################
##don't change this variable unless you know what you are doing
$display_pl="display.pl";
$bench="0";
#############################
#filename for the variable files 
$mcfg="mcfg.pl";
$mgcfg="mgcfg.pl";
$vcfg="vcfg.pl";
#filename for superuser.db
$superuser="superuser.db";
#############################
$start=(times)[0];
if ($ENV{'REQUEST_METHOD'} eq 'GET') {
@pairs = split(/&/, $ENV{'QUERY_STRING'});
}
else {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
}
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/\s/g;
if ($name && $value) { $input{$name} = $value; }
if ($value=~ tr/;<>*|`&$!#()[]{}:'"//) {
print "Content-type: text/html\n\n";
&header;
print "<h2>Action canceled</h2>\n";
print "Please don't use weird symbols\n";
&footer;
exit;
}
}
#import variables. However, use eval to avoid 500 error message 
#when something goes wrong
eval {
require "$mcfg";
require "$vcfg";
};
if ($@) {
print "Content-type: text/html\n\n";
print "Error in mcfg.pl, vcfg.pl, or mgcfg.pl<br><br>
If you don't know how to fix this problem or you are using Windows NT servers,
please check out our faq section or use our technical support forum at cgi-factory.com.
<br><br>Error Message:<br> $@";
exit;
}
#
open (board, "<$bdata/board1.bd") or &error("Unable to open board1.bd in the data directory.");
if ($flock eq "y") {
flock board, 2;	
}	
@board=<board>;
close (board);
chomp(@board[0]);
chomp(@board[1]);
chomp(@board[2]);
chomp(@board[3]);
chomp(@board[4]);
$messages=@board[0];
$messages2=@board[1];
	
@months = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
@days = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
print "Content-type: text/html\n\n";
opendir (DIR, "$messages") or &error("Unable to open the dir for reading");
if ($flock==1) {
flock DIR, 2; 
}
@reading=readdir(DIR);
@reading=grep(/\.dat/, @reading);
close (DIR);
$total=0;
COUNTING:foreach $read(@reading) {
$read=~ s/\.dat//;
open (content, "<$messages/$read.dat") or next COUNTING;
if ($flock eq "y") {
flock content, 2; 
}
@content=<content>;
close (content);
$order[$total]="@content[2]x$read";
$total++;
}
@order=sort(@order);
@order=reverse(@order);	
if ($input{'next'} and $input{'next'}<$total and $input{'next'}>=0) {
@order=splice(@order, $input{'next'}); 
}
else {
$input{'next'}=0;	
}	
&header;
$num=0;
if ($btitle eq "y") {
print "<font color=\"$other_color\" face=\"$font_face\"><b>@board[2]</b>";
if (@board[3]==0) {
print " [Private Board]";
}	
print "</font><br><br>\n";	
}	
print "<table border=\"0\" width=\"$table_width\">\n
<tr valign=\"top\"><td></td>
<td bgcolor=\"$title_color\"><font color=\"$title_text\" face=\"$font_face\" size=\"$font_size\"><b>Subject</b>&nbsp;</font></td>\n
<td bgcolor=\"$title_color\"><font color=\"$title_text\" face=\"$font_face\" size=\"$font_size\"><b>Replies</b>&nbsp;</font></td>\n
<td bgcolor=\"$title_color\"><font color=\"$title_text\" face=\"$font_face\" size=\"$font_size\"><b>From</b>&nbsp;</font></td>\n
<td bgcolor=\"$title_color\" width=\"$post_width\"><font color=\"$title_text\" face=\"$font_face\" size=\"$font_size\"><b>Last Post</b>&nbsp;</font></td></tr>\n";
if ($total==0) {
print "<tr><td valign=\"top\"></td><td bgcolor=\"$topic_color\"><img src=\"$default_icon\" border=\"0\"> <font color=\"$topic_text\" face=\"$font_face\" size=\"$font_size\">No messages were posted.</font></td>
<td bgcolor=\"$replies_color\" align=\"center\"><font color=\"$replies_text\" face=\"$font_face\" size=\"$font_size\">-</font></td>\n
<td bgcolor=\"$post_color\" align=\"center\"><font color=\"$post_text\" face=\"$font_face\" size=\"$font_size\">-</font></td><td bgcolor=\"$lastpost_color\" align=\"center\"><font color=\"$lastpost_text\" face=\"$font_face\" size=\"$font_size\">-</font></td></tr>
<tr><td></td><td><font color=\"$other_color\" face=\"$font_face\" size=\"$font_size\">Powered by <a href=\"http://www.cgi-factory.com/\">Message Board V4</a> - &copy; CGI-Factory.com</font></td><td></td><td></td><td></td></tr></table>\n";
&skip;
}
Opening:foreach $order (@order) {
@xorder=split (/x/, "$order");
open (data, "<$messages/@xorder[1].dat") or next Opening;
if ($flock eq "y") {
flock data, 2; 
}
@data=<data>;
close (data);
#time
chomp(@data[2]);
($sec,$min,$hour,$mday,$mon,$year,$wday) = (localtime(@data[2]))[0,1,2,3,4,5,6];
$sec = sprintf("%.02d",$sec);
$min = sprintf("%.02d",$min);
$hour = sprintf("%.02d",$hour);
$mday = sprintf("%.02d",$mday);
$year += 1900;
#date format
if ($date_format==1) {
$date = "$days[$wday], $months[$mon] $mday, $year at $hour:$min:$sec";
}
elsif ($date_format==2) {
$mon++;
$date = "$mday/$mon/$year $hour:$min:$sec";	
}
else {
$mon++;
$date = "$mon/$mday/$year $hour:$min:$sec";	
}		
#
$num++;
if (!@data[4]) {
@data[4]=$default_icon;	
}	
print "<tr valign=\"top\"><td valign=\"top\"></td><td bgcolor=\"$topic_color\"><img src=\"@data[4]\" border=\"0\"> <font face=\"$font_face\" size=\"$font_size\"><a href=\"$messages2/@xorder[1].$file_ending\">@data[0]</a></font></td>
<td bgcolor=\"$replies_color\"><font color=\"$replies_text\" face=\"$font_face\" size=\"$font_size\">@data[1]</font></td>
<td bgcolor=\"$post_color\"><font color=\"$post_text\" face=\"$font_face\" size=\"$font_size\">@data[3]</font></td>
<td bgcolor=\"$lastpost_color\"><font color=\"$lastpost_text\" face=\"$font_face\" size=\"$font_size\">$date</font></td></tr>\n";
if ($num==$max) {
print "<tr><td></td><td><font color=\"$other_color\" face=\"$font_face\" size=\"$font_size\">
Powered by <a href=\"http://www.cgi-factory.com/\">Message Board V4</a> - &copy; CGI-Factory.com</font></td><td></td><td></td><td></td></tr></table>\n";
&links;
if (@board[4]==1) {
&post_box;
}
if ($bench==1) {
$end=(times)[0];
print "<br>It took ", ($end - $start) , " second to complete this job (start: $start end $end)";
}
&footer;
exit;	
}
}
sub skip {
&links;
if (@board[4]==1) {
&post_box;
}
if ($bench==1) {
$end=(times)[0];
print "<br>It took ", ($end - $start) , " second to complete this job (start: $start end $end)";
}
&footer;
exit;	
}
print "<tr><td></td><td><font color=\"$other_color\" face=\"$font_face\" size=\"$font_size\">
Powered by <a href=\"http://www.cgi-factory.com/\">Message Board V4</a> - &copy; CGI-Factory.com</font></td><td></td><td></td><td></td></tr></table>\n";
&links;
if (@board[4]==1) {
&post_box;
}
if ($bench==1) {
$end=(times)[0];
print "<br>It took ", ($end - $start) , " second to complete this job (start: $start end $end)";
}
&footer;
exit;
#post box
sub post_box {
print "<br><br><br>\n";
if ($opticon eq "y") {
open (icon, "<iconbase.dat") or &error("Unable to open iconbase.dat");
if ($flock eq "y") {
flock icon, 2; 
}
@icon=<icon>;
close (icon);
}
print "<font color=\"$other_color\" face=\"$font_face\" size=\"$font_size\">
<b>Post a new message:</b></font><br><br>\n
<table border=\"0\">\n
<form action=\"$cgi\" method=\"post\">\n
<tr><td><font color=\"$other_color\" face=\"$font_face\" size=\"$font_size\">Name:</font></td><td><input type=\"text\" name=\"name\" maxlength=\"20\"></td></tr>\n
<tr><td><font color=\"$other_color\" face=\"$font_face\" size=\"$font_size\">Email:</font></td><td><input type=\"text\" name=\"email\" maxlength=\"50\"></td></tr>\n";
if ($replymail eq "y") {
print "<tr><td></td><td><font color=\"$other_color\" face=\"$font_face\" size=\"$font_size\">Notify me when I get a reply to my message:<input type=\"radio\" name=\"ereply\" value=\"yes\">Yes &nbsp;<input type=\"radio\" name=\"ereply\" value=\"no\" checked>No</font><br><br></td></tr>\n";
}
print "<tr><td><font color=\"$other_color\" face=\"$font_face\" size=\"$font_size\">Subject:</font></td><td><input type=\"text\" name=\"subject\" maxlength=\"50\"></td></tr>\n
<tr><td valign=\"top\"><font color=\"$other_color\" face=\"$font_face\" size=\"$font_size\">Message:</font></td><td valign=\"top\"><textarea name=\"message\" cols=\"50\" rows=\"10\" wrap=\"physical\"></textarea>\n
<input type=\"hidden\" name=\"action\" value=\"preview\"><input type=\"hidden\" name=\"board\" value=\"$input{'board'}\"></td></tr>\n
<tr><td></td><td><font face=\"$font_face\"><input type=\"submit\" value=\"Preview / Post\"></font></form></td></tr>\n
</table>";
}

#####################links
sub links {
print "<font color=\"$other_color\" face=\"$font_face\" size=\"$font_size\">";
print "<br><br>Pages: ";
$link=int($total/$max);
$link2=$total%$max;	
unless ($link2==0) {
$link++;	
}
$next=0;	
$count=1;
for ($count; $count<=$link; $count++) {
if ($input{'next'}==$next) {
print "<b>$count</b> ";
$next=$next+$max;
}
else {
print "<a href=\"$cgi2?board=board1&next=$next\">$count</a> ";
$next=$next+$max;	
}
}
print "</font>";
}
######################header input
sub header {
open (header, "<vheader.txt") or &error("Unable to open the vheader file");
if ($flock eq "y") {
flock header, 2; 
}
@header=<header>;
close(header);
print @header;
}
####################footer input
sub footer {
open (footer, "<vfooter.txt") or &error("Unable to open the vfooter file");
if ($flock eq "y") {
flock footer, 2; 
}
@footer=<footer>;
close(footer);
print @footer;
}
#####error
sub error {
print "Content-type: text/html\n\n";
print "<h2>An error has occured</h2> The error is $_[0]<br>\n";	
print "Reason: $!\n<br><br>";
print "Please contact the webmaster of this web site if you keep getting this message.";	
exit;	
}

