#!/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`"
MOD_TAB_LIST="#/cgi-bin/Protocol/usrapp_edit?appid=${CGI_appid}&appname=${CGI_appname} ڵ#/cgi-bin/Protocol/usrapp_node?appid=${CGI_appid}&appname=${CGI_appname}"

echo -n "
<script type=\"text/javascript\" src=\"/img/common.js\"></script>
<script type=\"text/javascript\">
function onCancel()
{
	window.location.href = \"/cgi-bin/Protocol/usrapp_list\";
}
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 [ "${REQUEST_METHOD}" = "POST" ]; then
	yes=1
	errmsg=`${FLOWEYE} app set ${CGI_appid} flowttl=${CGI_flowttl} nodettl=${CGI_nodettl}`
	if [ "$?" != "0" ]; then
		afm_dialog_msg "Ӻͽڵڳִ:${errmsg}"
		yes=0
	fi
	if [ ${yes} -ne 0 ]; then
		if [ "${CGI_tports}" = "" -a "${CGI_uports}" = "" ]; then
			errmsg=`${FLOWEYE} port clear ${CGI_appid}`
			if [ "$?" != "0" ]; then
				afm_dialog_msg "˿ʧ:${errmsg}"
			else
				afm_dialog_msg "ɹ!"
			fi
		else
			args=""
			[ "${CGI_tports}" != "" ] && args="tcp=${CGI_tports}"
			[ "${CGI_uports}" != "" ] && args="${args} udp=${CGI_uports}"
			errmsg=`${FLOWEYE} port load ${CGI_appid} ${args}`
			if [ "$?" != "0" ]; then
				afm_dialog_msg "ض˿ʧ:${errmsg}!"
			else
				afm_dialog_msg "ɹ!"
			fi
		fi
	fi
fi

echo -n "
</head>
<body>
"; cgi_show_title "ԶЭ->${CGI_appname}" 
echo -n "
<br>
"; cgi_print_mod_header "" 813 
echo -n "
<br>
";
output=`${FLOWEYE} app get ${CGI_appid}`
if [ "${output}" = "" ]; then
	afm_dialog_msg "Э${CGI_appid}!"
	echo "</body></html>"
	exit 0
fi
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 -n "
<form method=post onsubmit=\"return beforeEditApp(this)\" action=\"${myself}\">
<table width=800 border=0 cellspacing=1 cellpadding=2> 
<tr id=row1>
        <td width=40></td>
        <td width=100></td>
        <td width=* align=left>
                <input type=text name=flowttl value=\"${CGI_flowttl}\" style=\"width:120;height:21\"></input>
                &nbsp;(,ΧΪ30~65535)
        </td>
</tr>
<tr id=row1>
        <td></td>
        <td>ڵ</td>
        <td>
                <input type=text name=nodettl value=\"${CGI_nodettl}\" style=\"width:120;height:21\"></input>
                &nbsp;(,ΧΪ30~65535)
        </td>
</tr>
<tr id=row1>
        <td></td>
        <td>TCP˿</TD>
        <td>
                <input type=text name=tports value=\"${CGI_tports}\" style=\"width:340;height:21\"></input>
                &nbsp;(˿֮ԶŸ)
        </td>
</tr>
<tr id=row1>
        <td></td>
        <td>UDP˿</TD>
        <td>
                <input type=text name=uports value=\"${CGI_uports}\" style=\"width:340;height:21\"></input>
                &nbsp;(˿֮ԶŸ)
        </td>
</tr>
</table>
<table style=\"width:813; border-bottom:1px #787882 solid; color:#0000ff\">
<tr><td align=right>&nbsp;</td></tr>
</table>
<table style=\"width:800\">
<tr>
        <td align=right>
                <input type=submit style=\"width:90\" value=\"ύ\"></input>
                <input type=button style=\"width:90\" value=\"ȡ\" onclick=\"onCancel()\"></input>
		<input type=hidden name=appid value=\"${CGI_appid}\"></input>
		<input type=hidden name=appname value=\"${CGI_appname}\"></input>
        </td>
</tr>
</table>
</form>
</body>
</html>
";