#!/bin/sh
#This script is created by ssparser automatically. The parser first created by MaoShouyan
printf "Content-type: text/html
Cache-Control: no-cache

"
echo -n "";
. ../common/common.sh 
myself="/cgi-bin/Setup/`basename $0`"

echo -n "
<script type=\"text/javascript\" src=\"/img/common.js\"></script>
<script language=\"javascript\">
function beforeAddRule(frm)
{
	var rngs;
	var polno   = document.getElementsByName(\"polno\")[0];
	var intype  = document.getElementsByName(\"intype\")[0];
	var inipnet = document.getElementsByName(\"inipnet\")[0];
	var iniprng = document.getElementsByName(\"iniprng\")[0];
	var outtype  = document.getElementsByName(\"outtype\")[0];
	var outipnet = document.getElementsByName(\"outipnet\")[0];
	var outiprng = document.getElementsByName(\"outiprng\")[0];
	var action   = document.getElementsByName(\"action\")[0];
	var actip = document.getElementsByName(\"actip\")[0];
	var actproxy = document.getElementsByName(\"actproxy\")[0];
	polno.value = TrimAll(polno.value);
	if (!IsDigitIn(polno.value, 1, 65535)) {
		alert(\"űǽ165535֮һ!\");
		polno.select();
		return false;
	}
	inipnet.value = TrimAll(inipnet.value);
	iniprng.value = TrimAll(iniprng.value);
	if (intype.value == \"net\") {
		if (!IsIPAddr(inipnet.value)) {
			alert(\"ȷIPַʽΪx.x.x.x/n\");
			inipnet.select();
			return false;
		}
	}
	else
	if (intype.value == \"range\") {
		rngs = iniprng.value.split(\"-\");
		if (rngs.length != 2 || !IsIPAddr(rngs[0]) || !IsIPAddr(rngs[1])) {
			alert(\"ȷIPΣʽΪx.x.x.x-y.y.y.y\");
			iniprng.select();
			return false;
		}
	}
	outipnet.value = TrimAll(outipnet.value);
	outiprng.value = TrimAll(outiprng.value);
	if (outtype.value == \"net\") {
		if (!IsIPAddr(outipnet.value)) {
			alert(\"ȷIPַʽΪx.x.x.x/n\");
			outipnet.select();
			return false;
		}
	}
	else
	if (outtype.value == \"range\") {
		rngs = outiprng.value.split(\"-\");
		if (rngs.length != 2 || !IsIPAddr(rngs[0]) || !IsIPAddr(rngs[1])) {
			alert(\"ȷIPΣʽΪx.x.x.x-y.y.y.y\");
			outiprng.select();
			return false;
		}
	}
	if (action.value == \"reply\") {
		actip.value = TrimAll(actip.value);
		if (!IsIPAddr(actip.value)) {
			alert(\"IPַ!\");
			actip.select();
			return false;
		}
	}
	else
	if (action.value == \"rdr\") {
		actproxy.value = TrimAll(actproxy.value);
		if (actproxy.value == \"\") {
			alert(\"ѡ!\");
			return false;
		}
	}
	return true;
}
function onCancel(polgrpid)
{
	window.location.href = \"/cgi-bin/Setup/dns_listrule\";
}
function onIntypeChanged(obj)
{
	var net = document.getElementsByName(\"inipnet\")[0];
	var rng = document.getElementsByName(\"iniprng\")[0];
	var tbl = document.getElementsByName(\"iniptbl\")[0];
	if (obj.value == \"any\") {
		net.style.display = \"none\";
		rng.style.display = \"none\";
		tbl.style.display = \"none\";
	}
	else
	if (obj.value == \"net\") {
		net.style.display = \"block\";
		rng.style.display = \"none\";
		tbl.style.display = \"none\";
	}
	else
	if (obj.value == \"range\") {
		net.style.display = \"none\";
		rng.style.display = \"block\";
		tbl.style.display = \"none\";
	}
	else
	if (obj.value == \"table\") {
		net.style.display = \"none\";
		rng.style.display = \"none\";
		tbl.style.display = \"block\";
	}
}
function onOuttypeChanged(obj)
{
	var net = document.getElementsByName(\"outipnet\")[0];
	var rng = document.getElementsByName(\"outiprng\")[0];
	var tbl = document.getElementsByName(\"outiptbl\")[0];
	if (obj.value == \"any\") {
		net.style.display = \"none\";
		rng.style.display = \"none\";
		tbl.style.display = \"none\";
	}
	else
	if (obj.value == \"net\") {
		net.style.display = \"block\";
		rng.style.display = \"none\";
		tbl.style.display = \"none\";
	}
	else
	if (obj.value == \"range\") {
		net.style.display = \"none\";
		rng.style.display = \"block\";
		tbl.style.display = \"none\";
	}
	else
	if (obj.value == \"table\") {
		net.style.display = \"none\";
		rng.style.display = \"none\";
		tbl.style.display = \"block\";
	}
}
function onActionChanged(obj)
{
	var actip = document.getElementsByName(\"actip\")[0];
	var actproxy = document.getElementsByName(\"actproxy\")[0];
	if (obj.value == \"rdr\") {
		actip.style.display = \"none\";
		actproxy.style.display = \"block\";
	}
	else
	if (obj.value == \"reply\") {
		actip.style.display = \"block\";
		actproxy.style.display = \"none\";
	}
	else {
		actip.style.display = \"none\";
		actproxy.style.display = \"none\";
	}
}
function onInit()
{
	var act = document.getElementsByName(\"action\")[0];
	onActionChanged(act);
}
</script>
";
if [ "${REQUEST_METHOD}" = "POST" ]; then
	case "${CGI_intype}" in
	"any")
		CGI_inip="any"
		;;
	"net")
		CGI_inip="${CGI_inipnet}"
		;;
	"range")
		CGI_inip="${CGI_iniprng}"
		;;
	"table")
		CGI_inip="${CGI_iniptbl}"
		;;
	esac
	case "${CGI_outtype}" in
	"any")
		CGI_outip="any"
		;;
	"net")
		CGI_outip="${CGI_outipnet}"
		;;
	"range")
		CGI_outip="${CGI_outiprng}"
		;;
	"table")
		CGI_outip="${CGI_outiptbl}"
		;;
	esac
	if [ "${CGI_action}" = "rdr" ]; then
		CGI_actarg="${CGI_actproxy}"
	elif [ "${CGI_action}" = "reply" ]; then
		CGI_actarg="${CGI_actip}"
	else
		CGI_actarg="null"
	fi
	errmsg=`${FLOWEYE} dns setrule id=${CGI_polno} inip=${CGI_inip} outip=${CGI_outip} \
dns=${CGI_dns} action=${CGI_action} actarg=${CGI_actarg}`
	if [ "$?" != "0" ]; then
		afm_dialog_msg "ִ:${errmsg}"
	else
		afm_dialog_msg "ɹ!"
		afm_load_page 0 "/cgi-bin/Setup/dns_listrule"
		exit 0
	fi
else
	for tagval in `${FLOWEYE} dns getrule ${CGI_polno}`
        do
                eval "${tagval}"
        done
        CGI_intype="${intype}"
        CGI_inip="${inip}"
        CGI_outtype="${outtype}"
        CGI_outip="${outip}"
        CGI_dns="${dns}"
        CGI_action="${action}"
        CGI_actarg="${actarg}"
	CGI_bridge="${bridge}"	
fi

echo -n "
<body onload=\"onInit()\">
<script type=\"text/javascript\" src=\"/img/wz_tooltip.js\"></script>
<span id=\"hlpno\" style=\"display:none\">ŷΧΪ1~65535,СĹƥ</span>
<span id=\"hlpflow\" style=\"display:none\">ΧΪ0~65535,0ʾû</span>
";
for nameval in `${FLOWEYE} jflow stat`
do
	eval "${nameval}"
done
echo "<span id=\"hlpactarg\" style=\"display:none\">Ϊ\"ض\"\"Ӧ\"ʱ,дضӦIPַ</span>"
policyname=`${FLOWEYE} urlfilter listgrp | grep "^${CGI_policy} " | awk '{print $2}'`
cgi_show_title "HTTPܿ->->Ӳ"
tblexists=`${FLOWEYE} table list`

echo -n "
<br>
<form method=post onsubmit=\"return beforeAddRule(this)\" action=\"${myself}\">
<table width=700 border=0 cellspacing=0 cellpadding=3 bgcolor=\"#ffffff\">
<tr id=row1>
	<td width=50></td>
        <td width=120 align=left>Աʶ</td>
	<td width=130 align=left><input align=right type=text name=polno style=\"width:100%\" value=\"${CGI_polno}\"></input></td>
	<td width=* align=left><a style=\"color:#0000ff\" onmouseover=\"TagToTip('hlpno')\" onmouseout=\"UnTip()\">(1~65535)</a></td>
</tr>
<tr id=row1>
	<td></td>
	<td align=left>·</td>
	<td align=left>
		<select name=bridge style=\"width:100%\" value=\"${CGI_bridge}\"> 
		";
			for bdg in 0 `${FLOWEYE} bridge list`; do
				selit=""
				[ "${CGI_bridge}" = "$bdg" ] && selit="selected"
				if [ $bdg -eq 0 ]; then
					echo "<option value=$bdg ${selit}>·</option>"
				else
					echo "<option value=$bdg ${selit}>$bdg</option>"
				fi
			done
		
echo -n "
		</select>
	</td>
	<td width=*></td>
</tr>
<tr id=row1>
	<td></td>
        <td align=left>Դַ</td>
        <td align=left>
		<select name=intype style=\"width:100%\" value=\"${CGI_intype}\" onchange=\"onIntypeChanged(this)\">
		"; cgi_show_iptype "${CGI_intype}" 
echo -n "
		</select>
	</td>
	<td width=* align=left>
        ";
                rngdisplay="none"
                netdisplay="none"
                tbldisplay="none"
                case "${CGI_intype}" in
                "net")
                        netdisplay="block"
                        ;;
                "iprange")
                        rngdisplay="block"
                        ;;
                "table")
                        tbldisplay="block"
                        ;;
                esac
                inipval="${CGI_inip}"
                [ "${CGI_inip}" = "any" ] && inipval=""
                echo "<input type=text name=inipnet value=\"${inipval}\" style=\"width:120px;display:${netdisplay}\"></input>"
                echo "<input type=text name=iniprng value=\"${inipval}\" style=\"width:160px;display:${rngdisplay}\"></input>"
                echo "<select name=iniptbl value=\"${inipval}\" style=\"width:120px;display:${tbldisplay}\">"
                ${FLOWEYE} table list | while read tblid tblname
                do
                        if [ "${CGI_inip}" = "${tblid}" -o "${CGI_inip}" = "${tblname}" ]; then
                                echo "<option value=\"${tblid}\" selected>${tblname}</option>"
                        else
                                echo "<option value=\"${tblid}\">${tblname}</option>"
                        fi
                done
                echo "</select>"
        
echo -n "
	</td>
</tr>
<tr id=row1>
	<td></td>
        <td align=left>ĿDNS</td>
        <td align=left>
		<select name=outtype style=\"width:100%\" value=\"${CGI_outtype}\" onchange=\"onOuttypeChanged(this)\">
		"; cgi_show_iptype "${CGI_outtype}" 
echo -n "
		</select>
	</td>
	<td width=* align=left>
	";
                rngdisplay="none"
                netdisplay="none"
                tbldisplay="none"
                case "${CGI_outtype}" in
                "net")
                        netdisplay="block"
                        ;;
                "iprange")
                        rngdisplay="block"
                        ;;
                "table")
                        tbldisplay="block"
                        ;;
                esac
                outipval="${CGI_outip}"
                [ "${CGI_outip}" = "any" ] && outipval=""
                echo "<input type=text name=outipnet value=\"${outipval}\" style=\"width:120px;display:${netdisplay}\"></input>"
                echo "<input type=text name=outiprng value=\"${outipval}\" style=\"width:160px;display:${rngdisplay}\"></input>"
                echo "<select name=outiptbl value=\"${outipval}\" style=\"width:120px;display:${tbldisplay}\">"
                ${FLOWEYE} table list | while read tblid tblname
                do
                        if [ "${CGI_outip}" = "${tblid}" -o "${CGI_outip}" = "${tblname}" ]; then
                                echo "<option value=\"${tblid}\" selected>${tblname}</option>"
                        else
                                echo "<option value=\"${tblid}\">${tblname}</option>"
                        fi
                done
                echo "</select>"
	
echo -n "
	</td>
</tr>
<tr id=row1>
	<td></td>
        <td align=left></td>
        <td><select name=dns value=\"${CGI_dns}\" style=\"width:100%\">
	";
		if [ "${CGI_dns}" = "0" ]; then
			echo "<option value=0 selected></option>"
		else
			echo "<option value=0></option>"
		fi
		${FLOWEYE} dns listgrp | while read type id name theothers
		do
			[ "${type}" = "sys" ] && name="*${name}"
			if [ "${CGI_dns}" = "${id}" ]; then
				echo "<option value=${id} selected>${name}</option>"
			else
				echo "<option value=${id}>${name}</option>"
			fi
		done
	
echo -n "
	</select></td> 
</tr>
<tr id=row1>
	<td></td>
        <td align=left>ִж</td>
        <td align=left>
		<select name=action style=\"width:100%\" value=\"${CGI_action}\" onchange=\"onActionChanged(this)\">
		";
			if [ "${CGI_action}" = "pass" ]; then
				echo "<option value=\"pass\" selected>޶</option>"
			else
				echo "<option value=\"pass\">޶</option>"
			fi
			if [ "${CGI_action}" = "deny" ]; then
				echo "<option value=\"deny\" selected></option>"
			else
				echo "<option value=\"deny\"></option>"
			fi
			if [ "${CGI_action}" = "rdr" ]; then
				echo "<option value="rdr" selected>ض</option>"
			else
				echo "<option value="rdr">ض</option>"
			fi
			if [ "${CGI_action}" = "reply" ]; then
				echo "<option value="reply" selected>ٳIP</option>"
			else
				echo "<option value="reply">ٳIP</option>"
			fi
		
echo -n "
		</select>
	</td>
	<td width=* align=left>
		";     if [ "${CGI_action}" = "reply" ]; then	
				actipdisplay="block"
				actproxydisplay="none"
				CGI_actip="${CGI_actarg}"
				CGI_actproxy=""
			elif [ "${CGI_action}" = "rdr" ]; then
				actipdisplay="none"
				actproxydisplay="block"
				CGI_actip=""
				CGI_actproxy="${CGI_actarg}"
			else
				actipdisplay="none"
				actproxydisplay="none"
				CGI_actip=""
				CGI_actproxy=""
			fi
			echo "<input type=text name=actip value=\"${CGI_actip}\" style=\"width:150;display:${actipdisplay}\"></input>"
			echo "<select name=actproxy value=\"${CGI_actproxy}\" style=\"width:120px;display:${actproxydisplay}\">"
			${FLOWEYE} nat listproxy | while read name theothers
			do
				if [ "${CGI_actarg}" = "${name}" ]; then
					echo "<option value=\"${name}\" selected>${name}</option>"
				else
					echo "<option value=\"${name}\">${name}</option>"
				fi
			done
			echo "</select>" 
		
echo -n "
	</td>
</tr>
</table>
<table style=\"width:700; border-bottom:1px #787882 solid; color:#0000ff\">
<tr><td align=right>&nbsp;</td></tr>
</table>
<table style=\"width:700\"> 
<tr>
        <td align=right>
		<input type=submit style=\"width:80\" value=\"ύ\"></input>
		<input type=button style=\"width:80\" value=\"ȡ\" onclick=\"onCancel('${CGI_policy}')\"></input>
		<input type=hidden name=policy value=\"${CGI_policy}\"></input>
	</td>
</tr>
</table>
</form>
</body>
</html>
";