#!/usr/bin/perl #┌──────────────────────── #│ Shopping Cart - admin.cgi (2004/04/01) #│ Copyright (c) KentWeb #│ http://www.kent-web.com/ #└──────────────────────── # 外部ファイル取り込み require './init.cgi'; require $cgilib; require $jcode; # メイン処理 &decode2; if ($in{'pass'} eq "") { &enter; } elsif ($in{'pass'} ne $pass) { &error("パスワードが違います"); } &admin; #------------------------------------------------- # 管理機能 #------------------------------------------------- sub admin { if ($in{'job'} eq "") { &menu; } # 基本処理 if ($in{'job'} eq "new") { &new_log; } elsif ($in{'job'} eq "ord") { &ord_log; } elsif ($in{'job'} eq "csv") { &csv_log; } elsif ($in{'job'} eq "sort") { &sort_log; } elsif ($in{'no'} && $in{'job'} eq "dele") { &dele_log; } elsif ($in{'no'} && $in{'job'} eq "edit") { &edit_log; } # 在庫認識 if ($stock) { &stock; } &header; print <
EOM # 登録フォーム if ($in{'job'} eq "form") { print "▼商品の新規登録"; print "   ( 入力必須 )\n"; &form('new'); print "\n\n"; exit; } print <<"EOM"; ▼登録内容メンテ
処理:

