#!/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
myself="/cgi-bin/Setup/`basename $0`"

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(group, polno)
{
	var url;
	url = \"/cgi-bin/Setup/policy_setrule?group=\" + group + \"&polno=\" + polno;
	window.location.href = url;
}
function onSelectPolicy(obj)
{
	window.location.href = \"${myself}?policy=\" + obj.value; 
}
function deleteRule(group, rule)
{
	if (confirm(\"ȷҪɾù?\")) {
		window.location.href = \"${myself}?action=rmvrule\" +
		                       \"&policy=\" + group + \"&ruleid=\" + rule;
	}
}
function onDeletePolicy(policy)
{
	if (confirm(\"ȷҪɾ˲?\")) 
		window.location.href = \"${myself}?action=deletegroup\" + \"&policy=\" + policy;
}
function enablePolicy(grp, polno)
{
        var url;
        url = \"${myself}?action=enable&link=${CGI_link}\" + \"&group=\" + grp + \"&policyid=\" + polno;
        window.location.href = url;
}
function disablePolicy(grp, polno)
{
        var url;
        url = \"${myself}?action=disable&link=${CGI_link}\" + \"&group=\" + grp + \"&policyid=\" + polno;
        window.location.href = url;
}
</script>
";
if [ "${CGI_action}" = "deletegroup" ]; then
	operator_check "${myself}?policy=${CGI_policy}"
	errmsg=`${FLOWEYE} policy rmvgrp ${CGI_policy}`
	if [ "$?" != "0" ]; then
		afm_dialog_msg "ִ:${errmsg}"
	else
		WEB_LOGGER "ɾ" "group=${CGI_policy}"
		afm_dialog_msg "ɹ!"
		afm_load_page 0 "${myself}"
		exit 0
	fi
fi
if [ "${CGI_action}" = "rmvrule" ]; then
	operator_check "${myself}?policy=${CGI_policy}"
	errmsg=`${FLOWEYE} policy rmvrule ${CGI_policy} ${CGI_ruleid}`
	if [ "$?" != "0" ]; then
		afm_dialog_msg "ִ:${errmsg}"
	else
		WEB_LOGGER "ɾ" "group=${CGI_policy},id=${CGI_ruleid}"
		afm_dialog_msg "ɹ!"
	fi
fi
if [ "${CGI_action}" = "enable" ]; then
	operator_check "${myself}"
	errmsg=`${FLOWEYE} policy disable group=${CGI_group} id=${CGI_policyid} disable=0`
	if [ "$?" != "0" ]; then
		afm_dialog_msg "ʧ:${errmsg}"
	else
		afm_dialog_msg "ɹ!"
	fi
fi
if [ "${CGI_action}" = "disable" ]; then
	operator_check "${myself}"
	errmsg=`${FLOWEYE} policy disable group=${CGI_group} id=${CGI_policyid} disable=1`
	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:1080; border-bottom:1px #787882 solid; font-bold:true;font-size:15px; color:#0000ff\">
<tr>
	<td><b>˵༭<a style=\"color:#ff0000\" href=\"/cgi-bin/Setup/policy_listtime\">Ե</a>ŻЧ!</td>
</tr>
</table>
<br>
<table width=1080 border=0 cellspacing=1 cellpadding=1> 
<tr id=tblhdr height=22>
	<td width=72 align=center>ѡ</td>
	<td width=180>";
	echo "<select name=policy value=\"${CGI_policy}\" style=\"width:100%;height:19\" 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=right>
";
	if [ "${CGI_policy}" != "" ]; then
		echo "&nbsp;&nbsp;&nbsp;<input type=button style=\"width:106;height:21\" onclick=\"onDeletePolicy('${CGI_policy}')\" value=\"ɾ\"></input>&nbsp;&nbsp;<input type=button align=right style=\"width:106;height:21\" value=\"Ʋ>>\" onclick=\"onClonePolicy('${CGI_policy}')\"></input>"
	fi

echo -n "
	&nbsp;&nbsp;<input type=button align=right style=\"width:106;height:21\" value=\">>\" onclick=\"onAddPolicy()\"></input>
	</td>
</tr>
</table>
";
if [ "${CGI_policy}" != "" ]; then
	rowno=1 
echo -n "
	<table width=1080 border=0 cellspacing=1 cellpadding=1>
	<tr id=tblhdr height=22>
        <td width=40  align=center></td>
        <td width=80  align=center>·</td>
        <td width=60  align=center></td>
	<td width=120 align=center>ַ</td>
	<td width=120 align=center>ַ</td>
        <td width=40  align=center>Э</td>
        <td width=100  align=center>Ӧ</td>
        <td width=40  align=center></td>
        <td width=60  align=center>ƶ豸</td>
        <td width=80  align=center></td>
        <td width=60  align=center>IP</td>
	<td width=40  align=center>DSCP</td>
	<td width=40  align=center>ȼ</td>
        <td width=40  align=center>ƥ</td>
        <td width=*   align=right>
		<a style=\"color:#0000ff;font-size:14px\" href=\"javascript:onAddRule('${CGI_policy}')\">Ӳ>></a>&nbsp;&nbsp;
	</td>
	</tr>
";
	idname="row1"
	${FLOWEYE} policy getgrp group=${CGI_policy} | \
	while read polno bridge dir appid appname proto from intype inip inport to outtype outip outport \
action nextstep iprate prclevel bps1 bps2 tos pri natip linkid disabled hasms theothers
	do 
echo -n "
	"; if [ ${disabled} -ne 0 ]; then 
echo -n "
        	<tr id=row4>
	"; else 
echo -n "
        	<tr id=${idname}>
	"; fi 
echo -n "
		<td align=center>${polno}</td>
		"; 
			bdgname="${bridge#_wg.}"
			[ "${bdgname}" = "0" ] && bdgname="any" 
		
echo -n "
		<td align=center>${bdgname}</td>
		"; if [ "${dir}" = "both" ]; then 
echo -n "
		<td align=center>any</td>
		"; elif [ "${dir}" = "in" ]; then 
echo -n "
		<td align=center></td>
		"; else 
echo -n "
		<td align=center></td>
		"; fi 
echo -n "
		"; if [ "${inport}" = "any" ]; then 
echo -n "
		<td align=center>${inip}</td>
		"; else 
echo -n "
		<td align=center>${inip}:${inport}</td>
		"; fi 
echo -n "
		"; if [ "${outport}" = "any" ]; then 
echo -n "
		<td align=center>${outip}</td>
		"; else 
echo -n "
		<td align=center>${outip}:${outport}</td>
		"; fi 
echo -n "
		<td align=center>${proto}</td>
        	<td align=center>${appname}</td>
		"; if [ "${natip}" = "0" ]; then 
echo -n "
		<td align=center></td>
		"; else 
echo -n "
		<td align=center>${natip}</td>
		"; fi 
echo -n "
		"; if [ "${hasms}" = "0" ]; then 
echo -n "
		<td align=center></td>
		"; else 
echo -n "
		<td align=center></td>
		"; fi 
echo -n "
	";
	case "${action}" in
	"deny")
		echo "<td align=center></td>"
		;;
	"permit")
		echo "<td align=center></td>"
		;;
	ROUTE*)
		pxy=`echo ${action} | cut -d'-' -f2-`
		echo "<td align=center>ת->${pxy}</td>"
		;;
	PROXY*)
		echo "<td align=center>${action}</td>"
		;;
	DUP*)
		pxy=`echo ${action} | cut -d'-' -f2`
		echo "<td align=center>ݾ->${pxy}</td>"
		;;
	*)
		echo "<td align=center>${action}</td>"
		;;
	esac
	
