#!/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 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 maxtflow = document.getElementsByName(\"maxtflow\")[0];
	var maxuflow = document.getElementsByName(\"maxuflow\")[0];
	var maxflow  = document.getElementsByName(\"maxflow\")[0];
	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;
		}
	}
	maxtflow.value = TrimAll(maxtflow.value);
	if (maxtflow.value == \"\") maxtflow.value = \"0\";
	if (!IsDigitIn(maxtflow.value, 0, 65536)) {
		alert(\"TCP0~65535֮!\");
		maxtflow.select();
		return false;
	}
	
	maxuflow.value = TrimAll(maxuflow.value);
	if (maxuflow.value == \"\") maxuflow.value = \"0\";
	if (!IsDigitIn(maxuflow.value, 0, 65536)) {
		alert(\"UDP0~65535֮!\");
		maxuflow.select();
		return false;
	}
	maxflow.value = TrimAll(maxflow.value);
	if (maxflow.value == \"\") maxflow.value = \"0\";
	if (!IsDigitIn(maxflow.value, 0, 65536)) {
		alert(\"0~65535֮!\");
		maxflow.select();
		return false;
	}
	return true;
}
function OnCancel(polgrpid)
{
	window.location.href = \"/cgi-bin/Setup/conlimit_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 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\";
	}
}
</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
	[ "${CGI_maxtflow}" = "" ] && CGI_maxtflow="0"
	[ "${CGI_maxuflow}" = "" ] && CGI_maxuflow="0"
	[ "${CGI_maxflow}" = "" ]  && CGI_maxflow="0"
	errmsg=`${FLOWEYE} conlimit setrule group=${CGI_policy} id=${CGI_polno} inip=${CGI_inip} \
outip=${CGI_outip} maxtcpflow=${CGI_maxtflow} maxudpflow=${CGI_maxuflow} maxflow=${CGI_maxflow} \
bridge=${CGI_bridge} app=${CGI_appid}`
	if [ "$?" != "0" ]; then
		afm_dialog_msg "ִ:${errmsg}"
	else
		afm_dialog_msg "ɹ!"
		afm_load_page 0 "/cgi-bin/Setup/conlimit_getgrp?policy=${CGI_policy}"
		exit 0
	fi
else
	[ "${CGI_inip}" = "" ] && CGI_inip="any"
	[ "${CGI_outip}" = "" ] && CGI_outip="any"
	[ "${CGI_maxtflow}" = "" ] && CGI_maxtflow="0"
	[ "${CGI_maxuflow}" = "" ] && CGI_maxuflow="0"
	[ "${CGI_maxflow}" = "" ] && CGI_maxflow="0"
	[ "${CGI_exclude80}" = "" ] && CGI_exclude80="0"
fi

echo -n "
<body>
<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>
<span id=\"hlp80\" style=\"display:none\">ѡ,ôϵͳĿ˿Ϊ80TCPӲ</span>
"; 
policyname=`${FLOWEYE} conlimit listgrp | grep "^${CGI_policy} " | awk '{print $2}'`
cgi_show_title "ӿ->->Ӳ"
tblexists=`${FLOWEYE} table list`

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><input align=right type=hidden name=polno value=\"${CGI_polno}\"></input>
		<b>${policyname}->${CGI_polno}</b></td>
	<td width=* align=left></td>
</tr>
<tr id=row1>
	<td></td>
        <td align=left>·</td>
	<td align=left>
	<select name=bridge style=\"width:100%\" value=\"${CGI_bridge}\">
	";
		if [ "${CGI_bridge}" = "0" ]; then
			echo "<option value=0 selected>·</option>"
		else
			echo "<option value=0>·</option>"
		fi
			
	 	for bdg in  `${FLOWEYE} bridge list`; do
			if [ "${CGI_bridge}" = "${bdg}" ]; then
				echo "<option value=${bdg} selected>${bdg}</option>"
			else
				echo "<option value=${bdg}>${bdg}</option>"
			fi
		done
	
echo -n "
	</select></td>
	<td width=* align=left></td>
</tr>
<tr id=row1>
        <td></td>
        <td align=left>IP</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>IP</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>
        ";
                orngdisplay="none"
                onetdisplay="none"
                otbldisplay="none"
                case "${CGI_outtype}" in
                "net")
                        onetdisplay="block"
                        ;;
                "iprange")      
                        orngdisplay="block"
                        ;;
                "table")        
                        otbldisplay="block" 
                        ;;                      
                esac                    
                outipval="${CGI_outip}"   
                [ "${CGI_outip}" = "any" ] && outipval=""
                echo "<input type=text name=outipnet value=\"${outipval}\" style=\"width:120px;display:${onetdisplay}\"></input>"
                echo "<input type=text name=outiprng value=\"${outipval}\" style=\"width:160px;display:${orngdisplay}\"></input>"
                echo "<select name=outiptbl value=\"${outipval}\" style=\"width:120px;display:${otbldisplay}\">"
                ${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>
";
[ "${CGI_appid}" = "" ] && CGI_appid="any"
appname=`${FLOWEYE} app name ${CGI_appid}`

echo -n "
<tr id=row1>
	<td></td>
        <td align=left>ӦЭ</td>
        <td align=left>
		<input type=hidden name=appid value=\"${CGI_appid}\"></input>
		<input type=text name=appname value=\"${appname}\" disabled style=\"width:100%\">
	</td>
	<td width=* class=cssTDArial align=left>
	  <input type=button name=tmp value=\"ѡЭ...\" style=\"width:80px\" onclick=\"selectApp('appid', 'appname')\"></input>
	</td>
</tr>
<tr id=row1>
	<td></td>
        <td align=left>ÿIPTCP</td>
        <td><input type=text name=maxtflow value=\"${CGI_maxtflow}\" style=\"width:100%\"></input></td>
	<td width=* align=left>
		<a style=\"color:#0000ff\" onmouseover=\"TagToTip('hlpflow')\" onmouseout=\"UnTip()\">[?]</a>
	</td>
</tr>
<tr id=row1>
	<td></td>
        <td align=left>ÿIPUDP</td>
        <td><input type=text name=maxuflow value=\"${CGI_maxuflow}\" style=\"width:100%\"></input></td>
	<td width=* align=left>
		<a style=\"color:#0000ff\" onmouseover=\"TagToTip('hlpflow')\" onmouseout=\"UnTip()\">[?]</a>
	</td>
</tr>
<tr id=row1>
	<td></td>
        <td align=left>ÿIP</td>
        <td><input type=text name=maxflow value=\"${CGI_maxflow}\" style=\"width:100%\"></input></td>
	<td width=* align=left>
		<a style=\"color:#0000ff\" onmouseover=\"TagToTip('hlpflow')\" 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=group  value=\"${CGI_policy}\"></input>
	</td>
</tr>
</table>
</form>
</body>
</html>
";