#!/usr/local/bin/perl
# -------------------------------------------------------------------------
# nominat.cgi - Based on the HTML file, C:\TRANSFER\NOMINAT.HTM
#        1. Please make sure that the control structure is not altered.
#        2. Save the edited file in text form.
#        3. Transfer this file to the WWW server in ASCII mode.
#        4. Set its protection to world read, execute (e.g. chmod 755 filename)
# -------------------------------------------------------------------------
# (To see if the script works as intended: Type 'thisfile.pl < test', where test is a text file containing
#  the item identifiers as, Item1=string&Item2=string&Item3=...
#  Alternatively, you may like to install the CGI and HTML and test via the form.)
# -------------------------------------------------------------------------

# -------------------------------------------------------------------------
# Start of the Perl script:
# -------------------------------------------------------------------------
# Procedure to send mail
sub do_mail
{
  local($filename) = "/tmp/cf-$$";
  open(TFILE,">$filename");

# >>>>> Start of User configurable section 1 of 2 >>>>>
# The mail subject will be at the top of the message.
   print TFILE "************************************************\n";
   print TFILE "Title: Happyclown Nomination Hall Submition Form\n";
   print TFILE "************************************************\n\n";

  if ($name ne "")
  {
   print TFILE "name:	$name\n";
   print TFILE "***************\n";
  }

  if ($description ne "")
  {
   print TFILE "description:\n";
   print TFILE "$description\n";
   print TFILE "***************\n";
  }

  if ($url ne "")
  {
   print TFILE "url:	$url\n";
  }

  close(TFILE);
 `/usr/bin/mail  $mailreceiver < $filename`;
  $mailstatus = $?;
  unlink("$filename");
}
# <<<<< End of User configurable section 1 of 2 <<<<<
# >>>>> Do not alter anything in the following section >>>>>
# convert %20 to a space and other %dd to a character
sub reformat
{
  local($tmp) = $_[0] ;
  $tmp =~ s/\+/ /g ;
  while ($tmp =~ /%([0-9A-Fa-f][0-9A-Fa-f])/)
{
      $num = $1;
      $dec = hex($num);
      $chr = pack("c",$dec);
      $chr =~ s/&/and/g;  # Replace if it is the & char.
      $tmp =~ s/%$num/$chr/g;
  }
  return($tmp);
}

# Main body of the script
sub do_main
{
  $cl = $ENV{'CONTENT_LENGTH'};
  if ($cl > 0)
  {
   read(STDIN, $_, $cl);
   $_ .= "&"; # Append an & char so that the last item is not ignored
   $pquery = &reformat($_);

   while ($pquery =~ /name=([^&]*)&/) {
      if (!$name) {
         $name = $1;
      }
      else {
         $name = $name."\n        ".$1;
      }
      $pquery =~ s/name=([^&]*)//;
   }
   $name = &reformat($name);

   while ($pquery =~ /description=([^&]*)&/) {
      if (!$description) {
         $description = $1;
      }
      else {
         $description = $description."\n        ".$1;
      }
      $pquery =~ s/description=([^&]*)//;
   }
   $description = &reformat($description);

   while ($pquery =~ /url=([^&]*)&/) {
      if (!$url) {
         $url = $1;
      }
      else {
         $url = $url."\n        ".$1;
      }
      $pquery =~ s/url=([^&]*)//;
   }
   $url = &reformat($url);

# <<<<< Do not alter anything in the above section <<<<<
# >>>>> Start of User configurable section 2 of 2 >>>>>
  print "Content-type: text/html\n\n";
  print "<HEAD>\n<TITLE>Your form has been sent...</TITLE>\n</HEAD>\n";
  print "<body bgcolor=#FFFFFF link=#408080 vlink=#FF8000 alink=#FF0000 text=#FF8000>\n";
  print "<font color=#FF0000><h1>Acknowledgement</h1></font>\n";
  print "<h2>Thank you for your participation.</h2><h3>Your nomination has been accepted.</h3>\n";
  print "<hr><h2>The following data has been sent. </h2>\n";
  print "<pre>";
  if ($name ne "")
  {
   print "Name:		: $name\n";
   print "<p align=center><b><font size=+4 color=#FF8000>....</font><font size=+4 color=#FF0000>..</font></b></p>\n";
  }

  if ($description ne "")
  {
   print "Description:\n";
   print "<blockquote>";
   print "$description\n";
   print "</blockquote>";
   print "<p align=center><b><font size=+4 color=#FF8000>....</font><font size=+4 color=#FF0000>..</font></b></p>\n";
  }

  if ($url ne "")
  {
   print "URL:		: $url\n";
  }

  print "</pre>";
  print "<hr>\n";
  &do_mail;
  print "<h3>Sent at $misterwolf from $ENV{'REMOTE_HOST'}</h3>\n";
   if ($mailstatus == 0)
    {
     print "<P><h3>Mail successfully sent.</h3>\n";
    }
    else
    {
     print "<P><h3><Blink>An error occurred when sending the mail. </Blink></h3><br>\n";
     print "The error status was $mailstatus).\n";
    }
print "<p align=center>\n";
print "<b><font size=+6 color=#FF0000>. . .</font></b><br>\n";
print "<font size=-1 color=#800040>\n";
print "<a href=http://www.happyclown.com/mainmenu.html>[Home]</a>\n";
print "<a href=http://www.happyclown.com/clown.html>[Happyclown]</a>\n";
print "<a href=http://www.happyclown.com/new.html>[New]</a>\n";
print "<a href=http://www.happyclown.com/projects.html>[Projects]</a>\n";
print "<a href=http://www.avic.org>[AVIC]</a>\n";
print "<a 
href=http://www.happyclown.com/links.html>[Links]</a></font></p>\n";

print "<p align=center><font color=#FF8000 size=-2>&copy; </font><font color=#FF0000 size=-2>Happyclown Inc.</font></p></body>\n";
  }
}
# <<<<< End of User configurable section 2 of 2 <<<<<

$misterwolf = `/bin/date`; $misterwolf =~ s/\n//g ;
$mailreceiver = "bignose\@happyclown.com";

$|=1;
eval '&do_main';
sleep(1);

# ***** End of the Perl Script file. *****
