<?php


/* ᡼륢ɥ쥹Ĺ */
define("MAXMAIL", 256);

/***********************************************************
 * read_global
 *
 * keepalived.confեɤ߹
 *
 * []
 *        $filename     keepalived.confХѥ
 *        &$data        ǤǼ
 * [֤]
 *        TRUE       
 *        FALSE      顼
 *
 **********************************************************/
function read_global($filename, &$data)
{

    global $msgarr;
    global $err_msg;
    global $log_msg;
    global $web_conf;

    $fh = fopen($filename, "r");
    if ($fh === FALSE) {
        $err_msg = sprintf($msgarr['28045'][SCREEN_MSG], $filename);
        $log_msg = sprintf($msgarr['28045'][LOG_MSG], $filename);
        return FALSE;
    }
        
    while($result = fgets($fh)) {
        $result = trim($result);
        if ($result === "global_defs {") {
            break;
        } 
    }

    if ($result === FALSE) {
        $err_msg = sprintf($msgarr['28053'][SCREEN_MSG], $filename);
        $log_msg = sprintf($msgarr['28053'][LOG_MSG], $filename);
        return FALSE;
    }

    $mail_flag = 0;
    $key = 0;
    while ($result = fgets($fh)) {
        $result = trim($result);
        $lines = preg_split("[\s+]", $result, 2);
        if ($mail_flag === 1) {
            if ($lines[0] === "}") {
                $mail_flag = 0;
                continue;
            }
            $data["notification_email"][$key] = $result;
            $key = $key + 1;
        } else { 

            /* notification_emailμ */
            if ($lines[0] === "notification_email") {
                $mail_flag = 1;
                continue;
            } 
            if ($lines[0] === "}") {
                break;
            }
 
            /* notification_email_fromμ*/
            if ($lines[0] === "notification_email_from") {
                $data["notification_email_from"] = $lines[1];
                continue;
            }

            /* smtp_serverμ*/
            if ($lines[0] === "smtp_server") {
                $data["smtp_server"] = $lines[1];
                continue;
            }

            /* smtp_connect_timeoutμ*/
            if ($lines[0] === "smtp_connect_timeout") {
                $data["smtp_connect_timeout"] = $lines[1];
                continue;
            }
        }
    }

    if ($result === FALSE) {
        $err_msg = sprintf($msgarr['28053'][SCREEN_MSG], $filename);
        $log_msg = sprintf($msgarr['28053'][LOG_MSG], $filename);
        return FALSE;
    }

    return TRUE;
}

/***********************************************************
 * write_global
 *
 * keepalived.confؤν񤭹
 *
 * []
 *         $filename      keepalived.confХѥ
 *         $data          Ϥ줿ǤǼ
 *         $tmplfilename  keepalived.conf.tmplХѥ
 * [֤]
 *         0       
 *         1       
 *         2       ƥ२顼
 **********************************************************/
function write_global($filename, $data, $tmplfilename)
{
    global $msgarr;
    global $err_msg;
    global $log_msg;
    global $web_conf;
    $dirname = dirname($filename);
    $tmpfile = basename($filename);

    /* ե뤬񤭹߲ǽĴ٤ */
    if (!is_writable($filename)) {
       return 1;
    }

    /* conf.tmpl */
    $conftmpl = file_get_contents($tmplfilename);
    if ($conftmpl === FALSE) {
        return 2;
    }
    $data["notification_email"] = implode("\n", $data["notification_email"]);

    /* tagִ */
    $tag["<<NOTITICATION_EMAIL>>"] = "notification_email {\n$data[notification_email]\n}";
    $tag["<<NOTITICATION_EMAIL_FROM>>"] = "notification_email_from $data[notification_email_from]";
    $tag["<<SMTP_SERVER>>"] = "smtp_server $data[smtp_server]";
    $tag["<<SMTP_CONNECT_TIMEOUT>>"] = "smtp_connect_timeout $data[smtp_connect_timeout]";

    $conftemp = change_template_tag($conftmpl, $tag);

    $tmpfname = tempnam($dirname, $tmpfile);
    if ($tmpfname === FALSE) {
        return 1;
    }

    $fh = fopen($tmpfname, "w");
    if ($fh === FALSE) {
        return 1;
    }
        
    $ret = fwrite($fh, $conftemp);
    if ($ret === FALSE) {
        return 1;
    }
    fclose($fh);

    $ret = rename($tmpfname, $filename);
    if ($ret === FALSE) {
        return 1;
    }
    return 0;
}


/*********************************************************
 * check_mail()
 *
 * ᡼륢ɥ쥹Υå
 *
 * []
 *      $mail        ᡼륢ɥ쥹
 * [֤]
 *      TRUE         
 *      FALSE        ۾
 **********************************************************/
function check_mail($mail)
{
    global $msgarr;
    global $err_msg;
    global $log_msg;
    global $web_conf;

    /* ᡼륢ɥ쥹Ĺå */
    if (strlen($mail) > MAXMAIL) {
        $err_msg = $msgarr['01018'][SCREEN_MSG];
        $log_msg = $msgarr['01018'][LOG_MSG];
        return FALSE;
    }

    /* @Ĥ˶ڤ뤫Υå */
    $buf = explode('@', $mail, 2);
    if (count($buf) != 2 || $buf[0] == "" || $buf[1] == "") {
        $err_msg = $msgarr['01018'][SCREEN_MSG];
        $log_msg = $msgarr['01018'][LOG_MSG];
    }

    /* @Υå */
    /* ȾѱѾʸʲεΤߵ */
    $num = "0123456789";
    $sl = "abcdefghijklmnopqrstuvwxyz";
    $ll = strtoupper($sl);
    $sym = "!#$%&'*+-/=?^_{}~.";
    $allow_letter = $num . $sl . $ll . $sym;
    if (strspn($buf[0], $allow_letter) != strlen($buf[0])) {
        $err_msg = $msgarr['01018'][SCREEN_MSG];
        $log_msg = $msgarr['01018'][LOG_MSG];
        return FALSE;
    }

    /* web.confLocalPartRFCCheck1ξ */
    if (isset($web_conf["iluka"]["localpartrfccheck"]) === TRUE &&
              $web_conf["iluka"]["localpartrfccheck"] === "1") {
        /* ѡȤηå */
        if (check_localpart($buf[0]) === FALSE) {
            $err_msg = $msgarr['01018'][SCREEN_MSG];
            $log_msg = $msgarr['01018'][LOG_MSG];
            return FALSE;
        }
    }

    /*  @Υå */
    if (strlen($buf[1]) < 3) {
        $err_msg = $msgarr['01018'][SCREEN_MSG];
        $log_msg = $msgarr['01018'][LOG_MSG];
        return FALSE;
    }

    $sym = "-_.";
    $must = ".";
    $must_not = "..";

    /* ɥåȤϤޤХ顼 */
    if (substr($buf[1], 0, 1) == $must) {
        $err_msg = $msgarr['01018'][SCREEN_MSG];
        $log_msg = $msgarr['01018'][LOG_MSG];
        return FALSE;
    }

    /* 1İʾΥɥåȤɬܡ */
    if (strpos($buf[1], $must) === FALSE) {
        $err_msg = $msgarr['01018'][SCREEN_MSG];
        $log_msg = $msgarr['01018'][LOG_MSG];
        return FALSE;
    }

    /* 2İʾΥɥåȤϢ³϶ػߡ */
    if (strpos($buf[1], $must_not) !== FALSE) {
        $err_msg = $msgarr['01018'][SCREEN_MSG];
        $log_msg = $msgarr['01018'][LOG_MSG];
        return FALSE;
    }

    $allow_letter = $num . $sl . $ll . $sym;
    $length = strlen($buf[1]);
    if (strspn($buf[1], $allow_letter) != $length) {
        $err_msg = $msgarr['01018'][SCREEN_MSG];
        $log_msg = $msgarr['01018'][LOG_MSG];
        return FALSE;
    }

    return TRUE;
}

/*********************************************************
 * check_localpart()
 *
 * ᡼륢ɥ쥹Υѡȥå
 *
 * []
 *      $local       ᡼륢ɥ쥹
 * [֤]
 *      TRUE         
 *      FALSE        ۾
 **********************************************************/
