#!/usr/local/bin/perl

#┌────────────────────────
#│ mart_order.cgi - 2004/04/01
#│ Copyright (c) KentWeb
#└────────────────────────

# 外部ファイル取り込み
require './jcode.pl';
require './mart_init.cgi';

# 基本処理定義
&decode(1);
if ($mode eq "note") { &note; }
elsif ($mode eq "addr") { &addr; } # Step1: 住所入力画面
elsif ($mode eq "view") { &view; } # Step2: 最終確認画面
elsif ($mode eq "send") { &send; } # Step3: 確定送信
#elsif ($mode eq "zipcode" && $zip_coop) { require $zipcode; &zipcode; }
&error("不明な処理です");

#-------------------------------------------------
# 住所入力画面 (Step1)
#-------------------------------------------------
sub addr {
local($cnam,$ckana,$ceml,$czip,$cpref,$caddr,$ctel,$cfax,$cnam2,$ckana2,
$czip2,$cpref2,$caddr2,$ctel2,$cfax2,$id,$code,$num,$size,$col,
$prf,$pri,$pref,$postage,@cart);

# 買物情報取得
@cart = split(/\0/, $in{'cart'});

if (@cart == 0) {
&error("買物カゴの中身が空です。<br>
ブラウザのクッキー設定がオンになっているかをご確認ください。");
}

local(@cook)=();
foreach (@cart) {
($id,$code,$num,$size,$col) = split(/;/);

if (defined($in{"num:$code:$size:$col"})) { $num = $in{"num:$code:$size:$col"}; }
push(@cook,"$id,$code,$num,$size,$col");

$param .= "<input type=hidden name=cart value=\"$id;$code;$num;$size;$col\">\n";
}

# クッキー更新
&set_cookie(@cook);

# 前画面からの戻りの場合
if ($in{'job'} eq "back") {

($cnam,$ckana,$ceml,$czip,$cpref,$caddr,$ctel,$cfax,$cnam2,$ckana2,$czip2,
$cpref2,$caddr2,$ctel2,$cfax2) = ($in{'name'},$in{'kana'},$in{'email'},$in{'zip'},
$in{'pref'},$in{'addr'},$in{'tel'},$in{'fax'},
$in{'name2'},$in{'kana2'},$in{'zip2'},$in{'pref2'},$in{'addr2'},$in{'tel2'},$in{'fax2'});

# 顧客情報のクッキー取り出し
} else {

($cnam,$ckana,$ceml,$czip,$cpref,$caddr,$ctel,$cfax,$cnam2,$ckana2,
$czip2,$cpref2,$caddr2,$ctel2,$cfax2) = &get_cookie2();
}

# 改行復元
$caddr =~ s/<br>/\n/g;
$caddr2 =~ s/<br>/\n/g;
$in{'memo'} =~ s/&lt;br&gt;/\n/g;

# データ読み取り
&get_data;

# 画面出力
&header("addr", "", "no-cashe");
print <<EOM;
[ <a href="$home" target="_top">ホームに戻る</a>
| <a href="$script?mode=note">特定商取引法の表\示</a> ]<br>
<div align="center">
EOM

# 進行パネル
&panel(1);

# 送料で有償の地区があるかをチェック
$f=0;
foreach (0 .. $#pref) {
($prf,$pri) = split(/,/, $pref[$_]);

if ($pri > 0) { $f++; last; }
}

# 買物カゴの中身
&cart_tbl('addr', $f);

# チェックマーク
$chk = "<span style='color:red'>※</span>";
$w_l = 100;
$w_r = 400;

print <<EOM;
<p>
<form action="$order" method="POST" name="adrForm">
<input type=hidden name=mode value="view">
<input type=hidden name=back value="$in{'back'}">
$param
<table width=500><tr><td>
<ul>
<li>下記フォームに必要事項をご記入ください。
<li>$chkのマークは入力必須のフィールドです。
</ul>
</td></tr></table>
<Table border=0 cellspacing=0 cellpadding=0 width="500">
<Tr><Td bgcolor="$col1">
<table border=0 cellspacing=1 cellpadding=4 width="100%">
<tr bgcolor="$col1">
<td colspan=2 bgcolor="$col3">▼お支払・配達方法</td>
</tr>
<tr bgcolor="$col1">
<td bgcolor="$col2" width="$w_l">&nbsp; 支払方法 $chk</td>
<td bgcolor="$col2" width="$w_r">
EOM

foreach (0 .. $#payment) {
($pay,$cost) = split(/,/, $payment[$_]);

if (($in{'payment'} eq $_) || ($in{'payment'} eq "" && $_ == 0)) {
print "<input type=radio name=payment value=$_ checked>$pay\n";
} else {
print "<input type=radio name=payment value=$_>$pay\n";
}
}

print <<EOM;
</td>
</tr>
<tr bgcolor="$col1">
<td bgcolor="$col2" width="$w_l">&nbsp; 配達日時</td>
<td bgcolor="$col2" width="$w_r">希望日:
<input type=text name=mon size=6 value="$in{'mon'}" style="ime-mode:inactive"> 月
<input type=text name=day size=6 value="$in{'day'}" style="ime-mode:inactive"> 日<br>
時間帯:
<select name=deli>
<option value="">▼選択
EOM

foreach (0 .. $#deli) {
if ($in{'deli'} eq $_) {
print "<option value=\"$_\" selected>$deli[$_]\n";
} else {
print "<option value=\"$_\">$deli[$_]\n";
}
}

print <<EOM;
</select>
</td>
</tr>
</table>
</Td></Tr></Table>
<p>
<Table border=0 cellspacing=0 cellpadding=0 width="500">
<Tr><Td bgcolor="$col1">
<table width="100%" border=0 cellspacing=1 cellpadding=4>
<tr bgcolor="$col1">
<td colspan=2 bgcolor="$col3">▼ご注文者</td>
</tr>
<tr>
<td bgcolor="$col2" width="$w_l">&nbsp; お名前 $chk</td>
<td bgcolor="$col2" width="$w_r"><input type=text name=name size=30 value="$cnam">
(例 : 山田 太郎)</td>
</tr>
<tr bgcolor="$col1">
<td bgcolor="$col2" width="$w_l">&nbsp; ふりがな</td>
<td bgcolor="$col2" width="$w_r"><input type=text name=kana size=30 value="$ckana">
(例 : やまだ たろう)</td>
</tr>
<tr bgcolor="$col1">
<td bgcolor="$col2" width="$w_l">&nbsp; 電子メール $chk</td>
<td bgcolor="$col2" width="$w_r">
<input type=text name=email size=30 value="$ceml" style="ime-mode:inactive">
(例 : taro\@email.xx.jp)</td>
</tr>
<tr bgcolor="$col1">
<td bgcolor="$col2" width="$w_l">&nbsp; 郵便番号 $chk</td>
<td bgcolor="$col2" width="$w_r">
<input type="text" name="zip" size="15" value="$czip" style="ime-mode:inactive">
(例 : 100-1234)
EOM

if ($zip_coop) {
print "&nbsp; [<a href=\"javascript:zipcode(1)\">住所検索</a>]";
}

print <<EOM;
</td>
</tr>
<tr bgcolor="$col1">
<td bgcolor="$col2" width="$w_l">&nbsp; ご住所 $chk</td>
<td bgcolor="$col2" width="$w_r">
<select name="pref">
<option value="">▼選択
EOM

foreach (0 .. $#pref) {
($pref,$postage) = split(/,/, $pref[$_]);
if ($cpref eq $_) {
print "<option value=\"$_\" selected>$pref\n";
} else {
print "<option value=\"$_\">$pref\n";
}
}

print <<EOM;
</select> (都道府県)<br>
<textarea name=addr cols=40 rows=3 wrap=soft>$caddr</textarea></td>
</tr>
<tr bgcolor="$col1">
<td bgcolor="$col2" width="$w_l">&nbsp; 電話番号 $chk</td>
<td bgcolor="$col2" width="$w_r">
<input type=text name=tel size=25 value="$ctel" style="ime-mode:inactive">
(例 : 03-1234-1000)</td>
</tr>
<tr bgcolor="$col1">
<td bgcolor="$col2" width="$w_l">&nbsp; FAX番号</td>
<td bgcolor="$col2" width="$w_r">
<input type=text name=fax size=25 value="$cfax" style="ime-mode:inactive">
(例 : 03-1234-1001)</td>
</tr>
</table>
</Td></Tr></Table>
<p>
<Table border=0 cellspacing=0 cellpadding=0 width="500">
<Tr><Td bgcolor="$col1">
<table width="100%" border=0 cellspacing=1 cellpadding=4>
<tr bgcolor="$col1">
<td colspan=2 bgcolor="$col3">▼配送先
(ご注文者の住所と配送先が異なる場合)</td>
</tr>
<tr bgcolor="$col1">
<td bgcolor="$col2" width="$w_l">&nbsp; お名前</td>
<td bgcolor="$col2" width="$w_r"><input type=text name=name2 size=30 value="$cnam2">
(例 : 山田 太郎)</td>
</tr>
<tr bgcolor="$col1">
<td bgcolor="$col2" width="$w_l">&nbsp; ふりがな</td>
<td bgcolor="$col2" width="$w_r"><input type=text name=kana2 size=30 value="$ckana2">
(例 : やまだ たろう)</td>
</tr>
<tr bgcolor="$col1">
<td bgcolor="$col2" width="$w_l">&nbsp; 郵便番号</td>
<td bgcolor="$col2" width="$w_r">
<input type="text" name="zip2" size="15" value="$czip2" style="ime-mode:inactive">
(例 : 103-1234)
EOM

if ($zip_coop) {
print "&nbsp; [<a href=\"javascript:zipcode(2)\">住所検索</a>]</td>";
}

print <<EOM;
</tr>
<tr bgcolor="$col1">
<td bgcolor="$col2" width="$w_l">&nbsp; ご住所</td>
<td bgcolor="$col2" width="$w_r">
<select name="pref2">
<option value="">▼選択
EOM

foreach (0 .. $#pref) {
($pref,$postage) = split(/,/, $pref[$_]);
if ($cpref2 eq $_) {
print "<option value=\"$_\" selected>$pref\n";
} else {
print "<option value=\"$_\">$pref\n";
}
}

print <<EOM;
</select> (都道府県)<br>
<textarea name=addr2 cols=40 rows=3 wrap=soft>$caddr2</textarea></td>
</tr>
<tr bgcolor="$col1">
<td bgcolor="$col2" width="$w_l">&nbsp; 電話番号</td>
<td bgcolor="$col2" width="$w_r">
<input type=text name=tel2 size=25 value="$ctel2" style="ime-mode:inactive">
(例 : 03-1234-1000)</td>
</tr>
<tr bgcolor="$col1">
<td bgcolor="$col2" width="$w_l">&nbsp; FAX番号</td>
<td bgcolor="$col2" width="$w_r">
<input type=text name=fax2 size=25 value="$cfax2" style="ime-mode:inactive">
(例 : 03-1234-1001)</td>
</tr>
</table>
</Td></Tr></Table>
<p>
<Table border=0 cellspacing=0 cellpadding=0 width="500">
<Tr><Td bgcolor="$col1">
<table width="100%" border=0 cellspacing=1 cellpadding=4>
<tr bgcolor="$col1">
<td colspan=2 bgcolor="$col3">▼その他</td>
</tr>
<tr bgcolor="$col1">
<td bgcolor="$col2" width="$w_l">&nbsp; 連絡事項</td>
<td bgcolor="$col2" width="$w_r">
<textarea name=memo cols=40 rows=3 wrap=soft>$in{'memo'}</textarea></td>
</tr>
<tr bgcolor="$col1">
<td bgcolor="$col2" width="$w_l">&nbsp; クッキー保存</td>
<td bgcolor="$col2" width="$w_r">
<input type=checkbox name=cook value="1" checked>
注文者・配送先情報をブラウザに記憶</td>
</tr>
</table>
</Td></Tr></Table>
<p>
<table cellpadding=5>
<tr>
<td valign=top>
<input type=button value="&lt;&lt; 前の画面に戻る" onClick=window.open
("$script?back=$in{'back'}","_self")>
</td>
<td width=10></td>
<td valign=top>
<input type=submit value="確認画面へ進む &gt;&gt;">
</form>
</td>
</tr>
</table>
※消費税や送料が別途かかる場合には最終確認画面で表\示されます.
<!-- 著作権表\記削除不可 ($ver) -->
<p style="font-size:10px;font-family:Verdana,Helvetica,Arial">
- <a href="http://www.kent-web.com/">WebMart</a> -
</p>
</div>
</body>
</html>
EOM
exit;
}

#-------------------------------------------------
# 確認画面 (Step2)
#-------------------------------------------------
sub view {
local($param,$param2,$id,$code,$num,$size,$col,$pref,$pref2,
$postage,$pay,$cost,$date,$w_l,$w_r,$deli2,$stock_check,@cart,@cook);

# 入力確認
if ($in{'payment'} eq "") { &error("支払方法が未選択です"); }
if ($in{'name'} eq "") { &error("名前が未入力です"); }
if ($in{'email'} !~ /[\w\.\-]+\@[\w\.\-]+\.[a-zA-Z]{2,6}$/) {
&error("電子メールの入力が不正です");
}
if ($in{'pref'} eq "") { &error("都道府県が未選択です"); }
while ($in{'addr'} =~ /<br>$/) { $in{'addr'} =~ s/<br>$//; }
if ($in{'addr'} eq "") { &error("住所が未入力です"); }
if ($in{'tel'} eq "") { &error("電話番号が未入力です"); }

# 顧客情報をクッキー格納
if ($in{'cook'} == 1) {
&set_cookie2($in{'name'},$in{'kana'},$in{'email'},$in{'zip'},$in{'pref'},$in{'addr'},
$in{'tel'},$in{'fax'},$in{'name2'},$in{'kana2'},$in{'zip2'},$in{'pref2'},$in{'addr2'},$in{'tel2'},$in{'fax2'});
}

# 買物情報取得
@cart = split(/\0/, $in{'cart'});
if (@cart == 0) { &error("買物情報がありません"); }

# 在庫
if ($stock) {
local($id,$zan);

# ロック開始
&lock if ($lockkey);

%stock=();
open(IN,"$stkfile") || &error("Open Error: $stkfile");
while (<IN>) {
($id,$zan) = split(/<>/);
$stock{$id} = $zan;
}
close(IN);

# ロック解除
&unlock if ($lockkey);
}

# データ読み取り
&get_data;

@cook=();
$param="";
$stock_check=0;
foreach (@cart) {
($id,$code,$num,$size,$col) = split(/;/);
push(@cook,"$id,$code,$num,$size,$col");

$param .= "<input type=hidden name=cart value=\"$id;$code;$num;$size;$col\">\n";

# 在庫数チェック
if ($stock) {
if ($stock{$code} - $num < 0) {
$stock_check++;
last;
}
}
}

# 在庫切れ
if ($stock_check) {
($name) = (split(/<>/, $cart{$code}))[1];

&error("大変申\し訳ありません。<br>「$name」は在庫切れです
(現在の在庫数:$stock{$code})<br>たった今、他の方からの購入があったようです");
}

&header("", "", "no-cashe");
print <<EOM;
[ <a href="$home" target="_top">ホームに戻る</a>
| <a href="$script?mode=note">特定商取引法の表\示</a> ]<br>
<div align="center">
EOM

# 進行パネル
&panel(2);

print "<p><b>以下の内容で注文を受け付けます。ご確認ください。</b><p>\n";

# 都道府県 & 送料
$pref = $pref2 = "";
$postage = 0;
if ($in{'pref2'} ne "") {
($pref2,$postage) = split(/,/, $pref[$in{'pref2'}]);
($pref) = split(/,/, $pref[$in{'pref'}]);
} else {
($pref,$postage) = split(/,/, $pref[$in{'pref'}]);
}

# 支払方法の手数料
($pay,$cost) = split(/,/, $payment[$in{'payment'}]);

# 買物カゴの中身
&cart_tbl('view', $postage, $cost);

# 配達時間
$date='';
if ($in{'mon'} ne "" && $in{'day'} ne "") { $date = "$in{'mon'}月$in{'day'}日"; }
if ($in{'deli'} eq "") { $deli2 = ""; }
else { $deli2 = $deli[$in{'deli'}]; }

# 郵便番号
$in{'zip'} =~ s/(\d{3})(\d{4})/$1-$2/;
$in{'zip2'} =~ s/(\d{3})(\d{4})/$1-$2/;

$w_l = 100;
$w_r = 400;

# 確認画面
print <<EOM;
<p>
<Table border=0 cellspacing=0 cellpadding=0 width="500">
<Tr><Td bgcolor="$col1">
<table width="100%" border=0 cellspacing=1 cellpadding=4>
<tr bgcolor="$col1">
<td colspan=2 bgcolor="$col3">▼お支払・配達方法</td>
</tr>
<tr bgcolor="$col1">
<td width="$w_l" bgcolor="$col2">&nbsp; 支払方法</td>
<td width="$w_r" bgcolor="$col2">$pay</td>
</tr>
<tr bgcolor="$col1">
<td width="$w_l" bgcolor="$col2">&nbsp; 配達日時</td>
<td width="$w_r" bgcolor="$col2">$date $deli2</td>
</tr>
</table>
</Td></Tr></Table>
<p>
<Table border=0 cellspacing=0 cellpadding=0 width="500">
<Tr><Td bgcolor="$col1">
<table width="100%" border=0 cellspacing=1 cellpadding=4>
<tr bgcolor="$col1">
<td colspan=2 bgcolor="$col3">▼注文者</td>
</tr>
<tr bgcolor="$col1">
<td width="$w_l" bgcolor="$col2">&nbsp; お名前</td>
<td width="$w_r" bgcolor="$col2">$in{'name'}</td>
</tr>
<tr bgcolor="$col1">
<td width="$w_l" bgcolor="$col2">&nbsp; ふりがな</td>
<td width="$w_r" bgcolor="$col2">$in{'kana'}</td>
</tr>
<tr bgcolor="$col1">
<td width="$w_l" bgcolor="$col2">&nbsp; 電子メール</td>
<td width="$w_r" bgcolor="$col2">$in{'email'}</td>
</tr>
<tr bgcolor="$col1">
<td width="$w_l" bgcolor="$col2">&nbsp; 郵便番号</td>
<td width="$w_r" bgcolor="$col2">$in{'zip'}</td>
</tr>
<tr bgcolor="$col1">
<td width="$w_l" bgcolor="$col2">&nbsp; ご住所</td>
<td width="$w_r" bgcolor="$col2">$pref<br>$in{'addr'}</td>
</tr>
<tr bgcolor="$col1">
<td width="$w_l" bgcolor="$col2">&nbsp; 電話番号</td>
<td width="$w_r" bgcolor="$col2">$in{'tel'}</td>
</tr>
<tr bgcolor="$col1">
<td width="$w_l" bgcolor="$col2">&nbsp; FAX番号</td>
<td width="$w_r" bgcolor="$col2">$in{'fax'}</td>
</tr>
</table>
</Td></Tr></Table>
EOM

# 配送先
if ($in{'name2'}) {
print "<p><Table border=0 cellspacing=0 cellpadding=0 width=\"500\">
<Tr><Td bgcolor=\"$col1\">
<table width=\"100%\" border=0 cellspacing=1 cellpadding=4>
<tr bgcolor=\"$col1\">
<td colspan=2 bgcolor=\"$col3\">▼配送先</td></tr>
<tr bgcolor=\"$col1\">
<td width=\"$w_l\" bgcolor=\"$col2\">&nbsp; お名前</td>
<td width=\"$w_r\" bgcolor=\"$col2\">$in{'name2'}</td></tr>
<tr bgcolor=\"$col1\">
<td width=\"$w_l\" bgcolor=\"$col2\">&nbsp; ふりがな</td>
<td width=\"$w_r\" bgcolor=\"$col2\">$in{'kana2'}</td></tr>
<tr bgcolor=\"$col1\">
<td width=\"$w_l\" bgcolor=\"$col2\">&nbsp; 郵便番号</td>
<td width=\"$w_r\" bgcolor=\"$col2\">$in{'zip2'}</td></tr>
<tr bgcolor=\"$col1\">
<td width=\"$w_l\" bgcolor=\"$col2\">&nbsp; ご住所</td>
<td width=\"$w_r\" bgcolor=\"$col2\">$pref2<br>$in{'addr2'}</td></tr>
<tr bgcolor=\"$col1\">
<td width=\"$w_l\" bgcolor=\"$col2\">&nbsp; 電話番号</td>
<td width=\"$w_r\" bgcolor=\"$col2\">$in{'tel2'}</td></tr>
<tr bgcolor=\"$col1\">
<td width=\"$w_l\" bgcolor=\"$col2\">&nbsp; FAX番号</td>
<td width=\"$w_r\" bgcolor=\"$col2\">$in{'fax2'}</td></tr>
</table></Td></Tr></Table>\n";
}

$param2="";
foreach ('payment','mon','day','deli','name','kana','email','zip','pref','addr','tel','
fax','name2','kana2','zip2','pref2','addr2','tel2','fax2','memo') {
next if ($in{$_} eq "");

$param2 .= "<input type=hidden name=\"$_\" value=\"$in{$_}\">\n";
}

print <<EOM;
<p>
<Table border=0 cellspacing=0 cellpadding=0 width="500">
<Tr><Td bgcolor="$col1">
<table width="100%" border=0 cellspacing=1 cellpadding=4>
<tr bgcolor="$col1">
<td colspan=2 bgcolor="$col3">▼その他</td>
</tr>
<tr bgcolor="$col1">
<td width="$w_l" bgcolor="$col2">&nbsp; 連絡事項</td>
<td width="$w_r" bgcolor="$col2">$in{'memo'}</td>
</tr>
</table>
</Td></Tr></Table>
<form action="$order" method="POST">
<input type=hidden name=mode value="addr">
<input type=hidden name=job value="back">
<input type=hidden name=back value="$in{'back'}">
$param
$param2
<table cellpadding=5>
<tr>
<td valign=top>
<input type=submit value="&lt;&lt; 前の画面に戻る">
</form>
</td>
<td width=10></td>
<td valign=top>
<form action="$order" method="POST">
<input type=hidden name=mode value="send">
$param
$param2
<input type=submit value="確定し注文する &gt;&gt;">
</form>
</td>
</tr>
</table>
<!-- 著作権表\記削除不可 ($ver) -->
<p style="font-size:10px;font-family:Verdana,Helvetica,Arial">
- <a href="http://www.kent-web.com/">WebMart</a> -
</p>
</div>
</body>
</html>
EOM
exit;
}

#-------------------------------------------------
# 注文送信 (Step3)
#-------------------------------------------------
sub send {
local($msub,$mbody,$date,$mdate,$msub,$mbody,$all,$all2,$alls,
$no_tax,$kei,$i,$id,$code,$num,$size,$col,$code,$name,
$price,$intax,$deli,$dai,$deliv,$pref,$pref2,$postage,
$postage2,$pay,$cost,@op,@cart,%stock);

# 買物情報取得
@cart = split(/\0/, $in{'cart'});
if (@cart == 0) { &error("買物情報がありません"); }

# 在庫
if ($stock) {
local($id,$zan);

# ロック開始
&lock if ($lockkey);

%stock=();
open(IN,"$stkfile") || &error("Open Error: $stkfile");
while (<IN>) {
($id,$zan) = split(/<>/);
$stock{$id} = $zan;
}
close(IN);
}

# ホスト名及び時間を取得
&get_host;
($date,$mdate) = &get_time;

# メール題名を定義
$msub = &base64("ご注文メール ($in{'name'}様)");

# メール本文を定義
$mbody = <<EOM;
To: $master
From: $in{'email'}
Cc: $in{'email'}
Subject: $msub
Date: $mdate
MIME-Version: 1.0
Content-type: text/plain; charset=ISO-2022-JP
Content-Transfer-Encoding: 7bit
X-Mailer: $ver

この度はご注文をありがとうございます。
以下のとおりお受けいたしましたのでご確認ください。

▼注文情報
EOM

# データ読み取り
&get_data;

@op = ('消費税別','消費税込み');
$all=0;
$no_tax=0;
$i=0;
foreach (@cart) {
($id,$code,$num,$size,$col) = split(/;/);
next if (!defined($cart{$code}));
($code,$name,$price,$intax,$deli,$dai) = split(/<>/, $cart{$code});

# 小計
$kei = $price * $num;
$all += $kei;

# 在庫チェック
if ($stock) {
if ($stock{$code} - $num < 0) {
&error("大変申\し訳ありません。<br>「$name」は在庫切れです
(現在の在庫数:$stock{$code})<br>たった今、他の方からの購入があったようです");
}

$stock{$code} -= $num;
}

# 外税商品 & 消費税を合計
if (!$intax) { $no_tax += $kei; }

$price = &comma($price);
$kei = &comma($kei);

$i++;
$mbody .= "($i) $code $name ($op[$intax])";
$mbody .= " [$opt01] $col" if ($col ne "");
$mbody .= " [$opt02] $size" if ($size ne "");
$mbody .= "\n";
$mbody .= " \\$price × $num = \\$kei\n";
}

# 配達時間
$deliv='';
if ($in{'mon'} ne "" && $in{'day'} ne "") {
$deliv = "$in{'mon'}月$in{'day'}日";
if ($in{'deli'} ne "") {
$deliv .= " - $deli[$in{'deli'}]";
}
}

# 改行復元
$in{'addr'} =~ s/&lt;br&gt;/\n /g;
$in{'addr2'} =~ s/&lt;br&gt;/\n /g;
$in{'memo'} =~ s/&lt;br&gt;/\n /g;

# 小計コンマ付加
$alls = &comma($all);

$mbody .= "--------------------------\n";
$mbody .= "小 計 : \\$alls\n";

# 都道府県 & 送料
$pref = $pref2 = "";
$postage = 0;
($pref,$postage) = split(/,/, $pref[$in{'pref'}]);
if ($in{'pref2'} ne "") {
($pref2,$postage) = split(/,/, $pref[$in{'pref2'}]);
}

# 支払方法の手数料
($pay,$cost) = split(/,/, $payment[$in{'payment'}]);

# 送料サービスフラグ
local($serv_flag)=0;

# 県別送料
if ($postage > 0) {
local($memo);

# 送料サービス有り
if ($cari_serv && $cari_serv <= $all) {
$postage2 = $postage = 0;
$memo = ' (送料サービス)';

$serv_flag++;

# 送料サービス無し
} else {
$postage2 = &comma($postage);
$memo = '';
}

$mbody .= "送 料 : \\$postage2$memo\n";
}

# 送料が設定されている場合
if (!$serv_flag) {
$all += $postage;

if ($cari_tax == 0) {
$no_tax += $postage;
}
}

# 支払手数料
if ($cost > 0) {
$all += $cost;

if ($paym_tax == 0) {
$no_tax += $cost;
}

$cost = &comma($cost);

$mbody .= "$pay手数料 : \\$cost\n";
}

if ($no_tax > 0) { $tax = int($no_tax * $per_tax); }
$all2 = $all + $tax;

# コンマ付加
$tax = &comma($tax) if ($tax > 0);
$all2 = &comma($all2);

$mbody .= "消費税 : \\$tax\n" if ($tax > 0);

$mbody .= "合 計 : \\$all2\n\n";
$mbody .= "▼お支払・配達方法\n";
$mbody .= "支払方法\ : $pay\n";
$mbody .= "配達日時 : $deliv\n\n";
$mbody .= "▼注文者\n";
$mbody .= "お名前 : $in{'name'}\n";
$mbody .= "ふりがな : $in{'kana'}\n";
$mbody .= "E-mail : $in{'email'}\n";
$mbody .= "郵便番号 : 〒$in{'zip'}\n";
$mbody .= "ご住所 : $pref\n";
$mbody .= " $in{'addr'}\n";
$mbody .= "電話番号 : $in{'tel'}\n";
$mbody .= "FAX 番号 : $in{'fax'}\n\n";

if ($in{'name2'} && $in{'addr2'}) {
$mbody .= "▼配送先\n";
$mbody .= "お名前 : $in{'name2'}\n";
$mbody .= "ふりがな : $in{'kana2'}\n";
$mbody .= "郵便番号 : 〒$in{'zip2'}\n";
$mbody .= "ご住所 : $pref2\n";
$mbody .= " $in{'addr2'}\n";
$mbody .= "電話番号 : $in{'tel2'}\n";
$mbody .= "FAX 番号 : $in{'fax2'}\n";
}
$mbody .= "\n▼その他\n連絡事項 : $in{'memo'}\n\n";
$mbody .= "----------------------------------------------------------------------\n";
$mbody .= "注文日時 = $date\n";
$mbody .= "ホスト名 = $host\n";
$mbody .= "ブラウザ = $ENV{'HTTP_USER_AGENT'}\n";
$mbody .= "----------------------------------------------------------------------\n";

# sendmail起動
open(MAIL,"| $sendmail -t") || &error("メール送信失敗");
foreach ( split(/\n/, $mbody) ) {
&jcode'convert(*_, 'jis', 'sjis');
print MAIL $_, "\n";
}
close(MAIL);

# 買物情報のクッキー消去
print "Set-Cookie: $cookieID=; path=/;\n";

# 在庫調整
if ($stock) {
open(OUT,">$stkfile") || &error("Write Error: $stkfile");
while ( ($id,$zan) = each(%stock) ) {
print OUT "$id<>$zan<>\n";
}
close(OUT);

# ロック解除
&unlock if ($lockkey);
}

# 完了メッセージ表示開始
&header("", "", "no-cashe");
print <<EOM;
[ <a href="$home" target="_top">ホームに戻る</a>
| <a href="$script?mode=note">特定商取引法の表\示</a> ]<br>
<div align="center">
EOM

# 進行パネル
&panel(3);

print <<EOM;
<h3>ご注文をありがとうございました</h3>
<form>
<input type="button" value="トップページに戻る" onClick=window.open("$home","_top")>
</form>
</div>
</body>
</html>
EOM
exit;
}

#-------------------------------------------------
# 進行パネル
#-------------------------------------------------
sub panel {
local($step) = @_;

if ($step == 1) {
$moji1 = $col5;
$maru1 = $col6;
$moji2 = $moji3 = $maru2 = $maru3 = $col7;
} elsif ($step == 2) {
$moji2 = $col5;
$maru2 = $col6;
$moji1 = $moji3 = $maru1 = $maru3 = $col7;
} else {
$moji3 = $col5;
$maru3 = $col6;
$moji1 = $moji2 = $maru1 = $maru2 = $col7;
}

print <<EOM;
<table border=0 cellspacing=1 cellpadding=4 style="background-color:$col4;">
<tr>
<td align="center" width=80 style="color:$moji1">情報入力</td>
<td align="center" width=30></td>
<td align="center" width=80 style="color:$moji2">確認画面</td>
<td align="center" width=30></td>
<td align="center" width=80 style="color:$moji3">注文完了</td>
</tr>
<tr>
<td align="center" width=80 style="color:$maru1">●</td>
<td align="center" width=30 style="color:$col5">→</td>
<td align="center" width=80 style="color:$maru2">●</td>
<td align="center" width=30 style="color:$col5">→</td>
<td align="center" width=80 style="color:$maru3">●</td>
</tr>
</table>
<br><br>
EOM
}

#-------------------------------------------------
# BASE64変換
#-------------------------------------------------
# とほほのWWW入門で公開されているルーチンを参考にしました。
# ( http://tohoho.wakusei.ne.jp/ )
sub base64 {
local($sub) = @_;
&jcode'convert(*sub, 'jis', 'sjis');

$sub =~ s/\x1b\x28\x42/\x1b\x28\x4a/g;
$sub = "=?iso-2022-jp?B?" . &b64enc($sub) . "?=";
$sub;
}
sub b64enc {
local($ch)="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
local($x, $y, $z, $i);
$x = unpack("B*", $_[0]);
for ($i=0; $y=substr($x,$i,6); $i+=6) {
$z .= substr($ch, ord(pack("B*", "00" . $y)), 1);
if (length($y) == 2) {
$z .= "==";
} elsif (length($y) == 4) {
$z .= "=";
}
}
$z;
}

#-------------------------------------------------
# 時間取得
#-------------------------------------------------
sub get_time {
local($sec,$min,$hour,$mday,$mon,$year,$wday) = localtime(time);
local($date,$mdate,@w,@m);

@w = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
@m = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');

# 日時をフォーマット
$date = sprintf("%04d/%02d/%02d(%s) %02d:%02d",
$year+1900,$mon+1,$mday,$w[$wday],$hour,$min);

# メール用日時にフォーマット
$mdate = sprintf("%s, %02d %s %04d %02d:%02d:%02d",
$w[$wday],$mday,$m[$mon],$year+1900,$hour,$min,$sec) . " +0900";

return ($date,$mdate);
}

#-------------------------------------------------
# ホスト名取得
#-------------------------------------------------
sub get_host {
$host = $ENV{'REMOTE_HOST'};
$addr = $ENV{'REMOTE_ADDR'};

if ($gethostbyaddr && ($host eq "" || $host eq $addr)) {
$host = gethostbyaddr(pack("C4", split(/\./, $addr)), 2);
}
if (!$host) { $host = $addr; }
}

#-------------------------------------------------
# クッキー処理 : 顧客住所情報
#-------------------------------------------------
sub set_cookie2 {
local(@cook) = @_;
local($gmt, $cook, @t, @m, @w);

# 国際標準時でクッキー期限を定義
@t = gmtime(time + 60*24*60*60);
@m = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
@w = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
$gmt = sprintf("%s, %02d-%s-%04d %02d:%02d:%02d GMT",
$w[$t[6]], $t[3], $m[$t[4]], $t[5]+1900, $t[2], $t[1], $t[0]);

# 格納データをURLエンコード
foreach (@cook) {
s/(\W)/sprintf("%%%02X", unpack("C", $1))/eg;
$cook .= "$_<>";
}

# クッキーを格納
print "Set-Cookie: $cookieID2=$cook; expires=$gmt;";
print " secure" if ($ssl_cookie);
print "\n";
}
sub get_cookie2 {
local($key, $val, *cook);

# クッキーを取得
$cook = $ENV{'HTTP_COOKIE'};

# ID毎に分割し、ハッシュに代入
foreach ( split(/;/, $cook) ) {
($key, $val) = split(/=/);
$key =~ s/\s//g;
$cook{$key} = $val;
}

# 該当IDのデータをURLデコードして復元
foreach ( split(/<>/, $cook{"$cookieID2"}) ) {
s/%([0-9A-Fa-f][0-9A-Fa-f])/pack("C", hex($1))/eg;

push(@cook,$_);
}
return (@cook);
}


__END__

←#!/usr/local/bin/perlまたは#!/usr/bin/perl
サーバー会社に確認して下さい。(アット・ワイエムシー社は
#!/usr/bin/perlです)


次へ