<?

/*
** SellaNMS
** $Id: ne_viewer.php,v 1.6 2005/03/31 18:57:12 sella Exp $
** Copyright (c) 2001 Digital Genesis Software, LLC. All Rights Reserved.
** Released under the GPL Version 2 License.
** http://www.digitalgenesis.com
*/

//NOTE: This is a hack script displays the network_element_* tables.

require_once('../config/config.php');

function display_entry($sql, $title) {
	global $con;

	$result = mysql_query($sql, $con);
	printf("<SMALL>Rows Selected: %lu</SMALL>", mysql_num_rows($result));
	printf("<TABLE>\n");
	if (@mysql_num_rows($result) < 1) {
		printf("<TR><TD><B>No data returned from select. Have you done a discovery?</B></TD></TR>\n");
	} else {
		printf("<TR ALIGN=\"center\" BGCOLOR=\"5f5f5f\"><TD COLSPAN=4><B>%s</B></TD></TR>\n", $title);
		printf("<TR ALIGN=\"center\" BGCOLOR=\"5f5f5f\"><TD>ID</TD><TD>Parent</TD><TD>OID</TD><TD>Value</TD></TR>\n");
		while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
			printf("<TR>\n");
			printf("<TD BGCOLOR=\"aeaeae\">%lu</TD>", $row['id']);
			printf("<TD BGCOLOR=\"aeaeae\">%lu</TD>", $row['parent']);
			printf("<TD BGCOLOR=\"aeaeae\">%s</TD>", $row['oid']);
			printf("<TD BGCOLOR=\"aeaeae\">%s</TD>", $row['value']);
			printf("\n</TR>\n");
		}
	}
	printf("</TABLE>\n");
	printf("<P>[<A HREF=\"?\">Home</A>] [<A HREF=\"javascript:history.go(-1)\">Return to Previous</A>]\n");
}

