<?

/*
** SellaNMS
** $Id: ne_viewer.php,v 1.7 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
*/

//NOTE: This is a hack script displays the 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['variable']);
			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 element.id AS element, interface.id AS interface, ip.id AS ip, element_data.value AS element_name, interface_data.value AS interface_name, ip_data.value AS ip_name FROM element LEFT JOIN interface ON (element.id = interface.element) LEFT JOIN ip ON (interface.id = ip.interface) LEFT JOIN element_data ON (element.id = element_data.element) LEFT JOIN interface_data ON (interface.id = interface_data.interface) LEFT JOIN ip_data ON (ip.id = ip_data.ip) WHERE element_data.variable = '.1.3.6.1.2.1.1.5.0' AND interface_data.variable = '.1.3.6.1.2.1.2.2.1.2' AND ip_data.variable = '.1.3.6.1.2.1.4.20.1.1' ORDER BY element.id, interface.id, ip.id";
	} else {
		$sql = sprintf("SELECT element.id AS element, interface.id AS interface, ip.id AS ip, element_data.value AS element_name, interface_data.value AS interface_name, ip_data.value AS ip_name FROM element LEFT JOIN interface ON (element.id = interface.element) LEFT JOIN ip ON (interface.id = ip.interface) LEFT JOIN element_data ON (element.id = element_data.element) LEFT JOIN interface_data ON (interface.id = interface_data.interface) LEFT JOIN ip_data ON (ip.id = ip_data.ip) WHERE element_data.variable = '.1.3.6.1.2.1.1.5.0' AND interface_data.variable = '.1.3.6.1.2.1.2.2.1.2' AND ip_data.variable = '.1.3.6.1.2.1.4.20.1.1' AND element.id = %lu ORDER BY element.id, interface.id, 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>element</B></TD><TD COLSPAN=2><B>interface</B></TD><TD COLSPAN=2><B>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 element.id AS element, interface.id AS interface, ip.id AS ip FROM element LEFT JOIN interface ON (element.id = interface.element) LEFT JOIN ip ON (interface.id = ip.interface) ORDER BY element.id, interface.id, ip.id";
	} else {
		$sql = sprintf("SELECT element.id AS element, interface.id AS interface, ip.id AS ip FROM element LEFT JOIN interface ON (element.id = interface.element) LEFT JOIN ip ON (interface.id = ip.interface) WHERE element.id = %lu ORDER BY element.id, interface.id, 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>element</B></TD><TD><B>interface</B></TD><TD><B>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 element_data.id AS id, element_data.element AS parent, element_data.variable AS variable, element_data.value AS value FROM element_data WHERE element_data.element = %lu ORDER BY element_data.id ", $id);
		display_entry($sql, 'element_data');
		break;
	case 'in':
		$sql = sprintf("SELECT interface_data.id AS id, interface_data.interface AS parent, interface_data.variable AS variable, interface_data.value AS value FROM interface_data WHERE interface_data.interface = %lu ORDER BY interface_data.id ", $id);
		display_entry($sql, 'interface_data');
		break;
	case 'ip':
		$sql = sprintf("SELECT ip_data.id AS id, ip_data.ip AS parent, ip_data.variable AS variable, ip_data.value AS value FROM ip_data WHERE ip_data.ip = %lu ORDER BY ip_data.id ", $id);
		display_entry($sql, '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
*/

?>
