<?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$
 **********************************************************/
// ᡼륢ɥ쥹Ĺ(forward)
define("FORWARD_LENGTH", 128);
// forwardconfե饰
define("FORWARD_ON", "1");
// csvѥʸĹ
define("ORDER_CSV_LENGTH", 128);
// csvѥƥʸĹ
define("ARTICLE_CSV_LENGTH", 1024);

// ƥؿdefault
define("FROM_FLAG", "F");
define("FROM_TERM", "f");
define("SUBJECT_FLAG", "S");
define("SUBJECT_TERM", "s");
define("RECIPIENT_FLAG", "T");
define("RECIPIENT_TERM", "t");
define("TRANS_ADDR", "A");
define("RECEIVED_MAIL", "d");
define("RECEIVED_ON", "0");
define("RECEIVED_OFF", "1");
define("INVALID_TERM", "0");
define("MATCH_TERM", "1");
define("INCLUDE_TERM", "2");
define("NOT_INCLUDE_TERM", "3");
define("EMPTY_TERM", "4");
define("MATCH_INPUT", "0");
define("INCLUDE_INPUT", "1");
define("NOT_INCLUDE_INPUT", "2");
define("EMPTY_INPUT", "3");

// ᡼ž֤
define("SENDER_ERR", -1);
define("SUBJECT_ERR", -2);
define("RECIPIENT_ERR", -3);
define("FORWARD_ERR", -4);
define("ARTICLE_ERR", -5);

/*********************************************************
 * order_analysis
 *
 * ե륿βϤԤʤ
 *
 * []
 *         $ldap         LDAP
 *         $filterorder  Ϸ
 * [֤]
 *         ̵
 *
 **********************************************************/
function order_analysis($ldap, &$filterorder)
{
    // ":"Ƕڤ
    $data = explode(":", $ldap);

    foreach ($data as $value) {
        // ʸ1ХȤʬ
        $element[0] = mb_substr($value, 0, 1);
        $element[1] = mb_substr($value, 1);

        // +-Ƚ(+ʤ1-ʤ0Ѵ)
        $status = "1";
        if ($element[0] === "-") {
            $status = "0";
        }
        $tmp_array = array("filter_id" => $element[1],
                           "state" => $status);

        // $filteroderpushƤ
        array_push($filterorder, $tmp_array);
    }

    return TRUE;
}

/*********************************************************
 * article_analysis
 *
 * ե륿ƥβϤԤʤ
 *
 * []
 *         $ldap           LDAP
 *         $filterarticle  Ϸ
 * [֤]
 *         TRUE     
 *         FALSE    ۾
 *
 **********************************************************/
function article_analysis($ldap, &$filterarticle)
{

    $pre_article = array();

    // ǡ
    foreach ($ldap as $encode_str) {
        // base64ǥ
        $decode_str = base64_decode($encode_str);

        // ʸܲ
        $tmp_data = explode(":", $decode_str, 2);

        // ե륿ID
        // ʣå
        if (isset($pre_article[$tmp_data[0]]) === TRUE) {
            // ʣƤ顼åå
            return FALSE;
        }

        // articleʬμ
        $article = $tmp_data[1];

        // articleĹ֤
        for ($article_len = strlen($article);
             $article_len > 0;
             $article_len = strlen($article)) {
            // ʸܤΥפ
            $type = substr($article, 0, 1);

            // פˤԤʤ
            switch ($type) {
                // fromν
                case FROM_FLAG :
                    // ʸڤФоݤ
                    $buf = explode(":", $article, 2);
                    $strnum = substr($buf[0], 1);
                    $strdata = $buf[1];

                    // 
                    $pre_article[$tmp_data[0]][FROM_FLAG] = substr($strdata, 0, $strnum);

                    // article
                    $article = substr($strdata, $strnum);
                break;

                // from_termν
                case FROM_TERM :
                    // 2ʸܤڤФ
                    $pre_article[$tmp_data[0]][FROM_TERM] = substr($article, 1, 1);

                    // article
                    $article = substr($article, 2);
                break;

                // subjectν
                case SUBJECT_FLAG :
                    // ʸڤФоݤ
                    $buf = explode(":", $article, 2);
                    $strnum = substr($buf[0], 1);
                    $strdata = $buf[1];

                    // 
                    $pre_article[$tmp_data[0]][SUBJECT_FLAG] = substr($strdata, 0, $strnum);

                    // article
                    $article = substr($strdata, $strnum);
                break;

                // subject_offν
                case SUBJECT_TERM :
                    // 2ʸܤڤФ
                    $pre_article[$tmp_data[0]][SUBJECT_TERM] = substr($article, 1, 1);

                    // article
                    $article = substr($article, 2);
                break;

                // recipientν
                case RECIPIENT_FLAG :
                    // ʸڤФоݤ
                    $buf = explode(":", $article, 2);
                    $strnum = substr($buf[0], 1);
                    $strdata = $buf[1];

                    // 
                    $pre_article[$tmp_data[0]][RECIPIENT_FLAG] = substr($strdata, 0, $strnum);

                    // article
                    $article = substr($strdata, $strnum);
                break;

                // recipient_offν
                case RECIPIENT_TERM :
                    // 2ʸܤڤФ
                    $pre_article[$tmp_data[0]][RECIPIENT_TERM] = substr($article, 1, 1);

                    // article
                    $article = substr($article, 2);
                break;

                // trans_addrν
                case TRANS_ADDR :
                    // ʸڤФоݤ
                    $buf = explode(":", $article, 2);
                    $strnum = substr($buf[0], 1);
                    $strdata = $buf[1];

                    // 
                    $pre_article[$tmp_data[0]][TRANS_ADDR] = substr($strdata, 0, $strnum);

                    // article
                    $article = substr($strdata, $strnum);
                break;

                // recived_mailν
                case RECEIVED_MAIL :
                    // 2ʸܤڤФ
                    $pre_article[$tmp_data[0]][RECEIVED_MAIL] = substr($article, 1, 1);

                    // article
                    $article = substr($article, 2);
                break;

                // 졢۾ʥǡäƤFALSE
                default :
                    return FALSE;
            }
        }
    }

    // ֤
    $filterarticle = $pre_article;

    return TRUE;
}

