#!/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 action = document.getElementsByName(\"action\")[0];
	var redurl = document.getElementsByName(\"redurl\")[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;
		}
	}
	return true;
}
function onCancel(polgrpid)
{
	window.location.href = \"/cgi-bin/Setup/urlfilter_getgrp?policy=\" + polgrpid;
}
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 onActionChanged(obj)
{
        var actarg = document.getElementsByName(\"actarg\")[0];
        if (obj.value == \"prompt\" || obj.value == \"redirect\")
                EnableIt(actarg, true);
	else
                EnableIt(actarg, false);
}
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_action}" in
	"prompt"|"redirect")
		CGI_actarg="actarg=${CGI_actarg}"
		;;
	reqfwd*)
		CGI_actarg=`echo ${CGI_action} | cut -d'-' -f2`
		CGI_actarg="actarg=${CGI_actarg}"
		CGI_action="reqfwd"
		;;
	*)
		CGI_actarg=""
		;;
	esac
	[ "${CGI_natip}" = "" ] && CGI_natip="0"
	errmsg=`${FLOWEYE} urlfilter setrule group=${CGI_policy} id=${CGI_polno} inip=${CGI_inip} \
method=${CGI_method} ext=${CGI_ext} dns=${CGI_dns} natip=${CGI_natip} action=${CGI_action} ${CGI_actarg}`
	if [ "$?" != "0" ]; then
		afm_dialog_msg "ִ:${errmsg}"
	else
		afm_dialog_msg "ɹ!"
		afm_load_page 0 "/cgi-bin/Setup/urlfilter_getgrp?policy=${CGI_policy}"
		exit 0
	fi
else
	for expval in `${FLOWEYE} urlfilter getrule ${CGI_policy} ${CGI_polno}`
	do
		eval "${expval}"
	done
	CGI_intype="${iptype}"
	CGI_inip="${inip}"
	CGI_method="${method}"
	CGI_dns="${dns}"
	CGI_ext="${ext}"
	CGI_natip="${natip}"
	CGI_action="${action}"
	CGI_actarg="${actarg}"
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=\"hlpnatip\" style=\"display:none\">ͨIPСû0ʾ</span>
";
for nameval in `${FLOWEYE} jflow stat`
do
	eval "${nameval}"
done
if [ ${jflow_jos_release} -eq 0 ]; then 
	echo "<span id=\"redirect\" style=\"display:none\">ϢʾURLضֻרҵЧ׼˹Ч</span>"
else 
	echo "<span id=\"redirect\" style=\"display:none\">ҪʾϢҪضURL</span>"
fi
cgi_show_title "HTTPܿ->->Ա༭"
tblexists=`${FLOWEYE} table list`
policyname=`${FLOWEYE} urlfilter listgrp | grep "^${CGI_policy}" | awk '{print $2}'`

echo -n "
<br>
<form method=post onsubmit=\"return beforeAddRule(this)\" action=\"${myself}\">
<table width=600 border=0 cellspacing=0 cellpadding=3 bgcolor=\"#ffffff\">
<tr id=row1>
	<td width=70></td>
        <td width=120 align=left>Աʶ</td>
	<td width=130 align=left><b>${policyname}-->${CGI_polno}</b></td>
	<td width=* align=left></td>
</tr>
</table>
<br>
<table style=\"width:600; border-bottom:1px #787882 solid; color:#000000\">
<tr><td align=left style=\"font-size:11px\"><b>ƥ</b></td></tr>
</table>
<br>
<table width=600 border=0 cellspacing=0 cellpadding=3 bgcolor=\"#ffffff\">
<tr id=row1>
	<td width=70></td>
        <td width=120 align=left>IP</td> 
        <td width=130 align=left>
                <select name=intype style=\"width:100%\" value=\"${CGI_intype}\" onchange=\"onIntypeChanged(this)\"> ";
			case "${CGI_intype}" in
			"any")  
				anyselect="selected"
				;;
			"net")
				netselect="selected"
				;;
			"iprange")
				rngselect="selected"
				;;
			esac
			echo "<option value=\"any\" ${anyselect}>ַ</option>"
			echo "<option value=\"net\" ${netselect}>xxx.xxx.xxx.xxx/nn</option>"
			echo "<option value=\"range\" ${rngselect}>n.n.n.n-m.m.m.m</option>"
			if [ "${tblexists}" != "" ]; then
				if [ "${CGI_intype}" = "table" ]; then
					echo "<option value=\"table\" selected>IPȺ</option>"
				else
					echo "<option value=\"table\">IPȺ</option>"
				fi
			fi 
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>
<tr id=row1>
	<td></td>
        <td align=left>ʷ</td>
        <td><select name=method value=\"${CGI_method}\" style=\"width:100%\">
	";
		if [ "${CGI_method}" = "get" ]; then
			echo "<option value=any>ⷽʽ</option>"
			echo "<option value=get selected>Ϣ</option>"
			echo "<option value=post>Ϣϴ</option>"
		elif [ "${CGI_method}" = "post" ]; then
			echo "<option value=any>ⷽʽ</option>"
			echo "<option value=get>Ϣ</option>"
			echo "<option value=post selected>Ϣϴ</option>"
		else
			echo "<option value=any selected>ⷽʽ</option>"
			echo "<option value=get>Ϣ</option>"
			echo "<option value=post>Ϣϴ</option>"
		fi
	
