IP Reg is a IPAM tool to keep track of assets, nodes (IP addresses, MAC addresses, DNS aliases) within different subnets, over different locations or even VLAN's. Written in PHP, used with a MySQL-database to have a unique insight in your local network.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
ipreg/subnetview.php

216 lines
7.7 KiB

<?php
include("header.php");
// get id
$subnet_id = $_GET['subnet_id'];
// 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", '&nbsp;');
} 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:&nbsp;' . $iprange1 . '.' . $iprange2 . '.&nbsp;';
$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:&nbsp;';
// 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", 'grey');
} else if ($node[0]=="subnet_address") {
$tp->set("url", "");
$tp->set("remotetext", $ip . '&nbsp;' . $lang['lang_subnet_subnetaddress']);
$tp->set("color", 'cross');
} else if ($node[0]=="broadcast_address") {
$tp->set("url", "");
$tp->set("remotetext", $ip . '&nbsp;' . $lang['lang_subnet_broadcastaddress']);
$tp->set("color", 'cross');
} else {
$tp->set("url", 'nodeview.php?node_id=' . $node['node_id']);
$tp->set("remotetext", $ip . '&nbsp;' . $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 assetclass info
$result = mysql_query("SELECT assetclassgroup_id, assetclassgroup_name, color FROM assetclassgroup ORDER BY assetclassgroup_id") 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"));
// display options
if($_SESSION['suser_level'] >= $config_userlevel_assignlocationtosubnet) {
$tp->parse("assignlocationtosubnet");
} else {
$tp->hide("assignlocationtosubnet");
}
if($_SESSION['suser_level'] >= $config_userlevel_assignvlantosubnet) {
$tp->set("vlan_id", $vlan_id);
$tp->parse("assignvlantosubnet");
} else {
$tp->hide("assignvlantosubnet");
}
if($_SESSION['suser_level'] >= $config_userlevel_subnetedit) {
$tp->parse("subnetedit");
} else {
$tp->hide("subnetedit");
}
if($_SESSION['suser_level'] >= $config_userlevel_subnetdel) {
$tp->parse("subnetdel");
} else {
$tp->hide("subnetdel");
}
// output
$tp->parse();
$tp->spit();
include("footer.php");
?>