Avatar billede nette Nybegynder
13. december 2001 - 22:45 Der er 1 kommentar

Hjælp til ændring af shoppingcart script

Jeg bruger S-mart shoppingcart-script og er faktisk vældig tilfreds med dette, men jeg kunne godt tænke mig at bruge en rigtig database (mysql) til lagring af de forskellige vare istedet for den tekstfil der bruges men da jeg ikke har arbejdet ret meget med perl ved jeg ikke en gang om det er muligt er der nogen der kan hjælpe?
Scriptet ser sådan ud :


#!/usr/local/bin/perl
###########################################################################
# S-Mart Shopping Cart Script v1.9.5
my $version = \'1.9.5\';
# Shop smart. Shop S-Mart.
# Written by Barry Robison. (brobison@rcinet.com)
# This script uses code written by Matt Wright.
# Please visit his site at http://worldwidemart.com
# This script is mailware. Please e-mail me if you use it.
#
# Selling the code for this program without prior written consent is       
# expressly forbidden.  In other words, please ask first before you try and 
# make money off of my program.                                       
#                                                                           
# Obtain permission before redistributing this software over the Internet or
# in any other medium.  In all cases copyright and header must remain intact.
###########################################################################
# Custimization portion - All Options are set in smart.cfg !!
###########################################################################

# Measure CPU time usage;
my $start = (times)[0];
# Unbuffer the output;
$| = 1;

# Read in user prefs
require \'smart.cfg\';

