<?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$
 * $Revision$
 * $Date$
 **********************************************************/

/* ƹԤκ祿ֿ */
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/%s%d.png");

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

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

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

/* ƥץ졼ȥǥ쥯ȥ */
define("TMPLDIR", "tmpl/");

/* ƥ२顼ƥץ졼 */
define("SYSERR_TEMPLATE", "system_err.tmpl");

/* 롼׳ϥ */
define("STARTTAG", "<<STARTLOOP>>");

/* 롼׽λ */
define("ENDTAG", "<<ENDLOOP>>");

/*----------------------------------------------------------
 * 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 = "";

    /* ե뤬ɤ߹᤿ */
    if (isset($web_conf["global"]["titlename"]) === TRUE) {

        /* ȥ̾ѿ */
        $title_name = escape_html($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>
</head>

EOD;

}

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

    print <<<EOD
<table class="footer">
  <td class="footer"></td>
</table>
</body>
</html>
EOD;

}

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

    /* å */
    $hidden = "<input type=\"hidden\" name=\"sk\" value=\"" .
               $sesskey . "\">";

    /* å */
    $message = "";
    if (!is_null($msg)) {
        $message = "<input type=\"hidden\" " .
                              "name=\"msg\" value=\"$msg\">";
    }

    /* HTML */
    display_header();
    print <<<EOD
<script type="text/javascript">
<!--
function msgConfirm(msg) {
        return(window.confirm(msg));
}

function dgpSubmit(url) {
    document.common.action = url;
    document.common.submit();
}
// -->
</script>
<body onload="dgpSubmit('$url')">
...
<form method="post" name="common">
    $hidden
    $message
</form>
</body>
</html>
EOD;
    exit;

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

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

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

/***********************************************************
 * get_tab()
 *
 * ֤ɽHTMLʸ֤
 *
 * []
 *      ʤ
 * [֤]
 *      $tab_html   HTMLʸ
 ************************************************************/
function get_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);
            }
        }
    }

    /* ֿ */
    $num = count($tab);

    /* ֤HTML */
    $tab_html = "";

    /* ֤1ʲκݥ֤ɽʤ */
    if ($num > 1) {
        /* ֤ɽ */
        $tab_html .= "\n<table class=\"tab\">\n";
        for ($i = 0; $i < $tabs; $i++) {
            /* ֺ */
            if (($i % MAX_TAB_NUM) == 0) {
                $tab_html .= "  <tr>\n";
            }

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

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

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

    return $tab_html;
}

/***********************************************************
 * get_topic()
 *
 * ѥ󤯤ɽHTMLʸ֤
 *
 * []
 *      ʤ
 * [֤]
 *      $topic_html   HTMLʸ
 ************************************************************/
function get_topic()
{
    global $tab_conf;
    global $menu_conf;
    global $url_data;

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

    /* ѥ󤯤HTML */
    $topic_html = "<table class=\"head\"><tr><td class=\"topic\">";

    /* ̤ξ */
    if ($tab == "") {
        $topic_html .= "&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"]) {
                $topic_html .= $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) {
                $topic_html .= "<a href=\"#\" onClick=\"dgpSubmit('../')\">";
                $topic_html .= $tab_conf[$script][$i][$key];
                $topic_html .= "</a>";
                break;
            }
        }

        $topic_html .= "&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) {
                $topic_html .= $menu_conf[$script][$tab][$i][$key];
                break;
            }
        }
    }

    $topic_html .= "</td></tr></table>";

    return $topic_html;
}

/*********************************************************
 * display()
 * ƥץ졼ȥեɤ߹ߥִԤ
 *
 * []
 *      $template     ƥץ졼ȥե
 *      $tag          ֤륿
 *        : $tag[<<TAG>>] = "֤ʸ"
 *
 *      $looptag      롼ץ
 *        : $looptag[0][<<LOOPTAG>>] = "롼1"
 *              $looptag[1][<<LOOPTAG>>] = "롼2"
 *
 *      $starttag     롼׳ϥ
 *      $endtag       롼׽λ
 * [֤]
 *      ʤ
 **********************************************************/
