#!/usr/bin/perl $| = 1; print "Content-type: text/html\n\n"; &require_supporting_libraries (__FILE__, __LINE__, "./Library2/web_store.setup.db2"); &require_supporting_libraries (__FILE__, __LINE__, "$sc_cgi_lib_path", "$sc_html_setup_file_path", "$sc_mail_lib_path", "./Library2/sc_template_lib.pl"); &read_and_parse_form_data; $page = $form_data{'page'}; if ($form_data{'srbx'}) { $form_data{'search_request_button.x'} = "Search"; $form_data{'product'} = $form_data{'srbx'}; $remote_account = $form_data{'rem'}; } $product = $form_data{'product'}; $prod_type = $form_data{'product'}; $search_request = $form_data{'search_request_button.x'}; $cart_id = $form_data{'cart_id'}; $sc_cart_path = "$sc_user_carts_directory_path/$cart_id.cart"; $cat_name = $cat_headers{$product}; $cat_desc = $cat_text{$product}; $cat_pic_url = $cat_pictures{$product}; $cat_text_filename = $cat_text_file{$product}; $hits_seen_last = (int ($form_data{'hits_seen'}) - 25); if (($page eq "search_results") && ($product =~ /ALL/i)) { $form_data{'page'} = "all_category_search_results"; $page = "all_category_search_results"; } $form_data{'image_url'} =~ s/\///g; $form_data{'image_url'} =~ s/ //g; $item_url = "/images2/defaults/NIA.jpg"; #$testit = "./images2/X$product/$form_data{image_url}.jpg"; #if (-e "$testit") # { # $item_url = "images2/X$product/$form_data{image_url}.jpg"; # } $sub_dir = &get_sub_dir($form_data{image_url}); $testit3 = "Images/products/" . $sub_dir . "/" . $form_data{image_url} . ".jpg"; if (-e "$testit3") { $item_url = $testit3; } &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{'show_item'} ne "") { &show_item; exit; } if ($form_data{'add_to_cart_button.x'} ne "") { &add_to_the_cart; exit; } elsif ($form_data{'modify_cart_button.x'} ne "") { &display_cart_contents; exit; } elsif ($form_data{'change_quantity_button.x'} ne "") { &output_modify_quantity_form; exit; } elsif ($form_data{'submit_change_quantity_button.x'} ne "") { &modify_quantity_of_items_in_cart; exit; } elsif ($form_data{'delete_item_button.x'} ne "") { &output_delete_item_form; exit; } elsif ($form_data{'submit_deletion_button.x'} ne "") { &delete_from_cart; exit; } elsif ($form_data{'order_form_button.x'} ne "") { &require_supporting_libraries (__FILE__, __LINE__, "$sc_order_lib_path"); &display_order_form; exit; } elsif ($form_data{'submit_order_form_button.x'} ne "") { &require_supporting_libraries (__FILE__, __LINE__, "$sc_order_lib_path"); &process_order_form; exit; } elsif (($page ne "" || $form_data{'search_request_button.x'} ne "" || $form_data{'continue_shopping_button.x'} || $are_any_query_fields_filled_in =~ /yes/i) && ($form_data{'return_to_frontpage_button.x'} eq "")) { $sc_data_file_path = "./Data_files/$prod_type.file"; if(($form_data{'search_request_button.x'} =~ /list/i) || ($product =~ /ALL/i)) { $sc_data_file_path = "./Data_files/data.file"; } if($form_data{'show_cat_page'} ne "") { &show_category_page; exit; } &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 "I am sorry but I was unable to require $require_file at line $line in $file. Would you please make sure that you have the path correct and that the permissions are set so that I have read access? Thank you."; exit; } } } sub read_and_parse_form_data { &ReadParse(*form_data); } sub error_check_form_data {} 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"); } } } 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 .= ".$$"; $cart_id .= "$remote_account"; $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++; } open (CART, ">$sc_cart_path") || &file_open_error("$sc_cart_path", "Assign a Shopping Cart", __FILE__, __LINE__); } ######################################### # Output Frontpage sub output_frontpage { foreach $filename ("./templates/headers/scheader", "./templates/store/frontpage", "./templates/headers/scfooter") { &template($filename); } exit; } ######################################### # 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) { $options = ""; $option_subtotal = ""; $option_grand_total = ""; $item_grand_total = ""; # # /g; $item_ordered_with_options =~ s/~lt~/\>$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 { 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; } ################################################## # 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 { &create_html_page_from_db; } ################################################## # create_html_page_from_db Subroutine sub create_html_page_from_db { local (@database_rows, @database_fields, @item_ids, @display_fields); local ($total_row_count, $id_index, $display_index); local ($row, $field, $empty, $option_tag, $option_location, $output); if ($form_data{'search_by'} ne "") { $sf = $form_data{'search_by'}; $si = $db{"$sf"}; @sc_db_query_criteria = ("keywords|$si|=|string"); } &product_page_header($sc_product_display_title); $template_file = "./templates/store/search_results"; if ($page eq "all_category_search_results") { $template_file = "./templates/store/all_category_search_results"; } &template($template_file); if ($form_data{'add_to_cart_button.x'} ne "" && $sc_shall_i_let_client_know_item_added eq "yes") { print "$sc_item_ordered_message"; } if (!($sc_db_lib_was_loaded =~ /yes/i)) { &require_supporting_libraries (__FILE__, __LINE__, "$sc_db_lib_path"); } ($status,$total_row_count) = &submit_query(*database_rows, $form_data{'hits_seen'}); #### NEW SORT 3-1-98/ 8-7-98 mdg #### If this script get's too slow - this is what should be changed FIRST foreach $row (@database_rows) { @row = split (/\|/, $row); unless ($row[3] eq "") { $sortable_field = $row[3]; unshift (@row, $sortable_field); $new_row = join ("\|", @row); push (@new_rows, $new_row); } } @database_rows = (); @sorted_rows = sort (@new_rows); foreach $sorted_row (@sorted_rows) { @row = split (/\|/, $sorted_row); $sorted_field = shift (@row); $old_but_sorted_row = join ("\|", @row); push (@database_rows, $old_but_sorted_row); } #### #### END SORT if ($total_row_count == 0) { print qq~

