$Id: guestbook.pl,v 1.1 2001/12/17 17:46:01 shane Exp $
#! /usr/bin/perl
# 
#############################################################################
#                                                                           #
#c0ded by Shane 							    #
#Shane has now left the planet earth to join the Borgs. Warpspeed="9";-)    #
#									    #
#############################################################################
#Freedom of speach, freedom of c0de!					    #
#This is free software, it may be used and modified free of charge by 	    #
#anyone. If you manage to make money of it. Great!			    #
#############################################################################
#Filer som følger med:	GJB version 0.3.1              			    #
#        1) guestbook.pl - Selve hacket. Skrevet i perl			    #
#        2) guestbook.html - Fila som perl skriver til.			    #
#        3) addguest.html - html-formen som generer input til perl-scriptet #
#        4) guestlog.html - En kort log-fil. Nyttig til feilsøking.	    #
#        5) README.TXT - Dokumetasjon. Kommer i neste version.   	    #
#############################################################################

#Her konfigurerer du gjesteboka di

#Url'er og path's på serveren
$isp = "http://nettlevrandør/ditt_brukernavn/"		#url til sida hvor html-filene ligger
$guestbookurl = "$isp/guestbook.html";	
$guestbookreal = "/home/ .. /public_html/guestbook.html";	#ekte sti på servern
$guestlog = "/home/ .. /public_html/guestlog.html";		#ekte sti på servern
$cgiurl = "$isp/cgi-bin/guestbook.pl";                          
$date_command = "/usr/bin/date";				#datostempling fra OS

#Innstillinger
$uselog = 0;            				# 1 = Ja; 0 = Nei: Logg av/på
$linkmail = 1;          				# 1 = Ja; 0 = Nei: Linke mailadr med en mailto
$separator = 1;         				# 1 = <hr>; 0 = <p>: Avsnitt eller strek imellom innlegga
$redirection = 1;       				# 1 = Ja; 0 = Nei:
$entry_order = 1;       				# 1 = siste først; 0 = eldste først
$allow_html = 0;        				# 1 = Ja; 0 = Nei: tillate <P> og <BR> i fritekstfeltet
$line_breaks = 0;					# 1 = Ja; 0 = Nei:

$mail = 1;              				# 1 = Ja; 0 = Nei Få mail ved nytt innlegg
$mailprog = '/usr/lib/sendmail';            		# sti til mailerdeamon, forskjellig på linux og BSD
$recipient = 'you@your.isp.no'; 	                # mottageradr for $mail

$remote_mail = 1;       				# 1 = Ja; 0 = Nei: sende takk til gjesten
$sub_remote_mail ="Hilsen i gjesteboka til $recipient"; #Subject felt i takkemail
$hilsen ="Takk for at du tok deg tid til å skrive litt til meg.";		#innhold i takkemail

#slutt på konfig, ikke endre noe nedenfor her om du ikke vet hva du
#driver med....enjoy ..Shane
##############################################################################

# Få dato fra OS=>gjestebok
$date = `$date_command +"%A, %B %d, %Y at %T (%Z)"`; 
    chop($date);
$shortdate = `$date_command +"%D %T %Z"`; 
    chop($shortdate);

# Lese fra addguest

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

# splitte name-value parene

@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;
}

# Skrive ut subrutinene fra blanke-felt

&no_comments unless $FORM{'comments'};
&no_name unless $FORM{'realname'};

#########################################################
# Åpne for skriving, her gjøres hele endringa.
#
open (FILE,"$guestbookreal") || die "Can't Open $guestbookreal: $!\n";
@LINES=<FILE>;
close(FILE);
$SIZE=@LINES;

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'}) {
         print GUEST "<A HREF=\"$FORM{'url'}\">$FORM{'realname'}</a>";
      }
      else {
         print GUEST "$FORM{'realname'}";
      }

      if ( $FORM{'username'} ){
         if ($linkmail eq '1') {
            print GUEST qq| \&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{'state'} ){
         print GUEST " $FORM{'state'}";
      }

      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);

# Skrive til logfila

if ($uselog eq '1') {
   &log('entry');
}


###############################################################################
# Tilbakemld ved innlegg

if ($mail eq '1') {
   open (MAIL, "|$mailprog $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: Gjestebok\n\n";
   print MAIL "Det har kommet et nytt innlegg på $guestbookurl:\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{'state'} ){
      print MAIL " $FORM{'state'}";
   }

   if ( $FORM{'country'} ){
      print MAIL " $FORM{'country'}";
   }

   print MAIL " - $date\n";
   print MAIL "------------------------------------------------------\n";

   close (MAIL);
}
###############################################################################
#Sende takkemail til innskriver'n

if ($remote_mail eq '1' && $FORM{'username'}) {
   open (MAIL, "|$mailprog -t") || die "Can't open $mailprog!\n";

   print MAIL "To: $FORM{'username'}\n";
   print MAIL "From: $recipient\n";
   print MAIL "Subject: $sub_remote_mail\n\n";
   print MAIL "$hilsen\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{'state'} ){
      print MAIL " $FORM{'state'}";
   }

   if ( $FORM{'country'} ){
     print MAIL " $FORM{'country'}";
   }

   print MAIL " - $date\n";
   print MAIL "------------------------------------------------------\n";

   close (MAIL);
}

