#!/usr/bin/perl
# Don't alter the line just above...
#
# Anything after # in a line is ignored by perl.
#
# 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 his/her name (assigned to variable 'instructor')
# course title (variable 'course'); department (variable 'dept')
# phone number (variable 'phone'); email address (assigned to variable 'email')
# class size (variable 'csize'); class begins (variable 'classbegin')
# AM or PM (variable 'clbegin'); class ends (variable 'classends')
# AM or PM (variable  'clend'); date (variable 'date')
# alternate date (variable 'altdate'); assignment (variable 'assign') e.g.
#
#     <FORM METHOD="post" ACTION="/cgi-bin/libinstruct.pl">
# 	<INPUT NAME="contact">Preferred method of contact<br>
# 	<INPUT NAME="library">Library<br>
# 	<INPUT NAME="instructor">Instructor (your name)<br>
#	<INPUT NAME="course">Course Title and number<br>
#	<INPUT NAME="dept">Department<br>
#	<INPUT NAME="phone">Phone<br>
#	<INPUT NAME="email">Email<br>
#	<INPUT NAME="csize">Class Size<br>
#	<INPUT NAME="classbegin">Class begins at<br>
#	<INPUT NAME="clbegin">AM or PM<br>
#	<INPUT NAME="classend">Class ends at<br>
#	<INPUT NAME="clend">AM or PM<br>
#	<INPUT NAME="date">Date(s) Desired<br>
#	<INPUT NAME="altdate">Alternative Date(s)<br>
#	<INPUT NAME="assign">Briefly describe the research assignment<br>
#       </FORM>
# last modified 4/11/2013 kk

############################################################
# 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 select your "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{'library'})) {
print<<EOF; # print everything until EOF is encountered
<HTML><HEAD><TITLE>FORM ERROR!</TITLE></HEAD>
<BODY><H2>Library is needed!</H2><HR>
You must select a Library from the drop down list labeled "Library".
<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;
}

elsif (! ($FORM{'instructor'})) {
print<<EOF; # print everything until EOF is encountered
<HTML><HEAD><TITLE>FORM ERROR!</TITLE></HEAD>
<BODY><H2>Your name is needed!</H2><HR>
You must fill out the field marked "Your Name".
<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;
} 

elsif (! ($FORM{'course'})) {
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;
}

elsif (! ($FORM{'dept'})) {
print<<EOF;
<HTML><HEAD><TITLE>FORM ERROR!</TITLE></HEAD>
<BODY><H2>Department is needed</H2><HR>
You must fill in the field marked "Department".
<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;
} 

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;
}

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;
}

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;
}

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;
}

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;
}

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;
}

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;
}

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;
}

elsif (! ($FORM{'assign'})) {
print<<EOF;
<HTML><HEAD><TITLE>FORM ERROR!</TITLE></HEAD>
<BODY><H2>Assignment is needed</H2><HR>
We need a brief description of the research assignment for this session.  Please fill in a description in the text box "Briefly describe the research assignment".
<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;
} # 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 Form</Title></Head>
<Body><H2>Library Instruction Request 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 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: instrrequest@isu.edu
Subject: Request for Library Instruction


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

Library: $FORM{'library'}
________________________________________________
  
Instructor: $FORM{'instructor'}
________________________________________________
  
Course title and number: $FORM{'course'}
________________________________________________
    
Department: $FORM{'dept'}
________________________________________________
    
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'}

EOF

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

