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/vlan.php

240 lines
7.7 KiB

<?php
/*****************************************************************************
IP Reg, a PHP/MySQL IPAM tool
Copyright (C) 2007-2009 Wietse Warendorff (up to v0.5)
Copyright (C) 2011-2023 Thomas Hooge
SPDX-License-Identifier: GPL-3.0-or-later
*****************************************************************************/
include("includes.php");
if (isset($_REQUEST['id'])) {
$id = (int) $_REQUEST['id'] or $id = 0;
}
// ========== ADDITIONAL ACTION DEFINITIONS ===================================
define ('ACT_SUBNET_EDIT', 100);
define ('ACT_SUBNET_ADD', 101);
define ('ACT_SUBNET_DEL', 102);
// ========== ACTIONS START ===================================================
switch ($submit = form_get_action()) {
case NULL: break;
case 'add': $action = ACT_ADD; break;
case 'view': $action = ACT_VIEW; break;
case 'edit': $action = ACT_EDIT; break;
case 'del': $action = ACT_DELETE; break;
// Subnet
case 'sedit': $action = ACT_SUBNET_EDIT; break;
case 'sadd': $action = ACT_SUBNET_ADD; break;
case 'sdel': $action = ACT_SUBNET_DEL; break;
case 'exec-sedit':
if ($_POST['action'] == 'vlansubnetadd') {
$action = ACT_SUBNET_ADD;
} elseif ($_POST['action'] == 'vlansubnetdel') {
$action = ACT_SUBNET_DEL;
} else {
$g_warning->Add('Invalid action: '. $_POST['action']);
}
break;
case 'exec-sadd':
$subnet_id = sanitize($_POST['subnet_id']);
$sql = "INSERT INTO subnetvlan (subnet_id, vlan_id) VALUES (?, ?)";
$sth = $dbh->prepare($sql);
$sth->execute([$subnet_id, $id]);
$action = ACT_VIEW;
break;
case 'exec-sdel':
$subnet_id = sanitize($_POST['subnet_id']);
$sth = $dbh->prepare("DELETE FROM subnetvlan WHERE subnet_id=? AND vlan_id=?");
$sth->execute([$subnet_id, $id]);
$g_message->Add('Removed link to subnet');
$action = ACT_VIEW;
break;
case 'insert':
$vlan_name = sanitize($_POST['vlan_name']);
$vlan_number = sanitize($_POST['vlan_number']);
$vlan_info = sanitize($_POST['vlan_info']);
$vlan_color = sanitize($_POST['vlan_color']);
$sql = "INSERT INTO vlan (vlan_name, vlan_number, vlan_color, vlan_info)
VALUES (?, ?, ?, ?)";
$sth = $dbh->prepare($sql);
$sth->execute([$vlan_name, $vlan_number, $vlan_color, $vlan_info]);
$id = $dbh->lastInsertId();
$action = ACT_VIEW;
break;
case 'update':
$vlan_name = sanitize($_POST['vlan_name']);
$vlan_number = sanitize($_POST['vlan_number']);
$vlan_info = sanitize($_POST['vlan_info']);
$vlan_color = sanitize($_POST['vlan_color']);
$sql = "UPDATE vlan SET vlan_name=?, vlan_number=?, vlan_color=?, vlan_info=? WHERE vlan_id=?";
$sth = $dbh->prepare($sql);
$sth->execute([$vlan_name, $vlan_number, $vlan_color, $vlan_info, $id]);
$action = ACT_VIEW;
break;
case 'delete':
$sth = $dbh->prepare("DELETE FROM subnetvlan WHERE vlan_id=?");
$sth->execute([$id]);
$n = $sth->rowCount();
if ($n > 0) {
$g_message->Add("$n vlan to subnet references were removed.");
}
$sth = $dbh->prepare("DELETE FROM vlan WHERE vlan_id=?");
$sth->execute([$id]);
$g_message->Add(_("VLAN deleted."));
$action = ACT_DEFAULT;
break;
default:
$g_error->Add(submit_error($submit));
$valid = FALSE;
}
// ========== ACTIONS END =====================================================
$smarty->assign("scripts", 'jscolor.js');
include("header.php");
if ($action == ACT_DEFAULT):
// ========== VARIANT: default behavior =======================================
$sql = "SELECT vlan_id AS id, vlan_number AS number, vlan_name AS name,
vlan_color AS color, LEFT(vlan_info, 60) AS info
FROM vlan
ORDER BY vlan_number";
$sth = $dbh->query($sql);
$smarty->assign("vlans", $sth->fetchAll());
$smarty->display("vlan.tpl");
elseif ($action == ACT_ADD):
// ========== VARIANT: add record =============================================
$smarty->display("vlanadd.tpl");
elseif ($action == ACT_VIEW):
// ========== VARIANT: view single record =====================================
// vlan
$sql = "SELECT vlan_id AS id, vlan_name AS name, vlan_number AS number,
vlan_info AS info, vlan_color AS color
FROM vlan
WHERE vlan_id=?";
$sth = $dbh->prepare($sql);
$sth->execute([$id]);
$smarty->assign("vlan", $sth->fetch(PDO::FETCH_OBJ));
// subnets
$sql = "SELECT s.subnet_id, s.subnet_address, s.subnet_mask, s.subnet_info
FROM subnet AS s LEFT JOIN subnetvlan AS v USING (subnet_id)
WHERE v.vlan_id=?
ORDER BY INET_ATON(s.subnet_address)";
$sth = $dbh->prepare($sql);
$sth->execute([$id]);
$smarty->assign("subnets", $sth->fetchAll());
$smarty->display("vlanview.tpl");
elseif ($action == ACT_EDIT):
// ========== VARIANT: edit single record =====================================
$sql = "SELECT vlan_id AS id, vlan_name AS name, vlan_number AS number,
vlan_color AS color, vlan_info AS info
FROM vlan
WHERE vlan_id=?";
$sth = $dbh->prepare($sql);
$sth->execute([$id]);
$smarty->assign("vlan", $sth->fetch(PDO::FETCH_OBJ));
$smarty->display("vlanedit.tpl");
elseif ($action == ACT_DELETE):
// ========== VARIANT: delete record ==========================================
$sql = "SELECT vlan_id AS id, vlan_name AS name, vlan_number AS number
FROM vlan
WHERE vlan_id=?";
$sth = $dbh->prepare($sql);
$sth->execute([$id]);
$smarty->assign("vlan", $sth->fetch(PDO::FETCH_OBJ));
$smarty->display("vlandel.tpl");
elseif ($action == ACT_SUBNET_EDIT):
// ========== VARIANT: subnet to vlan =========================================
$sql = "SELECT vlan_id AS id, vlan_name AS name, vlan_number AS number FROM vlan WHERE vlan_id=?";
$sth = $dbh->prepare($sql);
$sth->execute([$id]);
$smarty->assign("vlan", $sth->fetch(PDO::FETCH_OBJ));
$smarty->display("vlansubnetedit.tpl");
elseif ($action == ACT_SUBNET_ADD):
// ========== VARIANT: subnet to vlan =========================================
$sql = "SELECT vlan_id AS id, vlan_name AS name, vlan_number AS number
FROM vlan
WHERE vlan_id=?";
$sth = $dbh->prepare($sql);
$sth->execute([$id]);
$smarty->assign("vlan", $sth->fetch(PDO::FETCH_OBJ));
// possible subnets to add to vlan
// - exclude already assingned subnets from selection
$sql = "SELECT subnet_id, subnet_address, subnet_mask
FROM subnet
WHERE subnet_id NOT IN (SELECT subnet_id FROM subnetvlan WHERE vlan_id=?)
ORDER BY INET_ATON(subnet_address)";
$sth = $dbh->prepare($sql);
$sth->execute([$id]);
$subnets = $sth->fetchAll();
foreach ($subnets as $subnet) {
$subnet_options[$subnet['subnet_id']] = $subnet['subnet_address'].'/'.$subnet['subnet_mask'];
}
$smarty->assign("subnet_options", $subnet_options);
$smarty->display("vlansubnetadd.tpl");
elseif ($action == ACT_SUBNET_DEL):
// ========== VARIANT: subnet to vlan =========================================
$sql = "SELECT vlan_id AS id, vlan_name AS name, vlan_number AS number
FROM vlan
WHERE vlan_id=?";
$sth = $dbh->prepare($sql);
$sth->execute([$id]);
$smarty->assign("vlan", $sth->fetch(PDO::FETCH_OBJ));
// TODO only linked subnets!
$smarty->assign("subnet_options", db_get_options_subnet());
$smarty->display("vlansubnetdel.tpl");
else:
// ========== ERROR UNKNOWN VARIANT ===========================================
echo "<p>Unknown function call: Please report to system development!</p>\n";
endif; // $action == ...
// ========== END OF VARIANTS =================================================
$smarty->display('footer.tpl');