File : cartprocess.pl. ID : 1002
Skin : Default | Sons-of-obsidian | Sunburst | Highlighter | Frame
#!/usr/bin/perl
&parse;
$mailprog='/var/qmail/bin/qmail-inject';
if ($config{'action'} eq "additem") { &additem; }
elsif ($config{'action'} eq "removeitem") { &removeitem; }
elsif ($config{'action'} eq "viewcart") { &viewcart; }
elsif ($config{'action'} eq "tomail") { &tomail; }
else { 
  print "Content-type: text/html\n\n"; 
  print "Nothing to do"; exit; 
}
sub tomail {
  print "Content-type: text/html\n";
  if (&GetCookies('cart')) {
    $mailcookie = "$Cookies{'cart'}";
    $tobeadded = $mailcookie;
    @msg = split(/\|/, $mailcookie);
    if ($config{'email'} && @msg > 0) {
      &sendmail;
      print "\n Your order is processed .. Thank you ";
    } else { 
      print "\n Nothing in cart or invalid e-mail\n";
    }
  } else {
    print "\n Nothing in cart";
  }
}
sub sendmail {
  open (MAIL, "|$mailprog") || die "Can't open $mailprog!\n";  
  print MAIL "To: webmaster\@thaiall.com,$config{'email'}\n";
  print MAIL "From: $config{'email'}\n";  
  print MAIL "Subject: shopping cart \n\n";
  print MAIL "Dear sir\n Thank for your order\n";
  print MAIL "Order by $config{'Name'}\n Address : $config{'Addr'}\n";
  print MAIL "Pay type: $config{'CardType'} \n\n Order listing\n";
  foreach $msg (@msg) {
    ($mquantity, $mprice, $mitem) = split(/:/ , $msg);
    print MAIL "$mitem  $mprice $mquantity\n";
  }
  &totalmny;
  print MAIL "Total: $TheTotal\n";
  print Mail "From IP: $ENV{'HTTP_REFERER'}";
  close (MAIL);
}
sub viewcart {
  print "Content-type: text/html\n";
  if (&GetCookies('cart')) {
    $viewcookie = "$Cookies{'cart'}";
    $tobeadded = $viewcookie;
    @dcarts = split(/\|/, $viewcookie);
    print "\n View cart";
    &incart; 
  } else {
    print "\n Empty";
  }
}
sub removeitem {
  print "Content-type: text/html\n"; 
  if (&GetCookies('cart')) {
    (@ToBeModified) = split(/\|/, $Cookies{'cart'});
    $KillNumber = $config{'number'};
    splice(@ToBeModified,$KillNumber,1);
    $tobeadded = ""; 
    foreach  $restd (@ToBeModified) { 
      if ($tobeadded eq "") { $tobeadded = $restd; 
      } else { $tobeadded = join "|",$tobeadded,$restd; }
      push @dcarts,$restd;
    } 
    &SetCookies('cart',$tobeadded);
    print "\n Remove product in cart";	
    &incart; 
  }
}
sub totalmny {
  $RunningTotal = 0;
  (@tobetotaled) = split(/\|/, $tobeadded);
  foreach $tobetotaled (@tobetotaled) {
    ($HowMany, $HowMuch, $What) = split(/:/, $tobetotaled);
    $ItemTotal = $HowMany * $HowMuch;
    $RunningTotal = $RunningTotal + $ItemTotal;
  }
  $TheTotal = $RunningTotal;
}
sub additem {
  print "Content-type: text/html\n";  
  if (&GetCookies('cart')) {
    $tobeadded="$Cookies{'cart'}\|$config{'quantity'}:$config{'price'}:$config{'item'}";
    &SetCookies('cart',$tobeadded);
    &totalmny;
    @dcarts = split(/\|/, $tobeadded);
    print "\n <b><u>Add new product</u></b>"; 
  } else {
    $newitem = "$config{'quantity'}:$config{'price'}:$config{'item'}";
    &SetCookies('cart',$newitem); 
    $tobeadded = $newitem;
    &totalmny;
    @dcarts = split(/\|/, $newitem);
    print "\n <b><u>First product</u></b>"; 
  }
  print "<br>Product:$config{'item'}<br>Quantity:$config{'quantity'}";
  print "<br>Price:$config{'price'}<hr>";
  &incart;
}
sub parse {
  if ($ENV{'REQUEST_METHOD'} eq 'GET') {
    @pairs = split(/&/, $ENV{'QUERY_STRING'});
  } elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
    read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
    @pairs = split(/&/, $buffer);
  }  
  foreach $pair (@pairs) {
    ($name, $value) = split(/=/, $pair);
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $config{$name} = $value;
  }
}
sub incart {
  $RemoveNumber=0;
  print "<table><tr><td>No</td><td>Product</td><td>Amount</td><td>Price</td><td>Total</td></tr>";
  foreach $dcart (@dcarts) {
    ($quantity, $price, $item) = split(':',$dcart);  
    if ($quantity > "0") {
       $seq = $RemoveNumber + 1;
       print "<tr><td>$seq</td><td>$item</td><td>$quantity</td><td>$price</td><td align=right>";
       printf("%.2f", $price*$quantity);
       print "</td><td><a href=cartprocess.pl?action=removeitem\&number=$RemoveNumber>";
	   print "Remove</a></td></tr>";
      $RemoveNumber++;
    }
  }
  &totalmny;
  print "<tr><td colspan=4 align=right>Total:</td><td align=right>";
  printf("%.2f", $TheTotal);
  print "</td></tr></table>";
  print "<a href=cartprocess.pl?action=viewcart>viewcart</a> | <a href=cartprocess.htm>Product</a>";
  print "<hr><form action=cartprocess.pl method=post><input type=hidden name=action value=tomail>";
  print "<b>Check out</b><table><tr><td>E-mail</td><td><input type=text name=email></td></tr>";
  print "<tr><td>Name-Surn</td><td><input type=text name=Name></td></tr>";
  print "<tr><td>Address</td><td><input type=text name=Addr></td></tr>";
  print "<tr><td>Pay type</td><td>";
  print "<select name=CardType><option value=พกง>พกง<option value=โอนเงิน>โอนเงิน</select>";
  print "</td></tr><tr><td><input type=Submit></td></tr></table></form>";
}
sub GetCookies {
  local(@ReturnCookies) = @_;
  local($cookie_flag) = 0;
  local($cookie,$value);
  if ($ENV{'HTTP_COOKIE'}) {
    foreach (split(/; /,$ENV{'HTTP_COOKIE'})) {
      ($cookie,$value) = split(/=/); 
      foreach $ReturnCookie (@ReturnCookies) {
        if ($ReturnCookie eq $cookie && $value) {
          $Cookies{$cookie} = $value;
          $cookie_flag = 1;
          $cookiename = $cookie;
        }
      }
    }
  }
  return $cookie_flag;
}
sub SetCookies {
  local(@cookies) = @_;  
  ($cookie,$value) = @cookies;  
  print 'Set-Cookie: '.$cookie.'='.$value."\n";
}