/*********************************************************
 * reflect_filter_data
 *
 * ե륿β̤ؤȿ
 *
 * []
 *         $ldap         LDAP
 *         $filterorder  Ϸ
 * [֤]
 *         TRUE     
 *         FALSE    ۾
 *
 **********************************************************/
function reflect_filter_data($filterorder, $filterarticle, &$subloop)
{
    global $web_conf;
    global $url_data;

    // 򸵤˺Ƥ
    foreach ($filterorder as $order_val) {

        $preloop = array();

        // ID¸ߤʤϽɽ
        if (! isset($filterarticle[$order_val['filter_id']])) {
            $id = $order_val['filter_id'];
            $preloop["<<FILTER_ID>>"] = $id;

            // ž饸
            $preloop["<<ALL_FORWARD>>"] = "checked";

            // ܺ饸
            $preloop["<<DETAIL_FORWARD>>"] = "";

            // 
            $preloop["<<FORWARD_CHECK>>"] = "";
            $preloop["<<FORWARD_TEXT>>"] = "";
            $preloop["<<FORWARD_MATCH>>"] = "selected";
            $preloop["<<FORWARD_INCLUDE>>"] = "";
            $preloop["<<FORWARD_NOT_INC>>"] = "";
            $preloop["<<FORWARD_EMPTY>>"] = "";

            // ̾
            $preloop["<<SUBJECT_CHECK>>"] = "";
            $preloop["<<SUBJECT_TEXT>>"] = "";
            $preloop["<<SUBJECT_MATCH>>"] = "selected";
            $preloop["<<SUBJECT_INCLUDE>>"] = "";
            $preloop["<<SUBJECT_NOT_INC>>"] = "";
            $preloop["<<SUBJECT_EMPTY>>"] = "";

            // 
            $preloop["<<RECIPT_CHECK>>"] = "";
            $preloop["<<RECIPT_TEXT>>"] = "";
            $preloop["<<RECIPT_MATCH>>"] = "selected";
            $preloop["<<RECIPT_INCLUDE>>"] = "";
            $preloop["<<RECIPT_NOT_INC>>"] = "";
            $preloop["<<RECIPT_EMPTY>>"] = "";

            // ž衢᡼ν
            $preloop["<<TRANSFER_ADDR>>"] = "";
            $preloop["<<MAIL_LEAVE>>"] = "selected";
            $preloop["<<MAIL_DEL>>"] = "";
        } else {

            // νžξ⤳͡
            // filter id
            $id = $order_val['filter_id'];
            $preloop["<<FILTER_ID>>"] = $id;

            // ž饸
            $preloop["<<ALL_FORWARD>>"] = "checked";

            // ܺ饸
            $preloop["<<DETAIL_FORWARD>>"] = "";

            // ԥå
            $preloop["<<FORWARD_CHECK>>"] = "";

            // 
            $preloop["<<FORWARD_TEXT>>"] = "";

            // Խ
            $preloop["<<FORWARD_MATCH>>"] = "selected";
            $preloop["<<FORWARD_INCLUDE>>"] = "";
            $preloop["<<FORWARD_NOT_INC>>"] = "";
            $preloop["<<FORWARD_EMPTY>>"] = "";

            // ̾å
            $preloop["<<SUBJECT_CHECK>>"] = "";

            // ̾
            $preloop["<<SUBJECT_TEXT>>"] = "";

            // ̾
            $preloop["<<SUBJECT_MATCH>>"] = "selected";
            $preloop["<<SUBJECT_INCLUDE>>"] = "";
            $preloop["<<SUBJECT_NOT_INC>>"] = "";
            $preloop["<<SUBJECT_EMPTY>>"] = "";

            // å
            $preloop["<<RECIPT_CHECK>>"] = "";

            // 
            $preloop["<<RECIPT_TEXT>>"] = "";

            // 
            $preloop["<<RECIPT_MATCH>>"] = "selected";
            $preloop["<<RECIPT_INCLUDE>>"] = "";
            $preloop["<<RECIPT_NOT_INC>>"] = "";
            $preloop["<<RECIPT_EMPTY>>"] = "";


            // ɤ줫ĤǤ̵ʳФƤܺꤢ
            if ($filterarticle[$id][FROM_TERM] !== INVALID_TERM ||
                $filterarticle[$id][SUBJECT_TERM] !== INVALID_TERM ||
                $filterarticle[$id][RECIPIENT_TERM] !== INVALID_TERM) {
                // ž饸
                $preloop["<<ALL_FORWARD>>"] = "";

                // ܺ饸
                $preloop["<<DETAIL_FORWARD>>"] = "checked";
            }

            // 
            // ̵ʳϥåܥåå
            if ($filterarticle[$id][FROM_TERM] !== INVALID_TERM) {
                $preloop["<<FORWARD_CHECK>>"] = "checked";
            }

            // ʸ󤬤ȿ
            if (isset($filterarticle[$id][FROM_FLAG])) {
                $preloop["<<FORWARD_TEXT>>"] =
                               htmlspecialchars($filterarticle[$id][FROM_FLAG]);
            }

            // Υ쥯ȥܥåȿ
            // ޤ
            if ($filterarticle[$id][FROM_TERM] === INCLUDE_TERM) {
                $preloop["<<FORWARD_MATCH>>"] = "";
                $preloop["<<FORWARD_INCLUDE>>"] = "selected";
                $preloop["<<FORWARD_NOT_INC>>"] = "";
                $preloop["<<FORWARD_EMPTY>>"] = "";

            // ޤޤʤ
            } elseif ($filterarticle[$id][FROM_TERM] === NOT_INCLUDE_TERM) {
                $preloop["<<FORWARD_MATCH>>"] = "";
                $preloop["<<FORWARD_INCLUDE>>"] = "";
                $preloop["<<FORWARD_NOT_INC>>"] = "selected";
                $preloop["<<FORWARD_EMPTY>>"] = "";

            // Ǥ
            } elseif ($filterarticle[$id][FROM_TERM] === EMPTY_TERM) {
                $preloop["<<FORWARD_MATCH>>"] = "";
                $preloop["<<FORWARD_INCLUDE>>"] = "";
                $preloop["<<FORWARD_NOT_INC>>"] = "";
                $preloop["<<FORWARD_EMPTY>>"] = "selected";
            }

            // ̾
            // ̵ʳϥåܥåå
            if ($filterarticle[$id][SUBJECT_TERM] !== INVALID_TERM) {
                $preloop["<<SUBJECT_CHECK>>"] = "checked";
            }

            // ʸ󤬤ȿ
            if (isset($filterarticle[$id][SUBJECT_FLAG])) {
                $preloop["<<SUBJECT_TEXT>>"] =
                            htmlspecialchars($filterarticle[$id][SUBJECT_FLAG]);
            }

            // Υ쥯ȥܥåȿ
            // ޤ
            if ($filterarticle[$id][SUBJECT_TERM] === INCLUDE_TERM) {
                $preloop["<<SUBJECT_MATCH>>"] = "";
                $preloop["<<SUBJECT_INCLUDE>>"] = "selected";
                $preloop["<<SUBJECT_NOT_INC>>"] = "";
                $preloop["<<SUBJECT_EMPTY>>"] = "";

            // ޤޤʤ
            } elseif ($filterarticle[$id][SUBJECT_TERM] === NOT_INCLUDE_TERM) {
                $preloop["<<SUBJECT_MATCH>>"] = "";
                $preloop["<<SUBJECT_INCLUDE>>"] = "";
                $preloop["<<SUBJECT_NOT_INC>>"] = "selected";
                $preloop["<<SUBJECT_EMPTY>>"] = "";

            // Ǥ
            } elseif ($filterarticle[$id][SUBJECT_TERM] === EMPTY_TERM) {
                $preloop["<<SUBJECT_MATCH>>"] = "";
                $preloop["<<SUBJECT_INCLUDE>>"] = "";
                $preloop["<<SUBJECT_NOT_INC>>"] = "";
                $preloop["<<SUBJECT_EMPTY>>"] = "selected";
            }

            // 
            // ̵ʳϥåܥåå
            if ($filterarticle[$id][RECIPIENT_TERM] !== INVALID_TERM) {
                $preloop["<<RECIPT_CHECK>>"] = "checked";
            } else {
                $preloop["<<RECIPT_CHECK>>"] = "";
            }

            // ʸ󤬤ȿ
            if (isset($filterarticle[$id][RECIPIENT_FLAG])) {
                $preloop["<<RECIPT_TEXT>>"] =
                          htmlspecialchars($filterarticle[$id][RECIPIENT_FLAG]);
            } else {
                $preloop["<<RECIPT_TEXT>>"] = "";
            }

            // Υ쥯ȥܥåȿ
            // ޤ
            if ($filterarticle[$id][RECIPIENT_TERM] === INCLUDE_TERM) {
                $preloop["<<RECIPT_MATCH>>"] = "";
                $preloop["<<RECIPT_INCLUDE>>"] = "selected";
                $preloop["<<RECIPT_NOT_INC>>"] = "";
                $preloop["<<RECIPT_EMPTY>>"] = "";

            // ޤޤʤ
            } elseif ($filterarticle[$id][RECIPIENT_TERM] === 
                                                            NOT_INCLUDE_TERM) {
                $preloop["<<RECIPT_MATCH>>"] = "";
                $preloop["<<RECIPT_INCLUDE>>"] = "";
                $preloop["<<RECIPT_NOT_INC>>"] = "selected";
                $preloop["<<RECIPT_EMPTY>>"] = "";

            // Ǥ
            } elseif ($filterarticle[$id][RECIPIENT_TERM] === EMPTY_TERM) {
                $preloop["<<RECIPT_MATCH>>"] = "";
                $preloop["<<RECIPT_INCLUDE>>"] = "";
                $preloop["<<RECIPT_NOT_INC>>"] = "";
                $preloop["<<RECIPT_EMPTY>>"] = "selected";
            }

            // ž衢᡼ν
            $preloop["<<TRANSFER_ADDR>>"] = "";
            if (isset($filterarticle[$id][TRANS_ADDR])) {
                $preloop["<<TRANSFER_ADDR>>"] =
                              htmlspecialchars($filterarticle[$id][TRANS_ADDR]);
            }

            // Ф˻Ĥ/Ĥʤ
            $preloop["<<MAIL_LEAVE>>"] = "selected";
            $preloop["<<MAIL_DEL>>"] = "";
            if ($filterarticle[$id][RECEIVED_MAIL] === RECEIVED_OFF) {
                $preloop["<<MAIL_LEAVE>>"] = "";
                $preloop["<<MAIL_DEL>>"] = "selected";
            }
        }

        array_push($subloop, $preloop);
    }

    return TRUE;
}

