#!/usr/bin/perl
#  ^
# / \
#  |   Modify the line the arrow is pointing to, to the correct
#  |   location of Perl v5 or better.
#Uninet######################################################################
#   w  w  w  .  u  n  i  n  e  t  s  o  l  u  t  i  o  n  s  .  c  o   m    #
# UNINET PERL COMPANY(TM) PRESENTS                                          #
# GuestCall 2000 SR1 (2.1.0)                                                #
# Compact, Powerful, GuestCall.                                             #
# ------------------------------------------------------------------------- #
# (C)1997-2001 Universal Networks, All Rights Reserved.                     #
#                                                                           #
# 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 (at your option) 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 for more details.                              # 
#                                                                           #
# You should have received a copy of the GNU General Public License         #
# along with this program; if not, write to the                             #
#                Free Software Foundation, Inc.                             #
#                59 Temple Place - Suite 330                                #
#                Boston, MA  02111-1307, USA.                               #
#                                                                           #
# -->A full copy of the GNU General Public License is included in LICENSE   #
# ------------------------------------------------------------------------- #
# Need Support? We offer FREE technical support!                            #
# Email us at support@uninetsolutions.com for quick, free support today!    # 
# ------------------------------------------------------------------------- #
# Modification History:                                                     #
# 1.0.0: 5/5/1997  GuestCall Released.                                      #
#   SR1: 5/6/1997  HTML Creation Error Fixed.                               #
#   SR2: 3/1/2000  Newly discovered Netscape incompatiblity fixed.          #
# 2.0.0: 6/6/2000  Overhaul of most GuestCall functions - details in README #
# 2.1.0: 4/21/2000 Fixed minor bug, added line length limiter.              #
#######################################################################Uninet 

#Settings (see README for more information)
#1. What is your site's name?
$bookname = "RTSI Guestbook \(powered by GuestCall\)";

#2.What color background do you want?
# NOTE: You can use English names
# of colors or a color's HEX number.
# EXAMPLE: To have a white background,
# you can type "white" or "#FFFFFF."
$color = "#FFFFFF";

#3. Where is the data file?
$gdatl = "/home/ftp/OS9/FAQ/data/gbook.txt";

#4. Enter words you would like
# filtered from you guest book
# REMEMBER: it is case sensitive,
# so word is different from Word
# and WORD.
@badwords = ("shit");

#5. You can enter a custom header
# on the lines between the two
# EOT's.
$header = <<"EOT";

<i>Real-Time Services guestbook for people to post in!</i>


EOT

#6. You can enter a custom footer
# on the lines between the two
# EOT's.
$footer = <<"EOT";

<HR>


EOT

#7. Password for clearing Guest-
# Call messages.
$password = "GC2k";

#8. Are you using Win32?
$win32 = "0";

#9. How many postings should be
# displayed on one page?
$displaynum = "10";

#10. What should be the maximum
# length that length of a line?
# Anything over the number you
# select will be wrapped to the
# next line.
$wrap = "70";


##################################
#your done! your done! your done!#
##################################

$passok = "${password}ok";
$displaynum--;

@months = ('Jan','Feb','Mar','Apr','May','June','July','Aug','Sept','Oct','Nov','Dec');

@days = ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');

($sec,$min,$hour,$mday,$mon,$year,$wday) = (localtime(time))[0,1,2,3,4,5,6];

$year += 1900;

if ($min < 10){
   $min = "0$min";
}


if ($hour < 12){
   
   $ampm = "A.M.";
   
}
else{

   $ampm = "P.M.";
   
}

if ($hour > 12) {
   
   $hour -= 12;
   
}

if ($hour < 1){

   $hour = 12;
   
}


    

$date = "$days[$wday], $months[$mon] $mday, $year at $hour:$min $ampm";

open (data,"$gdatl");
   @rawdat = <data>;
close (data);

foreach $dat (@rawdat){
   $dat =~ s/\r\n//g;
   $dat =~ s/\r//g;
   $dat =~ s/\n//g;     
   $dat_wo_lf .= $dat;
}
   
@gdat = split (/\&\t\&/, $dat_wo_lf);
   

if (@gdat eq ""){

   $gdatnum = 0;
   
}
else{

   $gdatnum = $#gdat + 1;

}

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

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

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/~!/ ~!/g; 
    $FORM{$name} = $value;
}