EOM print "" if ($stock); print "\n"; # 商品情報読み出し open(IN,"$logfile") || &error("Open Error: $logfile"); while () { s/\n//; ($no,$item,$price,$size,$color,$img,$memo,$w,$h,$intax,$nmb,$id,$cate,$link,$tail) = split(/<>/); if ($intax == 1) { $zei="込み"; } else { $zei="別"; } if ($id eq "") { $id = $no; } $price = &filler($price); # $memo =~ s/<//g; # $memo =~ s/"/"/g; # $memo =~ s/&/&/g; if ($category) { if ($cate2 ne $cate) { print "\n"; $i=1; } } print ""; print ""; print ""; print ""; if ($stock) { if (!defined($stock{$no})) { $stock{$no} = 0; } print ""; } print "\n"; $cate2 = $cate; } close(IN); print < EOM exit; } #------------------------------------------------- # 新規登録 #------------------------------------------------- sub new_log { local($no,$item,$price,$size,$col,$img,$memo,$w,$h,$intax,$nmb,$id,$cate,$link); # 入力チェック if ($in{'item'} eq "") { &error('商品名の入力モレです'); } if ($in{'price'} eq "") { &error('単価の入力モレです'); } if ($in{'price'} =~ /[^\d\,]/) { &error('単価には半角数字とコンマ以外は入力できません'); } if ($in{'id'} =~ /[^A-Za-z0-9\-]/) { &error('商品Noは英数字とハイフンのみです'); } if ($category && $in{'cate'} eq "") { &error('カテゴリ選択は必須です'); } if ($stock && $in{'stk'} eq "") { &error("在庫数が未入力です"); } $in{'memo'} =~ s/<//g; $in{'memo'} =~ s/"/"/g; $in{'memo'} =~ s/&/&/g; if ($in{'img'} eq "http://") { $in{'img'} = ""; } if ($in{'link'} eq "http://") { $in{'link'} = ""; } # ログ読み込み $new=0; @sort=(); @data=(); open(IN,"$logfile") || &error("Open Error: $logfile"); while () { ($no,$item,$price,$size,$col,$img,$memo,$w,$h,$intax,$nmb,$id,$cate,$link) = split(/<>/); # 通番チェック if ($new < $no) { $new = $no; } push(@sort,$cate); push(@data,$_); } close(IN); # 採番 $new = sprintf("%06d", $new+1); if ($in{'id'} eq "") { $in{'id'} = $new; } if ($in{'cate'} eq "") { $in{'cate'}=0; } # 画像アップ ($tail,$w,$h) = ('','',''); if ($in{'upfile'}) { ($tail,$w,$h) = &upload($new); } # 新規登録を定義 push(@data,"$new<>$in{'item'}<>$in{'price'}<>$in{'size'}<>$in{'color'}<>$in{'img'}<>$in{'memo'}<>$w<>$h<>$in{'intax'}<>$in{'nmb'}<>$in{'id'}<>$in{'cate'}<>$in{'link'}<>$tail<>\n"); push(@sort,$in{'cate'}); # カテゴリ順にソート @data = @data[sort {$sort[$a] <=> $sort[$b]} 0 .. $#sort]; # 更新 open(OUT,">$logfile") || &error("Write Error: $logfile"); print OUT @data; close(OUT); # 在庫更新 if ($stock) { &lock if ($lockkey); @file=(); $f=0; open(IN,"$stkfile") || &error("Open Error: $stkfile"); while () { ($no,$num) = split(/<>/); if ($no == $new) { $f++; $_="$no<>$in{'stk'}<>\n"; } push(@file,$_); } close(IN); if (!$f) { push(@file,"$new<>$in{'stk'}<>\n"); } open(OUT,">$stkfile") || &error("Write Error: $stkfile"); print OUT @file; close(OUT); &unlock if ($lockkey); } } #------------------------------------------------- # 削除処理 #------------------------------------------------- sub dele_log { local($no,$item,$price,$size,$color,$img,$memo,$w,$h,$intax,$nmb,$id,$cate,$link,$tail,$f,$del,@data,@del); # 削除情報 @del = split(/\0/, $in{'no'}); @data=(); open(IN,"$logfile") || &error("Open Error: $logfile"); while () { ($no,$item,$price,$size,$color,$img,$memo,$w,$h,$intax,$nmb,$id,$cate,$link,$tail) = split(/<>/); $f=0; foreach $del (@del) { if ($no == $del) { $f++; unlink("$imgdir/$no$tail") if ($tail); last; } } if (!$f) { push(@data,$_); } } close(IN); # 更新 open(OUT,">$logfile") || &error("Write Error: $logfile"); print OUT @data; close(OUT); # 在庫 if ($stock) { &lock if ($lockkey); @data=(); open(IN,"$stkfile") || &error("Open Error: $stkfile"); while () { ($no) = split(/<>/); $f=0; foreach $del (@del) { if ($no == $del) { $f++; last; } } if (!$f) { push(@data,$_); } } close(IN); open(OUT,">$stkfile") || &error("Write Error: $stkfile"); print OUT @data; close(OUT); &unlock if ($lockkey); } } #------------------------------------------------- # 修正処理 #------------------------------------------------- sub edit_log { # 修正実行 if ($in{'edit'}) { # 入力チェック if ($in{'item'} eq "") { &error('商品名の入力モレです'); } if ($in{'price'} eq "") { &error('単価の入力モレです'); } if ($in{'price'} =~ /[^\d\,]/) { &error('単価には半角数字とコンマ以外は入力できません'); } $in{'price'} =~ s/,//g; if ($in{'id'} =~ /[^A-Za-z0-9\-]/) { &error('商品Noは英数字とハイフンのみです'); } # カテゴリ if ($category && $in{'cate'} eq "") { &error('カテゴリ選択は必須です'); } if ($stock && $in{'stk'} eq "") { &error("在庫数が未入力です"); } $in{'memo'} =~ s/<//g; $in{'memo'} =~ s/"/"/g; $in{'memo'} =~ s/&/&/g; if ($in{'img'} eq "http://") { $in{'img'} = ""; } if ($in{'link'} eq "http://") { $in{'link'} = ""; } # 画像アップ ($tail,$w,$h) = ('','',''); if ($in{'upfile'}) { ($tail,$w,$h) = &upload($in{'no'}); } # マッチング @data=(); @sort=(); open(IN,"$logfile") || &error("Open Error: $logfile"); while () { ($no,$item,$price,$size,$col,$img,$memo,$w2,$h2,$intax,$nmb,$id,$cate,$link,$tail2) = split(/<>/); if ($cate eq "") { $cate = 0; } if ($in{'no'} == $no) { if ($tail) { if ($tail2 && $tail ne $tail2) { unlink("$imgdir/$no$tail2"); } $tail2 = $tail; $w2 = $w; $h2 = $h; } elsif ($in{'imgdel'} == 1) { unlink("$imgdir/$no$tail2"); $tail2 = $w2 = $h2 = ""; } $_ = "$no<>$in{'item'}<>$in{'price'}<>$in{'size'}<>$in{'color'}<>$in{'img'}<>$in{'memo'}<>$w2<>$h2<>$in{'intax'}<>$in{'nmb'}<>$in{'id'}<>$in{'cate'}<>$in{'link'}<>$tail2<>\n"; $cate = $in{'cate'} if ($category); } push(@sort,$cate) if ($category); push(@data,$_); } close(IN); # ソート処理 if ($category) { @data = @data[sort {$sort[$a] <=> $sort[$b]} 0 .. $#sort]; } # 更新 open(OUT,">$logfile") || &error("Write Error: $logfile"); print OUT @data; close(OUT); # 在庫更新 if ($stock && $in{'now'} != $in{'stk'}) { &lock if ($lockkey); @file=(); $f=0; open(IN,"$stkfile") || &error("Open Error: $stkfile"); while () { ($no,$num) = split(/<>/); if ($no == $in{'no'}) { $f++; $_="$no<>$in{'stk'}<>\n"; } push(@file,$_); } close(IN); if (!$f) { push(@file,"$in{'no'}<>$in{'stk'}<>\n"); } open(OUT,">$stkfile") || &error("Write Error: $stkfile"); print OUT @file; close(OUT); &unlock if ($lockkey); } # 管理画面に戻る $in{'job'}='mente'; $in{'no'}=''; &admin; } # 修正フォーム画面 &header; print <<"EOM";

