#!/bin/sh
#This script is created by ssparser automatically. The parser first created by MaoShouyan
printf "Content-type: text/html;charset=gb2312
Cache-Control: no-cache

"
echo -n "";
. ../common/common.sh
myself="/cgi-bin/Route/`basename $0`"

echo -n "
<script type=\"text/javascript\" src=\"/img/common.js\"></script>
<script language=\"javascript\">
function onAddProxy()
{
	window.location.href = \"/cgi-bin/Route/proxy_add\";
}
function onImportProxy()
{
	window.location.href = \"/cgi-bin/Route/proxy_import\";
}
function onExportProxy()
{
	window.location.href = \"/cgi-bin/Route/proxy_export\";
}
function onDeleteProxy(id)
{
	if (confirm(\"ϵͳɾʹô·ĹȷҪɾ·?\")) 
		window.location.href = \"${myself}?action=delete&pxyid=\" + id; 
}
function showProxy(type, proxyname)
{
        var url;
	url = \"/cgi-bin/Monitor/proxy_show?proxyname=\" + proxyname;
	if (type == \"pppoe\")
		ShowWindow(url, \"\", 380, 490);
	else
        	ShowWindow(url, \"\", 380, 400);
}
function editProxy(type, name)
{
	window.location.href = \"/cgi-bin/Route/proxy_edit?proxyname=\" + name;
}
function enableProxy(pxyid)
{
	window.location.href = \"${myself}?action=enable&id=\" + pxyid;
}
function disableProxy(pxyid)
{
	window.location.href = \"${myself}?action=disable&id=\" + pxyid;
}
function onAddIflan()
{
        window.location.href = \"/cgi-bin/Route/iflan_add\";
}
function onDeleteIflan(id)
{
        if (confirm(\"ϵͳɾʹӿص·ɲԣȷҪɾ?\"))
                window.location.href = \"${myself}?action=delete&pxyid=\" + id;
}
function pppoeRedial(name)
{
	if (confirm(\"ȷҪز?\"))
		window.location.href = \"${myself}?action=redial&proxyname=\" + name;
}
function onEditWG(id, name, type)
{
	window.location.href = \"/cgi-bin/Route/wg_edit?id=\" + id + 
		\"&name=\" + name + \"&type=\" + type;
}
function onAddWGwan(id, name)
{
	window.location.href = \"/cgi-bin/Route/wg_addwan?id=\" + id + \"&name=\" + name;
}
function onEditWGwan(id, name, proxy, weight)
{
	window.location.href = \"/cgi-bin/Route/wg_editwan?id=\" + id + 
		\"&name=\" + name + \"&proxy=\" + proxy + \"&weight=\" + weight;
}
function onRmvWGwan(id, proxy)
{
	if (confirm(\"ȷҪWANȺɾ·?\"))
		window.location.href = \"${myself}?action=rmvwgwan&id=\" + id + \"&proxy=\" + proxy;
}
function showIf(ifname)
{
        var url = \"/cgi-bin/Monitor/ifview?ifname=\" + ifname + \"&title=ӿ->\" + ifname;
        ShowWindow(url, \"\", 840, 800);
}
function onRtgraph(name, type)
{
        var url = \"/cgi-bin/Monitor/ifpxy_rtgraph?name=\" + name + \"&type=\" + type;
        ShowWindow(url, \"\", 720, 680);
}
function onSelectItemSub(obj)
{
	var i = 0, count = 0;
	var wans = document.getElementsByName(\"wan\");
	for (i = 0; i < wans.length; i++) {
		if (wans[i].checked) count++;
	}
	if (count > 0) {
		document.getElementsByName(\"a_redial\")[0].style.color = \"#0000ff\";
		document.getElementsByName(\"a_delete\")[0].style.color = \"#0000ff\";
		if (count == wans.length)
			document.getElementsByName(\"cb_main\")[0].checked = true;
		else
			document.getElementsByName(\"cb_main\")[0].checked = false;
	}
	else {
		document.getElementsByName(\"a_redial\")[0].style.color = \"#c0c0c0\";
		document.getElementsByName(\"a_delete\")[0].style.color = \"#c0c0c0\";
	}
}
function onSelectItemMain(obj)
{
	var i;
	var wans = document.getElementsByName(\"wan\");
	if (wans.length <= 0) {
		document.getElementsByName(\"a_redial\")[0].style.color = \"#c0c0c0\";
		document.getElementsByName(\"a_delete\")[0].style.color = \"#c0c0c0\";
		return;
	}
	
	if (obj.checked) {
		document.getElementsByName(\"a_redial\")[0].style.color = \"#0000ff\";
		document.getElementsByName(\"a_delete\")[0].style.color = \"#0000ff\";
	}
	else {
		document.getElementsByName(\"a_redial\")[0].style.color = \"#c0c0c0\";
		document.getElementsByName(\"a_delete\")[0].style.color = \"#c0c0c0\";
	}
	for (i = 0; i < wans.length; i++)
		wans[i].checked = obj.checked;
}
function onDeleteSelectItem(obj)
{
	var i = 0, items =\"\";
	var wans = document.getElementsByName(\"wan\");
	for (i = 0; i < wans.length; i++) {
		if (wans[i].checked)
			items += wans[i].value + \",\";
	}
	
	if(items!=\"\") {
		if (confirm(\"ϵͳɾѡ·ӿںص·ɲԣȷҪɾ?\")) {
			items = items.substr(0, items.length - 1);
			window.location.href = \"${myself}?action=batchdelete&items=\"+items;
		}
	}
}
function onRedialSelectItem(obj)
{
	var i = 0, items =\"\";
	var wans = document.getElementsByName(\"wan\");
	for (i = 0; i < wans.length; i++) {
		if (wans[i].checked)
			items += wans[i].value + \",\";
	}
	if(items != \"\") {
		if (confirm(\"ȷҪѡ·ز?\")) {
			items = items.substr(0, items.length - 1);
			window.location.href = \"${myself}?action=batchredial&items=\"+items;
		}
	}
}
function ajxs(settings)
{
	var http;
	if (typeof XMLHttpRequest != 'undefined') 
		http = new XMLHttpRequest();
	else {
		try {
			http = new ActiveXObject(\"Msxml2.XMLHTTP\");    
		}catch (e) {
			try {
				http = new ActiveXObject(\"Microsoft.XMLHTTP\");
			} catch (e) {return ;}
		}
	}
	http.open(settings.type, settings.url, true);
	http.setRequestHeader(\"Accept\", \"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\");
	http.setRequestHeader(\"Cache-Control\", \"no-cache\");
	http.setRequestHeader(\"Accept-Language\", \"zh-CN,zh;q=0.8\");
	http.setRequestHeader(\"Content-Type\", \"text/html; charset=GB2312\");
	http.onreadystatechange = function() { 
		if (http.readyState == 4) {
			if (http.status == 200)
				settings.success(http.responseText);
			else
				settings.error(http.status);
		}
	}
	http.send(null);
}
function getremarks()
{
	var i, remarkobj;
	var wannames = document.getElementsByName(\"wanname\");
	if (wannames.length <= 0)
		return;
	ajxs({type: 'POST', 
	       url: '/cgi-bin/Route/ajax_proxy_remark', 
	     error: function(status){setTimeout(getremarks, 4000);},
	   success: function(data) { 
		var d;
		try {
			d = eval('('+data+')');
		} catch(e) {
			setTimeout(getremarks, 4000);
			return;
		}
		for (i = 0; i < d.length; i++) {
			remarkobj = document.getElementById(\"remark_\" + d[i].name);
			if (remarkobj != null)
				remarkobj.innerHTML = d[i].remark;
		}
	}});
}
function getproxystats()
{
	var row;
	ajxs({type: 'POST', 
	       url: '/cgi-bin/Route/ajax_proxy_list', 
	     error: function(status){setTimeout(getproxystats, 4000);},
	   success: function(data) { 
		var d, i;
		var stateobj;
		var inbpsobj, outbpsobj;
		try {
			d = eval('('+data+')');
		} catch(e) {
			setTimeout(getproxystats, 4000);
			return;
		}
		for (i = 0; i < d.length; i++) {
			stateobj = document.getElementById(\"linkstate_\" + d[i].id);
			if (stateobj != null) {
				if (d[i].state)
					stateobj.src = \"/img/yes.png\";
				else
					stateobj.src = \"/img/no.png\";
			}
			inbpsobj = document.getElementById(\"inbps_\" + d[i].id);
			if (inbpsobj != null) 
				inbpsobj.innerHTML = d[i].inbps;
			outbpsobj = document.getElementById(\"outbps_\" + d[i].id);
			if (outbpsobj != null) 
				outbpsobj.innerHTML = d[i].outbps;
		}
		
		setTimeout(getproxystats, 4000);
	}});
}
function onresize()
{
        if (document.body.clientWidth > 1200)
                document.getElementById(\"mytbl\").style.width=\"100%\";
        else
                document.getElementById(\"mytbl\").style.width=\"1200\";
}
function onloaddoc()
{
	getremarks();
	getproxystats();
	onresize();
}
</script>
";
if [ "${CGI_action}" = "delete" ]; then
	operator_check "${myself}"
	errmsg=`${FLOWEYE} nat rmvproxy ${CGI_pxyid}`
	if [ "$?" != "0" ]; then
		afm_dialog_msg "ʧ:${errmsg}"
	fi
elif [ "${CGI_action}" = "redial" ]; then
	operator_check "${myself}"
	errmsg=`${FLOWEYE} nat setpppoe name=${CGI_proxyname} redial=1`
	if [ "$?" != "0" ]; then
		afm_dialog_msg "ʧ:${errmsg}"
	else
		sleep 1
	fi
elif [ "${CGI_action}" = "batchdelete" ]; then
	operator_check "${myself}"
	errcount=0
	allcount=0
	CGI_items=`echo ${CGI_items} | tr ',' ' '`
	for id in ${CGI_items}
	do
		errmsg=`${FLOWEYE} nat rmvproxy ${id}`
		[ "$?" != "0" ] && errcount=$((${errcount}+1))
		allcount=$((${allcount}+1))
	done
	[ "${errcount}" != "0" ] && afm_dialog_msg "ܹ${allcount}ʧ${errcount}: ·ʹУ޷ɾ"
elif [ "${CGI_action}" = "batchredial" ]; then
	operator_check "${myself}"
	errcount=0
	allcount=0
	CGI_items=`echo ${CGI_items} | tr ',' ' '`
	for name in ${CGI_items}
	do
		errmsg=`${FLOWEYE} nat setpppoe name=${name} redial=1`
		[ "$?" != "0" ] && errcount=$((${errcount}+1))
		allcount=$((${allcount}+1))
		echo "<br><b><p style=\"color:#0000ff;font-size:13px\">زָ·\"${name}\"......</p>"
		sleep .1
	done
	sleep .5
	afm_load_page 0 "${myself}"
	exit 0
elif [ "${CGI_action}" = "enable" ]; then
	errmsg=`${FLOWEYE} nat config enableproxy=${CGI_id}`
	if [ "$?" != "0" ]; then
		afm_dialog_msg "ʧ:${errmsg}"
	fi
elif [ "${CGI_action}" = "disable" ]; then
	errmsg=`${FLOWEYE} nat config disableproxy=${CGI_id}`
	if [ "$?" != "0" ]; then
		afm_dialog_msg "ʧ:${errmsg}"
	fi
fi

echo -n "
<body onload=\"onloaddoc();\">
";
cgi_show_title "Ӧ·->->LANӿ"

echo -n "
<table id=mytbl width=\"100%\" border=0 cellspacing=1 cellpadding=1> 
<tr id=tblhdr>
	<td width=60  align=center></td>
	<td width=*   align=center>ӿ</td>
	<td width=100 align=right></td>
	<td width=100 align=right>ӿ״̬</td>
	<td width=120 align=right>IPַ</td>
	<td width=120 align=right></td>
	<td width=100 align=right>VLAN</td>
	<td width=120 align=right></td>
	<td width=120 align=right></td>
	<td width=130 align=right><a style=\"color:#0000ff;font-size:14px\" href=\"javascript:onAddIflan()\">>>&nbsp;&nbsp;</a></td>
</tr>
";
natcount=0
idname="row1"
${FLOWEYE} nat listproxy type=routerif | while read type id name state ifname addr netmask vlan mtu \
inbytes outbytes inbps outbps dnsreqs dnstimeouts linkup disabled theothers
do 
	natcount=$((${natcount}+1))
	if [ "${disabled}" = "disable" ]; then
    		echo "<tr id=row4 name=pxy_${id}>"
	else
    		echo "<tr id=${idname} name=pxy_${id}>"
	fi

echo -n "
    <td align=right>${natcount}&nbsp;</td>
    <td align=right><a style=\"color:#0000ff\" href=\"javascript:showProxy('rtif', '${name}')\"><span>${name}</span>&nbsp;</a></td>
    <td align=right><a style=\"color:#0000ff\" href=\"javascript:showIf('${ifname}')\"><span>${ifname}</span>&nbsp;</a></td>
    "; if [ "${disabled}" = "disable" ]; then 
echo -n "
    <td align=right>&nbsp;</td>
    "; elif [ ${state} -eq 1 ]; then 
echo -n "
    <td align=right><img id=\"linkstate_${id}\" src=\"/img/yes.png\" style=\"margin-right:5px;vertical-align:middle;\" /></td>
    "; elif [ ${state} -eq 0 ]; then 
echo -n "
    <td align=right><img id=\"linkstate_${id}\" src=\"/img/no.png\" style=\"margin-right:5px;vertical-align:middle;\" /></td>
    "; fi 
echo -n "
    <td align=right>${addr}</td>
    <td align=right>${netmask}</td>
    "; [ "${vlan}" = "0" ] && vlan="&nbsp;" 
echo -n "
    <td align=right>${vlan}</td>
    <td align=right><span id=\"inbps_${id}\">${inbps}</span></td>
    <td align=right><span id=\"outbps_${id}\">${outbps}</span></td>
    <td align=right>
	<img src=\"/img/edit.png\" style=\"margin-top:3px\" onclick=\"window.open('/cgi-bin/Route/iflan_edit?iflanname=${name}', '_self')\" title=\"޸Ľӿ\" />&nbsp;
	<img src=\"/img/delete.png\" style=\"margin-top:3px\" onclick=\"onDeleteIflan('${id}')\"  title=\"ɾӿ\" />&nbsp;
	<img src=\"/img/trend.png\" style=\"margin-top:3px\" onclick=\"onRtgraph('${name}', 'lan')\"  title=\"ʵʱ\" />&nbsp;
	"; if [ "${disabled}" != "disable" ]; then 
echo -n "
                <img src=\"/img/disable.png\" style=\"margin-top: 3px\" onclick=\"disableProxy('${id}')\" title=\"ýӿ\" />&nbsp;
	"; else 
echo -n "
                <img src=\"/img/enable.png\" style=\"margin-top: 3px\" onclick=\"enableProxy('${id}')\" title=\"ýӿ\" />&nbsp;
	"; fi 
echo -n "
    </td>
    </tr>
";
    if [ "${idname}" = "row1" ]; then
        idname="row2"
    else
        idname="row1"
    fi
done 

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