#!/usr/local/bin/perl
#
# register
#
# Description
#   Process the form for the registration and abstract submission to the
#   Como '98 CNOC 
#   It is a "free" re-interpretation of the C-shell program used for the
#   Rome Symposium on BeppoSAX/RXTE results by Angelo Antonelli at SDC.
#
# Author:               Mauro Orlandini <orlandini@tesre.bo.cnr.it>
# Revision:             $Revision: 0.8 $
# Last Changed by:      $Author: mauro $
# Last Changed on:      $Date: 98/08/10 08:39:08 $
#

# Initialization
$rev = '$Revision: 0.8 $';   $rev =~ s/\$//g;
$author = '$Author: mauro $';   $author =~ s/\$//g;
$author = substr($author, 8);
$upd_date = '$Date: 98/08/10 08:39:08 $';  $upd_date =~ s/\$//g;
$upd_date = substr($upd_date, 6);

# List of people that will receive the form submitted by any users
$recipients = "campana\@merate.mi.astro.it";
#$recipients = "campana\@merate.mi.astro.it";

# Mail program that is opened with a pipe (second pass)
$mailprog   = "/usr/lib/sendmail -t -oi -oem";
# Standard mail program
$mail = "/usr/ucb/mail";
# Where to store the form files
$Archive = "/buz1/campana/archive";

# You should not change anything below this line

# For debugging purpose
$debugging = 0;

# Read and parse the form input
&ReadParse;

# Prepare the output file
print &PrintHeader;

if ($debugging) {
  # Print all the fields
  print &PrintVariables(%in);
  exit;
}

# Check to see if first or second pass
goto SECONDPASS if $in{secondpass} =~ /true/;

# Test if the compulsory fields are OK
$in{email} =~ s/\s//g;
if ($in{firstname} eq "" || $in{name} eq "" || $in{email} !~ /\@/) {
  &printReport;
  print "You must fill at least the First name, family name and e-mail fields";
  &printReport_Close;
}

# Test if paying by cheque without bank and shipping date
if ($in{payment} eq "cheque" &&
    ($in{bank} eq "" || $in{shipping} eq "") ) {
  &printReport;
#  print "You chose to pay by cheque, so you must fill the Bank name ",
#	"and the shipping date of your cheque to the LOC";
  &printReport_Close;
}

# Test if contribution without title and abstract
if ($in{paper} =~ /y/ && (!$in{title} || !$in{abstract})) {
  &printReport;
  print "If you decided to contribute to the workshop<Br> you must fill in\n";
  print "<BLINK>BOTH</BLINK> the TITLE and ABSTRACT of your contribution";
  &printReport_Close;
}

if ($in{paper} =~ /no/) {
  $in{contrib} = $in{kind} = $in{title} = $in{authors} = $in{abstract} = "none";
} elsif ($in{paper} =~ /yes/) {
  if ($in{poster} !~ /poster|oral/i) {
  &printReport;
  print "If you decided to contribute to the workshop<Br> you must decide\n";
  print "whether to present an Oral or Poster contribution";
  &printReport_Close;

  }
  $in{contrib} = $in{paper};
  $in{kind} = $in{poster};
  $in{title} =~ s/[\cM]//g;
  $in{authors} =~ s/[\cM]//g;
  $in{abstract} =~ s/[\cM]//g;
}
  
# Counts words in the abstract
$counts = split(" ", $in{abstract});

# Put everything in a temporary file
$tmp = "/tmp/$$.symp";
$tmp_title = $in{title};   $tmp_authors = $in{authors};
$tmp_abs = $in{abstract};
open(TMP, ">$tmp") || die "Cannot open $tmp file: $!\n";
write TMP;
close(TMP);

# Here we prepare the confirmation form
print "<Html>\n<Head>\n<Title>Registration Form Confirmation</Title>\n";
print "</Head><Body BGCOLOR=\"#FFFFFF\" TEXT=\"#0000FF\">\n";
print "<Center><H1>Congresso nazionale oggetti compatti (CNOC)</H1>\n";
print "<P><H3>Registration Form Confirmation</H3></Center>\n";
print "<FORM method=POST ACTION=\"/cgi-bin/register.pl\">\n";
print "This is the content of the registration form that ",
      "we are going to receive.<Br>\n";
