#!/usr/local/bin/perl
############################################################################
#
#                            TEKBOOK.CGI V2.0
#
#                            By Metertek 1999
#                           metertek@yahoo.com
#
#           Call script from a HTML page using a similar tag
#
# <a href="http://your.domain.com/yourdir/cgi-bin/tekbook.cgi">Guestbook</a> 
#
#   To call script Admin function enter a similar tag into your browser :
#
#         http://your.domain.com/yourdir/cgi-bin/tekbook.cgi?admin
#
############################################################################
#                                variables

$guestfile = 'http://dnipro.ukrainiansoccer.net/cgi-bin/tekbook.txt';
$tablebackground = 'background="http://your.domain.com/yourdir/image.gif"';
$background = 'bgcolor="#8080FF"';
$textcolor = '"#000080"';
$bookname = 'Гостевая Книга болельщиков МЮ';
$adminpass = 'guestpass';
$homeurl = 'http://eurofootball.com.ru/mu/';
$mailoff = '0';
$mailprog = '/usr/lib/sendmail -t';
$emailfrom = 'maxdnepr@mail.ru';
$mailcontent = 'Thank you for visiting my website';
$addsecs ='0';

#
############################################################################
#                       DO NOT ALTER PERL BELOW HERE

$domain = 'http://'.$ENV{'SERVER_NAME'};
$path = $ENV{'DOCUMENT_ROOT'};
$referrer = $ENV{'HTTP_REFERER'};
$cgiurl = $domain.$ENV{'SCRIPT_NAME'};
$home = 'http://members.xoom.com/metertek/archive/';
$ip = $ENV{'REMOTE_ADDR'}; 
$pagemark = 'guestentryhere';

&date;
&read;
if ($INPUT{'REMOVE'}) { &remove; &admin;}
if ($INPUT{'SENDPASS'}) { &admin; }
if ($INPUT{'SIGN'}) { &create; }
if ($INPUT{'ADD'}) { &editbook; &mail;}
&showbook;
exit;

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

sub read {

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

@pairs = split(/&/, $namevalues);
	foreach $pair (@pairs) {
	($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;
        if ($name eq 'admin') { &adminpass; }
        $value =~ s/<([^>]|\n)*>//g;
	$INPUT{$name} = $value;}

}

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

sub date { 

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time+$addsecs); 
@months = ("1","2","3","4","5","6","7","8","9","10","11","12"); 
@days = ("Sun","Mon","Tue","Wed","Thur","Fri","Sat");

   if ($sec < 10) {$sec = "0$sec";}
   if ($min < 10) {$min = "0$min";}
   if ($hour < 10) {$hour = "0$hour";}
   if ($hour > 11) {$ap = "PM";}
   if ($hour < 12) {$ap = "AM";}
   if ($mday < 10) {$mday = "0$mday";}
   
$date = "@days[$wday] $mday/@months[$mon]/$year"; 
$time = "$hour:$min:$sec $ap";

} 
 
############################################################################

sub mail {

if($mailoff eq 1) { &showbook };
open (MAIL, "|$mailprog");
print MAIL "To: $email\n";
print MAIL "From: $emailfrom\n";
print MAIL "Subject: $bookname response\n\n";
print MAIL "Hi $name, $mailcontent\n";
close(MAIL);

}

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

sub editbook {

$num = 0;
$name = $INPUT{'NAME'}; 
$email = $INPUT{'EMAIL'};
$comments = $INPUT{'COMMENTS'};

$enter = '
';
$replace = '';
$comments =~ s/$enter/$replace/g;

if ($name eq '') { $num = ($num + 1); }
if ($email eq '') { $num = ($num + 1); }
if ($comments eq '') { $num = ($num + 1); }

if ($num ne 0) {
print ("Content-type: text/html\n\n");
print ("<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n");
print ("<H2>TEKBOOK.CGI ERROR MESSAGE</H2>\n\n");
print ("<font face=verdana size=2 color=\"\#0000FF\"><b>\n");
print ("Please complete all supplied form fields<br></b></font>\n");
print ("<font face=verdana size=2 color=\"\#FF0000\"><b>\n");
print ("$num Form field\s not completed<p><FORM>\n");
print ("<INPUT TYPE=button VALUE=\"RETRY\" onClick=\"history.go(-1)\"></FORM>\n");
exit;}

open (FILE, "$guestfile");
@lines = <FILE>;
close(FILE);

open (GUEST,">$guestfile");
if (@lines < 1) {print GUEST "<!--$pagemark-->\n"; close (GUEST);
open (FILE, "$guestfile"); @lines = <FILE>; close(FILE);
open (GUEST,">$guestfile");}

foreach $line (@lines) {
if ($line =~ /<!--$pagemark-->/) { 
print GUEST ("<!--$pagemark-->\n");
print GUEST ("$date - $time<br>IP Address: $ip<br>From: $name<br><A HREF=\"Mailto:$email\">$email</a><P>$comments<P><hr width=90% size=5>\n");}
else {print GUEST ("$line");} 
}
close (GUEST);
}

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