function check_localpart($local)
{
    /* ʸ */
    $dot_atom = ".";
    $dot_continuous = "..";

    /* Ƭ.Ϸ顼 */
    if (substr($local, 0, 1) === $dot_atom) {
        return FALSE;
    }

    /* .Ϸ顼 */
    if (substr($local, -1, 1) === $dot_atom) {
        return FALSE;
    }

    /* .Ϣ³Ϸ顼 */
    if (strpos($local, $dot_continuous)) {
        return FALSE;
    }

    return TRUE;
}

/**********************************************************
 * read_vslist
 *
 * virtual_server.confɤ߹
 *
 * []
 *        $file            virtual_server.confΥѥ
 *        &$data           С륵Фξ
 * [֤]
 *        TRUE             
 *        FALSE            顼
 *
 **********************************************************/
function read_vslist($file, &$data)
{
    global $msgarr;
    global $err_msg;
    global $log_msg;

    $fh = fopen($file, "r");
    if ($fh === FALSE) {
        $err_msg = sprintf($msgarr['28055'][SCREEN_MSG], $file);
        $log_msg = sprintf($msgarr['28055'][LOG_MSG], $file);
        return FALSE;
    }

    $key = 0;
    while ($result = fgets($fh)) {
        $result = trim($result);
        $lines = explode("/", $result, 2);
        if ($lines[0] === $result) {
            $err_msg = sprintf($msgarr['28060'][SCREEN_MSG], $file);
            $log_msg = sprintf($msgarr['28060'][LOG_MSG], $file);
            fclose($fh);
            return FALSE;
        }

        /* å */
        if ($lines[0] !== 'include virtual_server' && $lines[0] !== '#include virtual_server') {
            $err_msg = sprintf($msgarr['28060'][SCREEN_MSG], $file);
            $log_msg = sprintf($msgarr['28060'][LOG_MSG], $file);
            fclose($fh);
            return FALSE;
        }
            
        /* disable:enable */
        $lines_able = strncmp('#', $lines[0], 1);
        if ($lines_able === 0) {
            $data[$key]["able"] = "disable";
        } else {
            $data[$key]["able"] = "enable";
        }

        /* IPɥ쥹 */
        $lines_ip = explode("_", $lines[1], 2);
        if ($lines_ip[0] === $lines[1]) {
            $err_msg = sprintf($msgarr['28060'][SCREEN_MSG], $file);
            $log_msg = sprintf($msgarr['28060'][LOG_MSG], $file);
            fclose($fh);
            return FALSE;
        }
        if (!filter_var($lines_ip[0], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6)) {
            $err_msg = sprintf($msgarr['28060'][SCREEN_MSG], $file);
            $log_msg = sprintf($msgarr['28060'][LOG_MSG], $file);
            fclose($fh);
            return FALSE;
        }
        $data[$key]["ipaddress"] = $lines_ip[0];
        

         /* ݡֹ */
        $lines_port = explode(".", $lines_ip[1], 2);
        if ($lines_port[0] === $lines_ip[1]) {
            $err_msg = sprintf($msgarr['28060'][SCREEN_MSG], $file);
            $log_msg = sprintf($msgarr['28060'][LOG_MSG], $file);
            fclose($fh);
            return FALSE;
        }
        $ret = ctype_digit($lines_port[0]);
        if ($ret === FALSE) {
            $err_msg = sprintf($msgarr['28060'][SCREEN_MSG], $file);
            $log_msg = sprintf($msgarr['28060'][LOG_MSG], $file);
            fclose($fh);
            return FALSE;
        }
        $data[$key]["port"] = $lines_port[0];

        $key++;
    }
    fclose($fh);
    return TRUE;
}


/**********************************************************
 * write_vslist
 *
 * virtual_server.confν񤭹
 *
 * []
 *        $file           virtual_server.confΥѥ
 *        &$data           virtual_server.conf
 * [֤]
 *        TRUE             
 *        FALSE             
 **********************************************************/

function write_vslist ($file, &$data)
{
    global $msgarr;
    global $err_msg;
    global $log_msg;
    global $web_conf;
    $dirname = dirname($file);
    $vsfilename = basename($file);
    $write_data = "";

    /* 񤭹߲ǽγǧ */
    if (!is_writable($file)) {
        $err_msg = sprintf($msgarr['28059'][SCREEN_MSG], $file);
        $log_msg = sprintf($msgarr['28059'][LOG_MSG], $file);
        return FALSE;
    }

    /* 񤭹ѥǡκ */
    $cht = count($data);
    for ($i = 0; $i < $cht; $i++) {
        if ($data[$i]["able"] === 'disable') {
            $write_data .= "#";
        }
        $write_data .= "include virtual_server/".$data[$i]["ipaddress"]."_".$data[$i]["port"].".conf\n";
    }

    /* ե˽񤭹 */
    $tmpfname = tempnam($dirname, $vsfilename);
    if ($tmpfname === FALSE) {
        $err_msg = sprintf($msgarr['28059'][SCREEN_MSG], $file);
        $log_msg = sprintf($msgarr['28059'][LOG_MSG], $file);
        return FALSE;
    }

    $fh = fopen($tmpfname, "w");
    if ($fh === FALSE) {
        $err_msg = sprintf($msgarr['28059'][SCREEN_MSG], $file);
        $log_msg = sprintf($msgarr['28059'][LOG_MSG], $file);
        return FALSE;
    }

    $ret = fwrite($fh, $write_data);
    if ($ret === FALSE) {
        $err_msg = sprintf($msgarr['28059'][SCREEN_MSG], $file);
        $log_msg = sprintf($msgarr['28059'][LOG_MSG], $file);
        fclose($fh);
        return FALSE;
    }
    fclose($fh);

    /* ե֤ */
    $ret = rename($tmpfname, $file);
    if ($ret === FALSE) {
        $err_msg = sprintf($msgarr['28059'][SCREEN_MSG], $file);
        $log_msg = sprintf($msgarr['28059'][LOG_MSG], $file);
        return FALSE;
    }
    return TRUE;
}

/***********************************************************
 * reload_status
 *
 * ɤ뤫ݤη
 *
 * []
 *        $filename           virtual_server.confΥѥ
 *        $iport              оݹԤIP_PORT
 * [֤]
 *        $tag                ݻ줿
 **********************************************************/
function reload_status ($filename, $iport)
{
    global $msgarr;
    global $err_msg;
    global $log_msg;
    global $web_conf;

    $result = read_vslist($filename, $vsconfdata);
    if ($result === FALSE) {
        return FALSE;
    }

    $cht = count($vsconfdata);

    for ($i = 0; $i < $cht; $i++) {
        $check = $vsconfdata[$i]["ipaddress"]."_".$vsconfdata[$i]["port"];
        if ($check == $iport) {
            $status = $vsconfdata[$i]["able"];
            break;
        }
    }

    if ($i === $cht){
        $err_msg = sprintf($msgarr['28060'][SCREEN_MSG], $filename);
        return FALSE;
    }

    /* оݹԤstatusͭʤХ */
    if ($status === "enable") {
        $result = reload_keepalived();
        if ($result === FALSE)  {
            result_log(OPERATION . ":NG:" . $log_msg, LOG_ERR);
            syserr_display();
            exit(0);
        }
    }

    return TRUE;
}

/**********************************************************
 * reload_keepalived
 *
 * keepalivedΥ
 *
 * []
 *        ̵
 * [֤]
 *        TRUE             
 *        FALSE            
 **********************************************************/
function reload_keepalived ()
{
    global $msgarr;
    global $err_msg;
    global $log_msg;
    global $web_conf;

    system($web_conf["iluka"]["keepalivedreloadscript"], $result);
    if ($result !== 0) {
        $err_msg = $msgarr['28051'][SCREEN_MSG];
        $log_msg = $msgarr['28051'][LOG_MSG];
        return FALSE;
    }
    return TRUE;
}


/**********************************************************
 * lock_file
 *
 * եΥå
 *
 * []
 *        ʤ 
 * [֤]
 *        FALSE            
 **********************************************************/
function lock_file ()
{

    global $msgarr;
    global $err_msg;
    global $log_msg;
    global $web_conf;

    $file = $web_conf["iluka"]["lockfile"];
    $lock_fh = fopen($file, "w");
    if ($lock_fh === FALSE) {
        $err_msg = $msgarr['28067'][SCREEN_MSG];
        $log_msg = $msgarr['28067'][LOG_MSG];
        return FALSE;
    } 

    $result = flock($lock_fh, LOCK_EX);
    if ($result === FALSE) {
        $err_msg = $msgarr['28067'][SCREEN_MSG];
        $log_msg = $msgarr['28067'][LOG_MSG];
        return FALSE;
    }
    return $lock_fh;
}