修正フォーム

▼変更する部分のみ修正して送信ボタンを押してください。 EOM # ログを検索 open(IN,"$logfile") || &error("Open Error: $logfile"); while () { chop; @f = split(/<>/); last if ($f[0] == $in{'no'}); } close(IN); # フォーム呼び出し &form("edit", @f); print "\n"; exit; } #------------------------------------------------- # 登録フォーム #------------------------------------------------- sub form { local($job,$num,$item,$price,$size,$col,$img, $memo,$w,$h,$intax,$nmb,$id,$cate,$link,$tail) = @_; if ($img eq "") { $img = "http://"; } if ($link eq "") { $link = "http://"; } if ($id eq "") { $id = $num; } # 在庫認識 if ($stock) { &stock; } print <
選択 コード 商品名 単価 消費税在庫画像
$cate[$cate]
$id$item\\$price$zei$stock{$no}"; print "画像" if ($tail); print "
EOM # カテゴリ if ($category) { print "\n"; print "\n"; } print ""; print " EOM # 在庫 if ($stock) { if (!defined($stock{$num})) { $stock{$num} = ""; } print ""; print "\n"; print "\n"; } print <
商品コード (英数字又はハイフンで指定)
商品名
カテゴリ
金額単価 円   "; if ($intax eq '0') { print "税込み\n"; print "税別\n"; } else { print "税込み\n"; print "税別\n"; } print <
数量単位 (プルダウンで数量選択。スペースで区切る。例: 1 2 3 4 5)
在庫
$attr1指定 (スペースで区切り、複数指定。例: S M L)
$attr2指定 (スペースで区切り、複数指定。例: 赤 青 黄)
画像ファイル (アップロード画像 GIF/JPEG/PNG)

EOM if ($tail) { print "[画像]  "; print "削除\n"; } print <
リンク先 (画像に詳細説明等をリンクする場合)
備考情報 (タグ有効。改行無効)