sub create {

print ("Content-type: text/html\n\n");
print ("<HTML>\n");
print ("<HEAD>\n");
print ("<TITLE>SIGN $bookname</TITLE></HEAD>\n");
print ("<BODY $background vlink=\"\#FFC0FF\" text=$textcolor><CENTER>\n");
print ("<font face=verdana size=\"\+3\">Sign $bookname</font>\n");
print ("<hr><P><font face=\"verdana\" size=2><b>\n");
print ("To sign $bookname complete all fields below</b></font><P>\n");
print ("<TABLE border=0 cellspacing=0 cellpadding=0 width=90%><TR>\n");
print ("<TD width=35% align=center><FORM METHOD=POST ACTION=\"$cgiurl\">\n");
print ("<font face=\"verdana\" size=2><b>Name<br></b></font>\n");
print ("<INPUT TYPE=\"TEXT\" NAME=\"NAME\" size=20><P>\n");
print ("<font face=\"verdana\" size=2><b>Email<br></b></font>\n");
print ("<INPUT TYPE=\"TEXT\" NAME=\"EMAIL\" size=20></TD>\n");
print ("<TD width=55% align=center>\n");
print ("<font face=\"verdana\" size=2><b>Comments<br></b></font>\n");
print ("<TEXTAREA name=\"COMMENTS\" COLS=30 ROWS=5 wrap=virtual></TEXTAREA></TD></TR>\n");
print ("</TABLE><P><INPUT TYPE=\"SUBMIT\" NAME=\"ADD\" VALUE=\"SIGN GUESTBOOK\"> \n");
print ("<INPUT TYPE=\"RESET\" VALUE=\"CLEAR FORM\"></FORM><P>\n");
print ("<hr width=90%>\n");
print ("<font face=verdana color=\"\#000080\"size=1><b><center>\n");
print ("TEKBOOK.CGI 1999<br>by <a href=\"$home\">Metertek</a><p></font></center>\n");
print ("</BODY>\n");
print ("</HTML>\n");

exit;

}

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

sub showbook {

print ("Content-type: text/html\n\n");
print ("<HTML>\n");
print ("<HEAD>\n");
print ("<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n");
print ("<TITLE>$bookname</TITLE></HEAD>\n");
print ("<BODY $background vlink=\"\#FFC0FF\" text=$textcolor><CENTER>\n");
print ("<font face=verdana size=\"\+4\">$bookname</font><HR>\n");
print ("<FORM METHOD=POST ACTION=\"$cgiurl\">\n");
print ("<INPUT TYPE=\"SUBMIT\" NAME=\"SIGN\" VALUE=\"SIGN GUESTBOOK\">\n");
print ("<P><TABLE BORDER=4 cellspacing=0 cellpadding=5 width=90%><TR>\n");
print ("<TD $tablebackground><font face=\"verdana\" size=2><b>\n");

open (FILE, "$guestfile");
@lines = <FILE>;
close(FILE);
open (GUEST,">$guestfile");
foreach $line (@lines) {
if(@lines >1) {print "$line"; print GUEST ("$line");} 
else {print ("<center>$bookname empty</center>\n"); print GUEST ("$line");}
}

close (GUEST);

print ("</b></font></TD></TR></TABLE><p><center>\n");
print ("<FORM METHOD=POST ACTION=\"$cgiurl\">\n");
print ("<INPUT TYPE=\"SUBMIT\" NAME=\"SIGN\" VALUE=\"SIGN GUESTBOOK\">\n");
print ("<P><font face=verdana size=2><b>\n");
print ("<a href=\"$homeurl\">HOME</a></font>\n");
print ("<br><hr width=90%>\n");
print ("<font face=verdana size=1><b><center>\n");
print ("TEKBOOK.CGI 1999<br>by <a href=\"$home\">Metertek</a><p></font></center>\n");
print ("</BODY>\n");
print ("</HTML>\n");

exit;

}

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

sub remove {

$removedate = $INPUT{'ENTRYDATE'};

open (FILE, "$guestfile");
@lines = <FILE>;
close(FILE);

open (GUEST,">$guestfile");
foreach $line (@lines) {
if($line =~ $removedate) { $removed eq 1; }
else { print GUEST ("$line"); }
}
close (GUEST);
$adminpass = $password;

}

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