/**********************************************************
 * unlock_file
 *
 * եΥå
 *
 * []
 *        $lock_fh         åեϥɥ
 * [֤]
 *        TRUE             
 *        FALSE            
 **********************************************************/
function unlock_file ($lock_fh)
{

    global $msgarr;
    global $err_msg;
    global $log_msg;
    global $web_conf;

    $result = flock($lock_fh, LOCK_UN);
    if ($result === FALSE) {
        $err_msg = $msgarr['28067'][SCREEN_MSG];
        $log_msg = $msgarr['28067'][LOG_MSG];
        return FALSE;
    }
    fclose($lock_fh);
    return TRUE;
}


/**********************************************************
 * check_form
 *
 * ͤηå
 *
 * []
 * $post                POSTƤ
 *
 * [֤]
 * TRUE                 
 * FALSE                ۾
 **********************************************************/
function check_form ($post)
{
    global $msgarr;
    global $err_msg;
    global $log_msg;
    global $web_conf;

    /* IPɥ쥹ηå */
    if ($post["ipaddress"] === "") {
        $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "IPɥ쥹");
        return FALSE;
    }
    if (strlen($post["ipaddress"]) > 39) {
        $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "IPɥ쥹");
        return FALSE;
    }
    if (!filter_var($post["ipaddress"], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6)) {
        $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "IPɥ쥹");
        return FALSE;
    }

    /* ݡֹηå */
    if ($post["port"] === "") {
        $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "ݡֹ");
        return FALSE;
    }
    if (strlen($post["port"]) > 5) {
        $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "ݡֹ");
        return FALSE;
    }
    if (ctype_digit($post["port"]) === FALSE) {
        $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "ݡֹ");
        return FALSE;
    }
    if ($post["port"] > 65535) {
        $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "ݡֹ");
        return FALSE;
    }

    /* ƻֳ֤ηå */
    if ($post["delay_loop"] === "") {
        $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "ƻֳ");
        return FALSE;
    }
    if (strlen($post["delay_loop"]) > 10) {
        $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "ƻֳ");
        return FALSE;
    }
    if (ctype_digit($post["delay_loop"]) === FALSE) {
        $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "ƻֳ");
        return FALSE;
    }

    /* ʬ르ꥺηå */
    if ($post["lb_algo"] === "") {
        $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "ʬ르ꥺ");
        return FALSE;
    }
    if (strlen($post["lb_algo"]) > 1) {
        $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "ʬ르ꥺ");
        return FALSE;
    }
    if (ctype_digit($post["lb_algo"]) === FALSE) {
        $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "ʬ르ꥺ");
        return FALSE;
    }
    if ($post["lb_algo"] > 6) {
        $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "ʬ르ꥺ");
        return FALSE;
    }
    /* žˡηå */
    if ($post["lb_kind"] === "") {
        $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "žˡ");
        return FALSE;
    }
    if (strlen($post["lb_kind"]) > 1) {
        $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "žˡ");
        return FALSE;
    }
    if (ctype_digit($post["lb_kind"]) === FALSE) {
        $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "žˡ");
        return FALSE;
    }
    if ($post["lb_kind"] > 2) {
        $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "žˡ");
        return FALSE;
    }

    /* ॢȤηå */
    if ($post["persistence_timeout"] === "") {
        $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "ॢ");
        return FALSE;
    }
    if (strlen($post["persistence_timeout"]) > 5) {
        $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "ॢ");
        return FALSE;
    }
    if (ctype_digit($post["persistence_timeout"]) === FALSE) {
        $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "ॢ");
        return FALSE;
    }
    if ($post["persistence_timeout"] > 65535) {
        $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "ॢ");
        return FALSE;
    }

    /* Сۥ */
    if (strlen($post["virtualhost"]) > 256) {
        $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "Сۥ");
        return FALSE;
    }
    $num = "0123456789";
    $sl = "abcdefghijklmnopqrstuvwxyz";
    $ll = strtoupper($sl);
    $sym = "!#$%&'*+-/=?^_{}~.";
    $allow_letter = $num . $sl . $ll . $sym;
    if (strspn($post["virtualhost"], $allow_letter) != strlen($post["virtualhost"])) {
        $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "Сۥ");
        return FALSE;
    }

    /* SorryIPɥ쥹ηå */
    if ($post["sorry_server_ipaddress"] !== "") {
        if (strlen($post["sorry_server_ipaddress"]) > 39) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "SorryIPɥ쥹");
            return FALSE;
        }
        if (!filter_var($post["sorry_server_ipaddress"], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6)) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "SorryIPɥ쥹");
            return FALSE;
        }

    /* SorryХݡֹηå */
        if ($post["sorry_server_port"] === "") {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "SorryХݡֹ");
            return FALSE;
        }
    }
    if ($post["sorry_server_port"] !== "") {
        if ($post["sorry_server_ipaddress"] === "") {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "SorryIPɥ쥹");
            return FALSE;
        }
        if (strlen($post["sorry_server_port"]) > 5) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "SorryХݡֹ");
            return FALSE;
        }
        if (ctype_digit($post["sorry_server_port"]) === FALSE) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "SorryХݡֹ");
            return FALSE;
        }
        if ($post["sorry_server_port"] > 65535) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "SorryХݡֹ");
            return FALSE;
        }
    }

    return TRUE;
}

/**********************************************************
 * read_rslist
 *
 * real_server.confɤ߹
 *
 * []
 *        $file            real_server.confΥѥ
 *        &$data           С륵Фξ
 * [֤]
 *        TRUE             
 *        FALSE            顼
 *
 **********************************************************/
function read_rslist($file, &$data)
{
    global $msgarr;
    global $err_msg;
    global $log_msg;
    global $web_conf;

    /* ե̵ͭǧ */
    if(!file_exists($file)) {
        return TRUE;
    }

    $fh = fopen($file, "r");
    if ($fh === FALSE) {
        $err_msg = sprintf($msgarr['28074'][SCREEN_MSG], $file);
        $log_msg = sprintf($msgarr['28074'][LOG_MSG], $file);
        return FALSE;
    }

    $data ="";
    $key = 0;
    while ($result = fgets($fh)) {
    
        $result = trim($result);
        $lines = explode(" ", $result, 2);
        if ($lines[0] === $result) {
            $err_msg = sprintf($msgarr['28075'][SCREEN_MSG], $file);
            $log_msg = sprintf($msgarr['28075'][LOG_MSG], $file);
            fclose($fh);
            return FALSE;
        }

        if ($lines[0] !== 'include' && $lines[0] !== '#include') {
            $err_msg = sprintf($msgarr['28075'][SCREEN_MSG], $file);
            $log_msg = sprintf($msgarr['28075'][LOG_MSG], $file);
            fclose($fh);
            return FALSE;
        }
   
        /* disable:enable */
        $lines_able = strncmp('#', $lines[0], 1);
        if ($lines_able === 0) {
            $data[$key]["rsable"] = "disable";
        } else {
            $data[$key]["rsable"] = "enable";
        }

        /* Ƭ "./" Ǥ뤳 */
        if (strncmp($lines[1], "./", 2)) {
            $err_msg = sprintf($msgarr['28075'][SCREEN_MSG], $file);
            $log_msg = sprintf($msgarr['28075'][LOG_MSG], $file);
            fclose($fh);
            return FALSE;
        }
        $lines[1] = substr($lines[1], 2);

        /* IPɥ쥹 */
        $lines_ip = explode("_", $lines[1], 2);
        if ($lines_ip[0] === $lines[1]) {
            $err_msg = sprintf($msgarr['28075'][SCREEN_MSG], $file);
            $log_msg = sprintf($msgarr['28075'][LOG_MSG], $file);
            fclose($fh);
            return FALSE;
        }
        if (!filter_var($lines_ip[0], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6)) {
            $err_msg = sprintf($msgarr['28075'][SCREEN_MSG], $file);
            $log_msg = sprintf($msgarr['28075'][LOG_MSG], $file);
            fclose($fh);
            return FALSE;
        }
        $data[$key]["rs_ipaddress"] = $lines_ip[0];

         /* ݡֹ */
        $lines_port = explode(".", $lines_ip[1], 2);
        if ($lines_port[0] === $lines_ip[1]) {
            $err_msg = sprintf($msgarr['28075'][SCREEN_MSG], $file);
            $log_msg = sprintf($msgarr['28075'][LOG_MSG], $file);
            fclose($fh);
            return FALSE;
        }
        $ret = ctype_digit($lines_port[0]);
        if ($ret === FALSE) {
            $err_msg = sprintf($msgarr['28075'][SCREEN_MSG], $file);
            $log_msg = sprintf($msgarr['28075'][LOG_MSG], $file);
            fclose($fh);
            return FALSE;
        }
        $data[$key]["rs_port"] = $lines_port[0];
        $key++;
    }
    fclose($fh);
    return TRUE;
}

