#!/usr/bin/perl

##########################################################################
# COPYRIGHT NOTICE:
# 
# Copyright FocalMedia.Net 2001
# 
# This program is free to use for commercial and non commercial use. 
# This script may be used and modified by anyone, so long as this copyright 
# notice and the header above remain intact. Selling the code for this 
# program without prior written consent is expressly forbidden.
# 
# This program is distributed "as is" and without warranty of any
# kind, either express or implied.
#
##########################################################################

&get_env;
&get_setup;

$cgidir = "/cgi/guestbook.cgi";


print "Content-type: text/html\n\n";
$datafile = "bothdata.txt";
$template = "shaanguestbook.shtml";
if ($fields{'code'} eq "shaan")
	{$datafile = "shaandata.txt";
	$template = "shaanguestbook.shtml";}
if ($fields{'code'} eq "sophie")
	{$datafile = "sophiedata.txt";
		$template = "sophieguestbook.shtml";}
	
	
if ($fields{'fct'} eq "post"){&post_operation;}
if ($fields{'fct'} eq ""){&display_guestbook;}


exit;

############################################################################################

sub display_guestbook
{

$fsize1 = (-s $template);
open (RVF, $template);
	read(RVF,$ml,$fsize1);
close (RVF);


if ($imagep eq "Y"){$ml = &InsertImages($ml);}
$ml =~ s/!!cgiscript!!/$cgidir/g;


$fsize1 = (-s "listings.html");
open (RVF, "listings.html");
	read(RVF,$listformat,$fsize1);
close (RVF);


open (DT, $datafile);
 while (defined($line=<DT>))
	{
	if (length($line) > 4)
		{
		($name, $msg, $email, $date) =split(/::-::/,$line);	
	
		$listings = $listformat;
		
	#	if ($email ne "") {$name = "<a href=\"mailto:$email\">$name</a>";}
		
		$listings =~ s/!!from!!/$name/g;
		$listings =~ s/!!date!!/$date/g;
		$listings =~ s/!!comment!!/$msg/g;
		
		$gentries = $gentries . $listings;
		}
	}
close (DT);

$listformat = &strip_html_body_tags ($listformat);

$ml =~ s/!!guest_book_entries!!/$gentries/g;

print "$ml";

}


sub post_operation
{
if (($fields{'code'} ne "shaan") &&($fields{'code'} ne "sophie")) { &post_prb ("You have to enter the code to post."); }
if ($fields{'name'} eq "") { &post_prb ("You have to supply a name to post."); }
if ($fields{'comment'} eq "") { &post_prb ("You have to supply a comment to post."); }

$fields{'comment'} =~ s/\n/<br>/g;

$fsize1 = (-s $datafile);
open (RVF, datafile);
	read(RVF,$tdata,$fsize1);
close (RVF);


($sec,$min,$hour,$mday,$mon,$year,$wday,$ydat,$isdst) = localtime();
$year = "20" . substr($year, 1, 2);
$mon = &decode_month($mon);
$wday = &decode_weekday($wday);
$today = "$wday $mday $mon $year";

$fields{'comment'} =~ s/\n//g;
$crit = chr(10);
$fields{'comment'} =~ s/$crit//g;
$crit = chr(13);
$fields{'comment'} =~ s/$crit//g;

open (DT, '>> '.$datafile);
	print DT $fields{'name'} . "::-::" . $fields{'comment'} . "::-::" . $fields{'email'} . "::-::" . $today . "\n" . $tdata;
close (DT);


&display_guestbook;

exit;

}




sub post_prb
{

my ($problem) = @_;

$fsize1 = (-s "problem.html");
open (RVF, "$html_loc/problem.html");
	read(RVF,$ml,$fsize1);
close (RVF);

if ($imagep eq "Y"){$ml = &InsertImages($ml);}
$ml =~ s/!!problem!!/$problem/g;

print "$ml";
exit;

}




sub get_env
{

if ($ENV{'QUERY_STRING'} ne "") {
								$temp = $ENV{'QUERY_STRING'};
								}
								else
								{
								read(STDIN, $temp, $ENV{'CONTENT_LENGTH'});
								}

@pairs=split(/&/,$temp);

foreach $item(@pairs) 
	{
	($key,$content)=split (/=/,$item,2);
	$content=~tr/+/ /;
	$content=~ s/%(..)/pack("c",hex($1))/ge;
	$fields{$key}=$content;
	}

}






