#! /usr/local/bin/perl
##############################################################################
# guestbook.pl   V3.0                                                        #
# COPYRIGHT NOTICE                                                           #
# Copyright 1998 Wolfgang Wiese     All Rights Reserved.                     #
#       EMail: unrzc9@rrze.uni-erlangen.de                                   #
#       URL  : http://www.rrze.uni-erlangen.de/~unrzc9                       #
#                                                                            #
# Guestbook may be used and modified free of charge by anyone so long as     #
# this copyright notice and the comments above remain intact.  By using this #
# code you agree to indemnify Wolfgang Wiese from any liability that         #  
# might arise from it's use.                                                 #  
#                                                                            #
# Selling the code for this program without prior written consent is         #
# expressly forbidden.  In other words, please ask first before you try and  #
# make money off of my program.                                              #
#                                                                            #
# Obtain permission before redistributing this software over the Internet or #
# in any other medium.	In all cases copyright and header must remain intact.#
##############################################################################

require "/home/iti_db/eike/public_html/pzguest/cgi-library.pl";

# Constants. 
$HOST      = $ENV{'REMOTE_HOST'};
$guestbook_log ='/home/iti_db/eike/public_html/pzguest/guestbooks.dat';
$guestbook_helppage='http://cgi.xwolf.com/cgi/guestbook.shtml';
$progurl   = 'http://wwwiti.cs.uni-magdeburg.de/~eike/pzguest/guestbook.cgi';
$tempfile  = '/home/iti_db/eike/public_html/pzguest/guestbook.tmp1';
$temp2     = '/home/iti_db/eike/public_html/pzguest/guestbook.tmp2';
$mailprog  = '/usr/lib/sendmail';

##############################################################################
# Get the variables passed down via the web
&ReadParse;
$rw_book     = $in{'rw'};            # rw must be either 'read' or 'write'.
$varfile     = $in{'varfile'};       
                                     # This definies the filename, where all  
                                     # parameters I explained above gave to be 
                                     # written in.
$name        = $in{'name'};          # Eingabe: Name des USers
$email       = $in{'email'};         # Eingabe: Email des Users
$text        = $in{'text'};          # Eingabe: Text des Users
$homepage    = $in{'homepage'};      # Eingabe: Homepage des Users
$input_zeit  = $in{'input_zeit'};    # Eingabe: Zeit der Eingabe.

# The following constants can be written in the $varfile, but it works here too

$hintergrund = $in{'hintergrund'};  
        # background of the book (<BODY>-tag) for the old version...
$bodytag     = $in{'bodytag'};
        # bodytags for html, replaces background!  (<body $bodytag>)      
$titel       = $in{'titel'} || 'Guestbook';   
        # Title of the book
$kopf        = $in{'top'} || ' ';    
        # Here, you can give in HTML-code, the head of your document. Esp. If 
        # you like to have icons there. The title is after it!
$bottom      = $in{'bottom'};        
        # Same as above, but it will be at the bottom  
$book_file   = $in{'book'};          
        # Text-File of the book
$forbit_html = $in{'forbit_html'} || 0; 
        # If this is set to 1, no html-tags are allowed
$counter     = $in{'counter'} || 0;   
        # 0 = Counter off, 1 = Dumb Counter on, 2 = HideCounter and normal on. 
        # The numbers of accesses of the normal counter will 
        # be written in the file $varfile, if it was set to 1
        # If the hidecounter is on, the hosts/accesses will be written in 
        # another file too. 
$ignore      = $in{'ignore'} || 'off';
$allow_home  = $in{'allow_home'} || 1; 
        # 1 = Feld fuer Homepage da, 0 = ohne Feld.
$language    = $in{'language'} || 'german';      
        # default=german, english possible in the future
$need_mail   = $in{'need_mail'} || 0;
        # 0 or 1. If 1, then the user has to enter an email. If 0, it don't
        # matter, if he lets it out. But notice, that if this value is 0,
        # no mail will be send to the user of writing a message. 
$answer_mail = $in{'answer_mail'} || 0;
        # 1 or 0, If 1 is set the author of a new entry will get a 'thank'-mail 
$new_file    = $in{'new_file'};     
        # If this field is given, a html-file named new_file will be created, 
        # in which all entrys will be written.  
$seperator   = $in{'seperator'} || '<P>';
        # <hr> or <p>. Is for the deviding of the entrys in the book
$add_beginning = $in{'add_beginning'} || 1;
        # 1 or 0. If 1 is set the entry will inserted at the top of the document.
$italic      = $in{'italic'} || 1;
        # If 1 the text of the entries will be displyed italic.
$pre_text    = $in{'pre_text'};
$entry_ask   = $in{'entry_ask'};
$past_text   = $in{'past_text'};
$newconfig   = $in{'new_config'};

        # This is the message, which is displayed above the guestbook-data.
        # entry_ask is the text, with the link to the input of new entries, why pre_text
        # and past_text are surrounding this field.
        # Notice: If $entry_ask is set to 'none', it won't be displayed. 
$okval	     = $in{'ok'};
	# To test if the input is right
	  
if ($need_mail == 0) {$answer_mail=0;}
if ($italic ==1) {$starti = '<i>'; $stopi = '</i>';}
else {$starti = ''; $stopi='';}
$varfile=&Check_Name($varfile);
#########################Main program is starting here#########################
print(&PrintHeader);

if ($okval) {
 if (($okval =~ /wiederholen /i) || ($okval =~/ Dismiss /i)) { $rw_book="dismiss";}
 else { $rw_book="save_now";}
}