/*********************************************************
 * check_forward_data
 *
 * ᡼žեηå
 *
 * []
 *         $post     post줿
 *         &$attrs   °Ǽ
 * [֤]
 *         TRUE      
 *         FALSE     ۾
 *
 **********************************************************/
function check_forward_data($post, &$attrs)
{
    global $msgarr;
    global $err_msg;
    global $ldapdata;
    global $web_conf;
    global $url_data;

    // ۾ｪλե饰
    $break = 0;

    $tmp_article = array();
    $tmp_order = "";
    $inval_id = array();
    // idο֤
    foreach ($post['id'] as $id) {
        // ͤΥå
        // sender
        if (isset($post["sender".$id]) && $post["sender".$id] !== "") {
            $ret = check_forward_length($post["sender".$id]);
            if ($ret === FALSE) {
                $err_msg = $msgarr['25001'][SCREEN_MSG];
                $break = SENDER_ERR;
                break;
            }
        }

        // subject
        if (isset($post["subject".$id]) && $post["subject".$id] !== "") {
            $ret = check_forward_length($post["subject".$id]);
            if ($ret === FALSE) {
                $err_msg = $msgarr['25002'][SCREEN_MSG];
                $break = SUBJECT_ERR;
                break;
            }
        }

        // recipient
        if (isset($post["recipient".$id]) && $post["recipient".$id] !== "") {
            $ret = check_forward_length($post["recipient".$id]);
            if ($ret === FALSE) {
                $err_msg = $msgarr['25003'][SCREEN_MSG];
                $break = RECIPIENT_ERR;
                break;
            }
        }

        // forward address
        if (isset($post["forward_address".$id]) &&
                  $post["forward_address".$id] !== "") {
            $ret = check_mail_forward($post["forward_address".$id]);
            if ($ret === FALSE) {
                $break = FORWARD_ERR;
                break;
            }
            $tmp_order .= "+" . $id . ":";

        // ž褬̵ϿʤΤǼΥؿʤ
        } else {
            $tmp_order .= "-" . $id . ":";
            $inval_id[] = $id;
        }

        // article
        $article = "";
        $ret = make_save_article($post, $id, $article);
        if ($ret === FALSE) {
            $break = ARTICLE_ERR;
            break;
        }
        // ƥ¸Ƥ
        $tmp_article[$id] = $article;
    }

    // ͤ顼äϥ顼
    if ($break !== 0) {
        return FALSE;
    }

    // Ǹפ":"
    $tmp_order = substr($tmp_order, 0, -1);

    // ϿĿϥĶƤϥ顼
    if ($web_conf[$url_data['script']]['forwardnum'] < count($tmp_article)) {
        // ̵ǤƤ
        foreach ($inval_id as $delete_id) {
            // article
            unset($tmp_article[$delete_id]);
            // 
            $tmp_order = str_replace(":-".$delete_id, "", $tmp_order);
        }

        // ϿǧǤĶƤХ顼
        if ($web_conf[$url_data['script']]['forwardnum'] < count($tmp_article)) {
            $err_msg = $msgarr['25006'][SCREEN_MSG];
            return FALSE;
        }
    }

    // Ͽǡ
    $attrs["mailFilterArticle"] = array_merge($tmp_article);
    //$attrs["mailFilterOrder"][0] = $tmp_order;
    $attrs["mailFilterOrder"][0] = $tmp_order;

    // web.confForwardConfONmailForwardingAddr¸ߤϺ
    if ($web_conf[$url_data['script']]['forwardconf'] === FORWARD_ON &&
        isset($ldapdata[0]["mailForwardingAddr"])) {
        $attrs["mailForwardingAddr"] = array();
    }

    return TRUE;
}

