<?php

/*
 * postLdapAdmin
 *
 * Copyright (C) 2006,2007 DesigNET, INC.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

/***********************************************************
 * ڡ饹
 *
 * $RCSfile: libpage,v $
 * $Revision: 1.9 $
 * $Date: 2009/06/18 07:46:12 $
 **********************************************************/

define("SYS_NAME",              "postLdapAdmin");

/* ѥޥ */
define("ADMIN_TAB_FILE",	BASEDIR . "/lib/admin.tab");
define("TAB_USER",		"user");
define("TAB_ML",		"ml");
define("TAB_ADMIN",		"admin");
define("TABROW",		3);

/* ѥ󤯤ꥹѥޥ */
define("TOPIC_USER_CONTROL",	"桼ȴ");
define("TOPIC_USER_REGIST",	"Ͽ");
define("TOPIC_USER_SEARCH_MOD",	"");
define("TOPIC_USER_ADD",	"Ͽ");
define("TOPIC_USER_EDIT",	"Խ");
define("TOPIC_USER_CSV",	"CSV");
define("RIGHT_ARROW",		"&gt;");

define("TOPIC_SYSTEM_ACCOUNT",	"ԥȴ");

define("TOPIC_SYSTEM_ML",	"᡼󥰥ꥹȴ");
define("TOPIC_SYSTEM_ML_M",	"᡼륢ɥ쥹");

/*  */
define("MAX_TAB_LINE",		1024);

/***********************************************************
 * 饹: ڡ
 **********************************************************/
Class page {

/*
 * ץѥƥ
 */

var $title = SYS_NAME;
var $message;

/*----------------------------------------------------------
 * ڡν
 *--------------------------------------------------------*/
function display($topic, $tabId)
{
    global $err_msg;
    global $err_log;
    global $env;

    /* å */
    if ($err_msg != "") {
        $this->message = $err_msg . "<br>";
    }
    if ($err_log != "") {
        $this->message .=  $err_log;
    }
    if ($this->message == "") {
        $this->message = "&nbsp;";
    } else {
        $this->message = preg_replace("/<br>$/i", "", $this->message);
    }

    $this->output_http_header();
    $this->display_header();
    $this->display_style();
    $this->display_topic($topic);
    $this->display_message();
    $this->display_tab($tabId, ADMIN_TAB_FILE);
    $this->display_body_frame();
    $this->display_footer();
}

/*----------------------------------------------------------
 * HTTP إåν
 *--------------------------------------------------------*/
function output_http_header()
{

    header("Cache-Control: no-cache, must-revalidate");	/* HTTP 1.1 */
    header("Pragma: no-cache");	/* HTTP 1.0 */
    header("Content-Type: text/html; charset=EUC-JP");

}

/*----------------------------------------------------------
 * إåɽ
 *--------------------------------------------------------*/
function display_header()
{
    print <<<EOD
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Cache-Control" content="no-cache, must-revalidate">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Content-Type" content="text/html; charset=EUC-JP">
<title>$this->title</title>
<script type="text/javascript">
<!--
function msgConfirm(msg) {
        return(window.confirm(msg));
}

function dgpSubmit(url) {
    document.common.action = url;
    document.common.submit();
}
// -->
</script>
EOD;

}

/*----------------------------------------------------------
 * ɽ
 *--------------------------------------------------------*/
function display_style()
{
    print <<<EOD

<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body topmargin="0" leftmargin="0">
<table class="title">
  <tr>
    <td class="title">$this->title</td>
    <td class="logout">
EOD;

    $this->display_logout();

    print <<<EOD
    </td>
  </tr>
</table>
EOD;
}

/*----------------------------------------------------------
 * ȥܥɽ
 *--------------------------------------------------------*/
function display_logout()
{
    global $sesskey;

    print <<<EOD

      <form method="post" action="logout.php">
        <input type="hidden" name="sk" value="$sesskey">
        <input type="submit" name="logout" value="">
      </form>

EOD;

}

/*----------------------------------------------------------
 * ѥ󤯤ɽ
 *--------------------------------------------------------*/
function display_topic($topic)
{

    print <<<EOD

<table class="head">
  <tr>
    <td class="topic">
EOD;

    if (is_array($topic) === FALSE) {
        print "&nbsp;";
    } else {

        $topic_max = count($topic);
        for ($i = 0; $i < $topic_max; $i++) {

            $name = $topic[$i]["name"];
            $url = $topic[$i]["url"];

            /* ǽΥȥԥåʳ  */
            if ($i > 0) {
                print "&nbsp;" . RIGHT_ARROW . "&nbsp;";
            }

            /* ǸΥȥԥåϥ󥯤ˤʤ */
            if ($i < $topic_max - 1) {
                 print "<a href=\"#\" onClick=\"dgpSubmit('$url')\">$name</a>";
            } else {
                 print $name;
            }
        }
    }

    print <<<EOD
    </td>
  </tr>
</table>
EOD;

}

/*----------------------------------------------------------
 * åɽ
 *--------------------------------------------------------*/
function display_message()
{

    print <<<EOD

<table class="head">
  <tr>
    <td class="message"><img class="bmessage" src="images/spacer.gif"></td>
  </tr>
  <tr>
    <td class="message">$this->message</td>
  </tr>
  <tr>
    <td class="message"><img class="amessage" src="images/spacer.gif"></td>
  </tr>
</table>

EOD;

    $this->message = "";
}

/*----------------------------------------------------------
 * ɽ
 *--------------------------------------------------------*/
function display_tab($tabId, $tabFile)
{

    global $err_msg;

    $html_h = <<<EOD

<table class="head">
  <tr>
    <td class="normal">

EOD;

    $html_f = <<<EOD
</td>
  </tr>
</table>

EOD;

    if (is_readable_file($tabFile) === FALSE) {
        $err_msg = "" . $err_msg;
        print $html_h;
        print $err_msg;
        print $html_f;
        result_log($err_msg);
        return(-1);
    }

    $fp = fopen($tabFile, "r");
    if ($fp === FALSE ) {
        $err_msg = "ե뤬ץǤޤ ($tabFile)"; 
        print $html_h;
        print $err_msg;
        print $html_f;
        result_log($err_msg);
        return(-1);
    }

    $line_cnt = 0;
    $tabs = 0;
    while (!feof($fp)) {
        $buf = fgets($fp, MAX_TAB_LINE);
        if ($buf === FALSE){
            continue; 
        }
        $buf = rtrim($buf);	
        $line_cnt ++;

        if (ereg("^#", $buf) || $buf == "") {
            continue;
        } else {
            $tmp = explode(":", $buf, 4);  
	    if (!isset($tmp[0]) || !isset($tmp[1]) ||
	        !isset($tmp[2]) || !isset($tmp[3])) {
	        print $html_h;
	        $this->message =
		"եʹԤ¸ߤޤ (" .
                $tabFile . " : " . $line_cnt . ")";
		print $this->message;
		print $html_f;
	        fclose($fp);
                result_log($err_msg);
		return(-1);
	    }
        }

        /* ϥ饤Ȥβ */
        if ($tabId == $tmp[0]) {
            $tab[$tabs] = array("img" => $tmp[2], "url" => $tmp[3]);
        } else {
            $tab[$tabs] = array("img" => $tmp[1], "url" => $tmp[3]);
        }
        $tabs++;
    }
    fclose($fp);

    /* ֤οĴ(ߡ֤) */
    $dum_tab["url"] = ":";
    for (; ($tabs % TABROW) != 0; $tabs ++) {
	array_push($tab, $dum_tab);
    }

    /* ֤ɽ */
    print "<table class=\"tab\">\n";
    for ($i = 0; $i < $tabs; $i++) {
        /* ֺ */
        if (($i % TABROW) == 0) {
	    print "  <tr>\n";
	}

	$url = $tab[$i]["url"];

        /* ߡ֤ξν */
	if ($url == ":") {
            print "    <td class=\"tab\"></td>\n";
        /* ̾Υ */
        } else {
	    print "    <td class=\"tab\">";
	    print "<a href=\"#\" onClick=\"dgpSubmit('$url')\"><img class=\"tab\" src=\"images/";
	    print $tab[$i]["img"];
	    print "\"></a></td>\n";
        }

	/* ֱ */
	if ($i == (TABROW - 1)) {
	    print "  </tr>\n";
	}
    }
    print "</table>\n";
	
}

/*----------------------------------------------------------
 * ܥǥɽ
 *--------------------------------------------------------*/
function display_body_frame()
{
    global $sys_err;
    global $sesskey;

    print <<<EOD
<table class="frame">
  <tr>
    <td class="normal"><img class="table" src="images/spacer.gif"></td>
  </tr>
  <tr>
    <td class="normal"><img src="images/spacer.gif"></td>
    <td class="normal">
      <table class="body">
        <tr>
          <td class="normal">
EOD;

    /* ƥ२顼λȤɽʤ */
    if($sys_err !== TRUE) {
        $this->display_body();
    }

    print <<<EOD

          </td>
        </tr>
      </table>
    </td>
  </tr>
  <tr>
    <td class="normal"><img class="table" src="images/spacer.gif"></td>
  </tr>
</table>
<form method="post" name="common">
  <input type="hidden" name="sk" value="$sesskey">
</form>
EOD;

}

/*----------------------------------------------------------
 * ܥǥɽ(С饤ɤ)
 *--------------------------------------------------------*/
function display_body()
{
}

/*----------------------------------------------------------
 * եåɽ
 *--------------------------------------------------------*/
function display_footer()
{

    print <<<EOD

</body>
</html>
EOD;

}

};

