#!/usr/bin/perl
### Best with Perl 5.6 or better
#  Path to Perl - you may need to check your server admin just in case
#  it is different.
##########################################################################
#  Weblog Script Based on:
#  DForum 1.01 (11/02/1999)
#  http://www.dansteinman.com/dforum
#  Copyright (C) 1998 Dan Steinman
#
#  This version by Ted Hawkins, Forum2.6, January 2003
#  http://scriptles.bluwall.com
#   Including
#   1) Footer script by Michael Mathews, August 2002
#      http://perlcircus.com
#
#   2) Latest reply to a subject posting
#      by Jeff Thomas, December 2002
#      http://www.jhthomas.net
#      Jeff redid the Original Dforum1 this way at www.roigi.com
#      and it has been written into Forum2 as of Version 2.5
#   
#   3) Weblog Script by Rio Akasaka, May 2003-February 2004
#      Changed the entire forum script
#      into a user-friendly weblog
#
#      http://www.perlblog.tk
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License (license.txt) for more details.
#  
########################################################################
#require 'weblog-config(original).pl';
require 'weblog-config(modified).pl';

$header = "$forumdir/header.txt";
#$menu = "$forumdir/menu.txt";
$menu = "$forumdir/../../menu.php";

######################### SETTINGS #####################################
$numnths = "2";
# parse form values
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
	local($name, $value) = split(/=/, $pair);
	$value =~ tr/+/ /;
	$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
	$value =~ s/<!--(.|\n)*-->//g;
	$in{$name} = $value;
}
####################### TIMES AND DATES ###############################
$bigwidth = ($width + 43);
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time);
$year = 1900 + $year;
$lastyear = ($year - 1);	                                      #JHT added lastyear
$dec = "12";                                                 # Use for December at year change
$lastmonth = $mon;
$bigwidth = ($width+ 22);
$today= $mday;
$yesterday= ($mday - 1);
$thismonth = ($mon + 1);

# These lines needed for BST(!):
$BSThour = ($hour + 1)%24;
if($BSThour<10) {$BSThour = "0$BSThour"};

if ($lastmonth <10) {$lastmonth = "0$lastmonth";}
if ($thismonth <10) {$thismonth = "0$thismonth";}
if ($mday <10) {$mday = "0$mday";}
if ($hour<10) {$hour = "0$hour";}
if ($min<10) {$min = "0$min";}
$NSTmin = ($min + 45)%60;
if($NSTmin<10) {$NSTmin = "0$NSTmin"};
$NSThour = ($hour + 5 + (($min+45)/60))%24;
if($NSThour<10) {$NSThour = "0$NSThour"};

#$todaysdate = "$thismonth/$mday/$year-$hour:$min";
#-$mday $NSThour:$NSTmin NST $hour:$min GMT"; 
$todaysday = "$mday";
$newheaderfile = "$year$thismonth";
@months = ("January","February","March","April","May","June","July","August","September","October","November","December");

# Toggle commented lines for BST: 
#$todaysdate = "$mday $months[$thismonth-1] $year $hour:$min GMT $NSThour:$NSTmin NST";
$todaysdate = "$mday $months[$thismonth-1] $year $BSThour:$min GMT+1 $NSThour:$NSTmin NST";

