#!/usr/local/bin/perl
# Set Variables

$guestbookurl = "http://www.pnpi.spb.ru/ofve/nni/guest.vol1";
$guestbookreal = 
"http://www.pnpi.spb.ru/ofve/nni/guest.vol1";
$return_page = 
"http://www.pnpi.spb.ru/ofve/nni/guestbook.shtml";
$guestbookvol1 = 
"http://www.pnpi.spb.ru/ofve/nni/guest.vol1";
$guestbookvol1real =  
"http://www.pnpi.spb.ru/ofve/nni/guest.vol1";
$return_page_vol1 = 
"http://www.pnpi.spb.ru/ofve/nni/guest.shtml";
$guestbookvol2 = "http://www.pnpi.spb.ru/ofve/nni/gb.db";
$guestbookvol2real = "http://www.pnpi.spb.ru/ofve/nni/gb.db";
$return_page_vol2 = 
"http://www.pnpi.spb.ru/ofve/nni/guest.shtml";
$guestlog = 
"http://www.pnpi.spb.ru/ofve/nni/cgi-bin/guestlog.html";
$cgiurl = 
"http://www.pnpi.spb.ru/ofve/nni/cgi-bin//addst.cgi";
$date_command = "/usr/bin/date";

# Set Your Options:
$mail = 1;              # 1 = Yes; 0 = No
$uselog = 0;            # 1 = Yes; 0 = No
$linkmail = 1;          # 1 = Yes; 0 = No
$separator = 1;         # 1 = <hr>; 0 = <p>
$redirection = 1;       # 1 = Yes; 0 = No
$entry_order = 1;       # 1 = Newest entries added first;
                        # 0 = Newest Entries added last.
$remote_mail = 1;       # 1 = Yes; 0 = No
$allow_html = 1;        # 1 = Yes; 0 = No
$line_breaks = 0;	# 1 = Yes; 0 = No

# If you answered 1 to $mail or $remote_mail you will need to fill out 
# these variables below:
$mailprog = '/usr/sbin/sendmail';
$recipient = 'lepekhin@pnpi.spb.ru';

# Done
##############################################################################

# Get the Date for Entry
$date = `$date_command +"%A, %B %d, %Y at %T (%Z)"`; chop($date);
$shortdate = `$date_command +"%D %T %Z"`; chop($shortdate);

# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

# Split the name-value pairs
@pairs = split(/&/, $buffer);

foreach $pair (@pairs) {
   ($name, $value) = split(/=/, $pair);

   # Un-Webify plus signs and %-encoding
   $value =~ tr/+/ /;
   $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
   $value =~ s/<!--(.|\n)*-->//g;
   if ($allow_html != 1) {
      $value =~ s/<([^>]|\n)*>//g;
   }
   $FORM{$name} = $value;
}
if ($FORM{'volume'} eq coctail) {
 $guestbookurl = $guestbookvol1;
 $guestbookreal = $guestbookvol1real;
 $return_page = $return_page_vol1;
}elsif($FORM{'volume'} eq icq){
 $guestbookurl = $guestbookvol2;
 $guestbookreal = $guestbookvol2real;
 $return_page = $return_page_vol2;
}
# Check URL and mail fields.
# We don't print this fields as link if they are empty
# (or without dot for URL)
      $FORM{'url'} =~ s/\s+//g;
      $FORM {'username'}=~ s/\s+//g;
      ($beginurl, $middleurl, $endofurl) = split(/\./, $FORM{'url'});

# Print the Blank Response Subroutines
&no_comments unless $FORM{'comments'};
&no_name unless $FORM{'realname'};

# Begin the Editing of the Guestbook File
open (FILE,"$guestbookreal") || die "Can't Open $guestbookreal: $!\n";
@LINES=<FILE>;
close(FILE);
$SIZE=@LINES;

# Open Link File to Output
open (GUEST,">$guestbookreal") || die "Can't Open $guestbookreal: $!\n";

