#!/usr/bin/perl
#
# Variablen 
#
$exlock=2;
$unlock=8;
$orig="/home/httpd/htdocs/pinwand/guestbook.txt";
$time=localtime(time);
$remotehost=$ENV{'REMOTE_HOST'};
$remoteaddr=$ENV{'REMOTE_ADDR'};

print "Content-type: text/html\n\n";

#
# GET oder POST?
#
if ($ENV{'REQUEST_METHOD'} eq "GET")
{
  $query_string=$ENV{'QUERY_STRING'};
}
elsif (($ENV{'REQUEST_METHOD'} eq "POST") &&
       ($ENV{'CONTENT_TYPE'} eq "application/x-www-form-urlencoded"))
{
  read(STDIN,$query_string,$ENV{'CONTENT_LENGTH'});
}
else
{
#  &html_out(0,"<B>Submission method not supported!</B>\n");
};

### Auswerten der Argumentzeilen
%args=();
foreach (split(/\&/,$query_string))
{
  if ( /^(\w+)=/ && ($key=$1) && ($value=$') )
  {
    $value =~ s/\+/ /go;
    $value =~ s/\%([0-9a-f]{2})/pack(C,hex($1))/eig;

   $args{"$key"}=$args{"$key"} . " " . $value if ($value!~/^\s*$/);
   $args{"$key"} =~ s/^\s*//o;
  }
}

$args{'INHALT'} =~ s/\n/<BR>\n/g; # Returns erhalten im HTML

# ***************************************************************************
# Web-page fuer den Betrachter kreieren mit ok-Meldung
# ***************************************************************************
print <<EOF;
<html><head><title>Pinwand</title></head>
<body TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000FF" VLINK="#FF00FF" ALINK="#FF0000">
<H1 ALIGN=CENTER>Danke</H1>
EOF

if ($args{NAME} ne "")
 { print "Lieber $args{NAME}"; print ",<P>" }
else
  { print "Lieber Anonymous (etwa sch&uuml;chtern? :-), <P>" }
print "Dein Beitrag wurde in die Pinwand aufgenommen und " ;
print "kann sofort gelesen werden. <P>\n" ;


print <<EOF;
</body>
</html>
EOF

#
# neues File anlegen
#
if ( open (OUT, ">>" . $orig) )
  {
  flock (OUT, $exlock);
  print OUT "<hr><p>" ;
  if ($args{'HEADLINE'} ne "")
   { print OUT "<h3 align=center> $args{'HEADLINE'} </h3>"; };
  print OUT "$args{'INHALT'}";
  print OUT "<p><blockquote><i><b>";
    
  # emailadresse angegeben
  if ($args{'EMAIL'} ne "")
    {
     print OUT "E-Mail: <a href=\"mailto:$args{'EMAIL'}\">";
     ## kein Name angegeben
     if ($args{'NAME'} eq "")
       { print OUT "$args{'EMAIL'}"; }
     else 
       { print OUT "$args{'NAME'}"; }
     print OUT "</a><br>";
    }
  # URL angegeben
  if ($args{'URL'} ne "")
    {
     print OUT "URL: <a href=\"$args{'URL'}\">";
     # kein Name angegeben
     if ($args{'NAME'} eq "")
       { print OUT "$args{'URL'}"; }
     else 
       { print OUT "$args{'NAME'}"; }
     print OUT "</a><br>";
    }
  print OUT "Posted from $remotehost ";
  if ($remoteaddr ne "")
   { print OUT "($remoteaddr) "; } ;
  print OUT "at ($time)</i></b></blockquote><p>\n";
  flock (OUT, $unlock);
  close OUT;
  }
else
  { print "Cannot write to data file $orig." };
exit 0