########################### QUERIES FOR SUB-ROUTINE ####################
if ($ENV{'QUERY_STRING'} ne '') {
	@qstr = split (/=/,$ENV{'QUERY_STRING'});
	$command = $qstr[0];
	$value = $qstr[1];
	$value =~ s/[\x00-\x20<>\|;\(\)\$^\+!\^\[\]\?\"\'\`]//g;
	
	if ($command eq "archives") {&displayArchives;}
	elsif ($command eq "month") {&displayMonth ($value);}
	elsif ($command eq "about") {&displayAbout ($value);}
	elsif ($command eq "msg") {&displayMessage ($value);}
	elsif ($command eq "$code") {&displaySubmit;}
	elsif ($command eq "reply") {&displayReply ($value);}
	elsif ($command eq "preview") {&displayPreview;}
	else {&displayDefault;}
}
else {
	# if no query string check for a 'post' form value (needed when posting a new thread or reply)
	if ($in{'post'}) {&postMessage;}
	# print the default page
	else {&displayDefault;}
}
########################## DEFAULT HTML PAGE #########################
sub displayDefault {
	&printHeader("");
	print <<"EOF";
EOF
	&printMonthLines ("$year$thismonth"); # this shows the weblog entries for this month                    
   
	print <<"EOF";
<BR><table cellpadding="0" cellspacing="0" border="0" width="$width" align="right"><tr><td><table cellpadding="10" cellspacing="1" border="0" width="$width" class="border"><tr><td colspan="0"><BR><table width="$width" align="center" border="0" cellspacing="0" cellpadding="0"><span class="text">

<!-- ADD A FOOTER SCRIPT TO THE BOTTOM OF YOUR COMMENTS -->
content &copy; 2005, Web site materials are the copyright of the individual contributors and organizations they belong to. Use on this public web site for open access is approved. Other permitted use of the materials is not implied.

<BR><BR></tr></table></td></tr></td></tr></table></table></td></tr></table></td></tr>
</table></td><BR><BR><BR><BR>
EOF
	&printFooter;
}

######################## MY ABOUT PAGE ###################################
sub displayAbout {
	&printHeader("");
	print <<"EOF";
EOF

	print <<"EOF";
<tr><td><table cellpadding="10" cellspacing="1" border="0" width="$width" class="border"><tr><td colspan="0"><BR><table width="$width" align="center" border="0" cellspacing="0" cellpadding="0"><span class="header"><B>About the I-Ex Blog</B> | <A HREF="weblog.pl"><B>Return to Weblog</b></A></span><BR><BR><span class="text">

<!- WRITE YOUR ABOUT PAGE DESCRIPTION HERE -->

This blog was written by Rob during his attempt to climb Everest in spring 2005. Alongside his thoughts and feelings, it records the comments of the many around the world who followed his progress with interest - and who were saddened by his tragic death. 
<!--
Rob Milne aims to climb Everest this spring - and it would also complete his round of the continent tops. 
This Blog will chart his progress.
<p>
His interest in AI technology gives us an opportunity to develop and application of the I-X tools and ideas in the area of expedition support. This is the goal of the <b>I-Ex</b> project.
-->
<p>
Blog powered by <a href="http://www.crazymoose.net/">Perl Blog</a> under the GPL. Thanks.


EOF
	&printFooter;
}

############################ THE ARCHIVES ################################
sub displayArchives {
	open (MONTHS,"$forumdir/months.txt");
	@monthlines = <MONTHS>;
	close (MONTHS);
	
	&printHeader("");
	
	print <<"EOF";
<table cellpadding=10 cellspacing='1' border='0' width='300' align='center' class="border"><tr><td width="0%" vAlign=top colspan="2"><P><BR><span class="header"><B>Archives</B> | <A HREF="weblog.pl"><B>Return to Weblog</b></A></span><TABLE BORDER=0 CELLSPACING=1 CELLPADDING=1><BR>
EOF
	foreach $headerfile (@monthlines) {
		if ($headerfile ne '') {
			$year = substr ($headerfile,0,4);
			$month = substr ($headerfile,4,6);
			$month = sprintf("%5.0f", $month);
			$monthname = $months[$month-1];
  	print <<"EOF";
<TR BGCOLOR="$jcolor"><TD><span class="text"><A CLASS="items" HREF="weblog.pl?month=$headerfile">$monthname&nbsp;&nbsp;$year</span></a></TD></TR>
EOF
		}
	}
	
	print <<"EOF";
	</TABLE>
	
EOF

# original line from above:
#<TR BGCOLOR="$jcolor"><TD><span class="text"><A CLASS="items" HREF="weblog.pl?month=$headerfile">$monthname</span></a> &nbsp;&nbsp;</TD><TD><span class="text"><A CLASS="items" HREF="weblog.pl?month=$headerfile">$year</span></TD></a></TR>
	&printFooter;
}

######################### SPECIFIC MONTH UNDER ARCHIVES ######################
sub displayMonth {
	my($headerfile) = @_;
	$year = substr ($headerfile,0,4);
	$month = substr ($headerfile,4,6);
	$month = sprintf("%5.0f", $month);
	$monthname = $months[$month-1];

	&printHeader("");

	print <<"EOF";
<table cellpadding=10 cellspacing='1' border='0' width='$bigwidth' align='center' class="border"><tr><td width="0%" vAlign=top colspan="2"><span class="header"><B>Archives for $monthname $year</B> | <A HREF="weblog.pl"><B>Return to Weblog</b></A></span></tr></table></td></tr></td></tr></table><BR><BR><table cellpadding="0" cellspacing="0" border="0" width="$width" align="center">
EOF
 &printMonthLines ($headerfile);

	print <<"EOF";
	</TABLE>


EOF
	&printFooter;
}

###################### SHOWS YOUR ENTRIES (GENERAL SETTINGS) #####################
sub printMonthLines {
	my($filename) = @_;
	if (open(DATA,"$forumdir/$filename.txt")) {
		@msglines = <DATA>;
		close(DATA);
		foreach $msgline (@msglines) {
			if ($msgline ne '') {
			    # this should tally with postThread:
				@info = split (/_/, $msgline);
				$num = $info[0];
				$subject = $info[1];
				$name = $info[2];
				$date = $info[3];
				$body = $info[4];
				$responses = ($info[5]);
				#$date = $info[1];
				#$body = $info[2];   
                                #$subject = $info[3];  
                                #$postm = $info[4]; 
				#$sub = $info[5];
				#$responses = ($info[6]);
			      
				my $commentstr = "comments";
				if($responses == 1) {$commentstr = "comment";}
			$year = substr ($headerfile,0,4);
			$month = substr ($headerfile,4,6);
			$month = sprintf("%5.0f", $month);
			$monthname = $months[$month-1]; 
	      print <<"EOF";
<tr>
  <td>
    <table cellpadding="10" cellspacing="1" border="0" width="$width" class="border">
       <tr>
         <td colspan="0"><br>
           <table width="$width" border="0" cellspacing="1" cellpadding="0">

<span class="jheader"><b>$subject</b></span><br><br><b>


<span class="text">Posted by $name<br>$date</b></span>


<BR><BR><span class="text">$body<br><br>

<a href="?reply=$num" title="Post a comment">$responses $commentstr</a><br></span><br>
           </table>
         </td>
       </tr>
     </table><br><br>
EOF
			}
		}
	}
}

########################### COMMENT BOX/REPLY PAGE SETTINGS ##########################
sub parseMessageData {
	my($filename) = @_;
	my($msgstart, $msgend, @msglines);
	open(DATA,"$forumdir/messages/$filename.txt");
	@msglines = <DATA>;
	close(DATA);
	for ($i=0; $i<@msglines; $i++) {
		if ($msglines[$i] =~ /<msg>/) {push (@msgstart, $i);}
		elsif ($msglines[$i] =~ /<\/msg>/) {push (@msgend, $i);}
	}
	for ($i=0; $i<@msgstart; $i++) {
		for ($j=$msgstart[$i]; $j<$msgend[$i]; $j++) {
			if ($msglines[$j] =~ /<subject>(.*)<\/subject>/) {
				$subject = $1;
				if ($subject) {print "<p><b>Subject: $subject</b><p>";}
			}
			elsif ($msglines[$j] =~ /<name>(.*)<\/name>/) {
				$name = $1;
				if ($i != 0) {
					print "<tr><td><table cellpadding=\"10\" cellspacing=\"1\" border=\"0\" width=\"$width\" class=\"border\"><tr><td colspan=\"0\"><BR><table width=\"$width\" align=\"center\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><span class=\"header\"><B>Comment \#$i</B></span><span class=\"text\"><BR><B>Author:</B> $name\n";
				}
				else {print "<b>Posted by $name </b>";}
				print "";
			}

			elsif ($msglines[$j] =~ /<email>(.*)<\/email>/) {
				$email = $1;
				if ($email) {print " (<A HREF=\"mailto:$email\">$email</A>) ";}
			}
  	elsif ($msglines[$j] =~ /<website>(.*)<\/website>/) {
				$website = $1;
				if ($website) {print " (#$website) ";}
			}
			elsif ($msglines[$j] =~ /<date>(.*)<\/date>/) {
				$date = $1;
				print "<b>on $date</b><p>\n";
			}
   			elsif ($msglines[$j] =~ /<body>/) {
				print "<BR>\n";
  		for ($k=$j+1; $k<@msglines; $k++) {
					if ($msglines[$k] =~ /<\/body>/) {
						print "<BR><BR></td>
<BR></tr></table></td></tr></td></tr></table><BR><BR>";
						last;
					}
					else {print "$msglines[$k]";}
					$j++;
				}
			}
		}
	}
}

################################## YOUR JOURNAL ENTRY PAGE ####################
sub displaySubmit {
	&printHeader("");
	print <<"EOF";
<tr><td><table cellpadding="10" cellspacing="1" border="0" width="$width" class="border"><tr><td colspan="0"><BR><table width="$width" align="center" border="0" cellspacing="0" cellpadding="0">
<span class="header"><B>Submit your comment</B> | <A HREF="weblog.pl"><B>Return to Weblog</b></A></span><BR><BR><BR>
<FORM NAME="forum" ACTION="access.pl?preview" METHOD="POST">
<INPUT TYPE="HIDDEN" NAME="date" VALUE="$todaysdate">
<INPUT TYPE="HIDDEN" NAME="headerfile" VALUE="$newheaderfile">
<INPUT TYPE="HIDDEN" NAME="postm" VALUE="">
<INPUT TYPE="HIDDEN" NAME="image" VALUE="">

<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0>
<tr><td><span class="text"><b>Username:</b></span></td>
    <td><input type="text" class="txt" name="username" size="25" maxlength="225"></td>
</tr>
<tr><td><span class="text"><b>Password:</b></span></td>
    <td><input type="password" class="txt" name="password" size="25" maxlength="225"></td>
</tr>

<tr><td><span class="text"><b>Display Name:</b></span></td>
    <td><input type="text" class="txt" name="name" size="25" maxlength="225"></td>
</tr>


<tr><td><span class="text"><b>Subject:</b></span></td>
    <td><input type="text" class="txt" name="subject" size="25" maxlength="225"></td>
</tr>
<!--
<tr><td><span class="text"><b>Link:</b></span></td>
    <td><input type="text" class="txt" name="postm" size="25" maxlength="225"></td>
</tr>
-->
<tr><td colspan="2"><p><span class="text"><br><b>Entry:</b></span></td></tr>
<tr><td colspan="2"><textarea style="overflow:hidden" class="entrytext" background="#E8E9EC" cols="54" rows="20" name="body" wrap="physical"></textarea></TD></TR>
<TR><TD COLSPAN=2><BR><BR><INPUT TYPE="Submit" class='submit' VALUE="Post Entry">
</TD></TR>
</FORM>
</TABLE>
EOF
	&printFooter;
}

################################ VISITOR COMMENTS PAGE ###################
sub displayReply {
	my($replynum) = @_;
	open(DATA,"$forumdir/messages/$replynum.txt");
	@msglines = <DATA>;
	close(DATA);
	for ($i=0; $i<@msglines; $i++) {
		if ($msglines[$i] =~ /<headerfile>(.*)<\/headerfile>/) {$headerfile = $1;}
		elsif ($msglines[$i] =~ /<subject>(.*)<\/subject>/) {$subject = $1;}
	}


	&printHeader("");
	print <<"EOF";
<tr><td><table cellpadding="10" cellspacing="1" border="0" width="$width" class="border"><tr><td colspan="0"><BR><table width="$width" align="center" border="0" cellspacing="0" cellpadding="0"><span class="header"><B>Post a comment</B> | <A HREF="weblog.pl"><B>Return to Weblog</b></A></span>
<FORM NAME="forum" ACTION="?preview" METHOD="POST">
<INPUT TYPE="HIDDEN" NAME="date" VALUE="$todaysdate">
<INPUT TYPE="HIDDEN" NAME="headerfile" VALUE="$headerfile">
<INPUT TYPE="HIDDEN" NAME="subject" VALUE="Re: $subject">
<INPUT TYPE="HIDDEN" NAME="reply" VALUE="$replynum"><span class="text">

EOF
	&parseMessageData ($replynum);

	print <<"EOF";

<!--tr><td><table cellpadding="10" cellspacing="1" border="0" width="$width" class="border"><tr><td colspan="0"><BR><table width="$width" align="center" border="0" cellspacing="0" cellpadding="0">
<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0>
<TR><td><span class="text"><b>Name:</b></span></TD><TD><INPUT TYPE="TEXT" class="txt" NAME="name" SIZE="25" MAXLENGTH="25"></TD></TR>
<TR><TD><span class="text"><b>Email:</b></span></TD><TD><INPUT TYPE="TEXT" class="txt" NAME="email" SIZE=25></TD></TR><BR>
<TR><TD COLSPAN="2"><P><span class="text"><BR><b>Message:</b></span></TD></TR>
<TR><TD COLSPAN=2><TEXTAREA STYLE="overflow:hidden" class="entrytext" background="#E8E9EC" COLS=50 ROWS=9 NAME="body" WRAP="Physical"></TEXTAREA></TD></TR>
<TR><TD COLSPAN=2><BR><BR><INPUT TYPE="Submit" class='submit' VALUE="Post Comment">
<TD></TR>
</FORM>
</TABLE>
</tr></table></td></td></tr></table><BR><BR-->
EOF

             }
	&printFooter;

############################ PREVIEW PAGE FOR VISITORS #############################
sub displayPreview {
	$name = $in{'name'};
	$name =~ s/</&lt;/g;
	$name =~ s/>/&gt;/g;
	$name =~ s/"/&quot;/g;
	$image = $in{'image'};
	$image =~ s/</&lt;/g;
	$image =~ s/>/&gt;/g;
	$image =~ s/"/&quot;/g;
	$postm= $in{'postm'};
	$postm =~ s/</&lt;/g;
	$postm =~ s/>/&gt;/g;
	$postm =~ s/"/&quot;/g;
	$email = $in{'email'};
	$email =~ s/<([^>]|\n)*>//g;
 $website = $in{'website'};
 $website =~ s/<([^>]|\n)*>//g;
	$date= $in{'date'};
	$headerfile = $in{'headerfile'};
	$subject = $in{'subject'};
	$reply = $in{'reply'};
 $body = &wrapText($in{'body'},$textwidth);
	$body =~ s/\cM//g;
	$bodyshow = $body;
	#$body =~ s/</&lt;/g;
	#$body =~ s/>/&gt;/g;
	#$body =~ s/"/&quot;/g;
	$body =~ s/"//g;

	if ($html eq "no") {
		$bodyshow =~ s/<([^>]|\n)*>//g;
	}
	if ($html eq "yes") {
		$bodyshow =~ s/</&lt;/g;
		$bodyshow =~ s/>/&gt;/g;
		$bodyshow =~ s/"/&quot;/g;
	}

	$subject =~ s/\cM//g;
	$subject =~ s/<([^>]|\n)*>//g;
	$subject =~ s/"/&quot;/g;
	$subject =~ s/_/ /g;

	&printHeader("");
	if ($name) {
		print "\n";
		if ($email) {
			print "\n";

  if ($website) {
			print " (#$website)\n";
		}
		}

		print <<"EOF";
<table cellpadding="10" cellspacing="1" border="0" width="50%" align="center" class="border">
  <tr>
    <td width="0%" valign="top" colspan="2"><br><span class="header"><b>Preview</b> | <a href="weblog.pl" title="Return to my weblog"><b>Return to Weblog</b></a></span><br><br>
<br><span class="text">Name: $name
<br>Date: $date
<br>Comment: $bodyshow
<form name="forum" method=post action="weblog.pl">
<input type="hidden" name="date" value="$date">
<input type="hidden" name="headerfile" value="$headerfile">
<input type="hidden" name="name" value="$name">
<input type="hidden" name="image" value="$image">
<input type="hidden" name="email" value="$email">
<input type="hidden" name="website" value="$website">
<input type="hidden" name="subject" value="$subject">
<input type="hidden" name="body" value="$body">
<p><br>
<input class="submit" type="button" value="Edit" onClick="history.back()">
<input class="submit" type="submit" value="Post Comment">
EOF
		if ($reply) {
			print "<input type=\"Hidden\" name=\"post\" value=\"reply\">\n";
			print "<input type=\"Hidden\" name=\"reply\" value=\"$reply\">\n";
		}
		else {
			print "<input type=\"Hidden\" name=\"post\" value=\"thread\">\n";
		}
		print "</form></td></tr></table>\n";
	}
	else {
		print "
<table cellpadding=\"10\" cellspacing=\"1\" border=\"0\" width=\"50%\" align=\"center\" class=\"border\">
  <tr>
   <td width=\"0%\" valign=\"top\" colspan=\"2\">
<br><span class=\"header\"><b>Error</b> | <a href=\"weblog.pl\"><b>Return to Weblog</b></a></span><br><br><br><span class=\"text\">Error: You are required to give a name, an email address and a message</span><p><br><input class=\"submit\" type=\"Button\" value=\"Edit\" onClick=\"history.back()\"><BR><BR></td></tr></table>";
             }
	&printFooter;
}

############################## wrap text ##########################
sub wrapText {
	my($body,$length) = @_;
	@lines = split(/\n/,$body);
	$newstr = "";
	for ($i=0; $i<@lines; $i++) {
		@words = split(/ /,@lines[$i]);
		$block = $words[0];
		for ($j=1; $j<@words; $j++) {
			$word = $words[$j];
			$test = $block." ".$word;
			if (split(//,$test) < $length+1) {
				$block = $block." ".$word;
			}
			else {
				$newstr = $newstr.$block."\n";
				$block = $word;
			}
		}
		$newstr = $newstr.$block."\n";
	}
	return ($newstr);
}

###################### DETERMINES NEW ENTRY OR REPLY ############################
sub postMessage {
	$name = $in{'name'};
	$postm = $in{'postm'};
        $image = $in{'image'};
	$email = $in{'email'};
	$website = $in{'website'};
	$date = $in{'date'};
	$headerfile = $in{'headerfile'};
	$reply = $in{'reply'};
		
	$body = $in{'body'};
	$body =~ s/\cM//g;
	$body =~ s/"//g;
	if ($html eq "no") {
		$body =~ s/<([^>]|\n)*>//g;
	}
	if ($html eq "yes") {
		#$body =~ s/</&lt;/g;
		#$body =~ s/>/&gt;/g;
		#$body =~ s/"/&quot;/g;
	}
	
	$subject = $in{'subject'};
	$subject =~ s/\cM//g;
	$subject =~ s/"/&quot;/g;
	$subject =~ s/_/ /g;
	if ($in{'post'} eq "thread") {&postThread;}
	elsif ($in{'post'} eq "reply") {&postReply;}

	&printHeader ("");
	if ($reply) {$message = $reply;}
	else {$message = $num;}
	print <<"EOF";
<table cellpadding="10" cellspacing="1" border="0" width="50%" align="center" class="border" summary="Your comment has been successfully posted">
 <tr>
    <td width="0%" valign="top" colspan="2"><br><span class="header"><b>Thank you</b></span><span class="header"> | </span><span class="header"><a href="weblog.pl"><b>Return to Weblog</b></span></a>
<br><br><br><span class="text">Your comment has been posted!<br><br><a href="weblog.pl" title="Return to my weblog">Return to weblog</a><br><br></span>
</td></tr></table>
EOF
	&printFooter;
}

######################### postS NEW ENTRY ########################
sub postThread {
	if (!opendir (DIR, "$forumdir")) {
		mkdir ("$forumdir", 0700);
	}
	open(NUM,"$forumdir/num.txt");
	$num = <NUM>;
	$num += 1;
	close(NUM);
	open(NUM,">$forumdir/num.txt");
	print NUM $num;
	close(NUM);
	
	open(MONTHS,"$forumdir/months.txt");
	@months = <MONTHS>;
	close(MONTHS);
	$addit = "true";
	foreach $month (@months) {
		if ($month =~ /$headerfile/) {
			$addit = "false";
		}
	}

	# sep added:
	$previousheaderfile = "";
	if ($addit eq "true") {
		open(MONTHS,"$forumdir/months.txt");
		@monthlines = <MONTHS>;
		close(MONTHS);

		# sep added - last month should be the first entry in the array:
		$previousheaderfile = substr($monthlines[0],0,6);
		open(MONTHS,">$forumdir/months.txt");
		unshift (@monthlines, "$headerfile\n");
		foreach $monthline (@monthlines) {
			print MONTHS $monthline;
		}
		close(MONTHS);
	}
	
	# sep added:
	if(open(TESTHEADER,"$forumdir/$headerfile.txt")){
	    # file exists...
	}
	else{
	    # file does not exist; copy last month to this:
	    system "cp $forumdir/$previousheaderfile.txt $forumdir/$headerfile.txt";
	}
	close(TESTHEADER);

	open(HEADER,"$forumdir/$headerfile.txt");
	@headers = <HEADER>;
	close(HEADER);
	open(HEADER,">$forumdir/$headerfile.txt");
	#unshift (@headers, "$num\_$subject\_$name\_$image\_$postm\_$date\_0\_&nbsp;\n");  # JHT modified: last field - &nbsp;
	unshift (@headers, "$num\_$subject\_$name\_$date\_$body\_0\_&nbsp;\n");  # JHT modified: last field - &nbsp;

	foreach $headerline (@headers) {
		print HEADER $headerline;
	}
	close(HEADER);
	if (!opendir (DIR, "$forumdir/messages")) {
		mkdir ("$forumdir/messages", 0777);
	}
	open(NEWTHREAD,">$forumdir/messages/$num.txt");
	print NEWTHREAD <<"EOF";
<threadinfo>
<num>$num</num>
<headerfile>$headerfile</headerfile>
<subject>$subject</subject>
</threadinfo>

<msg>
<subject>$subject</subject>
<name>$name</name>
<email>$email</email>
<website>$website</website>
<image>$image</image>
<date>$date</date>

<body>
$body
</body>
</msg>
EOF
	close(NEWTHREAD);
}

########################## postS NEW REPLY ############################
sub postReply {
	open(HEADER,"$forumdir/$headerfile.txt");
	@headers = <HEADER>;
	close(HEADER);
	open(HEADER,">$forumdir/$headerfile.txt");
	foreach $headerline (@headers) {
		@headerdata = split(/_/, $headerline);
		$threadnum = $headerdata[0];
		if ($threadnum == $reply) {
		    # should tally with postThread:
			@headerdata = split(/_/, $headerline);
			#$responses = $headerdata[6];
			$responses = $headerdata[5];
			$responses += 1;
			$replytime = $date;    # replytime is JHT addition
			#print HEADER "$headerdata[0]\_$headerdata[1]\_$headerdata[2]\_$headerdata[3]\_$headerdata[4]\_$headerdata[5]\_$responses\_&nbsp;\n";
			print HEADER "$headerdata[0]\_$headerdata[1]\_$headerdata[2]\_$headerdata[3]\_$headerdata[4]\_$responses\_&nbsp;\n";

		}
		else {print HEADER "$headerline";}
	}
	close(HEADER);

	open(REPLY,">>$forumdir/messages/$reply.txt");
	print REPLY <<"EOF";

<msg>
<name>$name</name>
<email>$email</email>
<website>$website</website>
<date>$date</date>
<body>
$body
</body>
</msg>
EOF
	close(REPLY);
}

########################### HEADER SCRIPT FOR ALL PAGES ############################
sub printHeader {
	my($title) = @_;
print "Content-type: text/html\n\n";
	print <<"EOF";
<HTML>
<HEAD>
<TITLE>$blogtitle</TITLE>

<link href="./iexblog.css" rel="stylesheet" type="text/css">
<link rel="SHORTCUT ICON" href="./inc/ix-web.ico">

</HEAD>
<!-- STYLE OF YOUR WEBLOG DO NOT EDIT -->
<!--
<style>
body { background-color:$bgcolor }

html { scrollbar-face-color: $scrollcolor; scrollbar-highlight-color: $scrollcolor; scrollbar-shadow-color: $scrollcolor; scrollbar-3dlight-color: $scrollborder; scrollbar-arrow-color: $scrollcolor; scrollbar-track-color: $scrolltrack; scrollbar-darkshadow-color: $scrollborder; }
TABLE, TR, TD { font-family: $font; font-size: 8.5pt; color: $txtcolor}
table.border { background-color: $jcolor; border-top: $bordercolor 1px solid; border-left: $bordercolor 1px solid; border-right: $bordercolor 1px solid; border-bottom: $bordercolor 1px solid }
table.border2 { background-color: $headercolor; border-top: $topborder 1px solid; border-left: $topborder 1px solid; border-right: $topborder 1px solid; border-bottom: $topborder 1px solid}
.header { color: $nheader; font-size: $headersize }
.header  a:link, .header  a:visited, .header  a:active { text-decoration: none; color: $lheader }
.header a:hover { font-family: font-family: $hovefon; text-decoration: none; color: $hovheader; font-size: $hhoversize}
.jheader { color: $headertxt; font-size: $jsize; font-family: $jfont; }
.jheader  a:link, .header  a:visited, .header  a:active { text-decoration: none; color: $jlink }
.jheader a:hover { text-decoration: none }
.text { font-family: $txtfont; color: $txtcolor; font-size: $txtsize;}
.text  a:link, .text  a:visited, .text  a:active { text-decoration: none; color: $linkcolor }
.text  a:hover { text-decoration: $txtstyle; font-weight: $hoverweight;}
.title { font-family: $titlefont; color: $titlecolor; color; font-size: $titlesize}
.title  a:link, .title  a:visited, .title  a:active { text-decoration: none; color: $titlecolor }
.title  a:hover { text-decoration: none;}
.small { font-family: $smallfont; color: $smallcolor; font-size: $smallsize}
.small  a:link, .small  a:visited, .small a:active { text-decoration: none; color: $smallcolor }
.small  a:hover { color: $smallhover; font-size: $smallhsize; font-family: $smallhfont; text-decoration: $smallstyle; font-weight: $smallweight; }
input.txt {          background-color: $texttbg; border: 1px $input solid; padding: 1px; font: 11px Verdana; color: $inputxt }
textarea.entrytext { background-color: $texttbg; border: 1px $input solid; padding: 1px; font: 11px Courier; color: $inputxt }
input.submit { font-family: $buttonfam; font-size: $buttonsize; background-color: $submitbg; color: $button; border-top: $input 1px solid; border-left: $input 1px solid; border-right: $input 1px solid; border-bottom: $input 1px solid }

#navbar{
	float: right;
	width:180px;
	padding: 0px;
	background-color: #C5C6FF;
	border-left: 1px solid #cccccc;
	border-right: 1px solid #cccccc;
	border-bottom: 1px solid #cccccc;
	border-top: 1px solid #cccccc;
	font: x-small Verdana,sans-serif;
}

#disclaimer{

	padding: 20px 0px 3px 10px;
	font: xx-small Verdana,sans-serif;
}


#navitems{
	margin: 0px;
	padding: 0px;

}

