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

"
echo -n "";
CGI_browsertitle="iXCache"
. ../../common/common.sh 
cfgroot="/cgi-bin/App/ixcache"
myself="${cfgroot}/`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 vlan = document.getElementsByName(\"vlan\")[0];
	var ftype = document.getElementsByName(\"ftype\")[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;
		}
	}
	vlan.value = TrimAll(vlan.value);
	ftype.value = TrimAll(ftype.value);
	if (ftype.value == \"ANY\")
		ftype.value = \"any\";
	else
	if (ftype.value == \"NULL\")
		ftype.value = \"null\";
	return true;
}
function onCancel()
{
	window.location.href = \"/cgi-bin/App/ixcache/webmain\";
}
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
	operator_check "/cgi-bin/App/ixcache/webmain"
	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
	cmdargs="id=${CGI_polno} inip=${CGI_inip} outip=${CGI_outip} dns=${CGI_dns}"
	[ "${CGI_vlan}" != "" ] && cmdargs="${cmdargs} vlan=${CGI_vlan}"
	[ "${CGI_inif}" != "" ] && cmdargs="${cmdargs} inif=${CGI_inif}"
	cmdargs="${cmdargs} ftype=${CGI_ftype} mirror=${CGI_mirror} cache=${CGI_cache}"
	errmsg=`${FLOWEYE} ixcpolicy set ${cmdargs}`
	if [ "$?" != "0" ]; then
		afm_dialog_msg "ִ:${errmsg}"
	else
		WEB_LOGGER "޸Ļǣ" "id=${CGI_polno}"
		afm_load_page 0 "/cgi-bin/App/ixcache/webmain"
		exit 0
	fi
else
	for nameval in `${FLOWEYE} ixcpolicy get id=${CGI_polno}`
	do
		eval "CGI_${nameval}"
	done
fi

echo -n "
<body>
";
cgi_show_title "ǣ->޸Ĳ[${CGI_polno}]"
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=20></td>
	<td width=100 align=left>Դӿ</td>
	<td width=140 align=left>
		<select name=inif style=\"width:140\" value=\"${CGI_inif}\"> 
		";
			if [ "${CGI_inif}" = "any" ]; then
				echo "<option value=any selected>ӿ</option>"
			else
				echo "<option value=any>ӿ</option>"
			fi
			${FLOWEYE} if list | while read inif mode zone theothers
			do
				[ "${zone}" != "inside" ] && continue
				if [ "${CGI_inif}" = "${inif}" ]; then
					echo "<option value=${inif} selected>${inif}</option>"
				else
					echo "<option value=${inif}>${inif}</option>"
				fi
			done
			for proxy in `${FLOWEYE} nat listproxy type=routerif | awk '{print $3}'`
			do
				if [ "${CGI_inif}" = "${proxy}" ]; then
					echo "<option value=${proxy} selected}>${proxy}</option>"
				else
					echo "<option value=${proxy}>${proxy}</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:140\" 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:150px;display:${netdisplay}\"></input>"
                echo "<input type=text name=iniprng value=\"${inipval}\" style=\"width:150px;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>Ŀַ</td>
        <td align=left>
                <select name=outtype style=\"width:140\" 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:150px;display:${netdisplay}\"></input>"
                echo "<input type=text name=outiprng value=\"${outipval}\" style=\"width:150px;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>VLAN</td>
        <td> <input type=text name=vlan value=\"${CGI_vlan}\" style=\"width:140\"></input></td>
	<td width=* align=left>(1010-20,0ʾԴ)</td>
</tr>
<tr id=row1>
	<td></td>
        <td align=left>ļ</td>
        <td><input type=text name=ftype style=\"width:140\" value=\"${CGI_ftype}\" /></td>
	<td width=*>(֮ԶŸflv,mp4, nullʾ͵ļ,anyʾ)</td>
</tr>
<tr id=row1>
	<td></td>
        <td align=left>ǣģʽ</td>
        <td><select name=mirror style=\"width:140\" value=\"${CGI_mirror}\">
	    "; if [ "${CGI_mirror}" = "1" ]; then 
echo -n "
			<option value=0>ǣ</option>
			<option value=1 selected>ݾ</option>
	    "; else 
echo -n "
			<option value=0 selected>ǣ</option>
			<option value=1>ݾ</option>
	    "; fi 
echo -n "
	</select></td>
	<td width=*>(ʹþģʽת淢302)</td>
</tr>
<tr id=row1>
	<td></td>
        <td align=left></td>
        <td align=left>
		<select name=cache style=\"width:140\" value=\"${CGI_cache}\">
		"; 
		    if [ "${CGI_cache}" = "NULL" ]; then
			echo "<option value=\"NULL\" selected>ǣ</option>"
		    else
			echo "<option value=\"NULL\">ǣ</option>"
		    fi
		    for cache in `${FLOWEYE} ixcache list | awk '{print $2}'`
		    do
			if [ "${CGI_cache}" = "${cache}" ]; then
				echo "<option value=\"${cache}\" selected>${cache}</option>"
			else
				echo "<option value=\"${cache}\">${cache}</option>"
			fi
		    done
                    ${FLOWEYE} ixcgroup list | while read grpid grpname theothers
                    do
                        if [ "${CGI_cache}" = "${grpname}" ]; then
                                echo "<option value=\"grp:${grpid}\" selected>${grpname}</option>"
                        else
                                echo "<option value=\"grp:${grpid}\">${grpname}</option>"
                        fi
                    done
		
echo -n "</select>
	</td>
	<td width=*></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 id=newbtn style=\"width:80\" value=\"ύ\"></input>
		<input type=button id=newbtn style=\"width:80\" value=\"ȡ\" onclick=\"onCancel()\"></input>
		<input type=hidden name=polno value=\"${CGI_polno}\" />
	</td>
</tr>
</table>
</form>
</body>
</html>
";