if (!($varfile)) { &Fehlermeldung("You gave no $varfile!<br> The guestbook cannot run without it");}
&LadeParameter;
&AutoRemove;
if (($rw_book eq 'read') && ($counter)) { 
 if ($counter == 2) {
  # Call for the hidecounter, if he was set on.
  $kurz=$book_file.','.$ignore;

  &Hidecount($kurz);
 } else {
  # Add 1 to the dumb counter:
  $access++;
  &SaveParameter; 
 }
}
if ($newconfig) {&SaveParameter;}

if ((($rw_book =~ /write/) || ($rw_book =~ /dismiss/)) && ($INPUT_LAYOUT)) {
	# Es liegt eine Eingabe vor und diese hat ihren eigenen Style,
	# also mach keine HTML_TOP-Ausgabe
} else {
  if ($HTML_TOP) {
     open (f5,"<$HTML_TOP") || &Fehlermeldung("Cannot open HTML_TOP: $HTML_TOP ");
     	while(<f5>) {
     	 chop($_);
     	 print "$_\n";
     	}
     close f5;  
  } else {
	if (!($bodytag)) {$bodytag='background="'.$hintergrund.'"';}
	$kurz=$titel.','.$bodytag.','.$kopf;
	&HtmlTop($kurz);
  }
}
 if ($rw_book eq 'write') { $dismiss=0;&new_entry; }
 elsif ($rw_book eq 'dismiss') { $dismiss=1;&new_entry; }
 elsif ($rw_book eq 'save') { &check_entry;}
 elsif ($rw_book eq 'save_now') { &save_entry; &read_data;}
 else {   # Read all entrys
  if ($entry_ask eq 'none') {print "$pre_text $past_text\n";}
  else {print "$pre_text <a href=\"$progurl?rw=write&varfile=$varfile\">$entry_ask</a> $past_text\n";}
  if ($language =~ /german/) {
   if ($counter) {print "<br>Diese Seite wurde bisher <b>$access</b> mal aufgerufen.\n";}
  } elsif ($language =~ /english/i) {
   if ($counter) {print "<br>This page was accessed <b>$access</b> times.\n";}
  }
  if (not $HTML_TOP) {print "<hr noshade>\n";}
  &read_data;
 }

#if ((($rw_book ne 'write') || ($rw_book ne 'dismiss')) && ($INPUT_LAYOUT)) {
  if ($HTML_BOT) {
     open (f5,"<$HTML_BOT") || &Fehlermeldung("Cannot open HTML_BOT: $HTML_BOT");
     	while(<f5>) {
     	 chop($_);
     	 print "$_\n";
     	}
     close f5;  
  } else {
    print "<dt>$bottom<br></font>\n";
    print "<P><HR noshade><p align=right><font size=-2>";
    print "Script written by\n";
    print "<a href=\"http://www.xwolf.com/\">xwolf</a><br>";
    print "</font></p>\n";	
  }
#}
exit;


############################Sub are starting here##############################
sub LogWriter {
 if ($LOGFILE) {
  open(f1,">>$LOGFILE");
   print f1 "$ENV{'REMOTE_ADDR'}, $zeit, $name, $email, $homepage\n";
  close f1;
 }
}
#####################################################################################
sub ReadLayout {
 my ($LAYOUTFILE)=@_;
 
 $i=0;
 open(f1,"<$LAYOUTFILE") || &Fehlermeldung("Kann Layout-Datei $LAYOUTFILE nicht oeffnen!");
  while(<f1>) {
   chop($_);
   if (length($_) > 3) {
    $LAYOUTDATA[$i++] = $_;
   }
  }
 close f1;
}
##############################################################################

