#!/usr/local/bin/perl # +---------------------------------------------+ # | order.cgi -- Order form CGI script | # | | # | Author : Ryan William Facer | # | Created On : Thursday May 21, 1998 | # | Completed On : | # | Last Modified By : Ryan William Facer | # | Last Modified On : Thursday January 7, 1999 | # | Update Count : 0 | # | E-Mail : cattails@cattails.ca | # +---------------------------------------------+ # ****************************************************************************** # Instructions # # Note: This script does provide error checking against invalid user input but # it doesn't provide error checking against the HTML code... So make sure # the HTML is set up correctly as per the instructions that (should) follow... # ****************************************************************************** $numdata = 9; # Variable holding the number of user data fields. $maxitemlength = 45; # Length in chars of longest item (used for formatting) $mailprog = '/usr/bin/mail -s "Order for the Body Break"'; # $mailprog is the mail program used by the system # ****************************************************************** # Confirm # # This subroutine is the main section of code run when the script is # in 'confirm' mode. # ****************************************************************** sub Confirm{ # ***************************************** # Print out the beginning stuff to tell the # server that we're sending it an HTML file # ***************************************** print "Content-type: text/html\n\n"; # **************************************************** # Print out the top of the HTML page that will be seen # **************************************************** print "\n"; print "Order Form\n"; print "\n\n"; # **************************************************** # As per the instructions, the first item on the array # will be the currency so we pop it off the array and # then we can use it for the tax calculations later on # **************************************************** $currency = pop(@in); ($name, $value) = split(/=/, $currency); $currency = $value; # ************************************************** # Now we get all the user data. We pop it all off of # the @in array and push it onto a new array called # @userdata. It will come off in the reverse order # that it appeared in the HTML file. Fields found to # be empty will be added onto an array called @empty # which will be examined later to check for errors. # ************************************************** # ************************************** # We store what the method of payment is # going to be in the variable $method # ************************************** $method = $in{'Method of Payment'}; if($method eq ''){ $method = 'Cheque'; push(@empty, 'Method of Payment'); }; # if # *********************************************** # We only go through the @in array $numdata times # which was defined up top to be the number of # data fields that a user can enter. We iterate # $numdata times popping off the the values. # *********************************************** for($i = 0; $i < $numdata; $i++){ $tempy = pop(@in); ($name, $value) = split(/=/, $tempy); if($value eq ''){ # ********************************************************** # This is where empty fields are added onto a list of items. # We won't add the item to the list if the item is an item # from a method of payment that wasn't selected. # ********************************************************** if($name eq 'Expiry Date'){ if($method eq 'Visa'){ push(@empty, $name); }; # if } elsif($name eq 'Credit Card Number'){ if($method eq 'Visa'){ push(@empty, $name); }; # if } elsif($name eq 'Telephone'){ if($method eq 'Visa by phone'){ push(@empty, $name); }; # if # ***************************************** # Any other field shouldn't be empty so # it is added onto the list of empty items. # ***************************************** } else { push(@empty, $name); }; # if } else { # ********************************************************** # This is where the user data is added onto a list of items. # We won't add the item to the list if the item is an item # from a method of payment that wasn't selected. # ********************************************************** if($name eq 'Expiry Date'){ if($method eq 'Visa'){ push(@userdata, $tempy); }; # if } elsif($name eq 'Credit Card Number'){ if($method eq 'Visa'){ push(@userdata, $tempy); }; # if } elsif($name eq 'Telephone'){ if($method eq 'Visa by phone'){ push(@userdata, $tempy); }; # if # ********************************* # Any other field should simply be # added onto the list of user data. # ********************************* } else { push(@userdata, $tempy); }; # if }; # if }; # for # ****************************************** # Now we check the @empty array for errors. # We store the number of items in the @empty # array in $length. If $length is greater # than 0, we know there are items that # shouldn't have been empty. # ****************************************** $length = @empty; if($length > 0){ print ""; print "

Body Break - Order Information"; print "

\n"; print "
\n"; print "

You didn't enter the following required fields of the order "; print "form:

\n"; print "\n"; print "

Press the back button on your browser, fill in the missing\n"; print "information, and resubmit the form.

\n"; print "
\n"; exit; }; # if # ************************************************** # Now we put in the hidden form to confirm the order # as well as print out the information so the user # can confirm that it's correct # ************************************************** print "
\n"; print "

Body Break - Order Submission

\n"; print "\n"; print "
\n"; print "
\n"; print "

\n"; # ****************************** # This is the outer table to # surround the order information # ****************************** print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "
Order "; print "Information:

\n"; # ********************** # Now we print the order # information in a table # ********************** print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; $total = 0; # This will be the running total of the purchases foreach $pair (@in){ ($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; # $value =~ s///g; # $value =~ s/<([^>]|\n)*>//g; # $value =~ s/<([^>]|\n)*>//g; if($value > 0){ # We ignore items ordered with quantity <= 0 if($name =~ /price/){ $price = $value; # We store prices in $price for the next pair } else { # After a price is the product so use it next time $subtotal = $price * $value; # Subtotal is (quantity x price) print "\n"; print "\n", $price); print ""; print "\n", $subtotal); # ************************************************************* # Hide a form field containing the product and quantity desired # ************************************************************* print "\n", $subtotal); $total = $total + $subtotal; # Sum up all the subtotals }; # if }; # if }; # foreach # Now we calculate the cost of shipping and store it in $shipping if($total < 74.85){ $shipping = 4.95; } elsif($total > 74.85){ $shipping = 7.95; } elsif($total = 74.85){ $shipping = 7.95; } else { $shipping = 6.95; }; # if print "\n"; print "\n"; print "\n", $total); print "\n", $total); print "\n"; print "\n", $shipping); print "
ProductPriceQuantitySubtotal
$name"; printf ("\$%.2f$value"; printf ("\$%.2f

"; print "Total before shipping"; printf ("\$%.2f
"; print "Shipping"; printf ("\$%.2f