/*********************************************************
 * make_save_article()
 *
 * ƥؿ
 *
 * []
 *      $post        ݥȥǡ
 *      $id          оID
 *      &$article    article
 * [֤]
 *      TRUE         
 *      FALSE        ۾
 **********************************************************/
function make_save_article($post, $id, &$article)
{
    // Ƭid
    $article = $id . ":";

    // ԥ롼
    // ξ
    if ($post["sender".$id] !== "") {
        $article .= FROM_FLAG . strlen($post["sender".$id]) . ":" .
                    $post["sender".$id];
    }

    // Ԥν
    // ԥåܥåoffžξ̵ե饰
    if (! isset($post["sender_enable".$id]) ||
                $post["transfer_rule".$id] === INVALID_TERM) {
        $article .= FROM_TERM . INVALID_TERM;
    } else {
        // ФƤȿ
        // Ȱפ
        if ($post["sender_option".$id] === "0") {
            $article .= FROM_TERM . MATCH_TERM;
        } elseif ($post["sender_option".$id] === "1") {
            $article .= FROM_TERM . INCLUDE_TERM;
        } elseif ($post["sender_option".$id] === "2") {
            $article .= FROM_TERM . NOT_INCLUDE_TERM;
        } elseif ($post["sender_option".$id] === "3") {
            $article .= FROM_TERM . EMPTY_TERM;
        }
    }

    // ̾롼
    // ̾ξ
    if ($post["subject".$id] !== "") {
        $article .= SUBJECT_FLAG . strlen($post["subject".$id]) . ":" .
                    $post["subject".$id];
    }

    // ̾ν
    // ̾åܥåoffžξ̵ե饰
    if (! isset($post["subject_enable".$id]) ||
                $post["transfer_rule".$id] === INVALID_TERM) {
        $article .= SUBJECT_TERM . INVALID_TERM;
    } else {
        // ФƤȿ
        // Ȱפ
        if ($post["subject_option".$id] === "0") {
            $article .= SUBJECT_TERM . MATCH_TERM;
        } elseif ($post["subject_option".$id] === "1") {
            $article .= SUBJECT_TERM . INCLUDE_TERM;
        } elseif ($post["subject_option".$id] === "2") {
            $article .= SUBJECT_TERM . NOT_INCLUDE_TERM;
        } elseif ($post["subject_option".$id] === "3") {
            $article .= SUBJECT_TERM . EMPTY_TERM;
        }
    }

    // 롼
    // ξ
    if ($post["recipient".$id] !== "") {
        $article .= RECIPIENT_FLAG . strlen($post["recipient".$id]) . ":" .
                    $post["recipient".$id];
    }

    // ν
    // åܥåoffžξ̵ե饰
    if (! isset($post["recipient_enable".$id]) ||
                $post["transfer_rule".$id] === INVALID_TERM) {
        $article .= RECIPIENT_TERM . INVALID_TERM;
    } else {
        // ФƤȿ
        // Ȱפ
        if ($post["recipient_option".$id] === "0") {
            $article .= RECIPIENT_TERM . MATCH_TERM;
        } elseif ($post["recipient_option".$id] === "1") {
            $article .= RECIPIENT_TERM . INCLUDE_TERM;
        } elseif ($post["recipient_option".$id] === "2") {
            $article .= RECIPIENT_TERM . NOT_INCLUDE_TERM;
        } elseif ($post["recipient_option".$id] === "3") {
            $article .= RECIPIENT_TERM . EMPTY_TERM;
        }
    }

    // ž
    $article .= TRANS_ADDR . strlen($post["forward_address".$id]) . ":" .
                $post["forward_address".$id];

    // Ĥ/Ĥʤե饰
    $article .= RECEIVED_MAIL . $post["save".$id];

    // base64󥳡ɽ
    $article = base64_encode($article);
    if ($article === FALSE) {
        return FALSE;
    }

    return TRUE;
}