/**********************************************************
 * write_rslist
 *
 * real_server.confν񤭹
 *
 * []
 *        $file            real_server.confΥѥ
 *        &$data           real_server.conf
 * [֤]
 *        TRUE             
 *        FALSE            
 **********************************************************/

function write_rslist ($file, &$data)
{
    global $msgarr;
    global $err_msg;
    global $log_msg;
    global $web_conf;
    $dirname = dirname($file);
    $rsfilename = basename($file);
    $write_data = "";

    /* 񤭹߲ǽγǧ */
    if (!is_writable($file)) {
        $err_msg = sprintf($msgarr['28081'][SCREEN_MSG], $file);
        $log_msg = sprintf($msgarr['28081'][LOG_MSG], $file);
        return FALSE;
    }

    /* 񤭹ѥǡκ */
    $cht = count($data);
    for ($i = 0; $i < $cht; $i++) {
        if ($data[$i]["rsable"] === 'disable') {
            $write_data .= "#";
        }
        $write_data .= "include ./".$data[$i]["rs_ipaddress"]."_".$data[$i]["rs_port"].".conf\n";
    }

    /* ե˽񤭹 */
    $tmpfname = tempnam($dirname, $rsfilename);
    if ($tmpfname === FALSE) {
        $err_msg = sprintf($msgarr['28081'][SCREEN_MSG], $file);
        $log_msg = sprintf($msgarr['28081'][LOG_MSG], $file);
        return FALSE;
    }

    $fh = fopen($tmpfname, "w");
    if ($fh === FALSE) {
        $err_msg = sprintf($msgarr['28081'][SCREEN_MSG], $file);
        $log_msg = sprintf($msgarr['28081'][LOG_MSG], $file);
        return FALSE;
    }

    $ret = fwrite($fh, $write_data);
    if ($ret === FALSE) {
        $err_msg = sprintf($msgarr['28081'][SCREEN_MSG], $file);
        $log_msg = sprintf($msgarr['28081'][LOG_MSG], $file);
        fclose($fh);
        return FALSE;
    }
    fclose($fh);

    /* ե֤ */
    $ret = rename($tmpfname, $file);
    if ($ret === FALSE) {
        $err_msg = sprintf($msgarr['28081'][SCREEN_MSG], $file);
        $log_msg = sprintf($msgarr['28081'][LOG_MSG], $file);
        return FALSE;
    }
    return TRUE;
}

/**********************************************************
 * rs_check_form
 *
 * ͤηå
 *
 * []
 * $post                POSTƤ
 *
 * [֤]
 * TRUE                 
 * FALSE                ۾
 **********************************************************/
function rs_check_form ($post)
{
    global $msgarr;
    global $err_msg;
    global $log_msg;
    global $web_conf;


    /* IPɥ쥹ηå */
    if ($post["rs_ipaddress"] === "") {
        $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "IPɥ쥹");
        return FALSE;
    }
    if (strlen($post["rs_ipaddress"]) > 39) {
        $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "IPɥ쥹");
        return FALSE;
    }
    if (!filter_var($post["rs_ipaddress"], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6)) {
        $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "IPɥ쥹");
        return FALSE;
    }

    /* ݡֹηå */
    if ($post["rs_port"] === "") {
        $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "ݡֹ");
        return FALSE;
    }
    if (strlen($post["rs_port"]) > 5) {
        $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "ݡֹ");
        return FALSE;
    }
    if (ctype_digit($post["rs_port"]) === FALSE) {
        $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "ݡֹ");
        return FALSE;
    }
    if ($post["rs_port"] > 65535) {
        $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "ݡֹ");
        return FALSE;
    }

    /* Ťդηå */
    if ($post["weighting"] !== "") {
        if (strlen($post["weighting"]) > 5) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "Ťդ");
            return FALSE;
        }
        if (ctype_digit($post["weighting"]) === FALSE) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "Ťդ");
            return FALSE;
        }
    }

    /* إ륹åηå */
    if ($post["h_check"] === "") {
        $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "إ륹å");
        return FALSE;
    }
    if (strlen($post["h_check"]) > 1) {
        $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "إ륹å");
        return FALSE;
    }
    if (ctype_digit($post["h_check"]) === FALSE) {
        $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "إ륹å");
        return FALSE;
    }
    if ($post["h_check"] > 4) {
        $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "إ륹å");
        return FALSE;
    }

    /* η */
    if ($post["h_check"] == 0 || $post["h_check"] == 1) {
        if (check_http($post) === FALSE) {
            return FALSE;
        }

    } elseif ($post["h_check"] == 2) {
        if (check_tcp($post) === FALSE) {
            return FALSE;
        }

    } elseif ($post["h_check"] == 3) {
        if (check_smtp($post) === FALSE) {
            return FALSE;
        }

    } else {
        if (check_misc($post) === FALSE) {
            return FALSE;
        }
    } 

    return TRUE;
}


/**********************************************************
 * check_http
 *
 * ͤηå
 *
 * []
 * $post                POSTƤ
 *
 * [֤]
 * TRUE                 
 * FALSE                ۾
 **********************************************************/
function check_http ($post)
{
    global $msgarr;
    global $err_msg;
    global $log_msg;
    global $web_conf;

    /* ѥΥå */
    if ($post["http_path"] === "") {
        $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "ѥ");
        return FALSE;
    }
    if (strlen($post["http_path"]) > 256) {
        $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "ѥ");
        return FALSE;
    }

    /* Ȥηå */
    if ($post["http_digest"] !== "") {
        if (strlen($post["http_digest"]) > 64) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "");
            return FALSE;
        }
        $num = "0123456789";
        $sl = "abcdef";
        $ll = strtoupper($sl);
        $allow_letter = $num . $sl . $ll;
        if (strspn($post["http_digest"], $allow_letter) != strlen($post["http_digest"])) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "");
            return FALSE;
        }
    }

    /* ȥ饤ηå */
    if ($post["nb_get_retry"] !== "") {
        if (strlen($post["nb_get_retry"]) > 3) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "ȥ饤");
            return FALSE;
        }
        if (ctype_digit($post["nb_get_retry"]) === FALSE) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "ȥ饤");
            return FALSE;
        }
    }

    /* ȥ饤ֳ֤ηå */
    if ($post["http_delay_retry"] !== "") {
        if (strlen($post["http_delay_retry"]) > 3) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "ȥ饤ֳ");
            return FALSE;
        }
        if (ctype_digit($post["http_delay_retry"]) === FALSE) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "ȥ饤ֳ");
            return FALSE;
        }
    }

    /* ³IPɥ쥹ηå */
    if ($post["http_connect_ipaddress"] !== "") {
        if (strlen($post["http_connect_ipaddress"]) > 39) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "³IPɥ쥹");
            return FALSE;
        }
        if (!filter_var($post["http_connect_ipaddress"], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6)) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "³IPɥ쥹");
            return FALSE;
        }
    }

    /* ³ݡֹηå */
    if ($post["http_connect_port"] !== "") {
        if (strlen($post["http_connect_port"]) > 5) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "³ݡֹ");
            return FALSE;
        }
        if (ctype_digit($post["http_connect_port"]) === FALSE) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "³ݡֹ");
            return FALSE;
        }
        if ($post["http_connect_port"] > 65535) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "³ݡֹ");
            return FALSE;
        }
    }

    /* ³IPɥ쥹ηå */
    if ($post["http_connect_source_ipaddress"] !== "") {
        if (strlen($post["http_connect_source_ipaddress"]) > 39) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "³IPɥ쥹");
            return FALSE;
        }
        if (!filter_var($post["http_connect_source_ipaddress"], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6)) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "³IPɥ쥹");
            return FALSE;
        }
    }
    /* ³ݡֹηå */
    if ($post["http_connect_source_port"] !== "") {
        if (strlen($post["http_connect_source_port"]) > 5) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "³ݡֹ");
            return FALSE;
        }
        if (ctype_digit($post["http_connect_source_port"]) === FALSE) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "³ݡֹ");
            return FALSE;
        }
        if ($post["http_connect_source_port"] > 65535) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "³ݡֹ");
            return FALSE;
        }
    }

    /* ³ॢȤηå */
    if ($post["http_connect_timeout"] !== "") {
        if (strlen($post["http_connect_timeout"]) > 3) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "³ॢ");
            return FALSE;
        }
        if (ctype_digit($post["http_connect_timeout"]) === FALSE) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "³ॢ");
            return FALSE;
        }
    }

    return TRUE;
}

