#!/usr/bin/perl

require "/home/grumil/public_html/guest/HTMfiles/cgi-library.pl";

$HOST      = $ENV{'REMOTE_HOST'};
$guestbook_log ='guestbooks.dat';
$guestbook_helppage='http://tph16.tuwien.ac.at/~grumil/guest/HTMfiles/help.htm';
$progurl   = 'http://tph16.tuwien.ac.at/~grumil/guest/HTMfiles/guestbook.cgi';
$tempfile  = 'guestbook.tmp1';
$temp2     = '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

$HTML_TOP = $in{'HTML_TOP'};  
$HTML_BOT = $in{'HTML_BOT'};
	# These both fields are making most of the following design-variables
	# obsolvent. The old variables are only left for compatibility
	# In HTML_TOP you can insert the filename where the html-code of
	# the text before the main part of the guestbook comes,
	# and in HTML_BOT you can set the file for the HTML-code after the
	# main book part.

$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'};
        # 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. 
   
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 (!($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 ($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; }
 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 eq 'german') {
   if ($counter) {print "<br>Diese Seite wurde bisher <b>1783$access</b> mal aufgerufen.\n";}
  } elsif ($language eq 'english') {
   if ($counter) {print "<br>This page was accessed <b>$access</b> times.\n";}
  }
  print "<hr noshade>\n";
  &read_data;
 }

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 "somebody else<br>";
  print "</font></p>\n";	
 }
exit;


############################Sub are starting here##############################
sub LadeParameter {  # Loads the parameters
 if (!(-r $varfile)) {
  $kurz='Cannot read varfile: '.$varfile.' !';
  &Fehlermeldung($kurz);
 }
 open (VAR, "<$varfile") || &Fehlermeldung("Cannot read $varfile");
  &NLock($varfile);
        @zeilen=<VAR>;
        chop(@zeilen);
        for ($i=0; $i <= $#zeilen; $i++) {
          if (index($zeilen[$i],'#') == 1) {print("beep!");}
          else {
           if ($zeilen[$i] eq 'END') {}
           else {
            ($vname,$vworth)=split('==',$zeilen[$i]);
            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;}            
          
           }
          }
        }
  &NUnlock($varfile);
 close (VAR);
 $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='';}

 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 eq 'german') {
   $pre_text='Hier kannst Du 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='';}
 # Call system to look up file info like "ls" does:
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime) = stat($varfile); 
 # Call system to associate login and name with uid:
 ($login,$passwd,$uid,$gid,$quota,$comment,$realname,$dir,$shell) = getpwuid($uid);
}
#####################################################################################
sub new_entry { # Here a new entry can be written
# New entry in the guestbook
  local($shorttext,$short_laenge);
 if ($language eq 'english') { 
  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=\"60\">\n";}
  else {print "<dd><input NAME=\"email\" size=60>\n";}
  if ($allow_home == 1) { 
   print "<dt>Your Homepage:\n";
   if ($dismiss) {print "<dd><input NAME=\"homepage\" value=\"$homepage\" size=\"60\">\n";}
   else {print "<dd><input NAME=\"homepage\" size=60 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=60>  ";
   for ($i=0;$i<$short_laenge;$i++) { 
    print "$shorttext[$i]\n";
   }
   print "</textarea>\n";
  }
  else {print "<dd><TEXTAREA NAME=\"text\" ROWS=5 COLS=60> </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 {
  print "Jetzt kannst Du einen neuen Eintrag in das G&auml;stebuch machen.\n";
  if ($forbit_html == 1) {print "Bitte beachte, 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>Dein 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>Deine EMail:\n";
  if ($dismiss) {print "<dd><input NAME=\"email\" value=\"$email\" size=60>\n";}
  else {print "<dd><input NAME=\"email\" size=60>\n";}
  if ($allow_home == 1) { 
   print "<dt>Deine Homepage:\n";
   if ($dismiss) {print "<dd><input NAME=\"homepage\" value=\"$homepage\" size=60>\n";}
   else {print "<dd><input NAME=\"homepage\" size=60 value=\"http://\">\n";}
  }
  print "<dt>Dein 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=60>";
   for ($i=0;$i<$short_laenge;$i++) { 
    print "$shorttext[$i]\n";
   }
   print "</textarea>\n";    
  }
  else {print "<dd><TEXTAREA NAME=\"text\" ROWS=5 COLS=60> </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 somebody else";
 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 eq 'german') {
   print "<hr><P><center><b>Du hast nicht alle n&ouml;tigen Felder ausgef&uuml;llt!<P>\n";
   if (! $name) { print "Wenn du schon anonym bleiben willst, erfinde wenigstens einen Namen!\n";}
   if ((! $email) && ($need_mail==1)) { print "Bitte email angeben! \n";}
   if (! $text) { print "Zumindest ein Buchstabe Text w&auml;\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 "Oh! You gave no name??\n";}
   if ((! $email) && ($need_mail ==1)) { print "Strange! No EMail?! \n";}
   if (! $text) { print "Hohoho! 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 eq '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 eq '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 value=\"   Eingabe wiederholen   \"></form> or ";
   
   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=\"input_zeit\" value=\"$input_zeit\" type=hidden>\n";
   print "<input name=\"rw\" value=\"save_now\" type=hidden>\n";
   print "<INPUT TYPE=SUBMIT value=\"   Eingabe &uuml;bernehmen   \"></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 value=\"   Dismiss   \"></form> or ";
   
   print "<FORM METHOD=get ACTION=\"$progurl\">\n";
   print "<input name=\"input_zeit\" value=\"$input_zeit\" type=hidden>\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=\"save_now\" type=hidden>\n";
   print "<INPUT TYPE=SUBMIT value=\"   Accept   \"></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 eq 'german') {print "<center><b>Dein Eintrag wurde bereits gesichert! <br>Bitte 'reloade' nicht mehr.</b></center>\n";}
 else {print "<center><b>Your entry was already saved! <br>Please don't reload anymore.</b></center>\n";}
} 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; 

  if ($language eq 'german') {print "<hr><P>Dein Eintrag wurde gesichert.\n";}
  else {print "<hr><P>Your entry was saved.\n";}
  
  &Write_Global_Guestbooklog;  
    # This will write the guestbook's data in the logfile of all guestbooks.
  

  if (($answer_mail == 1) && ($email)) {
   open (MAIL, "|$mailprog -t") || print "Warning: Can't open $mailprog!\n";
    print MAIL "To: $email\n";
    print MAIL "From: $login\n";
    print MAIL "Reply-To: $login\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 "You wrote:\n";
    for ($i=0;$i<$short_laenge;$i++) {
     print MAIL "$shorttext[$i]\n";
    }
    print MAIL " - $zeit\n";
    print MAIL "------------------------------------------------------\n";
   close (MAIL);
  }
 }
 if ($language eq 'german') {
  print "<P><a href=\"$progurl?rw=read&varfile=$varfile\">Zur&uuml;ck zum Buch...</a>\n";
 }
 if ($language eq 'english') {
  print "<P><a href=\"$progurl?rw=read&varfile=$varfile\">Back to the book...</a>\n";
 }
}
#####################################################################################
sub read_data {
 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 (($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);
     print "$seperator\n";
    }
   }
  }
  &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 "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 "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 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>";
  }
}
#####################################################################################