Sorry, no items were found which matched your keyword(s)

Please try another search or choose another category above

~; &print_footer; exit; } $hits_seen = $form_data{'hits_seen'}; ########################################## for ($i = 1;$i <= $hits_seen;$i++) { $seen_row = shift (@database_rows); } $length_of_database_rows = @database_rows; for ($i = $length_of_database_rows-1;$i >= $sc_db_max_rows_returned;$i--) { $extra_row = pop (@database_rows); } ########################################## $hits_seen = $form_data{'hits_seen'} + $sc_db_max_rows_returned; foreach $row (@database_rows) { $copy = $row; $copy =~ s/ /\^/g; @database_fields = split (/\|/, $row); @database_fields2 = split (/\|/, $copy); @display_fields = (); @temp_fields = @database_fields; foreach $display_index (@sc_db_index_for_display) { if ($display_index == $sc_db_index_of_price) { $temp_fields[$sc_db_index_of_price] = &display_price($temp_fields[$sc_db_index_of_price]); } push(@display_fields, $temp_fields[$display_index]); } @item_ids = (); foreach $id_index (@sc_db_index_for_defining_item_id) { $database_fields[$id_index] =~ s/\"/~qq~/g; $database_fields[$id_index] =~ s/\>/~gt~/g; $database_fields[$id_index] =~ s/\

~; &make_href; print qq~ $database_fields[4] $database_fields[8] $database_fields[2] ~; } &product_page_footer($status,$total_row_count); exit; } ############################################ # 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; } 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.") } sub display_page { local ($page, $routine, $file, $line) = @_; 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.x'} ne "" && $sc_shall_i_let_client_know_item_added eq "yes") { if ($_ =~ /
$counter_file") || &file_open_error("$counter_file", "Counter", $file, $line); print COUNTER_FILE "1\n"; close(COUNTER_FILE); } open (COUNTER_FILE, "$counter_file") || &file_open_error("$counter_file", "Counter", $file, $line); while () {$item_number = "$_";} close (COUNTER_FILE); $item_number += 1; open (NOTE, ">$counter_file") || &file_open_error("$counter_file", "Counter", $file, $line); print NOTE "$item_number\n"; close (NOTE); return $item_number; } sub update_error_log { local ($type_of_error, $file_name, $line_number) = @_; local ($log_entry, $email_body, $variable, @env_vars); @env_vars = keys(%ENV); $date = &get_date; if ($sc_shall_i_log_errors eq "yes") { $log_entry = "$type_of_error\|FILE=$file_name\|LINE=$line_number\|"; $log_entry .= "DATE=$date\|"; &get_file_lock("$sc_error_log_path.lockfile"); open (ERROR_LOG, ">>$sc_error_log_path") || &CgiDie ("The Error Log could not be opened"); foreach $variable (@env_vars) {$log_entry .= "$ENV{$variable}\|";} print ERROR_LOG "$log_entry\n"; close (ERROR_LOG); &release_file_lock("$sc_error_log_path.lockfile"); } if ($sc_shall_i_email_if_error eq "yes") { $email_body = "$type_of_error\n\n"; $email_body .= "FILE = $file_name\n"; $email_body .= "LINE = $line_number\n"; $email_body .= "DATE=$date\|"; foreach $variable (@env_vars) {$email_body .= "$variable = $ENV{$variable}\n";} &send_mail("$sc_admin_email", "$sc_admin_email", "Web Store Error", "$email_body"); } } sub get_date { local ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst,$date); local (@days, @months); @days = ('Sunday','Monday','Tuesday','Wednesday','Thursday', 'Friday','Saturday'); @months = ('January','February','March','April','May','June','July', 'August','September','October','November','December'); ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); if ($hour < 10) { $hour = "0$hour"; } if ($min < 10) { $min = "0$min"; } if ($sec < 10) { $sec = "0$sec"; } $year = ($sc_current_century-1) . "$year"; $date = "$days[$wday], $months[$mon] $mday, $year at $hour\:$min\:$sec"; return $date; } sub display_price { local ($price) = @_; local ($format_price); if ($sc_money_symbol_placement eq "front") {$format_price = "$sc_money_symbol $price";} else {$format_price = "$price $sc_money_symbol";} return $format_price; } sub get_file_lock { local ($lock_file) = @_; local ($endtime); $endtime = 20; $endtime = time + $endtime; while (-e $lock_file && time < $endtime) { sleep(1); } # open(LOCK_FILE, "$lock_file") || &CgiDie ("I could not open the lock file"); flock(LOCK_FILE, 2); # 2 exclusively locks the file } sub release_file_lock { local ($lock_file) = @_; # flock(LOCK_FILE, 8); # 8 unlocks the file close(LOCK_FILE); unlink($lock_file); } sub format_price { local ($unformatted_price) = @_; local ($formatted_price); $formatted_price = sprintf ("%.2f", $unformatted_price); return $formatted_price; } ######################################################################## sub get_sub_dir($inval) { local($_) = @_; $_ =~ m/([a-zA-Z_0-9])/; $_ =~ tr/A-Z/a-z/; $_ = substr($_,0,1); if ($_ =~ /^[0-9]/) { $_ = "other"; } return($_); }