print "Please, check that all data are correct and confirm your registration\n";
print "by clicking on the confirmation button below.<Br>\n";
print "If you wish to change something just go back to the previous form,\n";
print "correct the text and press SUBMIT again. Thank you.<P><P><P>\n";
print "<INPUT TYPE=HIDDEN NAME=file VALUE=\"$tmp\">\n";
print "<INPUT TYPE=HIDDEN NAME=firstname VALUE=\"$in{firstname}\">\n";
print "<INPUT TYPE=HIDDEN NAME=name VALUE=\"$in{name}\">\n";
print "<INPUT TYPE=HIDDEN NAME=email VALUE=\"$in{email}\">\n";
print "<INPUT TYPE=HIDDEN NAME=secondpass VALUE=\"true\">\n";
print "<DL><DD><B>Cognome:</B> $in{firstname}\n";
print "<DD><B>Nome:</B> $in{name}\n";
print "<DD><B>Instituto:</B> $in{institution}\n";
print "<DD><B>Indirizzo:</B> $in{address}\n";
print "<DD><B>Codice postale:</B> $in{zip}\n";
print "<DD><B>Citta`:</B> $in{city}\n";
#print "<DD><B>Country:</B> $in{country}\n";
print "<DD><B>Telefono:</B> $in{tel}\n";
print "<DD><B>FAX:</B> $in{fax}\n";
print "<DD><B>e-mail:</B> $in{email}</DL><P><P>\n";
if ($in{payment} eq "transfer") {
#  print "You chose to pay the registration fee by bank transfer<P><P>\n";
} else {
#  print "You chose to pay the registration fee by a cheque issued to ",
#	"<I>Lidia Barbanera - Conto Gamma</I>.<Br>";
#  print "The bank issuing the cheque is <I>$in{bank}</I>\n";
#  print "and you shipped the cheque by courier on <I>$in{shipping}</I><P><P>\n";
}
if ($in{paper} =~ /yes/) {
  if ($in{poster} =~ /none/) {
    print "You did not specify the contribution type (oral/poster)!\n";
  } else {
    print "You will present the following $in{poster} contribution:\n";
    print "<DL><DT><B>Title:</B>\n";
    print "<DD>$in{title}\n<P>\n";
    if ($in{authors}) {
      print "<DT><B>Authorship:</B>\n";
      print "<DD>$in{authors}\n<P>\n";
    }
    print "<DT><B>Abstract:</B>\n";
    print "<DD>$in{abstract}\n";
    print "<DT><Blink><B>WARNING!</B></Blink>\n<DD>Your abstract exceedes ",
          "100 words (it is $counts words)\nand it could be truncated in ",
          "the processing. Please trim it to the 100 words limit.\n"
       if $counts > 100;
  }
}
print "</DL><P>\n";
print "<INPUT TYPE=\"submit\" VALUE=\"CONFIRM REGISTRATION\">\n";
print "</Form>\n<Br CLEAR=ALL>\n<P>\n";
print "<H6>This form has been generated by <I>register</I> $rev,",
      " last updated by $author on $upd_date</H6>\n";
#print "<Hr>\n<A HREF=\"/rome98/\">",
#      "<IMG SRC=\"/Gif/from_rg/go_prev_btn.gif\" Border=0>\n";
#print "Return to the Rome98 Page</A>\n";
print "</Body></Html>\n";

exit;

##############################################################################
#
# This section is performed for the second passage (authentication of
# subscription)
#
##############################################################################

SECONDPASS: 

# Mail the acknowledge to the user
open(MAIL, "| $mailprog") || die "Can't open mailprog $mailprog, stopped";
print MAIL <<"STOP";
To: $in{email}
From: campana\@merate.mi.astro.it
Subject: Registrazione al CNOC 

Dear Dr $in{firstname} $in{name},

    we have successfully received your registration form. Your e-mail has been
added to our mailing list. Thank you for your interest.

The Local Organizing Committee
STOP
close(MAIL) || die "An error occurred while processing your application.\n",
		   "The program has logged your input and the operator will ",
		   "contact you if he will not be able to rebuild your ",
		   "record.\n<P>Thank you for your patience\n";

# Acknowledge the submitter
&printReport;
print "Your form has been successfully submitted.\n",
      "See you in Como next May.\n";
print "</Center><P><P><Hr>\n";
#print "<A HREF=\"/rome98/\">",
#      "<IMG SRC=\"/Gif/from_rg/go_prev_btn.gif\" Border=0>\n";
print "Return to the CNOC registration page</A>\n";
print "</Body></Html>\n"; 

# Mail the form to the LOC
#system("$Mail -s \"Participation Form for $in{name} $in{firstname}\" $recipients < $in{file}");
system("$mail $recipients < $in{file}");

# Store the file in the archive
($inname = $in{name}) =~ tr/A-Z/a-z/;
$inname =~ s/\s//g;
($infirstname = $in{firstname}) =~ tr/A-Z/a-z/;
$infirstname =~ s/\s//g;
$newfile_name =  $inname . "." . $infirstname . "." . $$;
system("mv $in{file} $Archive/$newfile_name");

exit;

format TMP =
FIRSTNAME = @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            $in{firstname}
FNAME = @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        $in{name}
INSTITUTION = @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
	      $in{institution}
ADDRESS = @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
	  $in{address}