# Skrive ut første ut heading for redirigering
if ($redirection eq '1') {
   print "Location: $guestbookurl\n\n";
}
else { 
   &no_redirection;
}

#################################################################################
# Subrutiner

sub no_comments {
   print "Content-type: text/html\n\n";
   print "<HTML><HEAD><TITLE>ERROR</TITLE></HEAD>\n";
   print "<BODY><H1>Skrev du ikke inn noen hilsen?</H1>\n";
   print "<P>Feltet til din hilsen ser ut til å være blankt, derfor vil ikke\n";
   print "denne hilsnen bli lagt til, vær vennlig å fylle inn\n";
   print "din hilsen i det blanke feltet under;)</P>\n";
   print "<FORM METHOD=POST action=\"$cgiurl\">\n";
   print "<P>Ditt navn/nick:<INPUT TYPE=text name=\"realname\" size="40" ";
   print "value=\"$FORM{'realname'}\"><br>\n";
   print "Mail-adr: <INPUT TYPE=text name=\"username\""; 
   print "value=\"$FORM{'username'}\" size=40><br>\n";
   print "By: <INPUT TYPE=text name=\"city\" value=\"$FORM{'city'}\" ";
   print "size=15>, Og det er i?: <input type=text name=\"state\" "; 
   print "value=\"$FORM{'state'}\" size=15> Country: <input type=text "; 
   print "name=\"country\" value=\"$FORM{'country'}\" size=15><P>\n";
   print "Skriv inn din hilsen:<br>\n";
   print "<textarea name=\"comments\" COLS="60" ROWS="4"></textarea><P>\n";
   print "<INPUT TYPE=submit VALUE="Gi Jernet!"> * <INPUT TYPE=reset VALUE="Gir opp!"></FORM><HR>\n";
   print "Tilbake til  <A HREF=\"$guestbookurl\">Gjesteboka</a>.";
   print "\n</BODY></HTML>\n";

   # Logge feilmld
   if ($uselog eq '1') {
      &log('no_comments');
   }

   exit;
}

sub no_name {
   print "Content-type: text/html\n\n";
   print "<HTML><HEAD><TITLE>error</TITLE></HEAD>\n";
   print "<BODY><H1>Du glemte å fylle inn Navn/Nick</H1>\n";
   print "Navnefeltet ser ut til å være blankt, derfor vil ikke\n";
   print "denne hilsnen bli lagt til, vær vennlig å fylle inn\n";
   print "Navn/Nick i det blanke feltet under;)<P>\n";
   print "<FORM METHOD=POST ACTION=\"$cgiurl\">\n";
   print "Navn el nick :<input type=text name=\"realname\" size=40><BR>\n";
   print "Mail-adr: <INPUT TYPE=text name=\"username\"";
   print " value=\"$FORM{'username'}\" size=40><BR>\n";
   print "By: <input type=text name=\"city\" value=\"$FORM{'city'}\" ";
   print "size=15>, State: <input type=text name=\"state\" ";
   print "value=\"$FORM{'state'}\" size=15> Land: <input type=text ";
   print "value="pottit-landet" name=\"country\" value=\"$FORM{'country'}\" ";
   print "size=15><p>\n";
   print "Comments have been retained.<P>\n";
   print "<INPUT TYPE=hidden name=\"comments\" ";
   print "value=\"$FORM{'comments'}\">\n";
   print "<INPUT TYPE=submit VALUE="Gi Jernet!"> * <INPUT TYPE=reset VALUE="Gir opp!"><HR>\n";
   print "Tilbake til  <A HREF=\"$guestbookurl\">Gjesteboka</a>.";
   print "\n</body></html>\n";

   # Logge feilmld
   if ($uselog eq '1') {
      &log('no_name');
   }

   exit;
}

# Skrive til logfila og ta tak i hostnamet til innskriver'n
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 innstillinger
sub no_redirection {

   # Print start på HTML
   print "Content-Type: text/html\n\n";
   print "<HTML><HEAD><TITLE>Takk</TITLE></HEAD>\n";
   print "<BODY><H1>Takk for ditt bidrag.</H1>\n";

   # Print svar
   print "Takk for at du tok deg tid til å fylle ut i gjesteboka vår\n";
   print "Ditt innlegg er nå lagt inn på sida.<HR>\n";
   print "Her er innlegget ditt:<P>\n";
   print "<B>$FORM{'comments'}</B><BR>\n";

   if ($FORM{'url'}) {
      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{'state'} ){
      print " $FORM{'state'}";
   }

   if ( $FORM{'country'} ){
      print " $FORM{'country'}";
   }

   print " - $date<p>\n";

# Print End of HTML
   print "<HR>\n";
   print "<A HREF=\"$guestbookurl\">Tilbake til gjesteboka</A>\n";
   print "- Det kan hende du må reloade sida for å se din hilsen.\n";
   print "</BODY></HTML>\n";

   exit;
}