/**********************************************************
 * check_tcp
 *
 * ͤηå
 *
 * []
 * $post                POSTƤ
 *
 * [֤]
 * TRUE                 
 * FALSE                ۾
 **********************************************************/
function check_tcp ($post)
{
    global $msgarr;
    global $err_msg;
    global $log_msg;
    global $web_conf;

    /* ³IPɥ쥹ηå */
    if ($post["tcp_connect_ipaddress"] !== "") {
        if (strlen($post["tcp_connect_ipaddress"]) > 39) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "³IPɥ쥹");
            return FALSE;
        }
        if (!filter_var($post["tcp_connect_ipaddress"], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6)) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "³IPɥ쥹");
            return FALSE;
        }
    }

    /* ³ݡֹηå */
    if ($post["tcp_connect_port"] !== "") {
        if (strlen($post["tcp_connect_port"]) > 5) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "³ݡֹ");
            return FALSE;
        }
        if (ctype_digit($post["tcp_connect_port"]) === FALSE) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "³ݡֹ");
            return FALSE;
        }
        if ($post["tcp_connect_port"] > 65535) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "³ݡֹ");
            return FALSE;
        }
    }

    /* ³IPɥ쥹ηå */
    if ($post["tcp_connect_source_ipaddress"] !== "") {
        if (strlen($post["tcp_connect_source_ipaddress"]) > 39) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "³IPɥ쥹");
            return FALSE;
        }
        if (!filter_var($post["tcp_connect_source_ipaddress"], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6)) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "³IPɥ쥹");
            return FALSE;
        }
    }
    /* ³ݡֹηå */
    if ($post["tcp_connect_source_port"] !== "") {
        if (strlen($post["tcp_connect_source_port"]) > 5) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "³ݡֹ");
            return FALSE;
        }
        if (ctype_digit($post["tcp_connect_source_port"]) === FALSE) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "³ݡֹ");
            return FALSE;
        }
        if ($post["tcp_connect_source_port"] > 65535) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "³ݡֹ");
            return FALSE;
        }
    }

    /* ³ॢȤηå */
    if ($post["tcp_connect_timeout"] !== "") {
        if (strlen($post["tcp_connect_timeout"]) > 3) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "³ॢ");
            return FALSE;
        }
        if (ctype_digit($post["tcp_connect_timeout"]) === FALSE) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "³ॢ");
            return FALSE;
        }
    }

    return TRUE;
}

/**********************************************************
 * check_smtp
 *
 * ͤηå
 *
 * []
 * $post                POSTƤ
 *
 * [֤]
 * TRUE                 
 * FALSE                ۾
 **********************************************************/
function check_smtp ($post)
{
    global $msgarr;
    global $err_msg;
    global $log_msg;
    global $web_conf;

    /* ȥ饤ηå */
    if ($post["get_retry"] !== "") {
        if (strlen($post["get_retry"]) > 3) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "ȥ饤");
            return FALSE;
        }
        if (ctype_digit($post["get_retry"]) === FALSE) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "ȥ饤");
            return FALSE;
        }
    }

    /* ȥ饤ֳ֤ηå */
    if ($post["smtp_delay_retry"] !== "") {
        if (strlen($post["smtp_delay_retry"]) > 3) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "ȥ饤ֳ");
            return FALSE;
        }
        if (ctype_digit($post["smtp_delay_retry"]) === FALSE) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "ȥ饤ֳ");
            return FALSE;
        }
    }

    /* ³IPɥ쥹ηå */
    if ($post["smtp_connect_ipaddress"] !== "") {
        if (strlen($post["smtp_connect_ipaddress"]) > 39) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "³IPɥ쥹");
            return FALSE;
        }
        if (!filter_var($post["smtp_connect_ipaddress"], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6)) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "³IPɥ쥹");
            return FALSE;
        }
    }

    /* ³ݡֹηå */
    if ($post["smtp_connect_port"] !== "") {
        if (strlen($post["smtp_connect_port"]) > 5) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "³ݡֹ");
            return FALSE;
        }
        if (ctype_digit($post["smtp_connect_port"]) === FALSE) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "³ݡֹ");
            return FALSE;
        }
        if ($post["smtp_connect_port"] > 65535) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "³ݡֹ");
            return FALSE;
        }
    }

    /* ³IPɥ쥹ηå */
    if ($post["smtp_connect_source_ipaddress"] !== "") {
        if (strlen($post["smtp_connect_source_ipaddress"]) > 39) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "³IPɥ쥹");
            return FALSE;
        }
        if (!filter_var($post["smtp_connect_source_ipaddress"], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6)) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "³IPɥ쥹");
            return FALSE;
        }
    }
    /* ³ݡֹηå */
    if ($post["smtp_connect_source_port"] !== "") {
        if (strlen($post["smtp_connect_source_port"]) > 5) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "³ݡֹ");
            return FALSE;
        }
        if (ctype_digit($post["smtp_connect_source_port"]) === FALSE) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "³ݡֹ");
            return FALSE;
        }
        if ($post["smtp_connect_source_port"] > 65535) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "³ݡֹ");
            return FALSE;
        }
    }

    /* ³ॢȤηå */
    if ($post["smtp_connect_timeout"] !== "") {
        if (strlen($post["smtp_connect_timeout"]) > 3) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "³ॢ");
            return FALSE;
        }
        if (ctype_digit($post["smtp_connect_timeout"]) === FALSE) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "³ॢ");
            return FALSE;
        }
    }

    /* HELOꥯȤηå */
    if ($post["helo_name"] !== "") {
        if (strlen($post["helo_name"]) > 256) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "HELOꥯ");
            return FALSE;
        }
        $num = "0123456789";
        $sl = "abcdefghijklmnopqrstuvwxyz";
        $ll = strtoupper($sl);
        $sym = "!#$%&'*+-/=?^_{}~.";
        $allow_letter = $num . $sl . $ll . $sym;
        if (strspn($post["helo_name"], $allow_letter) != strlen($post["helo_name"])) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "HELOꥯ");
            return FALSE;
        }
    }
    return TRUE;
}

/**********************************************************
 * check_misc
 *
 * ͤηå
 *
 * []
 * $post                POSTƤ
 *
 * [֤]
 * TRUE                 
 * FALSE                ۾
 **********************************************************/
function check_misc ($post)
{
    global $msgarr;
    global $err_msg;
    global $log_msg;
    global $web_conf;

    /* ץȥѥΥå */
   if ($post["misc_path"] === "") {
       $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "ץȥѥ");
       return FALSE;
   }
   if (strlen($post["misc_path"]) > MAXMAIL) {
       $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "ץȥѥ");
       return FALSE;
   }

    /* ॢȤηå */
    if ($post["misc_timeout"] !== "") {
        if (strlen($post["misc_timeout"]) > 3) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "ॢ");
            return FALSE;
        }
        if (ctype_digit($post["misc_timeout"]) === FALSE) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "ॢ");
            return FALSE;
        }
    }
    return TRUE;
}

/***********************************************************
 * iluka_location()
 *
 * Ԥ
 *
 * []
 *      $url
 *      $msg
 *      $post        ݥȤ(name, ͤvalue)
 *
 * [֤]
 *      ʤ
 ************************************************************/