/*********************************************************
 * keep_post_forward_data()
 *
 * ᡼žݻؿ
 *
 * []
 *      $post        ݥȥǡ
 *      &$loop       loop
 *      $del_flag    deleteե饰
 * [֤]
 *      ̵
 **********************************************************/
function keep_post_forward_data($post, &$loop, $del_flag)
{
    global $web_conf;
    global $url_data;

    // 줿IDο֤
    foreach ($post['id'] as $id) {
        // filter id
        $tag_loop["<<FILTER_ID>>"] = $id;

        // 饸ܥ
        $tag_loop["<<ALL_FORWARD>>"] = "checked";
        $tag_loop["<<DETAIL_FORWARD>>"] = "";
        // ܥ󤬲ƤʤȤPOSTͤݻ
        if ($post["transfer_rule".$id] === "1" && $_POST['filterId'] !== $id) {
            $tag_loop["<<ALL_FORWARD>>"] = "";
            $tag_loop["<<DETAIL_FORWARD>>"] = "checked";
        }

        // 
        // åܥå
        // ܥ󤬲ƤʤȤPOSTͤݻ
        $tag_loop["<<FORWARD_CHECK>>"] = "";
        if (isset($post["sender_enable".$id]) && $_POST['filterId'] !== $id) {
            $tag_loop["<<FORWARD_CHECK>>"] = "checked";
        }

        // 
        // ܥ󤬲ƤʤȤPOSTͤݻ
        $tag_loop["<<FORWARD_TEXT>>"] = "";
        if ($_POST['filterId'] !== $id) {
            $tag_loop["<<FORWARD_TEXT>>"] =
                                         htmlspecialchars($post["sender".$id]);
        }

        // Խ
        $tag_loop["<<FORWARD_MATCH>>"] = "selected";
        $tag_loop["<<FORWARD_INCLUDE>>"] = "";
        $tag_loop["<<FORWARD_NOT_INC>>"] = "";
        $tag_loop["<<FORWARD_EMPTY>>"] = "";

        // Դޤ
        // ܥ󤬲ƤʤȤPOSTͤݻ
        if ($post["sender_option".$id] === INCLUDE_INPUT &&
                                                 $_POST['filterId'] !== $id) {
            $tag_loop["<<FORWARD_MATCH>>"] = "";
            $tag_loop["<<FORWARD_INCLUDE>>"] = "selected";

        // Դޤޤʤ
        // ܥ󤬲ƤʤȤPOSTͤݻ
        } elseif ($post["sender_option".$id] === NOT_INCLUDE_INPUT &&
                                                 $_POST['filterId'] !== $id) {
            $tag_loop["<<FORWARD_MATCH>>"] = "";
            $tag_loop["<<FORWARD_NOT_INC>>"] = "selected";

        // ԶǤ
        // ܥ󤬲ƤʤȤPOSTͤݻ
        } elseif ($post["sender_option".$id] === EMPTY_INPUT &&
                                                 $_POST['filterId'] !== $id) {
            $tag_loop["<<FORWARD_MATCH>>"] = "";
            $tag_loop["<<FORWARD_EMPTY>>"] = "selected";
        }

        // ̾
        // ̾åܥå
        // ܥ󤬲ƤʤȤPOSTͤݻ
        $tag_loop["<<SUBJECT_CHECK>>"] = "";
        if (isset($post["subject_enable".$id]) && $_POST['filterId'] !== $id) {
            $tag_loop["<<SUBJECT_CHECK>>"] = "checked";
        }

        // ̾
        // ܥ󤬲ƤʤȤPOSTͤݻ
        $tag_loop["<<SUBJECT_TEXT>>"] = "";
        if ($_POST['filterId'] !== $id) {
            $tag_loop["<<SUBJECT_TEXT>>"] =
                                        htmlspecialchars($post["subject".$id]);
        }

        // ̾
        // ܥ󤬲ƤʤȤPOSTͤݻ
        $tag_loop["<<SUBJECT_MATCH>>"] = "selected";
        $tag_loop["<<SUBJECT_INCLUDE>>"] = "";
        $tag_loop["<<SUBJECT_NOT_INC>>"] = "";
        $tag_loop["<<SUBJECT_EMPTY>>"] = "";

        // ޤब򤵤Ƥ
        // ܥ󤬲ƤʤȤPOSTͤݻ
        if ($post["subject_option".$id] === INCLUDE_INPUT &&
                                                 $_POST['filterId'] !== $id) {
            $tag_loop["<<SUBJECT_MATCH>>"] = "";
            $tag_loop["<<SUBJECT_INCLUDE>>"] = "selected";

        // ޤޤʤ򤵤Ƥ
        // ܥ󤬲ƤʤȤPOSTͤݻ
        } elseif ($post["subject_option".$id] === NOT_INCLUDE_INPUT &&
                                                 $_POST['filterId'] !== $id) {
            $tag_loop["<<SUBJECT_MATCH>>"] = "";
            $tag_loop["<<SUBJECT_NOT_INC>>"] = "selected";

        // Ǥ뤬򤵤Ƥ
        // ܥ󤬲ƤʤȤPOSTͤݻ
        } elseif ($post["subject_option".$id] === EMPTY_INPUT &&
                                                 $_POST['filterId'] !== $id) {
            $tag_loop["<<SUBJECT_MATCH>>"] = "";
            $tag_loop["<<SUBJECT_EMPTY>>"] = "selected";
        }

        // 
        // åܥå
        // ܥ󤬲ƤʤȤPOSTͤݻ
        $tag_loop["<<RECIPT_CHECK>>"] = "";
        if (isset($post["recipient_enable".$id]) &&
                                                 $_POST['filterId'] !== $id) {
            $tag_loop["<<RECIPT_CHECK>>"] = "checked";
        }

        // 
        // ܥ󤬲ƤʤȤPOSTͤݻ
        $tag_loop["<<RECIPT_TEXT>>"] = "";
        if ($_POST['filterId'] !== $id) {
            $tag_loop["<<RECIPT_TEXT>>"] =
                                      htmlspecialchars($post["recipient".$id]);
        }

        // 
        // ܥ󤬲ƤʤȤPOSTͤݻ
        $tag_loop["<<RECIPT_MATCH>>"] = "selected";
        $tag_loop["<<RECIPT_INCLUDE>>"] = "";
        $tag_loop["<<RECIPT_NOT_INC>>"] = "";
        $tag_loop["<<RECIPT_EMPTY>>"] = "";

        // ޤξ
        // ܥ󤬲ƤʤȤPOSTͤݻ
        if ($post["recipient_option".$id] === INCLUDE_INPUT &&
                                                 $_POST['filterId'] !== $id) {
            $tag_loop["<<RECIPT_MATCH>>"] = "";
            $tag_loop["<<RECIPT_INCLUDE>>"] = "selected";

        // ޤޤʤξ
        // ܥ󤬲ƤʤȤPOSTͤݻ
        } elseif ($post["recipient_option".$id] === NOT_INCLUDE_INPUT &&
                                                 $_POST['filterId'] !== $id) {
            $tag_loop["<<RECIPT_MATCH>>"] = "";
            $tag_loop["<<RECIPT_NOT_INC>>"] = "selected";

        // Ǥξ
        // ܥ󤬲ƤʤȤPOSTͤݻ
        } elseif ($post["recipient_option".$id] === EMPTY_INPUT &&
                                                 $_POST['filterId'] !== $id) {
            $tag_loop["<<RECIPT_MATCH>>"] = "";
            $tag_loop["<<RECIPT_EMPTY>>"] = "selected";
        }

        // ž
        // ܥ󤬲ƤʤȤPOSTͤݻ
        $tag_loop["<<TRANSFER_ADDR>>"] = "";
        if ($_POST['filterId'] !== $id) {
            $tag_loop["<<TRANSFER_ADDR>>"] =
                                htmlspecialchars($post["forward_address".$id]);
        }

        // Ĥ/Ĥʤ
        // ܥ󤬲ƤʤȤPOSTͤݻ
        $tag_loop["<<MAIL_LEAVE>>"] = "selected";
        $tag_loop["<<MAIL_DEL>>"] = "";
        if ($post["save".$id] === "1" && $_POST['filterId'] !== $id) {
            $tag_loop["<<MAIL_LEAVE>>"] = "";
            $tag_loop["<<MAIL_DEL>>"] = "selected";
        }

        array_push($loop, $tag_loop);
    }
    // 롼ץɽlineʤä
    // 碌ʺξϹԤʤʤ
    if ($del_flag === "") {
        $max_num = $web_conf[$url_data['script']]['forwardnum'];
        $data_num = count($loop);
        for ($max_num; $max_num < $data_num; $max_num++) {
            // ɽĴ
            array_pop($loop);
        }
    }

    return;
}

