#!/usr/bin/perl
# Don't alter the line just above...
#
# libinstructfys.pl - perl script for submission of a First Year Seminar
# request to libinst@isu.edu and Spencer Jardine jardspen@isu.edu
# from fysinstructreqform.htm
#
# created 07/21/2008 by Kent Kearns
# last edit 04/11/2013 KK
#
# script checks that all required fields have some data entry in them before
# emailing the request
#
# Make your input form in your HTML document refer to this program:
# This form-processing program assumes that your form has text boxes in which 
# the user enters their preferred method of contact, phone or email (assigned
# to variable 'contact'), required; his/her name (assigned to variable
# 'instructor'), required; the name of another instructor if applicable
# (assigned to variable 'otherinstructor'), not required; course section
# (assigned to variable 'coursesection'), required; phone number (assigned to
# variable 'phone'), required; email address (assigned to variable 'email'),
# required; class size (assigned to variable 'csize'), required; class begins
# (variable to 'classbegin'), required; AM or PM (assigned to variable
# 'clbegin'), required; class ends (assigned to variable 'classends'), required;
# AM or PM (assigned to variable 'clend'), required; date (assigned to variable
# 'date'), required; alternate date (assigned to variable 'altdate'), required;
# assignment (assigned to variable 'assign'), not required; whether Plagiarism
# was selected (assigned to variable 'plagiarism'), not required; whether tour
# was selected (assigned to variable 'tour'), not required; whether find a book
# was selected (assigned to variable 'findbook'), not required; whether Basic
# search was selected (assigned to variable 'search'), not required;whether
# literacy was selected (assigned to variable 'literacy'), not required;
# whether quiz was selected (assigned to variable 'quiz'), not required;

############################################################
# Define recipient of email message.
# You must provide an email address for the recepient of postings
# (i.e. who is the resulting email message being sent to?)
# Insert their address between the single quotes below.
############################################################

$recipient ='libinst@isu.edu';
# $recipient ='gowemich@isu.edu';

############################################################
# Don't alter anything in this section unless you know how to use perl!
#
# HTML forms spit out their contents in a long ugly string like:
#  user_name=Joe+Doofus&email1=doofjoe@isu.edu&...
#
# What this section does is create a perl associative array of
# variable name/value pairs from that long, ugly string.
#
# Afterwards, the value of any item from your form can then be referenced by
#   $FORM{'variable_name'}
# where variable_name is the NAME= value assigned to a form's component.
############################################################

# read in raw input from the form.
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

# Split out the name-value pairs by & seperator, and load into array
@pairs = split(/&/, $buffer);

foreach $pair (@pairs)
{
    # split name-value pairs by = sign
    ($name, $value) = split(/=/, $pair);

    # Un-Webify plus signs and %-encoding
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $name =~ tr/+/ /;
    $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

    # load name-value pairs into associative arrays, indexed by name
    $FORM{$name} = $value;
}

############################################################
# GENERAL INFO ABOUT PRINT COMMANDS
############################################################
# Sections between 
# 	print<<EOF;
# and 
# 	EOF
# get sent back to the browser. You are creating an HTML document 'on the fly',
# and you should send HTML code, not just plain text.
#
# Sections between
# 	print MAIL<<EOF;
# and
# 	EOF
# get printed to the email message generated by this script.
# The email message should be in plain text with no HTML code.
#
# You can print the values that were assigned to variables by including
#  $FORM{'variable_name'} in the print string, where variable_name is 
#  the name of any variable to which values were assigned in your form.
############################################################

# Print out a content-type for HTTP/1.0 compatibility: this tells user's browser
# that HTML code follows. Don't alter this line.

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

############################################################
# Check for unfilled form values and respond to user.
# The following code rejects the submission unless required fields are filled.
# The user is told to go back to the form and fill in the required fields.
#
# The conditions
# 	if (! $FORM{'variable_name'}) {
# will be true if the variable was not assigned by the form (i.e. if the
# form's field was left blank.
#
# Code is provided to check that the variables 'instructor', 'phone' 
# and other fields are filled in, otherwise you won't know who submitted
# the message. This provides no guarantee that the user will supply *accurate*
# information, though.
#
# You may repeat the section from 'elsif' to it's closing brace, substituting
# other variable names and messages, if you want to check more fields.
############################################################