function display($template, $tag, $looptag, $starttag, $endtag)
{
    global $err_msg;
    global $log_msg;
    global $basedir;

     /* ƥץ졼ȥեѥ */
     $template = $basedir . TMPLDIR . $template;

     /* ƥץ졼ȥեɤ߹ */
     $ret = read_template($template, $html);
     if ($ret === FALSE) {
         return FALSE;
     }

     /* ƥץ졼ȥեִ */
     $print_html = replace_template_tag($html, $tag, $looptag,
                                        $starttag, $endtag);
     /* HTML ɽ */
     print $print_html;
}

/*********************************************************
 * set_tab
 *
 * ѥκ
 *
 * []
 *       $tab_tag            ѥκ
 *
 * [֤]
 *       ʤ
 **********************************************************/
function set_tab(&$tab_tag)
{
    global $url_data;
    global $tab_conf;
    global $topdir;

    /* tab_confΤȤɽʤ */
    if (isset($tab_conf) === FALSE) {
        return;
    }

    /* ֤ο */
    $tabs = 0;

    /* ץ̾Ф */
    foreach($tab_conf as $script => $value) {
        foreach($value as $tab_key => $tab_value) {
            /* ѥΤ᥿̾Ф */
            $tmp_tab =  key($tab_value);
            /* ֤URL */
            $url =  $topdir . $script . "/" . $tmp_tab . "/";

            /* ֤URLȥƥ֤ɤξɽ̾򥿥 */
            if ($url_data["tab"] == $tmp_tab) {
                $tab_tag[$tabs]["<<ACTIVE>>"] = "active";
            } else {
                $tab_tag[$tabs]["<<ACTIVE>>"] = "";
            }

            $tab_tag[$tabs]["<<URL>>"] = $url;
            $tab_tag[$tabs]["<<NAME>>"] = $tab_value[$tmp_tab];

            $tabs++;
        }
    }
}

/*********************************************************
 * set_menu
 *
 * ˥塼ѥκ
 *
 * []
 *       $menu_tag            ˥塼ѥ
 *
 * [֤]
 *       ʤ
 **********************************************************/
function set_menu(&$menu_tag)
{
    global $url_data;
    global $menu_conf;
    global $topdir;

    /* ˥塼ʤȤ˥塼֤ */
    if(isset($menu_conf[$url_data["script"]][$url_data["tab"]]) === FALSE) {
        return;
    }

    /* ֿν */
    $tabs = 0;

    $menu_html = "";
    foreach ($menu_conf[$url_data["script"]][$url_data["tab"]] as $key_num
                                                               => $value_arr) {

        /* ˥塼Υѥ̾ */
        $tmp_name = key($value_arr);
        /* ץ̾ */
        $script_key = key($menu_conf);

        /* ˥塼Υѥ */
        $url =  $topdir . $url_data["script"] . "/" . $url_data["tab"]
             ."/" . $tmp_name . "/";

        /* ˥塼ɽ̾ */
        $menu_name = $value_arr[$tmp_name];

        /* ˥塼ƥ֤ɤURL˥塼ɽ̾򥿥 */
        if ($tmp_name == $url_data["menu"]) {
            $menu_tag[$tabs]["<<ACTIVE>>"] = "active";
        } else {
            $menu_tag[$tabs]["<<ACTIVE>>"] = "";
        }
        $menu_tag[$tabs]["<<URL>>"] = $url;
        $menu_tag[$tabs]["<<NAME>>"] = $menu_name;

        $tabs++;
    }

    return $menu_html;
}

/*********************************************************
 * replace_tab()
 * ֤ѴԤ
 *
 * []
 *      $html         ִԤHTML
 * [֤]
 *      $html        ִHTML
 **********************************************************/
function replace_tab($html)
{
    $rest_html = $html;
    $mod_html = "";
    $starttag = "<<START_TAB>>";
    $endtag   = "<<END_TAB>>";

    set_tab($tab_tag);
    while(1) {

        /* 롼׳ϥʬ */
        $dev_s = explode($starttag, $rest_html, 2);
        $mod_html .= $dev_s[0];
        if (isset($dev_s[1]) === FALSE || $dev_s[1] == "") {
            /* 롼׳ϥĤʤä */
            break;
        }

        /* 롼׽λʬ */
        $dev_e = explode($endtag, $dev_s[1], 2);
        if ($dev_e[1] == "") {
            /* 롼׽λĤʤä */
            $mod_html .= $starttag . $dev_s[1];
            break;
        }

        /* 롼ΥѴ */
        $loop_html = $dev_e[0];         // 롼ץHTML
        foreach ($tab_tag as $key => $value) {
            $str = change_template_tag($loop_html, $value);
            $mod_html .= $str;

        }

        /* Ĥ */
        $rest_html = $dev_e[1];
    }

    return $mod_html;
}

/*********************************************************
 * replace_menu()
 * ˥塼֤ѴԤ
 *
 * []
 *      $html         ִԤHTML
 * [֤]
 *      $html        ִHTML
 **********************************************************/
function replace_menu($html)
{
    $rest_html = $html;
    $mod_html = "";
    $starttag = "<<START_MENU>>";
    $endtag   = "<<END_MENU>>";

    set_menu($menu_tag);
    while(1) {

        /* 롼׳ϥʬ */
        $dev_s = explode($starttag, $rest_html, 2);
        $mod_html .= $dev_s[0];
        if (isset($dev_s[1]) === FALSE || $dev_s[1] == "") {
            /* 롼׳ϥĤʤä */
            break;
        }

        /* 롼׽λʬ */
        $dev_e = explode($endtag, $dev_s[1], 2);
        if ($dev_e[1] == "") {
            /* 롼׽λĤʤä */
            $mod_html .= $starttag . $dev_s[1];
            break;
        }

        /* 롼ΥѴ */
        $loop_html = $dev_e[0];         // 롼ץHTML
        $loop_num = count($menu_tag);   // 롼פ

        /* 롼ץʬΤߥ롼פ */
        for ($i = 0; $loop_num > $i; $i++) {
            $str = change_template_tag($loop_html, $menu_tag[$i]);
            $mod_html .= $str;
        }

        /* Ĥ */
        $rest_html = $dev_e[1];
    }
    return $mod_html;
}

/*********************************************************
 * replace_template_tag()
 * ƥץ졼ȥѴԤ
 *
 * []
 *      $html         ִԤHTML
 *      $tag          󤬳Ǽ줿Ϣ
 *      $looptag      롼ץˤϤޤ줿󤬳ǼƤ
 *      $starttag     롼׳ϥ򼨤ʸ
 *      $endtag       롼׽λ򼨤ʸ
 * [֤]
 *      $html        ִHTML
 **********************************************************/
function replace_template_tag($html, $tag, $looptag, $starttag, $endtag)
{

    global $url_data;

    /* ̤Ƥβ̤ǥ˥塼ȥ֤ɽԤ */
    if ($url_data["tab"] != "") {
        $html = replace_tab($html);
        $html = replace_menu($html);
    }

    /* 롼ץλ꤬ʤ */
    if ($starttag == "" || $endtag == "") {
        return change_template_tag($html, $tag);
    }

    /* 롼ץ */
    $rest_html = $html;
    $mod_html = "";

    while(1) {

        /* 롼׳ϥʬ */
        $dev_s = explode($starttag, $rest_html, 2);
        $mod_html .= $dev_s[0];
        if (isset($dev_s[1]) === FALSE || $dev_s[1] == "") {
            /* 롼׳ϥĤʤä */
            break;
        }

        /* 롼׽λʬ */
        $dev_e = explode($endtag, $dev_s[1], 2);
        if ($dev_e[1] == "") {
            /* 롼׽λĤʤä */
            $mod_html .= $starttag . $dev_s[1];
            break;
        }

        /* 롼ΥѴ */
        $loop_html = $dev_e[0];         // 롼ץHTML
        $loop_num = count($looptag);   // 롼פ

        /* 롼ץʬΤߥ롼פ */
        for ($i = 0; $loop_num > $i; $i++) {
            $str = change_template_tag($loop_html, $looptag[$i]);
            $mod_html .= $str;
        }

        /* Ĥ */
        $rest_html = $dev_e[1];
    }

    /* 롼ץʳִ */
    $mod_html = change_template_tag($mod_html, $tag);

    return $mod_html;
}

/*********************************************************
 * change_template_tag()
 * ƥץ졼ȥִԤ
 *
 * []
 *      $html         ִԤHTML
 *      $tag          󤬳Ǽ줿Ϣ
 * [֤]
 *      $html        ִHTML
 **********************************************************/
function change_template_tag($html, $tag)
{

    /* ʤ̤Ѵ */
    if (count($tag) <= 0) {
        return $html;
    }

    /* ִ */
    foreach($tag as $key => $value) {
        $html = str_replace($key, $value, $html);
    }

    return $html;
}

/*********************************************************
 * read_template()
 * ꤵ줿ƥץ졼ȥեɤ߹
 *
 * []
 *      $template     ƥץ졼ȥեΥѥ
 *      $html         ƥץ졼ȥե(Ϥ)
 * [֤]
 *      TRUE          
 *      FALSE         ۾
 **********************************************************/
function read_template($template, &$html)
{
    global $msgarr;
    global $err_msg;
    global $log_msg;

    /* եɹå */
    $ret = is_readable_file($template);
    if ($ret === FALSE) {
        return FALSE;
    }

    /* եɹ */
    $html = file_get_contents($template);
    if ($html === FALSE) {
        $err_msg = sprintf($msgarr['25001'][SCREEN_MSG], $template);
        $log_msg = sprintf($msgarr['25001'][LOG_MSG], $template);
        return FALSE;
    }

    return TRUE;

}

/*********************************************************
 * syserr_display()
 * ƥ२顼̤ɽ
 *
 * []
 *      ʤ
 * [֤]
 *      ʤ
 **********************************************************/
function syserr_display()
{
    global $topdir;
    global $sesskey;
    global $err_msg;
    global $web_conf;

    /*   */
    $tag["<<TITLE>>"] = "";
    if (isset($web_conf["global"]["titlename"]) === TRUE) {
        $tag["<<TITLE>>"] = $web_conf["global"]["titlename"];
    }
    if (empty($err_msg)) {
        $tag["<<MESSAGE>>"] = "&nbsp;";
    } else {
        $tag["<<MESSAGE>>"] = $err_msg;
    }
    $tag["<<SK>>"] = "<input type=\"hidden\" name=\"sk\" value=\"" .
                           $sesskey . "\">";
    $tag["<<TOPDIR>>"]  = $topdir;
    $tag["<<JAVASCRIPT>>"] = <<<EOD
<script type="text/javascript">
<!--
function msgConfirm(msg) {
        return(window.confirm(msg));
}

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

    /* HTML */
    display(SYSERR_TEMPLATE, $tag, array(), "", "");
}

/***********************************************************
 * set_tag_common
 *
 * ̤Ѥ륿򥻥å
 *
 * []
 *      $tag    (Ϥ):
 * [֤]
 *      TRUE    
 ************************************************************/
function set_tag_common(&$tag, $javascript = "")
{
    global $web_conf;
    global $err_msg;
    global $sesskey;

    /* ȥ */
    $tag["<<TITLE>>"] = $web_conf["global"]["titlename"];

    /* 顼å */
    if (empty($err_msg)) {
        $tag["<<MESSAGE>>"] = "&nbsp;";
    } else {
        $tag["<<MESSAGE>>"] = $err_msg;
    }

    /* å󥿥  */
    $tag["<<SK>>"] = "<input type=\"hidden\" name=\"sk\" value=\"" .
                           $sesskey . "\">";

    /* ȥԥå  */
    $tag["<<TOPIC>>"] = get_topic();

    /* ֥  */
    $tag["<<TAB>>"] = get_tab();

    /* JavaScript   */
    $tag["<<JAVASCRIPT>>"] = <<<EOD
<script type="text/javascript">
<!--
function msgConfirm(msg) {
        return(window.confirm(msg));
}

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

    return TRUE;
}

/***********************************************************
 * get_menu()
 *
 * ˥塼ɽHTMLʸ֤
 *
 * []
 *      ʤ
 * [֤]
 *      $menu_html   HTMLʸ
 ************************************************************/
function get_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 . '%';

    $menu_html = "\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, $url_data["tab"], $number);

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

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

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

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

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

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

    return $menu_html;
}
?>
