#!/usr/bin/perl
##::: Author
##::: Hostname 		ux5.isu.edu
##::: Language		perl
##::: Libs/Deps
##:::
##::: Path/Filename	/cwis/www/cgi-bin/library-mail.pl 
##::: Path/Exe
##:::
##::: Project		Ask A Librarian Form
##::: SubProject
##::: Keywords		library, media center
##:::
##::: DateCreated
##:::
##::: ExeByWho		www
##::: ExeByWhat
##::: 
##::: Description: 
##:::
##:::
##:::
##:::
##::: ModDate   WhoBy     Tag  Description
##::: ========  ========  ==== ===============================================
##::: 00/00/02  NameAcct  NA01 template
##:::
##::: END   

# 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 'user_name')
# and email address (assigned to variable 'email1'). e.g.
#
#     <FORM METHOD="post" ACTION="/cgi-bin/library-mail.pl">
# 	<INPUT NAME="user_name">Please enter your name<br>
#	<INPUT NAME="email1">And your Email address.<br>
#       </FORM>

############################################################
# 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 ='refdesk@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 'user_name'
# and 'email1' 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{'user_name'})) {
print<<EOF;
<HTML><HEAD><TITLE>FORM ERROR!</TITLE></HEAD>
<BODY><H2>Your Name Needed</H2><HR>
You must fill out the field marked "Your Complete 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{'email1'})) {
print<<EOF; # print everything until EOF is encountered
<HTML><HEAD><TITLE>FORM ERROR!</TITLE></HEAD>
<BODY><H2>Your Email Address Needed!</H2><HR>
You must fill out the field marked "Email address".
<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{'status'})) {
print<<EOF;
<HTML><HEAD><TITLE>FORM ERROR!</TITLE></HEAD>
<BODY><H2>Your Status Needed</H2><HR>
You must make a selection in the field marked "Your User Status".
<HR>Click your browser's "BACK" button to go back to
the form and select "Your User Status". Thank you.<HR>
</BODY></HTML>
EOF
exit;
}

# hidden field, if it has anything in it, then it is most likely submitted by
# a bot and therefore will never get submitted 2-9-2012 kk
elsif (! (length($FORM{'subject'}) == 0)) {
exit;
} # end elsif

# added to generate a random number for subject line so that Google Mail
# doesn't aggregate Ask a Librarian emails into conversations

$rand_num1 = rand();

# 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>Ask a Librarian Form</Title></Head>
<Body><H2>Ask a Librarian Form</H2><HR>
Your request has been sent to the Oboler Library Reference Staff. Please 
be patient as we attempt to respond. Our email is checked 
each morning for Ask a Librarian email information requests.
<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 'email1', 
# 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{'email1'}
From: $FORM{'email1'}
Subject: Request from Ask a Librarian Form - $rand_num1

-------------------------------------------------------------
$FORM{'user_name'}
-------------------------------------------------------------
a person of status $FORM{'status'}
-------------------------------------------------------------
email $FORM{'email1'}
-------------------------------------------------------------
phone number $FORM{'phone'}
-------------------------------------------------------------
sent the following:
-------------------------------------------------------------
$FORM{'request'}   
-------------------------------------------------------------
EOF

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

