#!/usr/local/bin/perl
require "timelocal.pl";

##################################################################
########     UPDATE THESE CAREFULLY FOR THIS YEAR:      ##########
##################################################################
$ConferenceYear="2003";
$workingdir='/www/faculty/copper/2003/INPUT';	# This Directory
$htmlform='/faculty/copper/2003/INPUT/';	# url which uses this form
$thisfile="register2003.cgi";
$CMtitle='CMCMM2003';		# CMCMM99, CMCIM2000, CMCMM2001...
$deadline = timelocal(1,0,0,2,1,$ConferenceYear-1900);	# "2,1"=Feb2
##################################################################
%prices =  (	"siam" => 330,		# before deadline
		"nonsiam" => 370,	# before deadline
		"student" => 100,	# before deadline
		"siam_late" => 370,	# after deadline
		"nonsiam_late" => 410,	# after deadline
		"student_late" => 100,	# after deadline
		);
##################################################################
$bgcolor="bgcolor=\"#009ebe\"";		# background color of webpages
$notify='copper@colorado.edu';		# email address
#$notify='vhenson@llnl.gov.';		# email address
$trouble='copper@colorado.edu';		# email in case this script dies
$logfile="./reg_Log";			# must be a chmod 666 text file
$sendmail = '/usr/lib/sendmail';	# the usual email sender
##################################################################


####################  REVISION INFORMATION  ######################
##################################################################
# Script to handle the Copper Mountain Conference registration form
# puts all information into a file and emails Cathy (copper)
#	
# REVISED Sept 2003 with 2003 conference details
# REVISED Jan  2001 with 2001 conference details
# REVISED Sept 1999 with 2000 conference details
# REVISED July 1998 with 1999 conference details
# REVISED October 1997 by Bruce.Fast@Colorado.EDU
#	1.  For perl5, every @ in a string (esp email addresses)
#		must be changed to \@ if inside "", but not if in ''
#	2.  Make $logfile year-specific
#	3.  Put year-specific stuff at top




@VarNames=();
%VarValues=();

#####################################################
##########  now process the form  ###################
#####################################################

chdir($workingdir);		# change to the working directory
select(STDOUT); $|=1;		# flush output after every write

# get the information
if ($ENV{'REQUEST_METHOD'} eq 'POST') {
	read(STDIN,$buffer,$ENV{'CONTENT_LENGTH'});
	} else {
	$buffer=$ENV{'QUERY_STRING'};
	}

# put the information in a form humans understand
&DecodeUrl($buffer);
#select(STDIN); $|=1;
#select(STDOUT); $|=1;

# tell the browser what we will return
print STDOUT "Content-Type: text/html\n\n";
print STDOUT "<!-- Virtual doc produced by $thisfile -->\n";
print STDOUT "<html>\n<head>\n";


$name=$VarValues{"name"};
$affiliation=$VarValues{"affiliation"};
$address=$VarValues{"address"};
$country=$VarValues{"country"};
$email=$VarValues{"email"};
$phonenum=$VarValues{"phonenum"};
$fee=$VarValues{"fee"}; if (time() > $deadline) { $fee .= '_late'; }
$method=$VarValues{"method"};
$creditcard=$VarValues{"creditcard"};
$cardnum=$VarValues{"cardnum"};
$cardexp=$VarValues{"cardexp"};


# do some checks that they filled out the form correctly
# this just checks that there is a name with letters in it and that we
# either a email address or a physical address

if (($name !~ /\w/) || !($email || $address))
{
    print STDOUT "<title>We need more information</title></head>\n";
    print STDOUT "<body $bgcolor>\n";
    print STDOUT "<h1>We need more information</h1>\n";
    print STDOUT "Please fill out the form completely.<p>\n";
    print STDOUT "We need, at the very least, your name and either your\n";
    print STDOUT "email address or your street address.<p>\n";
    print STDOUT "Try again, by backing up to the registration page\n";
    print STDOUT "and completing the missing entries.\n";
    print STDOUT "<hr>\n";
    print STDOUT "</body></html>\n";
    exit 1;
}

# if they're paying by credit card and
#   1. they didn't specify which credit card, or
#   2. they're giving info now, but failed to give info

