#!/usr/bin/perl5 -w
# 漢字コード EUC

#
use SIC;
use SICX::Commander;
use SICX::Commander::Auth;
use SICX::Util::File;
use Cycom::Util::Func;

my %MARGIN = ('mizuho'=>1, 'fvac'=>1, 'koshigaya'=>1 );

my $comm = SICX::Commander->new();

unless (&start($comm)) {
    $comm->error();
}

$comm->finish();

exit(0);

#
sub start {
    my $comm = shift;

    $comm->init_sess() or return 0;

    my $auth = SICX::Commander::Auth->new($comm);
    $auth->init();

    my $job = $comm->{cgi}->param("job") || "";
    $job eq "submit" or $job = "default";
    $job = sprintf("job_%s", $job);
    return &{$job}($comm, $auth);
}

sub render_page {
    my ($comm, $auth, $siteid, $passwd, $mem_sitepw, $ec, $def_siteid) = @_;

    my %new_design = (
        'main' => '1',
        'arida' => '1',
        'gobo' => '1',
        'wakayama' => '1',
        'kihoku' => '1',
        'hashimoto' => '1',
        'kinan' => '1'
    );

    my $w_siteid = $def_siteid || $comm->{cgi}->param('siteid') || $siteid || '';
    if (defined $new_design{$w_siteid}){
        &render_new_page($comm, $auth, $siteid, $passwd, $mem_sitepw, $ec, $def_siteid);
        return 1;
    }

    print
        $comm->{http}->header();
#        $comm->{html}->header();
    

    
    my $header = &header($comm, $w_siteid);#'HEADER';
    my $footer = &footer($comm, $w_siteid);#'FOOTER';
#    my $header = &header($comm, $siteid);#'HEADER';
#    my $footer = &footer($comm, $siteid);#'FOOTER';

    $comm->auto_html_footer();

    my $ec_html = $ec ? "<p>ec: $ec</p>" : "";
    my $mem_sitepw_check = $mem_sitepw ? " checked" : "";
#$ec_html

    my $margin = '';
    unless ( $MARGIN{$w_siteid} ){
        $margin = qq{ style="margin: 0px"};
    }

    print <<EOF;
<html>
<head>
<title>ログイン</title>
<meta http-equiv="Content-Type" content="text/html; charset=euc-jp">
<link rel="stylesheet" href="/css/default.css" type="text/css"/>
  <style type="text/css">
    <!--
body {
	text-align:center;
	margin: auto;
}

#body_attr{
	margin: 15px 0 0 10px;
}

#wrapper {
    margin: auto;
    text-align: center;
    width: 750px;
    background-color: #FFFFFF;
}
      -->
  </style>
</head>
<body id="body_attr">
<div id="wrapper">

$header

<form action="$ENV{SCRIPT_URL}" method="POST" style="margin:2em;">
<input type="hidden" name="job" value="submit">
<input type="hidden" name="def_siteid" value="$def_siteid">

<table border="0" cellpadding="6" style="border:1px solid #666666;" align="center">
  <tr>
    <td height="25">サイトＩＤ:</td>
    <td><input type="text" name="siteid" value="$siteid"></td>
  </tr>
  <tr>
    <td height="25" nowrap>パスワード:</td>
    <td><input type="password" name="passwd" value="$passwd">　<input type="submit" value="ログイン"></td>
  </tr>
  <tr>
    <td colspan="2" height="25"><input type="checkbox" name="mem_sitepw"$mem_sitepw_check>ID とパスワードを記憶する</td>
  </tr>
</table>

</form>

$footer

</div>
EOF
}





