#!/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`"

echo -n "
<script type=\"text/javascript\" src=\"/img/common.js\"></script>
<script language=\"javascript\">
function IsChar(str)
{
        if (str == \"\")
                return false;
        for (i = 0; i < str.length; i++) {
		if (str.charCodeAt(i) >= 255)
			return false;
        }
	return true;
}
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 beforeAddApp(frm)
{
	var appname = document.getElementsByName(\"appname\")[0];
	var appcname = document.getElementsByName(\"appcname\")[0];
	var flowttl = document.getElementsByName(\"flowttl\")[0];
	var nodettl = document.getElementsByName(\"nodettl\")[0];
	var tports  = document.getElementsByName(\"tports\")[0];
	var uports  = document.getElementsByName(\"uports\")[0];
	appname.value = TrimAll(appname.value);
	if (!IsChar(appname.value)) {
		alert(\"ЭƱΪӢĸ!\");
		appname.select();
		return false;
	}
	if (!IsName(appname.value)) {
		alert(\"вܰ&ַ!\");
		appname.select();
		return false;
	}
	appcname.value = TrimAll(appcname.value);
	if (appcname.value == \"\") {
		alert(\",Ҫ7ַ15Ӣַ!\");
		appcname.select();
		return false;
	}
	if (!IsName(appcname.value)) {
		alert(\"вܰ&ַ!\");
		appcname.select();
		return false;
	}
	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(flowttl.value, 30, 65535)) {
		alert(\"ڵڱ30~65535!\");
		nodettl.select();
		return false;
	}
	tports.value = TrimAll(tports.value);
	if (!IsPortValid(tports.value)) {
		alert(\"˿ڲȷ˿֮ԶŸ!\");
		tports.select();
		return false;
	}
	uports.value = TrimAll(uports.value);
	if (!IsPortValid(uports.value)) {
		alert(\"˿ڲȷ˿֮ԶŸ!\");
		uports.select();
		return false;
	}
	return true;
}
function onCancel()
{
	window.location.href = \"/cgi-bin/Protocol/usrapp_list\";
}
</script>
";
if [ "${REQUEST_METHOD}" = "POST" ];  then
	yes=1
	if [ "${CGI_tports}" != "" ]; then
		exists=`${FLOWEYE} port test ${CGI_tports}`
		if [ "${exists}" = "EXIST" ]; then
			yes=0
			afm_dialog_msg "TCP˿ͬж˿ظ!"
		elif [ "${CGI_uports}" != "" ]; then
			exists=`${FLOWEYE} port test ${CGI_uports}`
			if [ "${exists}" = "EXIST" ]; then
				yes=0
				afm_dialog_msg "UDP˿ͬж˿ظ!"
			fi
		fi
	fi
	if [ ${yes} -ne 0 ]; then
		portarg=""
		[ "${CGI_tports}" != "" ] && portarg="tports=${CGI_tports}"
		[ "${CGI_uports}" != "" ] && portarg="${portarg} uports=${CGI_uports}"
		errmsg=`${FLOWEYE} app add name=${CGI_appname} cname=${CGI_appcname} \
flowttl=${CGI_flowttl} nodettl=${CGI_nodettl} ${portarg}`
		if [ "$?" != "0" ]; then
			afm_dialog_msg "Эʧ:${errmsg}!"
		else
			if [ "${CGI_tports}" = "" -a "${CGI_uports}" = "" ]; then
				afm_dialog_msg "ɹ!"
				afm_load_page 0 "/cgi-bin/Protocol/usrapp_list?appid=${CGI_appname}"
				exit 0
			fi
			args=""
			[ "${CGI_tports}" != "" ] && args="tcp=${CGI_tports}"
			[ "${CGI_uports}" != "" ] && args="${args} udp=${CGI_uports}"
			
			errmsg=`${FLOWEYE} port load ${CGI_appname} ${args}`
			if [ "$?" != "0" ]; then
				afm_dialog_msg "ض˿ʧ:${errmsg}!"
				errmsg=`${FLOWEYE} app remove ${CGI_appname}`
			else
				afm_dialog_msg "ɹ!"
				afm_load_page 0 "/cgi-bin/Protocol/usrapp_list?appid=${CGI_appname}"
				exit 0
			fi
		fi
	fi
fi
[ "${CGI_flowttl}" = "" ] && CGI_flowttl=120
[ "${CGI_nodettl}" = "" ] && CGI_nodettl=600

echo -n "
<body>
"; cgi_show_title "ӦЭ->ԶЭ->Э" 
echo -n "
<br>
<form method=post onsubmit=\"return beforeAddApp(this)\" action=\"${myself}\">
<table width=600 border=0 cellspacing=1 cellpadding=2>
<tr id=row1>
	<td width=40></td>
	<td width=80 align=left>Ӣ</td>
	<td width=* align=left>
		<input type=text name=appname value=\"${CGI_appname}\" style=\"width:100;height:21\"></input>
		&nbsp;(ֻӢĸַ,ȲҪ15)
	</td>
</tr>
<tr id=row1>
	<td></td>
	<td></td>
	<td>
		<input type=text name=appcname value=\"${CGI_appcname}\" style=\"width:100;height:21\"></input>
		&nbsp;(Ӣַ,ȲҪ7ֻ15Ӣַ)
	</td>
</tr>
<tr id=row1>
	<td></td>
	<td></td>
	<td>
		<input type=text name=flowttl value=\"${CGI_flowttl}\" style=\"width:80;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:80;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:180;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:180;height:21\"></input>
		&nbsp;(˿֮ԶŸ)
	</td>
</tr>
</table>
<table style=\"width:600; border-bottom:1px #787882 solid; color:#0000ff\">
<tr><td align=right>&nbsp;</td></tr>
</table>
<table style=\"width:600\">
<tr>
        <td align=right>
                <input type=submit style=\"width:90\" value=\"ύ\"></input>
                <input type=button style=\"width:90\" value=\"ȡ\" onclick=\"onCancel()\"></input>
        </td>
</tr>
</table>
</form>
</body>
</html>
";