Приглашаем посетить
Спорт (sport.niv.ru)

WWWOrder

#!/usr/bin/perl
###########################################################
#                                                         #
# OrderForm.pl - Perl Script for Tabulating Online Orders #
#                                                         #
#     Written by Michael Hall - mikeh@isaac.biola.edu     #
#  Copyright ©1996 by Michael Hall, All Rights Reserved.  #
#                                                         #
###########################################################

###########################################################
# Define the following variables in your form:
#
# DataFile			-	Name of your orders data file that
#						contains item descriptions and
#						prices.
#
# OrderFile			-	Name of the file your orders will
#						be placed in.
#
# ErrorReturn		-	The URL to return to in case of an
#						error (required).
#
# ReturnURL			-	The URL to return to after
#						completing the order processing.
#
# Name				-	The name of the customer
#						(required).
# E-Mail			-	Customer's e-mail address
#						(required).
# Address1			-	Customer's address (required).
# Address2			-	Second Address line (optional).
# City				-	Customer's City (required).
# State				-	Customer's State (required).
# ZipCode			-	Customer's Zip Code (required).
# Phone				-	Customer's Phone Number
#						(required).
# SalesTax			-	Sales Tax (required).
# Shipping			-	Shipping Charges (optional).


###########################################################
# Edit the following variables below for your server's
# configuration.
#

$BaseDir = "/var/www/docs/scripts/";

$Sum = 0;
$Counter = 1;
@ItemDesc = "";
@ItemPrice = "";
$SumTax = 5;
$EMailOrders = 0; # Set this option to 1 if you want to have the orders e-mail. 0 if you just want a log.
$LogFile = 1; # Set this option to 1 if you want to write orders to log file. 0 if you don't.
$mailprog = '/usr/sbin/sendmail';
$EMail_Addr = "vmt@airmail.net"; # This is the address to e-mail the orders to.
$EMailReceipt = 1; # 1 = E-Mail Customer a receipt, 0 = Don't e-mail customer a receipt.
$StoreName = "The Scripts Home";
$RealName = "The Scripts Home";

$Header = <<'EOT';
<HTML>
<HEAD>
  <META NAME="GENERATOR" CONTENT="Adobe PageMill 2.0 Mac">
  <TITLE>The Scripts Home: WWWOrder</TITLE>
</HEAD>
<BODY BGCOLOR="#ffffff">

<P><IMG SRC="http://www.virtualcenter.com/scripts2/WebLogo.gif" ALT="The Scripts Home: A Free Source of Perl CGI Scripts for Interactive Web Pages"
BORDER="0" WIDTH="471" HEIGHT="102" NATURALSIZEFLAG="3" ALIGN="BOTTOM"></P>
EOT

$Footer = <<'EOT';
<P><B><HR ALIGN=LEFT><IMG SRC="http://www.virtualcenter.com/clip_art/logo.pl" ALIGN="LEFT">©1996
and Designed by <A HREF="http://www.virtualcenter.com/vmt/">Virtual Marketing
Technologies</A>. All Rights Reserved.<BR>
</BODY>
</HTML>
EOT

$PaymentInfo = <<'EOT';
Please make checks or money orders payable to Michael Hall and mail to:
                 Michael Hall
                 P.O. Box 592
                 DeSoto, TX  75123
                 
As soon as payment is received, you will receive an e-mail message telling you where you can download your scripts.

EOT

&UnWeb;

###########################################################
# Get the Date for Entry

$date = `date +"%A, %B %d, %Y at %T (%Z)"`;
        chop($date);
$shortdate = `date +"%D %T %Z"`;
        chop($shortdate);

###########################################################
# Check to see if the form was filled out completely.

if (($in{'Name'} eq "") || ($in{'E-Mail'} eq "") || ($in{'Address1'} eq "") || ($in{'City'} eq "") ||
	($in{'State'} eq "") || ($in{'ZipCode'} eq "") || ($in{'Phone'} eq "")) {
	
	# Something was left blank, return the error message and quit
	&PrintHeader;
	print "$Header\n";
	print "<h1>An Error Has Occurred</h1>\n";
	print "\n";
	print "<p>You did not fill in all of the fields required.  Please remedy this.<p>\n";
	print "Processed by Order Forms - A Perl Script to tabulate online orders.<br>\n";
	print "© 1996 by <a href=\"http://www.virtualcenter.com/scripts2\">Michael Hall</a>.  All Rights Reserved.\n";
	print "</body>\n";
	print "</html>\n";
	exit;
}


###########################################################
# Read in the data file

open(DATA,"$BaseDir$in{'DataFile'}");
@item = <DATA>;
chop @item;
close DATA;


###########################################################
# Tabulate the order


###########################################################
# Find out which items were ordered from the available
# selections.

