#!/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} urlfilter 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/urlfilter_addgrp\";
}
function onClonePolicy(policy)
{
	window.location.href = \"/cgi-bin/Setup/urlfilter_clonegrp?fromgrp=${CGI_policy}\";
}
function onAddRule(policy)
{ 
	window.location.href = \"/cgi-bin/Setup/urlfilter_addrule?policy=\" + policy;
}
function modifyRule(group, polno) 
{
	window.location.href = \"/cgi-bin/Setup/urlfilter_setrule?policy=\" + group + \"&polno=\" + polno;
}
function onSelectPolicy(obj)
{
	window.location.href = \"/cgi-bin/Setup/urlfilter_getgrp?policy=\" + obj.value; 
}
function deleteRule(group, rule)
{
	if (confirm(\"ȷҪɾù?\")) {
		window.location.href = \"/cgi-bin/Setup/urlfilter_getgrp?action=rmvrule\" +
		                       \"&group=\" + group + \"&ruleid=\" + rule;
	}
}
function onDeletePolicy(policy)
{
	if (confirm(\"ȷҪɾ˲?\")) {
		window.location.href = \"/cgi-bin/Setup/urlfilter_getgrp?action=deletegroup&\" +
		                       \"policy=\" + policy;
	}
}
function dnsEdit(grpname)
{
        var url = \"/cgi-bin/Protocol/urldnsgrp_edit?grpname=\" + grpname;
        ShowWindow(url, \"\", 840, 560);
}
</script>
";
if [ "${CGI_action}" = "deletegroup" ]; then
	errmsg=`${FLOWEYE} urlfilter rmvgrp ${CGI_policy}`
	if [ "$?" != "0" ]; then
		afm_dialog_msg "ִ:${errmsg}"
	else
		afm_dialog_msg "ɹ!"
		afm_load_page 0 "/cgi-bin/Setup/urlfilter_getgrp"
		exit 0
	fi
fi
if [ "${CGI_action}" = "rmvrule" ]; then
	errmsg=`${FLOWEYE} urlfilter rmvrule ${CGI_group} ${CGI_ruleid}`
	if [ "$?" != "0" ]; then
		afm_dialog_msg "ִ:${errmsg}"
	else
		afm_dialog_msg "ɹ!"
		CGI_policy="${CGI_group}"
	fi
fi
policylist=`${FLOWEYE} urlfilter listgrp`

echo -n "
<body>
"; cgi_show_title "HTTPܿ->" 
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=402>";
	echo "<select name=policy value=\"${CGI_policy}\" style=\"width:100%;height:22\" onchange=\"onSelectPolicy(this)\">"
	${FLOWEYE} urlfilter 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;<input type=button style=\"width:100;height:23\" onclick=\"onDeletePolicy('${CGI_policy}')\" value=\"ɾ\"></input>&nbsp;<input type=button style=\"width:100;height:23\" value=\"Ʋ>>\" onclick=\"onClonePolicy('${CGI_policy}')\"></input>"
	fi

echo -n "
	&nbsp;<input type=button style=\"width:100;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=40  align=center></td>
	<td width=120 align=center>ַ</td>
	<td width=80  align=center>ʷʽ</td>
	<td width=120 align=center>Ŀ</td>
	<td width=110 align=center>ļ</td>
	<td width=100 align=center>û</td>
	<td width=240 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} urlfilter getgrp ${CGI_policy} | \
	while read polno intype inip method dnstype dns ext natip action pkts actarg others 
	do
        	echo "<tr id=${idname}>"
        	echo "<td align=center><a style=\"color:#0000ff\" href=\"javascript:modifyRule('${CGI_policy}', '${polno}')\">${polno}</a></td>"
		echo "<td align=center>${inip}</td>"
		if [ "${method}" = "any" ]; then
			method="ⷽʽ"
		elif [ "${method}" = "get" ]; then
			method="Ϣ"
		elif [ "${method}" = "post" ]; then
			method="Ϣϴ"
		else
			method="δ֪"
		fi
		echo "<td align=center>${method}</td>"
		if [ "${dns}" = "any" ]; then
			echo "<td align=center></td>"
		else
			if [ "${dnstype}" = "usr" ]; then
			    echo "<td align=center><a style=\"color:#0000ff\" href=\"javascript:dnsEdit('${dns}')\">${dns}</a></td>"
			else
			    echo "<td align=center>*${dns}</td>"
			fi
		fi
		[ "${ext}" = "any" ] && ext=""
		echo "<td align=center>${ext}</td>"
		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}" = "accept" -o "${action}" = "permit" ]; then
			echo "<td align=center></td>"
		elif [ "${action}" = "reqfwd" ]; then
			echo "<td align=center>ת->${actarg}</td>"
		elif [ "${action}" = "prompt" ]; then
			echo "<td align=center>ʾϢ->\"${actarg}\"</td>"
		else
			echo "<td align=center>ض->${actarg}</td>"
		fi
        	echo "<td align=center><a style=\"color:#0000ff\" href=\"javascript:modifyRule('${CGI_policy}', '${polno}')\">&nbsp;&nbsp;༭&nbsp;&nbsp;</a><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>
";