function display_all($id) {
	global $con;

	if ($id < 1) {
		$sql = "SELECT network_element.id AS element, network_element_interface.id AS interface, network_element_interface_ip.id AS ip, network_element_data.value AS element_name, network_element_interface_data.value AS interface_name, network_element_interface_ip_data.value AS ip_name FROM network_element LEFT JOIN network_element_interface ON (network_element.id = network_element_interface.network_element) LEFT JOIN network_element_interface_ip ON (network_element_interface.id = network_element_interface_ip.network_element_interface) LEFT JOIN network_element_data ON (network_element.id = network_element_data.network_element) LEFT JOIN network_element_interface_data ON (network_element_interface.id = network_element_interface_data.network_element_interface) LEFT JOIN network_element_interface_ip_data ON (network_element_interface_ip.id = network_element_interface_ip_data.network_element_interface_ip) WHERE network_element_data.oid = '.1.3.6.1.2.1.1.5.0' AND network_element_interface_data.oid = '.1.3.6.1.2.1.2.2.1.2' AND network_element_interface_ip_data.oid = '.1.3.6.1.2.1.4.20.1.1' ORDER BY network_element.id, network_element_interface.id, network_element_interface_ip.id";
	} else {
		$sql = sprintf("SELECT network_element.id AS element, network_element_interface.id AS interface, network_element_interface_ip.id AS ip, network_element_data.value AS element_name, network_element_interface_data.value AS interface_name, network_element_interface_ip_data.value AS ip_name FROM network_element LEFT JOIN network_element_interface ON (network_element.id = network_element_interface.network_element) LEFT JOIN network_element_interface_ip ON (network_element_interface.id = network_element_interface_ip.network_element_interface) LEFT JOIN network_element_data ON (network_element.id = network_element_data.network_element) LEFT JOIN network_element_interface_data ON (network_element_interface.id = network_element_interface_data.network_element_interface) LEFT JOIN network_element_interface_ip_data ON (network_element_interface_ip.id = network_element_interface_ip_data.network_element_interface_ip) WHERE network_element_data.oid = '.1.3.6.1.2.1.1.5.0' AND network_element_interface_data.oid = '.1.3.6.1.2.1.2.2.1.2' AND network_element_interface_ip_data.oid = '.1.3.6.1.2.1.4.20.1.1' AND network_element.id = %lu ORDER BY network_element.id, network_element_interface.id, network_element_interface_ip.id", $id);
	}

	$result = mysql_query($sql, $con);
	if (@mysql_num_rows($result) < 1) {
		printf("<TR><TD><B>No data returned from select. Have you done a discovery?</B></TD></TR>\n");
	} else if ($id < 1) {
		$prev_element = 0;
		while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
			if ($prev_element != $row['element']) {
				printf("[<A HREF=\"#%lu\">%s</A>] ", $row['element'], $row['element_name']);
				$prev_element = $row['element'];
			}
		}
		printf("\n<P>\n");
		mysql_data_seek($result, 0);
	}

	$result = mysql_query($sql, $con);
	if (@mysql_num_rows($result) > 1) {
		printf("<SMALL>Rows Selected: %lu &nbsp; [<A HREF=\"?id=%lu&nodata=1\">Disable Data</A>]</SMALL>", mysql_num_rows($result), $id);
		printf("<TABLE>\n");
		$prev_element = 0;
		$prev_interface = 0;
		$prev_ip = 0;
		while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
			if ($prev_element != $row['element']) {
				if ($prev_element > 0) {
					printf("<TR><TD>[<A HREF=\"#top\">Top</A>]</TD></TR>\n");
				}
				printf("<TR ALIGN=\"center\" BGCOLOR=\"5f5f5f\"><TD COLSPAN=2><A NAME=\"%lu\"></A><B>network_element</B></TD><TD COLSPAN=2><B>network_element_interface</B></TD><TD COLSPAN=2><B>network_element_interface_ip</B></TD></TR>\n", $row['element']);
				printf("<TR ALIGN=\"center\" BGCOLOR=\"5f5f5f\"><TD COLSPAN=2>Element [<A HREF=\"?id=%lu\">Limit View</A>]</TD><TD COLSPAN=2>Interface</TD><TD COLSPAN=2>IP</TD></TR>\n", $row['element']);
			}
			printf("<TR>\n");
			if ($prev_element == $row['element']) {
				printf("<TD BGCOLOR=\"aeaeae\" COLSPAN=2>&nbsp;</TD>");
			} else {
				printf("<TD BGCOLOR=\"aeaeae\"><A HREF=\"?func=el&id=%lu\">%lu</A></TD>", $row['element'], $row['element']);
				printf("<TD BGCOLOR=\"aeaeae\"><A HREF=\"?func=el&id=%lu\">%s</A></TD>", $row['element'], $row['element_name']);
				$prev_element = $row['element'];
			}
			if ($prev_interface == $row['interface']) {
				printf("<TD BGCOLOR=\"aeaeae\" COLSPAN=2>&nbsp;</TD>");
			} else {
				printf("<TD BGCOLOR=\"aeaeae\"><A HREF=\"?func=in&id=%lu\">%lu</A></TD>", $row['interface'], $row['interface']);
				printf("<TD BGCOLOR=\"aeaeae\"><A HREF=\"?func=in&id=%lu\">%s</A></TD>", $row['interface'], $row['interface_name']);
				$prev_interface = $row['interface'];
			}
			if ($prev_ip == $row['ip']) {
				printf("<TD BGCOLOR=\"aeaeae\" COLSPAN=2>&nbsp;</TD>");
			} else {
				printf("<TD BGCOLOR=\"aeaeae\"><A HREF=\"?func=ip&id=%lu\">%lu</A></TD>", $row['ip'], $row['ip']);
				printf("<TD BGCOLOR=\"aeaeae\"><A HREF=\"?func=ip&id=%lu\">%s</A></TD>", $row['ip'], $row['ip_name']);
				$prev_ip = $row['ip'];
			}
			printf("\n</TR>\n");
		}
	}
	printf("</TABLE>\n");
	printf("<P>[<A HREF=\"?\">Home</A>] [<A HREF=\"javascript:history.go(-1)\">Return to Previous</A>]\n");
}