$NumItems = @item; # Find out how many items were returned.
$NumStuff = $in{'NumChoices'};
@ItemDesc = "";
$Sum = 0;

$Counter1 = 1;
$Counter2 = 0;
@Sub = 0;
while ($Counter1 != ($in{'NumChoices'} + 1)) {
	if ($in{$Counter1} eq "Checked") {
		$ItemDesc[$Counter2] = $item[$Counter1 - 1];
		$ItemPrice[$Counter2] = $item[($Counter1 + $in{'NumChoices'}) - 1];
		if ($in{"Q$Counter1"} < ".01") {
			$ItemQty[$Counter2] = "1";
		}
		else {
			$ItemQty[$Counter2] = $in{"Q$Counter1"};
		}
		$ItemSub[$Counter2] = $ItemPrice[$Counter2] * $ItemQty[$Counter2];
		$Sub[$Counter2] = $item[($Counter1 + $in{'NumChoices'}) - 1];
		$Sum = $Sum + ($ItemPrice[$Counter2] * $ItemQty[$Counter2]);
		$Counter2++;
	}
	$Counter1 = $Counter1 + 1;
}

$Tax = $in{'Tax'} * $Sum;

###########################################################
# Add Sales Tax and Shipping Charges

$SubTotal = $Sum;
#$Tax = ($SubTotal * $in{'SalesTax'});
$Sum = $Sum + $Tax;

$Sum = $Sum + $in{'Shipping'};


###########################################################
# If desired, send the customer a receipt

if ($EMailReceipt) {
	open(MAIL, "|$mailprog -t") || die "Can't open $mailprog!\n";
	print MAIL "To: $in{'E-Mail'}\n";
	print MAIL "From: $EMail_Addr ($RealName)\n";
	print MAIL "Subject: $StoreName Order\n\n";
	print MAIL "Thank you for your recent purchase from $StoreName.  Your order has been received and will be processed soon.\n\n";
	print MAIL "Please look over the following information.  If it is incorrect, please reply to this e-mail and note any corrections.\n\n";
	print MAIL "The following order was received by $in{'Name'} on $shortdate:\n";
	print MAIL "------------------------------------------------------------\n";
	print MAIL "Name:            $in{'Name'}\n";
	print MAIL "Address:         $in{'Address1'}\n";
	print MAIL "                 $in{'Address2'}\n";
	print MAIL "City:            $in{'City'}\n";
	print MAIL "State:           $in{'State'}\n";
	print MAIL "Zip Code:        $in{'ZipCode'}\n";
	print MAIL "Phone:           $in{'Phone'}\n";
	print MAIL "Credit Card:     $in{'CreditCard'}\n";
	print MAIL "Number:          $in{'CardNo.'}\n";
	print MAIL "Expiration Date: $in{'ExpDate'}\n\n";
	$i = 0;
	foreach $ItemDesc (@ItemDesc) {
		print MAIL "$ItemDesc - $ItemQty[$i] - \$";
		printf MAIL ("%5.2f",$ItemSub[$i]);
		print MAIL "\n";
		$i++;
	}
	printf MAIL ("Sales Tax: \$%5.2f",$Tax);
	print MAIL "\n";
	printf MAIL ("Shipping: \$%5.2f",$in{'Shipping'});
	print MAIL "\n";
	printf MAIL ("Total: \$%5.2f",$Sum);
	print MAIL "\n\n";
	print MAIL "$PaymentInfo\n";
	close MAIL;
}

###########################################################
# If desired, send the order via e-mail

if ($EMailOrders) {
	open(MAIL, "|$mailprog -t") || die "Can't open $mailprog!\n";
	print MAIL "To: $EMail_Addr\n";
	print MAIL "From: $in{'E-Mail'} ($in{'Name'})\n";
	print MAIL "Subject: $StoreName Order\n\n";
	print MAIL "The following information was received from $in{'Name'} ($in{'E-Mail'}):\n";
	print MAIL "-------------------------------------------------------------------------------\n\n";
	print MAIL "$in{'Name'}, $in{'E-Mail'} - $shortdate\n";
	print MAIL "$in{'Address1'}\n";
	print MAIL "$in{'Address2'}\n";
	print MAIL "$in{'City'}, $in{'State'}  $in{'ZipCode'}\n";
	print MAIL "$in{'Phone'}\n";
	print MAIL "Credit Card: $in{'CreditCard'}, No.: $in{'CardNo.'}\n";
	print MAIL "Exp. Date: $in{'ExpDate'}\n\n";
	$i = 0;
	foreach $ItemDesc (@ItemDesc) {
		print MAIL "$ItemDesc - $ItemQty[$i] - \$";
		printf MAIL ("%5.2f",$ItemSub[$i]);
		print MAIL "\n";
		$i++;
	}
	printf MAIL ("Total: \$%5.2f",$Sum);
	print MAIL "\n";
	close MAIL;
}