sub get_setup
{
#### GET CONFIGURATION ########################################################

$setupcfg = "setup.cfg";

$exists = (-e "$setupcfg");
if ($exists > 0)
	{

	open (STP, "$setupcfg");
		while (defined($line=<STP>))
			{
			if ($line =~ m/#/g)
				{
				$r = pos($line);
				$line = substr($line, 0, $r - 1);
				}
				
				$line =~ s/\n//g;
			
if ($line =~ /CGIURL/){$line =~ s/CGIURL//g; $line =~ s/ //g; $cgidir = $line;}
if ($line =~ /PASSWORD/){$line =~ s/PASSWORD//g; $line =~ s/ //g; $password = $line;}
if ($line =~ /USERNAME/){$line =~ s/USERNAME//g; $line =~ s/ //g; $username = $line;}
if ($line =~ /HTML_LOC/){$line =~ s/HTML_LOC//g; $line =~ s/ //g; $html_loc = $line;}
if ($line =~ /HTML_URL/){$line =~ s/HTML_URL//g; $line =~ s/ //g; $html_url = $line;}
if ($line =~ /IMAGEP/){$line =~ s/IMAGEP//g; $line =~ s/ //g; $imagep = $line;}

#chdir("$scripts_loc");
			
			}
	close (STP);
	
	}
	else
	{
	print "Content-type: text/html\n\n";
	print "Could not find setup.cfg";
	exit;
	}

#### END CONFIGURATION ########################################################
}





sub InsertImages
{

my ($html) = @_;

if ($html =~ /<img/i)
{

$html =~ s/<img/:-:image::_::-:tag:-:/gi;

@lines = split(/::-:tag:-:/,$html);

foreach $item (@lines)
  {
  
  if (($item =~ m/\.gif/gi) or ($item =~ m/\.jpg/gi))
     {
		$item = &ProcessLine ($item);
		$newline = $newline . $item;
	 }
	 else
	 {
	 	$newline = $newline . $item;
	 }

  }

$newline =~ s/:-:image::_/<img/g;
}

else
{
$newline = $html;
}

return ($newline);

} ### END SUB



sub ProcessLine
{

my ($aline) = @_;

my ($iname, $r, $acn, $orgp, $ichar, $geturl, $sb);

	if (($aline =~ m/\.gif/gi) or ($aline =~ m/\.jpg/gi))
	 {
	 $r = pos($aline);
	 $orgp = $r;
	 $r = $r -1;
	 }

	while ($r >= 0)
	{
	$ichar = substr($aline, $r, 1);
	$r = $r - 1;
	

	if (($ichar eq "\\") or ($ichar eq "/"))
		{
		$sb = "true";
		}


	if (($ichar eq "=") or ($ichar eq "\""))
		{
		if ($geturl ne "true")
			{
			$sb = "";
			$iname = "$html_url/" . $iname;
			$geturl = "true";
			}
		}


	if ($sb ne "true")
		{
		$iname = $ichar . $iname;
		}
	}

$iname = $iname . substr($aline, $orgp, length($aline) - $orgp);

#print "==> $iname <br>";
return ($iname);

}





sub strip_html_body_tags
{

my ($thehtml) = @_;

$thestripped = "";

@html_lines=split(/\n/,$thehtml);

foreach $item (@html_lines)
	{

	if (($item !~ /<html>/) and ($item !~ /<head>/) and ($item !~ /<meta name/) and ($item !~ /<body>/) and ($item !~ /<title>/) and 
	   ($item !~ /<\/html>/) and ($item !~ /<\/head>/) and ($item !~ /<\/body>/))
	   {
		$thestripped = $thestripped . "$item" . "\n";
	   }

	}

return ($thestripped);

}




sub decode_month
{

my ($themonth) = @_;

if ($themonth == 0) {$themonth = "January";}
if ($themonth == 1) {$themonth = "February";}
if ($themonth == 2) {$themonth = "March";}
if ($themonth == 3) {$themonth = "April";}
if ($themonth == 4) {$themonth = "May";}
if ($themonth == 5) {$themonth = "June";}
if ($themonth == 6) {$themonth = "July";}
if ($themonth == 7) {$themonth = "August";}
if ($themonth == 8) {$themonth = "September";}
if ($themonth == 9) {$themonth = "October";}
if ($themonth == 10) {$themonth = "November";}
if ($themonth == 11) {$themonth = "December";}

return ($themonth);

}



sub decode_weekday
{

my ($theweekday) = @_;

if ($theweekday == 0) {$theweekday = "Sunday";}
if ($theweekday == 1) {$theweekday = "Monday";}
if ($theweekday == 2) {$theweekday = "Tuesday";}
if ($theweekday == 3) {$theweekday = "Wednesday";}
if ($theweekday == 4) {$theweekday = "Thursday";}
if ($theweekday == 5) {$theweekday = "Friday";}
if ($theweekday == 6) {$theweekday = "Saturday";}

return ($theweekday);

}
