| ความสามารถของโปรแกรมนี้
เลือกสินค้าได้ทีละรายการเท่านั้นจาก cart.htm
สามารถดูสินค้าในตะกร้าได้
การหยิบออก จะเป็นการหยิบทุกชิ้น ไม่สามารถหยิบบางชิ้นได้
เมื่อส่งรายการไปให้หลัง shop เสร็จ จะส่งให้ทั้งผู้ขาย และผู้ซื้อ
โปรแกรมย่อยมี ดังนี้
mailit, memail, themmail, checkout, viewcart, removeitem, totalit, additem, parse และ banner
ซึ่งคำอธิบาย subroutine สั้น ได้เขียนไว้ใน source แล้ว
 
 
 
#!/usr/bin/perl
##############################################################################
# Shopping Cart
# Source by Yingyos Santiprasert [sritrang@thaimail.com]
# http://www.thaistarter.com/
# copyright 2000. Distribute freely. Don't sell.
####
# แล้ว บุรินทร์ รุจจนพันธุ์ เอามาแก้ไขนิดหน่อย
# แล้วเปิด source ให้ดูผ่านเว็บครับ
# http://www.thaiall.com และ http://thaiall.hypermart.net
# สำหรับคำอธิบายเพิ่มเติมลองไปอ่านที่ http://www.thaiall.com/perl
# ผมเขียนทั้งแบบใช้ cookie และแบบไม่ใช้ cookie ครับ
# ระบบนี้จะนำคำสั่งซื้อของท่าน ส่งเป็น mail ไปให้คนขาย ไม่ได้ตัด stock
#
# โปรแกรม Shopping Cart ประกอบด้วย
# 1. /shopping/cart.cgi ซึ่งเขียนด้วย perl
# 2. /shopping/cookie.lib ซึ่ง download จาก เว็บฝรั่งของนาย matt
# 3. /shopping/cart.htm ซึ่งเป็นจุดใช้เพิ่มลดสินค้า
#
# การติดตั้ง
# 1. สร้างห้องชื่อ shopping ใน server ที่ให้บริการ perl เช่น hypermart.net
# 2. ส่ง 3 แฟ้มนี้ลงห้อง shopping ที่ http://thaiall.hypermart.net/shopping
# 3. กำหนดที่อยู่ของ Mail Program ซึ่ง hypermart เป็น /var/qmail/bin/qmail-inject
# 4. แก้ไขค่าต่อไปนี้ url เป็น URL ของคุณ และ เปลี่ยน myemail, from_email เป็นที่ชอบ
# สำหรับ email อย่าลืม เครื่องหมาย Slash นะสำหรับอีเมล์ ต้องใส่ก่อนเครื่องหมาย @
# เช่น x@hotmail.com ก็ต้องใส่ว่า x\@hotmail.com
# 5. อัพโหลดไฟล์ cart.cgi, cookie.lib และ cart.htm ในโหมด ASCII ในห้อง shopping
# เพราะที่ hypermart.net สามารถสั่ง perl หรือ cgi run จากห้องไหนก็ได้
# เขาไม่ได้กำหนดว่าต้อง run จากห้อง cgi หรือ cgi-bin เท่านั้น จึงสะดวกมาก
# 6. เปลี่ยนโหมด cart.cgi เป็น 755 แต่ผมทดสอบที่ hypermart.net เขาจัดการอัตโนมัติ
# 7. เพิ่ม ลด สินค้าในส่วนของ input hidden ในแฟ้ม cart.htm ได้
##############################################################################
# Shopping cart is a simple little program that allows you to use a shopping
# cart on your site with relatively little pain. It's also the only good
# FREE shopping cart I can think of, which is why I did it.
#
#     NOTE!!!!! Shopping Cart REQUIRES Matt Wright's "cookie.lib," which
#     I have included in the "sit" and "zip" files. If you got this by
#     viewing the source online, you MUST get Matt's "cookie.lib at
#     http://www.worldwidemart.com/scripts/
#
# This shopping cart uses e-mail, and is not secure. What this means is, although
# I have never heard of it actually happening, there is a remote chance that some
# person may intercept information generated by this form in transit. By using
# this script, you agree that it's not my fault if something gets intercepted.
#
#####################  HTML CONFIGURATION  #####################
# This cart uses regular form inputs from your html pages. Each form input must
# have the following:
#
# <form action=/shopping/cart.cgi method=POST>
# (this tells the browser where to find the script)
#
# <input type=hidden name=action value=AddItem>
# (this tells the script that we're adding something to the shopping cart)
#
# Quantity: <input type=text name=quantity>
# (this allows the user to select how many of each item she wants)
#
# Item: Acer pentium X
# <input type=hidden name=item value="Acer pentium X">
# (this tells the script exactly which item the user is ordering)
#
# Price: $10.45
# <input type=hidden name=price value=10.45>
# (this tells the script exactly how much to charge for the item)
#
# <hr><input type=submit value="Add to my cart">
# (this is the submit button that has to appear at the bottom of the form)
# </form>
###################  END HTML CONFIGURATION  ###################
#
###################  SCRIPT CONFIGURATION  ###################
#
# the variables you need to set are defined below.
#
# $mailprog tells the script where to find your server's mail program
# if you're unsure where it is, contact your system administrator.
$mailprog = '/var/qmail/bin/qmail-inject';
$url = "http://thaiall.hypermart.net";
# Change yourname
# myemail is your email address. If you use perl5, you'll need to put a slash
# (\) before the @ sign. (ex: my\@email.com)
# กำหนดให้ผู้รับ email คือ shopping@thaiall.com
$myemail = "shopping\@thaiall.com";
# $from_mail is the address you want to appear on emails you send to the user. This
# address will appear in the "from" portion of that email. Usually, it should be
# the same as the $myemail value, but in rare cases, you may want to put a different
# value here, so I gave you the opportunity.
# กำหนดให้ส่งจาก sendershopping@thaiall.hypermart.net
# แต่ผมไม่ได้ใช้ แต่ให้รับ seller จาก ฟอร์มแทน
$from_mail = "sendershopping\@thaiall.com";
# $them_mail tells the program whether to send email to the USER once they
# have ordered their items. 0=no, 1=yes.
$them_mail = 1;
# $me_mail tells the program whether to send email to YOU when someone orders.
# Since there's no reason to have a shopping cart unless you can get the order,
# you should leave this value alone.
$me_mail = 1;
# $newform tells the script where to find itself
$newform = "$url/shopping/cart.cgi";
$store = "$url/shopping/cart.htm";
# this should be left alone, unless you put "cookie.lib" in a directory other
# than the same directory as the script (although I can't see why you would do
# that.) If you do, this needs to tell the directory where the script is.
# You shouldn't have to configure "cookie.lib" at all. It should work just as it is.
require 'cookie.lib';
###################  YOU'RE DONE! CONGRATULATIONS!  #####################
&parse;
$referer = $ENV{'HTTP_REFERER'};
if ($contents{'action'} eq "AddItem") {	&AddItem; }
if ($contents{'action'} eq "RemoveItem") { &RemoveItem; }
if ($contents{'action'} eq "ViewCart") { &ViewCart; }
if ($contents{'action'} eq "MailIt") { &MailIt; }
#####################
# end of main program
######################################################################
# start mailit : mail ข้อมูลการสั่งซื้อไปให้ผู้ขาย หรือ seller
sub MailIt {
if (&GetCookies('cart')) {
 print "Content-type: text/html\n";
 $mailcookie = "$Cookies{'cart'}";
 $tobeadded = $mailcookie;
 @msg = split(/\|/, $mailcookie);
 &memail;
 &themmail;
 print "\n";
 print "<body bgcolor=white><h1>Order</h1>\n";
 print "<center>รายการสั่งซื้อของคุณเราได้รับแล้ว ขอขอบคุณมากสำหรับการสั่งซื้อ.<br>เราจะรีบจัดการส่งให้ท่านอย่างรวดเร็วที่สุด<br>\n";
 &banner;
 print "</body></html>";
 exit;
} else {
 print "Content-type: text/html\n\n";
 print "<body bgcolor=white><h1>Whoops</h1>\n";
 print "<center>คุณไม่สามารถดูรถเข็นคุณได้เพราะว่าไม่มี.</center>\n";
}
}
# end mailit
############ MEMAIL SUBROUTINE ############
# sends email to $myemail (you) informing you
# that someone has used this script. IF you don't
# want email, set the me_mail value to 0.
# ส่ง email แจ้งผู้ขาย
sub memail {
if ($me_mail eq '1') {
 open (MAIL, "|$mailprog") || die "Can't open $mailprog!\n";
#print MAIL "To: $from_mail\n";
 print MAIL "To: $contents{'seller'}\n";
 print MAIL "From: $contents{'email'}\n";
 print MAIL "Subject: shopping_cart \n\n";
 print MAIL "มีคนสั่งซื้อสินค้าดังนี้\n\n";
 print MAIL "สั่งซื้อโดย\n";
 print MAIL "$contents{'FirstName'} $contents{'LastName'}\n";
 print MAIL "ที่อยู่ : $contents{'StreetAddress'}\n";
 if ($contents{'Apt'}) {
  print MAIL " $contents{'Apt'}\n";
 }
 print MAIL "อำเภอ : $contents{'City'}, จังหวัด: $contents{'State'}\n";
 print MAIL "รหัสไปรษณีย์ : $contents{'PostalCode'} โทรศัพท์ : $contents{'phone'}\n";
 print MAIL "แฟกซ์ : $contents{'Fax'} E-mail: $contents{'email'}\n";
 print MAIL "การชำระเงิน: $contents{'CardType'} \n";
 print MAIL "\n";
 print MAIL "รายการสั่งซื้อ\n";
 foreach $msg (@msg) {
  ($mquantity, $mprice, $mitem) = split(/:/ , $msg);
  print MAIL "$mquantity $mitem  $mprice \n";
 }
 &TotalIt;
 print MAIL "รวมทั้งหมด: ";
 print MAIL "$TheTotal\n";
 print MAIL "==============================================\n";
 print MAIL "http://thaiall.hypermart.net/shopping/cart.htm";
 close (MAIL);
}
else {
 print "Content-type: text/html\n\n";
 print "<body bgcolor=white><h1>คุณไม่สามารถเช็คเอาต์ได้เพราะไม่มีรายการสินค้าในรถเข็นของคุณ\n";
 &banner;
 print "</body></html>";
 exit;
}
}
############ THEMMAIL SUBROUTINE ############
# This sends email to the person who signs the guestbook.
# ส่ง email แจ้งผู้ซื้อ
sub themmail {
if ($them_mail eq '1' && $contents{'email'}) {
 open (MAIL, "|$mailprog -t") || die "Can't open $mailprog!\n";
 print MAIL "To: $contents{'email'}\n";
 print MAIL "From: $from_mail\n";
 print MAIL "Subject: Thank you!\n\n";
 print MAIL "Dear $contents{'FirstName'},\n\n";
 print MAIL "ขอขอบคุณครับสำหรับรายการสั่งซื้อจากคุณ\n\n";
 print MAIL "-----------------------------------------------\n";
 print MAIL "สั่งซื้อโดย\n";
 print MAIL "$contents{'FirstName'} $contents{'LastName'}\n";
 print MAIL "ที่อยู่ : $contents{'StreetAddress'}\n";
 if ($contents{'Apt'}) {
  print MAIL "$contents{'Apt'}\n";
 }
 print MAIL "อำเภอ : $contents{'City'}, จังหวัด: $contents{'State'}\n";
 print MAIL "รหัสไปรษณีย์ : $contents{'PostalCode'} โทรศัพท์ : $contents{'phone'}\n";
 print MAIL "แฟกซ์ : $contents{'Fax'} E-mail: $contents{'email'}\n";
 print MAIL "การชำระเงิน: $contents{'CardType'} \n";
 print MAIL "\n";
 print MAIL "รายการสั่งซื้อ\n";
 foreach $msg (@msg) {
  ($mquantity, $mprice, $mitem) = split(/:/ , $msg);
  print MAIL "$mquantity $mitem  $mprice \n";
 }
 &TotalIt;
 print MAIL "รวมทั้งหมด: ";
 print MAIL "$TheTotal\n";
 print MAIL "==============================================\n";
 print MAIL "http://thaiall.hypermart.net/shopping/cart.htm";
 close (MAIL);
}
}
# start checkout : ฟอร์ม ส่ง email ให้ผู้ขาย และผู้ซื้อ
sub CheckOut {
print "<p><hr noshade size=1><p><form action=$newform method=POST>\n";
print "<center><h3>Check out</h3></center><input type=hidden name=action value=MailIt>\n";
print 'E-Mail ผู้ขาย : <input type=text name=seller value="webmaster@thaiall.com" size=30> (ให้เปลี่ยนเป็น E-Mail ของท่าน)<hr width=50%>';
print "<table border=\"0\" cellpadding=\"3\" align=\"center\"><tr><td align=right>ชื่อผู้สั่ง:</td><td><input type=text name=FirstName></td>";
print "<td align=right> นามสกุล :</td><td><input type=text name=LastName></td></tr>\n";
print "<tr><td align=right>ที่อยู่ : </td><td><input type=text name=StreetAddress></td><td align=right>ที่อยู่</td><td><input type=text name=AptNumber></td></tr>\n";
print "<tr><td align=right>อำเภอ</td><td><input type=text name=City></td><td align=right>จังหวัด</td><td><input type=text name=State></td></tr>\n";
print "<tr><td align=right>รหัสไปรษณีย์</td><td><input type=text name=PostalCode></td><td align=right>โทรศัพท์</td><td><input type=text name=Phone></td></tr>\n";
print "<tr><td align=right>E-mail</td><td><input type=text name=email></td><td align=right>แฟกซ์</td><td><input type=text name=Fax></td></tr>\n";
print "<tr><td align=right>การชำระเงิน</td><td><select name=CardType><option value=พกง.>พัสดุเก็บเงินปลายทาง<option value=โอนเงิน.>โอนเงินเข้าบัญชี</select></td>\n";
print "<td colspan=2><input type=Submit value=\"ตกลง\"><input type=Reset value=\"ล้างข้อมูล\"></form></td></tr></table>\n";
}
# end checkout
# start viewcart : ดูสินค้าในตะกร้า
sub ViewCart {
if (&GetCookies('cart')) {
 print "Content-type: text/html\n";
 $viewcookie = "$Cookies{'cart'}";
 $tobeadded = $viewcookie;
 @chocolate = split(/\|/, $viewcookie);
 print "\n";
 print "<body bgcolor=white>\n";
 print "<center>คุณอาจจะกดปุ่มรีเฟรชหน้านี้เพื่อดูรายการล่าสุดในตะกร้าของคุณ.</center><br>\n";
 print "<table border=\"0\" align=\"center\"><tr><td>จำนวน.</td><td>รายการ</td><td>ราคา</td><td></td></tr>\n";
 $RemoveNumber=0;
 foreach $chocolate (@chocolate) {
  ($quantity, $price, $item) = split(/:/ , $chocolate);
  if ($quantity gt "0") {
   print "<tr><td>$quantity</td><td>$item</td><Td>$price </td><td>  <a href=$newform?action=RemoveItem\&number=$RemoveNumber>หยิบออก</a></td></tr>\n";
  }
  $RemoveNumber++;
 }
 &TotalIt;
 print "<tr><td colspan=3 align=right>ทั้งหมด:</td><td><b>";
 printf("%.2f", $TheTotal);
 print "</b></td></tr>\n";
 print "</table>\n";
 &CheckOut;
 print "<a href=$store>กลับไปที่หน้าร้าน</a>\n";
 &banner;
 print "</body></html>";
}else{
 print "Content-type: text/html\n\n";
 print "<body bgcolor=white><h1>Whoops</h1>\n";
 print "<center>ในตะกร้าของคุณว่างเปล่า.<./center>\n";
 print "<a href=$store>กลับไปที่หน้าร้าน</a>\n";
 &banner;
 print "</body></html>";
}
}
# end viewcart
# start removeitem : ลบทีเกลี้ยงตะกร้าเลยครับ
sub RemoveItem {
if (&GetCookies('cart')) {
 print "Content-type: text/html\n";
 (@ToBeModified) = split(/\|/, $Cookies{'cart'});
 $KillNumber = $contents{'number'};
 splice(@ToBeModified,$KillNumber,1,());
 &SetCookies('cart',@ToBeModified);
 print "\n";
 print "<html><head><title>Deleted</title></head><body bgcolor=white><h1>รายการที่ถูกลบ</h1>\n";
 print "<center>ในรถเข็นคุณบรรจุ</center>\n";
 print "<table border=\"0\" align=\"center\"><tr><td>จำนวน</td><td>รายการ</td><td>ราคา</td><td></td></tr>\n";
 $RemoveNumber=0;
 foreach $ToBeModified (@ToBeModified) {
  ($quantity, $price, $item) = split(/:/ , $ToBeModified);
  if ($RemoveNumber > 0) {
  print "<tr><td>$quantity</td><td>$item</td><Td>$price </td><td>  <a href=$newform?action=RemoveItem\&number=$RemoveNumber>หยิบออก</a></td></tr>\n";
  }
  $RemoveNumber++;
  $tobeadded = $ToBeModified;
 } #end foreach
 &TotalIt;
 print "<tr><td colspan=3 align=right>รวมทั้งหมด :</td><td><b>";
 printf("%.2f", $TheTotal);
 print "</b></td></tr>\n";
 print "</table>\n";
 &CheckOut;
 print "<a href=$store>กลับไปที่หน้าร้าน</a>\n";
 &banner;
 print "</body></html>";
} else {
 print "<center>คุณจะต้องมีรายการสินค้าในรถเข็นของคุณเพื่อเอาออก.</center>\n";
 print "<a href=$store>กลับไปที่หน้าร้าน</a>\n";
 &banner;	
 print "</body></html>";
 exit;
} #end else
} #end sub
# end removeitem
# start totalit : คำนวณผลรวมเป็นเงิน
sub TotalIt {
 $RunningTotal = 0;
 (@tobetotaled) = split(/\|/, $tobeadded);
 foreach $tobetotaled (@tobetotaled) {
  ($HowMany, $HowMuch, $What) = split(/:/, $tobetotaled);
  $ItemTotal = $HowMany * $HowMuch;
  $RunningTotal = $RunningTotal + $ItemTotal;
 }
 $TheTotal = $RunningTotal;
}
# end TotalIt
# start additem : เพิ่มสินค้าในตะกร้า
sub AddItem {
if (&GetCookies('cart')) {
 print "Content-type: text/html\n";
 $tobeadded = "$Cookies{'cart'}" . "\|" . "$contents{'quantity'}" . ":" . "$contents{'price'}" . ":" . "$contents{'item'}";
 &SetCookies('cart',$tobeadded);
 &TotalIt;
 print "\n";
 print "<html><head><title>Item added</title></head>\n";
 print "<body bgcolor=white>\n";
 print "  <center><h1>สินค้านี้คุณได้เลือก !</h1></center>\n";
 $oldcookie = "$Cookies{'cart'}";
 @dough = split(/\|/, $oldcookie);
 print "คุณได้เพิ่ม $contents{'quantity'} $contents{'item'}  $contents{'price'} บรรจุในรถเข็นคุณเป็นที่เรียบร้อยแล้ว :\n";
 print "<table border=\"0\" align=\"center\"><tr><td>จำนวน.</td><td>รายการ</td><td>ราคา</td></tr>\n";
 $RemoveNumber=0;
 foreach $dough (@dough) {
  ($quantity, $price, $item) = split(/:/ , $dough);
  if ($RemoveNumber > 0) {
  print "<tr><td>$quantity</td><td>$item</td><Td>$price </td><td>  <a href=$newform?action=RemoveItem\&number=$RemoveNumber>หยิบออก</a>  </td></tr>\n";
  }
  $RemoveNumber++;
 }
 print "<tr><td colspan=3 align=right>Total:</td><td><b>";
 printf("%.2f", $TheTotal);
 print "</b></td></tr>\n";
 print "</table>\n";
 &CheckOut;
 print "<a href=$store>กลับไปที่หน้าร้าน</a>\n";
 &banner;
 print "</body></html>";
} else {
 print "Content-type: text/html\n";
 $newitem = "$contents{'quantity'}" . ":" . "$contents{'price'}" . ":" . "$contents{'item'}";
 &SetCookies('cart',$newitem);
 print "\n";
 print "<html><head><title>Item added</title></head><body bgcolor=white>\n";
 print "  <center><h1>รายการที่เพิ่มขึ้น</h1>\n";
 print "คุณได้เพิ่ม $contents{'quantity'} $contents{'item'} $contents{'price'} ในรถเข็นของคุณ.\n";
 $NewQuantity = $contents{'quantity'};
 $NewPrice = $contents{'price'};
 $NewTotal = $NewQuantity * $NewPrice;
 print "<p>ยอดรวมทั้งหมดขณะนี้คือ ";    printf("%.2f", $NewTotal);
 &CheckOut;
 print "<a href=$store>กลับไปที่หน้าร้าน</a>\n";
 &banner;
 print "</body></html>\n";
}
}
# end AddItem
# start parse : ปรับค่าที่รับเข้ามา
sub parse {
 read(STDIN, $input, $ENV{'CONTENT_LENGTH'});
 $input = $ENV{'QUERY_STRING'} if $ENV{'QUERY_STRING'};
 @pairs = split(/&/, $input);
 foreach $pair (@pairs) {
  ($name, $value) = split(/=/, $pair);
  $value =~ tr/+/ /;
  $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  $contents{$name} = $value;
 }
}
# end parse
# start banner : เพื่อไม่ให้ hypermart แสดง banner ในหน้าต่างใหม่
sub banner {
print "<hr><center>";
print "Support by <a href=http://www.thaiall.com target=_blank>Thaiall.com</a>";
print "<br>ICQ : 20449588<br>";
print '<!--#echo banner=""--><br>';
}
# end banner
; |