#!/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 "";
. ../common/common.sh
myself="/cgi-bin/Monitor/`basename $0`"
[ "${CGI_link}" = "" ] && CGI_link=0
curpolinfo=`${FLOWEYE} policy getcurgrp`
curpolicy=`echo ${curpolinfo} | cut -d' ' -f1`
curpolicyname=`echo ${curpolinfo} | cut -d' ' -f2`

echo -n "
<script type=\"text/javascript\" src=\"/img/common.js\"></script>
<script language=\"javascript\">
function onAddPipe()
{
	var url = \"/cgi-bin/Setup/pipe_add\";
	ShowWindow(url, \"\", 640, 260);
}
function onDeletePipe(pname)
{
	if (confirm(\"ɾͨϵͳɾʹôͨĲԣȷҪɾ\")) 
		window.location.href = \"${myself}?action=deletepipe&pname=\" + pname; 
}
function onModifyPipe(url)
{
	ShowWindow(url, \"\", 460, 360);
}
function onStatPipe(pname)
{
	var url = \"/cgi-bin/Monitor/pipe_stat?pname=\" + pname;
	ShowWindow(url, \"\", 600, 300);
}
function onAddRule(policy) 
{
	var url = \"/cgi-bin/Setup/policy_addrule?policy=\" + policy + \"&policystat=1\";
	window.location.href = url;
}
function modifyRule(group, polno) 
{
	var url = \"/cgi-bin/Setup/policy_setrule?group=\" + group + \"&polno=\" + polno + \"&policystat=1\";
	window.location.href = url;
}
function deleteRule(group, rule, linkid)
{
	if (confirm(\"ȷҪɾù?\")) {
		window.location.href = \"${myself}?action=rmvrule\" + \"&group=\" + group + 
			\"&ruleid=\" + rule + \"&link=\" + linkid;
	}
}
function openPolicyTime()
{
	var url = \"/cgi-bin/Setup/policy_listtime\";
	ShowWindow(url, \"\", 840, 500);
}
function showProxy(proxyname)
{
	var url = \"/cgi-bin/Monitor/proxy_show?proxyname=\" + proxyname;
	ShowWindow(url, \"\", 350, 400); 
}
function showIPGrp(gname)
{
	var url = \"/cgi-bin/Protocol/ipgrp_list?ipgrp=\" + gname;
	ShowWindow(url, \"\", 860, 630); 
}
function showApp(appid, appname)
{
	var url = \"/cgi-bin/Monitor/app_show?appid=\" + appid + \"&title=\" + appname;
	ShowWindow(url, \"\", 700, 540); 
}
function setLink(linkid)
{
	var url = \"/cgi-bin/Setup/policy_setlink?showrate=1&link=\" + linkid;
	if (\"${JOS_RELEASE}\" == \"2\" || \"${JOS_RELEASE}\" == \"3\")
		ShowWindow(url, \"\", 540, 610); 
	else
		ShowWindow(url, \"\", 540, 350); 
}
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;
}
function showWG(wgname)
{
	var url;
	url = \"/cgi-bin/Route/wg_show?name=\" + wgname;
	ShowWindow(url, \"\", 540, 350);
}
function onLinkChanged(obj)
{
	var url;
	url = \"${myself}?link=\" + obj.value;
	window.location.href = url;
}
function ajxs(settings)
{
	var http;
	if (typeof XMLHttpRequest != 'undefined') 
		http = new XMLHttpRequest();
	else {
		try {
			http = new ActiveXObject(\"Msxml2.XMLHTTP\");    
		}catch (e) {
			try {
				http = new ActiveXObject(\"Microsoft.XMLHTTP\");
			} catch (e) {return ;}
		}
	}
	http.open(settings.type, settings.url, true);
	http.setRequestHeader(\"Accept\", \"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\");
	http.setRequestHeader(\"Cache-Control\", \"no-cache\");
	http.setRequestHeader(\"Accept-Language\", \"zh-CN,zh;q=0.8\");
	http.setRequestHeader(\"Content-Type\", \"text/html; charset=GB2312\");
	http.onreadystatechange = function() { 
		if (http.readyState == 4) {
			if (http.status == 200)
				settings.success(http.responseText);
			else
				settings.error(http.status);
		}
	}
	http.send(null);
}
function getbwostats()
{
	var i;
	var outobj, dropobj; 
	ajxs({type: 'POST', 
	       url: '/cgi-bin/Monitor/ajax_bwo_stat', 
	     error: function(status){setTimeout(getbwostats, 3000);},
	   success: function(data) { 
		var d;
		try {
			d = eval('('+data+')');
		} catch(e) {
			setTimeout(getbwostats, 4000);
			return;
		}
		for (i = 0; i < d.length; i++) {
			outobj = document.getElementById(\"bwoout_\" + d[i].id);
			if (outobj != null)
				outobj.innerHTML = d[i].outbps;
			dropobj = document.getElementById(\"bwodrop_\" + d[i].id);
			if (dropobj != null)
				dropobj.innerHTML = d[i].dropbps;
		}
		setTimeout(getbwostats, 4000);
	}});
}
function getpolicystats()
{
	var row;
	ajxs({type: 'POST', 
	       url: '/cgi-bin/Monitor/ajax_policy_stat', 
	     error: function(status){setTimeout(getpolicystats, 4000);},
	   success: function(data) { 
		var d, i;
		var bpsobj;
		try {
			d = eval('('+data+')');
		} catch(e) {
			setTimeout(getpolicystats, 4000);
			return;
		}
		for (i = 0; i < d.length; i++) {
			bpsobj = document.getElementById(\"polbps_\" + d[i].id);
			if (bpsobj != null) 
				bpsobj.innerHTML = d[i].inbps + \"/\" + d[i].outbps;
		}
		
		setTimeout(getpolicystats, 4000);
	}});
}
function onloaddoc()
{
	getbwostats();
	getpolicystats();
}
</script>
";
if [ "${CGI_action}" = "deletepipe" ]; then
	operator_check "${myself}"
	errmsg=`${FLOWEYE} policy rmvbwo ${CGI_pname}`
	if [ "$?" != "0" ]; then
		afm_dialog_msg "ʧ:${errmsg}"
	fi
fi
if [ "${CGI_action}" = "rmvrule" ]; then
	operator_check "${myself}"
	errmsg=`${FLOWEYE} policy rmvrule ${CGI_group} ${CGI_ruleid}`
	if [ "$?" != "0" ]; then
		afm_dialog_msg "ִ:${errmsg}"
	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}"
	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}"
	fi
fi

echo -n "
<body onload=\"onloaddoc()\">
";
cgi_show_title "ǰ->"
if [ ${curpolicy} -eq 0 ]; then
	echo "<br>"
	echo "<table style=\"width:100%; color:#0000ff\">"
	echo "<tr><td align=left>Ŀǰûв鱻ȣ<a style=\"color:#ff0000;font-bold:true;font-size:515;\" href=\"javascript:openPolicyTime()\"><b>˴Ե</b></a></td></tr>"
	echo "</table>"
	echo "<br>"
	echo "</body>"
	echo "</html>"
	exit 0
fi

echo -n "
<br>
<table style=\"width:700; font-bold:true;font-size:14px; color:#CE6C04\">
<tr>
	<td align=left><b>Activeͨ</b></td>
</tr>
</table>
<table width=1220 border=0 cellspacing=1 cellpadding=1>
<tr id=tblhdr>
        <td width=80  align=center></td>
        <td width=260 align=center></td>
        <td width=200 align=center>ͨ(kb/s)</td>
	<td width=200 align=center>(bps)</td>
	<td width=200 align=center>(bps)</td>
	<td width=* align=right><a style=\"color:#0000ff;font-size:14px\" href=\"javascript:onAddPipe()\">ͨ>></a>&nbsp;&nbsp;</td>
</tr>
";
idname="row1"
${FLOWEYE} policy listbwo | while read pid pname prate pbytes psize outbps dropbps active theothers
do 
	[ "${pid}" = "id" ] && continue
	[ "${active}" = "0" ] && continue

echo -n "
	<tr id=${idname}>
	<td align=center>${pid}</td>
	<td align=center>${pname}</td>
	<td align=center>${prate}</td>
	<td align=right><span id=\"bwoout_${pid}\">${outbps}</span></td>
	<td align=right><span id=\"bwodrop_${pid}\">${dropbps}</span></td>
	<td align=right>
	<a style=\"color:#0000ff\" href=\"javascript:onModifyPipe('/cgi-bin/Setup/pipe_edit?pname=${pname}')\">༭</a>
	&nbsp;&nbsp;<a style=\"color:#0000ff\" href=\"javascript:onDeletePipe('${pname}')\">ɾ</a>
	&nbsp;&nbsp;<a style=\"color:#0000ff\" href=\"javascript:onStatPipe('${pname}')\">Ϣ</a>&nbsp;&nbsp;</td>
	</tr>
";
	if [ "${idname}" = "row1" ]; then
        	idname="row2"
	else
        	idname="row1"
	fi
done

echo -n "
</table>
";
if [ "${curpolicy}" = "0" ]; then
	echo "</body></html>"
	exit 0
fi

echo -n "
<br> 
<table style=\"width:1220; font-bold:true;font-size:14px; color:#CE6C04\">
<tr>
<td align=left><b>Active:${curpolicyname}&nbsp;
<a style=\"color:#ff0000;\" href=\"javascript:openPolicyTime()\">[˴޸ĲԵ]</a></b></td>
<td width=* align=right>
	<select name=link style=\"width:90\" onchange=\"onLinkChanged(this)\" value=\"${CGI_link}\">
";
	if [ "${CGI_link}" = "0" ]; then
		echo "<option value=0 selected>·</option>"
	else
		echo "<option value=0>·</option>"
	fi
	${FLOWEYE} policy listlink | while read linkname linkid theothers
	do
		if [ "${linkid}" = "${CGI_link}" ]; then
			echo "<option value=${linkid} selected>${linkname}</option>"
		else
			echo "<option value=${linkid}>${linkname}</option>"
		fi
	done

echo -n "
	</select></td>
</tr>   
</table>
<table width=1220 border=0 cellspacing=1 cellpadding=1>
<tr id=tblhdr>
        <td width=35  align=center></td>
        <td width=70  align=right>·</td>
        <td width=40  align=right></td>
        <td width=40  align=right>VLAN</td>
        <td width=120 align=right>ַ</td>
        <td width=120 align=right>ַ</td>
        <td width=35  align=right>Э</td>
        <td width=80  align=right>Ӧ</td>
        <td width=155 align=right>û</td>
        <td width=80  align=right></td>
        <td width=60  align=right>IP</td>
        <td width=30  align=right>DSCP</td>
        <td width=45  align=right>ȼ</td>
        <td width=45  align=right>ƥ</td>
        <td width=120 align=right>ǰ/</td>
        <td width=*   align=right><a style=\"color:#0000ff;font-size:14px\" href=\"javascript:onAddRule('${curpolicy}')\">Ӳ>></a>&nbsp;</td>
</tr> 
";
idname="row1"
${FLOWEYE} policy getgrp group=${curpolicy} link="${CGI_link}" | \
while read polno bridge dir appid appname proto from intype inip inport to outtype outip outport action nextstep iprate prclevel \
pktbps pktbps2 tos pri natip linkid disabled hasms qqcnt vlan theothers
do 
echo -n "
"; if [ ${disabled} -ne 0 ]; then 
echo -n "
	<tr id=row4 height=23>
"; else 
echo -n "
	<tr id=${idname} height=23>
"; fi 
echo -n "
	<td align=center>${polno}</td>
	"; 
	bdgname="${bridge#_wg.}"
	if [ "${bdgname}" != "${bridge}" ]; then
		echo "<td align=right><a style=\"color:#0000ff\" href=\"javascript:showWG('${bdgname}')\">${bdgname}</a></td>"
	else
		[ "${bdgname}" = "0" ] && bdgname="any"
		if [ ${JOS_RELEASE} -ne 0 -a "${linkid}" != "0" ]; then
			echo "<td align=right><a style=\"color:#0000ff\" href=\"javascript:setLink('${linkid}')\">${bdgname}</a></td>"
		else
			echo "<td align=right>${bdgname}</td>"
		fi
	fi
	
echo -n "
	"; if [ "${dir}" = "in" ]; then 
echo -n "
	<td align=right></td>
	"; elif [ "${dir}" = "out" ]; then 
echo -n "
	<td align=right></td>
	"; else 
echo -n "
	<td align=right>any</td>
	"; fi 
echo -n "
	"; [ "${vlan}" = "0" ] && vlan="&nbsp;" 
echo -n "
	<td align=right>${vlan}</td>
	"; if [ "${intype}" = "table" ]; then 
		if [ "${inport}" = "any" ]; then 
echo -n "
			<td align=right><a style=\"color:#0000ff\" href=\"javascript:showIPGrp('${inip}')\">${inip}</a></td>
	";     else 
echo -n "
			<td align=right><a style=\"color:#0000ff\" href=\"javascript:showIPGrp('${inip}')\">${inip}:${inport}</a></td>
	";	fi 
echo -n "
	"; elif [ "${intype}" = "usr" -o "${intype}" = "pppoe" ]; then 
echo -n "
			<td align=right>${inip}</td>
	"; else 
		if [ "${inport}" = "any" ]; then 
echo -n "
			<td align=right>${inip}</td>
	";     else 
echo -n "
			<td align=right>${inip}:${inport}</td>
	";	fi 
	    fi 
echo -n "
	"; if [ "${outtype}" = "table" ]; then
		if [ "${outport}" = "any" ]; then 
echo -n "
		    <td align=right><a style=\"color:#0000ff\" href=\"javascript:showIPGrp('${outip}')\">${outip}</a></td>
	";	else 
echo -n "
		    <td align=right><a style=\"color:#0000ff\" href=\"javascript:showIPGrp('${outip}')\">${outip}:${outport}</a></td>
	";	fi 
echo -n "
	"; else 
		if [ "${outport}" = "any" ]; then 
echo -n "
			<td align=right>${outip}</td>
	";	else 
echo -n "
			<td align=right>${outip}:${outport}</td>
	";	fi
	    fi 
echo -n "
	<td align=right>${proto}</td>
	"; if [ "${appid}" = "any" ]; then 
echo -n "
	<td align=right>any</td>
	"; else 
echo -n "
	<td align=right><a style=\"color:#0000ff\" href=\"javascript:showApp('${appid}', '${appname}')\">${appname}</a></td>
	"; fi 
echo -n "
	";
		userattrs=""
		[ "${natip}" != "0" ] && userattrs=">=${natip}"
		if [ "${hasms}" != "0" -a "${hasms}" != "" ]; then
			if [ "${userattrs}" != "" ]; then
				userattrs="${userattrs};ƶն>=${hasms}"
			else
				userattrs="ƶն>=${hasms}"
			fi
		fi
		if [ "${qqcnt}" != "0" -a "${qqcnt}" != "" ]; then
			if [ "${userattrs}" != "" ]; then
				userattrs="${userattrs};QQû>=${qqcnt}"
			else
				userattrs="QQû>=${qqcnt}"
			fi
		fi
		[ "${userattrs}" = "" ] && userattrs="&nbsp;"
		echo "<td align=right>${userattrs}</td>"
	
echo -n "
	"; 
	case "${action}" in
	"deny")
		echo "<td align=right><img src=\"/img/no.png\" title=\"\" style=\"margin-right:5px;vertical-align:middle;\" /></td>"
		;;
	"permit")
		echo "<td align=right><img src=\"/img/yes.png\" title=\"\" style=\"margin-right:5px;vertical-align:middle;\" /></td>"
		;;
	ROUTE*)
		pxy=`echo ${action} | cut -d'-' -f2-`
		echo "<td align=right>"
		echo "<a style=\"color:#0000ff\" href=\"javascript:showProxy('${pxy}')\">ת->${pxy}</a>"
		echo "</td>"
		;;
	PROXY*)
		pxy=`echo ${action} | cut -d'-' -f2-`
		echo "<td align=right>"
		echo "<a style=\"color:#0000ff\" href=\"javascript:showProxy('${pxy}')\">${action}</a>"
		echo "</td>"
		;;
	DUP*)
		pxy=`echo ${action} | cut -d'-' -f2-`
		echo "<td align=right>"
		echo "<a style=\"color:#0000ff\" href=\"javascript:showProxy('${pxy}')\">->${pxy}</a>"
		echo "</td>"
		;;
	*)
		echo "<td align=right>${action}</td>"
		;;
	esac
	[ "${iprate}" = "0" ] && iprate="&nbsp;" 
	echo "<td align=right>${iprate}</td>"
	if [ "${tos}" != "0" ]; then 
		echo "<td align=right>${tos}</td>"
	else 
		echo "<td align=right>&nbsp;</td>"
	fi 
	if [ "${pri}" != "0" ]; then 
		echo "<td align=right>${pri}</td>"
	else 
		echo "<td align=right>&nbsp;</td>"
	fi 
	if [ "${nextstep}" = "continue" ]; then 
		echo "<td align=right></td>"
	else 
		echo "<td align=right>ֹͣ</td>"
	fi 
	
echo -n "
	<td style=\"color:#0000ff\" align=right><span id=\"polbps_${polno}\" ${pktbps}/${pktbps2}</span></td>
	<td align=right>
		<img src=\"/img/edit.png\" style=\"margin-top: 3px\" onclick=\"modifyRule('${curpolicy}', '${polno}')\" title=\"޸Ĳ\" />&nbsp;
		<img src=\"/img/delete.png\" style=\"margin-top: 3px\" onclick=\"deleteRule('${curpolicy}', '${polno}', '${linkid}')\" title=\"ɾ\" />&nbsp;
	"; if [ ${disabled} -eq 0 ]; then 
echo -n "
		<img src=\"/img/disable.png\" style=\"margin-top: 3px\" onclick=\"disablePolicy('${curpolicy}', '${polno}')\" title=\"ò\" />&nbsp;
	"; else 
echo -n "
		<img src=\"/img/enable.png\" style=\"margin-top: 3px\" onclick=\"enablePolicy('${curpolicy}', '${polno}')\" title=\"ò\" />&nbsp;
	"; fi 
echo -n "
	</td>
       	</tr>
";
	if [ "${idname}" = "row1" ]; then
		idname="row2"
	else
		idname="row1"
	fi
done

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