EOM } #------------------------------------------------- # 並替処理 #------------------------------------------------- sub sort_log { local($no,$item,$price,$size,$color,$img,$memo,$w,$h,$intax,$nmb,$id,$cate,$i,$cate2); # ソート実行 if ($in{'log'} eq "sort") { open(IN,"$logfile") || &error("Open Error: $logfile"); while () { ($no,$cate) = (split(/<>/))[0,12]; $file{$no} = $_; $cate{$no} = $cate; $sort{$no} = $in{"sort$no"}; } close(IN); # ソート処理(カテゴリ分け → 指定順) local(@new); foreach (sort { ($cate{$a} <=> $cate{$b}) || ($sort{$a} <=> $sort{$b}) } keys(%sort)) { push(@new,$file{$_}); } # 更新 open(OUT,">$logfile") || &error("Write Error: $logfile"); print OUT @new; close(OUT); } &header; print <<"EOM";
商品の並び替え

EOM # 商品情報読み出し $i=0; open(IN,"$logfile") || &error("Open Error: $logfile"); while () { $i++; ($no,$item,$price,$size,$color,$img,$memo,$w,$h,$intax,$nmb,$id,$cate) = split(/<>/); if ($category) { if ($cate2 ne $cate) { print "

[$cate[$cate]]\n"; $i=1; } } print "
", "$id $item (\\$price)\n"; $cate2 = $cate; } close(IN); print <
EOM exit; } #------------------------------------------------- # 注文情報ログ #------------------------------------------------- sub ord_log { local($date,$eml,$comp,$nam,$zip,$pref,$addr,$tel,$fax,$comp2,$nam2,$zip2,$pref2,$addr2,$tel2,$fax2,$pay,$mon,$day,$hour,$note,$kei,$cost,$cari,$tax,$all,$deli_1,$deli_2,$i,$next,$back,@detail,@list); # 画面表示 &header; print <
注文情報ログ