###########################################################################
# Done. Make no changes below this line, unless you know what you are doing.
###########################################################################
use vars qw($conver $debug $tmpdir $storename %shipping2 %shipping3 $trackurl $useverify $usertime $orderdir $usercurr $trackdb $numtolist);
my($incoming, @pairs, %FORM);
$incoming = $ENV{\'QUERY_STRING\'};
#$incoming = $ENV{\'PATH_INFO\'};
@pairs = split(/&/, $incoming);
#@pairs = split(/\\//, $incoming);
foreach (@pairs) {
  my($name, $value) = split(/=/, $_);
#  my($name, $value) = split(/\\./, $pair);
  $value =~ tr/+/ /;
  $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack(\"C\", hex($1))/eg;
  $FORM{$name} = $value;
}

# Debug stuff
# Print version number
if ($FORM{\'command\'} eq \'debug\' && $debug == 1) {
    print \"Content-type: text/plain\\n\\n\";
    print \"S-Mart Shopping Cart v$version.\\nConfig file $conver.\\n\";
    my $path = `pwd`; print \"Path: $path\\n\";
    my $perl = `which perl`; print \"Perl: $perl\\n\";
    my $sendmail = `which sendmail`; print \"Sendmail: $sendmail\\n\";
    my $dir = `ls -al`; print \"Dir: $dir\\n\";
    exit;
    }
if ($ARGV[0] eq \'-v\') { print \"S-Mart Shopping Cart v$version.\\nConfig file $conver.\\n\"; exit; }

# Get command variables
my $command = $FORM{\'command\'};
my $pos = $FORM{\'pos\'};
my $type = $FORM{\'type\'};

# Parse form input
&parse_form;

# Get the date
&get_date;

&get_host;


# See if they have a cart already, if not, make one
&check_file;

# See what they want to do.

# Add an item to the cart.
if ($command eq \'add\') {
&add_item;
}
# Change a quantity of an item in the cart, if at zero, delete it
elsif ($command eq \'change\') {
&change_items;
}
# Show the order form. Get their name, phone number, CC info, etc..
elsif ($command eq \'buy1\') {
&buy_items1;
}
# Process the order form info, mail out receipt, and order
elsif ($command eq \'buy2\') {
&buy_items2;
}
# Show current cart
elsif ($command eq \'review\') {
&review_items;
}
# List items, based on database call
elsif ($command eq \'listitems\') {
&gen_page;
}
# Show a databae frontend.
elsif ($command eq \'showstore\') {
&show_store;
}
# Delete a whole cart
elsif ($command eq \'delete\') {
&delete_cart;
}
elsif ($command eq \'deleteitem\') {
&delete_item;
}
else { &show_store; }
# Subroutines:

# Generate page, based on database search
sub gen_page {
my($SIZE,$i);
&print_header;
# Table header, change this for table options!
print \"<center><table cellpadding=5 border=1>\\n\";
print \"<tr>\\n\";

open (FILE,\"$basepath$delim$resourcedb\") || die \"Content-type: text/html\\n\\nCan\'t Open $resourcedb(r): $!\\n\";
my(@LINES)=<FILE>;
close(FILE);
$SIZE=@LINES;

if ($type eq \'all\') {
    my($max) = $pos + $numtolist;
    if ($max > $SIZE) {
    $max = $SIZE;
    }
    for ($i=$pos;$i<$max;$i++) {
          my(%ITEM);
        ($ITEM{\'itemid\'}, $ITEM{\'name\'}, $ITEM{\'price\'}, $ITEM{\'descrip\'}, $ITEM{\'image\'}, $ITEM{\'weight\'}, $ITEM{\'itemurl\'}, $ITEM{\'group\'}) = split(/\\|/,$LINES[$i]);
        &print_item(\\%ITEM);
        }
    print \"</table>\\n\";
    if ($max < $SIZE) {
        print \"<center><a href=\\\"$cgiurl\\?command=listitems\\&pos=$max&type=all\\\">Next $numtolist items</a></center>\\n\";
        }
    }

elsif ($type eq \'search\') {
    my($matches) = 0;
    my($i) = $pos;
    while (($matches < $numtolist) && ($i < $SIZE)) {
        if (index(lc $LINES[$i],lc $FORM{\'search\'}) >= 0) {
            $matches++;
            my(%ITEM);
            ($ITEM{\'itemid\'}, $ITEM{\'name\'}, $ITEM{\'price\'}, $ITEM{\'descrip\'}, $ITEM{\'image\'}, $ITEM{\'weight\'}, $ITEM{\'itemurl\'}, $ITEM{\'group\'}) = split(/\\|/,$LINES[$i]);
            &print_item(\\%ITEM);
            }
        $i++;
        }
    print \"</table>\\n\";
    if ($i < $SIZE) {
        print \"<form method=post action=\\\"$cgiurl\\?command=listitems\\&pos=$i\\&type=search\\\">\\n\";
        print \"<input type=hidden name=itemid value=\\\"$itemid\\\">\\n\";
        print \"<input type=hidden name=search value=\\\"$FORM{\'search\'}\\\">\\n\";
        print \"<center><input type=submit value=\\\"Continue Search\\\"></form>\\n\";
        }
    }

elsif ($type eq \'group\') {
    my($matches) = 0;
    my($i) = $pos;
    while (($matches < $numtolist) && ($i < $SIZE)) {
        $_=$LINES[$i];
        my(%ITEM);
        ($ITEM{\'itemid\'}, $ITEM{\'name\'}, $ITEM{\'price\'}, $ITEM{\'descrip\'}, $ITEM{\'image\'}, $ITEM{\'weight\'}, $ITEM{\'itemurl\'}, $ITEM{\'group\'}) = split(/\\|/,$_);
        my(@groups) = split(/,/,$ITEM{\'group\'});
        my(%is_group);
        undef %is_group;
        for (@groups) { $is_group{$_} = 1 }
        if ($is_group{$FORM{\'group\'}}) {
            $matches++;
            &print_item(\\%ITEM);
            }
        $i++;
        }
    print \"</table>\\n\";
    if ($i < $SIZE) {
            print \"<a href=\\\"$cgiurl\\?command=listitems\\&pos=$i\\&type=group\\&group=$FORM{\'group\'}\\\">Continue Search</a>\\n\";
            }
    }

&print_footer;

# This is where you want to customize your generated pages!!
# Basically the top part will write out the header, then the table header
# Change the table header for options like spacing, borders, etc...

sub print_item {
local(*ITEM) = shift;
print \"<form method=post action=\\\"$cgiurl\\?command=add\\\">\\n\";
# Hide the weight for later use
print \"<input type=hidden name=weight value=\\\"$ITEM{\'weight\'}\\\">\\n\";
print \"<input type=hidden name=itemid value=\\\"$ITEM{\'itemid\'}\\\">\\n\";

# For showing product images
if ($ITEM{\'image\'}) {
    print \"<td rowspan=2><img src=\\\"$imageurl/$ITEM{\'image\'}\\\" $imageopt></td>\\n\";
    }
    else {
        print \"<td rowspan=2></td>\\n\";
        }

# If the item has a URL, make it a link
if ($ITEM{\'itemurl\'}) {
    print \"<td><a href=\\\"$ITEM{\'itemurl\'}\\\">$ITEM{\'name\'}</a><input type=hidden name=itemname value=\\\"$ITEM{\'name\'}\\\"></td>\\n\";
    }
    else {
        print \"<td>$ITEM{\'name\'}<input type=hidden name=itemname value=\\\"$ITEM{\'name\'}\\\"></td>\\n\";
        }
       
# Print other info

#Price
print \"<td>\\$ $ITEM{\'price\'}<input type=hidden name=itemprice value=\\\"$ITEM{\'price\'}\\\"></td>\\n\";

#Quantity
print \"<td><input type=text size=2 name=itemquant value=1></td>\\n\";

#Add button
print \"<td rowspan=2><input type=submit value=\\\"Buy Me!\\\"></td>\\n\";

#Description
print \"</tr><tr><td colspan=3>$ITEM{\'descrip\'}</td>\\n\";

# Close
print \"</form></tr><tr>\\n\";
   
}
## END print_item

}
## END gen_page


# Show front end of database store.
sub show_store {
&print_header;
print \"<center>\\n\";
print \"<table width=80%><td>\\n\";
print \"<a href=\\\"$cgiurl\\?command=listitems\\&type=all\\&pos=0\\\">List all items available</a><br>\\n
<Br>\\n
<table>
<td>Search for a particular item(s):</td>
<td>
<form method=post action=\\\"$cgiurl\\?command=listitems\\&pos=0\\&type=search\\\">
<input type=text name=search size=30></td>
<td><input type=submit value=Search></form></tD>
</table>\\n\";
print \"<table>
<td>List all:</td>
<td>
<form method=post action=\\\"$cgiurl\\?command=listitems\\&pos=0\\&type=group\\\">
<select name=group><option selected>Select a Group</option>\\n\";

foreach $group (keys %groups){
    print \"<OPTION value=\\\"$group\\\"> $groups{$group} </option>\\n\";
    }

print \"</select></td><td>\\n\";

print \"<center><input type=submit value=\\\"List\\\"</form></td>\\n</table>\\n<br>\";

if ($usetrack eq 1) {
    print \"<br><a href=\\\"$trackurl\\\">Track an Order</a><br>\\n\";
    }

print \"</td></table>\\n\";
&print_footer;
}
## END show_store

# Print the HTML header
sub print_header {
print \"Content-type: text/html\\n\\n\";
open (HEADER,\"$basepath$delim$header\") || print \"Could not open $basepath$delim$header $! \\n\";
while (<HEADER>) {
print $_;
    }
close(HEADER);   
}
## END print_header;

#Print the HTML footer
sub print_footer {
open (FOOTER,\"$basepath$delim$footer\") || print \"Could not open $basepath$delim$footer $! \\n\";
while (<FOOTER>) {
print $_;
    }
close(FOOTER);   
### Please do not delete this message.
print \"<br><br>\\n\";
print \"<hr width=75\\%>\\n\";

$end = (times)[0];
print \"<font size=-1>This page generated in \";
print $end - $start;
print \" seconds by <a href=\\\"http://www.rcinet.com/~brobison/scripts/\\\">S-Mart</a>. Copyright <a href=\\\"mailto:brobison\\@rcinet.com\\\">Barry Robison</a></font><br>\\n\";
}
## END print_footer

# List items in cart.
sub list_items {
my($totalprice,$totalquant,$totalweight) = 0;
open (REFFILE,\"$reffile\") || die \"Content-type: text/html\\n\\nCan\'t Open $reffile(r): $!\\n\";
my(@LINES)=<REFFILE>;
close(REFFILE);
$SIZE=@LINES;

print \"<form name=select1 method=POST action=\\\"$cgiurl?command=change\\\">\\n\";
print \"<table border=1 cellpadding=5><tr>\\n\";

if ($useimage eq \'1\') {
    print \"<th></th>\";
    }
   
if ($useid eq \'1\') {
    print \"<th>Item ID</th>\";
    }

print \"<th>Item Name</th><th>Price</th><th>Quantity</th></tr>\";

for ($i=1;$i<$SIZE;$i++) {
    $_=$LINES[$i];
    ($itemname, $itemprice, $itemquant, $weight, $itemid) = split(/\\|/,$_);
    $tmpprice = ($itemprice*$itemquant);
    $tmpprice = round($tmpprice);
    $tmpprice = pad($tmpprice);

    $totalprice += $tmpprice;
    $totalquant += $itemquant;
    $totalweight += ($weight*$itemquant);
    print \"<tr>\";

if ($useimage eq \'1\') {
    if ($image) {
        print \"<td><img src=\\\"$imageurl/$image\\\"></td>\\n\";
        }
    else {
        print \"<td></td>\";
        }
    }
   
if ($useid eq \'1\') {
    print \"<td>$itemid <input type=hidden name=itemid value=\\\"$itemid\\\"></td>\\n\";
    }

    print \"<td>$itemname</td><td align=right>\\$ $itemprice</td><td align=center><input type=number size=2 value=\\\"$itemquant\\\" name=item$i></td><td align=right>\\$ $tmpprice</tr>\\n\";
    }
   
print \"<tr><td><b>Subtotal:</b></td>\";
if ($useimage eq \'1\') {
    print \"<td></td>\";
    }
if ($useid eq \'1\') {
    print \"<td></td>\";
    }
   
$totalprice = pad($totalprice);
print \"<td></td><td></td><td align=right><b>\\$ $totalprice</b></td></tr>\\n\";
print \"<tr><td><b>Shipping:</b></td>\";
    if ($useimage eq \'1\') {
        print \"<td></td>\";
        }
    if ($useid eq \'1\') {
        print \"<td></td>\";
        }
if ($multiship eq 1) {
    print \"<td align=right colspan=2>\";
    open (SHIPDB, \"$basepath$delim$shipdb\");
    my(@SHIP) = <SHIPDB>;
    close(SHIPDB);
    print \"<select name=shipopt>\";
    foreach $ship (@SHIP) {
        my(@ships)=split(/\\|/,$ship);
        print \"<option \";
        if ($usership eq $ships[0]) { print \"selected \"; $totalprice+=$ships[2];}
        print \"value=$ships[0]>$ships[1] \\+\\$$ships[2] </option>\";
        }
    print \"</select></td>\\n\";
    }
else { print \"<td></td><td></td>\"; }
if ($shipping eq \'0\') {
    # print nothing
    }
elsif ($shipping eq \'1\') {
    my($low,@prices,$price);
    @prices = sort number keys %shipping;
    $low = $prices[0];
    foreach $price (@prices) {
        if (($price > $low) && ($price <= $totalquant)) {$low = $price};
        }
    $shipamt = $shipping{$low};
    $totalprice += $shipamt;
    print \"<td align=right><b>\\$ $shipamt</b></td></tr>\\n\";
    }
elsif ($shipping eq \'2\') {
    $totalprice += $shipamt;
    print \"<td align=right><b>\\$ $shipamt</b></td></tr>\\n\";
    }
elsif ($shipping eq \'3\') {
    my($low,@weights,$weight);
    @weights = sort number keys %shipping;
    $low = $weights[0];
    foreach $weight (@weights) {
        if ($weight > $low && $weight <= $totalweight) {$low = $weight};
        }
    $shipamt = $shipping{$low};
    $totalprice += $shipamt;
    print \"<td align=right><b>\\$ $shipamt</b></td></tr>\\n\";
    }
elsif ($shipping eq \'4\') {
    my($low,@prices);
    @prices = sort number keys %shipping;
    $low = $prices[0];
    foreach $price (@prices) {
        if ($price > $low && $price <= $totalprice) {$low = $price};
        }
    $shipamt = $shipping{$low};
    $totalprice += $shipamt;
    print \"<td align=right><b>\\$ $shipamt</b></td></tr>\\n\";   
    }

if ($tax eq \'0\') {
# print nothing
}
elsif ($tax eq \'1\') {
print \"<tr><td><b>Taxes:</b></td>\";
if ($useimage eq \'1\') {
    print \"<td></td>\";
    }
if ($useid eq \'1\') {
    print \"<td></td>\";
    }
print \"<td align=center colspan=2>\";
print \"<select name=taxopt><option value=NULL>None</option>\";
foreach $tax (keys %taxes) {
    print \"<option \";
    if ($usertax eq $tax) { print \"selected \"; $totaltax=($totalprice*$taxes{$tax});$totalprice+=$totaltax;}
    print \"value=$tax>$tax \\+\" . $taxes{$tax}*100 . \"\\% </option>\";
    }
    print \"</select></td>\\n\";

$totaltax = round($totaltax);
$totaltax = pad($totaltax);
print \"</td><td align=right><b>\\$$totaltax</b></td></tr>\\n\";
}
elsif ($tax eq \'2\') {
$taxtotal = $totalprice * $taxamt;
$taxtotal = int($taxtotal * (10 ** 2) + .5) / (10 ** 2);

$totalprice += $taxtotal;
print \"<tr><td><b>Taxes:</b></td>\";
if ($useimage eq \'1\') {
    print \"<td></td>\";
    }
if ($useid eq \'1\') {
    print \"<td></td>\";
    }
print \"<td></td><td></td><td align=right><b>\\$ $taxtotal</center></b></td></tr>\\n\";
}

$totalprice = round($totalprice);
$totalprice = pad($totalprice);
print \"<tr><td><b>Total price:</b></td>\";
if ($useimage eq \'1\') {
    print \"<td></td>\";
    }
if ($useid eq \'1\') {
    print \"<td></td>\";
    }
print \"<td></td><td></td><td align=right><b>\\$ $totalprice</b></td></tr>\\n\";


if ($multicurr eq 1) {
print \"<tr><td><b>Total price in<br>selected currency:</b></td>\";
    if ($useimage eq \'1\') {
        print \"<td></td>\";
    }
    if ($useid eq \'1\') {
        print \"<td></td>\";
    }

my($path) = $basepath . $delim . $currdb;
open (CURRDB, \"$path\");
my(@CURR) = <CURRDB>;
close(CURRDB);
print \"<td colspan=2 align=center><select name=curropt>\";
foreach $curr (@CURR) {
    my(@currs)=split(/\\|/,$curr);
    print \"<option \";
    if ($usercurr eq $currs[0]) { print \"selected \"; $usercurr=$currs[2] }
    print \"value=$currs[0]>$currs[1] x$currs[2] </option>\";
    }
    print \"</select></td>\\n\";

    $totalprice *= $usercurr;
    $totalprice = int($totalprice * (10 ** 2) + .5) / (10 ** 2);
    if (index($totalprice,\".\") >= 0) {
    my($left,$right) = split(/\\./,$totalprice);
    if (length($right) == 0) {
        $totalprice = $totalprice . \"00\";
        }
        elsif (length($right) == 1) { $totalprice = $totalprice . \"0\"; }
    }
    else { $totalprice = $totalprice . \".00\"; }
    print \"<td align=right><b>\\$ $totalprice</b></td></tr>\\n\";
    }
   
print \"</table><br>\\n\";
   
}

# Review all items in cart. Can modify quantities.
sub review_items {
&print_header;
&list_items;
print \"<center><input type=submit value=\\\"Re-Calculate\\\"></form>\\n\";
&print_footer;
}

# Add a item to the cart, show review page, unless they included a redirect variable.
sub add_item {

open (REFFILE,\">>$reffile\") || print \"Content-type: text/html\\n\\n Can\'t Open $reffile(r): $!\\n\";
print REFFILE join \"\\|\", $FORM{\'itemname\'},$FORM{\'itemprice\'},$FORM{\'itemquant\'},$FORM{\'weight\'},$FORM{\'itemid\'},\"\\|\\n\";
close(REFFILE);
if ($useredirect eq \'1\') {
    print \"Content-type: text/html\\n\\n <html><body bgcolor=\\#ffffff onload=\\\"history.back()\\\">Item added.<br>If you browser does not support Javascript, please press BACK now.\\n\";
    }
elsif ($FORM{\'redirect\'}) {
        print \"Location: $FORM{\'redirect\'} \\n\\n\";
        }
else {
    &review_items;
    }
}

# Change quanities of items, delete zero quantity items.
sub change_items {
open (REFFILE,\"$reffile\") || print \"Content-type: text/html\\n\\n Can\'t Open $reffile(r): $!\\n\";
my(@LINES)=<REFFILE>;
close(REFFILE);
$SIZE=@LINES;

open (REFFILE,\">$reffile\") || print \"Content-type: text/html\\n\\n Can\'t Open $reffile(r): $!\\n\";
print REFFILE \"$date\\| $ENV{\'REMOTE_HOST\'}\\| $ENV{\'HTTP_USER_AGENT\'}\\|$FORM{\'curropt\'}\\|$FORM{\'shipopt\'}\\|$FORM{\'taxopt\'}\\|\\n\";
for ($i=1;$i<$SIZE;$i++) {
    local($itemq) = \"item$i\";
      $_=$LINES[$i];
    my($itemname, $itemprice, $itemquant, $weight, $itemid) = split(/\\|/,$_);
    if ($FORM{$itemq} > 0) {
        print REFFILE join \"\\|\", $itemname,$itemprice,$FORM{$itemq},$weight,$itemid,\"\\|\\n\";
        }
    }
close(REFFILE);
print \"Location: $cgiurl\\?command=review\\n\\n\";
}


# Show the order form
sub buy_items1 {
&print_header;
&list_items;
open (ORDER,\"$basepath$delim$order\") || print \"Could not open $basepath$delim$order $! \\n\";
while (<ORDER>) {
    print $_;
    }
close(ORDER);   
&print_footer;
}


# Process the order form
sub buy_items2 {
&check_required;

&print_header;

if ($useverify eq \'1\' && ($cardtype eq \'visa\' || $cardtype eq \'mastercard\' || $cardtype eq \'novus\' || $cardtype eq \'amex\')) {
    &CC_Verify;
    }
if ($usetrack eq 1) {
    &track_order;
    }
&send_order;
&send_verify;
print \"You have successfully ordered the following:<br><br>\\n\";
&list_items;
print \"<br><br>\\n\";

if ($usetrack eq 1) {
    my($sys) = \"mv $reffile $basepath/$orderdir/$storename-$orderid\";
    system($sys);
    print \"<a href=\\\"$trackurl?command=track\\&trackid=$orderid\\\">$trackurl</a>Track your order</a><br>\";
    }
unlink($reffile);

&print_footer;
}

# parse form input
sub parse_form {
my(@pairs);
  if ($ENV{\'REQUEST_METHOD\'} eq \'POST\') {
      # Get the input
      read(STDIN, $buffer, $ENV{\'CONTENT_LENGTH\'});

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

  foreach $pair (@pairs) {
      my($name, $value) = split(/=/, $pair);

      $name =~ tr/+/ /;
      $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack(\"C\", hex($1))/eg;

      $value =~ tr/+/ /;
      $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack(\"C\", hex($1))/eg;

      # If they try to include server side includes, erase them, so they
      # arent a security risk if the html gets returned.  Another
      # security hole plugged up.

      $value =~ s/<!--(.|\\n)*-->//g;

      # Create two associative arrays here.  One is a configuration array
      # which includes all fields that this form recognizes.  The other
      # is for fields which the form does not recognize and will report
      # back to the user in the html return page and the e-mail message.
      # Also determine required fields.

      if ($name eq \'bgcolor\' ||
      $name eq \'background\' ||
      $name eq \'link_color\' ||
      $name eq \'vlink_color\' ||
      $name eq \'text_color\' ||
        $name eq \'alink_color\' ||
      $name eq \'title\' ||
      $name eq \'sort\' ||
      $name eq \'print_config\' ||
      $name eq \'return_link_title\' ||
      $name eq \'return_link_url\' && ($value)) {
       
    $CONFIG{$name} = $value;
      }
      elsif ($name eq \'required\') {
        @required = split(/,/,$value);
      }
      elsif ($name eq \'exclude\') {
          @exclude = split(/,/,$value);
          }
      else {
        if ($FORM{$name} && ($value)) {
        $FORM{$name} = \"$FORM{$name}, $value\";
    }
        elsif ($value) {
            $FORM{$name} = $value;
        }
      }
  }
}

# Send the order
sub send_order {
open (REFFILE,\"$reffile\") || print \"Content-type: text/html\\n\\n Can\'t Open $reffile(r): $!\\n\";

my(@LINES)=<REFFILE>;
close(REFFILE);
$SIZE=@LINES;

my($totalquant,$totalprice,$totalweight) = 0;

# Open The Mail Program
open(MAIL,\"|$mailprog -t\");

  print MAIL \"To: $FORM{\'recipient\'}\\n\";
  print MAIL \"From: $FORM{\'email\'} ($FORM{\'realname\'})\\n\";
  print MAIL \"Subject: $FORM{\'subject\'}\";
  if ($usetrack eq 1) { print MAIL \"\\#$orderid\"; }
  print MAIL \"\\n\\n\";
  print MAIL \"Below is the result of the purchase form.  It was \";
  print MAIL \"submitted by $FORM{\'realname\'} ($FORM{\'email\'}) on \";
  print MAIL \"$date\\n\";
  print MAIL \"----------------------------------------------------------------\\n\";
  print MAIL \"Buyer info:\\n\";
  print MAIL \"Remote Host: $userhost\\n\";
  print MAIL \"Buyer Agent: $useragent\\n\";
  print MAIL \"\\n\\n\";
  print MAIL \"The purchase is:\\n\\n\";
 
print MAIL sprintf \"%-28.28s\", \"Item Name\";
print MAIL sprintf \"%14.14s\", \"Item Price\";
print MAIL sprintf \"%10.10s\", \"Quantity\";
print MAIL sprintf \"%18.18s\", \"Item Total\";
print MAIL \"\\n\";
print MAIL \"----------------------------------------------------------------------\\n\";

for ($i=1;$i<$SIZE;$i++) {
    $_=$LINES[$i];
    ($itemname, $itemprice, $itemquant, $weight, $itemid) = split(/\\|/,$_);
    $tmpprice = $itemprice*$itemquant;
    $tmpprice = round($tmpprice);
    $totalprice += $tmpprice;
    $totalquant += $itemquant;
    $totalweight += ($weight*$itemquant);
    print MAIL sprintf \"%-6.6s\", $itemid;
    print MAIL sprintf \"%-18.18s\", $itemname;
    print MAIL sprintf \"%13.13s\", \"\\$$itemprice\";
    print MAIL sprintf \"%10.10s\", $itemquant;
    print MAIL sprintf \"%18.18s\", \"\\$$tmpprice\";
    print MAIL \"\\n\";
    }

print MAIL \"----------------------------------------------------------------------\\n\";
print MAIL \"Subtotal:\";
$totalprice = pad($totalprice);
print MAIL sprintf \"%57.57s\", \"\\$$totalprice\\n\";


if ($shipping eq \'0\') {
    # Print Nothing
    }
elsif ($shipping eq \'1\') {
    my($low,@prices,$price);
    @prices = sort number keys %shipping;
    $low = $prices[0];
    foreach $price (@prices) {
        if ($price > $low && $price <= $totalquant) {$low = $price};
        }
    $shipamt = $shipping{$low};
    $totalprice += $shipamt;
    }
elsif ($shipping eq \'2\') {
    $totalprice += $shipamt;
    }
elsif ($shipping eq \'3\') {
    my($low,@weights,$weight);
    @weights = sort number keys %shipping;
    $low = $weights[0];
    foreach $weight (@weights) {
        if ($weight > $low && $weight <= $totalweight) {$low = $weight};
        }
    $shipamt = $shipping{$low};
    $totalprice += $shipamt;
    }
elsif ($shipping eq \'4\') {
    my($low,@prices);
    @prices = sort number keys %shipping;
    $low = $prices[0];
    foreach $price (@prices) {
        if ($price > $low && $price <= $totalprice) {$low = $price};
        }
    $shipamt = $shipping{$low};
    $totalprice += $shipamt;
    }

if ($multiship eq 1) {
    my($path) = $basepath . $delim . $shipdb;
    open (SHIPDB, \"$path\");
    my(@SHIP) = <SHIPDB>;
    close(SHIPDB);
    print \"<select name=shipopt>\";
    foreach $ship (@SHIP) {
        my(@ships)=split(/\\|/,$ship);
        if ($usership eq $ships[0]) { $shipamt += $ships[2]; $totalprice += $ships[2]; }
        }
   
    }

$shipamt = round($shipamt);
$shipamt = pad($shipamt);
print MAIL \"Shipping:\";
print MAIL sprintf \"%57.57s\", \"\\$$shipamt\\n\";


if ($tax eq \'0\') {
    # Do nothing
    }
elsif ($tax eq \'1\') {
    if ($taxes{$FORM{\'state\'}}) { $taxamt = $taxes{$FORM{\'state\'}}; }
    $taxtotal = $totalprice * $taxamt;
    $totalprice += $taxtotal;
    }
elsif ($tax eq \'2\') {
    $taxtotal = $totalprice * $taxamt;
    $totalprice += $taxtotal;
    }
   
$taxtotal = round($taxtotal);
$taxtotal = pad($taxtotal);
print MAIL \"Taxes:\";
print MAIL sprintf \"%60.60s\", \"\\$$taxtotal\\n\";

$totalprice = round($totalprice);
$totalprice = pad($totalprice);
print MAIL \"Total Price:\";
print MAIL sprintf \"%56.56s\" , \"\\$$totalprice\\n\\n\\n\";

if ($CONFIG{\'sort\'} eq \'alphabetic\') {
      foreach $key (sort keys %FORM) {
        # Print the name and value pairs in FORM array to mail.
        print MAIL \"$key: $FORM{$key}\\n\\n\";
      }
  }
  elsif ($CONFIG{\'sort\'} =~ /^order:.*,.*/) {
        $CONFIG{\'sort\'} =~ s/order://;
      @sorted_fields = split(/,/, $CONFIG{\'sort\'});
      foreach $sorted_field (@sorted_fields) {
        # Print the name and value pairs in FORM array to mail.
        if ($FORM{$sorted_field}) {
            print MAIL \"$sorted_field: $FORM{$sorted_field}\\n\\n\";
        }
      }
      $CONFIG{\'sort\'} = \'order:\' . $CONFIG{\'sort\'};
  }
  else {
      foreach $key (keys %FORM) {
        # Print the name and value pairs in FORM array to html.
        print MAIL \"$key: $FORM{$key}\\n\\n\";
      }
  }
 
$totalprice = 0;
close (MAIL);
}

# Send to receipt
sub send_verify {
my($totalquant,$totalprice,$taxtotal) = 0;

# Open The Mail Program
open(MAIL,\"|$mailprog -t\");

  print MAIL \"To: $FORM{\'email\'}\\n\";
  print MAIL \"From: $FORM{\'recipient\'}\\n\";
  print MAIL \"Subject: Order Verification\";
  if ($usetrack eq 1) { print MAIL \"\\#$orderid\"; }
  print MAIL \"\\n\\n\";
  print MAIL \"Below is the result of the purcahse form.  It was \";
  print MAIL \"submitted by $FORM{\'realname\'} ($FORM{\'email\'}) on \";
  print MAIL \"$date\\n\";
  print MAIL \"----------------------------------------------------------------\\n\";
  print MAIL \"The purchase is:\\n\\n\";
 
 
print MAIL sprintf \"%-28.28s\", \"Item Name\";
print MAIL sprintf \"%14.14s\", \"Item Price\";
print MAIL sprintf \"%10.10s\", \"Quantity\";
print MAIL sprintf \"%18.18s\", \"Item Total\";
print MAIL \"\\n\";
print MAIL \"----------------------------------------------------------------------\\n\";


for ($i=1;$i<$SIZE;$i++) {
    $_=$LINES[$i];
    my($itemname, $itemprice, $itemquant, $weight,$itemid) = split(/\\|/,$_);
    $tmpprice = $itemprice*$itemquant;
    $tmpprice = round($tmpprice);
    $totalprice=$totalprice + $tmpprice;
    $totalquant=$totalquant + $itemquant;
    $totalweight += ($weight*$itemquant);
    print MAIL sprintf \"%-6.6s\", $itemid;
    print MAIL sprintf \"%-18.18s\", $itemname;
    print MAIL sprintf \"%13.13s\", \"\\$$itemprice\";
    print MAIL sprintf \"%10.10s\", $itemquant;
    print MAIL sprintf \"%18.18s\", \"\\$$tmpprice\";
    print MAIL \"\\n\";
    }

print MAIL \"----------------------------------------------------------------------\\n\";
$totalprice = pad($totalprice);
print MAIL \"Subtotal:\";
print MAIL sprintf \"%57.57s\", \"\\$$totalprice\\n\";


if ($shipping eq \'0\') {
    # Print Nothing
    }
elsif ($shipping eq \'1\') {
    my($low,@prices,$price);
    @prices = sort number keys %shipping;
    $low = $prices[0];
    foreach $price (@prices) {
        if ($price > $low && $price <= $totalquant) {$low = $price};
        }
    $shipamt = $shipping{$low};
    $totalprice += $shipamt;
    }
elsif ($shipping eq \'2\') {
    $totalprice += $shipamt;
    }
elsif ($shipping eq \'3\') {
    my($low,@weights,$weight);
    @weights = sort number keys %shipping;
    $low = $weights[0];
    foreach $weight (@weights) {
        if ($weight > $low && $weight <= $totalweight) {$low = $weight};
        }
    $shipamt = $shipping{$low};
    $totalprice += $shipamt;
    }
elsif ($shipping eq \'4\') {
    my($low,@prices);
    @prices = sort number keys %shipping;
    $low = $prices[0];
    foreach $price (@prices) {
        if ($price > $low && $price <= $totalprice) {$low = $price};
        }
    $shipamt = $shipping{$low};
    $totalprice += $shipamt;
    }

if ($multiship eq 1) {
    my($path) = $basepath . $delim . $shipdb;
    open (SHIPDB, \"$path\");
    my(@SHIP) = <SHIPDB>;
    close(SHIPDB);
    print \"<select name=shipopt>\";
    foreach $ship (@SHIP) {
        my(@ships)=split(/\\|/,$ship);
        if ($usership eq $ships[0]) { $shipamt += $ships[2]; $totalprice += $ships[2]; }
        }
   
    }

$shipamt = round($shipamt);
$shipamt = pad($shipamt);
print MAIL \"Shipping:\";
print MAIL sprintf \"%57.57s\", \"\\$$shipamt\\n\";

### Taxes
if ($tax eq \'0\') {
    # Do nothing
    }
## For tax by state
elsif ($tax eq \'1\') {
    if (defined $taxes{$FORM{\'state\'}}) { $taxamt = $taxes{$FORM{\'state\'}}; }
    $taxtotal = $totalprice * $taxamt;
    $taxtotal = int($taxtotal * (10 ** 2) + .5) / (10 ** 2);
    $totalprice = $totalprice + $taxtotal;
    }
## Always tax
elsif ($tax eq \'2\') {
    $taxtotal = $totalprice * $taxamt;
    $taxtotal = int($taxtotal * (10 ** 2) + .5) / (10 ** 2);
    $totalprice = $totalprice + $taxtotal;
    }
## Format and print the tax

$taxtotal = round($taxtotal);
$taxtotal = pad($taxtotal);
print MAIL \"Taxes:\";
print MAIL sprintf \"%60.60s\", \"\\$$taxtotal\\n\";

## Format and print the totalprice
$totalprice = round($totalprice);
$totalprice = pad($totalprice);
print MAIL \"Total Price:\";
print MAIL sprintf \"%56.56s\" , \"\\$$totalprice\\n\\n\\n\";

## Print all the variables on the order form
undef %is_exclude;
for (@exclude) { $is_exclude{$_} = 1 }
  if ($CONFIG{\'sort\'} eq \'alphabetic\') {
      foreach $key (sort keys %FORM) {
        # Print the name and value pairs in FORM array to mail.
        print MAIL \"$key: $FORM{$key}\\n\\n\" unless $is_exclude{$key};
      }
  }
  elsif ($CONFIG{\'sort\'} =~ /^order:.*,.*/) {
      $CONFIG{\'sort\'} =~ s/order://;
      @sorted_fields = split(/,/, $CONFIG{\'sort\'});
      foreach $sorted_field (@sorted_fields) {
        # Print the name and value pairs in FORM array to mail.
        if ($FORM{$sorted_field}) {
            print MAIL \"$sorted_field: $FORM{$sorted_field}\\n\\n\" unless $is_exclude{$sorted_field};
        }
      }
  }
  else {
      foreach $key (keys %FORM) {
        # Print the name and value pairs in FORM array to html.
        print MAIL \"$key: $FORM{$key}\\n\\n\" unless $is_exclude{$key};
      }
  }
 
$totalprice = 0; 
print MAIL \"\\n\\nIf this appears incorrect please contact us ASAP.\\n\\n\";
close (MAIL);
}

# Get the hostname, for file reference
sub get_host {
$host = $ENV{\'REMOTE_HOST\'};
$reffile = \"$tmpdir$delim$storename-$host\";
}

sub check_file {
$exists = \'1\' if -e \"$reffile\";
if ($exists eq \'1\') {
    &parse_file;
    }
    else {
    &make_file;
    }
}

# Parse the cart
sub parse_file {
open (REFFILE,\"$reffile\") || die \"Can\'t Open $reffile(r): $!\\n\";
@LINES=<REFFILE>;
close(REFFILE);
$SIZE=@LINES;
$_=$LINES[0];
($usertime, $userhost, $useragent, $usercurr, $usership, $usertax) = split(/\\|/,$_);
}

# If they don\'t have a cart, make them one.
sub make_file {
open(REFFILE,\">$reffile\");
print REFFILE \"$date\\| $ENV{\'REMOTE_HOST\'}\\| $ENV{\'HTTP_USER_AGENT\'}\\|\";
if ($multicurr == 1) { print REFFILE \"NULL\\|\"; } else { print REFFILE \"1\\|\"; }
if ($multiship == 1) {
    open (SHIPDB, \"$basepath$delim$shipdb\");
    my(@SHIP) = <SHIPDB>;
    close(SHIPDB);
    my(@ships)=split(/\\|/,$SHIP[0]);
    print REFFILE \"$ships[0]\\|\\n\";
    }
    else { print REFFILE \"\\|\\n\" }
close(REFFILE);
}

#Delete a cart
sub delete_cart {
unlink($reffile);
}

# Delete an item from the cart
sub delete_item {
open (REFFILE,\"$reffile\") || print \"Content-type: text/html\\n\\n Can\'t Open $reffile(r): $!\\n\";
@LINES=<REFFILE>;
close(REFFILE);
$SIZE=@LINES;

open (REFFILE,\">$reffile\") || print \"Content-type: text/html\\n\\n Can\'t Open $reffile(r): $!\\n\";
print REFFILE \"$date\\| $ENV{\'REMOTE_HOST\'}\\| $ENV{\'HTTP_USER_AGENT\'}\\|NULL\\|NULL\\|\\n\";
for ($i=1;$i<$SIZE;$i++) {
      $_=$LINES[$i];
    my($itemname, $itemprice, $itemquant, $weight, $itemid) = split(/\\|/,$_);
    unless ($FORM{$itemid} eq $itemid) { print REFFILE $_; }
    }
close(REFFILE);
&review_items;
}

# Get the date.
sub get_date {
  @days =
(\'Søndag\',\'Mandag\',\'Tirsdag\',\'Onsdag\',\'Torsdag\',\'Fredag\',\'Lørdag\');
  @months = (\'January\',\'February\',\'March\',\'April\',\'May\',\'June\',\'July\',
              \'August\',\'September\',\'October\',\'November\',\'December\');

  ($sec,$min,$hour,$mday,$mon,$year,$wday) =
localtime(time);
  if ($hour < 10) { $hour = \"0$hour\"; }
  if ($min < 10) { $min = \"0$min\"; }
  if ($sec < 10) { $sec = \"0$sec\"; }

  $date = \"$days[$wday], $months[$mon] $mday, 19$year at $hour\\:$min\\:$sec\";

}

# Did they fill out all necasary info?
sub check_required {

  foreach $require (@required) {
      if ($require eq \'bgcolor\' ||
          $require eq \'background\' ||
          $require eq \'link_color\' ||
          $require eq \'vlink_color\' ||
          $require eq \'alink_color\' ||
          $require eq \'text_color\' ||
            $require eq \'sort\' ||
          $require eq \'title\' ||
          $require eq \'print_config\' ||
          $require eq \'return_link_title\' ||
          $require eq \'return_link_url\') {

        if (!($CONFIG{$require}) || $CONFIG{$require} eq \' \') {
            push(@ERROR,$require);
        }
      }
      elsif (!($FORM{$require}) || $FORM{$require} eq \' \') {
        push(@ERROR,$require);
      }
  }

  if (@ERROR) {
      &error(\'missing_fields\', @ERROR);
  }

}

# Warning Warning.
sub error {
&print_header;
  ($error,@error_fields) = @_;

if ($error eq \'missing_fields\') {


      print \"  <h1>Error: Blank Fields</h1>\\n\\n\";
      print \"The following fields were left blank in your submission form:<p>\\n\";

      # Print Out Missing Fields in a List.
      print \"<ul>\\n\";
      foreach $missing_field (@error_fields) {
        print \"<li>$missing_field\\n\";
      }
      print \"</ul>\\n\";

      # Provide Explanation for Error and Offer Link Back to Form.
      print \"<p><hr size=7 width=75\\%><p>\\n\";
      print \"These fields must be filled out before you can successfully submit\\n\";
      print \"the form.  Please return to the <a href=\\\"$ENV{\'HTTP_REFERER\'}\\\">Fill Out Form</a> and try again.\\n\";
  }
elsif ($error eq \'noship\') {
print \"<h2>Error: Shipping not Set!</h2><br> Please go back and calcualte shipping!\\n\";
}
&print_footer;
  exit;
}


sub track_order {
## First make a tracking id. A combo of the current time, and the PID.
$orderid = time() . \"-\" . $$;

# Write the order to the tracking DB.
open (TRACK, \">>$trackdb\");
print TRACK $orderid . \"\\|\";
foreach $key (%FORM) {
    print TRACK \"$key\\=$FORM{$key}\\&\";
    }
print TRACK \"\\|pending\\|NONE\\|\\n\";
close(TRACK);
}

sub number { $a <=> $b }


sub round {
my $number = shift;
$number = int($number * (10 ** 2) + .5) / (10 ** 2);
return $number;
}


sub pad {
my $number = shift;
if (index($number,\".\") >= 0) {
    my($left,$right) = split(/\\./,$number);
    if (length($right) == 0) {
        $number = $number . \"00\";
        }
        elsif (length($right) == 1) { $number = $number . \"0\"; }
    }
    else { $number = $number . \".00\"; }
return $number;
}

# END smart.cgi
Avatar billede alvion Nybegynder
14. december 2001 - 10:18 #1
aaarrrgghhh!!! :-)

Jo det kan du sagtens. Du bruger modulet DBI til at arbejde med databaser.

Du skal definere et tabelformat, som stemmer overens med det, der bruges i filen. Derefter kan du \"blot\" ændre de sub-rutiner, der arbejder på filerne til, at arbejde på databasen istedet.

Men hvis du ikke har arbejdet så meget med Perl før, vil jeg nok foreslå dig at starte med nogle mindre - og overskuelige - opgaver, inden du kaster dig over denne.
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester