<?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: index.php,v $
 * $Revision: 5.00 $
 * $Date: 2021/06/02 11:21:54 $
 **********************************************************/

include_once("../../initial");
include_once("lib/dglibpostldapadmin");
include_once("lib/dglibcommon");
include_once("lib/dglibpage");
include_once("lib/dglibsess");
include_once("lib/dglibldap");
include_once("lib/dglibsamma");

/********************************************************
ƥڡ
*********************************************************/
define("TMPLFILE", "samma/samma_admin_tmpl.tmpl");
define("MODE_SENDER", 1);
define("MODE_RCPT",   2);

/*
 * ƥץ졼Խ̤
 */
define("OPERATION_RCPT",         "ƥץ졼Խ");
define("TEMPLATEPATH_RCPT",      "rcpttemplatepath");
define("OK_MSGTMPLPATH_RCPT",    "ƥץ졼Ȥ򹹿ޤ");
define("ERR_NO_SET_PATH_RCPT",   "եԽ̤Υ᡼ƥץ졼ȥѥꤷƤ");

/*
 * ƥץ졼Խ̤
 */
define("OPERATION_SENDER",       "ƥץ졼Խ");
define("TEMPLATEPATH_SENDER",    "templatepath");
define("OK_MSGTMPLPATH_SENDER",  "ƥץ졼Ȥ򹹿ޤ");
define("ERR_NO_SET_PATH_SENDER", "եԽ̤Υ᡼ƥץ졼ȥѥꤷƤ");

/***********************************************************
 * ؿ
 **********************************************************/
/***********************************************************
 * set_tag_data()
 *
 * ֤ξ򥻥åȤ
 *
 * []
 *      $tag  ֤
 *
 * [֤]
 *       ʤ
 **********************************************************/
function set_tag_data(&$tag) {

    global $sesskey;
    global $templ_data;

    /* html */
    $cnv_templ_data = escape_html($templ_data);
    $tag["<<CNV_TMPL_DATA>>"] = $cnv_templ_data;

    return TRUE;

}

/***********************************************************
 * proc_tmpl()
 *
 * MAIN
 *
 * []
 *      $mode          1: ƥץ졼Խ
 *                     2: ƥץ졼Խ
 * [֤]
 *       ʤ
 **********************************************************/
function proc_tmpl($mode)
{
    global $web_conf;
    global $samma_conf;
    global $err_msg;
    global $templ_data;
    global $reload;
    global $commandpass;

    /* ͤν */
    $tag = array();

    /* ƥץ졼Խ */
    if ($mode === MODE_SENDER) {
        $operation = OPERATION_SENDER;
        $tmpl_path = TEMPLATEPATH_SENDER;
        $success_msg = OK_MSGTMPLPATH_SENDER;
        $err_setpath = ERR_NO_SET_PATH_SENDER;

    /* ƥץ졼Խ */
    } else if ($mode === MODE_RCPT) {
        $operation = OPERATION_RCPT;
        $tmpl_path = TEMPLATEPATH_RCPT;
        $success_msg = OK_MSGTMPLPATH_RCPT;
        $err_setpath = ERR_NO_SET_PATH_RCPT;
    }

    /* POSTͤ */
    if (isset($_POST["templ_data"]) === TRUE) {
        $templ_data = $_POST["templ_data"];
    }
    /* å󥭡ѿ */
    if (isset ($_POST["sk"]) === TRUE) {
        $sesskey = $_POST["sk"];
    }

    /* ե롦ִեɹåå */
    $ret = init();
    if ($ret === FALSE) {
        syserr_display();
        exit (1);
    }

    /* SaMMAեɹ */
    $ret = read_samma_conf($web_conf["postldapadmin"]["sammaconf"]);
    if ($ret === FALSE) {
        result_log($operation . ":NG:" . $err_msg);
        syserr_display();
        exit (1);
    }

    /* samma.confܤ̤ξ */
    if (!isset($samma_conf[$tmpl_path])) {
        $err_msg = $err_setpath;
        result_log($operation . ":NG:" . $err_msg);

        /* ̤Υ */
        set_tag_common($tag);

        set_tag_data($tag);

        /* ڡν */
        $ret = display(TMPLFILE, $tag, array(), "", "");
        if ($ret === FALSE) {
            result_log($log_msg, LOG_ERR);
            syserr_display();
            exit(1);
        }

        exit(0);
    }

    /* commandpass */
    if (isset($samma_conf["commandpass"]) === TRUE) {
        $commandpass = $samma_conf["commandpass"];
    }

    /***********************************************************
     * main
     **********************************************************/

    /* ѹܥ󤬲줿Ȥ */
    if (isset($_POST["mod"]) === TRUE) {

        /* ϥå */
        if (empty($templ_data) === TRUE) {
            $err_msg = "Υ᡼ƥץ졼ȤϤƤޤ";
            result_log($operation . ":OK:" . $err_msg);
        } else {
            /* ƥץ졼ȥեν񤭴 */
            $ret = write_file($samma_conf[$tmpl_path], STRCODE, $templ_data);
            if ($ret === FALSE) {
                result_log($operation . ":NG:" . $err_msg);
            } else {
                /* sammaƥץ졼ȤΥ */
                $ret = reload_samma(TEMPLATE);
                if ($ret === FALSE) {
                    result_log($operation . ":NG:" . $err_msg);
                } else {
                    /* ｪλ */
                    $err_msg = $success_msg;
                    result_log($operation . ":OK:" . $err_msg);

                    /* SaMMA˥塼̤ */
                    dgp_location("../index.php", $err_msg);
                    exit(0);
                }
            }
        }

    /* 󥻥ܥ󤬲줿Ȥ */
    } elseif (isset($_POST["cancel"]) === TRUE) {

        /* SaMMA˥塼̤ */
        dgp_location("../index.php", $err_msg);
        exit(0);
    }

    /* ɽ */
    if (isset($_POST["templ_data"]) === FALSE) {

        /* ƥץ졼ȥեɤ߹ */
        $ret = read_file($samma_conf[$tmpl_path], $templ_data);
        if ($ret === FALSE) {
            result_log($operation . ":NG:" . $err_msg);
            syserr_display();
            exit (1);
        }
    }

    /***********************************************************
     * ɽ
     **********************************************************/
    /* ̤Υ */
    set_tag_common($tag);

    set_tag_data($tag);

    /* ڡν */
    $ret = display(TMPLFILE, $tag, array(), "", "");
    if ($ret === FALSE) {
        result_log($log_msg, LOG_ERR);
        syserr_display();
        exit(1);
    }
}

?>