echo -n "
        	"; if [ "${iprate}" = "0" ]; then 
echo -n "
               	<td></td>
        	"; else 
echo -n "
               	<td align=center>${iprate}</td>
        	"; fi 
echo -n "
		<td align=center>${tos}</td>
		<td align=center>${pri}</td>
		"; if [ "${nextstep}" = "continue" ]; then 
echo -n "
		<td align=center></td>
		"; else 
echo -n "
		<td align=center>ֹͣ</td>
		"; fi 
echo -n "
        	<td align=center>
		<a style=\"color:#0000ff\" href=\"javascript:modifyRule('${CGI_policy}', '${polno}')\">&nbsp;༭&nbsp;</a>
		<a style=\"color:#0000ff\" href=\"javascript:deleteRule('${CGI_policy}', '${polno}')\">&nbsp;ɾ&nbsp;</a>
		"; if [ ${disabled} -eq 0 ]; then 
echo -n "
                <a style=\"color:#0000ff\" href=\"javascript:disablePolicy('${CGI_policy}', '${polno}')\"></a>
		"; else 
echo -n "
                <a style=\"color:#ff0000\" href=\"javascript:enablePolicy('${CGI_policy}', '${polno}')\"></a>
		"; fi 
echo -n "
		</td>
        	</tr>
";
        	if [ "${idname}" = "row1" ]; then
                	idname="row2"
        	else
                	idname="row1"
        	fi
	done
fi

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