<HTML>
	<HEAD>
		<TITLE>SellaNMS v0.3.0</TITLE>
		<LINK REL="SHORTCUT ICON" HREF="../favicon.ico">
		<LINK REL="stylesheet" HREF="../common/default.css" TYPE="text/css">
		<META HTTP-EQUIV="expires" CONTENT="Thu, 1 Jan 1970 00:00:00 GMT">
	</HEAD>

	<BODY TOPMARGIN="0" LEFTMARGIN="0" MARGINWIDTH="0" MARGINHEIGHT="0">

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

	global $config;

	$form = getFormData();

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

	if ($form['acknowlege']) {
		$sql = sprintf('UPDATE gui_event SET acknowleged = 1, timestamp = timestamp WHERE id = %d', $form['acknowlege']);
		$result = mysql_query($sql, $db);

		$sql = sprintf("INSERT INTO gui_history SET element = %d, interface = %d, ip = %d, channel = '%s', state = 'informational', brief = 'Event Acknowleged', detail = 'Event was acknowleged by %s'", $form['element'], $form['interface'], $form['ip'], $form['channel'], isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : 'user');
		$result = mysql_query($sql, $db);
	} else if ($form['note']) {
		$sql = sprintf("INSERT INTO gui_history SET element = %d, interface = %d, ip = %d, channel = '%s', state = 'informational', ticket = '%s', detail = '%s: %s'", $form['element'], $form['interface'], $form['ip'], $form['channel'], $form['ticket'], isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : 'user', $form['detail']);
		$result = mysql_query($sql, $db);
	}

	$sql = sprintf("SELECT gui_event.acknowleged AS ack, gui_event.id AS event_id, gui_event.state AS state, gui_event.element AS element_id, gui_event.interface AS interface_id, gui_event.ip AS ip_id, interface_data.variable AS variable, UNIX_TIMESTAMP(gui_event.timestamp) AS stamp, element_data.value AS element, interface_data.value AS interface_data, gui_event.channel AS channel, gui_event.source AS source, gui_event.protocol AS protocol, gui_event.brief AS brief, gui_event.detail AS detail FROM gui_event LEFT JOIN element_data ON ( element_data.element = gui_event.element AND element_data.variable = '.1.3.6.1.2.1.1.5.0' ) LEFT JOIN interface_data ON ( interface_data.interface = gui_event.interface AND ( interface_data.variable = '.1.3.6.1.2.1.2.2.1.2' OR interface_data.variable = '.1.3.6.1.2.1.31.1.1.1.18' ) ) WHERE gui_event.id = '%d' ORDER BY stamp DESC, element_data.value, interface_data.value", $form['id']);

	$data = array();
//	echo "SQL: $sql<BR>";
	$result = mysql_query($sql, $db);
	if (mysql_num_rows($result) < 1) {
		printf("<P CLASS=\"tblentry\">Event has been cleared or changed states.</P>\n");
		printf("<CENTER>[<A HREF=\"%s?args=%s&rand=%d\">Return to Listing</A>]</CENTER>\n", $form['listing'], $form['args'], rand(0, getrandmax()));
		return;
	}

	for ($id = 0; $row = mysql_fetch_array($result); $id++) {
		if (!isset($data[$row['event_id']])) {
			$data[$row['event_id']]['order'] = $id;
			$data[$row['event_id']]['acknowleged'] = $row['ack'];

			$data[$row['event_id']]['event_id'] = $row['event_id'];
			$data[$row['event_id']]['element_id'] = $row['element_id'];
			$data[$row['event_id']]['interface_id'] = $row['interface_id'];
			$data[$row['event_id']]['ip_id'] = $row['ip_id'];

			$data[$row['event_id']]['timestamp'] = $row['stamp'];
			$data[$row['event_id']]['element'] = $row['element'];
			$data[$row['event_id']]['source'] = $row['source'];

			$data[$row['event_id']]['state'] = $row['state'];
			$data[$row['event_id']]['channel'] = $row['channel'];
			$data[$row['event_id']]['protocol'] = $row['protocol'];
			$data[$row['event_id']]['detail'] = $row['detail'];
			$data[$row['event_id']]['brief'] = $row['brief'];
		}
		if ($row['variable'] == '.1.3.6.1.2.1.2.2.1.2') {
			$data[$row['event_id']]['interface_name'] = $row['interface_data'];
		} else if ($row['variable'] == '.1.3.6.1.2.1.31.1.1.1.18') {
			$data[$row['event_id']]['interface_desc'] = $row['interface_data'];
		}
	}
?>