sub render_new_page {
    my ($comm, $auth, $siteid, $passwd, $mem_sitepw, $ec, $def_siteid) = @_;

    my $w_siteid = $def_siteid || $comm->{cgi}->param('siteid') || $siteid || '';
    my $site_dir = ($w_siteid eq 'gobo') ? 'gobo-hidaka' : $w_siteid;

    print
        $comm->{http}->header();
#        $comm->{html}->header();
    


    $comm->auto_html_footer();

    my $ec_html = $ec ? "<p>ec: $ec</p>" : "";
    my $mem_sitepw_check = $mem_sitepw ? " checked" : "";


    print <<EOF;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>ログイン</title>
<meta http-equiv="Content-Type" content="text/html; charset=euc-jp">
<link href="/$site_dir/css/common.css" rel="stylesheet" type="text/css" />
  <style type="text/css">
    <!--
      -->
  </style>
</head>
<body>
<div id="wrapper">
${\( &header($comm, $w_siteid, 'default1000') )}
${\( &sidebar($comm, $w_siteid, 'default1000') )}

<div id="container">

  <h3 class="page_title">管理者ログイン</h3>

<form action="$ENV{SCRIPT_URL}" method="POST" style="margin:2em;">
<input type="hidden" name="job" value="submit">
<input type="hidden" name="def_siteid" value="$def_siteid">

<table border="0" cellpadding="6" style="border:1px solid #666666;" align="center">
  <tr>
    <td height="25">サイトＩＤ:</td>
    <td><input type="text" name="siteid" value="$siteid"></td>
  </tr>
  <tr>
    <td height="25" nowrap>パスワード:</td>
    <td><input type="password" name="passwd" value="$passwd">　<input type="submit" value="ログイン"></td>
  </tr>
  <tr>
    <td colspan="2" height="25"><input type="checkbox" name="mem_sitepw"$mem_sitepw_check>ID とパスワードを記憶する</td>
  </tr>
</table>

</form>

</div><!-- /container -->

${\( &footer($comm, $w_siteid, 'default1000') )}

</div>
EOF
}

sub job_default {
    my ($comm, $auth) = @_;

    my ($siteid, $passwd) = ("", "");
    my $mem_sitepw = $auth->get_data("mem_sitepw") || 0;
    if ($mem_sitepw) {
        $siteid = $auth->get_data("siteid") || $comm->{cgi}->param('siteid') || '';
        if ($siteid) {
            $passwd = $auth->get_data("passwd") || "";
        }
    }
    my $def_siteid = $siteid || '';

    my $ec = $comm->{cgi}->param("ec") || "";

    &render_page($comm, $auth, $siteid, $passwd, $mem_sitepw, $ec, $def_siteid);

    return 1;
}

sub job_submit {
    my ($comm, $auth) = @_;

    my $cgi = $comm->{cgi};

    my $siteid = $cgi->param("siteid") || "";
    my $passwd = $cgi->param("passwd") || "";
    my $mem_sitepw = $cgi->param("mem_sitepw") ? 1 : 0;

    my $ec = "";
    if (is_valid_id($siteid) and is_valid_id($passwd)) {
        my $bool;

        unless (($bool) = $auth->by_site($siteid, $passwd)) {
            $comm->auto_html_footer();
            return 0;
        }

        if ($bool) {
            $auth->clear_login();
            $auth->set_data(
                siteid => $siteid,
                passwd => $passwd,
                mem_sitepw => $mem_sitepw
            );
            $auth->apply_data_to_sess();

            $comm->auto_save_sess();

            print
                $comm->{http}->redirect(
                    sprintf("%s?siteid=%s",
                        '/cgi-edit/sites/menu.cgi',
                        $siteid
                    )
                );

            return 1;
        }
        else {
            $ec = $auth->get_status("ec");
        }

    }
    else {
        $ec = "invalid_site_param";
    }

    my $def_siteid = $cgi->param("def_siteid") || "";
    &render_page($comm, $auth, $siteid, $passwd, $mem_sitepw, $ec, $def_siteid);

    return 1;
}

#
sub header {
    my $comm = shift;
    my ($siteid, $name) = @_;
    $name ||= 'default';
    $name .= '.html';

    my $html = include_html($comm,'header', siteid=>$siteid, name=>$name);

    return $html;
}

sub footer {
    my $comm = shift;
    my ($siteid, $name) = @_;
    $name ||= 'default';
    $name .= '.html';
    #
    my $html = include_html($comm,'footer', siteid=>$siteid, name=>$name);

    return $html;
}

sub sidebar {
    my $comm = shift;
    my ($siteid, $name) = @_;
    $name ||= 'default';
    $name .= '.html';
    #
    my $html = include_html($comm,'sidebar', siteid=>$siteid, name=>$name);

    return $html;
}

#
exit(0);