###########################################################
# Check to see if the Orders File is locked

$quit = 0;
while ($quit != 1) {
	if (-e "$BaseDir.orderlock") {
		# The file is locked so take a short nap
		sleep(1);
	}
	else {


###########################################################
# Lock the Order File for Protection

		open(ORDERLOCK,">$BaseDir.orderlock");
		close ORDERLOCK;
	}


###########################################################
# Write the order to the orders file

	if ($LogFile) {
		open(ORDERFILE,">>$BaseDir$in{'OrderFile'}");
		print ORDERFILE "======================================================================\n";
		print ORDERFILE "$in{'Name'}, $in{'E-Mail'} - $shortdate\n";
		print ORDERFILE "$in{'Address1'}\n";
		if ($in{'Address2'} ne "") {
			print ORDERFILE "$in{'Address2'}\n";
		}
		print ORDERFILE "$in{'City'}, $in{'State'}  $in{'ZipCode'}\n";
		print ORDERFILE "$in{'Phone'}\n";
		print ORDERFILE "Username: $in{'Username'}, Password: $in{'Password'}\n";
		print ORDERFILE "Comments: $in{'Comments'}\n\n";
		$i = 0;
		foreach $ItemDesc (@ItemDesc) {
    		print ORDERFILE "$ItemDesc - $ItemQty[$i] - \$";
    		printf ORDERFILE ("%5.2f",$ItemSub[$i]);
    		print ORDERFILE "\n";
			$i++;
		}
		printf ORDERFILE ("Total: \$%5.2f",$Sum);
		print ORDERFILE "\n";
		close ORDERFILE;
	}
	
###########################################################
# Unlock the Order File Now That We're Done

unlink("$BaseDir.orderlock");
$quit = 1;
}


###########################################################
# Now Print the Receipt for the User

print "Content-type: text/html\n\n";
print "$Header\n";
print "<h1>Receipt for $in{'Name'}</h1><p>\n";
print "<hr><b>ORDERED BY:<br></b>\n";
print "<b>$in{'Name'} - $in{'E-Mail'}</b><br>\n";
print "<b>$in{'Address1'}</b><br>\n";
if ($in{'Address2'} ne "") {
	print "<b>$in{'Address2'}</b><br>\n";
}
print "<b>$in{'City'}, $in{'State'} $in{'ZipCode'}</b><br>\n";
print "<b>$in{'Phone'}</b><p><hr><p>\n";
print "<center><table BORDER=5 CELLSPACING=2 CELLPADDING=2>\n";
print "<tr><td><b>Item Description:</b></td><td><b>Price Each:</td><td><b>Quantity:</b></td><td><b>Price:</b></td></tr>\n";
$Counter = 1;
$i = 0;
foreach $ItemDesc (@ItemDesc) {
    print "<tr><td>$ItemDesc</td><td align=right>\$$Sub[$i]</td><td align=center>$ItemQty[$i]</td><td align=right>\$";
    printf ("%5.2f",$ItemSub[$i]);
    print "</td></tr>\n";
	$i++;
}
print "<tr></tr>\n";
print "<tr><td align=center><b>Sub-Total:</b></td><td align=right><b>\$";
printf ("%5.2f",$SubTotal);
print "</b></td></tr>\n";
print "<tr><td align=center><b>Sales Tax:</b></td><td align=right><b>\$";
printf ("%5.2f",$Tax);
print "</b></td></tr>\n";
print "<tr></tr>\n";
print "<tr><td align=center><b>Shipping:</b></td><td align=right><b>\$";
printf ("%5.2f",$in{'Shipping'});
print "</b></td></tr>\n";
print "<tr><td align=right><b>TOTAL DUE:</b></td><td align=right><b>\$";
printf ("%5.2f",$Sum);
print "</b></td></tr>\n";
print "</table></center><p>\n";
print "<hr><h2>Thank you for your order!</h2><p>\n";
print "If you did not include credit card information, your order will be sent as soon as payment is received.</b><p>\n";
print "Processed by WWWOrder v2.0 - © 1996 by <a href=\"http://www.virtualcenter.com/scripts2/index.html\">Michael Hall</a>.  All Rights Reserved.\n";
print "</body>\n </html>\n";

sub UnWeb {

   # Get the input
   read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

   # Split the name-value pairs
   @pairs = split(/&/, $buffer);

   foreach $pair (@pairs) {
      ($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/<!--(.|\n)*-->//g;

      if ($allow_html != 1) {
         $value =~ s/<([^>]|\n)*>//g;
      }
      else {
         unless ($name eq 'body') {
	    $value =~ s/<([^>]|\n)*>//g;
         }
      }

      $in{$name} = $value;
   }

}


#######################
# Print HTML Header

sub PrintHeader {

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