function iluka_location($url, $msg = "", $post = array())
{
    global $sesskey;

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

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

    $values = "";
    if (count($post) != 0) {
        foreach ($post as $name => $value) {
            $e_name = escape_html($name);
            $e_val  = escape_html($value);
            $values .= "<input type=\"hidden\" " .
                      "name=\"$e_name\" value=\"$e_val\">";
        }
    }

    /* 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
    $values
</form>
</body>
</html>
EOD;
    exit;

}

/***********************************************************
 * make_rsconf
 *
 * ꥢ륵եmain
 *
 * []
 *       $rsdir              եΥǥ쥯ȥѥ
 *       $rsfile             եΥեѥ
 *       $conftmpl           ե  
 * [֤]
 *       TRUE                 
 *       FALSE               ۾ 
 **********************************************************/
function make_rsconf ($rsdir, $rsfile, $conftmpl)
{

    global $msgarr;
    global $err_msg;
    global $log_msg;
    global $web_conf;

    /* ե */
    $tmpfname = tempnam($rsdir, $rsfile);
    if ($tmpfname === FALSE) {
        $err_msg = sprintf($msgarr['28083'][SCREEN_MSG], $rsfile);
        $log_msg = sprintf($msgarr['28083'][LOG_MSG], $rsfile);
        return FALSE;
    }

    $fh = fopen($tmpfname, "w");
    if ($fh === FALSE) {
        $err_msg = sprintf($msgarr['28083'][SCREEN_MSG], $rsfile);
        $log_msg = sprintf($msgarr['28083'][LOG_MSG], $rsfile);
        return FALSE;
    }

    if (fwrite($fh, $conftmpl) === FALSE) {
        $err_msg = sprintf($msgarr['28083'][SCREEN_MSG], $rsfile);
        $log_msg = sprintf($msgarr['28083'][LOG_MSG], $rsfile);
        return FALSE;
    }
    fclose($fh);

    if (rename($tmpfname, $rsfile) === FALSE) {
        $err_msg = sprintf($msgarr['28083'][SCREEN_MSG], $rsfile);
        $log_msg = sprintf($msgarr['28083'][LOG_MSG], $rsfile);
        return FALSE;
    }

    return TRUE;
}

/***********************************************************
 * replace_tmpls
 *
 * ꥢ륵եѥƥץ졼Ⱥ
 *
 * []
 *       $post         Ϥ줿
 *       &$conftmpl    ƥץ졼 
 * [֤]
 *       TRUE          
 *       FALSE         
 **********************************************************/
function replace_tmpls ($post, &$conftmpl)
{

    global $msgarr;
    global $err_msg;
    global $log_msg;
    global $web_conf;
    global $tmpl_list;

    $RSconf = "../../../../tmpl/iluka/real_server.conf.tmpl";

    $health_tmpl = $tmpl_list[$post["h_check"]];
    $healthtmpl  = file_get_contents($health_tmpl);

    if ($healthtmpl === FALSE) {
        $err_msg = sprintf($msgarr['28070'][SCREEN_MSG], $vsfile);
        $log_msg = sprintf($msgarr['28070'][LOG_MSG], $vsfile);
        return FALSE;
    }

    /* httpsslѴ */
    if ($post["h_check"] == 0 || $post["h_check"] == 1) {
        $tag["<<PATH>>"] = "path ".$post["http_path"];

        if ($post["http_digest"] == "") {
            $tag["<<DIGEST>>"] = "";
        } else {
            $tag["<<DIGEST>>"] = "digest ".$post["http_digest"];
        }

        if ($post["http_status_code"] == "") {
            $tag["<<STATUS_CODE>>"] = "";
        } else {
            $tag["<<STATUS_CODE>>"] = "status_code ".$post["http_status_code"];
        }

        if ($post["nb_get_retry"] == "") {
            $tag["<<NB_GET_RETRY>>"] = "";
        } else {
            $tag["<<NB_GET_RETRY>>"] = "nb_get_retry ".$post["nb_get_retry"];
        }


        if ($post["http_delay_retry"] == "") {
            $tag["<<DELAY_BEFORE_RETRY>>"] = "";
        } else {
            $tag["<<DELAY_BEFORE_RETRY>>"] = "delay_before_retry ".$post["http_delay_retry"];
        }

        if ($post["http_connect_ipaddress"] == "") {
            $tag["<<CONNECT_IP>>"] = "";
        } else {
            $tag["<<CONNECT_IP>>"] = "connect_ip ".$post["http_connect_ipaddress"];
        }

        if ($post["http_connect_port"] == "") {
            $tag["<<CONNECT_PORT>>"] = "";
        } else {
            $tag["<<CONNECT_PORT>>"] = "connect_port ".$post["http_connect_port"];
        }

        if ($post["http_connect_source_ipaddress"] == "") {
            $tag["<<BINDTO>>"] = "";
        } else {
            $tag["<<BINDTO>>"] = "bindto ".$post["http_connect_source_ipaddress"];
        }

        if ($post["http_connect_source_port"] == "") {
            $tag["<<BIND_PORT>>"] = "";
        } else {
            $tag["<<BIND_PORT>>"] = "bind_port ".$post["http_connect_source_port"];
        }

        if ($post["http_connect_timeout"] == "") {
            $tag["<<CONNECT_TIMEOUT>>"] = "";
        } else {
            $tag["<<CONNECT_TIMEOUT>>"] = "connect_timeout ".$post["http_connect_timeout"];
        }
    }

    /* tcpѴ */
    if ($post["h_check"] == 2) {
        if ($post["tcp_connect_ipaddress"] == "") {
            $tag["<<CONNECT_IP>>"] = "";
        } else {
            $tag["<<CONNECT_IP>>"] = "connect_ip ".$post["tcp_connect_ipaddress"];
        }

        if ($post["tcp_connect_port"] == "") {
            $tag["<<CONNECT_PORT>>"] = "";
        } else {
            $tag["<<CONNECT_PORT>>"] = "connect_port ".$post["tcp_connect_port"];
        }

        if ($post["tcp_connect_source_ipaddress"] == "") {
            $tag["<<BINDTO>>"] = "";
        } else {
            $tag["<<BINDTO>>"] = "bindto ".$post["tcp_connect_source_ipaddress"];
        }

        if ($post["tcp_connect_source_port"] == "") {
            $tag["<<BIND_PORT>>"] = "";
        } else {
            $tag["<<BIND_PORT>>"] = "bind_port ".$post["tcp_connect_source_port"];
        }

        if ($post["tcp_connect_timeout"] == "") {
            $tag["<<CONNECT_TIMEOUT>>"] = "";
        } else {
            $tag["<<CONNECT_TIMEOUT>>"] = "connect_timeout ".$post["tcp_connect_timeout"];
        }
    }

    /* smtpѴ */
    if ($post["h_check"] == 3) {
        if ($post["smtp_connect_ipaddress"] == "") {
            $tag["<<CONNECT_IP>>"] = "";
        } else {
            $tag["<<CONNECT_IP>>"] = "connect_ip ".$post["smtp_connect_ipaddress"];
        }

        if ($post["smtp_connect_port"] == "") {
            $tag["<<CONNECT_PORT>>"] = "";
        } else {
            $tag["<<CONNECT_PORT>>"] = "connect_port ".$post["smtp_connect_port"];
        }

        if ($post["smtp_connect_source_ipaddress"] == "") {
            $tag["<<BINDTO>>"] = "";
        } else {
            $tag["<<BINDTO>>"] = "bindto ".$post["smtp_connect_source_ipaddress"];
        }

        if ($post["smtp_connect_source_port"] == "") {
            $tag["<<BIND_PORT>>"] = "";
        } else {
            $tag["<<BIND_PORT>>"] = "bind_port ".$post["smtp_connect_source_port"];
        }

        if ($post["smtp_connect_timeout"] == "") {
            $tag["<<CONNECT_TIMEOUT>>"] = "";
        } else {
            $tag["<<CONNECT_TIMEOUT>>"] = "connect_timeout ".$post["smtp_connect_timeout"];
        }

        if ($post["get_retry"] == "") {
            $tag["<<RETRY>>"] = "";
        } else {
            $tag["<<RETRY>>"] = "retry ".$post["get_retry"];
        }

        if ($post["smtp_delay_retry"] == "") {
            $tag["<<DELAY_BEFORE_RETRY>>"] = "";
        } else {
            $tag["<<DELAY_BEFORE_RETRY>>"] = "delay_before_retry ".$post["smtp_delay_retry"];
        }

        if ($post["helo_name"] == "") {
            $tag["<<HELO_NAME>>"] = "";
        } else {
            $tag["<<HELO_NAME>>"] = "helo_name ".$post["helo_name"];
        }
    }

    /* miscѴ */
    if ($post["h_check"] == 4) {

        $tag["<<MISC_PATH>>"] = "misc_path ".$post["misc_path"];

        if ($post["misc_timeout"] == "") {
            $tag["<<MISC_TIMEOUT>>"] = "";
        } else {
            $tag["<<MISC_TIMEOUT>>"] = "misc_timeout ".$post["misc_timeout"];
        }
    }

    $health_check_tmpl= change_template_tag($healthtmpl, $tag);

    $rsconftmpl = file_get_contents($RSconf);
    if ($rsconftmpl === FALSE) {
        $err_msg = sprintf($msgarr['28070'][SCREEN_MSG], $RSconf);
        $log_msg = sprintf($msgarr['28070'][LOG_MSG], $RSconf);
        return FALSE;
    }

    $tag["<<IP_ADDRESS>>"]   = $post["rs_ipaddress"];
    $tag["<<PORT>>"]         = $post["rs_port"];
    $tag["<<HEALTH_CHECK>>"] = $health_check_tmpl;

    if ($post["weighting"] == "") {
        $tag["<<WEIGHT>>"] = "";
    } else {
        $tag["<<WEIGHT>>"] = "weight ".$post["weighting"];
    }

    $conftmpl = change_template_tag($rsconftmpl, $tag);
    return TRUE;
}

/***********************************************************
 * get_rsconf
 *
 * ꥢ륵եɤ߹ 
 *
 * []
 *      $post        ݥȤƤ
 *      &$data       ꥢ륵ФϿ
 * [֤]
 *      TRUE         
 *      FALSE        
 ************************************************************/
function get_rsconf ($post, &$data)
{

    global $msgarr;
    global $err_msg;
    global $log_msg;
    global $web_conf;

    $rsconf = $web_conf["iluka"]["keepalivedbasedir"]."virtual_server/".$post["ipaddress"]."_".$post["port"]."/".$post["rs_ipaddress"]."_".$post["rs_port"].".conf";
    $rshead = "real_server ".$post["rs_ipaddress"]." ".$post["rs_port"]." {";

    $data = array("ipaddress"          => $post["ipaddress"],
                  "port"               => $post["port"],
                  "rs_ipaddress"        => $post["rs_ipaddress"],
                  "rs_port"             => $post["rs_port"],
                  "weight"             => "",
                  "path"               => "",
                  "digest"             => "",
                  "status_code"        => "",
                  "nb_get_retry"       => "",
                  "delay_before_retry" => "",
                  "connect_ip"         => "",
                  "connect_port"       => "",
                  "bindto"             => "",
                  "bind_port"          => "",
                  "connect_timeout"    => "",
                  "retry"              => "",
                  "helo_name"          => "",
                  "misc_path"          => "",
                  "misc_timeout"       => "");

    $fh = fopen($rsconf, "r");
    if ($fh === FALSE) {
        $err_msg = sprintf($msgarr['28074'][SCREEN_MSG], $rsconf);
        $log_msg = sprintf($msgarr['28074'][LOG_MSG], $rsconf);
        return FALSE;
    }

    /* rear_server [IP] [PORT] {פޤɤФ */
    while($result = fgets($fh)) {
        $result = trim($result);
        if ($result === $rshead) {
            break;
        }
    }

    if ($result === FALSE) {
        $err_msg = sprintf($msgarr['28085'][SCREEN_MSG], $rsconf);
        $log_msg = sprintf($msgarr['28085'][LOG_MSG], $rsconf);
        return FALSE;
    }

    $health_flag = 0;
    $url_flag = 0;
    $host_flag = 0;

    while ($result = fgets($fh)) {
        $result = trim($result);
        $lines = preg_split("[\s+]", $result, 2);

        /* HTT_GETSSL_GETξ */
        if ($health_flag === 1) {
            if ($lines[0] === "url") {
                $url_flag = 1;
                continue;
            }
            if ($url_flag === 1) {
                if ($lines[0] === "path") {
                    $data["path"] = $lines[1];
                    continue;
                }
                if ($lines[0] === "digest") {
                    $data["digest"] = $lines[1];
                    continue;
                }
                if ($lines[0] === "status_code") {
                    $data["status_code"] = $lines[1];
                    continue;
                }
                if ($lines[0] === "}") {
                    $url_flag = 0;
                    continue;
                }
            } else {
                if ($lines[0] === "nb_get_retry") {
                    $data["nb_get_retry"] = $lines[1];
                    continue;
                }
                if ($lines[0] === "delay_before_retry") {
                    $data["delay_before_retry"] = $lines[1];
                    continue;
                }
                if ($lines[0] === "connect_ip") {
                    $data["connect_ip"] = $lines[1];
                    continue;
                }
                if ($lines[0] === "connect_port") {
                    $data["connect_port"] = $lines[1];
                    continue;
                }
                if ($lines[0] === "bindto") {
                    $data["bindto"] = $lines[1];
                    continue;
                }
                if ($lines[0] === "bind_port") {
                    $data["bind_port"] = $lines[1];
                    continue;
                }
                if ($lines[0] === "connect_timeout") {
                    $data["connect_timeout"] = $lines[1];
                    continue;
                }
                if ($lines[0] === "}") {
                    $healt_flag = 0;
                    continue;
                }
            }

        /* TCP_CHECKξ */
        } elseif ($health_flag === 2) {
            if ($lines[0] === "connect_ip") {
                $data["connect_ip"] = $lines[1];
                continue;
            }
            if ($lines[0] === "connect_port") {
                $data["connect_port"] = $lines[1];
                continue;
            }
            if ($lines[0] === "bindto") {
                $data["bindto"] = $lines[1];
                continue;
            }
            if ($lines[0] === "bind_port") {
                $data["bind_port"] = $lines[1];
                continue;
            }
            if ($lines[0] === "connect_timeout") {
                $data["connect_timeout"] = $lines[1];
                continue;
            }
            if ($lines[0] === "}") {
               $healt_flag = 0;
                continue;
            }

        /* SMTP_CHECKξ */
        } elseif ($health_flag === 3) {
            if ($lines[0] === "host") {
                $host_flag = 1;
                continue;
            }
            if ($host_flag === 1) {
                if ($lines[0] === "connect_ip") {
                    $data["connect_ip"] = $lines[1];
                    continue;
                }
                if ($lines[0] === "connect_port") {
                    $data["connect_port"] = $lines[1];
                    continue;
                }
                if ($lines[0] === "bindto") {
                    $data["bindto"] = $lines[1];
                    continue;
                }
                if ($lines[0] === "bind_port") {
                    $data["bind_port"] = $lines[1];
                    continue;
                }
                if ($lines[0] === "connect_timeout") {
                    $data["connect_timeout"] = $lines[1];
                    continue;
                }
                if ($lines[0] === "}") {
                    $host_flag = 0;
                    continue;
                }
            } else {
                if ($lines[0] === "retry") {
                    $data["retry"] = $lines[1];
                    continue;
                }
                if ($lines[0] === "delay_before_retry") {
                    $data["delay_before_retry"] = $lines[1];
                    continue;
                }
                if ($lines[0] === "helo_name") {
                    $data["helo_name"] = $lines[1];
                    continue;
                }
                if ($lines[0] === "}") {
                    $healt_flag = 0;
                    continue;
                }
            }

        /* MISC_CHECKξ */
        } elseif ($health_flag === 4) {
            if ($lines[0] === "misc_path") {
                $data["misc_path"] = $lines[1];
                continue;
            }
            if ($lines[0] === "misc_timeout") {
                $data["misc_timeout"] = $lines[1];
                continue;
            }
            if ($lines[0] === "}") {
                $healt_flag = 0;
                continue;
            }

        } else {
            /* إ륹åμ */
            if ($lines[0] === "HTTP_GET") {
                $data["h_check"] = 0;
                $health_flag = 1;
                $continue;
            }
            if ($lines[0] === "SSL_GET") {
                $data["h_check"] = 1;
                $health_flag = 1;
                $continue;
            }
            if ($lines[0] === "TCP_CHECK") {
                $data["h_check"] = 2;
                $health_flag = 2;
                $continue;
            }
            if ($lines[0] === "SMTP_CHECK") {
                $data["h_check"] = 3;
                $health_flag = 3;
                $continue;
            }
            if ($lines[0] === "MISC_CHECK") {
                $data["h_check"] = 4;
                $health_flag = 4;
                $continue;
            }

            /* Ťդμ */
            if ($lines[0] === "weight") {
                $data["weight"] = $lines[1];
                continue;
            }

            /* λμ */    
            if ($lines[0] === "}") {
                break;
            }
        }
        if ($result === FALSE) {
            $err_msg = sprintf($msgarr['28085'][SCREEN_MSG], $rsconf);
            $log_msg = sprintf($msgarr['28085'][LOG_MSG], $rsconf);
            return FALSE;
        }
    }
    return TRUE;
}

/**********************************************************
 * rsconf_check_form
 *
 * ͤηå
 *
 * []
 * $post                POSTƤ
 *
 * [֤]
 * TRUE                 
 * FALSE                ۾
 **********************************************************/
function rsconf_check_form ($post)
{
    global $msgarr;
    global $err_msg;
    global $log_msg;
    global $web_conf;

    /* Ťդηå */
    if ($post["weighting"] !== "") {
        if (strlen($post["weighting"]) > 5) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "Ťդ");
            return FALSE;
        }
        if (ctype_digit($post["weighting"]) === FALSE) {
            $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "Ťդ");
            return FALSE;
        }
    }

    /* إ륹åηå */
    if ($post["h_check"] === "") {
        $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "إ륹å");
        return FALSE;
    }
    if (strlen($post["h_check"]) > 1) {
        $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "إ륹å");
        return FALSE;
    }
    if (ctype_digit($post["h_check"]) === FALSE) {
        $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "إ륹å");
        return FALSE;
    }
    if ($post["h_check"] > 4) {
        $err_msg = sprintf($msgarr['28069'][SCREEN_MSG], "إ륹å");
        return FALSE;
    }

    /* η */
    if ($post["h_check"] == 0 || $post["h_check"] == 1) {
        if (check_http($post) === FALSE) {
            return FALSE;
        }

    } elseif ($post["h_check"] == 2) {
        if (check_tcp($post) === FALSE) {
            return FALSE;
        }

    } elseif ($post["h_check"] == 3) {
        if (check_smtp($post) === FALSE) {
            return FALSE;
        }

    } else {
        if (check_misc($post) === FALSE) {
            return FALSE;
        }
    }

    return TRUE;
}