/*********************************************************
 * delete_forward_input_value()
 *
 * ᡼žݻؿ
 *
 * []
 *      $id     id
 *      &$pre   
 * [֤]
 *      ̵
 **********************************************************/
function delete_forward_input_value($id, &$pre)
{

    // ֤˽񤭴
    $pre["f"] = "0";
    $pre["s"] = "0";
    $pre["t"] = "0";

    return;
}

/*********************************************************
 * convert_forward_value()
 *
 * ᡼žݻؿ
 *
 * []
 *      $id      id
 *      &$loop   loop
 * [֤]
 *      ̵
 **********************************************************/
function convert_forward_value($id, &$loop)
{
    global $ldapdata;

    // filter id
    $tag_loop["<<FILTER_ID>>"] = $id;


    // ž饸
    $tag_loop["<<ALL_FORWARD>>"] = "checked";
    // ܺ饸
    $tag_loop["<<DETAIL_FORWARD>>"] = "";
    // 
    $tag_loop["<<FORWARD_CHECK>>"] = "";
    $tag_loop["<<FORWARD_TEXT>>"] = "";
    $tag_loop["<<FORWARD_MATCH>>"] = "selected";
    $tag_loop["<<FORWARD_INCLUDE>>"] = "";
    $tag_loop["<<FORWARD_NOT_INC>>"] = "";
    $tag_loop["<<FORWARD_EMPTY>>"] = "";
    // ̾
    $tag_loop["<<SUBJECT_CHECK>>"] = "";
    $tag_loop["<<SUBJECT_TEXT>>"] = "";
    $tag_loop["<<SUBJECT_MATCH>>"] = "selected";
    $tag_loop["<<SUBJECT_INCLUDE>>"] = "";
    $tag_loop["<<SUBJECT_NOT_INC>>"] = "";
    $tag_loop["<<SUBJECT_EMPTY>>"] = "";
    // 
    $tag_loop["<<RECIPT_CHECK>>"] = "";
    $tag_loop["<<RECIPT_TEXT>>"] = "";
    $tag_loop["<<RECIPT_MATCH>>"] = "selected";
    $tag_loop["<<RECIPT_INCLUDE>>"] = "";
    $tag_loop["<<RECIPT_NOT_INC>>"] = "";
    $tag_loop["<<RECIPT_EMPTY>>"] = "";

    // ž, Ĥ/Ĥʤ
    $tag_loop["<<TRANSFER_ADDR>>"] = "";
    $tag_loop["<<MAIL_LEAVE>>"] = "";
    $tag_loop["<<MAIL_DEL>>"] = "selected";
    foreach ($ldapdata[0]['mailForwardingAddr'] as $mailaddr) {
        // ldapϿƤ᡼륢ɥ쥹Ȱۤʤʪ
        if ($mailaddr !== $ldapdata[0]['mail'][0]) {
            $tag_loop["<<TRANSFER_ADDR>>"] = $mailaddr;

        // ᡼륢ɥ쥹פϻĤե饰ΩƤ
        } else {
            $tag_loop["<<MAIL_LEAVE>>"] = "selected";
            $tag_loop["<<MAIL_DEL>>"] = "";
        }
    }

    array_push($loop, $tag_loop);

    return;
}