if (($method eq "card")&&((!$creditcard)||($VarValues{'do_it_later'} eq "")&&
    (($cardnum !~ /[0-9]/)||(!$cardexp))))
{
    print STDOUT "<title>Not enough information</title></head>\n";
    print STDOUT "<body $bgcolor>\n";
    print STDOUT "<h1>Not enough Information</h1>\n";
    print STDOUT "You checked that you were going to pay by credit\n";
    print STDOUT "card, but you didn't:<p>\n<ul>\n";
    print STDOUT "<li>Tell which credit card you are using\n" if 
	!$creditcard;
    if ($VarValues{'do_it_later'} eq "")
    {
        print STDOUT "<li>Give a numeric credit card number\n" if $cardnum !~
    	    /\d/;
        print STDOUT "<li>Give an expiration date\n" if !$cardexp;
    }
    print STDOUT "</ul><p>Try <a href=\"$htmlform\">again</a>.\n<p><hr>\n";
    print STDOUT "</body></html>\n";
    exit 1;
}

$oldmask=umask(0177);
# open the logfile for appending
if (!open(LOG,">>$logfile"))
{
    print STDOUT "<title>Unexpected Error!</title></head>\n";
    print STDOUT "<body $bgcolor>\n";
    print STDOUT "<h1>Unexpected Error</h1>\n";
    print STDOUT "We experienced an unexpected error trying to open\n";
    print STDOUT "the file where we keep out records. Don't worry it\'s\n";
    print STDOUT "not your fault. Please <a href=\"$htmlform\">try again";
    print STDOUT "</a>.\n<p>If the same thing occurs again, please inform\n";
    print STDOUT "<a href=\"mailto:$trouble\">$trouble</a>.\n";
    print STDOUT "</body></html>\n";
    exit 1;
}

# flush the log file after every write 
select(LOG); $|=1; select(STDOUT);

# give em some feedback
print STDOUT "<title>What you told us</title><head>\n";
print STDOUT "<body $bgcolor>\n<ul>\n";
print STDOUT "<h1>You have been registered</h1>\n";
print STDOUT "We have you logged and you will be registered.<p>\n";
print STDOUT "Here's what you told us.<p>\n";

print LOG "\n------------------------------------------------------\n";
print LOG "---------> ",`date`; # automatic newline
print LOG "---------> Name: $name\n";
print STDOUT "<li>Name: <strong>$name</strong>\n";

print LOG "Email Address: $email\n";
print STDOUT "<li>Email Address: <strong>$email</strong>\n";

$tempadd=$address;
# replace newline with "tab newline"
$tempadd =~ s/\n/\n\t/g;
print LOG "Street Address:\n\t$tempadd\n";

$tempadd = $address;
# replace newlines with html newlines (<br>)
$tempadd =~ s/\n/<br>\n/g;
print STDOUT "<li>Street Address:<br>\n<strong>$tempadd</strong>\n";

print LOG "Phone Number: $phonenum\n";
print STDOUT "<li>Phone Number: <strong>$phonenum</strong>\n";

print LOG "Country: $country\n";
print STDOUT "<li>Country: <strong>$country</strong>\n";

print LOG "Affiliation: $affiliation\n";
print STDOUT "<li>Affiliation: <strong>$affiliation</strong>\n";

print LOG "Fee: ";
print STDOUT "<li>Fee: <strong>";
if ($fee =~ /^nonsiam/)
{
    print LOG "Non-SIAM Member";
    print STDOUT "Non-SIAM Member";
}
elsif ($fee =~ /^siam/)
{
    print LOG "SIAM Member";
    print STDOUT "SIAM Member";
}
elsif ($fee =~ /^student/)
{
    print LOG "Student";
    print STDOUT "Student";
}
print LOG " Late Registration" if ($fee =~ /_late$/);
print STDOUT " Late Registration" if ($fee =~ /_late$/);
print LOG " (\$$prices{$fee})\n";
print STDOUT " (\$$prices{$fee})</strong>\n";