/***********************************************************
 * rsconf_reload_status
 *
 * ɤ뤫ݤη
 *
 * []
 *        $post               IPPORT
 *        $vsiport            С륵ФIP_PORT
 *        $rsiport            ꥢ륵ФIP_POT
 * [֤]
 *        TRUE                 
 *        FALSE               
 **********************************************************/
function rsconf_reload_status ($post, $vsiport ,$rsiport)
{
    global $msgarr;
    global $err_msg;
    global $log_msg;
    global $web_conf;

    $vsfilename = $web_conf["iluka"]["keepalivedbasedir"]."virtual_server.conf";
    $rsfilename = $web_conf["iluka"]["keepalivedbasedir"]."virtual_server/".$vsiport."/real_server.conf";

    /* ꥢ륵Фξֳǧ */
    $result = read_rslist($rsfilename, $rsconfdata);
    if ($result === FALSE) {
        return FALSE;
    }

    $rs_cht = count($rsconfdata);
    for ($i = 0; $i < $rs_cht; $i++) {
        $check = $rsconfdata[$i]["rs_ipaddress"]."_".$rsconfdata[$i]["rs_port"];
        if ($check == $rsiport) {
            $status = $rsconfdata[$i]["rsable"];
            break;
        }
    }
    if ($i === $rs_cht){
        $err_msg = $msgarr['28051'][SCREEN_MSG];
        return FALSE;
    }

    /* оݹԤstatus̵ʤХɤʤ */
    if ($status === "disable") {
        return TRUE;
    }

    /* С륵Фξֳǧ */
    $result = read_vslist($vsfilename, $vsconfdata);
    if ($result === FALSE) {
        return FALSE;
    }
    $vs_cht = count($vsconfdata);

    for ($i = 0; $i < $vs_cht; $i++) {
        $check = $vsconfdata[$i]["ipaddress"]."_".$vsconfdata[$i]["port"];
        if ($check == $vsiport) {
            $status = $vsconfdata[$i]["able"];
            break;
        }
    }

    if ($i === $vs_cht){
        $err_msg = $msgarr['28051'][SCREEN_MSG];
        return FALSE;
    }

    /* оݹԤstatusͭʤХ */
    if ($status === "enable") {
        $result = reload_keepalived();
        if ($result === FALSE)  {
            result_log(OPERATION . ":NG:" . $log_msg, LOG_ERR);
            syserr_display();
            exit(0);
        }
    }

    return TRUE;
}

