#!/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 
if [ "${CGI_policy}" = "" ]; then
	for policy in `${FLOWEYE} policy listgrp | awk '{print $1}'`
	do
		CGI_policy="${policy}"
		break
	done
fi

echo -n "
<script type=\"text/javascript\" src=\"/img/common.js\"></script>
<script languate=javascript>
function onAddPolicy()
{
	window.location.href = \"/cgi-bin/Setup/policy_addgrp\";
}
function onClonePolicy(policy)
{
	window.location.href = \"/cgi-bin/Setup/policy_clonegrp?fromgrp=\" + policy;
}
function onAddRule(policy)
{
	window.location.href = \"/cgi-bin/Setup/policy_addrule?policy=\" + policy;
}
function modifyRule(args)
{
	window.location.href = \"/cgi-bin/Setup/policy_setrule?\" + args;
}
function onSelectPolicy(obj)
{
	window.location.href = \"/cgi-bin/Setup/policy_getgrp?policy=\" + obj.value; 
}
function deleteRule(group, rule)
{
	if (confirm(\"ȷҪɾù?\")) {
		window.location.href = \"/cgi-bin/Setup/policy_getgrp?action=rmvrule\" +
		                       \"&group=\" + group + \"&ruleid=\" + rule;
	}
}
function onDeletePolicy(policy)
{
	if (confirm(\"ȷҪɾ˲?\")) {
		window.location.href = \"/cgi-bin/Setup/policy_getgrp?action=deletegroup\" +
		                       \"&policy=\" + policy;
	}
}
</script>
";
if [ "${CGI_action}" = "deletegroup" ]; then
	errmsg=`${FLOWEYE} policy rmvgrp ${CGI_policy}`
	if [ "$?" != "0" ]; then
		afm_dialog_msg "ִ:${errmsg}"
	else
		afm_dialog_msg "ɹ!"
		afm_load_page 0 "/cgi-bin/Setup/policy_getgrp"
		exit 0
	fi
fi
if [ "${CGI_action}" = "rmvrule" ]; then
	errmsg=`${FLOWEYE} policy rmvrule ${CGI_group} ${CGI_ruleid}`
	if [ "$?" != "0" ]; then
		afm_dialog_msg "ִ:${errmsg}"
	else
		afm_dialog_msg "ɹ!"
	fi
fi
policylist=`${FLOWEYE} policy listgrp`

echo -n "
<body>
"; cgi_show_title "->" 
echo -n "
<br>
<table style=\"width:900; border-bottom:1px #787882 solid; font:bold  14px; color:#0000ff\">
<tr>
	<td style=\"width:100%;font:14px\"><b>˵༭ԵȣŻЧ!</td>
</tr>
</table>
<br>
<table width=900 border=0 cellspacing=1 cellpadding=1> 
<tr id=tblhdr height=23>
	<td width=74 align=center>ѡ</td>
	<td width=362>";
	echo "<select name=policy value=\"${CGI_policy}\" style=\"width:100%;height:22\" onchange=\"onSelectPolicy(this)\">"
	${FLOWEYE} policy listgrp | while read policy policyname
	do
		if [ "${CGI_policy}" = "${policy}" ]; then
			echo "<option value=${policy} selected>${policyname}</option>"
		else
			echo "<option value=${policy}>${policyname}</option>"
		fi
	done 
	if [ "${policylist}" = "" ]; then
		echo "<option value=0>(ûжκβ)</option>"
	fi
	echo "</select>" 
echo -n "
	</td>
	<td width=* align=left>
";
	if [ "${CGI_policy}" != "" ]; then
		echo "&nbsp;&nbsp;&nbsp;<input type=button style=\"width:106;height:23\" onclick=\"onDeletePolicy('${CGI_policy}')\" value=\"ɾ\"></input>&nbsp;&nbsp;<input type=button align=right style=\"width:106;height:23\" value=\"Ʋ>>\" onclick=\"onClonePolicy('${CGI_policy}')\"></input>"
	fi

echo -n "
	&nbsp;&nbsp;<input type=button align=right style=\"width:106;height:23\" value=\">>\" onclick=\"onAddPolicy()\"></input>
	</td>
</tr>
</table>
";
if [ "${CGI_policy}" != "" ]; then
	rowno=1
	echo "<table width=900 border=0 cellspacing=1 cellpadding=1>
<tr id=tblhdr height=22>
        <td width=30  align=center></td>
        <td width=70  align=center>·</td>
	<td width=100 align=center>ַ</td>
	<td width=100 align=center>ַ</td>
        <td width=80  align=center>Э</td>
        <td width=80  align=center></td>
        <td width=80  align=center></td>
        <td width=80  align=center>IP(kb/s)</td>
	<td width=60  align=center>Զ</td>
	<td width=60  align=center>DSCP</td>
	<td width=50  align=center>ȼ</td>
        <td width=50  align=center>ƥ</td>
        <td width=*   align=center><input type=button style=\"width:100%;height:22px\" onclick=\"onAddRule('${CGI_policy}')\" value=\"Ӳ>>\"></input></td>
</tr>"
	idname="row1"
	${FLOWEYE} policy getgrp ${CGI_policy} | \
	while read polno bridge dir appid appname from intype inip to outtype outip action nextstep iprate prclevel \
bps1 bps2 tos pri natip theothers
	do
        	echo "<tr id=${idname}>"
		echo "<td align=center><a style=\"color:#0000ff\" href=\"javascript:modifyRule('group=${CGI_policy}&polno=${polno}&intype=${intype}&inip=${inip}&outtype=${outtype}&outip=${outip}&action=${action}&matchact=${nextstep}&iprate=${iprate}&bridge=${bridge}&dir=${dir}&appid=${appid}&prclevel=${prclevel}&tos=${tos}&priority=${pri}')\">${polno}</a></td>"
		isproxy=`echo ${bridge} | cut -d'-' -f1`
		if [ "${isproxy}" = "PROXY" -o "${isproxy}" = "proxy" ]; then
			bgname="${bridge}->"
		else
			if [ ${bridge} -eq 0 ]; then
				bgname="any"
			else
				bgname="${bridge}->"
			fi
		fi
		case "${dir}" in
		"both") 
			if [ "${bgname}" = "any" ]; then
				echo "<td align=center>·</td>"
			else
				echo "<td align=center>${bgname}˫</td>"
			fi
			;;      
		"in")   
			echo "<td align=center>${bgname}</td>"
			;;      
		"out")  
			echo "<td align=center>${bgname}</td>"
			;;      
		esac
		echo "<td align=center>${inip}</td>"
		echo "<td align=center>${outip}</td>"
		if [ "${appname}" = "any" ]; then
			echo "<td align=center>Э</td>"
		else
        		echo "<td align=center>${appname}</td>"
		fi
		if [ "${natip}" = "0" ]; then
			echo "<td align=center></td>"
		else
			echo "<td align=center>${natip}</td>"
		fi
			
		if [ "${action}" = "deny" ]; then
			echo "<td align=center></td>"
		elif [ "${action}" = "permit" ]; then
			echo "<td align=center></td>"
		else
			echo "<td align=center>${action}</td>"
		fi
        	if [ "${iprate}" = "0" ]; then
                	echo "<td></td>"
        	else
                	echo "<td align=right>${iprate}</td>"
        	fi
		if [ "${prclevel}" = "0" ]; then
			echo "<td align=center></td>"
		else
			echo "<td align=center>${prclevel}ǿ</td>"
		fi
		echo "<td align=center>${tos}</td>"
		echo "<td align=center>${pri}</td>"
		if [ "${nextstep}" = "continue" ]; then
			echo "<td align=center></td>"
		else
			echo "<td align=center>ֹͣ</td>"
		fi
        	echo "<td align=center><a style=\"color:#0000ff\" href=\"javascript:modifyRule('group=${CGI_policy}&polno=${polno}&intype=${intype}&inip=${inip}&outtype=${outtype}&outip=${outip}&action=${action}&matchact=${nextstep}&iprate=${iprate}&bridge=${bridge}&dir=${dir}&appid=${appid}&prclevel=${prclevel}&tos=${tos}&priority=${pri}&natip=${natip}')\">&nbsp;&nbsp;༭&nbsp;&nbsp;<a style="color:#0000ff" href=\"javascript:deleteRule('${CGI_policy}', '${polno}')\">&nbsp;&nbsp;ɾ&nbsp;&nbsp;</a></td>"
        	echo "</tr>"
        	if [ "${idname}" = "row1" ]; then
                	idname="row2"
        	else
                	idname="row1"
        	fi
	done
fi

echo -n "
</body>
</html>
";