# paying by check, tell em where to send it
if ($method eq 'check')
{
	print LOG "Paying by check or money order.\n";
	print STDOUT "<li>Paying by check or money order.\n";
	print STDOUT "<dl>\n";
	print STDOUT "<dd>Please make your check or money order to:\n";
	print STDOUT "<strong>Copper Mtn. Conference</strong><br>\n";
	print STDOUT "Please send check or money order to:\n";
	print STDOUT "<address>$CMtitle<br>Front Range Scientific Computing, Inc.,<br>\n";
	print STDOUT "1390 Claremont Drive<br>\n";
	print STDOUT "Boulder, CO  80303\n</address>\n</dl>\n";
}

# paying by credit card
elsif ($method eq 'card')
{
    print LOG "Paying by credit card: $creditcard\n";
    print STDOUT "<li>Paying by credit card:\n";
    print STDOUT "<strong>$creditcard</strong>\n<dl>\n";

    # they decided to give info now
    if ($VarValues{'do_it_later'} eq "")
    {
        print LOG "\tCard #: $cardnum\n";
        print STDOUT "<dd>Card #: $cardnum\n";
        print LOG "\tExp. Date: $cardexp\n";
        print STDOUT "<dd>Exp. Date: $cardexp\n</dl>\n";
    }
    # give info later, tell em where
    else
    {
	print LOG "\tSending information at a later time.\n";
	print STDOUT "<dd>Please send your credit card number and\n";
	print STDOUT "expiration date to:<br>\n";
	print STDOUT "<address>$CMtitle<br>CU-Boulder,\n";
	print STDOUT "Dept.Appl.Math.: CB 526<br>\n";
        print STDOUT "Boulder, CO  80309-0526<br>\n";
	print STDOUT "$notify</address>\n</dl>\n";
    }
}
print STDOUT "<p><hr>\n";
print LOG "======================================================\n";
close(LOG);

# set up to email -- put everything into a tmp file
# $tempfilename="/tmp/copper.reg.$$";

if (open(TEMP,"| $sendmail $notify"))
{
    # the header for the sendmail command
    print TEMP "To: $notify\n";
    print TEMP "Subject: Copper Mountain Registration on WWW\n\n";
    # the body of the email
    print TEMP "Name: $name\n";
    print TEMP "Email: $email\n" if $email;
    $tempadd = $address;
    $tempadd =~ s/\n/\n\t/g;
    print TEMP "Address:\n\t$tempadd\n" if $tempadd;

    print TEMP "Paying by: ";
    print TEMP "check" if $method eq 'check';
    print TEMP "credit card ($creditcard)" if $method eq 'card';

    print TEMP "\n\n";
    close(TEMP);
}

# email the tmpfile
# system("$sendmail $notify < $tempfilename");
# remove the tmp file
# unlink $tempfilename;

print STDOUT "</body></html>\n";
exit 0;

###################################################################

sub DecodeUrl
{
    local($str) = $_[0]; # don't modify original string
    local(@entries,$val);

    %VarValues=(); @VarNames=(); # clear any old values
    @entries = split("&",$str); # split into entries

    #print STDOUT "Num = ",$#entries,"\n";

    # go through each entry, split into varname and value, remove
    # +'s and hex codes
    for (local($i)=0;$i<=$#entries;$i++)
    {
        #print STDOUT 'entry=',$entries[$i],"\n";

        # split into name and value
        ($VarNames[$i],$val) = split("=",$entries[$i]);

        #print STDOUT "name=",$names[$i]," val=",$val,"\n";

        $val =~ s/\+/ /g; # replace +'s with spaces
        # this is why we love perl--replace hex codes w/ ascii char
        # there's probably a better way to do this, but this seems to
        # work pretty well.
        1 while $val =~
            s/(.*)%([\da-fA-F]{2})(.*)/$val=sprintf("%s%c%s",$1,hex($2),$3)/eg;
	# get rid of ^M's
	$val =~ s/\r//g;
        $VarValues{$VarNames[$i]} = $val;

        #print STDOUT "-----------\n";
    }
    $i; # return the number of entries (not the highest index)
}

# $number = &GetNumVars($string);
sub GetNumVars
{
    local($str) = $_[0]; # don't modify original string
    local(@temp);
    @temp = split("&",$str); # split around &'s, into entries
    $#temp + 1; # returns the number elements in $temp
}