/***********************************************************
 * dgp_location()
 *
 * Ԥ
 *
 * []
 *      $url
 * [֤]
 *      ʤ
 ************************************************************/
function dgp_location($url, $msg = null)
{
    global $sesskey;

    $mypg = new page();

    $mypg->output_http_header();
    $mypg->display_header();

    print <<<EOD

<body onload="dgpSubmit('$url')">
...
<form method="post" name="common">
<input type="hidden" name="sk" value="$sesskey">

EOD;

    /* å */
    if (!is_null($msg)) {
        print <<< EOD
<input type="hidden" name="msg" value="$msg">

EOD;
    }

    print <<<EOD

</form>
</body>
</html>

EOD;
   exit;
    
}

/***********************************************************
 * dgp_location_search()
 *
 * Ԥ
 *
 * []
 *      $url
 * [֤]
 *      ʤ
 ************************************************************/
function dgp_location_search($url, $msg = null)
{
    global $sesskey;
    global $filter;
    global $page;
    global $form_name;
    global $name_match;

    $mypg = new page();

    $mypg->output_http_header();
    $mypg->display_header();

    print <<<EOD

<body onload="dgpSubmit('$url')">
...
<form method="post" name="common">
<input type="hidden" name="sk" value="$sesskey">
<input type="hidden" name="page" value="$page">
<input type="hidden" name="filter" value="$filter">
<input type="hidden" name="form_name" value="$form_name">
<input type="hidden" name="name_match" value="$name_match">

EOD;

    /* å */
    if (!is_null($msg)) {
        print <<< EOD
<input type="hidden" name="msg" value="$msg">

EOD;
    }

    print <<<EOD

</form>
</body>
</html>

EOD;
   exit;
    
}

?>
