<?php
$pageref = "tools";
include_once('include/config.php');
include_once("$cs_base_path/include/header.php");
extract($_POST);
if (empty($showhosts)) {
$showhosts="no";
};
if (empty($input)) {
print "
<h1>IP Accounting</h1><p />
<form method=\"POST\" action=\"tools.php\">
<p>
<b>Copy and Paste SHOW IP ACCOUNTING output here</b>
</p>
<textarea class=\"ipacc\" rows=\"20\" cols=\"80\" name=\"input\"></textarea>
<p />
Show Internal Hostnames on top 10?<input type=\"checkbox\" name=\"showhosts\" value=\"yes\">
<p />
<input type=\"submit\" value=\"Submit\">
</form>";
} else {
$line_input = preg_split('/[\n\r]+/',$input);
$source = array();
$destination = array();
$numoflines = count($line_input);
for ($x = 0; $x < $numoflines; $x++) {
$line = $line_input[$x];
preg_match('/^\s*([\d+\.]+)\s+([\d+\.]+)\s+(\d+)\s+(\d+)/', $line, $matches);
if (!empty($matches)) {
if (empty($source[$matches[1]])) {
$source[$matches[1]] = $matches[4];
} else {
$source[$matches[1]] += $matches[4];
};
if (empty($destination[$matches[2]])) {
$destination[$matches[2]] = $matches[4];
} else {
$destination[$matches[2]] += $matches[4];
};
};
};
arsort($source);
arsort($destination);
$source_ips = array_keys($source);
$destination_ips = array_keys($destination);
$numsource_ips = count($source_ips);
$numdestination_ips = count($destination_ips);
print "
<table>
<tr>
<td valign=top>
<table border=1>
<tr>
<td>
Source IP
</td>
";
if ($showhosts == "yes") {
print "
<td>
HostName
</td>
";
};
print "
<td>
Bytes
</td>
</tr>
";
for ($x = 0; $x < $numsource_ips; $x++) {
$bytes = $source[$source_ips[$x]];
if ($showhosts == "yes" and $x < 10) {
preg_match('/^\b10\.\b/', $source_ips[$x], $RFC1918);
if (!empty($RFC1918)) {
$hostname = gethostbyaddr($source_ips[$x]);
} else {
$hostname = "External IP";
};
};
if ($x >= 10) {
$hostname = "N/A";
};
print "
<tr>
<td>
$source_ips[$x]
</td>
";
if ($showhosts == "yes") {
print "
<td>
$hostname
</td>
";
};
print "
<td>
$bytes
</td>
</tr>
";
};
print "
</table>
</td>
<td>
</td>
<td valign=top>
<table border=1>
<tr>
<td>
Destination IP
";
if ($showhosts == "yes") {
print "
<td>
HostName
</td>
";
};
print "
<td>
Bytes
</td>
</tr>
";
for ($x = 0; $x < $numdestination_ips; $x++) {
$bytes = $destination[$destination_ips[$x]];
if ($showhosts == "yes" and $x < 10) {
preg_match('/^\b10\.\b/', $destination_ips[$x], $RFC1918);
if (!empty($RFC1918)) {
$hostname = gethostbyaddr($destination_ips[$x]);
} else {
$hostname = "External IP";
};
};
if ($x >= 10) {
$hostname = "N/A";
};
print "
<tr>
<td>
$destination_ips[$x]
</td>
";
if ($showhosts == "yes") {
print "
<td>
$hostname
</td>
";
};
print "
<td>
$bytes
</td>
</tr>
";
};
print "
</table>
</td>
</tr>
</table>
";
};
include_once("$cs_base_path/include/footer.php");
?>