#!/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(\"ѡ!\");
			actproxy.select();
			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 addrule id=${CGI_polno} inip=${CGI_inip} outip=${CGI_outip} \
dns=${CGI_dns} bridge=${CGI_bridge} 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
	[ "${CGI_bridge}" = "" ] && CGI_bridge="0"
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 "DNSܿ->Թ->Ӳ"
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=\"any\" onchange=\"onIntypeChanged(this)\">
			<option value=\"any\" selected>ַ</option>
			<option value=\"net\">xxx.xxx.xxx.xxx/nn</option>
			<option value=\"range\">n.n.n.n-m.m.m.m</option>
		";
			if [ "${tblexists}" != "" ]; then
				echo "<option value=\"table\">IPȺ</option>"
			fi
		
echo -n "
		</select>
	</td>
	<td width=* align=left>
		<input type=text name=inipnet style=\"width:120px;display:none\"></input>
		<input type=text name=iniprng style=\"width:160px;display:none\"></input>
		<select name=iniptbl style=\"width:120px;display:none\">
		";
			${FLOWEYE} table list | while read tblid tblname
			do
				echo "<option value=\"${tblid}\">${tblname}</option>"
			done
		
echo -n "
		</select>
	</td>
</tr>
<tr id=row1>
	<td></td>
        <td align=left>ĿDNS</td>
        <td align=left>
		<select name=outtype style=\"width:100%\" value=\"any\" onchange=\"onOuttypeChanged(this)\">
			<option value=\"any\" selected></option>
			<option value=\"net\">xxx.xxx.xxx.xxx/nn</option>
			<option value=\"range\">n.n.n.n-m.m.m.m</option>
		";
			if [ "${tblexists}" != "" ]; then
				echo "<option value=\"table\">IPȺ</option>"
			fi
		
echo -n "
		</select>
	</td>
	<td width=* align=left>
		<input type=text name=outipnet style=\"width:120px;display:none\"></input>
		<input type=text name=outiprng style=\"width:160px;display:none\"></input>
		<select name=outiptbl style=\"width:120px;display:none\">
		";
			${FLOWEYE} table list | while read tblid tblname
			do
				echo "<option value=\"${tblid}\">${tblname}</option>"
			done
		
echo -n "
		</select>
	</td>
</tr>
<tr id=row1>
	<td></td>
        <td align=left></td>
        <td><select name=dns style=\"width:100%\">
		<option value=0></option>
	";
		${FLOWEYE} dns listgrp | while read type id name theothers
		do
			if [ "${type}" = "usr" ]; then
				echo "<option value=${id}>${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=\"pass\" onchange=\"onActionChanged(this)\">
			<option value=\"pass\" selected>޶</option>
			<option value=\"deny\"></option>
			<option value=\"rdr\">ض</option>
			<option value=\"reply\">ٳΪIP</option>
		</select>
	</td>
	<td width=* align=left>
		<input type=text name=actip style=\"width:150px;display:none\"></input>
		<select name=actproxy style=\"width:120px;display:none\">
		";
			${FLOWEYE} nat listproxy | while read name theothers
			do
				echo "<option value=\"${name}\">${name}</option>"
			done
		
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>
";