for ($i=0;$i<=$SIZE;$i++) {
   $_=$LINES[$i];
   if (/<!--begin-->/) { 

      if ($entry_order eq '1') {
         print GUEST "<!--begin-->\n";
      }
   
      if ($line_breaks == 1) {
         $FORM{'comments'} =~ s/\cM\n/<br>\n/g;
      }

      print GUEST "<B>$FORM{'comments'}</B><BR>\n";
      
      if ($FORM{'url'} ne "http://" && $middleurl ne "") {
         print GUEST "<A HREF=\"$FORM{'url'}\">$FORM{'realname'}</A>";
      }
      else {
         print GUEST "$FORM{'realname'}";
      }

      if ( $FORM{'username'} ){
         if ($linkmail eq '1') {
            print GUEST " \&lt;<A HREF=\"mailto:$FORM{'username'}\">";
            print GUEST "$FORM{'username'}</A>\&gt;";
         }
         else {
            print GUEST " &lt;$FORM{'username'}&gt;";
         }
      }

      print GUEST "<BR>\n";

      if ( $FORM{'city'} ){
         print GUEST "$FORM{'city'},";
      }
     
      if ( $FORM{'country'} ){
         print GUEST " $FORM{'country'}";
      }

      if ($separator eq '1') {
         print GUEST " - $date<HR>\n\n";
      }
      else {
         print GUEST " - $date<P>\n\n";
      }

      if ($entry_order eq '0') {
         print GUEST "<!--begin-->\n";
      }

   }
   else {
      print GUEST $_;
   }
}

close (GUEST);

# Log The Entry

if ($uselog eq '1') {
   &log('entry');
}


#########
# Options

# Mail Option
if ($mail eq '1') {
   open (MAIL, "|$mailprog -oeq $recipient") || die "Can't open  $mailprog!\n";

   print MAIL "Reply-to: $FORM{'username'} ($FORM{'realname'})\n";
   print MAIL "From: $FORM{'username'} ($FORM{'realname'})\n";
   print MAIL "Subject: Entry to Guestbook\n\n";
   print MAIL "You have a new entry in your guestbook:\n\n";
   print MAIL "------------------------------------------------------\n";
   print MAIL "$FORM{'comments'}\n";
   print MAIL "$FORM{'realname'}";

   if ( $FORM{'username'} ){
      print MAIL " <$FORM{'username'}>";
   }

   print MAIL "\n";

   if ( $FORM{'city'} ){
      print MAIL "$FORM{'city'},";
   }
        
   if ( $FORM{'country'} ){
      print MAIL " $FORM{'country'}";
   }

   print MAIL " - $date\n";
   print MAIL "------------------------------------------------------\n";

   close (MAIL);
}

if ($remote_mail eq '1' && $FORM{'username'}) {
   open (MAIL, "|$mailprog -t -oeq") || die "Can't open $mailprog!\n";

   print MAIL "To: $FORM{'username'}\n";
   print MAIL "From: $recipient\n";
   print MAIL "Subject: Entry to Guestbook\n\n";
   print MAIL "Thank you for adding to my guestbook.\n\n";
   print MAIL "------------------------------------------------------\n";
   print MAIL "$FORM{'comments'}\n";
   print MAIL "$FORM{'realname'}";

   if ( $FORM{'username'} ){
      print MAIL " <$FORM{'username'}>";
   }

   print MAIL "\n";

   if ( $FORM{'city'} ){
      print MAIL "$FORM{'city'},";
   }

   if ( $FORM{'country'} ){
     print MAIL " $FORM{'country'}";
   }

   print MAIL " - $date\n";
   print MAIL "------------------------------------------------------\n";

   close (MAIL);
}

# Print Out Initial Output Location Heading
if ($redirection eq '1') {
   print "Location: $return_page\n\n";
}
else { 
   &no_redirection;
}

#######################
# Subroutines

sub no_comments {
   print "Content-type: text/html\n\n";
   print "<HTML><HEAD><TITLE>No Comments</TITLE></HEAD>\n";
   print "<BODY><BLINK><H1>Your Comments appear to be blank</H1></BLINK>\n";
   print "The comment section in the guestbook fillout form appears\n";
   print "to be blank and therefore the Guestbook Addition was not\n";
   print "added.  Please enter your comments below.<P>\n";
   print "<FORM METHOD=POST ACTION=\"$cgiurl\">\n";
   print "Your Name:<INPUT TYPE=text NAME=\"realname\" SIZE=50 ";
   print "VALUE=\"$FORM{'realname'}\"><BR>\n";
   print "E-Mail: <INPUT TYPE=text NAME=\"username\""; 
   print "VALUE=\"$FORM{'username'}\" SIZE=50><BR>\n";
   print "City: <INPUT TYPE=text NAME=\"city\" VALUE=\"$FORM{'city'}\" ";
   print "SIZE=30>, Country: <INPUT TYPE=text "; 
   print "NAME=\"country\" VALUE=\"$FORM{'country'}\" SIZE=20><P>\n";
   print "Comments:<BR>\n";
   print "<TEXTAREA NAME=\"comments\" COLS=60 ROWS=7></TEXTAREA><P>\n";
   print "<INPUT TYPE=submit> &nbsp <INPUT TYPE=reset></FORM><HR>\n";
   print "Return to the <A HREF=\"$guestbookurl\">Guestbook</a>.";
   print "\n</BODY></HTML>\n";

   # Log The Error
   if ($uselog eq '1') {
      &log('no_comments');
   }

   exit;
}

