#!/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/Route/`basename $0`"
returnurl="/cgi-bin/Route/portmap_list"

echo -n "
<script type=\"text/javascript\" src=\"/img/common.js\"></script>
<script language=\"javascript\">
function beforeAddProxy(frm)
{
	var wan = document.getElementsByName(\"wan\")[0];
	var port  = document.getElementsByName(\"port\")[0];
	var mapip = document.getElementsByName(\"mapip\")[0];
	var mapport = document.getElementsByName(\"mapport\")[0];
	if (wan.value == \"\") {
		alert(\"ѡWAN·!\");
		wan.select();
		return false;
	}
	port.value = TrimAll(port.value);
	mapport.value = TrimAll(mapport.value);
	if (!IsDigitIn(mapport.value, 0, 65535)) {
		alert(\"Ƿ˿!\");
		mapport.select();
		return false;
	}
	mapip.value = TrimAll(mapip.value);
	if (!IsIPAddr(mapip.value)) {
		alert(\"ǷIP!\");
		mapip.select();
		return false;
	}
	return true;
}
function onCancel() 
{
	window.location.href = \"${returnurl}\";
}
</script>
";
if [ "${REQUEST_METHOD}" = "POST" ]; then
	operator_check "${myself}"
	errmsg=`${FLOWEYE} nat addportmap wan=${CGI_wan} port=${CGI_port} \
proto=${CGI_proto} mapip=${CGI_mapip} mapport=${CGI_mapport}`
	if [ "$?" != "0" ]; then
		afm_dialog_msg "ʧ:${errmsg}"
	else
		afm_load_page 0 "${returnurl}"
		exit 0
	fi
fi

echo -n "
<body>
"; cgi_show_title "->˿ӳ->" 
echo -n "
<br>
<form method=post onsubmit=\"return beforeAddProxy(this)\" action=\"${myself}\">
<table style=\"font:14px\" width=600 border=0 cellspacing=0 cellpadding=3 bgcolor=\"#ffffff\">
<tr id=row1>
	<td></td>
        <td align=left>WAN·</td>
        <td align=left>
		<select name=wan style=\"width:120\" value=\"${CGI_wan}\">
		";
		${FLOWEYE} nat listproxy | while read type id name theothers
		do
			[ "${type}" = "rtif" ] && continue
			echo "<option value=\"${name}\">${name}</option>"
		done
		${FLOWEYE} nat listwg | while read id name theothers
		do
			echo "<option value=\"_wg.${name}\">${name}</option>"
		done
		
echo -n "
		</select>
	</td>
</tr>
<tr id=row1>
	<td></td>
        <td align=left>WAN˿</td>
        <td align=left><input type=text name=port style=\"width:120px\" value=\"${CGI_port}\" />&nbsp;(ʽΪNNNN-MM)</td>
</tr>
<tr id=row1>
	<td></td>
        <td align=left>Э</td>
        <td align=left>
		<select name=proto style=\"width:120\" value=\"${CGI_proto}\">
		"; for proto in TCP UDP; do
			if [ "${CGI_proto}" = "${proto}" ]; then
				echo "<option value=\"${proto}\" selected>${proto}</option>"
			else
				echo "<option value=\"${proto}\">${proto}</option>"
			fi
		   done 
echo -n "
		</select>
	</td>
</tr>
<tr id=row1>
	<td></td>
        <td align=left>ӳIP</td>
        <td align=left>
		<input type=text name=mapip style=\"width:120px\" value=\"${CGI_mapip}\" />&nbsp;(xxx.xxx.xxx.xxx)
	</td>
</tr>
<tr id=row1>
	<td></td>
        <td align=left>ӳ˿</td>
        <td align=left><input type=text name=mapport style=\"width:120px\" value=\"${CGI_mapport}\" />&nbsp;(0ʾʹԭ˿)</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 id=newbtn style=\"width:80\" value=\"ύ\"></input>
		<input type=button id=newbtn style=\"width:80\" value=\"ȡ\" onclick=\"onCancel()\"></input>
	</td>
</tr>
</table>
</form>
</body>
</html>
";