if (! ($FORM{'contact'})) {
print<<EOF; # print everything until EOF is encountered
<HTML><HEAD><TITLE>FORM ERROR!</TITLE></HEAD>
<BODY><H2>Preferred method of contact is needed!</H2><HR>
You must fill out the field marked "Preferred method of contact".
<HR>Click your browser's "BACK" button to go back to
the form and fill out all the required fields. Thank you.<HR>
</BODY></HTML>
EOF
exit; # exit this program after printing above error message
} # end if

elsif (! ($FORM{'instructor'})) {
print<<EOF; # print everything until EOF is encountered
<HTML><HEAD><TITLE>FORM ERROR!</TITLE></HEAD>
<BODY><H2>Instructor name is needed!</H2><HR>
You must fill out the field marked "Instructor".
<HR>Click your browser's "BACK" button to go back to
the form and fill out all the required fields. Thank you.<HR>
</BODY></HTML>
EOF
exit; # exit this program after printing above error message
} # end elsif

elsif (! ($FORM{'coursesection'})) {
print<<EOF;
<HTML><HEAD><TITLE>FORM ERROR!</TITLE></HEAD>
<BODY><H2>Course title and number is needed!</H2><HR>
You must fill out the field marked "Course Title and #".
<HR>Click your browser's "BACK" button to go back to
the form and fill out all the required fields. Thank you.<HR>
</BODY></HTML>
EOF
exit; # exit this program after printing above error message
} # end elsif

elsif (! ($FORM{'phone'})) {
print<<EOF;
<HTML><HEAD><TITLE>FORM ERROR!</TITLE></HEAD>
<BODY><H2>Phone is needed</H2><HR>
You must fill in the field marked "Phone".
<HR>Click your browser's "BACK" button to go back to
the form and fill out all the required fields. Thank you.<HR>
</BODY></HTML>
EOF
exit; # exit this program after printing above error message
} # end elsif

elsif (! ($FORM{'email'})) {
print<<EOF;
<HTML><HEAD><TITLE>FORM ERROR!</TITLE></HEAD>
<BODY><H2>Email is needed</H2><HR>
You must fill in the field marked "Email".
<HR>Click your browser's "BACK" button to go back to
the form and fill out all the required fields. Thank you.<HR>
</BODY></HTML>
EOF
exit; # exit this program after printing above error message
} # end elsif

elsif (! ($FORM{'csize'})) {
print<<EOF;
<HTML><HEAD><TITLE>FORM ERROR!</TITLE></HEAD>
<BODY><H2>Class size is needed</H2><HR>
You must fill in the field marked "Class Size".
<HR>Click your browser's "BACK" button to go back to
the form and fill out all the required fields. Thank you.<HR>
</BODY></HTML>
EOF
exit; # exit this program after printing above error message
} # end elsif

elsif (! ($FORM{'classbegin'})) {
print<<EOF;
<HTML><HEAD><TITLE>FORM ERROR!</TITLE></HEAD>
<BODY><H2>Time the class begins is needed</H2><HR>
You must fill in the field marked "Class begins at".
<HR>Click your browser's "BACK" button to go back to
the form and fill out all the required fields. Thank you.<HR>
</BODY></HTML>
EOF
exit; # exit this program after printing above error message
} # end elsif

elsif (! ($FORM{'clbegin'})) {
print<<EOF;
<HTML><HEAD><TITLE>FORM ERROR!</TITLE></HEAD>
<BODY><H2>Time the class begins is needed</H2><HR>
We need to know whether the class begins in the morning or afternoon.
Please select AM or PM in the field next to "Class begins at".
<HR>Click your browser's "BACK" button to go back to
the form and fill out all the required fields. Thank you.<HR>
</BODY></HTML>
EOF
exit; # exit this program after printing above error message
} # end elsif

elsif (! ($FORM{'classend'})) {
print<<EOF;
<HTML><HEAD><TITLE>FORM ERROR!</TITLE></HEAD>
<BODY><H2>Time the class ends is needed</H2><HR>
You must fill in the field marked "Class ends at".
<HR>Click your browser's "BACK" button to go back to
the form and fill out all the required fields. Thank you.<HR>
</BODY></HTML>
EOF
exit; # exit this program after printing above error message
} # end elsif

