#!/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 "";
if [ "${PALANG}" = "en" ]; then
	LANG_001="Stop"
	LANG_002="Start"
	LANG_003="Network Interface"
	LANG_004="Show"
	LANG_005="Please Select a Network Interface"
	LANG_006="Show Items num can't be empty"
fi
myself="/cgi-bin/App/iftop/`basename $0`"
if [ "${REQUEST_METHOD}" = "POST" ]; then
	FLOWEYE=/usr/ramdisk/bin/floweye
	if [ "${CGI_action}" = "start" ]; then
		errmsg=`${FLOWEYE} iftop config if=${CGI_ifname} enable=1`
		firstelem=1
		printf "["
		${FLOWEYE} iftop list top=${CGI_top} | while read ip bps theothers
		do
        		if [ ${firstelem} -eq 1 ]; then
                		printf "{ 'ip':'${ip}','bps':'${bps}'}"
                		firstelem=0
        		else
                		printf ",{ 'ip':'${ip}','bps':'${bps}'}"
        		fi
		done
		echo "]"
	elif [ "${CGI_action}" = "stop" ]; then
		errmsg=`${FLOWEYE} iftop config enable=0`
	fi
		
	exit 0;
else
	[ "${CGI_top}" = "" ] && CGI_top="50"
fi
CGI_browsertitle="iftop"
. ../../common/common.sh

echo -n "
<script type=\"text/javascript\" src=\"/img/common.js\"></script>
<script>
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);
}
var g_ifname;
var g_top;
var iStoping = true;
function cmdping()
{
	var action;
	if(g_ifname == \"\" || g_top == \"\")
		return;
	if (iStoping)
		action = \"stop\";
	else
		action = \"start\";
	ajxs({type: 'POST', 
	      url: '${myself}?top=' + g_top + '&ifname=' + g_ifname + '&action=' + action, 
	      error: function(status){setTimeout(cmdping, 4000);},
	      success: function(data){ 
		// var d = JSON.parse(data);
		var d;
		try {
			d = eval('('+data+')');
		}catch(e) {
			d = undefined;
		}
		if (iStoping) return;
		if(typeof(d) == \"undefined\") {
			setTimeout(cmdping, 4000);
			return;
		}
		var i;
		var row;
		var cell0, cell1, cell2;
		var pingbox = document.getElementById(\"pingbox\");
		while (pingbox.rows.length > 0)
			pingbox.deleteRow(0);
		for(i = 0; i < d.length; i++) {
			row = pingbox.insertRow(-1);
			row.style.color = \"#00F\";
			row.style.fontSize = \"12px\";
			cell0 = row.insertCell(0);
			cell0.setAttribute('align', 'left');
			cell0.setAttribute('width', '2');
			cell0.innerText = \"\"; 
			cell1 = row.insertCell(1);
			cell1.setAttribute('align', 'left');
			cell1.setAttribute('width', '160');
			cell1.innerText = \" \" + d[i].ip;
			cell2 = row.insertCell(2);
			cell2.setAttribute('align', 'left');
			cell2.setAttribute('width', '*');
			cell2.innerText = d[i].bps;
		}
		setTimeout(cmdping, 4000);
	}});
}
function doping()
{
	var pingbox = document.getElementById(\"pingbox\");
	var ifname = document.getElementById(\"ifname\");
	var top = document.getElementById(\"top\");
	var action = document.getElementById(\"action\");
	if (action.value == \"${LANG_001:=ֹͣ}\") {
		iStoping = true;
		action.value = \"${LANG_002:=ʼ}\";
	}
	else {
		if(ifname.value == \"\") {
			alert(\"${LANG_005:=ѡ}!\");
			return ;
		}
		if(top.value == \"\"){
			alert(\"${LANG_006:=TopΪ}!\");
			return ;
		}
		g_ifname = ifname.value;
		g_top = top.value;
		iStoping = false;
		action.value = \"${LANG_001:=ֹͣ}\";
	}
	cmdping();
}
</script>
<body>
<table width=300 border=0 cellspacing=0 cellpadding=1 bgcolor=\"#ffffff\">
<tr id=row1>
	<td align=left width=50>${LANG_003:=ѡ}</td>
	<td align=left width=60> 
		<select id=\"ifname\" style=\"width:100%;height:28px;\">
		";
		${FLOWEYE} if list | while read ifname bridge zone theothers
		do
			[ "${zone}" != "outside" ] && continue
			if [ "${ifname}" = "${CGI_ifname}" ]; then
				echo "<option value=\"${ifname}\" selected>${ifname}</option>"
			else
				echo "<option value=\"${ifname}\">${ifname}</option>"
			fi
		done 
echo -n "
		</select>
	</td>
	<td width=4></td>
	<td align=left width=40>${LANG_004:=ʾǰ}</td>
	<td width=60><input type=text id=\"top\" value=\"${CGI_top}\" style=\"width:100%;height:28px;\"></input></td>
	<td align=left width=*><input type=button id=\"action\" style=\"width:75\" value=\"${LANG_002:=ʼ}\" onclick=\"doping();\" /> </td>
</tr>
</table>
<table width=300 border=0 cellspacing=0 cellpadding=0 bgcolor=\"#ffffff\" style=\"font-size:12px\" id=pingbox>
</table>
</body>
</html>
";