<?php
/*****************************************************************************
IP Reg, a PHP/MySQL IPAM tool
Copyright (C) 2008 Wietse Warendorff
This program 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 3 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, see < http: / / www . gnu . org / licenses / > .
For more information, visit http://sourceforge.net/projects/ipreg,
or contact me at wietsew@users.sourceforge.net
*****************************************************************************/
// includes
include("includes.php");
// get id
$subnet_id = $_GET['subnet_id'];
// check authorisation
$auth = auth("subnet", $config_auth_subnetview, $subnet_id);
// start output
include("header.php");
// get page
if(isset($_GET['page'])) {
$page = $_GET['page'];
}
// set template
$tp = new Template("tpl/subnetview.tpl");
// set language variables
$tp->setvars($lang);
// get subnet info
$result = mysql_query("SELECT subnet_address, subnet_mask, vlan_id, subnet_info FROM subnet WHERE subnet_id='$subnet_id'") or die(mysql_error());
$row = mysql_fetch_object($result);
$tp->set("subnet_id", $subnet_id);
$tp->set("subnet_address", $row->subnet_address);
$tp->set("subnet_mask", $row->subnet_mask);
$tp->set("subnet_info", nl2br($row->subnet_info));
// set needed variables
$subnet_address = $row->subnet_address;
$subnet_mask = $row->subnet_mask;
$vlan_id = $row->vlan_id;
// split up the range
$iprange = explode('.', $subnet_address);
$iprange1 = $iprange[0];
$iprange2 = $iprange[1];
$iprange3 = $iprange[2];
$iprange4 = $iprange[3];
// create array for these addresses
$subnet = array();
if ($subnet_mask>=24) {
// Class C
// calculate hosts
$hostcount = pow(2,(32-$subnet_mask));
// fill array with addresses we want to see
for($i=0;$i< $hostcount;$i++) {
$ip = $iprange1 . '.' . $iprange2 . '.' . $iprange3 . '.' . ($iprange4+$i);
$subnet[$ip] = array();
}
// calculate broadcast address
$broadcast_address = $iprange1 . '.' . $iprange2 . '.' . $iprange3 . '.' . ($iprange4+$i-1);
// no pagination needed
$tp->set("pagination", ' ');
} else if ($subnet_mask>=16) {
// Class B
// which part do we want to see?
if((empty($page)) ? $page=$subnet_address : $page=$page);
$page = explode('.', $page);
$page2 = $page[2];
// fill array with addresses we want to see
for($i=0;$i< 256 ; $ i + + ) {
$ip = $iprange1 . '.' . $iprange2 . '.' . $page2 . '.' . $i;
$subnet[$ip] = array();
}
// calculate broadcast address
$broadcast_address = $iprange1 . '.' . $iprange2 . '.' . ($iprange3+$i-1) . '.255';
// create pagination
$pagination = 'Page: ' . $iprange1 . '.' . $iprange2 . '. ';
$pagination .= '< select name = "pagination" onchange = "linkTo(this.options[this.selectedIndex].value);" > ';
for($i=$iprange3;$i< (pow(2,(32-$subnet_mask))/256);$i++) {
if(($i==$page2) ? $selected='selected' : $selected='');
$pagination .= '< option value = "' . $subnet_id . '&page=' . $iprange1 . '.' . $iprange2 . '.' . $i . '.0" ' . $ selected . ' > ' . $i . '.0< / option > ';
}
$pagination .= '< / select > ';
$tp->set("pagination", $pagination);
} else {
// Class A
// which part do we want to see?
if((empty($page)) ? $page=$subnet_address : $page=$page);
$page = explode('.', $page);
$page2 = $page[1];
$page3 = $page[2];
// fill array with addresses we want to see
for($i=0;$i< 256 ; $ i + + ) {
$ip = $iprange1 . '.' . $page2 . '.' . $page3 . '.' . $i;
$subnet[$ip] = array();
}
// calculate broadcast address
$broadcast_address = $iprange1 . '.' . ($iprange+$i-1) . '.255.255';
// create pagination
$pagination = 'Page: ';
// selectbox 1
$pagination .= '< select name = "pagination" onchange = "linkTo(this.options[this.selectedIndex].value);" > ';
for($i=$iprange2;$i< (pow(2,(24-$subnet_mask))/256);$i++) {
if(($i==$page2) ? $selected='selected' : $selected='');
$pagination .= '< option value = "' . $subnet_id . '&page=' . $iprange1 . '.' . $i . '.' . $page3 . '.0" ' . $ selected . ' > ' . $iprange1 . '.' . $i . '< / option > ';
}
$pagination .= '< / select > < select name = "pagination" onchange = "linkTo(this.options[this.selectedIndex].value);" > ';
// selectbox 2
for($i=0;$i< 256 ; $ i + + ) {
if(($i==$page3) ? $selected='selected' : $selected='');
$pagination .= '< option value = "' . $subnet_id . '&page=' . $iprange1 . '.' . $page2 . '.' . $i . '.0" ' . $ selected . ' > ' . $i . '.0< / option > ';
}
$pagination .= '< / select > ';
$tp->set("pagination", $pagination);
}
// get nodes for this subnet and implement the values into the array
$result = mysql_query("SELECT a.asset_name, acg.color, n.node_id, n.ip FROM asset a, assetclass ac, assetclassgroup acg, node n WHERE n.ip IN ('".implode("','",array_keys($subnet))."') AND n.subnet_id='$subnet_id' AND a.asset_id=n.asset_id AND ac.assetclass_id=a.assetclass_id AND acg.assetclassgroup_id=ac.assetclassgroup_id") or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$subnet[$row['ip']] = $row;
}
// replace subnet address (if in array)
if(array_key_exists($subnet_address, $subnet)) {
$subnet[$subnet_address]=array("subnet_address");
}
// replace broadcast address (if in array)
if(array_key_exists($broadcast_address, $subnet)) {
$subnet[$broadcast_address]=array("broadcast_address");
}
// loop array and send to template
$i=1;
foreach($subnet as $ip => $node) {
if(($i%64==0) ? $tr="< / tr > < tr > " : $tr="");
if(empty($node)) {
$tp->set("url", 'assigniptonode.php?subnet_id=' . $subnet_id . '& ip='. $ip);
$tp->set("remotetext", $ip);
$tp->set("color", $config_color_unused);
} else if ($node[0]=="subnet_address") {
$tp->set("url", "");
$tp->set("remotetext", $ip . ' ' . $lang['lang_subnet_subnetaddress']);
$tp->set("color", $config_color_blocked);
} else if ($node[0]=="broadcast_address") {
$tp->set("url", "");
$tp->set("remotetext", $ip . ' ' . $lang['lang_subnet_broadcastaddress']);
$tp->set("color", $config_color_blocked);
} else {
$tp->set("url", 'nodeview.php?node_id=' . $node['node_id']);
$tp->set("remotetext", $ip . ' ' . $node['asset_name']);
$tp->set("color", $node['color']);
}
$tp->set("tr", $tr);
$tp->parse("iprow");
$i++;
}
$tp->parse("subnet");
// get vlan info
$result = mysql_query("SELECT vlan_id, vlan_number, vlan_name FROM vlan WHERE vlan_id='$vlan_id'") or die(mysql_error());
for ($i=0;$row=mysql_fetch_object($result);$i++) {
$tp->set("vlan_id", $row->vlan_id);
$tp->set("vlan_name", $row->vlan_name);
$tp->parse("vlanrow");
}
if (($i>0) ? $tp->parse("vlan") : $tp->hide("vlan"));
// get location info
$result = mysql_query("SELECT l.location_id, l.location_name FROM location l INNER JOIN subnetlocation sl ON l.location_id=sl.location_id WHERE sl.subnet_id='$subnet_id'") or die(mysql_error());
for ($i=0;$row=mysql_fetch_object($result);$i++) {
$tp->set("location_id", $row->location_id);
$tp->set("location_name", $row->location_name);
$tp->parse("locationrow");
}
if (($i>0) ? $tp->parse("location") : $tp->hide("location"));
// get assetclassgroup info
$result = mysql_query("SELECT assetclassgroup_id, assetclassgroup_name, color FROM assetclassgroup ORDER BY assetclassgroup_name") or die(mysql_error());
for ($i=0;$row=mysql_fetch_object($result);$i++) {
$tp->set("assetclassgroup_id", $row->assetclassgroup_id);
$tp->set("assetclassgroup_name", $row->assetclassgroup_name);
$tp->set("color", $row->color);
$tp->parse("assetclassgrouprow");
}
if (($i>0) ? $tp->parse("assetclassgroup") : $tp->hide("assetclassgroup"));
// output
$tp->parse();
$tp->spit();
include("footer.php");
?>