elsif (! ($FORM{'clend'})) {
print<<EOF;
<HTML><HEAD><TITLE>FORM ERROR!</TITLE></HEAD>
<BODY><H2>Time the class ends is needed</H2><HR>
We need to know whether the class ends in the morning or afternoon.
Please select AM or PM in the field next to "Class ends at".
<HR>Click your browser's "BACK" button to go back to
the form and fill out all the required fields. Thank you.<HR>
</BODY></HTML>
EOF
exit; # exit this program after printing above error message
} # end elsif

elsif (! ($FORM{'date'})) {
print<<EOF;
<HTML><HEAD><TITLE>FORM ERROR!</TITLE></HEAD>
<BODY><H2>Date of the class is needed</H2><HR>
You must fill in the field marked "Date(s) Desired".
<HR>Click your browser's "BACK" button to go back to
the form and fill out all the required fields. Thank you.<HR>
</BODY></HTML>
EOF
exit; # exit this program after printing above error message
} # end elsif

elsif (! ($FORM{'altdate'})) {
print<<EOF;
<HTML><HEAD><TITLE>FORM ERROR!</TITLE></HEAD>
<BODY><H2>An alternate date for the class is needed</H2><HR>
You must fill in the field marked "Alternate Date(s)".
<HR>Click your browser's "BACK" button to go back to
the form and fill out all the required fields. Thank you.<HR>
</BODY></HTML>
EOF
exit; # exit this program after printing above error message
} # end elsif

# We wouldn't get this far unless all required fields had input.

# Print a title and initial heading to user's browser. Modify at will 
# between print<<EOF; and EOF

print<<EOF;
<Head><Title>Library Instruction Request FYS Form</Title></Head>
<Body><H2>Library Instruction Request FYS Form</H2><HR>
Your request has been sent to the ISU Oboler Library Instruction Staff.
You will be contacted within three business days by phone or email to confirm
the session date and time.
<p>
<b>Please submit a separate request for each class/section.</b>
If you need to request another Library Instruction FYS session, click your
browser's "BACK" button to return to the form.  The information from your
previous request will be filled in, so be sure to update all the information
for the <b>new request</b>.
<HR>Return to <A HREF="http://www.isu.edu/library/">Library</A> home page
<HR></BODY></HTML>
EOF

################################
# Create email message
################################

# Start creating email message by piping output directly to sendmail program. 
# Don't alter this line.
open (MAIL, "|/usr/lib/sendmail $recipient") || die "Can't open sendmail!\n";

# Write email headers. The From: line of the outgoing email message will
# be automatically set to the user that the web server is running as, 
# not the original sender. You can't control the FROM line.
#
# Sender must enter his/her email address in a form field named 'email', 
# and that gets assigned to the Reply-To: field in the email message.
#
# In the space between the dashed lines, you can specify any text output to 
# the mail message.
#
# If you want to include the values of any variables sent from your form, 
# refer to them by $FORM{'variable-name'}
#
# The blank line after the SUBJECT: line is necessary, so don't delete it.
#
# You may remove the stuff between the 2nd dashed line and EOF if you wish:
# those lines print out information about where the form was posted from.
# You may also remove the dashed lines if you don't want them in your email
# message.

print MAIL<<EOF;
Reply-to: $FORM{'email'}
From: fysinstrrequest@isu.edu
Subject: Library Instruction Menu Request

Preferred method of Contact: $FORM{'contact'}

Instructor: $FORM{'instructor'}

Other Instructor: $FORM{'otherinstructor'}
  
Course Section: $FORM{'coursesection'}
    
Phone: $FORM{'phone'}
  
Email: $FORM{'email'}
  
Class size: $FORM{'csize'}
  
Class begins: $FORM{'classbegin'} $FORM{'clbegin'}
  
Class ends: $FORM{'classend'} $FORM{'clend'}
  
Date desired: $FORM{'date'}
  
Alternate date: $FORM{'altdate'}
  
Brief description of assignment: $FORM{'assign'}

Plagiarism Discussion (25 min.): $FORM{'plagiarism'}

Tour of the Library (25 min.): $FORM{'tour'}

Locate Book in the Library (25 min.): $FORM{'findbook'}

Basic Search Strategies (25 min.): $FORM{'search'}

Information-Literacy (50 min.): $FORM{'literacy'}

Jeopardy/Quiz Bowl (50 min.): $FORM{'quiz'}

EOF

# Close off the email message (which sends it) and exit.
close (MAIL);
exit;