EOM # ログ読出 $i=0; @list=(); open(IN,"$ordfile") || &error("Open Error: $ordfile"); while () { $i++; next if ($i < $page + 1); next if ($i > $page + $adminLog); push(@list,$i); s/\n//; ($date,$eml,$comp,$nam,$zip,$pref,$addr,$tel,$fax,$comp2,$nam2,$zip2,$pref2,$addr2,$tel2,$fax2,$pay,$mon,$day,$hour,$note,$kei,$cost,$cari,$tax,$all,@detail) = split(/<>/); if ($comp) { $nam .= "
[会社] $comp"; } if ($comp2) { $nam2 .= "
[会社] $comp2"; } $tel = "TEL: $tel"; if ($fax) { $tel .= "
FAX: $fax"; } $tel2 = "TEL: $tel2"; if ($fax2) { $tel2 .= "
FAX: $fax2"; } if ($mon && $day) { $deli_1 = "$mon月$day日"; } else { $deli_1 = "年月指定なし"; } if ($hour ne "") { $deli_2 = "$deli[$hour]"; } else { $deli_2 = "時間指定なし"; } $kei = &filler($kei); $cost = &filler($cost); $tax = &filler($tax); $all = &filler($all); $date =~ s|\d{4}/(\d{2}/\d{2})(\(\w+\)) (\d{2}\:\d{2})|$1
$2
$3|; print "
"; print ""; print ""; print ""; print ""; print ""; print "\n"; } close(IN); print <

 [ $i件中 $list[0]件 〜 $list[$#list]件 を表\示 ]

日付 注文者 配送先 支払方法 配達日 金額 明細 備考
$date[email] $eml
"; print "[名前] $nam
〒$zip
$pref
$addr
$tel
[名前] $nam2
〒$zip2
$pref2
$addr2
$tel2
$pay$deli_1
$deli_2
小計 \\$kei
"; print "手数料 \\$cost
" if ($cost > 0); print "送料 \\$cari
" if ($cari > 0); print "消費税 \\$tax
合計 \\$all
"; while (@detail > 0) { ($id) = shift(@detail); ($item) = shift(@detail); ($price) = shift(@detail); ($num) = shift(@detail); ($price2) = shift(@detail); $price = &filler($price); $price2 = &filler($price2); print "【商品】$id
$item
\\$price × $num = \\$price2
\n"; } if (!$note) { $note = "
"; } print "
$note
EOM $next = $page + $adminLog; $back = $page - $adminLog; if ($back >= 0) { print "\n"; } if ($next < $i) { print "\n"; } print <
\n"; print "\n"; print "\n"; print "\n"; print "
\n"; print "\n"; print "\n"; print "\n"; print "
EOM exit; } #------------------------------------------------- # CSVデータ出力 #------------------------------------------------- sub csv_log { local($i,$wid,@br); # ダウンロード if ($in{'ope'} eq "down") { local($br,$out,$id,$item,$price,$num,$price2,@sub,@data); # 改行コード定義 if ($in{'br'} == 1) { $br = "\r"; } elsif ($in{'br'} == 2) { $br = "\n"; } else { $br = "\r\n"; } # 項目別タイトルを定義 @sub = ('日付','E-Mail','会社名','名前','〒','県名','住所','電話','FAX','会社名2','名前2','〒2','県名2','住所2','電話2','FAX2','支払方法','配達月','配達日','配達時間','連絡事項','明細'); # タイトル編集 foreach $i (0 .. 25) { if ($in{"data$i"} == 1) { $out .= "$sub[$i],"; } } # ダウンロード用ヘッダ print "Content-type: application/octet-stream\n"; print "Content-Disposition: attachment; filename=data.csv\n\n"; # バイナリ出力 (Windowsサーバ環境) binmode(STDOUT); # タイトル出力 print "$out$br"; # 注文ログ読み出し open(IN,"$ordfile") || &error("Open Error: $ordfile"); while () { s/\n//g; s/\r//g; @data = split(/<>/); # CSV展開(明細以外) $out=''; foreach $i (0 .. 24) { if ($in{"data$i"} == 1) { $out .= "$data[0],"; } shift(@data); } # 明細部分展開 if ($in{'data25'} == 1) { while (@data > 0) { ($id) = shift(@data); ($item) = shift(@data); ($price) = shift(@data); ($num) = shift(@data); ($price2) = shift(@data); $out .= "$id,$item,$price,$num,$price2,"; } } # CSV出力 print "$out$br"; } close(IN); # 出力完了 exit; } # 注文ログの件数を数える $i=0; open(IN,"$ordfile") || &error("Open Error: $ordfile"); ++$i while (); close(IN); # 項目セルの幅を定義 $wid = 60; # 画面表示 &header; print <

CSVデータ出力
  • 現在のデータ件数 : $i件
  • 必要な項目をチェックしてダウンロードボタンを押してください。
改行コード:

注文者
会社名 名前 県名 住所 電話 FAX

配送先
会社名 名前 県名 住所 電話 FAX

小計 手数料 消費税 合計 明細

EOM exit; } #------------------------------------------------- # ログイン画面 #------------------------------------------------- sub enter { &header; print <<"EOM";

パスワードを入力してください

EOM exit; } #------------------------------------------------- # 管理メニュー #------------------------------------------------- sub menu { &header; print <<"EOM";
- 処理を選択してください -
選択 管理メニュー一覧
  商品の新規登録
  商品の登録修正
  商品の並び替え
  注文ログの閲覧
  CSVデータ出力

