#!/usr/bin/perl
$| = 1;
$|++;
print "Content-type: text/html\n\n";
require "./Library2/web_store.setup.frames.javascript";
require "./Library2/pulldown_menu.txt";
require "./Library2/page_format.cfg";
require "./Library2/web_store_db_lib.pl";
require "./Library2/mail-lib.pl";
require "./Library2/web_store_html_lib.pl";
require "./Library2/credit_card_validation_lib.pl";
&ReadParse;
use DBI;
$page = $form_data{'page'};
$page =~ s/\0//g;
$form_data{'page'} =~ s/\0//g;
$search_request = $form_data{'search_request_button'};
$cart_id = $form_data{'cart_id'};
$sc_cart_path = "$sc_user_carts_directory_path/$cart_id.cart";
&error_check_form_data;
if ($cart_id eq "")
{
&delete_old_carts;
&assign_a_unique_shopping_cart_id;
}
$are_any_query_fields_filled_in = "no";
foreach $query_field (@sc_db_query_criteria) {
@criteria = split(/\|/, $query_field);
if ($form_data{$criteria[0]} ne "") {
$are_any_query_fields_filled_in = "yes";
}
}
if ($form_data{'add_to_cart_button'} ne "")
{
&add_to_the_cart;
exit;
}
elsif ($form_data{'modify_cart_button'} ne "")
{
&display_cart_contents;
exit;
}
elsif ($form_data{'change_quantity_button'} ne "")
{
&output_modify_quantity_form;
exit;
}
elsif ($form_data{'submit_change_quantity_button'} ne "")
{
&modify_quantity_of_items_in_cart;
exit;
}
elsif ($form_data{'delete_item_button'} ne "")
{
&output_delete_item_form;
exit;
}
elsif ($form_data{'submit_deletion_button'} ne "")
{
&delete_from_cart;
exit;
}
elsif ($form_data{'order_form_button'} ne "")
{
&require_supporting_libraries (__FILE__, __LINE__, "$sc_order_lib_path");
&display_order_form;
exit;
}
elsif ($form_data{'submit_order_form_button'} ne "")
{
&require_supporting_libraries (__FILE__, __LINE__, "$sc_order_lib_path");
&process_order_form;
exit;
}
elsif (($page ne "" || $form_data{'search_request_button'} ne ""
|| $form_data{'continue_shopping_button'}
|| $are_any_query_fields_filled_in =~ /yes/i) &&
($form_data{'return_to_frontpage_button'} eq ""))
{
&display_products_for_sale;
exit;
}
else
{
&output_frontpage;
exit;
}
#######################################################################
# Require Supporting Libraries. #
#######################################################################
sub require_supporting_libraries
{
local ($file, $line, @require_files) = @_;
local ($require_file);
foreach $require_file (@require_files)
{
if (-e "$require_file" && -r "$require_file") { require "$require_file"; }
else
{ print qq~I am sorry, there is a problem with your request. Please try again.~;
exit; }
} # End of foreach $require_file (@require_files)
} # End of sub require_supporting_libraries
#######################################################################
# Error Check Form Data. #
#######################################################################
sub error_check_form_data
{
foreach $file_extension (@acceptable_file_extensions_to_display)
{
if ($page =~ /$file_extension/ || $page eq "") { $valid_extension = "yes"; }
}
if ($valid_extension ne "yes")
{
print "$sc_page_load_security_warning";
&update_error_log("PAGE LOAD WARNING", __FILE__, __LINE__);
exit;
}
}
#######################################################################
# Delete Old Carts. #
#######################################################################
sub delete_old_carts
{
opendir (USER_CARTS, "$sc_user_carts_directory_path") ||&file_open_error("$sc_user_carts_directory_path", "Delete Old Carts", __FILE__, __LINE__);
@carts = grep(/\.cart/,readdir(USER_CARTS));
closedir (USER_CARTS);
foreach $cart (@carts)
{
if (-M "$sc_user_carts_directory_path/$cart" > $sc_number_days_keep_old_carts)
{
unlink("$sc_user_carts_directory_path/$cart");
}
}
} # End of sub delete_old_carts
#######################################################################
# Assign a Shopping Cart. #
#######################################################################
sub assign_a_unique_shopping_cart_id
{
if ($sc_shall_i_log_accesses eq "yes")
{
$date = &get_date;
&get_file_lock("$sc_access_log_path.lockfile");
open (ACCESS_LOG, ">>$sc_access_log_path");
@env_keys = keys(%ENV);
$new_access = "$date\|";
foreach $env_key (@env_keys)
{
$new_access .= "$ENV{$env_key}\|";
}
chop $new_access;
print ACCESS_LOG "$new_access\n";
close (ACCESS_LOG);
&release_file_lock("$sc_access_log_path.lockfile");
}
srand (time|$$);
$cart_id = int(rand(10000000));
$cart_id .= ".$$";
$sc_cart_path = "$sc_user_carts_directory_path/${cart_id}.cart";
$cart_count = 0;
while (-e "$sc_cart_path")
{
if ($cart_count == 3)
{
print "$sc_randomizer_error_message";
&update_error_log("COULD NOT CREATE UNIQUE CART ID", __FILE__,
__LINE__);
exit;
}
srand (time|$$);
$cart_id = int(rand(10000000));
$cart_id .= ".$$";
$cart_count++;
} # End of while (-e $sc_cart_path)
open (CART, ">$sc_cart_path") ||&file_open_error("$sc_cart_path", "Assign a Shopping Cart", __FILE__, __LINE__);
}
#######################################################################
# Output Frontpage. #
#######################################################################
sub output_frontpage { &display_page("$sc_store_front_path", "Output Frontpage", __FILE__, __LINE__); }
#######################################################################
# Add to Shopping Cart #
#######################################################################
sub add_to_the_cart
{
@items_ordered = keys (%form_data);
foreach $item (@items_ordered)
{
if (($item =~ /^item-/i ||
$item =~ /^option/i) &&
$form_data{$item} ne "")
{
$item =~ s/^item-//i;
if ($item =~ /^option/i){push (@options, $item);}
else
{
if (($form_data{"item-$item"} =~ /\D/) || ($form_data{"item-$item"} == 0))
{
&bad_order_note;
}
else
{
$quantity = $form_data{"item-$item"};
push (@items_ordered_with_options, "$quantity\|$item\|");
}
}
} # End of if ($item ne "$variable" && $form_data{$item} ne "")
} #End of foreach $item (@items_ordered)
foreach $item_ordered_with_options (@items_ordered_with_options)
{
# First, clear out a few variables that we are going to
# use for each item.
#
# $options will be used to keep track of all of the
# options selected for any given item.
#
# $option_subtotal will be used to determine the total
# cost of each option.
#
# $option_grand_total will be used to calculate the
# total cost of all ordered options.
#
# $item_grand_total will be used to calculate the total
# cost of the item ordered factoring in quantity and
# options.
$options = "";
$option_subtotal = "";
$option_grand_total = "";
$item_grand_total = "";
$item_ordered_with_options =~ s/~qq~/\"/g;
$item_ordered_with_options =~ s/~gt~/\>/g;
$item_ordered_with_options =~ s/~lt~/\Red
#
# This is the second option modifying item number 0001.
# When displayed in the display cart sscreen, it will read
# "Red 0.00, and will not affect the cost of the item.
($option_name, $option_price) = split (/\|/,$form_data{$option});
$options .= "$option_name $option_price,";
$unformatted_option_grand_total = $option_grand_total + $option_price;
$option_grand_total = &format_price($unformatted_option_grand_total);
} # End of if ($option_item_number eq "$item_id_number")
} # End of foreach $option (@options)
chop $options;
$options =~ s/,/, /g;
$item_number = &counter ($sc_counter_file_path, __FILE__, __LINE__);
$unformatted_item_grand_total = $item_price + $option_grand_total;
$item_grand_total = &format_price("$unformatted_item_grand_total");
foreach $field (@cart_row) { $cart_row .= "$field\|"; }
$cart_row .= "$options\|$item_grand_total\|$item_number\n";
} # End of foreach $item_ordered_with_options.....
open (CART, ">>$sc_cart_path") || &file_open_error("$sc_cart_path", "Add to Shopping Cart", __FILE__, __LINE__);
print CART "$cart_row";
close (CART);
if ($sc_use_html_product_pages eq "yes")
{
if ($sc_should_i_display_cart_after_purchase eq "yes") { &display_cart_contents; }
else { &display_page("$sc_html_product_directory_path/$page", "Display Products for Sale"); }
}
else
{
# this will show the cart if the itempur tag was not sent, which means there is no accessories for that product.
if (!$form_data{'item_pur'})
# if ($sc_should_i_display_cart_after_purchase eq "yes")
{
&display_cart_contents;
}
elsif ($are_any_query_fields_filled_in =~ /yes/i)
{
$page = "";
&display_products_for_sale;
}
else { &create_html_page_from_db; }
}
}
#######################################################################
# Output Modify Quantity Form #
#######################################################################
sub output_modify_quantity_form
{
&standard_page_header("Change Quantity");
&display_cart_table("changequantity");
&modify_form_footer;
}
#######################################################################
# Modify Quantity of Items in the Cart #
#######################################################################
sub modify_quantity_of_items_in_cart
{
@incoming_data = keys (%form_data);
foreach $key (@incoming_data)
{
if ((($key =~ /[\d]/) && ($form_data{$key} =~ /\D/)) ||
$form_data{$key} eq "0")
{
&update_error_log("BAD QUANTITY CHANGE", __FILE__, __LINE__);
&bad_order_note;
}
unless ($key =~ /[\D]/ && $form_data{$key} =~ /[\D]/)
{
if ($form_data{$key} ne "")
{
push (@modify_items, $key);
}
}
} # End of foreach $key (@incoming_data)
open (CART, "$sc_cart_path") || &file_open_error("$sc_cart_path", "Modify Quantity of Items in the Cart", __FILE__, __LINE__);
while ()
{
@database_row = split (/\|/, $_);
$cart_row_number = pop (@database_row);
push (@database_row, $cart_row_number);
$old_quantity = shift (@database_row);
chop $cart_row_number;
foreach $item (@modify_items)
{
if ($item eq $cart_row_number)
{
$shopper_row .= "$form_data{$item}\|";
foreach $field (@database_row) { $shopper_row .= "$field\|"; }
$quantity_modified = "yes";
chop $shopper_row; # Get rid of last pipe symbol but not the
# newline character
} # End of if ($item eq $cart_row_number)
} # End of foreach $item (@modify_items)
if ($quantity_modified ne "yes") { $shopper_row .= $_; }
$quantity_modified = "";
} # End of while ()
close (CART);
open (CART, ">$sc_cart_path") || &file_open_error("$sc_cart_path", "Modify Quantity of Items in the Cart", __FILE__, __LINE__);
print CART "$shopper_row";
close (CART);
&display_cart_contents;
} # End of if ($form_data{'submit_change_quantity'} ne "")
#######################################################################
# Output Delete Item Form #
#######################################################################
sub output_delete_item_form
{
&standard_page_header("Delete Item");
&display_cart_table("delete");
&delete_form_footer;
} # End of if ($form_data{'delete_item'} ne "")
#######################################################################
# Delete Item From Cart #
#######################################################################
sub delete_from_cart
{
@incoming_data = keys (%form_data);
foreach $key (@incoming_data)
{
unless ($key =~ /[\D]/)
{
if ($form_data{$key} ne "")
{
push (@delete_items, $key);
}
} # End of unless ($key =~ /[\D]/...
} # End of foreach $key (@incoming_data)
open (CART, "$sc_cart_path") || &file_open_error("$sc_cart_path", "Delete Item From Cart", __FILE__, __LINE__);
while ()
{
@database_row = split (/\|/, $_);
$cart_row_number = pop (@database_row);
$db_id_number = pop (@database_row);
push (@database_row, $db_id_number);
push (@database_row, $cart_row_number);
chop $cart_row_number;
$old_quantity = shift (@database_row);
$delete_item = "";
foreach $item (@delete_items)
{
if ($item eq $cart_row_number) { $delete_item = "yes"; }
} # End of foreach $item (@add_items)
if ($delete_item ne "yes") { $shopper_row .= $_; }
} # End of while ()
close (CART);
open (CART, ">$sc_cart_path") || &file_open_error("$sc_cart_path", "Delete Item From Cart", __FILE__, __LINE__);
print CART "$shopper_row";
close (CART);
&display_cart_contents;
} # End of if ($form_data{'submit_deletion'} ne "")
#######################################################################
# Display Products for Sale #
#######################################################################
sub display_products_for_sale
{
if ($sc_use_html_product_pages eq "yes")
{
if ($form_data{'search_request_button'} ne "")
{
&standard_page_header("Search Results");
require "$sc_html_search_routines_library_path";
&html_search;
&html_search_page_footer;
exit;
}
&display_page("$sc_html_product_directory_path/$page", "Display Products for Sale", __FILE__, __LINE__);
}
else { &create_html_page_from_db; }
}
&show_page_format;
#######################################################################
# display_cart_contents Subroutine #
#######################################################################
sub display_cart_contents
{
local (@cart_fields);
local ($field, $cart_id_number, $quantity, $display_number, $unformatted_subtotal, $subtotal, $unformatted_grand_total, $grand_total);
&standard_page_header("View/Modify Cart");
&display_cart_table("");
&cart_footer;
exit;
} # End of sub display_cart_contents
#######################################################################
# file_open_error Subroutine #
#######################################################################
sub file_open_error
{
local ($bad_file, $script_section, $this_file, $line_number) = @_;
&update_error_log("FILE OPEN ERROR-$bad_file", $this_file, $line_number);
&CgiDie ("I am sorry, but I was not able to access $bad_file in the
$script_section routine of $this_file at line number $line_number.
Would you please make sure the path is correctly defined in
web_store.setup and that the permissions are correct.")
}
#######################################################################
# display_page Subroutine #
#######################################################################
sub display_page
{
local ($page, $routine, $file, $line) = @_;
# the subroutine begins by opening the requested file for
# reading, exiting with file_open_error if there is a
# problem as usual.
# here I'm going to try and make it open a new page for the first time viewers ft will equal yes
if ($form_data{'ft'}) { $page = "./Html/Products/main_page.html"; }
open (PAGE, "$page") || &file_open_error("$page", "$routine", $file, $line);
while ()
{
s/cart_id=/cart_id=$cart_id/g;
s/%%cart_id%%/$cart_id/g;
s/%%page%%/$form_data{'page'}/g;
if ($form_data{'add_to_cart_button'} ne "" && $sc_shall_i_let_client_know_item_added eq "yes")
{
if ($_ =~ /