<?php
/*
$Id: html_out.php 579 2014-05-17 21:23:44Z bhockney $
(C) 2003-2014 by Bob Hockney <zeus@ix.netcom.com>

html output for webfwlog

Webfwlog is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" 
		"http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
  <title><?php print "{$config["title"]}"; ?></title>
<?php
$dsource = "$self?data_source=".urlencode("{$FWLOG["data_source"]}");
if (isset($FWLOG["data_table"]))
	$dsource = "$dsource&data_table=".urlencode("{$FWLOG["data_table"]}");
if (isset($FWLOG["syslog_file"]))
	$dsource = "$dsource&syslog_file=".urlencode("{$FWLOG["syslog_file"]}");
$muri = "$dsource";
if (isset($FWLOG["upd_hosts"]) and (!isset($FWLOG["page_length"]) or $FWLOG["page_length"] == 0))
	$muri = "$muri&upd_hosts=";
if (isset($FWLOG["populate_cache"]) and (!isset($FWLOG["page_length"]) or $FWLOG["page_length"] == 0))
	$muri = "$muri&populate_cache=";
if (isset($FWLOG["populate_cache_all"]) and (!isset($FWLOG["page_length"]) or $FWLOG["page_length"] == 0))
	$muri = "$muri&populate_cache_all=";
$muri = "${muri}&action=run+report&level=$level";

if (isset($FWLOG["refresh"]) and $FWLOG["refresh"]) {
?>
  <meta http-equiv="refresh" content="<?php print "{$FWLOG["refresh"]};url=$muri"; ?>">
<?php
}
?>
  <LINK REL=StyleSheet HREF="style.css" TYPE="text/css" MEDIA=screen>
</head>

<body bgcolor=#FFFFFF>
	<?php if ($config["debug"]) include "include/debug.php"; ?>
	<div align="center"><h1><?php print "{$config["title"]}"; ?></h1></div>

<?php

if ($source <> "sql")
	$link = db("open", "$database");

$rownum = 0;

print "<pre>";

if ($source == "syslog" and $config["debug"] >= 2)
	print htmlspecialchars("{$query}", ENT_QUOTES);

if (isset($FWLOG["be_verbose"]) and isset($config["allow_be_verbose"])) {
	if ($source == "sql") {
		print htmlspecialchars("$o_query", ENT_QUOTES)."\n";
	}

	if ($source == "syslog") {
		print htmlspecialchars("{$query}", ENT_QUOTES);
		print "Exit code: $retval\n";
		print htmlspecialchars("{$prog_params}", ENT_QUOTES)."\n";
		while($line = db("nextrow", $result, "$source")) {
			print htmlspecialchars("{$line}", ENT_QUOTES)."\n";
		}
	}
} else {
	if ($source == "syslog") {
		while($line = db("nextrow", $result, "$source")) {
			if ($retval) //bad exit status
				print htmlspecialchars("{$line}", ENT_QUOTES)."\n";
		}
	}
}

if ($source == "syslog" and $retval) { //bad exit status
	print "<font color=red>";
	print "Exit code: $retval\n";
	print "syslog parser terminated abnormally\n";
	die;
}

print "</pre>\n\n";

// Output Report
{
	if (isset($FWLOG["report_title"]))
		print "<div align=center><h2>".htmlspecialchars("{$FWLOG["report_title"]}", ENT_QUOTES)."</h2></div>\n";

    $table_opts = "cellpadding=1 cellspacing=1 align=\"center\"";
    $class1 = "html_r1";
    $class2 = "html_r2";
	$row_opts = "align=\"center\"";
    
	$class = $class1;

    print "<table $table_opts>\n";

    print "\t<tr><td colspan=4>\n";
    print "\t\t<a href=\"$dsource\">Home</a>&nbsp;\n";
    print "\t\t<a href=\"$self?action=use+report&select_report=".urlencode($FWLOG["report"])."&level=$level&drill_down=yes\">Edit this report</a>&nbsp;\n";
    print "\t</td></tr>\n";
	
	if ($source == "syslog") {
		$temp = $shortnames;
	} else {
		$temp = array_flip($shortnames);
	}

	if ($source == "syslog")
		$db_data = $source;
	else
		$db_data = $link;
	$colnum = 0;
	while ($name = db("nextfield", $result, $db_data)) {
		if ("$name" == "ip_saddr_t" or "$name" == "ip_daddr_t" or "$name" == "oob_family_t" or "$name" == "ip_protocol_t"
				or "$name" == "tcp_sport_t" or "$name" == "udp_sport_t" or "$name" == "tcp_dport_t" or "$name" == "udp_dport_t")
			continue;
		if (!isset($FWLOG["c_sname"]) and "$name" == "{$shortnames["sname"]}")
			continue; 
		$fields[] = "$name";
	}

	print "\t<tr $row_opts class='$class'>\n";
	foreach ($fields as $col) {
		if ($source == "syslog") {
			$name = "$col";
		} else {
			$name = "{$temp["$col"]}";
		}

		if ("$name" == "sname" and !isset($FWLOG["c_sname"]))
			continue;
		if (!isset($FWLOG["c_oob_family"]) and "$name" == "oob_family")
			continue; 

		switch ("$name") {
			case "dst_host":	// Would never sort
			case "src_host":	// by these so no
			case "dst_service":	// link in header
			case "src_service":	//
				print "\t\t<th>{$shortnames["$name"]}</th>\n";
				break;
			default:
				$s = "s_$name";
				$o = "o_$name";
				$h_uri = "$muri&drilldown=yes&start=0&$s=0";
				if (isset($FWLOG["$s"]) and $FWLOG["$s"] == 1)
					if (isset($FWLOG["$o"])) {
						$h_uri = "$h_uri&$o=";
					} else {
						$h_uri = "$h_uri&$o=on";
					}
				print "\t\t<th><A HREF=\"$h_uri\">{$shortnames["$name"]}</A></th>\n";
				break;
		}
    }
    print "\t</tr>\n";
	
	if ($source == "syslog")
		$rownum = $rownum + 1;

	while ($line = db("nextrow", $result, "$source")) {

		switch ($class) {
			case $class1: $class = $class2; break;
			case $class2: $class = $class1; break;
		}

		print "\t<tr $row_opts class='$class'>\n";
		$family = AF_INET;
		foreach ($line as $column => $value) {
			//format dates
			switch ("$column") {
				// database
				case "{$shortnames['oob_time_sec']}":
				case "{$shortnames['local_time']}":
				case "{$shortnames['earliest']}":
				case "{$shortnames['latest']}":
				case "{$shortnames['oob_earliest']}":
				case "{$shortnames['oob_latest']}":
				// syslog
				case "local_time":
				case "earliest":
				case "latest":
					$value = date("{$config["dformat"]}", $value);
					break;
				default:
					break;	
			}

			if ("$column" == "ip_saddr_t" or "$column" == "ip_daddr_t" or "$column" == "ip_protocol_t"
				or "$column" == "tcp_sport_t" or "$column" == "udp_sport_t"
					or "$column" == "tcp_dport_t" or "$column" == "udp_dport_t")
				continue;
			if ($source == "syslog") {
				$l = "l_$column";
				$colkey = $column;
				if ("$colkey" == "oob_family") {
					$family = $line["oob_family"];
					if (!isset($FWLOG["c_oob_family"]))
						continue;
				}
			} else {
				if("$column" == "oob_family_t") {
					$family = $value;
					continue;
				} elseif("$column" == "oob_family") {
					$family = $value;
				}
				$l = "l_{$temp["$column"]}";
				$colkey = $temp["$column"];
			}

			if ("$colkey" == "sname" and !isset($FWLOG["c_sname"]))
				continue;

			if (is_numeric($value) and isset($config["format.$colkey"]))
				$value = sprintf($config["format.$colkey"], $value);

			$count = "";
			if (("$colkey" == "dport" or "$colkey" == "sport") and $value > 65535 ) {
				print "\t\t<td></td>\n";
			} else {
				$s = "";
				if (!isset($FWLOG["summarize"]))
					if($source == "syslog") {
						$s = trim("{$line['sname']}");
					} else {
						$s = trim("{$line[$shortnames['sname']]}");
					}
				if (isset($FWLOG["$l"])) { // add link if requested
					$a = makewhere("$colkey", "$value", "$s", "$family");
					$uri = "$muri";
					$uri .= "&drilldown=yes&start=0";
					if ($source <> "syslog" and isset($FWLOG["w_extra"]) and
						! preg_match ('/&w_extra=/', "$a"))
							$uri .= "&w_extra=".urlencode("{$FWLOG["w_extra"]}");
					$uri .= "$a";
					print "\t\t<td><A HREF=\"$uri\">".htmlspecialchars("$value", ENT_QUOTES)."</A></td>\n";
				} else { 
					print "\t\t<td>".htmlspecialchars("$value", ENT_QUOTES)."</td>\n";
				}
			}
		}
		print "\t</tr>\n";
	}

	if ($source == "syslog") {
		$matched_rows = $result[$rownum];
		$rownum = $rownum + 1;
		$matched_records = $result[$rownum];
		$rownum = $rownum + 1;
		$hostupdatetime = $result[$rownum];
		$rownum = $rownum + 1;
		$servicesupdatetime = $result[$rownum];
		$rownum = $rownum + 1;
		$cachepopulatetime = $result[$rownum];
		$rownum = $rownum + 1;

		$querytime = $querytime - $hostupdatetime - $servicesupdatetime - $cachepopulatetime;
	}

	flush();

	// add paging links on LIMITed report
	if (isset($FWLOG["page_length"]) and $FWLOG["page_length"] > 0) {
		if ($matched_rows > $FWLOG["page_length"]) {
			print "<tr>\n";
			print "<td colspan=" . count($fields) .">\n";
			print_scrollbar("$muri&start=",$start,$matched_rows,$FWLOG["page_length"],count($fields) * 4);
			print "</td>\n";
			print "</tr>\n";
		}
	}

	print "<tr>\n";
	print "<td colspan=4>";
	if (isset($FWLOG["page_length"]) and $FWLOG["page_length"] > 0) {
		$begin = $start + 1;
		$end = min($start + $FWLOG["page_length"], $matched_rows);
		print "Rows $begin to $end of $matched_rows displayed";
	} else {
		print "$matched_rows rows returned";
	}
	print "</td>";
	print "</tr>\n";

	if (isset($FWLOG["summarize"])) {
		print "<tr>\n";
		print "<td colspan=4>";
		print "$matched_records logged entries matched";
		print "</td>";
		print "</tr>\n";
	}

	print "</table>\n";

	print "<p>";

	$reporttime = time() - $time -  $querytime - $hostupdatetime - $servicesupdatetime - $cachepopulatetime;
	$totaltime = time() - $time;

	if (isset($FWLOG["upd_hosts"])) {
		print "Host update time was $hostupdatetime Seconds<br>\n";
		print "Services update time was $servicesupdatetime Seconds<br>\n";
	}

	if (isset($FWLOG["populate_cache"]) or isset($FWLOG["populate_cache_full"])) {
		if (! isset($FWLOG["upd_hosts"]))
			$cachepopulatetime += $hostupdatetime + $servicesupdatetime;
		print "Cache Populate time was $cachepopulatetime Seconds<br>\n";
	}

	print "Query time was $querytime Seconds.<br>\n";
	print "Report time was $reporttime Seconds.<br>\n";
	print "Total time was $totaltime Seconds.<br>\n";
}

if($source == "sql") {
	db("statement", "DROP TABLE $output", $link);
	db("statement", "DROP TABLE $allrows", $link);
}

db("close", $link);

?>
<p>
<em><font size="-2">Powered by
	<font color="blue"><a href="http://www.webfwlog.net">Webfwlog <?php print "$webfwlog_version"; ?></a>
	</font></font>
</em>

</body>
</html>

<!-- vim:set ts=4: -->
