#!/usr/local/bin/perl
#----------------------------------------------------------------------------------------------------------------------
#
#    ルミーズショッピングカート ver 1.2.0 2004/07/06
#
#
#---------------------------------------------------------------------------------------------------------------------
package main;
use CGI qw(:standard);
use lib './lib';
use Template;

require "./lib/cgi-lib.pl";
require "./initdata.cgi";
require "./auth.pl";
require "./store.pl";
require "./cart.pl";
require "./dataprocessing.pl";

require "./basicUtil.pl";
my($butil)= new basicUtil;

my($auth) = new Auth($user_id,$password);
my($store)= new Store($storefile,$salesfile,$amoutfile,$customerfile,$cartfile,$deliveryfile,$blockfile,$envfile,$server);
my($cart) = new Cart($storefile,$salesfile,$amoutfile,$customerfile,$cartfile,$deliveryfile,$blockfile,$envfile,$remisefile,$rankingfile,$downloadfile,$sendmail,$server);
my($datum)=new DataProcessing($salesfile,$customerfile,$cartfile,$rankingfile,$downloadfile);

#//////////////////////////////////////////////////////////////////////////////////////////////
#    MAIN
#//////////////////////////////////////////////////////////////////////////////////////////////
MAIN:
{
    $requestType=$ENV{"REQUEST_METHOD"};

        if($requestType eq "POST" ){
               &doPost;
        }else{
               &doGet;
        }

}
#//////////////////////////////////////////////////////////////////////////////////////////////
#
#    HTTP Post リクエストの処理
#
#//////////////////////////////////////////////////////////////////////////////////////////////
sub doPost {
#                       local $gfile = param('gfile');
#                       local $gfile2 = param('gfile2');
        if ( &ReadParse(*input) ) {

               #------------------------------------------------------
               # ログイン
               # HTML Template : login.html からのPOSTデータ処理
               #------------------------------------------------------
               if( $input{'mode'} eq 'login' ){

                        #ログイン画面表示
                        $auth->AuthLogin($input{'user_id'},$input{'password'});

               #------------------------------------------------------
               # ストアを作成
               # HTML Template :  newstore.html   からのPOSTデータ処理
               #------------------------------------------------------
               }elsif( $input{'mode'} eq 'MakeStore' && &checkAuth){

                        #ストアの作成
                        $store->MakeStore(*input );

               #------------------------------------------------------
               # ストア編集
               # HTML Template :   dispstore.html   からのPOSTデータ処理
               #------------------------------------------------------
               }elsif( $input{'mode'} eq 'ShowStore' && &checkAuth){

                       if(length  $input{'store_id'}==0){
                               $store->ShowStore();
                       }elsif($input{'submit'} eq "削除"){
                               #ストアの削除
                               $store->DeleteStore( $input{'store_id'} );
                      }else{
                              #商品リスト表示・登録
                              $store->EditStore( $input{'store_id'});
                      }

               #------------------------------------------------------
               #
               # HTML Template :
               #------------------------------------------------------
               }elsif( $input{'mode'} eq 'ShowAmount' && &checkAuth){

                     #受注集計
                     $store->ShowAmount;

               #------------------------------------------------------
               #
               # HTML Template :
               #------------------------------------------------------
               }elsif( $input{'mode'} eq 'ShowCustomer' && &checkAuth){

                     #顧客リスト表示
                     $store->ShowCustomer;

               #------------------------------------------------------
               # 登録したストア一覧
               # HTML Template :  dispstore.html　を表示
               #------------------------------------------------------
               }elsif( $input{'mode'} eq 'ShowStore' && &checkAuth){

                     #ストア一覧
                     $store->ShowStore;

               #------------------------------------------------------
               # 指定したストアの商品登録
               # HTML Template :  addgoods.html  からのPOSTデータ処理
               #--------------------------------------------------------
#               }elsif( $input{'mode'} eq 'RegistMerchandise' && &checkAuth){

#                     if($input{'submit'} eq "商品を登録"){
#                              #商品をデータベースに登録
#                              $store->RegistMerchandise( *input );
#                     }elsif($input{'submit'} eq "商品一覧に戻る"){
#                              #商品リスト表示に戻る
#                              $store->EditStore( $input{'store_id'} );
#                     }

               #------------------------------------------------------
               # 登録済み商品の編集・更新・削除
               # HTML Template :  editgoods.html  からのPOSTデータ処理
               #--------------------------------------------------------
#               }elsif( $input{'mode'} eq 'UpdateMerchandise' && &checkAuth){

#                     if($input{'submit'} eq "削除"){

#                           #ショッピングカート登録商品削除
#                           $store->DeleteGoods( $input{'goods_id'} );

#                     }elsif($input{'submit'} eq "商品一覧に戻る"){

#                           #商品リスト表示に戻る
#                           $store->EditStore( $input{'store_id'} );

#                     }if($input{'submit'} eq "商品情報を更新"){

#                           #商品データベースを更新
#                           $store->UpdateMerchandise( *input);

#                     }

               #------------------------------------------------------
               # 指定したストアの商品リスト表示
               # HTML Template :  editstore.html   からのPOSTデータ処理
               #------------------------------------------------------
               }elsif( $input{'mode'} eq 'EditStore' && &checkAuth){

                     #ショッピングカート登録商品編集ページ 表示
                     if($input{'submit'} eq "編集"){
                           #
                           $store->EditMerchandice( $input{'store_id'},$input{'goods_id'} );

                     }elsif($input{'submit'} eq "商品追加"){

                           #ショッピングカート登録商品追加ページ表示
                           $store->AddGoods( $input{'store_id'} );

                     }elsif($input{'submit'} eq "全表示"){

                           #商品リストを全て表示
                           $store->EditStore( $input{'store_id'},"search",$input{'order'},$input{'search'},"");

                     }else{

                           $store->EditStore( $input{'store_id'},"search",$input{'order'},$input{'search'},$input{'keyword'});
                           #ショッピングカートHTML生成
                           #$store->CreateHtml( $input{'goods_id'} );

                     }

               #------------------------------------------------------
               # 配送料金用地域データの編集 画面表示
               # HTML Template :  editdelivfee.html   からのPOSTデータ処理
               #------------------------------------------------------
               }elsif($input{'mode'} eq "editdelivfee" && &checkAuth){

                     if( $input{'submit'} eq "設定" ){
                          $store->editDelivBlock();
                     }elsif($input{'submit'} eq "追加"){
                          $store->addDelivFee(*input);
                     }elsif($input{'submit'} eq "削除"){
                          $store->delDelivFee();
                     }

               #------------------------------------------------------
               # 配送料金用地域データの編集
               # HTML Template :  block.html   からのPOSTデータ処理
               #------------------------------------------------------
               }elsif( $input{'mode'} eq 'block' && &checkAuth){

                     if($input{'submit'} eq "作成"){
                          $store->createDelivBlock($input{'newdistrict'},$input{'pos_district'},$input{'direction'});
                     }elsif($input{'submit'} eq "追加"){
                          $store->addPref($input{'district'},$input{'newpref'});
                     }elsif($input{'submit'} eq "削除"){
                          $store->deletePref($input{'district'},$input{'newpref'});
                     }elsif($input{'submit'} eq "消去"){
                          $store->deleteDelivBlock($input{'clear_district'});
                     }elsif($input{'submit'} eq "テスト"){
                          $store->deleteDelivBlock($input{'clear_district'});
                     }else{

                     }

               #------------------------------------------------------
               # 認証チェック
               #HTML Template :
               #------------------------------------------------------
               }elsif( $input{'mode'} eq 'revise' && &checkAuth ){

                       $store-> ReviseStoreInfo( *input );

               #------------------------------------------------------
               #環境設定
               #HTML Template :
               #------------------------------------------------------
               }elsif( $input{'mode'} eq 'editenv' && &checkAuth ){

                       $store-> setEnv( *input );

               #------------------------------------------------------
               # 商品情報更新
               #  HTML Template :
               #------------------------------------------------------
               }elsif( $input{'mode'} eq 'update' ){

                       $cart->add2cart( *input);

               #------------------------------------------------------
               #
               # HTML Template :
               #------------------------------------------------------
               }elsif( $input{'mode'} eq 'regist1' ){

                       $cart->regist2( *input );

               #------------------------------------------------------
               # 支払い方法選択画面からのPOST情報を取得し確認画面を表示
               # HTML Template :
               #------------------------------------------------------
               }elsif( $input{'mode'} eq 'regist2' ){

                       $cart->confirm( *input );

               #------------------------------------------------------
               # 確認画面からのPOST情報を取得
               # HTML Template :
               #------------------------------------------------------
               }elsif($input{'mode'} eq 'confirm'){

                       $cart->confirm(*input);

               #------------------------------------------------------
               # 完了画面表示
               # HTML Template :
               #------------------------------------------------------
               }elsif($input{'mode'} eq 'complete'){

                       $cart->complete(*input);

               #------------------------------------------------------
               #
               # HTML Template :
               #------------------------------------------------------
               }elsif( $input{'mode'} eq 'add'){


                       $cart->add2cart(*input);

#                        if( $input{'color'} eq "none"  ||  $input{'size'} eq "none"){
#                               $cart->details($input{'sid'},$input{'gid'},$input{'color'},$input{'size'},$input{'counter'});
#                        }else{
#                               $cart->addmerchandise( $input{'sid'},$input{'gid'}, $input{'color'},$input{'size'},$input{'counter'});
#                        }

               #------------------------------------------------------
               #
               # HTML Template :
               #------------------------------------------------------
               }elsif( $input{'mode'} eq 'gcart' ){

                       $cart->dispcart( $input{'sid'},$input{'g1'}, $input{'g2'},$input{'sgroup'},$input{'keyword'},$input{'p'});

               #------------------------------------------------------
               # 買い物カゴの表示
               # HTML Template :
               #------------------------------------------------------
               }elsif( $input{'mode'} eq 'dispbasket' ){

                       $cart->dispbasket( $input{'sid'} );

               #------------------------------------------------------
               # 受注集計
               # HTML Template :
               #------------------------------------------------------
               }elsif( $input{'mode'} eq 'orderlist' && &checkAuth ){

                       $datum->orderlist(*input);

               #------------------------------------------------------
               # 売上の表示
               # HTML Template :
               #------------------------------------------------------
               }elsif($input{'mode'} eq 'sales' && &checkAuth){

                       $datum->salesstat(*input);

               #------------------------------------------------------
               # 顧客情報の表示
               # HTML Template :
               #------------------------------------------------------
               }elsif($input{'mode'} eq 'customerlist' && &checkAuth){

                       $datum->customerInfo(*input);

               #------------------------------------------------------
               # 顧客情報の更新
               # HTML Template :
               #------------------------------------------------------
               }elsif($input{'mode'} eq 'editcustomer' && &checkAuth){

                       $datum->editCustomer(*input);

               #------------------------------------------------------
               #
               # HTML Template :
               #------------------------------------------------------
               }elsif($input{'mode'} eq 'editstatus' && &checkAuth){

               $datum->showOrder(*input);

               #------------------------------------------------------
               #
               # HTML Template :
               #------------------------------------------------------
               }elsif($input{'mode'} eq 'deletesales' && &checkAuth){

                       $datum->deleteSales(*input);

               }else{

                    &DispBlank;

               }

      }else{
                &DispBlank;
      }

}