EOM exit; } #------------------------------------------------- # フォームデコード #------------------------------------------------- sub decode2 { local($key,$val); undef(%in); &ReadParse; while ( ($key,$val) = each(%in) ) { next if ($key eq "upfile"); # シフトJISコード変換 &jcode'convert(*val, 'sjis'); # 不要コード排除 $val =~ s/\r//g; $val =~ s/\n//g; $val =~ s/<>/<>/g; $val =~ s/&/&/g; $val =~ s/"/"/g; $val =~ s//>/g; # 改行処理 $val =~ s/\r\n/
/g; $val =~ s/\r/
/g; $val =~ s/\n/
/g; $in{$key} = $val; } $mode = $in{'mode'}; $page = $in{'page'}; } #------------------------------------------------- # 画像アップロード #------------------------------------------------- sub upload { local($new) = @_; local($macbin,$fname,$flag,$upfile,$imgfile); # 画像処理 $macbin=0; foreach (@in) { if (/(.*)Content-type:(.*)/i) { $tail=$2; } if (/(.*)filename=\"(.*)\"/i) { $fname=$2; } if (/application\/x-macbinary/i) { $macbin=1; } } $tail =~ s/\r//g; $tail =~ s/\n//g; # ファイル形式を認識 $flag=0; if ($tail =~ /image\/gif/i) { $tail=".gif"; $flag=1; } if ($tail =~ /image\/p?jpeg/i) { $tail=".jpg"; $flag=1; } if ($tail =~ /image\/x-png/i) { $tail=".png"; $flag=1; } if (!$flag) { if ($fname =~ /\.gif$/i) { $tail=".gif"; $flag=1; } if ($fname =~ /\.jpe?g$/i) { $tail=".jpg"; $flag=1; } if ($fname =~ /\.png$/i) { $tail=".png"; $flag=1; } } # アップロード失敗処理 if (!$flag || !$fname) { &error("アップロードできません"); } $upfile = $in{'upfile'}; # マックバイナリ対策 if ($macbin) { $length = substr($upfile,83,4); $length = unpack("%N",$length); $upfile = substr($upfile,128,$length); } # 画像データを書き込み $imgfile = "$imgdir$new$tail"; open(OUT,">$imgfile") || &error("画像アップロード失敗"); binmode(OUT); binmode(STDOUT); print OUT $upfile; close(OUT); chmod(0666,$imgfile); # 画像サイズ取得 if ($tail eq ".jpg") { ($W, $H) = &j_size($imgfile); } elsif ($tail eq ".gif") { ($W, $H) = &g_size($imgfile); } elsif ($tail eq ".png") { ($W, $H) = &p_size($imgfile); } # 画像表示縮小 if ($W > $MaxW || $H > $MaxH) { $W2 = $MaxW / $W; $H2 = $MaxH / $H; if ($W2 < $H2) { $key = $W2; } else { $key = $H2; } $W = int ($W * $key) || 1; $H = int ($H * $key) || 1; } return($tail,$W,$H); } #------------------------------------------------- # JPEGサイズ認識 #------------------------------------------------- sub j_size { local($jpeg) = @_; local($t, $m, $c, $l, $W, $H); open(JPEG,"$jpeg") || return (0,0); binmode(JPEG); read(JPEG, $t, 2); while (1) { read(JPEG, $t, 4); ($m, $c, $l) = unpack("a a n", $t); if ($m ne "\xFF") { $W = $H = 0; last; } elsif ((ord($c) >= 0xC0) && (ord($c) <= 0xC3)) { read(JPEG, $t, 5); ($H, $W) = unpack("xnn", $t); last; } else { read(JPEG, $t, ($l - 2)); } } close(JPEG); return ($W, $H); } #------------------------------------------------- # GIFサイズ認識 #------------------------------------------------- sub g_size { local($gif) = @_; local($data); open(GIF,"$gif") || return (0,0); binmode(GIF); sysread(GIF,$data,10); close(GIF); if ($data =~ /^GIF/) { $data = substr($data,-4); } $W = unpack("v",substr($data,0,2)); $H = unpack("v",substr($data,2,2)); return ($W, $H); } #------------------------------------------------- # PNGサイズ認識 #------------------------------------------------- sub p_size { local($png) = @_; local($data); open(PNG,"$png") || return (0,0); binmode(PNG); read(PNG, $data, 24); close(PNG); $W = unpack("N", substr($data, 16, 20)); $H = unpack("N", substr($data, 20, 24)); return ($W, $H); } __END__