#!/usr/bin/perl

# whatifreg.pl - WHATIF registration form
# 03082001

push(@INC, "/home/whatif/httpd/cgi-bin");
require 'cgi-lib.pl';

&ReadParse;
print &PrintHeader;

# Definieer de mail parameteres
#
$subject = "WHATIF aanvraag";
#$emailto = "maarten\@cmbi.ru.nl";
$emailto = "vriend\@cmbi.ru.nl";

$DASHLIN = "--------------------------------";

# Lees de waarde van de variabelen in
#
$title     = $in{'title'};
$firstname = $in{'firstname'};
$initials  = $in{'initials'};
$surname   = $in{'surname'};
$telephone = $in{'telephone'};
$fax       = $in{'fax'};
$email     = $in{'email'};
$emailnot  = $in{'emailnotify'};
$institute = $in{'institute'};
$workgroup = $in{'workgroup'};
$contact   = $in{'contact'};
$street    = $in{'street'};
$zipcode   = $in{'zipcode'};
$city      = $in{'city'};
$account   = $in{'account'};
$account_nr= $in{'account_nr'};
$conf      = $in{'conf'};

# Kompleteer het mail-subject
#
$subject = $subject . "[$institute $surname]";


# Test of de variabelen ingevuld zijn

if ( $initials eq "" )  {$initials_err = 1;}
if ( $firstname eq "" ) {$firstname_err = 1;}
if ( $surname eq "" )   {$surname_err = 1;}
if ( $telephone eq "" ) {$telephone_err = 1;}
if ( $email eq "" )     {$email_err = 1;}
if ( $institute eq "" ) {$institute_err = 1;}
if ( $workgroup eq "" ) {$workgroup_err = 1;}
if ( $contact eq "" )   {$contact_err = 1;}
if ( $street eq "" )    {$street_err = 1;}
if ( $zipcode eq "" )   {$zipcode_err = 1;}
if ( $city eq "" )      {$city_err = 1;}
if ( $account eq "" )   {$account_err = 1;}
if ( $account_nr eq "" ){$account_nr_err = 1;}

$allempty = $initials_err + $firstname_err + $surname_err + $telephone_err + $email_err;
$allempty = $allempty + $institute_err + $workgroup_err + $contact_err;
$allempty = $allempty + $street_err + $zipcode_err + $city_err;
$allempty = $allempty + $account_err + $account_nr_err + $package_err;
$allempty = $allempty + $title_err + $fax_err;

# test if the form is filled otherwise show the form

