#!/usr/bin/perl # # COPYRIGHT NOTICE # Copyright(c) 1999-2002 Lots of Bytes, LLC # # This program is protected by US copyright and international treaties and may not be copied or altered # without the express permission of Lots of Bytes, LLC. which reserves all rights. # # For more information, please contact kenji@lotsofbytes.com. # use lib "/forlifedesign.com/cart/cgi"; # $Id: cart.cgi,v 1.5 2006/09/05 22:48:14 kenji Exp $ $| = 1; # no buffering use CGI qw(:standard); ############################################# # global variables my $Mode = (param('mode') ne "") ? param('mode'): "add"; my %Cart = (); # current Cart require "local.cgi"; ############################################# # main SWITCH: { ($Mode eq 'version') && do { &Version; }; ($Mode eq 'add') && do { &Add; }; ($Mode eq 'view') && do { &View; }; ($Mode eq 'update') && do { &Update; }; ($Mode eq 'remove') && do { &Remove; }; if ($AFFILIATE_FLAG eq 'Y') { ($Mode eq 'a') && do {&Affiliate; }; } } # main end ############################################# sub Add { # security check CheckUrl(); # variable declarations my $cart_id = cookie(-name=>$COOKIE_NAME); my $filename = $CART_DIR."/tmp/".$cart_id; my $cookie; my $line_num = 0; my $sku = param('sku'); my @chars = ("A" .. "Z", 0 .. 9); if ($sku eq "") { &View; return; } # if nothing in cookie or a cart file doesn't exist, assign the new number if ($cart_id eq "" or !(-e $filename)) { srand(time()^($$+($$<15))); do { $cart_id = join("", @chars[ map {rand @chars} (1..12)]); } while (-e $CART_DIR."/tmp/".$cart_id); $cookie = cookie(-name=>$COOKIE_NAME, -value=>$cart_id) } # read the current cart $line_num = ReadCart($cart_id) if (!ref($cookie)); # add a new item to the cart my $new_name = param('name'); $new_name = "product name is missing" if ($new_name eq ""); $new_name .= " ".param('attr1') if (param('attr1') ne ""); $new_name .= " ".param('attr2') if (param('attr2') ne ""); $new_name .= " ".param('attr3') if (param('attr3') ne ""); my $new_price = param('price'); $new_price = 0 if ($new_price eq ""); Error("err_invalid_quantity") if ($new_price !~ /^[0-9.]+$/); my $new_weight = param('weight'); $new_weight = 0 if ($new_weight eq ""); Error("err_invalid_quantity") if ($new_weight !~ /^[0-9.]+$/); my $new_quantity = param('quantity'); $new_quantity = 1 if ($new_quantity eq ""); Error("err_invalid_quantity") if ($new_quantity !~ /^[1-9][0-9]*$/); my $done = 0; while (defined($Cart{$sku})) { # already in the basket, increment the quantity my ($name, $price, $weight, $quantity) = split (/\t/, $Cart{$sku}); if ($name eq $new_name) { $quantity += param('quantity'); $Cart{$sku} = join("\t", $name, $price, $weight, $quantity); $done = 1; last; } $sku .= "*"; } $Cart{$sku} = join("\t", $new_name, $new_price, $new_weight, $new_quantity) if (!$done); # append current data to the order file $line_num = WriteCart($cart_id); # display the cart &DisplayCart($cart_id, $cookie, $line_num); } ############################################# sub View { # variable declarations my $cart_id = cookie(-name=>$COOKIE_NAME); my $line_num = 0; # read the current cart $line_num = ReadCart($cart_id); # display the cart &DisplayCart($cart_id, "", $line_num); } ############################################# sub Update { # variable declarations my $cart_id = cookie(-name=>$COOKIE_NAME); my $line_num = 0; # read the current cart $line_num = ReadCart($cart_id); # verify the input values for ($i = 1; $i <= $line_num; $i++) { my $quantity = param('quantity'.$i); Error("err_invalid_quantity") if ($quantity !~ /^[1-9][0-9]*$/); } # update quantity for ($i = 1; $i <= $line_num; $i++) { my $sku = param('sku'.$i); my ($name, $price, $weight, $quantity) = split (/\t/, $Cart{$sku}); $quantity = param('quantity'.$i); if ($quantity <= 0) { delete($Cart{$sku}); } else { $Cart{$sku} = join("\t", $name, $price, $weight, $quantity) } } # write the updated data to the order file $line_num = WriteCart($cart_id); # display the order status page &DisplayCart($cart_id, "", $line_num); } ############################################# sub Remove { # variable declarations my $cart_id = cookie(-name=>$COOKIE_NAME); my $line_num = 0; # read the current cart $line_num = ReadCart($cart_id); my $sku = param('sku'); delete($Cart{$sku}); # write the updated data to the order file $line_num = WriteCart($cart_id); # display the order status page &DisplayCart($cart_id, "", $line_num); } ############################################# sub ReadCart { my ($cart_id) = @_; my $filename = $CART_DIR."/tmp/".$cart_id; my $line_num = 0; return 0 if ($cart_id eq ""); # empty cart %Cart=(); open CART, "<$filename" or Error("err_cantopen_file","can't open $filename"); while () { chomp(); $Cart{$1} = $2 if /^(.*?)\t(.*)$/; $line_num++; } close CART; return $line_num; } ############################################# sub WriteCart { my ($cart_id) = @_; my $filename = $CART_DIR."/tmp/".$cart_id; my $line_num = 0; open CART, ">$filename" or Error("err_cantopen_file","can't open $filename"); foreach $sku (sort keys %Cart) { print CART $sku."\t".$Cart{$sku}."\n"; $line_num++; } close CART; return $line_num; } ############################################# sub DisplayCart { my ($cart_id, $cookie, $line_num) = @_; if (!ref($cookie)) { print header(-type=>'text/html; charset=$CHARSET'); } else { print header(-type=>'text/html; charset=$CHARSET', -cookie=>$cookie); } my $affiliate = ""; if ($AFFILIATE_FLAG eq "Y") { $affiliate = cookie(-name=>'affiliate'); } # dispaly header message print $MSG{'cart_header'}; # check if the cart is empty if ($line_num == 0) { print $MSG{'cart_empty'}; print $MSG{'cart_footer'}; exit; } # display the cart my $i = 0; my $total = 0; foreach $sku (sort keys %Cart) { next if (!defined($Cart{$sku})); # skip a removed item $i++; my ($name, $price, $weight, $quantity) = split (/\t/, $Cart{$sku}); my $extended_price = $price*$quantity; # display the header if ($i == 1) { print <
END_HTML } my $sku_on_display = $sku; $sku_on_display =~ s/\*+$//; # remove the * marks my $price_currency = CurrencyFormat($price, 0); my $extended_currency = CurrencyFormat($extended_price, 0); # display each item in the basket print < END_HTML $total += $extended_price; } # for end if ($i > 0) { my $total_currency = CurrencyFormat($total, 1); # dipslay the footer print <
$MSG{'cart_title'}
$MSG{'sku'} $MSG{'product_name'} $MSG{'product_price'} $MSG{'product_quantity'} $MSG{'product_subtotal'}  
$sku_on_display $name $price_currency $extended_currency $MSG{'item_remove'}
$MSG{'total_amount'} $total_currency  
$MSG{'quantity_update_msg'}

$MSG{'checkout_button_before'}
END_HTML if ($AFFILIATE_FLAG eq "Y") { print "\n"; } print "
\n"; } # dispaly footer message print $MSG{'cart_footer'}; }