#//////////////////////////////////////////////////////////////////////////////////////////////
#    HTTP Get リクエストの処理
#//////////////////////////////////////////////////////////////////////////////////////////////
sub doGet{

        if (&ReadParse(*input) ) {

                  #------------------------------------------------------
                  #
                  # HTML Template :
                  #------------------------------------------------------
                  if( $input{'mode'} eq 'newstore' && &checkAuth ){

                       $store->NewStore;

                  #------------------------------------------------------
                  #
                  # HTML Template :
                  #------------------------------------------------------
                  }elsif( $input{'mode'} eq 'showstore'  && &checkAuth){

                       $store->ShowStore();

                  #------------------------------------------------------
                  #
                  # HTML Template :
                  #------------------------------------------------------
                  }elsif( $input{'mode'} eq 'cart' ){

                       $cart->dispcart( $input{'sid'},$input{'g1'}, $input{'g2'},'','',$input{'p'});

                  #------------------------------------------------------
                  #
                  # HTML Template :
                  #------------------------------------------------------
                  }elsif( $input{'mode'} eq 'add' || $input{'mode'} eq 'update' ){
                       $cart->add2cart(*input);
#                       $cart->addmerchandise( $input{'sid'},$input{'gid'});

                  #------------------------------------------------------
                  #
                  # HTML Template :
                  #------------------------------------------------------
#                  }elsif( $input{'mode'} eq 'update' ){

#                       $cart->updatemerchandise( *input );

                  #------------------------------------------------------
                  #
                  # HTML Template :
                  #------------------------------------------------------
                  }elsif( $input{'mode'} eq 'delete' ){

                       $cart->deletemerchandise( *input );

                  #------------------------------------------------------
                  #
                  # HTML Template :
                  #------------------------------------------------------
                  }elsif( $input{'mode'} eq 'buy' ){

                       $cart->regist1( *input );

                  #------------------------------------------------------
                  #
                  # HTML Template :
                  #------------------------------------------------------
                  }elsif( $input{'mode'} eq 'dispbasket' ){

                       $cart->dispbasket( $input{'sid'} );

                  #------------------------------------------------------
                  #
                  # HTML Template :
                  #------------------------------------------------------
                  }elsif( $input{'mode'} eq  'editdfee' && &checkAuth ){

                       $store->editDelivFee();

                  #------------------------------------------------------
                  #
                  # HTML Template :
                  #------------------------------------------------------
                  }elsif( $input{'mode'} eq  'details' ){

                       $cart->details($input{'sid'},$input{'gid'},$input{'color'},$input{'size'},$input{'counter'});

                  #------------------------------------------------------
                  #
                  # HTML Template :
                  #------------------------------------------------------
                  }elsif( $input{'mode'} eq  'revise' && &checkAuth ){

                       $store->DispStoreInfo($input{'sid'});

                  #------------------------------------------------------
                  #
                  # HTML Template :
                  #------------------------------------------------------
                  }elsif( $input{'mode'} eq  'celaw' ){

                       $cart->celaw($input{'sid'});

                  #------------------------------------------------------
                  #
                  # HTML Template :
                  #------------------------------------------------------
                  }elsif( $input{'mode'} eq 'top' && &checkAuth){

                       &MainMenu;

                  #------------------------------------------------------
                  #
                  # HTML Template :
                  #------------------------------------------------------
                  }elsif( $input{'mode'} eq 'createHtml' && &checkAuth ){

                       $store->createhtml( $input{'sid'},$input{'gid'});

                  #------------------------------------------------------
                  #
                  # HTML Template :
                  #------------------------------------------------------
                  }elsif($input{'mode'} eq 'env' && &checkAuth){

                       $store->setEnv(*input);

                  #------------------------------------------------------
                  #
                  # HTML Template :
                  #------------------------------------------------------
                  }elsif( $input{'mode'} eq 'orderlist' && &checkAuth ){

                       $datum->orderlist(*input);

                  #------------------------------------------------------
                  #
                  # HTML Template :
                  #------------------------------------------------------
                  }elsif($input{'mode'} eq 'sales' && &checkAuth){

                       $datum->salesstat(*input);

                  #------------------------------------------------------
                  #
                  # HTML Template :
                  #------------------------------------------------------
                  }elsif($input{'mode'} eq 'customer' && &checkAuth){

                       $datum->customerInfo(*input);

                  #------------------------------------------------------
                  #
                  # HTML Template :
                  #------------------------------------------------------
                  }elsif($input{'mode'} eq 'cdetails' && &checkAuth){

                       $datum->showCustomer(*input);

                  #------------------------------------------------------
                  #
                  # HTML Template :
                  #------------------------------------------------------
                  }elsif($input{'mode'} eq 'showorder' && &checkAuth){

                       $datum->showOrder(*input);

                  #------------------------------------------------------
                  #
                  # HTML Template :
                  #------------------------------------------------------
                  }elsif( $input{'mode'} eq 'manage' ){

                       $auth->Login;

                  }else{

                       &DispBlank;

                  }

        }else{

                &DispBlank;

        }

}