sub no_name {
   print "Content-type: text/html\n\n";
   print "<HTML><HEAD><TITLE>No Name</TITLE></HEAD>\n";
   print "<BODY><BLINK><H1>Your Name appears to be blank</H1></BLINK>\n";
   print "The Name Section in the guestbook fillout form appears to\n";
   print "be blank and therefore your entry to the guestbook was not\n";
   print "added.  Please add your name in the blank below.<P>\n";
   print "<FORM METHOD=POST ACTION=\"$cgiurl\">\n";
   print "Your Name:<INPUT TYPE=text NAME=\"realname\" SIZE=50><BR>\n";
   print "E-Mail: <INPUT TYPE=text NAME=\"username\"";
   print " VALUE=\"$FORM{'username'}\" size=50><BR>\n";
   print "City: <INPUT TYPE=text NAME=\"city\" VALUE=\"$FORM{'city'}\" ";
   print "size=30>, Country: <INPUT TYPE=text ";
   print "NAME=\"country\" VALUE=\"$FORM{'country'}\" ";
   print "size=20><P>\n";
   print "Comments have been retained.<P>\n";
   print "<INPUT TYPE=hidden NAME=\"comments\" ";
   print "VALUE=\"$FORM{'comments'}\">\n";
   print "<INPUT TYPE=submit> &nbsp <INPUT TYPE=reset><HR>\n";
   print "Return to the <A HREF=\"$guestbookurl\">Guestbook</A>.";
   print "\n</BODY></HTML>\n";

   # Log The Error
   if ($uselog eq '1') {
      &log('no_name');
   }

   exit;
}

# Log the Entry or Error
sub log {
   $log_type = $_[0];
   open (LOG, ">>$guestlog");
   if ($log_type eq 'entry') {
      print LOG "$ENV{'REMOTE_HOST'} - [$shortdate]<br>\n";
   }
   elsif ($log_type eq 'no_name') {
      print LOG "$ENV{'REMOTE_HOST'} - [$shortdate] - ERR: No Name<br>\n";
   }
   elsif ($log_type eq 'no_comments') {
      print LOG "$ENV{'REMOTE_HOST'} - [$shortdate] - ERR: No ";
      print LOG "Comments<br>\n";
   }
}

# Redirection Option
sub no_redirection {

   # Print Beginning of HTML
   print "Content-Type: text/html\n\n";
   print "<HTML><HEAD><TITLE>Thank You</TITLE></HEAD>\n";
   print "<BODY><H1>Thank You For Signing The Guestbook</H1>\n";

   # Print Response
   print "Thank you for filling in the guestbook.  Your entry has\n";
   print "been added to the guestbook.<HR>\n";
   print "Here is what you submitted:<P>\n";
   print "<B>$FORM{'comments'}</B><BR>\n";

   if ($FORM{'url'} ne 'http://' && $middleurl ne "") {
      print "<A HREF=\"$FORM{'url'}\">$FORM{'realname'}</A>";
   }
   else {
      print "$FORM{'realname'}";
   }

   if ( $FORM{'username'} ){
      if ($linkmail eq '1') {
         print " &lt;<A HREF=\"mailto:$FORM{'username'}\">";
         print "$FORM{'username'}</A>&gt;";
      }
      else {
         print " &lt;$FORM{'username'}&gt;";
      }
   }

   print "<BR>\n";

   if ( $FORM{'city'} ){
      print "$FORM{'city'},";
   }

   if ( $FORM{'country'} ){
      print " $FORM{'country'}";
   }

   print " - $date<P>\n";

   # Print End of HTML
   print "<HR>\n";
   print "<A HREF=\"$return_page\">\n";
   print "<FONT SIZE=3><B>Back to the Guestbook</B></FONT></A>\n";         
   print "- You may need to reload it when you get there to see your\n";
   print "entry.\n";
   print "<P>\n";
   print "</BODY></HTML>\n";

   exit;
}