ZIP = @<<<<<<<<<<
      $in{zip}
CITY = @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
       $in{city}
COUNTRY = @<<<<<<<<<<<<<<<<<<<<<<<<
	  $in{country}
PHONE = @<<<<<<<<<<<<<<<<<<<
	$in{tel}
FAX = @<<<<<<<<<<<<<<<<<<<
      $in{fax}
E-MAIL = @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
	 $in{email}
DATE = late
PAYMENT = @<<<<<<<
	  $in{payment}
BANK = @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
       $in{bank}
SHIPPING = @<<<<<<<<<<<<<<<<<<<
	   $in{shipping}
CONTRIB = @<<<
	  $in{paper}
KIND = @<<<<<
       $in{poster}
TITLE = ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
	$tmp_title
~~      ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
	$tmp_title
AUTHORS = ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
          $tmp_authors
~~        ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
	  $tmp_authors
ABSTRACT = ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
	   $tmp_abs
~~	   ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
	   $tmp_abs
.

sub printReport {
  print "<Html>\n<Head>\n<Title>Registration Form Report</Title>\n";
  print "</Head><Body BGCOLOR=\"#FFFFFF\" TEXT=\"#0000FF\">\n";
  print "<Center><H1>CNOC</H1>\n";
  print "<P><H3>Registration Form Report</H3>\n";
}

sub printReport_Close {
  print "\n</Center><P><P>\n<Hr>\n";
#  print "<A HREF=\"/rome98/reg.html\">",
#	"<IMG SRC=\"/Gif/from_rg/go_prev_btn.gif\" Border=0>\n";
  print "Return to CNOC registration page</A>\n";
  print "</Body></Html>\n";
  exit;
}

########################## Taken from cgi-lib.pl ############################
# Perl Routines to Manipulate CGI input
# S.E.Brenner@bioc.cam.ac.uk
#
# Copyright 1993 Steven E. Brenner  
# Unpublished work.
# Permission granted to use and modify this library so long as the
# copyright above is maintained, modifications are documented, and
# credit is given for any use of the library.

# ReadParse
# Reads in GET or POST data, converts it to unescaped text, and puts
# one key=value in each member of the list "@in"
# Also creates key/value pairs in %in, using '\0' to separate multiple
# selections

# If a variable-glob parameter (e.g., *cgi_input) is passed to ReadParse,
# information is stored there, rather than in $in, @in, and %in.

sub ReadParse {
  if (@_) {
    local (*in) = @_;
  }

  local ($i, $loc, $key, $val);

  # Read in text
  if ($ENV{'REQUEST_METHOD'} eq "GET") {
    $in = $ENV{'QUERY_STRING'};
  } elsif ($ENV{'REQUEST_METHOD'} eq "POST") {
    for ($i = 0; $i < $ENV{'CONTENT_LENGTH'}; $i++) {
      $in .= getc;
    }
  } 

  @in = split(/&/,$in);

  foreach $i (0 .. $#in) {
    # Convert plus's to spaces
    $in[$i] =~ s/\+/ /g;

    # Convert %XX from hex numbers to alphanumeric
    $in[$i] =~ s/%(..)/pack("c",hex($1))/ge;

    # Split into key and value.
    $loc = index($in[$i],"=");
    $key = substr($in[$i],0,$loc);
    $val = substr($in[$i],$loc+1);
    $in{$key} .= '\0' if (defined($in{$key})); # \0 is the multiple separator
    $in{$key} .= $val;
  }

  return 1; # just for fun
}

# PrintHeader
# Returns the magic line which tells WWW that we're an HTML document

sub PrintHeader {
  return "Content-type: text/html\n\n";
}

# PrintVariables
# Nicely formats variables in an associative array passed as a parameter
# And returns the HTML string.

sub PrintVariables {
  local (%in) = @_;
  local ($old, $out);
  $old = $*;  $* =1;
  $output .=  "<DL COMPACT>";
  foreach $key (sort keys(%in)) {
    ($out = $in{$key}) =~ s/\n/<BR>/g;
    $output .=  "<DT><B>$key</B><DD><i>$out</I><BR>";
  }
  $output .=  "</DL>";
  $* = $old;

  return $output;
}

# PrintVariablesShort
# Nicely formats variables in an associative array passed as a parameter
# Using one line per pair (unless value is multiline)
# And returns the HTML string.

sub PrintVariablesShort {
  local (%in) = @_;
  local ($old, $out);
  $old = $*;  $* =1;
  foreach $key (sort keys(%in)) {
    if (($out = $in{$key}) =~ s/\n/<BR>/g) {
      $output .= "<DL COMPACT><DT><B>$key</B> is <DD><i>$out</I></DL>";
    } else {
      $output .= "<B>$key</B> is <i>$out</I><BR>";
    }
  }
  $* = $old;

  return $output;
}