if ($FORM{'snd'} eq ""){

   if ($ENV{'QUERY_STRING'} eq "log") {
      
      #User wants to see logs.
       
      print ("Content-type: text/html\n\n");
      print "<html><head><title>Log file</title></head><body bgcolor = '$color'>You currently have $gdatnum messages.<BR>";
      print "<tr><table border=\"0\" width = \"100%\"><td>Message No.</td><td>User's Name</td><td>User's Client</td><td>Date written</td></tr>";
      print "<tr><td colspan = 5><HR></td></tr>";
      
      for $i (0...$#gdat){
         ($name, $email, $pdate, $comments, $useragent) = split (":\t:", $gdat[$i]);
         $pnum = $i + 1;
         print "<tr><td>$pnum</td><td>$name ($email)</td><td>$useragent</td><td>$pdate</td></tr>";
      }
      
      print "</table><BR>(C)1997-2000 <a href=\"http://www.uninetsolutions.com/guest\">Universal Networks</a>. Processed on $date.</body></html>";

   }
   elsif ($ENV{'QUERY_STRING'} eq $password){
      print ("Content-type: text/html\n\n");
      print "<html><head><title>Erase Messages?</title></head>";
      print "<body bgcolor=\"#FFFFFF\"><h2>Erase Messages?</h2>";
      print "Are you sure you want to erase all of your ";
      print "GuestCall Messages?<HR><a href=\"$ENV{'SCRIPT_NAME'}\?$passok\">OK</a></body></html>";
   }
   elsif ($ENV{'QUERY_STRING'} eq $passok){
      
      open (mordat, ">$gdatl");
      
         unless ($win32){
            flock mordat, 2;
            flock mordat, 8;
         }
         
      close(mordat);

      print ("Content-type: text/html\n\n");
      print "<html><head><title>Messages Erased</title></head>";
      print "<body bgcolor=\"#FFFFFF\"><h2>Messages Erased</h2>";
      print "GuestCall has deleted all of the postings in your ";
      print "guest book.</body></html>";

   }
   else{

      #User wants to see guest book.

      for $i (0...$#gdat){
         $gdat[$i] =~ s/\</\&lt\;/g;
         $gdat[$i] =~ s/\>/\&gt\;/g;
         $gdat[$i] =~ s/\:ctrln\:/\<BR\>/g;
         (@pairs) = split (/ /, $gdat[$i]);
         $number = 0;
         foreach $pair (@pairs) {
            @pairs2 = split(//, $pair);
            if ($#pairs2 >= $wrap) {
               $num = 0;
               $num2 = 0;
               foreach $num2 (0...$#pairs2){
                  if ($num == $wrap) {
                     $pairs2[$num2] .= " ";
                     $num = "0";
                  }
                  else{
                     $num++;
                  }

               }
            }
            $entrydat ="";
            foreach $pair2 (@pairs2){
               $entrydat .= $pair2;
            }
            $pairs[$number] = "$entrydat ";
            $number++;
         }
         $gdat[$i] = "@pairs";

         for $ii (0...$#badwords){
            $gdat[$i] =~ s/$badwords[$ii]/\(censored\)/g;
         }

      }
   
   
      if ($FORM{'displaynum'}) {
         $startnum = $FORM{'displaynum'};
         $stopnum = $FORM{'displaynum'} + $displaynum;
      }
      else{
         $FORM{'displaynum'} = 0;
         $startnum = "0";
         $stopnum = $displaynum;

         
      }
      
      if ($displaynum < $#gdat) {
         
         $prenum = $startnum - ($displaynum + 1);
         $nextnum = $stopnum + 1;
         
         if ($FORM{'displaynum'} > 0){
            $previous="<center><form action=\"$ENV{'SCRIPT_NAME'}\" method=\"post\"><input type=\"hidden\" name=\"displaynum\" value=\"$prenum\"><input type=\"submit\" value=\"Next\"></form></center>";
         }
         
         if (($FORM{'displaynum'} + 10) < $#gdat){
            $next="<center><form action=\"$ENV{'SCRIPT_NAME'}\" method=\"post\"><input type=\"hidden\" name=\"displaynum\" value=\"$nextnum\"><input type=\"submit\" value=\"Previous\"></form></center>";
         }  
      
      }
      
      for $i ($startnum...$stopnum){
         $selectgdat[$i] = $gdat[$i];
      }
   
      print ("Content-type: text/html\n\n");
      print "<head><title>$bookname</title></head><body bgcolor = '$color'>$header";
      print "${dataaaa}Currently there are $gdatnum postings in $bookname<BR><BR>$previous";
      

     foreach $dat (@selectgdat) {
         if ($dat){
            ($name, $email, $pdate, $comments, $useragent) = split (":\t:", $dat);
            print "<HR width=\"75%\"><b>On $pdate, <a href=\"mailto:$email\">$name</a> said:</b><BR>$comments<BR>";
         }
      }
   
      print "$next<BR><HR width=\"75%\"><FORM ACTION='$ENV{'SCRIPT_NAME'}' METHOD=POST>Your Name:<Input type = 'text' name = 'name' SIZE='45'><BR>Email Address:<Input type = 'text' name = 'email' SIZE='45'><BR>Comments:<BR> <TEXTAREA width = 100% height = 100% name='comment' wrap='soft' cols=60 rows=15></TEXTAREA> <Input type = 'Hidden' name = 'snd' value='2444211'><BR><center><input type='submit' name='submit' value='Submit'> <input type='reset'  name='reset'  value='Clear Form'></center><BR></form><BR>$footer<font size=\"1\">Powered by <a href = \"http://www.uninetsolutions.com/guest/\">GuestCall 2000 \(2.0.1\)</a>, (C) 1997-2001 Universal Networks.</font></body></html>";
   }
}
else{

   #Add message.
   $i = $i + 1;
   open (mordat, "$gdatl");
      @current_messages = <mordat>;
   close(mordat);
   
   open (mordat, ">$gdatl");
   
      unless ($win32){
         flock mordat, 2;
      
         #Check for change made during locking process.
         seek mordat, 0, 2;
      }
        
      $FORM{'comment'} =~ s/\r\n/\:ctrln\:/g;
      $FORM{'comment'} =~ s/\r/\:ctrln\:/g;
      $FORM{'comment'} =~ s/\n/\:ctrln\:/g;
   
      print mordat "$FORM{'name'}:\t:$FORM{'email'}:\t:$date:\t:$FORM{'comment'}:\t:$ENV{'useragent'}&\t&";
      
      if (@current_messages){
         print mordat "\n@current_messages";
      }
      
      unless ($win32){
         flock mordat, 8;
      }
      
   close(mordat);
   
   $form_filt[0] = $FORM{'name'};
   $form_filt[1] = $FORM{'email'};
   $form_filt[2] = $FORM{'comment'};


      for $i (0...$#form_filt){
         $form_filt[$i] =~ s/\</\&lt\;/g;
         $form_filt[$i] =~ s/\>/\&gt\;/g;
         $form_filt[$i] =~ s/\:ctrln\:/\<BR\>/g;
         (@pairs) = split (/ /, $form_filt[$i]);
         $number = 0;
         foreach $pair (@pairs) {
            @pairs2 = split(//, $pair);
            if ($#pairs2 >= $wrap) {
               $num = 0;
               $num2 = 0;
               foreach $num2 (0...$#pairs2){
                  if ($num == $wrap) {
                     $pairs2[$num2] .= " ";
                     $num = "0";
                  }
                  else{
                     $num++;
                  }

               }
            }
            $entrydat ="";
            foreach $pair2 (@pairs2){
               $entrydat .= $pair2;
            }
            $pairs[$number] = "$entrydat ";
            $number++;
         }
         $form_filt[$i] = "@pairs";

         for $ii (0...$#badwords){
            $form_filt[$i] =~ s/$badwords[$ii]/\(censored\)/g;
         }

      }

   $FORM{'name'} = $form_filt[0];
   $FORM{'email'} = $form_filt[1];
   $FORM{'comment'} = $form_filt[2];

   print ("Content-type: text/html\n\n");
   print "<html><head><title>Message Posted to $bookname</title></head><body bgcolor = '$color'>$header<font size=\"4\">Message Posted</font><BR>Here is what you added:<BR><B>Name:</B>$FORM{'name'}<BR><B>Email Address:</B>$FORM{'email'}<BR><B>Comments:</B><BR>$FORM{'comment'}<BR><a href = \"$ENV{'HTTP_REFERER'}\">Back to $bookname</a><HR>Powered by <a href=\"http://www.uninetsolutions.com/guest\">Universal Networks</a>.$footer</body></html>";
}