#navitems h3{
	padding: 10px 0px 2px 10px;
	border-bottom: 1px solid #cccccc;
}

#navitems a:link, #navitems a:visited {
	display: block;
	border-top: 1px solid #ffffff;
	border-bottom: 1px solid #cccccc;
	font-weight: bold;
	padding: 3px 0px 3px 10px;
	color: #000099;
}

#navitems a:hover{
	border-top: 1px solid #cccccc;
	background-color: #DDEEFF;
	background-image: none;
	font-weight: bold;
	text-decoration: none;
}


</style>
-->
<!--
<noscript><BODY bgcolor="$bgcolor"></noscript>
-->
<BODY bgcolor=$bgcolor background="img/Everestbg.gif">

EOF
# Call header file
	open MENU, "$menu"
         or die "Can't open menu file because $!\n";
     print while (<MENU>);
	close MENU;

print << "EOF";

<table cellpadding=0 cellspacing='1' border='0' align='center'><tr><td width="0%" vAlign=top><P><BR><table border="0" cellspacing="0" cellpadding="0" align="center" width="$width" height="45"><tr><td><table cellpadding="10" cellspacing="0" border="0" width="$width" class="border2"><tr><td colspan="0"><BR><table width="$width" align="center" border="0" cellspacing="0" cellpadding="0">
<span class="btitle"><B>


<!-- TITLE OF YOUR BLOG -->
<a href="weblog.pl">$blogtitle</a></b><BR></span>
<span class="small">
<!-- LINKS TO YOUR PAGES -->
                    [ <!--a href="weblog.pl">$subtitle</a-->  
                      <a href="?archives">archives</a> | 
                      <a href="?about">about</a>  ]

</span>

<BR><BR></tr></table></td></tr></td></tr></table><BR><BR>

EOF
# Call header file
	open HEADER, "$header"
         or die "Can't open header file because $!\n";
     print while (<HEADER>);
	close HEADER;

}
######################## FOOTER SCRIPT FOR ALL PAGES ########################
 sub printFooter {
	open FOOTER, "$forumdir/footer.txt"
         or die "Can't open footer file because $!\n";
     print while (<FOOTER>);
	close FOOTER;
}
	print "\n<noscript></body></html>\n\n";