<FORM METHOD="get">
	<INPUT TYPE="hidden" NAME="note" VALUE="1">
	<INPUT TYPE="hidden" NAME="id" VALUE="<?php printf('%d', $form['id']); ?>">
	<INPUT TYPE="hidden" NAME="listing" VALUE="<?php printf('%s', $form['listing']); ?>">
	<INPUT TYPE="hidden" NAME="func" VALUE="<?php printf('%s', $form['func']); ?>">
	<INPUT TYPE="hidden" NAME="group" VALUE="<?php printf('%s', $form['group']); ?>">
	<INPUT TYPE="hidden" NAME="element" VALUE="<?php printf('%d', $data[$form['id']]['element_id']); ?>">
	<INPUT TYPE="hidden" NAME="interface" VALUE="<?php printf('%d', $data[$form['id']]['interface_id']); ?>">
	<INPUT TYPE="hidden" NAME="ip" VALUE="<?php printf('%d', $data[$form['id']]['ip_id']); ?>">
	<INPUT TYPE="hidden" NAME="channel" VALUE="<?php printf('%s', $data[$form['id']]['channel']); ?>">
	<INPUT TYPE="hidden" NAME="protocol" VALUE="<?php printf('%s', $data[$form['id']]['protocol']); ?>">
	<INPUT TYPE="hidden" NAME="state" VALUE="<?php printf('%s', $data[$form['id']]['state']); ?>">
<TABLE CELLSPACING="1" CELLPADDING="0" BORDER="0" WIDTH="100%">
	<TR CLASS="tbltitle">
		<TD COLSPAN="4" NOWRAP>
			Event Information
		</TD>
	</TR>
	<TR CLASS="tblentry">
		<TD NOWRAP>
			Time Stamp
		</TD>
		<TD WIDTH="100%">
			<?php printf('%s', date($config['dateFormat'], $data[$form['id']]['timestamp'])); ?>
		</TD>
	</TR>
	<TR CLASS="tblentry">
		<TD NOWRAP>
			Source
		</TD>
		<TD WIDTH="100%">
			<?php printf('%s', $data[$form['id']]['source']); ?>
		</TD>
	</TR>
	<TR CLASS="tblentry">
		<TD NOWRAP>
			Protocol
		</TD>
		<TD WIDTH="100%">
			<?php printf('%s', $data[$form['id']]['protocol']); ?>
		</TD>
	</TR>
	<TR CLASS="tblentry">
		<TD NOWRAP>
			Channel
		</TD>
		<TD WIDTH="100%">
			<?php printf('%s', $data[$form['id']]['channel']); ?>
		</TD>
	</TR>
	<TR CLASS="tblentry">
		<TD NOWRAP>
			Acknowleged
		</TD>
		<TD WIDTH="100%">
			<?php printf('%s', $data[$form['id']]['acknowleged'] == 1 ? 'Yes' : 'No'); ?>
		</TD>
	</TR>
	<TR CLASS="tblentry">
		<TD NOWRAP>
			Element
		</TD>
		<TD>
			<?php printf('%s', $data[$form['id']]['element']); ?>
		</TD>
	</TR>
	<TR CLASS="tblentry">
		<TD NOWRAP>
			Interface Name
		</TD>
		<TD>
			<?php printf('%s', $data[$form['id']]['interface_name']); ?>
		</TD>
	</TR>
	<TR CLASS="tblentry">
		<TD NOWRAP>
			Interface Description
		</TD>
		<TD>
			<?php printf('%s', $data[$form['id']]['interface_desc']); ?>
		</TD>
	</TR>
	<TR CLASS="tblentry">
		<TD NOWRAP>
			Event Summary
		</TD>
		<TD>
			<?php printf('%s', $data[$form['id']]['brief']); ?>
		</TD>
	</TR>
	<TR CLASS="tblentry">
		<TD NOWRAP>
			Event Description
		</TD>
		<TD>
			<?php printf('%s', $data[$form['id']]['detail']); ?>
		</TD>
	</TR>
	<TR CLASS="tblentry">
		<TD NOWRAP VALIGN="top">
			Ticket
		</TD>
		<TD>
			<INPUT TYPE="text" NAME="ticket" SIZE="10"> &nbsp; <INPUT TYPE="submit" VALUE="Submit Note"> &nbsp; <INPUT TYPE="reset">
		</TD>
	</TR>
	<TR CLASS="tblentry">
		<TD NOWRAP VALIGN="top">
			Note
		</TD>
		<TD>
			<TEXTAREA NAME="detail" COLS="60" ROWS="2" WRAP="virtual"></TEXTAREA>
		</TD>
	</TR>
