<?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: dglibpage,v $
 * $Revision: 1.1.1.1 $
 * $Date: 2010/04/22 01:52:47 $
 **********************************************************/

/* ƹԤκ祿ֿ */
define("MAX_TAB_NUM", 6);

/* ƹԤκ˥塼ܥ */
define("MAX_MENU_NUM", 3);

/* ˥塼ܥβ */
define("ICON_NUM", 6);

/* 륷Ȥ̾ */
define("STYLE_CSS", "style.css");

/* ȥե̾ */
define("LOGOUT", "logout.php");

/* ˥塼ܥ̾(ֹʤ) */
define("ICON_IMAGE", "images/icon_image%d.gif");

/* ̾ */
define("SPACER_IMAGE", "images/spacer.gif");

/* ֤β */
define("ON_IMAGE", "on.png");
define("OFF_IMAGE", "off.png");

/* ɽmode */
define("MENU", 0);
define("CONTENT", 1);

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

/*
 * ץѥƥ
 */
var $message;

/*----------------------------------------------------------
 * ڡν
 *--------------------------------------------------------*/
function display($display_mode)
{
    global $err_msg;
    global $err_log;
    global $sys_err;

    /* å */
    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();
    if($sys_err !== TRUE) {
        $this->display_topic();
    }
    $this->display_message(); 
    if($sys_err !== TRUE) {
        $this->display_tab();
    }
    $this->display_body_frame($display_mode);
    $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()
{
    global $web_conf;

    /* ȥ̾ѿ */
    $title_name = htmlspecialchars($web_conf["global"]["titlename"]);

    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>$title_name</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()
{
    global $web_conf;
    global $topdir;

    /* ȥ̾ѿ */
    $title_name = htmlspecialchars($web_conf["global"]["titlename"]);

    /* 륷ȤΥѥ */
    $style_path = $topdir . STYLE_CSS;

    print <<<EOD

<link rel="stylesheet" type="text/css" href="$style_path">
</head>
<body topmargin="0" leftmargin="0">
<table class="title">
  <tr>
    <td class="title">$title_name</td>
    <td class="logout">

EOD;

    $this->display_logout();

    print <<<EOD

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

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

    /* ȥեΥѥ */
    $logout_path = $topdir . LOGOUT;

    print <<<EOD
      <form method="post" action="$logout_path">
        <input type="hidden" name="sk" value="$sesskey">
        <input type="submit" name="logout" value="">
      </form>
EOD;

}

/*----------------------------------------------------------
 * ѥ󤯤ɽ
 *--------------------------------------------------------*/
function display_topic()
{
    global $tab_conf;
    global $menu_conf;
    global $url_data;

    /* ߳ƤڡΥץ̾֡˥塼  */
    $script = $url_data["script"];
    $tab = $url_data["tab"];
    $menu = $url_data["menu"];

    print <<<EOD

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

    /* ̤ξ */
    if ($tab == "") {
        print "&nbsp;";
   
    /* ֲ̤ξ */
    } elseif ($menu == ""){
        /* ̾Ф */
        $count = count($tab_conf[$script]);

        /* ֥եȤ鸡 */
        for ($i = 0; $i < $count; $i++) {
            $key = key($tab_conf[$script][$i]);

            /* ߤΥ֤ȰפΤФ */
            if ($key == $url_data["tab"]) {
                print $tab_conf[$script][$i][$key];
                break;
            }
        }
 
    /* ˥塼̤ξ */
    } else {
        /* ֤ο */
        $count = count($tab_conf[$script]);

        /* ֥ե椫鸡 */
        for ($i = 0; $i < $count; $i++) {
            $key = key($tab_conf[$script][$i]);

            /* ߤΥ֤ȰפΤФ */
            if ($key == $tab) {
                print "<a href=\"#\" onClick=\"dgpSubmit('../')\">";
                print $tab_conf[$script][$i][$key];
                print "</a>";
                break;
            }
        }

        print "&nbsp;&gt;&nbsp;";

        /* ˥塼ο */
        $count = count($menu_conf[$script][$tab]);

        /* ֥ե椫鸡 */
        for ($i = 0; $i < $count; $i++) {
            $key = key($menu_conf[$script][$tab][$i]);

            /* ߤΥ֤ȰפΤФ */
            if ($key == $menu) {
                print $menu_conf[$script][$tab][$i][$key];
                break;
            }
        }
    }

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

}

/*----------------------------------------------------------
 * åɽ
 *--------------------------------------------------------*/
function display_message()
{
    global $url_data;
    global $topdir;

    /* եޤǤΥѥ */
    $img_spacer_path = $topdir . SPACER_IMAGE;

    print <<<EOD
<table class="head">
  <tr>
    <td class="message"><img class="bmessage" src="$img_spacer_path"></td>
  </tr>
  <tr>
    <td class="message">$this->message</td>
  </tr>
  <tr>
    <td class="message"><img class="bmessage" src="$img_spacer_path"></td>
  </tr>
</table>
EOD;

    $this->message = "";
}

/*----------------------------------------------------------
 * ɽ
 *--------------------------------------------------------*/
function display_tab()
{
    global $url_data;
    global $tab_conf;
    global $topdir;

    /* ֤ο */
    $tabs = 0;
   
    /* ץ̾Ф */ 
    foreach($tab_conf as $script => $value) {
        $i = 0;

        /* ƥ֤Υѥ */
        foreach($value as $tab_key => $tab_value) {
            $tmp_tab =  key($value[$i++]);
            $url =  $topdir . $script . "/" . $tmp_tab . "/";

            /* ϥ饤Ȥβ */
            if ($url_data["tab"] == $tmp_tab && $url_data["script"] == $script) {
                $tab[$tabs++] = array("img" => $url. ON_IMAGE,
                                "url" => $url);
            } else {
                $tab[$tabs++] = array("img" => $url. OFF_IMAGE,
                                      "url" => $url);
            }
        }
    }

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

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

        print "    <td class=\"tab\">";
        print "<a href=\"#\" onClick=\"dgpSubmit('$url')\">";
        print "<img class=\"tab\" src=\"";
        print $tab[$i]["img"];
        print "\"></a></td>\n";

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

/*----------------------------------------------------------
 * ˥塼ɽ
 *--------------------------------------------------------*/
function display_menu()
{
    global $url_data;
    global $menu_conf;
    global $topdir;

    /* ˥塼θĿμ */
    $count = count($menu_conf[$url_data["script"]][$url_data["tab"]]);

    /*  */
    if ($count > MAX_MENU_NUM) {
        /* ˥塼Ԥˤ錄Ȥ */
        $size = floor(100 / MAX_MENU_NUM);
    } else {
        /* ˥塼ԤǤȤ */
        $size = floor(100 / $count);
    }
    $widthsize = $size . '%';

    print "\n            <table class=\"body\">\n";

    /* ˥塼ο֤ */
    for ($i = 0; $i < $count; $i++) {

        /* ˥塼Υѥ̾ */
        $tmp_name = key($menu_conf[$url_data["script"]][$url_data["tab"]][$i]);

        /* ˥塼Υѥ */
        $url = $tmp_name . "/";

        /* ˥塼̾ */
        $menu_name = $menu_conf[$url_data["script"]][$url_data["tab"]][$i][$tmp_name];

        /* iconֹ */
        $number = $i % ICON_NUM;

        /* ̾ */
        $icon_name = sprintf(ICON_IMAGE, $number);

        /* iconޤǤΥѥ */
        $img_icon_path = $topdir . $icon_name;

        /* ƬΥ˥塼ˤ<tr>֤ */
        if (($i % MAX_MENU_NUM) == 0) {
            print "              <tr>\n";
        }

        print "                <td class=\"menu\" width=\"$widthsize\">";
	print "<a href=\"#\" onClick=\"dgpSubmit('$url')\">";
        print "<img class=\"menu\" src=\"";
	print $img_icon_path;
	print "\"><br>\n";
        print $menu_name;
	print "</a></td>\n";

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

    /* menu԰ʾˤʤäȤ */
    if ($count > MAX_MENU_NUM) {
        /* Ĥäơ֥Υˤ϶ */
        for ($j = $i; $j % MAX_MENU_NUM != 0;  $j++) {
            print "                <td class=\"menu\" width=\"$widthsize\">";
            print "<img src=\"";
            print $topdir . SPACER_IMAGE;
            print "\"></td>\n";
        }
    }

    /* ƹԤνˤ</tr>֤ */
    if ($i != MAX_MENU_NUM) {
        print "              </tr>\n";
    }

    print "            </table>\n";
}

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

    /* ޤǤΥѥ */
    $img_spacer_path = $topdir . SPACER_IMAGE;

    print <<<EOD
<table class="frame">
  <tr>
    <td class="normal"><img class="table" src="$img_spacer_path"></td>
  </tr>
  <tr>
    <td class="normal"><img src="$img_spacer_path"></td>
    <td class="normal">
      <table class="body">
        <tr>
          <td class="normal">
EOD;

    /* ƥ२顼λȤɽʤ */
    if($sys_err !== TRUE) {
        if ($display_mode == MENU) {
            /* ˥塼̤ɽ */
            $this->display_menu();
        } elseif ($display_mode == CONTENT) {
            /* ƥƥĲ̤ɽ */
            $this->display_body();
        } else {
            /* ꤬äƤ */
        }
    }

    print <<<EOD
          </td>
        </tr>
      </table>
    </td>
  </tr>
  <tr>
    <td class="normal"><img class="table" src="$img_spacer_path"></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;
    
}

/***********************************************************
 * err_location()
 *
 * ̤˥Ԥ
 *
 * []
 *      $url 
 * [֤]
 *      ʤ
 ************************************************************/
function err_location($url)
{
    global $topdir;

    /* ߳ƤڡΥץ̾֡˥塼  */
    url_search();

    header("Location: ". $topdir . $url);
}

?>