# --------------------------------------------------------
#   メインメニューに戻る
# --------------------------------------------------------
sub MainMenu {

      my $tmpl = HTML::Template->new(
         die_on_bad_params => 0,
         filename          => './html/top.html'
      );

      print "Content-Type: text/html\n\n";
      print $tmpl->output();

}

# --------------------------------------------------------
#   認証チェック
# --------------------------------------------------------
sub checkAuth{
my ($key);

   $key=$butil->get_cookie("REMISE");
   if($key eq "$user_id$password"){
       return 1;
   }else{
       return 0;
   }

}

# --------------------------------------------------------
#   空白画面表示
# --------------------------------------------------------
sub DispBlank{
my($remotehost,$remoteip);

       $remotehost=$ENV{"REMOTE_HOST"};
       $remoteip=$ENV{'REMOTE_ADDR'};

       if(!$remotehost){$remotehost=$remoteip}

      my $tmpl = HTML::Template->new(
         die_on_bad_params => 0,
         filename          => './html/blank.html'
      );

      $tmpl->param(
            remotehost=>$remotehost,
            remoteaddr=>$remoteip
      );

      print "Content-Type: text/html\n\n";
      print $tmpl->output();

}

# --------------------------------------------------------
#
# --------------------------------------------------------
sub readPostData
{
   local (*queryString) = @_ if @_;

   local($contentLength);

   # 環境変数ＣＯＮＴＥＮＴ＿ＬＥＮＧＴＨを読み込む

   $contentLength = $ENV{"CONTENT_LENGTH"};

   #読み込むデータがあるか調べる

   if($contentLength)
   {
       # 標準入力から変数$queryString にcontentLength 分の文字を読み込む
       read(STDIN,$queryString,$contentLength);
   }

   return 1;

}

# --------------------------------------------------------
#
# --------------------------------------------------------
sub parseData
{
   local (*queryString,*formData) = @_;

   local($key,$value,$curString,@tmpArray);

   @tmpArray = split(/&/,$queryString);

   foreach $curString (@tmpArray)
   {

       ($key,$value)=split(/=/,$curString);

       &decodeData(*key);
       &decodeData(*value);

       $formData{$key} = $value;

   }

   return 1;

}

# --------------------------------------------------------
#
# --------------------------------------------------------
sub decodeData
{
   local(*queryString) = @_ if @_;

   #'+'を空白に変換する

   $queryString = ~  s/\+/ /g;
   $queryString = ~  s/%(..)/pack("c",hex($1))/ge;

   return 1;

}