/***********************************************************
 * check_vs_exists
 *
 * С륵Ф¸߳ǧ
 *
 * []
 *        $post               ݥȤƤ
 * [֤]
 *        0                   
 *        1                   å󥨥顼
 *        2                   Ф¸ߤʤ
 **********************************************************/
function check_vs_exists ($post)
{
    global $msgarr;
    global $err_msg;
    global $log_msg;
    global $web_conf;

    /* IPȥݡȤ錄äƤ뤫γǧ */
    if ($post["ipaddress"] === "" || strlen($post["ipaddress"]) > 39 || !filter_var($post["ipaddress"], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6)) {
        return 1;
    }

/* ݡֹηå */
    if ($_POST["port"] === "" || strlen($_POST["port"]) > 5 || ctype_digit($_POST["port"]) === FALSE || $_POST["port"] > 65535) {
        return 1; 
    }

    /* С륵եȥǥ쥯ȥ¸߳ǧ */
    $vsdir = $web_conf["iluka"]["keepalivedbasedir"]."virtual_server/".$post["ipaddress"]."_".$post["port"];
    $vsfile = $web_conf["iluka"]["keepalivedbasedir"]."virtual_server/".$post["ipaddress"]."_".$post["port"].".conf";

    if (!file_exists($vsdir)) {
        $err_msg = sprintf($msgarr['28084'][SCREEN_MSG], $vsdir);
        $log_msg = sprintf($msgarr['28084'][LOG_MSG], $vsdir);
        return 2;
    }
    if (!file_exists($vsfile)) {
        $err_msg = sprintf($msgarr['28084'][SCREEN_MSG], $vsfile);
        $log_msg = sprintf($msgarr['28084'][LOG_MSG], $vsfile);
        return 2;
    }

    return 0;
}

/***********************************************************
 * check_rs_exists
 *
 * ꥢ륵Ф¸߳ǧ
 *
 * []
 *        $post               ݥȤƤ
 * [֤]
 *        0                   
 *        1                   å󥨥顼
 *        2                   Ф¸ߤʤ
 **********************************************************/
function check_rs_exists ($post)
{
    global $msgarr;
    global $err_msg;
    global $log_msg;
    global $web_conf;

    /* IPȥݡȤ錄äƤ뤫γǧ */
    if ($post["rs_ipaddress"] === "" || strlen($post["rs_ipaddress"]) > 39 || !filter_var($post["rs_ipaddress"], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6)) {
        return 1;
    }

    /* ݡֹηå */
    if ($_POST["rs_port"] === "" || strlen($_POST["rs_port"]) > 5 || ctype_digit($_POST["rs_port"]) === FALSE || $_POST["rs_port"] > 65535) {
        return 1; 
    }

    /* ꥢ륵ե¸߳ǧ */
    $rsfile = $web_conf["iluka"]["keepalivedbasedir"]."virtual_server/".$post["ipaddress"]."_".$post["port"]."/".$post["rs_ipaddress"]."_".$post["rs_port"].".conf";

    if (!file_exists($rsfile)) {
        $err_msg = sprintf($msgarr['28084'][SCREEN_MSG], $rsfile);
        $log_msg = sprintf($msgarr['28084'][LOG_MSG], $rsfile);
        return 2;
    }

    return 0;
}

?>