</TABLE>
</FORM>
<?php
	printf('<CENTER>');
	if (!empty($form['group'])) {
		printf('[<A HREF="%s?&rand=%d">Return to Summary</A>]', $form['listing'], rand(1, getrandmax()));
	}
	printf('[<A HREF="%s?func=%s&group=%s&rand=%d">Return to Listing</A>]', $form['listing'], $form['func'], $form['group'], rand(1, getrandmax()));
	printf('&nbsp;[<A HREF="?id=%d&listing=%s&func=%s&group=%s&acknowlege=%d&element=%d&interface=%d&ip=%d&channel=%s&protocol=%s&state=%s&rand=%d">Acknowledge Event</A>]', $form['id'], $form['listing'], $form['func'], $form['group'], $form['id'], $data[$form['id']]['element_id'], $data[$form['id']]['interface_id'], $data[$form['id']]['ip_id'], $data[$form['id']]['channel'], $data[$form['id']]['protocol'], $data[$form['id']]['state'], rand(1, getrandmax()));
	printf('&nbsp;[<A HREF="%s?action=clear&id=%d&func=%s&group=%s&element=%d&interface=%d&ip=%d&channel=%s&protocol=%s&state=%s&rand=%d">Clear Event</A>]', $form['listing'], $form['id'], $form['func'], $form['group'], $data[$form['id']]['element_id'], $data[$form['id']]['interface_id'], $data[$form['id']]['ip_id'], $data[$form['id']]['channel'], $data[$form['id']]['protocol'], $data[$form['id']]['state'], rand(1, getrandmax()));
	printf("</CENTER>\n");

	$w = strcmp($data[$form['id']]['element_id'], '') == 0 ? 'gui_history.element IS NULL' : sprintf('gui_history.element = %d', $data[$form['id']]['element_id']);
	$w .= strcmp($data[$form['id']]['interface_id'], '') == 0 ? ' AND gui_history.interface IS NULL' : sprintf(' AND gui_history.interface = %d', $data[$form['id']]['interface_id']);
	$w .= strcmp($data[$form['id']]['ip_id'], '') == 0 ? ' AND gui_history.ip IS NULL' : sprintf(' AND gui_history.ip = %d', $data[$form['id']]['ip_id']);

	$sql = sprintf("SELECT gui_history.id AS event_id, UNIX_TIMESTAMP(gui_history.timestamp) AS stamp, gui_history.channel AS channel, gui_history.ticket AS ticket, gui_history.brief AS brief, gui_history.detail AS detail FROM gui_history WHERE %s ORDER BY stamp DESC LIMIT %d", $w, is_int($config['history-max']) ? $config['history-max']: 25);

	$data = array();
	$result = mysql_query($sql, $db);
	for ($id = 0; $row = mysql_fetch_array($result); $id++) {
		if (!isset($data[$row['event_id']])) {
			$data[$row['event_id']]['order'] = $id;

			$data[$row['event_id']]['event_id'] = $row['event_id'];

			$data[$row['event_id']]['timestamp'] = $row['stamp'];

			$data[$row['event_id']]['channel'] = $row['channel'];
			$data[$row['event_id']]['ticket'] = $row['ticket'];
			$data[$row['event_id']]['brief'] = $row['brief'];
			$data[$row['event_id']]['detail'] = $row['detail'];
		}
	}
	mysql_close($db);
?>

<TABLE CELLSPACING="1" CELLPADDING="0" BORDER="0" WIDTH="100%">
	<TR CLASS="tbltitle">
		<TD COLSPAN="3" NOWRAP>
			Element History
		</TD>
	</TR>

	<TR CLASS="tblhdr">
		<TD NOWRAP>
			Time Stamp
		</TD>
		<TD NOWRAP>
			Channel
		</TD>
		<TD WIDTH="100%" NOWRAP>
			Summary/Ticket
		</TD>
	</TR>

	<?php while (list($i,) = each($data)) { ?>
	<TR CLASS="tblentry">
		<TD VALIGN="top" NOWRAP>
			<?php printf('%s', date($config['dateFormat'], $data[$i]['timestamp'])); ?>
		</TD>
		<TD VALIGN="top" NOWRAP>
			<?php printf('%s', $data[$i]['channel']); ?>
		</TD>
		<TD VALIGN="top" WIDTH="100%" NOWRAP>
			<?php printf('%s', (strlen($data[$i]['brief']) > 0) ? $data[$i]['brief'] : $data[$i]['ticket']); ?>
		</TD>
	</TR>
	<?php if (strlen($data[$i]['detail']) > 0) { ?>
	<TR CLASS="tblentry">
		<TD>&nbsp;</TD>
		<TD COLSPAN="2" VALIGN="top" WIDTH="100%">
			<?php printf('&nbsp;%s', nl2br($data[$i]['detail'])); ?>
		</TD>
	</TR>
	<?php } } ?>
</TABLE>

	</BODY>
</HTML>
