#!/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 
myself="/cgi-bin/Protocol/`basename $0`"
if [ "${CGI_appid}" = "" ]; then
	for appid in `${FLOWEYE} app showtree all | grep " usrapp" | awk '{print $3}'`
	do
		CGI_appid="${appid}"
		break
	done
fi

echo -n "
<script type=\"text/javascript\" src=\"/img/common.js\"></script>
<script languate=javascript>
function onAddApp() 
{
	window.location.href = \"/cgi-bin/Protocol/usrapp_add\";
}
function onEditApp(appid, appname) 
{
	window.location.href = \"/cgi-bin/Protocol/usrapp_edit?appid=\" + appid + \"&appname=\" + appname;
}
function onDeleteApp(appid)
{
	if (confirm(\"ɾЭ飬ôͬйصĲԶɾ,ȷҪɾ?\")) 
		window.location.href = \"/cgi-bin/Protocol/usrapp_list?action=delete\" + \"&appid=\" + appid;
}
function IsPortValid(ports)
{
        var ar;
        if (ports == \"\")
                return true;
        ar = ports.split(\",\");
        for (i = 0; i < ar.length; i++) {
                if (!IsDigitIn(ar[i], 1, 65535))
                        return false;
        }
        return true;
}
function beforeEditApp(frm)
{
        var flowttl = document.getElementsByName(\"flowttl\")[0];
        var nodettl = document.getElementsByName(\"nodettl\")[0];
        var tports  = document.getElementsByName(\"tports\")[0];
        var uports  = document.getElementsByName(\"uports\")[0];
        flowttl.value = TrimAll(flowttl.value);
        if (!IsDigitIn(flowttl.value, 30, 65535)) {
                alert(\"ڱ30~65535!\");
                flowttl.select();
                return false;
        }
        nodettl.value = TrimAll(nodettl.value);
        if (!IsDigitIn(nodettl.value, 30, 65535)) {
                alert(\"ڵڱ30~65535!\");
                nodettl.select();
                return false;
        }
	if (tports.value != \"\") {        
		tports.value = TrimAll(tports.value);
        	if (!IsPortValid(tports.value)) {
                	alert(\"˿ڲȷ˿֮ԶŸ!\");
                	tports.select();
                	return false;
		}
        }
	if (uports.value != \"\") {
        	uports.value = TrimAll(uports.value);
        	if (!IsPortValid(uports.value)) {
                	alert(\"˿ڲȷ˿֮ԶŸ!\");
                	uports.select();
                	return false;
		}
        }
        return true; 
}
</script>
";
if [ "${CGI_action}" = "delete" ]; then
	errmsg=`${FLOWEYE} app remove ${CGI_appid}`
	if [ "$?" != "0" ]; then
		afm_dialog_msg "ִ:${errmsg}"
	else
		afm_dialog_msg "ɹ!"
		CGI_appid=""
		for appid in `${FLOWEYE} app list2 | grep "^custom" | awk '{print $2}'`
		do
			CGI_appid="${appid}"
			break
		done
	fi
fi

echo -n "
<body>
"; cgi_show_title "->ԶЭ" 
echo -n "
<br>
<table width=900 border=0 cellspacing=1 cellpadding=1>
<tr id=tblhdr height=22>
	<td width=120 align=center>Э</td>
        <td width=200 align=center>TCP˿</td>
	<td width=200 align=center>UDP˿</td>
	<td width=100 align=center>ڵ</td>
	<td width=100 align=center></td>
        <td width=*   align=center>
		<input type=button style=\"width:80;height:20px\" value=\"Э>>\" onclick=\"onAddApp()\"></input>
	</td>
</tr>
";
tdid="row1"
${FLOWEYE} app showtree all | while read tmp1 tmp2 appid appname type 
do
	[ "${type}" != "usrapp" ] && continue
	output=`${FLOWEYE} app get ${appid}`
	if [ "${output}" != "" ]; then
		CGI_appname=""
		CGI_appcname=""
		CGI_flowttl=""
		CGI_tports=""
		CGI_uports=""
		for nameval in ${output}; do
			name=`echo ${nameval} | cut -d'=' -f1`
			value=`echo ${nameval} | cut -d'=' -f2`
			case "${name}" in
			"name")
				CGI_appname="${value}"
				;;
			"cname")
				CGI_appcname="${value}"
				;;
			"flowttl")
				CGI_flowttl="${value}"
				;;
			"nodettl")
				CGI_nodettl="${value}"
				;;
			"tports")
				CGI_tports="${value}"
				[ "${CGI_tports}" = "NONE" ] && CGI_tports=""
				;;
			"uports")
				CGI_uports="${value}"
				[ "${CGI_uports}" = "NONE" ] && CGI_uports=""
				;;
			esac
		done 
	
		echo "<tr id=${tdid}>"	
		echo "<td id=${tdid} align=center><a style=\"color:#0000ff\" href=\"javascript:onEditApp('${appid}','${appname}')\">${CGI_appcname}</a></td>"
		echo "<td id=${tdid} align=center>${CGI_tports}</td>"
		echo "<td id=${tdid} align=center>${CGI_uports}</td>"
		echo "<td id=${tdid} align=center>${CGI_nodettl}</td>"
		echo "<td id=${tdid} align=center>${CGI_flowttl}</td>"
		echo "<td id=${tdid} align=center>&nbsp;&nbsp;&nbsp;<a style=\"color:#0000ff\" href=\"javascript:onEditApp('${appid}','${appname}')\">༭</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a style=\"color:#0000ff\" href=\"javascript:onDeleteApp('${appid}')\">ɾ</a></td>"
		echo "</tr>"
		if [ "${tdid}" = "row1" ]; then
			tdid="row2"
		else
			tdid="row1"
		fi
	fi
done

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