if ($allempty eq 13) {
	&displayform;
} else {

# Test of de variabelen "verboden" karakters bevatten.
	#
	if (index($title,"&") >= 0 || index($title,"+") >= 0 ) { $title_err = 2; }
	if (index($initials,"&") >= 0 || index($initials,"+") >= 0 ) { $initials_err = 2; }
	if (index($firstname,"&") >= 0 || index($firstname,"+") >= 0 ) { $firstname_err = 2; }
	if (index($surname,"&") >= 0 || index($surname,"+") >= 0 ) { $surname_err = 2; }
	if (index($telephone,"&") >= 0 || index($telephone,"+") >= 0 ) { $telephone_err = 2; }
	if (index($fax,"&") >= 0 || index($fax,"+") >= 0 ) { $fax_err = 2; }
	if (index($email,"&") >= 0 || index($email,"+") >= 0 ) { $email_err = 2; }
	if (index($email,"@") < 0 ) { $email_err = 3; }
	if (index($institute,"&") >= 0 || index($institute,"+") >= 0 ) { $institute_err = 2; }
	if (index($workgroup,"&") >= 0 || index($workgroup,"+") >= 0 ) { $workgroup_err = 2; }
	if (index($contact,"&") >= 0 || index($contact,"+") >= 0 ) { $contact_err = 2; }
	if (index($street,"&") >= 0 || index($street,"+") >= 0 ) { $street_err = 2; }
	if (index($zipcode,"&") >= 0 || index($zipcode,"+") >= 0 ) { $zipcode_err = 2; }
	if (index($city,"&") >= 0 || index($city,"+") >= 0 ) { $city_err = 2; }
	if (index($account,"&") >= 0 || index($account,"+") >= 0 ) { $account_err = 2; }
	
	$tot_err = $initials_err + $firstname_err + $surname_err + $telephone_err + $email_err;
	$tot_err = $tot_err + $institute_err + $workgroup_err + $contact_err;
	$tot_err = $tot_err + $street_err + $zipcode_err + $city_err;
	$tot_err = $tot_err + $account_err + $package_err;
	$tot_err = $tot_err + $title_err + $fax_err;
	
	if ( $tot_err != 0 ) {
	
	  print "<HEAD><TITLE>Error</TITLE></HEAD>";
	  print "<BODY  background=\"/whatif/back02.gif\"bgcolor='#FFFFFF'>";
	  print "<BR><H1>An error has occured, the form cannot be processed.</H1><BR>";
	  print "You haven't completed the form. Items are missing or contain illegal characters:";
	  print "<UL>";
	  if ( $initials_err != 0 ) {
	    if ( $initials_err == 1 ) { print "<LI> The Initials are missing."; }
	    if ( $initials_err == 2 ) { print "<LI> The Initials contain illegal characters."; }
	  }
	  if ( $firstname_err != 0 ) {
	    if ( $firstname_err == 1 ) { print "<LI> The First name is missing."; }
	    if ( $firstname_err == 2 ) { print "<LI> The First name contains illegal characters."; }
	  }
	  if ( $surname_err != 0 ) {
	    if ( $surname_err == 1 ) { print "<LI> The Surname is missing."; }
	    if ( $surname_err == 2 ) { print "<LI> The Surname contains illegal characters."; }
	  }
	  if ( $telephone_err != 0 ) {
	    if ( $telephone_err == 1 ) { print "<LI> The Telephone-number is missing."; }
	    if ( $telephone_err == 2 ) { print "<LI> The Telephone-number contains illegal characters."; }
	  }
	  if ( $email_err != 0 ) {
	    if ( $email_err  == 1 ) { print "<LI> The Email-address is missing."; }
	    if ( $email_err  == 2 ) { print "<LI> The Email-address contains illegal characters."; }
	    if ( $email_err  == 3) { print "<LI> The Email-address isn't valid"; }
	  }
	  if ( $institute_err != 0 ) {
	    if ( $institute_err == 1 ) { print "<LI> The Institute is missing."; }
	    if ( $institute_err == 2 ) { print "<LI> The Institute contains illegal characters."; }
	  }
	  if ( $workgroup_err != 0 ) {
	    if ( $workgroup_err == 1 ) { print "<LI> The Workgroup is missing."; }
	    if ( $workgroup_err == 2 ) { print "<LI> The Workgroup contains illegal characters."; }
	  }
	  if ( $contact_err != 0 ) {
	    if ( $contact_err == 1 ) { print "<LI> The Workgroup leader is missing."; }
	    if ( $contact_err == 2 ) { print "<LI> The Workgroup leader contains illegal characters."; }
	  }
	  if ( $street_err != 0 ) {
	    if ( $street_err == 1 ) { print "<LI> The Street is missing."; }
	    if ( $street_err == 2 ) { print "<LI> The Street contains illegal characters."; }
	  }
	  if ( $zipcode_err != 0 ) {
	    if ( $zipcode_err == 1 ) { print "<LI> The Zipcode is missing."; }
	    if ( $zipcode_err == 2 ) { print "<LI> The Zipcode contains illegal characters."; }
	  }
	  if ( $city_err != 0 ) {
	    if ( $city_err == 1 ) { print "<LI> The City is missing."; }
	    if ( $city_err == 2 ) { print "<LI> The City contains illegal characters."; }
	  }
	  if ( $account_err != 0 ) {
	    if ( $account_err == 1 ) { print "<LI> The Preferred-account is missing."; }
	    if ( $account_err == 2 ) { print "<LI> The Preferred-account contains illegal characters."; }
	  }
	  if ( $account_nr_err != 0 ) {
	  	if ( $account_nr_err == 1 ) { print "<LI> The WHATIF user number is not filled in."; }
	  }

	  if ( $title_err == 2 ) { print "<LI> The Title contains illegal characters."; }
	  if ( $fax_err == 2 ) { print "<LI> The Fax-number contains illegal characters."; }
	  print "</UL>";
	  print "Please, use the Back-button of your browser and fill in these fields.";
	  print "</BODY>";
	
	}else {
	
	  # Ga na van welke site de mail vandaan komt
	  #
	  $remotehost = $ENV{'REMOTE_HOST'};
	
	  # Bij onderstaande print-statement is de \n nodig om de source
	  # vanuit Netscape zelf goed te kunnen bekijken
	  #
	  print "<HTML>\n";
	  print "<HEAD>WHATIF registration</TITLE></HEAD>\n";
	  print "<BODY  background=\"/whatif/back02.gif\"bgcolor='#FFFFFF'>\n"; 
	  print "<TABLE>\n";
	  print "<TR><TH ALIGN='right'>Preferred login:</TH><TD>$account</TD></TR>\n";
	  print "<TR><TH ALIGN='right'>Title:          </TH><TD>$title</TD></TR>\n";
	  print "<TR><TH ALIGN='right'>Initials:       </TH><TD>$initials</TD></TR>\n";
	  print "<TR><TH ALIGN='right'>First name:     </TH><TD>$firstname<BR></TD></TR>\n";
	  print "<TR><TH ALIGN='right'>Surname:        </TH><TD>$surname<BR></TD></TR>\n";
	  print "<TR><TH ALIGN='right'>Street:         </TH><TD>$street</TD></TR>\n";
	  print "<TR><TH ALIGN='right'>Zipcode:        </TH><TD>$zipcode</TD></TR>\n";
	  print "<TR><TH ALIGN='right'>City:           </TH><TD>$city<BR></TD></TR><BR>\n";
	  print "<TR><TH ALIGN='right'>Telephone:      </TH><TD>$telephone</TD></TR>\n";
	  print "<TR><TH ALIGN='right'>Fax:            </TH><TD>$fax</TD></TR>\n";
	  print "<TR><TH ALIGN='right'>E-mail:         </TH><TD>$email<BR></TD></TR><BR>\n";
	  print "<TR><TH ALIGN='right'>Institute:      </TH><TD>$institute</TD></TR>\n";
	  print "<TR><TH ALIGN='right'>Workgroup:      </TH><TD>$workgroup</TD></TR>\n";
	  print "<TR><TH ALIGN='right'>Contact person: </TH><TD>$contact</TD></TR>\n";
	  print "<TR><TH ALIGN='right'>WHATIF number:  </TH><TD>$account_nr</TD></TR>\n";
	  print "</TABLE><HR>\n";
	
	 $createaccount = "title=$title&firstname=$firstname&initials=$initials&surname=$surname&telephone=$telephone&fax=$fax&email=$email&emailnot=$emailnot&institute=$institute&workgroup=$workgroup&contact=$contact&street=$street&zipcode=$zipcode&city=$city&account=$account&account_nr=$account_nr";
	$createaccount =~ s/ /\+/g;
	$createaccount =~ s/&/ /g;
	#$conf =1;### Debug
if ($conf == 0){
	  print "<H3>Please check your registration information above. To edit the fields please press the BACK button.<BR>\n";
	  print "By pressing the CONFIRM buttom, I confirm to have read the WHATIF legal information/end-user agreement,";
	  print " and acknowledge to adhere to the restrictions and obligations set forth within.<H3><BR>";

	  # Vraag om bevestiging

	  print "<FORM method=POST action='/cgi-bin/whatifreg.pl'>\n";
	  print "<INPUT type=\'hidden\' name=\'title\' value=\'$title\'>\n";
	  print "<INPUT type=\'hidden\' name=\'firstname\' value=\'$firstname\'>\n";
	  print "<INPUT type=\'hidden\' name=\'initials\' value=\'$initials\'>\n";
	  print "<INPUT type=\'hidden\' name=\'surname\' value=\'$surname\'>\n";
	  print "<INPUT type=\'hidden\' name=\'telephone\' value=\'$telephone\'>\n";
	  print "<INPUT type=\'hidden\' name=\'fax\' value=\'$fax\'>\n";
	  print "<INPUT type=\'hidden\' name=\'email\' value=\'$email\'>\n";
	  print "<INPUT type=\'hidden\' name=\'emailnotify\' value=\'$emailnot\'>\n";
	  print "<INPUT type=\'hidden\' name=\'institute\' value=\'$institute\'>\n";
	  print "<INPUT type=\'hidden\' name=\'workgroup\' value=\'$workgroup\'>\n";
	  print "<INPUT type=\'hidden\' name=\'contact\' value=\'$contact\'>\n";
	  print "<INPUT type=\'hidden\' name=\'street\' value=\'$street\'>\n";
	  print "<INPUT type=\'hidden\' name=\'zipcode\' value=\'$zipcode\'>\n";
	  print "<INPUT type=\'hidden\' name=\'city\' value=\'$city\'>\n";
	  print "<INPUT type=\'hidden\' name=\'account\' value=\'$account\'>\n";
	  print "<INPUT type=\'hidden\' name=\'account_nr\' value=\'$account_nr\'>\n";
	  print "<INPUT type=\'hidden\' name=\'conf\' value=1>\n"; 
	  print "<INPUT type=\'submit\' value=\'CONFIRM!\'>\n";
	  print "</FORM>\n";
	  print "</BODY>\n";
	  print "</HTML>\n";
}
else {
	  print "Your registration form will be processed.<BR>\n";
	  print "The account and password will be send to you by E-mail.<HR>\n";
	  # ga na of de .whatif_reg directory bestaat, zo niet maak deze
	  #
	
	  $direxists = chdir("/tmp/.whatifreg");
	  if ( $direxists == 0 ) {
	    system ( "mkdir /tmp/.whatifreg" );
	    system ( "chmod 750 /tmp/.whatifreg" );
	  }
	
	  $TMP = "/tmp/.whatifreg/whatifreg_$$";
	
	  # Save the data in a file for later processing...
	  #
	  open (TMPFILE, ">$TMP");
	  print TMPFILE "$DASHLIN" ."*". "$DASHLIN\n";
	  print TMPFILE "Preferred login:     $account\n";
	  print TMPFILE "WHATIF usernr:       $account_nr\n";
	  #
	  print TMPFILE "Title          :     $title\n";
	  print TMPFILE "Initials       :     $initials\n";
	  print TMPFILE "First name     :     $firstname\n";
	  print TMPFILE "Surname        :     $surname\n";
	  print TMPFILE "Street         :     $street\n";
	  print TMPFILE "Zipcode        :     $zipcode\n";
	  print TMPFILE "City           :     $city\n\n";
	  print TMPFILE "Telephone      :     $telephone\n";
	  print TMPFILE "Fax            :     $fax\n";
	  print TMPFILE "E-mail         :     $email\n\n";
	  print TMPFILE "E-mail notification: $emailnot\n";
	  #
	  print TMPFILE "Institute      :     $institute\n";
	  print TMPFILE "Workgroup      :     $workgroup\n";
	  print TMPFILE "Contact person :     $contact\n\n";
	  #
	  print TMPFILE "Remote host    :     $remotehost\n\n";
	  #
	  print TMPFILE "---run this line to --CREATE ACCOUNT---------------------\n";
	  print TMPFILE "/home/whatif/httpd/htdocs/whatif/5.0/adm/adduser.pl $createaccount\n";
	  
	  print TMPFILE "---------------------------------------------------------\n";
	  print TMPFILE "$DASHLIN" ."*". "$DASHLIN\n";
	  close TMPFILE;
	  system ("mail -s \"$subject\" $emailto < $TMP");
	  unlink $TMPFILE;

	}
	}
}