sub adminpass {

print ("Content-type: text/html\n\n");
print ("<HTML>\n");
print ("<HEAD>\n");
print ("<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n");
print ("<TITLE>$bookname Admin Validation</TITLE></HEAD>\n");
print ("<BODY $background vlink=\"\#FFC0FF\" text=$textcolor><CENTER>\n");
print ("<font face=verdana size=\"\+3\">Admin Password Validation</font>\n");
print ("<hr><P><BR><BR><font face=\"verdana\" size=2><b>\n");
print ("Enter the $bookname Administration Password below</b></font><P>\n");
print ("<BR><FORM METHOD=POST ACTION=\"$cgiurl\">\n");
print ("<INPUT TYPE=\"password\" NAME=\"PASSWORD\" size=20>\n");
print (" <INPUT TYPE=\"SUBMIT\" NAME=\"SENDPASS\" VALUE=\"VALIDATE\"></FORM>\n");
print ("<P><BR><BR><hr width=90%>\n");
print ("<font face=\"verdana\" color=\"\#000080\"size=1><b><center>\n");
print ("TEKBOOK.CGI 1999<br>by <a href=\"$home\">Metertek</a><p></font></center>\n");
print ("</BODY>\n");
print ("</HTML>\n");

exit;

}

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

sub admin {

$password = $INPUT{'PASSWORD'};

if ($adminpass ne $password) {
print ("Content-type: text/html\n\n");
print ("<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n");
print ("<H2>NO UNAUTHORISED ACCESS</H2>\n\n");
print ("<font face=verdana size=2 color=\"\#000080\">\n");
print ("<b>You are not authorised to access this resource</b>\n");
exit;}

open (FILE, "$guestfile");
@lines = <FILE>;
close(FILE);

print ("Content-type: text/html\n\n");
print ("<HTML>\n");
print ("<HEAD>\n");
print ("<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n");
print ("<TITLE>$bookname Administration</TITLE></HEAD>\n");
print ("<BODY $background vlink=\"\#FFC0FF\" text=$textcolor><CENTER>\n");
print ("<font face=verdana size=\"\+3\">$bookname Admin</font>\n");
print ("<hr><font face=\"verdana\" size=2><b>\n");
print ("<a href = \"$cgiurl\">Back to $bookname</a><P>\n");
if (@lines > 1) {print ("Choose an entry to remove from $bookname</font><P>\n");}
print ("</b><TABLE BORDER=4 cellspacing=0 cellpadding=5>\n");

if(@lines < 2) {
print ("<TR><TD $tablebackground align=center>\n");
print ("<font face=\"verdana\" size=2><b>$bookname empty\n");
}

else {
print ("<TR><TD $tablebackground align=center>\n");
print ("<font face=\"verdana\" size=2><b>Guest Names</b></font></td>\n");
print ("<TD $tablebackground align=center>\n");
print ("<font face=\"verdana\" size=2><b>Date of entry</b></font></td>\n");
print ("<TD $tablebackground align=center>\n");
print ("<font face=\"verdana\" size=2><b>Action</b></font></td></TR>\n");
}

open (GUEST,">$guestfile");
foreach $line (@lines) {
if($line =~ /<!--$pagemark-->/) {print GUEST ("$line");}

else {
@guestline = split("<br>",$line); $timedate = $guestline[0] ;$guestname = $guestline[2];
@name = split(":",$guestname); $entryname = $name[1];
print ("<TR><TD $tablebackground>\n");
print ("<font face=\"verdana\" size=2><b>\n");
print ("$entryname</font></td><TD $tablebackground align=center>\n");
print ("<font face=\"verdana\" size=2><b>\n");
print ("$timedate</font></td><TD $tablebackground align=center>\n");
print ("<font face=\"verdana\" size=2><b><FORM METHOD=POST ACTION=\"$cgiurl\">\n");
print ("<INPUT TYPE=\"HIDDEN\" NAME=\"ENTRYDATE\" VALUE=\"$timedate\">\n");
print ("<INPUT TYPE=\"SUBMIT\" NAME=\"REMOVE\" VALUE=\"REMOVE\">\n");
print ("</b></font></td></tr></FORM><p>\n");
print GUEST ("$line");}

}
close (GUEST);

print ("</font></TD></TR></TABLE><p><hr width=90%>\n");
print ("<font face=\"verdana\" color=\"\#000080\"size=1><b><center>\n");
print ("TEKBOOK.CGI 1999<br>by <a href=\"$home\">Metertek</a><p></font></center>\n");
print ("</BODY>\n");
print ("</HTML>\n");

exit;

}