/*********************************************************
 * mod_user
 *
 * 桼ν
 *
 * []
 *          $data  桼γǼ줿Ϣ
 * [֤]
 *          TRUE   
 *          FALSE  顼
 *
 **********************************************************/
function mod_user_forward($data)
{
    global $web_conf;
    global $url_data;
    global $ldapdata;
    global $userdn;

    // LDAPǡι
    //$ret = LDAP_mod_entry($userdn, $attrs);
    $ret = LDAP_mod_entry($userdn, $data);
    if ($ret !== LDAP_OK && $ret != LDAP_ERR_NOATTR) {
        return FALSE;
    }

    return TRUE;
}

/*********************************************************
 * check_csv_order
 *
 * csvեѤΥåؿ
 * 顼åϸƤӽФǥåȤ
 *
 * []
 *          $order  ᡼ե륿
 * [֤]
 *          TRUE   
 *          FALSE  顼
 *
 **********************************************************/
function check_csv_order($order)
{

    // ʸĹå
    if (strlen($order) > ORDER_CSV_LENGTH) {
        return FALSE;
    }

    // ʸåʿ+-:ʳޤޤƤХ顼
    $num = "0123456789";
    $sym = "+-:";
    $allow_letter = $num . $sym;

    if (strspn($order, $allow_letter) !== strlen($order)) {
        return FALSE;
    }

    return TRUE;
}

/*********************************************************
 * check_csv_article
 *
 * csvեѤΥƥåؿ
 * 顼åϸƤӽФǥåȤ
 *
 * []
 *          $article  ᡼ե륿
 * [֤]
 *          TRUE   
 *          FALSE  顼
 *
 **********************************************************/
function check_csv_article($article)
{

    // ":"ʬ
    $exp_art = explode(":", $article);

    // ǿʸå򷫤֤
    foreach ($exp_art as $piece_art) {
        // ʸĹå
        if (strlen($piece_art) > ARTICLE_CSV_LENGTH) {
            return FALSE;
        }

        // base64ǥɤԤХ顼
        $ret = base64_decode($piece_art, TRUE);
        if ($ret === FALSE) {
            return FALSE;
        }
    }

    return TRUE;
}

?>
