<?php

/*
** SellaNMS
** $Id: interfacelister_template.php,v 1.12 2005/08/22 02:25:41 sella Exp $
** Copyright (c) 2001 Digital Genesis Software, LLC. All Rights Reserved.
** Released under the GPL Version 2 License.
** http://www.digitalgenesis.com
*/

require_once('../config/config.php');
require_once('../libs/dgsutil/cgi.php');
require_once('../libs/util.php');

function print_interface($interface, $admin, $oper, $name, $description) {
	printf("\t\t\t<TR CLASS=\"tblentry\">\n");
	printf("\t\t\t\t<TD NOWRAP>&nbsp;%s&nbsp;</TD>\n", ($admin == 1) ? 'up' : 'down');
	printf("\t\t\t\t<TD NOWRAP>&nbsp;%s&nbsp;</TD>\n", ($oper == 1) ? 'up' : 'down');
	printf("\t\t\t\t<TD NOWRAP>&nbsp;<A TARGET=\"interfaceviewer\" HREF=\"sella_nms-interfaceviewer.php?interface=%d&rand=%d\">%s</A>&nbsp;</TD>\n", $interface, rand(0, getrandmax()), $name);
	printf("\t\t\t\t<TD NOWRAP>&nbsp;%s</TD>\n", $description);
	printf("\t\t\t</TR>\n");
}

$data = getFormData();

$db = mysql_connect($config['databaseServer'], $config['databaseUsername'], $config['databasePassword']);
mysql_select_db($config['databaseName'], $db);

if ($data['router']) {
	$filter = '';
	for ($i = 0; $i < count($config['interfaceFilter']); $i++) {
		$filter .= sprintf(" AND neid_1.value NOT LIKE '%s'", $config['interfaceFilter'][$i]);
	}
	$sql = sprintf("SELECT neid_1.interface AS iface, neid_1.value AS name FROM interface_data AS neid_1, interface WHERE neid_1.variable = '.1.3.6.1.2.1.2.2.1.2' AND neid_1.interface = interface.id AND interface.element = %d %s ORDER BY name", $data['router'], $filter);

	$result = mysql_query($sql, $db);
	for ($i = 0; $row = mysql_fetch_array($result); $i++) {
		$info[$i]['iface'] = $row['iface'];
		$info[$i]['name'] = $row['name'];
		$info[$i]['adminstate'] = 1;
		$info[$i]['operstate'] = 1;
	}

	/* FIXME: This select returns nothing if one of the specified oids is missing. Netscreen firewalls don't have an entry for '.1.3.6.1.2.1.31.1.1.1.18' for example. */ 
	$sql = sprintf("SELECT neid_1.interface AS iface, neid_1.value AS name, neid_2.value AS description, neid_3.value AS adminstate, neid_4.value AS operstate FROM interface_data AS neid_1, interface_data AS neid_2, interface_data AS neid_3, interface_data AS neid_4, interface WHERE neid_1.variable = '.1.3.6.1.2.1.2.2.1.2' AND neid_2.variable = '.1.3.6.1.2.1.31.1.1.1.18' AND neid_3.variable = '.1.3.6.1.2.1.2.2.1.7' AND neid_4.variable = '.1.3.6.1.2.1.2.2.1.8' AND neid_1.interface = neid_2.interface AND neid_1.interface = neid_3.interface AND neid_1.interface = neid_4.interface AND neid_1.interface = interface.id AND interface.element = %d %s ORDER BY name, description", $data['router'], $filter);

	$result = mysql_query($sql, $db);
	for ($i = 0; $row = mysql_fetch_array($result); $i++) {
		if ($row['iface'] == $info[$i]['iface']) {
			$info[$i]['description'] = $row['description'];
			$info[$i]['adminstate'] = $row['adminstate'];
			$info[$i]['operstate'] = $row['operstate'];
		}
	}
}

?>
	<TABLE WIDTH="100%" CELLSPACING="1" CELLPADDING="0" BORDER="0">
		<TR>
			<TD CLASS="tbltitle" COLSPAN="4">Interfaces: <?php printf('%d', count($info)); ?></TD>
		</TR>
		<TR CLASS="tblhdr">
			<TD NOWRAP>&nbsp;Admin&nbsp;</TD>
			<TD NOWRAP>&nbsp;Oper&nbsp;</TD>
			<TD NOWRAP>&nbsp;Name&nbsp;&nbsp;</TD>
			<TD NOWRAP WIDTH="99%">&nbsp;Description&nbsp;&nbsp;</TD>
		</TR>
<?php

for($i = 0; $i < count($info); $i++) {
	print_interface($info[$i]['iface'], $info[$i]['adminstate'], $info[$i]['operstate'], $info[$i]['name'], $info[$i]['description']);
}
printf("\t\t\t</TABLE>\n");

/*
** Local Variables:
** c-basic-offset: 3
** tab-width: 3
** End:
** vim600: fdm=marker
** vim: noet ts=3 sw=3
*/

?>