sub new_entry { 
 # Here a new entry can be written
 my ($shorttext,$short_laenge);
 my ($kurzmessage);
 
 if ($INPUT_LAYOUT) {
   # Vorgegebenes Layout
   
  &ReadLayout($INPUT_LAYOUT);
  
  $kurzmessage="";
  if ($dismiss) {
   $i=0;
   open(f1, "<$temp2") || print "<p>Error Cannot open $tempmess";
    &NLock($temp2);
     while(<f1>) {
      chop($_);
      $shorttext[$i]=$_;
      $i++;
     }
     &NUnlock($temp2);
   close f1; 
   $short_laenge=$i;

   for ($i=0;$i<short_laenge;$i++) {
    if (length($shorttext[$i]) > 1) {
     chop($shorttext[$i]);
     $kurzmessage=$kurzmessage."$shorttext[$i]\n";
    }
   }
  }
  
  for ($i=0; $i<=$#LAYOUTDATA; $i++) {
   $LAYOUTDATA[$i] =~ s/#MESSAGE#/$kurzmessage/g;
   $LAYOUTDATA[$i] =~ s/#INPUTTIME#/$zeit/g;
   $LAYOUTDATA[$i] =~ s/#INPUTNAME#/$name/g;
   $LAYOUTDATA[$i] =~ s/#INPUTHOMEPAGE#/$homepage/g;
   $LAYOUTDATA[$i] =~ s/#INPUTEMAIL#/$email/g;
 
   print "$LAYOUTDATA[$i]\n";
  }
  
 } else {
  # Ab hier Standard-Layout

  if ($language =~ /english/i) { 
   print "Now you can add your own entry for the guestbook.\n";
   if ($forbit_html == 1) {print "Please notice, that HTML-tags arn't allowed.\n";}
   else {print "HTML-Tags will be permitted\n";}
   print "<dl>\n";
   print "<dt>\n";
   print "<FORM METHOD=get ACTION=\"$progurl\">\n";
   print "<dd><dl>\n";
   print "<input name=\"varfile\" value=\"$varfile\" type=hidden>\n";
   print "<input name=\"rw\" value=\"save\" type=hidden>\n";
   print "<input name=\"input_zeit\" value=\"$zeit\" type=hidden>\n";
   print "<dt>Your Name:\n";
   if ($dismiss) {print "<dd><input name=\"name\" value=\"$name\" size=\"20\">\n";}
   else {print "<dd><input name=\"name\" size=\"20\">\n";}
  
   print "<dt>Your EMail:\n";
   if ($dismiss) {print "<dd><input NAME=\"email\" value=\"$email\" size=\"80\">\n";}
   else {print "<dd><input NAME=\"email\" size=80>\n";}
   if ($allow_home == 1) { 
    print "<dt>Your Homepage:\n";
    if ($dismiss) {print "<dd><input NAME=\"homepage\" value=\"$homepage\" size=\"80\">\n";}
    else {print "<dd><input NAME=\"homepage\" size=80 value=\"http://\">\n";}
   }
   print "<dt>Your Text:\n";
   if ($dismiss) {
    $i=0;
    open(f1, "<$temp2") || print "<p>Error Cannot open $tempmess";
     &NLock($temp2);
      while(<f1>) {
       chop($_);
       $shorttext[$i]=$_;
       $i++;
      }
      &NUnlock($temp2);
    close f1; 
    $short_laenge=$i;
    print "<dd><TEXTAREA NAME=\"text\" ROWS=5 COLS=80>  ";
    for ($i=0;$i<$short_laenge;$i++) { 
     print "$shorttext[$i]\n";
    }
    print "</textarea>\n";
   }
   else {print "<dd><TEXTAREA NAME=\"text\" ROWS=5 COLS=80> </textarea>\n";}
   print "<P>\n";
   print "<dt></dl><dt><center><INPUT TYPE=SUBMIT value=\"   Send new entry   \">  <INPUT TYPE=RESET value=\"   Reset all fields   \"></center>\n";
   print "</dl>\n";
 
  } else {
   # Now in german
     
   print "Jetzt k&ouml;nnen Sie einen neuen Eintrag in das G&auml;stebuch machen.\n";
   if ($forbit_html == 1) {print "Bitte beachten Sie, dass HTML-Tags nicht angenommen werden.\n";}
   else {print "HTML-Tags werden innerhalb des Textfeldes unterst&uuml;tzt.\n";}
   print "<dl>\n";
   print "<dt>\n";
   print "<FORM METHOD=get ACTION=\"$progurl\">\n";
   print "<dd><dl>\n";
   print "<input name=\"varfile\" value=\"$varfile\" type=hidden>\n";
   print "<input name=\"rw\" value=\"save\" type=hidden>\n";
   print "<input name=\"input_zeit\" value=\"$zeit\" type=hidden>\n";
   print "<dt>Ihr Name:\n";
   if ($dismiss) {print "<dd><input name=\"name\" value=\"$name\" size=\"20\">\n";}
   else {print "<dd><input name=\"name\" size=\"20\">\n";}
   
   print "<dt>Ihre EMail:\n";
   if ($dismiss) {print "<dd><input NAME=\"email\" value=\"$email\" size=80>\n";}
   else {print "<dd><input NAME=\"email\" size=80>\n";}
   if ($allow_home == 1) { 
    print "<dt>Ihre Homepage:\n";
    if ($dismiss) {print "<dd><input NAME=\"homepage\" value=\"$homepage\" size=80>\n";}
    else {print "<dd><input NAME=\"homepage\" size=80 value=\"http://\">\n";}
   }
   print "<dt>Text:\n";
   if ($dismiss) {
    $i=0;
    open(f1, "<$temp2") || print "<p>Error Cannot open $tempmess";
     &NLock($temp2);
      while(<f1>) {
       chop($_);
       $shorttext[$i]=$_;
       $i++;
      }
     &NUnlock($temp2);
    close f1; 
    $short_laenge=$i;
    print "<dd><TEXTAREA NAME=\"text\" ROWS=5 COLS=80>";
    for ($i=0;$i<$short_laenge;$i++) { 
     print "$shorttext[$i]\n";
    }
    print "</textarea>\n";    
   }
   else {print "<dd><TEXTAREA NAME=\"text\" ROWS=5 COLS=80> </textarea>\n";}
   print "<P>\n";
   print "<dt></dl><dt><center><INPUT TYPE=SUBMIT value=\"   EINTRAG ABSENDEN   \">  <INPUT TYPE=RESET value=\"   Alle Felder zur&uuml;cksetzen   \"></center>\n";
   print "</dl>\n";
  }
  
 }
}
#####################################################################################
sub Fehlermeldung {  # Gibt Fehlermeldungen aus
 local($fehlertext)=@_;
 if ($HEADER) {&HtmlTop(',,');}
 print "<P><blink><B><center>ERROR!</center></b></blink>\n";
 print "<p>The following failure was detected:<p>\n";
 print "<dt><dd><pre><b>$fehlertext</b></pre></dl>\n";
 print "<p>";
 print "<P>There is a help avaible for this. Check out: \n";
 print "<a href=\"$guestbook_helppage\">Informations about the guestbook</a>\n";
 print "<br><br><br>";
 print "Skript written by <a href=\"http://www.xwolf.com\">xwolf</a>, <a href=\"mailto:xwolf\@xwolf.com\">feedback welcome</a>";
 exit(0);
}
#####################################################################################
sub check_entry {
 my($server,$subpage);
 if (length($homepage) < 11) {$homepage="";}
 if (($allow_home) && ($homepage =~ /http/)) { 
  $server=substr($homepage,7,length($homepage));
  $subpage=substr($server,index($server,'/'),length($server));
  $server=substr($server,0,index($server,'/'));
  $status = &httpstatus($server,$subpage);
  if ($status >= 400) {$homepage='';}
 }

 if ((! $name) || ((! $email) && ($need_mail==1)) || (! $text)) {
  if ($language =~ /german/) {
   print "<hr><P><center><b>Sie haben nicht alle n&ouml;tigen Felder ausgef&uuml;llt!<P>\n";
   if (! $name) { print "Sie gaben keinen Namen an!\n";}
   if ((! $email) && ($need_mail==1)) { print "Nanu?! Keine EMail? \n";}
   if (! $text) { print "Einen Eintrag ohne Text??\n";}
   print "<p></b>\n";
   print "<FORM METHOD=post ACTION=\"$progurl\">\n";
   print "<input name=\"varfile\" value=\"$varfile\" type=hidden>\n";
   print "<input name=\"rw\" value=\"write\" type=hidden>\n";
   print "<INPUT TYPE=SUBMIT value=\"   Nochmal versuchen   \"></form> oder ";
   print "<FORM METHOD=post ACTION=\"$progurl\">\n";
   print "<input name=\"varfile\" value=\"$varfile\" type=hidden>\n";
   print "<input name=\"rw\" value=\"read\" type=hidden>\n";
   print "<INPUT TYPE=SUBMIT value=\"   Eintr&auml;ge lesen   \"></form></center>\n";
  } else {
   print "<hr><P><center><b>You gave not all important entrys!<P>\n";
   if (! $name) { print "You gave no name??\n";}
   if ((! $email) && ($need_mail ==1)) { print "Strange! No EMail?! \n";}
   if (! $text) { print "A message without text??\n";}
   print "<p></b>\n";
   print "<FORM METHOD=post ACTION=\"$progurl\">\n";
   print "<input name=\"varfile\" value=\"$varfile\" type=hidden>\n";
   print "<input name=\"rw\" value=\"write\" type=hidden>\n";
   print "<INPUT TYPE=SUBMIT value=\"   Try it again   \"></form> or ";
   print "<FORM METHOD=post ACTION=\"$progurl\">\n";
   print "<input name=\"varfile\" value=\"$varfile\" type=hidden>\n";
   print "<input name=\"rw\" value=\"read\" type=hidden>\n";
   print "<INPUT TYPE=SUBMIT value=\"   Read the existing entrys   \"></form></center>\n";
  } 
 }
 else { 
   # First of all, I save the text, to avoid problems with HTML-tags if I would
   # use this values as hidden-objects in the following input-form.
   open(f1,">$temp2") || print "<p>Error Cannot open $tempmess";
    print f1 "$text\n";   
   close f1;

   # Now the preview:
   &preview;
 }
}
#####################################################################################
sub preview {  # Here the users will get a preview of their new entry
 local($shorttext,$short_laenge);
 if ($language =~ /german/) { print "<h3>Voransicht: </h3>\n";}
 else {print "<h3>Preview:</h3>\n";}
 print "$seperator";

 $i=0;
 open(f1, "<$temp2") || print "<p>Error Cannot open $tempmess";
  &NLock($temp2);
     while(<f1>) {
      chop($_);
      $shorttext[$i]=$_;
      $shorttext[$i]=$shorttext[$i].'<br>';
      $i++;
     }
  &NUnlock($temp2);
 close f1; 
 $short_laenge=$i;
   
 if (($allow_home==1) && ($homepage)) {
     print "<dt><b><a href=\"$homepage\">$name</a></b>";
 } else {print "<dt><b>$name</b>";}
 if ($email) {print " (<a href=\"mailto:$email\">$email</a>), $zeit<dd>";}
 else {print ", $zeit<dd>";}
 print "<P>";
 for ($i=0;$i<$short_laenge;$i++) {
   print "<dd>$starti $shorttext[$i] $stopi\n";
 }  
 &EndHtmlTags(1);
 print "$seperator\n";
 print "<br><center>\n";
 if ($language =~ /german/) {
   print "<FORM METHOD=post ACTION=\"$progurl\">\n";
   print "<input name=\"name\" value=\"$name\" type=hidden>";
   print "<input name=\"homepage\" value=\"$homepage\" type=hidden>";
   print "<input name=\"email\" value=\"$email\" type=hidden>";
   print "<input name=\"varfile\" value=\"$varfile\" type=hidden>\n";
   print "<input name=\"rw\" value=\"dismiss\" type=hidden>\n";
   print "<input name=\"input_zeit\" value=\"$input_zeit\" type=hidden>\n";
   print "<INPUT TYPE=SUBMIT name=\"ok\"  value=\" Eingabe &uuml;bernehmen \"> or ";
   print "<INPUT TYPE=SUBMIT name=\"ok\" value=\" Eingabe wiederholen \"></form></center>\n"; 
 } else {
   print "<FORM METHOD=get ACTION=\"$progurl\">\n";
   print "<input name=\"name\" value=\"$name\" type=hidden>";
   print "<input name=\"homepage\" value=\"$homepage\" type=hidden>";
   print "<input name=\"email\" value=\"$email\" type=hidden>";   
   print "<input name=\"varfile\" value=\"$varfile\" type=hidden>\n";
   print "<input name=\"input_zeit\" value=\"$input_zeit\" type=hidden>\n";
   print "<input name=\"rw\" value=\"dismiss\" type=hidden>\n";
   print "<INPUT TYPE=SUBMIT name=\"ok\" value=\" Accept \"> or ";
   print "<INPUT TYPE=SUBMIT name=\"ok\" value=\" Dismiss \"> </form></center>\n";
   
 }
 print "</center>"; 
}
#####################################################################################
sub save_entry { # I will now save the entry
 local($shorttext,$short_laenge);
# First check for HTML-Tags

      $name     =~ s/<([^>]|\n)*>//g;
      $email    =~ s/<([^>]|\n)*>//g;
      $homepage =~ s/<([^>]|\n)*>//g;

 $i=0;
 open(f1, "<$temp2") || print "<p>Error Cannot open $temp2";
  &NLock($temp2);
     while(<f1>) {
      chop($_);
      $shorttext[$i]=$_;
      $shorttext[$i] =~ s/<meta([^>]|\n)*>//g;
      $shorttext[$i] =~ s/<META([^>]|\n)*>//g;   # No Meta
      $shorttext[$i] =~ s/<!--([^>]|\n)*>//g;    # No SSI and comments
      $shorttext[$i] =~ s/<script([^>]|\n)*>//g;     # No javascripts
      $shorttext[$i] =~ s/<\/script([^>]|\n)*>//g;    # No javascripts
      $shorttext[$i] =~ s/<SCRIPT([^>]|\n)*>//g;     # No javascripts
      $shorttext[$i] =~ s/<\/SCRIPT([^>]|\n)*>//g;    # No javascripts
      
      $shorttext[$i]=$shorttext[$i].'<br>';
      $i++;
     }
   &NUnlock($temp2);
 close f1; 
 $short_laenge=$i;
 
 if ($forbit_html == 1) {
  for ($i=0;$i<$short_laenge;$i++) {$shorttext[$i] =~ s/<([^>]|\n)*>//g;}
 }

# Now check the tmp-file, if the last entry is the same as this. 
# This I make, to avoid dupple saves cause of too much clicking on 'Reload'.
$r=1;$i=0;$t=0;
 open (f1, "<$tempfile") || print "<P>Error: Can't open $tempfile!"; 
  &NLock($tempfile);
  while (<f1>) {
   chop($_);
   if ($i == 0) { $rname[$r]    = $_; }
   if ($i == 1) { $remail[$r]   = $_; }
   if ($i == 2) { $rhomepage[$r]= $_; }
   if ($i == 3) { $rzeit[$r]    = $_; }
   if ($s == 1) {
    $antwort[$t]=$_;
    $t++;
   }
   if ($_ eq '#') {
    if ($s == 0) {$s=1;$t=0;}
    else {$s=0;}
   } 
   $i++;
  }
 &NUnlock($tempfile);
 close f1;

if (($rname[1] eq $name) && ($email[1] eq $email) && ($rhomepage[1] eq $homepage) && ($rzeit[1] eq $input_zeit)) {
 if ($language =~ /german/) {
    &Fehlermeldung("<center><b>Ihr Eintrag wurde bereits gesichert! <br>Bitte 'reloaden' Sie nicht mehr.</b></center>");
 } else {
    &Fehlermeldung("<center><b>Your entry was already saved! <br>Please don't reload anymore.</b></center>");
 }
} else {
 if ($add_beginning == 1) {
   open (FILE,"$book_file") || &Fehlermeldung('Cannot open book_file!');
    &NLock($book_file);
     @LINES_FILE=<FILE>;
    &NUnlock($book_file);
   close(FILE);
   $SIZE_FILE=$#LINES_FILE;
   open (NEWENTRY,">$book_file") || print "Can't Open $book_file: $!\n";
    &NLock($book_file);
   print NEWENTRY "$name\n$email\n$homepage\n$input_zeit\n#\n";
   for ($i=0;$i<$short_laenge;$i++) {
    print NEWENTRY "$shorttext[$i]\n";
   }
   print NEWENTRY "#\n";
   print NEWENTRY "-END-\n"; 
   for ($i=0;$i<=$#LINES_FILE;$i++) {
    $_=$LINES_FILE[$i];
    print NEWENTRY $_;
   }
   &NUnlock($book_file);
   close (NEWENTRY);
 }
 else {
  open (f1, ">>$book_file") || &Fehlermeldung("Cannot open $book_file!"); 
   &NLock($book_file);
   print f1 "$name\n$email\n$homepage\n$input_zeit\n#\n";
   for ($i=0;$i<$short_laenge;$i++) {
    print f1 "$shorttext[$i]\n";
   }
   print f1 "#\n";   
   print f1 "-END-\n"; 
   &NUnlock($book_file);
  close f1;
 }
 # Now I will save the this entry also in the tempfile.
  open (f1, ">$tempfile") || &Fehlermeldung('Cannot open $tempfile!'); 
   &NLock($tempfile);
   print f1 "$name\n$email\n$homepage\n$input_zeit\n#\n";
   for ($i=0;$i<$short_laenge;$i++) {
    print f1 "$shorttext[$i]\n";
   }
   print f1 "#\n";   
   print f1 "-END-\n"; 
   &NUnlock($tempfile);
  close f1; 

  
  &Write_Global_Guestbooklog;  
    # This will write the guestbook's data in the logfile of all guestbooks.
  

   open (MAIL, "|$mailprog -t") || print "Warning: Can't open $mailprog!\n";
    print MAIL "To: eike\@iti\.cs\.uni-magdeburg\.de,thomas\@peggyzoo\.de\n";
    print MAIL "From: eike\@iti\.cs\.uni-magdeburg\.de\n";
    print MAIL "Reply-To: eike\@iti\.cs\.uni-magdeburg\.de\n";
    print MAIL "Subject: Entry to Guestbook\n\n";
    print MAIL "Folgendes wurde eingetragen.\n\n";
    print MAIL "------------------------------------------------------\n";
    for ($i=0;$i<$short_laenge;$i++) {
     print MAIL "$shorttext[$i]\n";
    }
    print MAIL " - $zeit\n";
    print MAIL "------------------------------------------------------\n";
   close (MAIL);
 }
 
 &LogWriter;
 
# if ($language =~ /german/) {
#  print "<hr><P>Ihr Eintrag wurde gesichert.\n";
#  print "<P><a href=\"$progurl?rw=read&varfile=$varfile\">Zur&uuml;ck zum Buch...</a>\n";
# }
# if ($language =~ /english/i) {
#  print "<hr><P>Your entry was saved.\n";
#  print "<P><a href=\"$progurl?rw=read&varfile=$varfile\">Back to the book...</a>\n";
# }
}
#####################################################################################
sub read_data {
 my ($kurzmessage);
 my ($nowline);
 if ($OUTPUT_LAYOUT) {
  &ReadLayout($OUTPUT_LAYOUT);
 } else { 
  print "<dl>\n";
 }
 
 open (f2, "<$book_file") || &Fehlermeldung("Cannot open $book_file"); 
  @LINES_FILE=<f2>;
 close f2;
 open (f1, "<$book_file") || &Fehlermeldung("Cannot open $book_file"); 
  &NLock($book_file);
  $i=0;$s=0;$r=0;$l=0;
  while (<f1>) {
   chop($_);
   if ($i == 0) { $rname[$r]    = $_; }
   if ($i == 1) { $remail[$r]   = $_; }
   if ($i == 2) { $rhomepage[$r]= $_; }
   if ($i == 3) { $rzeit[$r]    = $_; }
   if ($s == 1) {
    $antwort[$t]=$_;
    $t++;
   }
   if ($_ eq '#') {
    if ($s == 0) {$s=1;$t=0;}
    else {$s=0;}
   } 
   $i++;$l++;
   if ($_ eq '-END-') {
   
    $i=0;
    if ($OUTPUT_LAYOUT) {
     $kurzmessage="";
     for ($z=0;$z<($t-1);$z++) {
      if (length($antwort[$z]) > 1) {
       $kurzmessage=$kurzmessage." $antwort[$z]\n";
      }
     }
     
     for ($z=0; $z<=$#LAYOUTDATA; $z++) {
      $nowline=$LAYOUTDATA[$z];
      $nowline =~ s/#TEXTCONTENT#/$kurzmessage/g;
      $nowline =~ s/#AUTHORURL#/$rhomepage[$r]/g;
      $nowline =~ s/#AUTHOREMAIL#/$remail[$r]/g;
      $nowline =~ s/#AUTHORNAME#/$rname[$r]/g;
      $nowline =~ s/#TEXTTIME#/$rzeit[$r]/g;
      print "$nowline\n";
     }
     
    } else {            
     if (($allow_home ==1) && ($rhomepage[$r])) {
      print "<dt><b><a href=\"$rhomepage[$r]\">$rname[$r]</a></b>";
     } else {print "<dt><b>$rname[$r]</b>";}
     if ($remail[$r]) {print " (<a href=\"mailto:$remail[$r]\">$remail[$r]</a>), $rzeit[$r]<dd>";}
     else {print ", $rzeit[$r]<dd>";}
     print "<P>";
     for ($a=0;$a<($t-1);$a++) {
      print "<dd>$starti $antwort[$a] $stopi\n";
     }  
    }
    
    $r++;
    if ($l < $#LINES_FILE-1) {
     &EndHtmlTags(1);
     if (not $OUTPUT_LAYOUT) { print "$seperator\n";}
    }
    
    
   } # Ende Blockende dieser Nachricht
  }
  &NUnlock($book_file);
 close f1;
 &EndHtmlTags(1);
}
#####################################################################################
sub SaveParameter {
  open (VA, ">$varfile") || &Fehlermeldung('Cannot open varfile to edit accesses!');
   print VA "# After this point the parameters are starting.\n";
   print VA "############################################################\n";
   print VA "HTML_TOP==$HTML_TOP\n";
   print VA "HTML_BOT==$HTML_BOT\n";
   print VA "# These both are files. The files have HTML-Code included, which will\n";
   print VA "# be displayed before (TOP) and after (BOT) the guestbook-part. If you use\n";
   print VA "# them, the following designing-variables will become obsolvent.\n";
   print VA "titel==$titel\n";
   print VA "# The title of the guestbook.\n";
   print VA "bodytag==$bodytag\n";
   print VA "# What you want to see in the <body>... background will be replaced\n";
   print VA "top==$kopf\n";
   print VA "# What you want to see above the title. Maybe an icon...\n";
   print VA "hintergrund==$hintergrund\n";
   print VA "# Background of the page. An URL-address. For compatibility. bodytag replaces this\n";
 
   print VA "pre_text==$pre_text\n";
   print VA "entry_ask==$entry_ask\n";
   print VA "past_text==$past_text\n";
   print VA "# This is the message, which is displayed above the guestbook-data.\n";
   print VA "# entry_ask is the text, with the link to the input of new entries, why pre_text\n";
   print VA "# and past_text are surrounding this field.\n";
   print VA "# NOTICE: If you set entry_ask==none then no link will be displayed!!\n";
   print VA "bottom==$bottom\n";
   print VA "# What you want to see at the end of the page.\n";
   print VA "book==$book_file\n";
   print VA "# The file of the guestbook-entries. Must be (chmod a=rw)\n";
   print VA "logfile==$LOGFILE\n";
   print VA "# To snoop, who is there. If nothing is given, it will be ignored\n";
   print VA "input_layout==$INPUT_LAYOUT\n";
   print VA "# If this file exists, it will be used as layoutfile for textinput.\n";
   print VA "# Variables in this HTML-File, that will be dynamically replaces\n";
   print VA "# are   #MESSAGE#   for the textfield,\n";
   print VA "#       #INPUTTIME# for the time of input and\n";
   print VA "#       #INPUTNAME#, #INPUTEMAIL#, #INPUTHOMEPAGE# for the VALUES (!) of the inputfields.\n";
   print VA "# We advise to have a look at the standard-input first to copy the\n";
   print VA "# needed variable-names, and then insert these two replacements at the\n";
   print VA "# dynamical parts.\n";
   print VA "output_layout==$OUTPUT_LAYOUT\n";
   print VA "# How a single message has to look. You have to insert as replacement-markers\n";
   print VA "# the following variables:\n";
   print VA "# #AUTHORNAME#, #AUTHOREMAIL#, #AUTHORURL#, #TEXTTIME#, #TEXTCONTENT#\n";
   print VA "# Only these variables will be replaced in the html-file. \n";   
   print VA "# Body and Bottom of the html-text will be taken from the HTML_TOP and HTML_BOT\n";
   print VA "forbit_html==$forbit_html\n";
   print VA "# If set to 1 , no html-tags are allowed. Else set 0.\n";
   print VA "counter==$counter\n";
   print VA "# 1 makes the counter on, 0 off, 2 sets the improved counter on\n";
   print VA "allow_home==$allow_home\n";
   print VA "# If 1 , the user can give his homepage too, else 0\n";
   print VA "language==$language\n";
   print VA "# You can set it to 'english' or to 'german'\n";
   print VA "AUTO_REMOVE==$AUTO_REMOVE\n";
   print VA "# The value says, after which days messages will be erased out of the book automatically\n";
   print VA "# If no autoremove should happen, set it to 0.\n";
   print VA "seperator==$seperator\n";
   print VA "# How you want to part different messages...<p> or <hr>\n";
   print VA "need_mail==$need_mail\n";
   print VA "# If the author is forced to give his email-address\n";
   print VA "answer_mail==$answer_mail\n";
   print VA "# If an mail will send to the author after his entry. Not yet ready\n";
   print VA "add_beginning==$add_beginning\n";
   print VA "# If 1 is set the new entry will be insert at the top of the document\n";
   print VA "italic==$italic\n";
   print VA "# 1 or 0. If 'on' the input will be displayed italic.\n";
   print VA "ACCESS==$access\n";
   print VA "# The number of the accesses to this page. Will add 1 only if its\n";
   print VA "# a read-call.\n";
   print VA "tempfile1==$tempfile\n";
   print VA "tempfile2==$temp2\n";
   print VA "# These both files are for internal use. If you don't set them, the\n";
   print VA "# default files will be used, which may be used also by many other users.\n";
   print VA "# This could lead to the problem, that messages of other books will be saved\n";
   print VA "# on your board, or messages will go lost before saved. \n";
  
   print VA "\n";
   print VA "# Script was made by Wolfgang Wiese,xwolf\@xwolf.com\n";
   print VA "#                              http://www.xwolf.com\n";
  close (VA);
}
###############################################################################
sub LadeParameter {  # Loads the parameters
 my ($vname,$vworth);
 my @zeilen;
 
 open (VAR, "<$varfile") || &Fehlermeldung("Cannot read $varfile");
  &NLock($varfile);
        @zeilen=<VAR>;
        chop(@zeilen);
        for ($i=0; $i <= $#zeilen; $i++) {
          if (index($zeilen[$i],'#') == 1) {}
          else {
           if ($zeilen[$i] eq 'END') {}
           else {
            ($vname,$vworth)=split('==',$zeilen[$i]);
            if ($vname eq 'input_layout') {$INPUT_LAYOUT=$vworth;}
            if ($vname eq 'output_layout') {$OUTPUT_LAYOUT=$vworth;}               
            if ($vname eq 'hintergrund') {$hintergrund=$vworth;}
            if ($vname eq 'bodytag') {$bodytag=$vworth;}
            if ($vname eq 'titel') {$titel=$vworth;}
            if ($vname eq 'top') {$kopf=$vworth;}
            if ($vname eq 'pre_text') {$pre_text=$vworth;}
            if ($vname eq 'entry_ask') {$entry_ask=$vworth;}
            if ($vname eq 'past_text') {$past_text=$vworth;}            
            if ($vname eq 'bottom') {$bottom=$vworth;}
            if ($vname eq 'book') {$book_file=$vworth;}
            if ($vname eq 'forbit_html') {$forbit_html=$vworth;}
            if ($vname eq 'counter') {$counter=$vworth;}
            if ($vname eq 'ignore') {$ignore=$vworth;}
            if ($vname eq 'allow_home') {$allow_home=$vworth;}
            if ($vname eq 'language') {$language=$vworth;}
            if ($vname eq 'seperator') {$seperator=$vworth;}
            if ($vname eq 'italic') {$italic=$vworth;}
            if ($vname eq 'answer_mail') {$answer_mail=$vworth;}
            if ($vname eq 'need_mail') {$need_mail=$vworth;}
            if ($vname eq 'ACCESS') {$access=$vworth;}
            if ($vname eq 'add_beginning') {$add_beginning=$vworth;}
            if ($vname eq 'tempfile1') {$tempfile=$vworth;}
            if ($vname eq 'tempfile2') {$temp2=$vworth;}
	    if ($vname eq 'HTML_BOT') {$HTML_BOT=$vworth;}
            if ($vname eq 'HTML_TOP') {$HTML_TOP=$vworth;}  
            if ($vname eq 'AUTO_REMOVE') {$AUTO_REMOVE=$vworth;}            
            if ($vname eq 'logfile') {$LOGFILE=$vworth;}            
          
           }
          }
        }
  &NUnlock($varfile);
 close (VAR);
 
 if ($INPUT_LAYOUT) {
  $INPUT_LAYOUT=&Check_Name($INPUT_LAYOUT);
  if (($INPUT_LAYOUT =~ /illegal/i) || (length($INPUT_LAYOUT) < 4)) {
   $INPUT_LAYOUT="";
  }
 }
 if ($OUTPUT_LAYOUT) {
  $OUTPUT_LAYOUT=&Check_Name($OUTPUT_LAYOUT);
  if (($OUTPUT_LAYOUT =~ /illegal/i) || (length($OUTPUT_LAYOUT) < 4)) {
   $OUTPUT_LAYOUT="";
  }
 }
 
 
 
 $book_file=&Check_Name($book_file);
 $HTML_TOP=&Check_Name($HTML_TOP);
 if ($HTML_TOP eq 'illegal_filename') {$HTML_TOP='';}
 $HTML_BOT=&Check_Name($HTML_BOT);
 if ($HTML_BOT eq 'illegal_filename') {$HTML_BOT='';}
 $LOGFILE=&Check_Name($LOGFILE);
 if ($LOGFILE =~ /illegal/) {$LOGFILE='';}

 if ($need_mail == 0) {$answer_mail=0;}
 if ($italic ==1) {$starti = '<i>'; $stopi = '</i>';}
 else {$starti = ''; $stopi='';}
 if ((!($pre_text)) && (!($entry_ask)) && (!($past_text))) {
  if ($language =~ /german/) {
   $pre_text='Hier k&ouml;nnen Sie die Eintr&auml;ge im G&auml;stebuch lesen und sogar';
   $entry_ask='eigene Eintr&auml;ge';
   $past_text='machen. Viel Spass.';
  } else {
   $pre_text='Here you can read all entries of the guestbook or';
   $entry_ask='make own entries.';
   $past_text='Have fun';
  }
 }  
 if (($pre_text) && ($past_text) && (!($entry_ask))) {$entry_ask=$pre_text;$pre_text='';}
 
}
#####################################################################################
sub Write_Global_Guestbooklog {
 # This procedure will mark the board in the global list of all boards, which
 # used somewhere.
 # The log should be only called, if a new entry was made; not if the book
 # was only accessed.
 
  open (f1,">>$guestbook_log") || print "<p>Warning: Cannot open $boardlog!";
   &NLock($guestbook_log);
    if ($titel) {print f1 "$titel\n";}
    else {print f1 "-no title-\n";}
    if ($realname) { print f1 "$realname\n"; }
    else {print f1 "-unknown-\n";}
    print f1 "$varfile\n";
    print f1 "$zeit\n";
    print f1 "#\n";
   &NUnlock($guestbook_log);
  close f1;
}
#####################################################################################
sub AutoRemove {
	# This sub will automatically remove messages, that are older then $AUTO_REMOVE days
 my(@gbname,@gbemail,@gbhomepage,@gbzeit,@gbtext,@gbzeilen);
 my ($tagessumme)=0;
 my ($textsumme)=0;
 my ($tdatum,$tzeit);
 my ($ttag,$tmonat,$tjahr);
 my ($update_need)=0;
 
 
 if ($AUTO_REMOVE > 0) {
  $tagessumme=$tag+30*$monat+365*$jahr;
  &NLock($book_file);
   open (f1, "<$book_file") || &Fehlermeldung("Cannot open $book_file"); 
  
    $i=0;   # Zeilennummer
    $s=0;   # Wenn s=0 ist er noch nicht beim text
    $r=0;   # Eintragsnummer
    $t=0;   # Zeilennummer im Text
    while (<f1>) {
     chop($_);
     if ($i == 0) { $gbname[$r]    = $_; }
     if ($i == 1) { $gbemail[$r]   = $_; }
     if ($i == 2) { $gbhomepage[$r]= $_; }
     if ($i == 3) { $gbzeit[$r]    = $_; 
      ($tdatum,$tzeit)=split(/ - /,$gbzeit[$r]);
      ($ttag,$tmonat,$tjahr)=split(/\./,$tdatum);
      $textsumme=$ttag+30*$tmonat+365*$tjahr;
      if (($tagessumme-$textsumme) > $AUTO_REMOVE) { $update_need=1; }
     }
     if (($s == 1) && ($_ ne '#')) {
      $gbtext[$r][$t]=$_;
      $t++;
     }
     if ($_ eq '#') {
      if ($s == 0) {
       $s=1;
      } else {
       $s=0;
      }
     } 
     $i++;
     if ($_ eq '-END-') {
      $i=0;    
      $r++;
      $gbzeilen[$r]=$t;
      $t=0;
     }
    }
   close f1;
   
   if ($update_need) {
    open (f1,">$book_file") || &Fehlermeldung("Cannot write $book_file");
     for ($i=0;$i<=$#gbname;$i++) {
      ($tdatum,$tzeit)=split(/ - /,$gbzeit[$i]);
      ($ttag,$tmonat,$tjahr)=split(/\./,$tdatum);
      $textsumme=$ttag+30*$tmonat+365*$tjahr;
      if (($tagessumme-$textsumme) <= $AUTO_REMOVE) {
       print f1 "$gbname[$i]\n";
       print f1 "$gbemail[$i]\n";
       print f1 "$gbhomepage[$i]\n";
       print f1 "$gbzeit[$i]\n";
       print f1 "#\n";
       for ($l=0;$l<=$gbzeilen[$i];$l++) {
        print f1 "$gbtext[$i][$l]\n";
       }
       print f1 "#\n-END-\n";
      }
     }
    close f1;
   }
  &NUnlock($book_file);
 } else {
  if ($AUTO_REMOVE < 0) { $AUTO_REMOVE=0;}
 }
}
#####################################################################################
sub EndHtmlTags {
  local($notable)=@_;
  if ($notable) {
   print "</dl></b></frameset></h1></h2></h3></h4></h5></blink></ul></a></form></pre></font></u></div></td></tr></table>";
  } else {
   print "</dl></b></frameset></h1></h2></h3></h4></h5></blink></ul></a></form></pre></font></u>";
  }
}
#####################################################################################