echo -n "</select>
	</td> 
	<td width=* align=left></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
			if [ "${id}" = "${CGI_dns}" ]; 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><select name=ext value=\"${CGI_ext}\" style=\"width:100%\">
	";
		if [ "${CGI_ext}" = "0" ]; then
			echo "<option value=0 selected></option>"
		else
			echo "<option value=0></option>"
		fi
		${FLOWEYE} urlfilter listext | while read id name theothers
		do
			if [ "${id}" = "${CGI_ext}" ]; 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><input type=text name=natip value=\"${CGI_natip}\" style=\"width:100%\"></input></td>
	<td width=* align=left>
		<a style=\"color:#0000ff\" onmouseover=\"TagToTip('hlpnatip')\" onmouseout=\"UnTip()\">()</a>
	</td>
</tr>
</table>
<br>
<table style=\"width:600; border-bottom:1px #787882 solid; color:#000000\">
<tr><td align=left style=\"font-size:11px\"><b>ִж</b></td></tr>
</table>
<br>
<table width=600 border=0 cellspacing=0 cellpadding=3 bgcolor=\"#ffffff\">
<tr id=row1>
	<td width=70></td>
        <td width=120 align=left>ִж</td>
        <td width=130 align=left>
		<select name=action value=\"${CGI_action}\" style=\"width:100%\" onchange=\"onActionChanged(this)\">
		";
			case "${CGI_action}" in
			"accept"|"permit")
				echo "<option value=\"accept\" selected></option>"
				echo "<option value=\"deny\"></option>"
				echo "<option value=\"prompt\">Ϣʾ</option>"
				echo "<option value=\"redirect\">URLض</option>" 
				;; 
			"deny")
				echo "<option value=\"accept\"></option>"
				echo "<option value=\"deny\" selected></option>"
				echo "<option value=\"prompt\">Ϣʾ</option>"
				echo "<option value=\"redirect\">URLض</option>"
				;;
			"prompt")
				echo "<option value=\"accept\"></option>"
				echo "<option value=\"deny\"></option>"
				echo "<option value=\"prompt\" selected>Ϣʾ</option>"
				echo "<option value=\"redirect\">URLض</option>"
				;;
			"redirect")
				echo "<option value=\"accept\"></option>"
				echo "<option value=\"deny\"></option>"
				echo "<option value=\"prompt\">Ϣʾ</option>"
				echo "<option value=\"redirect\" selected>URLض</option>"
				;;
			esac
			if [ "${CGI_action}" = "reqfwd" ]; then
				echo "<option value=\"accept\"></option>"
				echo "<option value=\"deny\"></option>"
				echo "<option value=\"prompt\">Ϣʾ</option>"
				echo "<option value=\"redirect\">URLض</option>" 
				${FLOWEYE} if list | while read ifname theothers
				do
					if [ "${CGI_actarg}" = "${ifname}" ]; then
						echo "<option value=\"reqfwd-${ifname}\" selected>ת->${ifname}</option>"
					else	
						echo "<option value=\"reqfwd-${ifname}\">ת->${ifname}</option>"
					fi
				done
			else
				${FLOWEYE} if list | while read ifname theothers
				do
					echo "<option value=\"reqfwd-${ifname}\">ת->${ifname}</option>"
				done
			fi
		
echo -n "
		</select>
	</td>
	<td width=* align=left>
		<input type=text name=actarg style=\"width:230\" value=\"${CGI_actarg}\"></input>
		<a style=\"color:#0000ff\" onmouseover=\"TagToTip('redirect')\" onmouseout=\"UnTip()\">[?]</a>
	</td>
</tr>
</table>
<table style=\"width:600; border-bottom:1px #787882 solid; color:#0000ff\">
<tr><td align=right>&nbsp;</td></tr>
</table>
<table style=\"width:600\"> 
<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>
		<input type=hidden name=polno value=\"${CGI_polno}\"></input>
	</td>
</tr>
</table>
</form>
</body>
</html>
";