function display_all_nodata($id) {
	global $con;

	if ($id < 1) {
		$sql = "SELECT network_element.id AS element, network_element_interface.id AS interface, network_element_interface_ip.id AS ip FROM network_element LEFT JOIN network_element_interface ON (network_element.id = network_element_interface.network_element) LEFT JOIN network_element_interface_ip ON (network_element_interface.id = network_element_interface_ip.network_element_interface) ORDER BY network_element.id, network_element_interface.id, network_element_interface_ip.id";
	} else {
		$sql = sprintf("SELECT network_element.id AS element, network_element_interface.id AS interface, network_element_interface_ip.id AS ip FROM network_element LEFT JOIN network_element_interface ON (network_element.id = network_element_interface.network_element) LEFT JOIN network_element_interface_ip ON (network_element_interface.id = network_element_interface_ip.network_element_interface) WHERE network_element.id = %lu ORDER BY network_element.id, network_element_interface.id, network_element_interface_ip.id", $id);
	}

	$result = mysql_query($sql, $con);
	if (@mysql_num_rows($result) < 1) {
		printf("<TR><TD><B>No data returned from select. Have you done a discovery?</B></TD></TR>\n");
	} else if ($id < 1) {
		$prev_element = 0;
		while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
			if ($prev_element != $row['element']) {
				printf("[<A HREF=\"#%lu\">%lu</A>] ", $row['element'], $row['element']);
				$prev_element = $row['element'];
			}
		}
		printf("\n<P>\n");
		mysql_data_seek($result, 0);
	}

	$result = mysql_query($sql, $con);
	if (@mysql_num_rows($result) > 1) {
		printf("<SMALL>Rows Selected: %lu &nbsp; [<A HREF=\"?id=%lu\">Enable Data</A>]</SMALL>", mysql_num_rows($result), $id);
		printf("<TABLE>\n");
		$prev_element = 0;
		$prev_interface = 0;
		$prev_ip = 0;
		while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
			if ($prev_element != $row['element']) {
				if ($prev_element > 0) {
					printf("<TR><TD>[<A HREF=\"#top\">Top</A>]</TD></TR>\n");
				}
				printf("<TR ALIGN=\"center\" BGCOLOR=\"5f5f5f\"><TD><A NAME=\"%lu\"></A><B>network_element</B></TD><TD><B>network_element_interface</B></TD><TD><B>network_element_interface_ip</B></TD></TR>\n", $row['element']);
				printf("<TR ALIGN=\"center\" BGCOLOR=\"5f5f5f\"><TD>Element [<A HREF=\"?id=%lu&nodata=1\">Limit View</A>]</TD><TD>Interface</TD><TD>IP</TD></TR>\n", $row['element']);
			}
			printf("<TR>\n");
			if ($prev_element == $row['element']) {
				printf("<TD BGCOLOR=\"aeaeae\">&nbsp;</TD>");
			} else {
				printf("<TD BGCOLOR=\"aeaeae\"><A HREF=\"?func=el&id=%lu\">%lu</A></TD>", $row['element'], $row['element']);
				$prev_element = $row['element'];
			}
			if ($prev_interface == $row['interface']) {
				printf("<TD BGCOLOR=\"aeaeae\">&nbsp;</TD>");
			} else {
				printf("<TD BGCOLOR=\"aeaeae\"><A HREF=\"?func=in&id=%lu\">%lu</A></TD>", $row['interface'], $row['interface']);
				$prev_interface = $row['interface'];
			}
			if ($prev_ip == $row['ip']) {
				printf("<TD BGCOLOR=\"aeaeae\">&nbsp;</TD>");
			} else {
				printf("<TD BGCOLOR=\"aeaeae\"><A HREF=\"?func=ip&id=%lu\">%lu</A></TD>", $row['ip'], $row['ip']);
				$prev_ip = $row['ip'];
			}
			printf("\n</TR>\n");
		}
	}
	printf("</TABLE>\n");
	printf("<P>[<A HREF=\"?\">Home</A>] [<A HREF=\"javascript:history.go(-1)\">Return to Previous</A>]\n");
}

$con = @mysql_connect($config['databaseServer'], $config['databaseUsername'], $config['databasePassword']);
if (!$con) {
	printf("Error: Connection to MySQL server '%s' failed.<BR>\n", $server);
 	return;
} else {
	if (!@mysql_select_db($config['databaseName'], $con)) {
		printf("Error: Connection to MySQL database failed.<BR>\n");
		return;
	}
}

printf("<HTML><BODY BGCOLOR=\"ffffff\">\n");
printf("<H3>SellaNMS Database Network Element Viewer</H3>\n");

if (isset($_REQUEST['id'])) { $id = $_REQUEST['id']; }
if (isset($_REQUEST['func'])) { $func = $_REQUEST['func']; }
if (isset($_REQUEST['nodata'])) { $nodata = $_REQUEST['nodata']; }

switch ($func) {
	case 'el':
		$sql = sprintf("SELECT network_element_data.id AS id, network_element_data.network_element AS parent, network_element_data.oid AS oid, network_element_data.value AS value FROM network_element_data WHERE network_element_data.network_element = %lu ORDER BY network_element_data.id ", $id);
		display_entry($sql, 'network_element_data');
		break;
	case 'in':
		$sql = sprintf("SELECT network_element_interface_data.id AS id, network_element_interface_data.network_element_interface AS parent, network_element_interface_data.oid AS oid, network_element_interface_data.value AS value FROM network_element_interface_data WHERE network_element_interface_data.network_element_interface = %lu ORDER BY network_element_interface_data.id ", $id);
		display_entry($sql, 'network_element_interface_data');
		break;
	case 'ip':
		$sql = sprintf("SELECT network_element_interface_ip_data.id AS id, network_element_interface_ip_data.network_element_interface_ip AS parent, network_element_interface_ip_data.oid AS oid, network_element_interface_ip_data.value AS value FROM network_element_interface_ip_data WHERE network_element_interface_ip_data.network_element_interface_ip = %lu ORDER BY network_element_interface_ip_data.id ", $id);
		display_entry($sql, 'network_element_interface_ip_data');
		break;
	default:
		if ($nodata > 0) {
			display_all_nodata($id);
		} else {
			display_all($id);
		}
}

printf("</BODY></HTML>\n");

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

?>