sub displayform {
	print "<HTML>\n";
	print "<HEAD>\n";
	print "<TITLE>\n";
	print "WHATIF Registration form\n";
	print "</TITLE>\n";
	print "</HEAD>\n";
	print "<BODY background=\"/whatif/back02.gif\">\n"; 
	#print "<img src=\"/pictures/sidebar.jpg\" height=1\% width=100\%>\n";
	#print "<img src=\"/pictures/gslogowhite.gif\" align=left>\n";
	print "<H4>WHATIF Registration form</H4>\n";
	print "The WHAT IF update is only accessible to valid WHAT IF users.<BR>\n";
	print "The user validity check is based on the combination institute - user number.<BR>\n";
	print "<STRONG>Forms without a valid user number will not be processed!</STRONG><BR>\n";
	print "<H3>Please read the <A HREF=\"/whatif/html/conditions.html\">legal information/end-user agreement<\/A>.<\/H3>\n";
	print "If requested, you will receive Email notification of future WHAT IF updates.<BR>\n";
	print "<A HREF=\"http://www.cmbi.kun.nl\">\n";
	print "<H5>Back to the CMBI homepage </H5>\n";
	print "</A>\n";
	print "\n";
	print "Questions? E-Mail us\n";
	print "<BR>\n";
	print "<A HREF=\"mailto:vriend\@cmbi.kun.nl?Subject=WHATIF Registration\">\n";
	#print "<img src=\"/pictures/emailicon.gif\" border=no>\n";
	print "</A>\n";
	print "<BR clear=left>\n";
	#print "<img src=\"/pictures/sidebar.jpg\" height=1\% width=100\%>\n";
	print "<BR>\n";
	print "<FORM METHOD=POST ACTION=\"/cgi-bin/whatifreg.pl\">\n";
	print "<TABLE ALIGN=\"CENTER\">\n";
	print "<TR><TH CLASS=\"centre-header\" COLSPAN=5>User of the account</TH></TR>\n";
	print "<TR>\n";
	print "   <TD ALIGN=\"RIGHT\">Title</TD>\n";
	print "   <TD COLSPAN=\"2\"><INPUT TYPE=TEXT NAME=\"title\" SIZE=14></TD>\n";
	print "   <TD COLSPAN=\"2\" ALIGN=\"RIGHT\">Initials\n";
	print "   <INPUT TYPE=\"TEXT\" NAME=\"initials\" SIZE=\"14\"></TD>\n";
	print "</TR>\n";
	print "<TR>\n";
	print "   <TD ALIGN=\"RIGHT\">First name</TD>\n";
	print "   <TD COLSPAN=\"4\"><INPUT TYPE=\"TEXT\" NAME=\"firstname\" SIZE=\"40\"></TD>\n";
	print "</TR>\n";
	print "<TR>\n";
	print "   <TD ALIGN=\"RIGHT\">Surname</TD>\n";
	print "   <TD COLSPAN=\"4\"><INPUT TYPE=\"TEXT\" NAME=\"surname\" SIZE=\"40\"></TD>\n";
	print "</TR>\n";
	print "<TR>\n";
	print "   <TD ALIGN=RIGHT>Street<SUP>*</SUP></TD>\n";
	print "   <TD COLSPAN=4><INPUT TYPE=TEXT NAME=\"street\" SIZE=40></TD>\n";
	print "</TR>\n";
	print "<TR>\n";
	print "   <TD ALIGN=RIGHT>Postcode<SUP>*</SUP></TD>\n";
	print "   <TD><INPUT TYPE=TEXT NAME=\"zipcode\" SIZE=7></TD>\n";
	print "   <TD COLSPAN=3 ALIGN=RIGHT>City<SUP>*</SUP>\n";
	print "   <INPUT TYPE=TEXT NAME=\"city\" SIZE=23></TD>\n";
	print "</TR>\n";
	print "<TR>\n";
	print "   <TD ALIGN=RIGHT>Telephone<SUP>*</SUP></TD>\n";
	print "   <TD COLSPAN=2><INPUT TYPE=TEXT NAME=\"telephone\" SIZE=14></TD>\n";
	print "   <TD COLSPAN=2 ALIGN=RIGHT>Fax<SUP>*</SUP>\n";
	print "   <INPUT TYPE=TEXT NAME=\"fax\" SIZE=14></TD>\n";
	print "</TR>\n";
	print "<TR>\n";
	print "   <TD ALIGN=RIGHT>E-mail<SUP>*</SUP></TD>\n";
	print "   <TD COLSPAN=3><INPUT TYPE=TEXT NAME=\"email\" SIZE=20></TD>\n";
	print "   <TD ALIGN=RIGHT>(*: at the institute)<BR></TD>\n";
	print "</TR>\n";
	print "<TR><TD>\n";
	print "<TD COLSPAN=3>\n";
	print "   <INPUT TYPE=CHECKBOX NAME=\"emailnotify\" VALUE=\"1\">\&nbsp; E-mail notification on updates<BR></TD>\n";
	print "</TD></TR>\n";
	print "<TR><TD>\&nbsp;</TD></TR>\n";
	print "<TR>\n";
	print "   <TH CLASS=\"centre-header\" COLSPAN=5>Affiliation</TH>\n";
	print "</TR>\n";
	print "<TR>\n";
	print "   <TD ALIGN=RIGHT>Institute</TD>\n";
	print "   <TD COLSPAN=4><INPUT TYPE=TEXT NAME=\"institute\" SIZE=40></TD>\n";
	print "  </TD>\n";
	print "</TR>\n";
	print "<TR>\n";
	print "   <TD ALIGN=RIGHT>Workgroup</TD>\n";
	print "   <TD COLSPAN=4><INPUT TYPE=TEXT NAME=\"workgroup\" SIZE=40></TD>\n";
	print "</TR>\n";
	print "<TR>\n";
	print "   <TD ALIGN=RIGHT>Workgroup leader</TD>\n";
	print "   <TD COLSPAN=4><INPUT TYPE=TEXT NAME=\"contact\" SIZE=40></TD>\n";
	print "</TR>\n";
	print "<TR><TD>\&nbsp;</TD></TR>\n";
	print "<TR>\n";
	print "   <TH CLASS=\"centre-header\" COLSPAN=5>Account information</TH>\n";
	print "</TR>\n";
	print "<TR>\n";
	print "   <TD ALIGN=RIGHT>Preferred login name</TD>\n";
	print "   <TD COLSPAN=2><INPUT TYPE=TEXT NAME=\"account\" SIZE=8></TD>\n";
	print "</TR>\n";
	print "<TR>\n";
	print "   <TD ALIGN=RIGHT>WHATIF user number</TD>\n";
	print "   <TD COLSPAN=2><INPUT TYPE=TEXT NAME=\"account_nr\" SIZE=8></TD>\n";
	print "   <TD>(<STRONG>Obligatory info!</STRONG> See your WHATIF bill for this)</TD>";
	print "</TR>\n";
	print "<TR><TD>\&nbsp;</TD></TR>\n";
	print "<TR>\n";
	print "   <TD COLSPAN=2> </TD>\n";
	print "   <TD> \&nbsp; \&nbsp; \&nbsp; \&nbsp; \&nbsp; \&nbsp; \&nbsp;\n";
	print "   \&nbsp; \&nbsp; \&nbsp; \&nbsp; \&nbsp; </TD>\n";
	print "   <TD COLSPAN=2> </TD>\n";
	print "</TR>\n";
	print "</TABLE>\n";
	print "<TABLE ALIGN=CENTER>\n";
	print "<TR>\n";
	print "<TD ALIGN=CENTER> <INPUT TYPE=SUBMIT NAME=\"register\" VALUE=\"submit\"> </TD>\n";
	print "<TD> your registration request to the <FONT COLOR=\"#0077EE\"><B>C</B></FONT><FONT COLOR=\"#9900FF\"><B>M</B></FONT><FONT COLOR=\"#DD0000\"><B>B</B></FONT><FONT COLOR=\"#EE9900\"><B>I</B></FONT>, or </TD>\n";
	print "</TR>\n";
	print "<TR>\n";
	print "<TD ALIGN=CENTER> <INPUT TYPE=RESET VALUE=\"reset\"> </TD>\n";
	print "<TD> all fields and start again. </TD>\n";
	print "</TR>\n";
	print "</TABLE>\n";
	print "</FORM>\n";
	print "</BODY>\n";
	print "</HTML>\n";
}


#--- einde programma ---
