Refactor interface linking system:

- Remove 'interface_connected_asset_interface' column usage
- Introduce 'asset_interface_links' table for one-to-one connections
- Update add/edit/delete queries and modals to handle new schema
- Exclude already-connected interfaces in dropdowns
- Improve data integrity and simplify linking logic
This commit is contained in:
johnnyq 2025-01-18 13:04:56 -05:00
parent d2b3970a7b
commit 360974d9f4
7 changed files with 454 additions and 181 deletions

View File

@ -105,21 +105,31 @@ if (isset($_GET['asset_id'])) {
ai.interface_port,
ai.interface_primary,
ai.interface_notes,
ai.interface_connected_asset_interface,
n.network_name,
n.network_id,
connected_assets.asset_name AS connected_asset_name,
connected_interfaces.interface_id AS connected_interface_id,
connected_interfaces.interface_name AS connected_interface_name,
connected_interfaces.interface_port AS connected_interface_port
FROM asset_interfaces ai
LEFT JOIN assets a ON a.asset_id = ai.interface_asset_id
LEFT JOIN networks n ON n.network_id = ai.interface_network_id
LEFT JOIN asset_interfaces connected_interfaces ON connected_interfaces.interface_id = ai.interface_connected_asset_interface
LEFT JOIN assets connected_assets ON connected_assets.asset_id = connected_interfaces.interface_asset_id
WHERE ai.interface_asset_id = $asset_id
connected_interfaces.interface_port AS connected_interface_port,
connected_assets.asset_name AS connected_asset_name
FROM asset_interfaces AS ai
LEFT JOIN networks AS n
ON n.network_id = ai.interface_network_id
LEFT JOIN asset_interface_links AS ail
ON (ail.interface_a_id = ai.interface_id OR ail.interface_b_id = ai.interface_id)
LEFT JOIN asset_interfaces AS connected_interfaces
ON (
(ail.interface_a_id = ai.interface_id AND ail.interface_b_id = connected_interfaces.interface_id)
OR
(ail.interface_b_id = ai.interface_id AND ail.interface_a_id = connected_interfaces.interface_id)
)
LEFT JOIN assets AS connected_assets
ON connected_assets.asset_id = connected_interfaces.interface_asset_id
WHERE
ai.interface_asset_id = $asset_id
AND ai.interface_archived_at IS NULL
ORDER BY ai.interface_name ASC
");
$interface_count = mysqli_num_rows($sql_related_interfaces);
// Related Files
@ -340,7 +350,9 @@ if (isset($_GET['asset_id'])) {
<div class="card-header py-2">
<h3 class="card-title mt-2"><i class="fa fa-fw fa-ethernet mr-2"></i><?php echo $asset_name; ?> Network Interfaces</h3>
<div class="card-tools">
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#addAssetInterfaceModal"><i class="fas fa-plus mr-2"></i>New Interface</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#addAssetInterfaceModal">
<i class="fas fa-plus mr-2"></i>New Interface
</button>
</div>
</div>
<div class="card-body">
@ -358,42 +370,37 @@ if (isset($_GET['asset_id'])) {
</tr>
</thead>
<tbody>
<?php while ($row = mysqli_fetch_array($sql_related_interfaces)) { ?>
<?php
while ($row = mysqli_fetch_array($sql_related_interfaces)) {
$interface_id = intval($row['interface_id']);
$interface_name = nullable_htmlentities($row['interface_name']);
$interface_mac = nullable_htmlentities($row['interface_mac']);
if ($interface_mac) {
$interface_mac_display = "$interface_mac";
} else {
$interface_mac_display = "-";
}
$interface_ip = nullable_htmlentities($row['interface_ip']);
if ($interface_ip) {
$interface_ip_display = "$interface_ip";
} else {
$interface_ip_display = "-";
}
$interface_ipv6 = nullable_htmlentities($row['interface_ipv6']);
$interface_port = nullable_htmlentities($row['interface_port']);
if ($interface_port) {
$interface_port_display = "$interface_port";
} else {
$interface_port_display = "-";
}
$interface_primary = intval($row['interface_primary']);
$network_id = intval($row['network_id']);
$network_name = nullable_htmlentities($row['network_name']);
if ($network_name) {
$network_name_display = "<i class='fas fa-fw fa-network-wired mr-1'></i>$network_name $network_id";
} else {
$network_name_display = "-";
}
$interface_notes = nullable_htmlentities($row['interface_notes']);
$connected_asset_interface = intval($row['interface_connected_asset_interface']);
// Prepare display text
$interface_mac_display = $interface_mac ?: '-';
$interface_ip_display = $interface_ip ?: '-';
$interface_port_display = $interface_port ?: '-';
$network_name_display = $network_name
? "<i class='fas fa-fw fa-network-wired mr-1'></i>$network_name $network_id"
: '-';
// Connected interface details
$connected_asset_name = nullable_htmlentities($row['connected_asset_name']);
$connected_asset_port = nullable_htmlentities($row['connected_interface_port']);
$connected_interface_port = nullable_htmlentities($row['connected_interface_port']);
// Show either "-" or "AssetName - Port"
if ($connected_asset_name) {
$connected_to_display = "<strong>$connected_asset_name</strong> - $connected_interface_port";
} else {
$connected_to_display = "-";
}
?>
<tr>
<td>
@ -406,7 +413,7 @@ if (isset($_GET['asset_id'])) {
<td><?php echo $interface_ip_display; ?></td>
<td><?php echo $interface_port_display; ?></td>
<td><?php echo $network_name_display; ?></td>
<td><?php echo "<strong>$connected_asset_name</strong> - $connected_asset_port"; ?></td>
<td><?php echo $connected_to_display; ?></td>
<td>
<div class="dropdown dropleft text-center">
<button class="btn btn-secondary btn-sm" type="button" data-toggle="dropdown">
@ -416,29 +423,22 @@ if (isset($_GET['asset_id'])) {
<a class="dropdown-item" href="#" data-toggle="modal" data-target="#editAssetInterfaceModal<?php echo $interface_id; ?>">
<i class="fas fa-fw fa-edit mr-2"></i>Edit
</a>
<?php if ($session_user_role == 3 && $interface_primary == 0) { ?>
<?php if ($session_user_role == 3 && $interface_primary == 0): ?>
<div class="dropdown-divider"></div>
<a class="dropdown-item text-danger text-bold" href="post.php?delete_asset_interface=<?php echo $interface_id; ?>&csrf_token=<?php echo $_SESSION['csrf_token'] ?>">
<a class="dropdown-item text-danger text-bold" href="post.php?delete_asset_interface=<?php echo $interface_id; ?>&csrf_token=<?php echo $_SESSION['csrf_token']; ?>">
<i class="fas fa-fw fa-trash mr-2"></i>Delete
</a>
<?php } ?>
<?php endif; ?>
</div>
</div>
</td>
</tr>
<?php
require "modals/client_asset_interface_edit_modal.php";
}
?>
<?php require "modals/client_asset_interface_edit_modal.php"; ?>
<?php } ?>
</tbody>
</table>
</div>
</div>
</div>

View File

@ -2424,10 +2424,48 @@ if (LATEST_DATABASE_VERSION > CURRENT_DATABASE_VERSION) {
mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '1.7.8'");
}
// if (CURRENT_DATABASE_VERSION == '1.7.8') {
// // Insert queries here required to update to DB version 1.7.9
if (CURRENT_DATABASE_VERSION == '1.7.8') {
// Use a seperate table for Interface connections / links. This will make it easier to manage.
$createInterfaceLinksTable = "
CREATE TABLE IF NOT EXISTS `asset_interface_links` (
`interface_link_id` INT AUTO_INCREMENT PRIMARY KEY,
`interface_a_id` INT NOT NULL,
`interface_b_id` INT NOT NULL,
`interface_link_type` VARCHAR(100) NULL,
`interface_link_status` VARCHAR(50) NULL,
`interface_link_created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`interface_link_updated_at` DATETIME NULL ON UPDATE CURRENT_TIMESTAMP,
CONSTRAINT `fk_interface_a`
FOREIGN KEY (`interface_a_id`)
REFERENCES `asset_interfaces` (`interface_id`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `fk_interface_b`
FOREIGN KEY (`interface_b_id`)
REFERENCES `asset_interfaces` (`interface_id`)
ON DELETE CASCADE
ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
";
mysqli_query($mysqli, $createInterfaceLinksTable) or die(mysqli_error($mysqli));
// Drop the old column from asset_interfaces if it exists
$dropConnectedColumn = "
ALTER TABLE `asset_interfaces`
DROP COLUMN IF EXISTS `interface_connected_asset_interface`
";
mysqli_query($mysqli, $dropConnectedColumn) or die(mysqli_error($mysqli));
mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '1.7.9'");
}
// if (CURRENT_DATABASE_VERSION == '1.7.9') {
// // Insert queries here required to update to DB version 1.8.0
// // Then, update the database to the next sequential version
// mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '1.7.9'");
// mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '1.8.0'");
// }
} else {

View File

@ -5,4 +5,4 @@
* It is used in conjunction with database_updates.php
*/
DEFINE("LATEST_DATABASE_VERSION", "1.7.8");
DEFINE("LATEST_DATABASE_VERSION", "1.7.9");

75
db.sql
View File

@ -134,6 +134,29 @@ CREATE TABLE `asset_history` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `asset_interface_links`
--
DROP TABLE IF EXISTS `asset_interface_links`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `asset_interface_links` (
`interface_link_id` int(11) NOT NULL AUTO_INCREMENT,
`interface_a_id` int(11) NOT NULL,
`interface_b_id` int(11) NOT NULL,
`interface_link_type` varchar(100) DEFAULT NULL,
`interface_link_status` varchar(50) DEFAULT NULL,
`interface_link_created_at` datetime NOT NULL DEFAULT current_timestamp(),
`interface_link_updated_at` datetime DEFAULT NULL ON UPDATE current_timestamp(),
PRIMARY KEY (`interface_link_id`),
KEY `fk_interface_a` (`interface_a_id`),
KEY `fk_interface_b` (`interface_b_id`),
CONSTRAINT `fk_interface_a` FOREIGN KEY (`interface_a_id`) REFERENCES `asset_interfaces` (`interface_id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_interface_b` FOREIGN KEY (`interface_b_id`) REFERENCES `asset_interfaces` (`interface_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `asset_interfaces`
--
@ -155,7 +178,6 @@ CREATE TABLE `asset_interfaces` (
`interface_updated_at` datetime DEFAULT NULL ON UPDATE current_timestamp(),
`interface_archived_at` datetime DEFAULT NULL,
`interface_network_id` int(11) DEFAULT NULL,
`interface_connected_asset_interface` int(11) NOT NULL DEFAULT 0,
`interface_asset_id` int(11) NOT NULL,
PRIMARY KEY (`interface_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
@ -353,7 +375,8 @@ DROP TABLE IF EXISTS `client_stripe`;
CREATE TABLE `client_stripe` (
`client_id` int(11) NOT NULL,
`stripe_id` varchar(255) NOT NULL,
`stripe_pm` varchar(255) DEFAULT NULL
`stripe_pm` varchar(255) DEFAULT NULL,
PRIMARY KEY (`client_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
@ -651,6 +674,24 @@ CREATE TABLE `documents` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `domain_history`
--
DROP TABLE IF EXISTS `domain_history`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `domain_history` (
`domain_history_id` int(11) NOT NULL AUTO_INCREMENT,
`domain_history_column` varchar(200) NOT NULL,
`domain_history_old_value` text NOT NULL,
`domain_history_new_value` text NOT NULL,
`domain_history_domain_id` int(11) NOT NULL,
`domain_history_modified_at` datetime NOT NULL DEFAULT current_timestamp(),
PRIMARY KEY (`domain_history_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `domains`
--
@ -682,24 +723,6 @@ CREATE TABLE `domains` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `domain_history`
--
DROP TABLE IF EXISTS `domain_history`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `domain_history` (
`domain_history_id` int(11) NOT NULL AUTO_INCREMENT,
`domain_history_column` varchar(200) NOT NULL,
`domain_history_old_value` text NOT NULL,
`domain_history_new_value` text NOT NULL,
`domain_history_domain_id` int(11) NOT NULL,
`domain_history_modified_at` datetime NOT NULL DEFAULT current_timestamp(),
PRIMARY KEY (`domain_history_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `email_queue`
--
@ -1606,12 +1629,12 @@ DROP TABLE IF EXISTS `services`;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `services` (
`service_id` int(11) NOT NULL AUTO_INCREMENT,
`service_name` varchar(200) NOT NULL,
`service_description` varchar(200) NOT NULL,
`service_category` varchar(20) NOT NULL,
`service_importance` varchar(10) NOT NULL,
`service_name` varchar(200) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,
`service_description` varchar(200) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,
`service_category` varchar(20) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,
`service_importance` varchar(10) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,
`service_backup` varchar(200) DEFAULT NULL,
`service_notes` text NOT NULL,
`service_notes` text CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,
`service_created_at` datetime NOT NULL DEFAULT current_timestamp(),
`service_updated_at` datetime DEFAULT NULL ON UPDATE current_timestamp(),
`service_accessed_at` datetime DEFAULT NULL,
@ -2320,4 +2343,4 @@ CREATE TABLE `vendors` (
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2025-01-14 23:47:59
-- Dump completed on 2025-01-18 13:03:55

View File

@ -14,6 +14,7 @@
<div class="modal-body bg-white">
<!-- Interface Name -->
<div class="form-group">
<label>Interface Name</label>
<div class="input-group">
@ -24,6 +25,7 @@
</div>
</div>
<!-- MAC Address -->
<div class="form-group">
<label>MAC Address</label>
<div class="input-group">
@ -34,6 +36,7 @@
</div>
</div>
<!-- IP (with optional DHCP checkbox) -->
<div class="form-group">
<label>IP or DHCP</label>
<div class="input-group">
@ -49,6 +52,7 @@
</div>
</div>
<!-- IPv6 -->
<div class="form-group">
<label>IPv6</label>
<div class="input-group">
@ -59,6 +63,7 @@
</div>
</div>
<!-- Port -->
<div class="form-group">
<label>Port</label>
<div class="input-group">
@ -69,6 +74,7 @@
</div>
</div>
<!-- Network -->
<div class="form-group">
<label>Network</label>
<div class="input-group">
@ -78,15 +84,15 @@
<select class="form-control select2" name="network">
<option value="">- None -</option>
<?php
$sql_network_select = mysqli_query($mysqli, "SELECT * FROM networks WHERE network_archived_at IS NULL AND network_client_id = $client_id ORDER BY network_name ASC");
while ($row = mysqli_fetch_array($sql_network_select)) {
$network_id = $row['network_id'];
$network_name = nullable_htmlentities($row['network_name']);
$network = nullable_htmlentities($row['network']);
?>
<option value="<?php echo $network_id; ?>"><?php echo $network_name; ?> - <?php echo $network; ?></option>
<option value="<?php echo $network_id; ?>">
<?php echo "$network_name - $network"; ?>
</option>
<?php } ?>
</select>
</div>
@ -101,21 +107,32 @@
<select class="form-control select2" name="connected_to">
<option value="">- None -</option>
<?php
$sql_interfaces_select = mysqli_query($mysqli, "
SELECT i.interface_id, i.interface_port, a.asset_name
FROM asset_interfaces i
LEFT JOIN assets a ON a.asset_id = i.interface_asset_id
WHERE a.asset_archived_at IS NULL
AND a.asset_client_id = $client_id
AND a.asset_id != $asset_id
AND i.interface_id NOT IN (SELECT interface_a_id FROM asset_interface_links)
AND i.interface_id NOT IN (SELECT interface_b_id FROM asset_interface_links)
ORDER BY a.asset_name ASC, i.interface_port ASC
");
$sql_interfaces_select = mysqli_query($mysqli, "SELECT * FROM asset_interfaces LEFT JOIN assets ON interface_asset_id = asset_id WHERE asset_archived_at IS NULL AND asset_client_id = $client_id ORDER BY asset_name ASC, interface_port ASC");
while ($row = mysqli_fetch_array($sql_interfaces_select)) {
$interface_id_select = intval($row['interface_id']);
$interface_port_select = nullable_htmlentities($row['interface_port']);
$asset_type_select = nullable_htmlentities($row['asset_type']);
$asset_name_select = nullable_htmlentities($row['asset_name']);
?>
<option value="<?php echo $interface_id_select; ?>"><?php echo "$asset_name_select - $interface_port_select"; ?></option>
<option value="<?php echo $interface_id_select; ?>">
<?php echo "$asset_name_select - $interface_port_select"; ?>
</option>
<?php } ?>
</select>
</div>
</div>
<!-- Notes -->
<div class="form-group">
<textarea class="form-control" rows="5" placeholder="Enter some notes" name="notes"></textarea>
</div>
@ -123,7 +140,9 @@
</div>
<div class="modal-footer bg-white">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="submit" name="add_asset_interface" class="btn btn-primary"><i class="fa fa-check"></i> Create</button>
<button type="submit" name="add_asset_interface" class="btn btn-primary">
<i class="fa fa-check"></i> Create
</button>
</div>
</form>
</div>

View File

@ -1,74 +1,150 @@
<?php
// Determine the linked interface for $interface_id
$linked_interface_id = null;
$sql_link = mysqli_query($mysqli, "
SELECT interface_a_id, interface_b_id
FROM asset_interface_links
WHERE interface_a_id = $interface_id
OR interface_b_id = $interface_id
LIMIT 1
");
if ($link_row = mysqli_fetch_assoc($sql_link)) {
if ($link_row['interface_a_id'] == $interface_id) {
$linked_interface_id = intval($link_row['interface_b_id']);
} else {
$linked_interface_id = intval($link_row['interface_a_id']);
}
}
?>
<div class="modal" id="editAssetInterfaceModal<?php echo $interface_id; ?>" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content bg-dark">
<div class="modal-header">
<h5 class="modal-title"><i class="fa fa-fw fa-ethernet mr-2"></i>Editing: <?php echo $interface_name; ?></h5>
<h5 class="modal-title">
<i class="fa fa-fw fa-ethernet mr-2"></i>
Editing: <?php echo htmlspecialchars($interface_name, ENT_QUOTES, 'UTF-8'); ?>
</h5>
<button type="button" class="close text-white" data-dismiss="modal">
<span>&times;</span>
</button>
</div>
<form action="post.php" method="post" autocomplete="off">
<input type="hidden" name="csrf_token" value="<?php echo $_SESSION['csrf_token'] ?>">
<input type="hidden" name="csrf_token" value="<?php echo $_SESSION['csrf_token']; ?>">
<input type="hidden" name="interface_id" value="<?php echo $interface_id; ?>">
<div class="modal-body bg-white" <?php if (lookupUserPermission('module_support') <= 1) { echo 'inert'; } ?>>
<!-- Interface Name -->
<div class="form-group">
<label>Interface Name</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-fw fa-ethernet"></i></span>
</div>
<input type="text" class="form-control" name="name" placeholder="Interface Name" maxlength="200" value="<?php echo $interface_name; ?>" required>
<input
type="text"
class="form-control"
name="name"
placeholder="Interface Name"
maxlength="200"
value="<?php echo nullable_htmlentities($interface_name); ?>"
required
>
</div>
</div>
<!-- MAC Address -->
<div class="form-group">
<label>MAC Address</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-fw fa-ethernet"></i></span>
</div>
<input type="text" class="form-control" name="mac" placeholder="MAC Address" maxlength="200" value="<?php echo $interface_mac; ?>" data-inputmask="'alias': 'mac'" data-mask>
<input
type="text"
class="form-control"
name="mac"
placeholder="MAC Address"
maxlength="200"
value="<?php echo nullable_htmlentities($interface_mac); ?>"
data-inputmask="'alias': 'mac'"
data-mask
>
</div>
</div>
<!-- IPv4 or DHCP -->
<div class="form-group">
<label>IPv4 or DHCP</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-fw fa-ethernet"></i></span>
</div>
<input type="text" class="form-control" name="ip" placeholder="IP Address" maxlength="200" value="<?php echo $interface_ip; ?>" data-inputmask="'alias': 'ip'" data-mask>
<input
type="text"
class="form-control"
name="ip"
placeholder="IP Address"
maxlength="200"
value="<?php echo nullable_htmlentities($interface_ip); ?>"
data-inputmask="'alias': 'ip'"
data-mask
>
<div class="input-group-append">
<div class="input-group-text">
<input type="checkbox" name="dhcp" value="1" title="Check to mark address as DHCP controlled" <?php if ($interface_ip == 'DHCP') { echo "checked"; } ?>>
<input
type="checkbox"
name="dhcp"
value="1"
title="Check to mark address as DHCP controlled"
<?php if ($interface_ip === 'DHCP') echo "checked"; ?>
>
</div>
</div>
</div>
</div>
<!-- IPv6 -->
<div class="form-group">
<label>IPv6</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-fw fa-ethernet"></i></span>
</div>
<input type="text" class="form-control" name="ipv6" placeholder="IPv6 Address" maxlength="200" value="<?php echo $interface_ipv6; ?>">
<input
type="text"
class="form-control"
name="ipv6"
placeholder="IPv6 Address"
maxlength="200"
value="<?php echo nullable_htmlentities($interface_ipv6); ?>"
>
</div>
</div>
<!-- Port -->
<div class="form-group">
<label>Port</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-fw fa-ethernet"></i></span>
</div>
<input type="text" class="form-control" name="port" placeholder="Interface Port ex. eth0" maxlength="200" value="<?php echo $interface_port; ?>">
<input
type="text"
class="form-control"
name="port"
placeholder="Interface Port ex. eth0"
maxlength="200"
value="<?php echo nullable_htmlentities($interface_port); ?>"
>
</div>
</div>
<!-- Network -->
<div class="form-group">
<label>Network</label>
<div class="input-group">
@ -78,20 +154,29 @@
<select class="form-control select2" name="network">
<option value="">- None -</option>
<?php
$sql_network_select = mysqli_query($mysqli, "
SELECT network_id, network_name, network
FROM networks
WHERE network_archived_at IS NULL
AND network_client_id = $client_id
ORDER BY network_name ASC
");
while ($net_row = mysqli_fetch_array($sql_network_select)) {
$network_id_select = intval($net_row['network_id']);
$network_name_select = nullable_htmlentities($net_row['network_name']);
$network_select = nullable_htmlentities($net_row['network']);
$sql_network_select = mysqli_query($mysqli, "SELECT * FROM networks WHERE network_archived_at IS NULL AND network_client_id = $client_id ORDER BY network_name ASC");
while ($row = mysqli_fetch_array($sql_network_select)) {
$network_id_select = intval($row['network_id']);
$network_name_select = nullable_htmlentities($row['network_name']);
$network_select = nullable_htmlentities($row['network']);
$selected = ($network_id == $network_id_select) ? 'selected' : '';
echo "<option value='$network_id_select' $selected>$network_name_select - $network_select</option>";
}
?>
<option <?php if ($network_id == $network_id_select) { echo "selected"; } ?> value="<?php echo $network_id_select; ?>"><?php echo $network_name_select; ?> - <?php echo $network_select; ?></option>
<?php } ?>
</select>
</div>
</div>
<!-- Connected to (One-to-One) -->
<div class="form-group">
<label>Connected to</label>
<div class="input-group">
@ -101,21 +186,39 @@
<select class="form-control select2" name="connected_to">
<option value="">- None -</option>
<?php
$sql_interfaces_select = mysqli_query($mysqli, "
SELECT i.interface_id, i.interface_port, a.asset_name
FROM asset_interfaces i
LEFT JOIN assets a ON a.asset_id = i.interface_asset_id
WHERE a.asset_archived_at IS NULL
AND a.asset_client_id = $client_id
AND i.interface_id != $interface_id
AND a.asset_id != $asset_id
AND (
(
i.interface_id NOT IN (SELECT interface_a_id FROM asset_interface_links)
AND i.interface_id NOT IN (SELECT interface_b_id FROM asset_interface_links)
)
OR i.interface_id = " . (int)$linked_interface_id . "
)
ORDER BY a.asset_name ASC, i.interface_port ASC
");
while ($row_if = mysqli_fetch_array($sql_interfaces_select)) {
$iface_id_select = intval($row_if['interface_id']);
$iface_port_select = nullable_htmlentities($row_if['interface_port']);
$iface_asset_name_select = nullable_htmlentities($row_if['asset_name']);
$sql_interfaces_select = mysqli_query($mysqli, "SELECT * FROM asset_interfaces LEFT JOIN assets ON interface_asset_id = asset_id WHERE asset_archived_at IS NULL AND asset_client_id = $client_id ORDER BY asset_name ASC, interface_port ASC");
while ($row = mysqli_fetch_array($sql_interfaces_select)) {
$interface_id_select = intval($row['interface_id']);
$interface_port_select = nullable_htmlentities($row['interface_port']);
$asset_type_select = nullable_htmlentities($row['asset_type']);
$asset_name_select = nullable_htmlentities($row['asset_name']);
$selected = ($linked_interface_id === $iface_id_select) ? 'selected' : '';
echo "<option value='$iface_id_select' $selected>";
echo "$iface_asset_name_select - $iface_port_select";
echo "</option>";
}
?>
<option <?php if ($connected_asset_interface == $interface_id_select) { echo "selected"; } ?> value="<?php echo $interface_id_select; ?>"><?php echo "$asset_name_select - $interface_port_select"; ?></option>
<?php } ?>
</select>
</div>
</div>
<!-- Notes -->
<div class="form-group">
<textarea class="form-control" rows="5" placeholder="Enter some notes" name="notes"><?php echo $interface_notes; ?></textarea>
</div>
@ -123,7 +226,9 @@
</div>
<div class="modal-footer bg-white">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="submit" name="edit_asset_interface" class="btn btn-primary"><i class="fa fa-check mr-2"></i>Save</button>
<button type="submit" name="edit_asset_interface" class="btn btn-primary">
<i class="fa fa-check mr-2"></i>Save
</button>
</div>
</form>
</div>

View File

@ -676,88 +676,176 @@ if (isset($_POST['export_client_assets_csv'])) {
if (isset($_POST['add_asset_interface'])) {
// 1) Permissions & CSRF
enforceUserPermission('module_support', 2);
validateCSRFToken($_POST['csrf_token']);
// Interface info
// 2) Gather posted values
$interface_id = intval($_POST['interface_id']);
$asset_id = intval($_POST['asset_id']);
// Defines $name, $mac, $ip, $ipv6, $port, $notes, $network, $connected_to, etc.
require_once 'asset_interface_model.php';
// Get Asset Name and Client ID for logging and alert message
$sql = mysqli_query($mysqli,"SELECT asset_name, asset_client_id FROM assets WHERE asset_id = $asset_id");
// 3) Fetch asset info for logging and alert
$sql = mysqli_query($mysqli, "
SELECT asset_name, asset_client_id
FROM assets
WHERE asset_id = $asset_id
");
$row = mysqli_fetch_array($sql);
$asset_name = sanitizeInput($row['asset_name']);
$client_id = intval($row['asset_client_id']);
mysqli_query($mysqli,"INSERT INTO asset_interfaces SET interface_name = '$name', interface_mac = '$mac', interface_ip = '$ip', interface_ipv6 = '$ipv6', interface_port = '$port', interface_notes = '$notes', interface_network_id = $network, interface_connected_asset_interface = $connected_to, interface_asset_id = $asset_id");
// 4) Insert new interface into asset_interfaces (using SET syntax)
$sql_insert = "
INSERT INTO asset_interfaces SET
interface_name = '$name',
interface_mac = '$mac',
interface_ip = '$ip',
interface_ipv6 = '$ipv6',
interface_port = '$port',
interface_notes = '$notes',
interface_network_id = $network,
interface_asset_id = $asset_id
";
mysqli_query($mysqli, $sql_insert);
$interface_id = mysqli_insert_id($mysqli);
$new_interface_id = mysqli_insert_id($mysqli);
//Logging
logAction("Asset Interface", "Create", "$session_name created interface $name for asset $asset_name", $client_id, $asset_id);
// 5) If user selected a connected interface, insert row in asset_interface_links
if (!empty($connected_to) && intval($connected_to) > 0) {
$sql_link = "
INSERT INTO asset_interface_links SET
interface_a_id = $new_interface_id,
interface_b_id = $connected_to
";
mysqli_query($mysqli, $sql_link);
}
// 6) Logging
logAction(
"Asset Interface",
"Create",
"$session_name created interface $name for asset $asset_name",
$client_id,
$asset_id
);
// 7) Alert message + redirect
$_SESSION['alert_message'] = "Interface <strong>$name</strong> created";
header("Location: " . $_SERVER["HTTP_REFERER"]);
exit;
}
if (isset($_POST['edit_asset_interface'])) {
enforceUserPermission('module_support', 2);
validateCSRFToken($_POST['csrf_token']);
// Interface info
$interface_id = intval($_POST['interface_id']);
require_once 'asset_interface_model.php';
// sets: $name, $mac, $ip, $ipv6, $port, $notes, $network, $connected_to, etc.
// Get Asset Name and Client ID for logging and alert message
$sql = mysqli_query($mysqli,"SELECT asset_name, asset_client_id, asset_id FROM asset_interfaces LEFT JOIN assets ON asset_id = interface_asset_id WHERE interface_id = $interface_id");
// 1) Get Asset Name and Client ID for logging and alert message
$sql = mysqli_query($mysqli, "
SELECT asset_name, asset_client_id, asset_id
FROM asset_interfaces
LEFT JOIN assets ON asset_id = interface_asset_id
WHERE interface_id = $interface_id
");
$row = mysqli_fetch_array($sql);
$asset_id = intval($row['asset_id']);
$asset_name= sanitizeInput($row['asset_name']);
$client_id = intval($row['asset_client_id']);
mysqli_query($mysqli,"UPDATE asset_interfaces SET interface_name = '$name', interface_mac = '$mac', interface_ip = '$ip', interface_ipv6 = '$ipv6', interface_port = '$port', interface_notes = '$notes', interface_network_id = $network, interface_connected_asset_interface = $connected_to WHERE interface_id = $interface_id");
// 2) Update the interface details in asset_interfaces
$sql_update = "
UPDATE asset_interfaces SET
interface_name = '$name',
interface_mac = '$mac',
interface_ip = '$ip',
interface_ipv6 = '$ipv6',
interface_port = '$port',
interface_notes = '$notes',
interface_network_id = $network
WHERE interface_id = $interface_id
";
mysqli_query($mysqli, $sql_update);
// Update the Connected device on the connecting device
mysqli_query($mysqli,"UPDATE asset_interfaces SET interface_connected_asset_interface = $interface_id WHERE interface_id = $connected_to");
// 3) Remove any existing link for this interface (one-to-one)
$sql_delete_link = "
DELETE FROM asset_interface_links
WHERE interface_a_id = $interface_id
OR interface_b_id = $interface_id
";
mysqli_query($mysqli, $sql_delete_link);
//Logging
logAction("Asset Interface", "Edit", "$session_name edited interface $name for asset $asset_name", $client_id, $asset_id);
// 4) If user selected a connected interface, create a new link
if (!empty($connected_to) && intval($connected_to) > 0) {
$sql_link = "
INSERT INTO asset_interface_links SET
interface_a_id = $interface_id,
interface_b_id = $connected_to
";
mysqli_query($mysqli, $sql_link);
}
// 5) Logging
logAction(
"Asset Interface",
"Edit",
"$session_name edited interface $name for asset $asset_name",
$client_id,
$asset_id
);
// 6) Alert and redirect
$_SESSION['alert_message'] = "Interface <strong>$name</strong> edited";
header("Location: " . $_SERVER["HTTP_REFERER"]);
exit;
}
if (isset($_GET['delete_asset_interface'])) {
enforceUserPermission('module_support', 2);
validateCSRFToken($_GET['csrf_token']);
$interface_id = intval($_GET['delete_asset_interface']);
$sql = mysqli_query($mysqli,"SELECT asset_name, interface_name, asset_client_id, asset_id FROM asset_interfaces LEFT JOIN assets ON asset_id = interface_asset_id WHERE interface_id = $interface_id");
// 1) Fetch details for logging / alerts
$sql = mysqli_query($mysqli, "
SELECT asset_name, interface_name, asset_client_id, asset_id
FROM asset_interfaces
LEFT JOIN assets ON asset_id = interface_asset_id
WHERE interface_id = $interface_id
");
$row = mysqli_fetch_array($sql);
$asset_id = intval($row['asset_id']);
$interface_name = sanitizeInput($row['interface_name']);
$asset_name = sanitizeInput($row['asset_name']);
$client_id = intval($row['asset_client_id']);
mysqli_query($mysqli,"DELETE FROM asset_interfaces WHERE interface_id = $interface_id");
// 2) Delete the interface this cascadingly delete asset_interface_links
mysqli_query($mysqli, "
DELETE FROM asset_interfaces
WHERE interface_id = $interface_id
");
//Logging
logAction("Asset Interface", "Delete", "$session_name deleted interface $interface_name from asset $asset_name", $client_id, $asset_id);
// 3) Logging
logAction(
"Asset Interface",
"Delete",
"$session_name deleted interface $interface_name from asset $asset_name",
$client_id,
$asset_id
);
// 4) Alert and redirect
$_SESSION['alert_type'] = "error";
$_SESSION['alert_message'] = "Interface <strong>$interface_name</strong> deleted";
header("Location: " . $_SERVER["HTTP_REFERER"]);
header("Location: " . $_SERVER['HTTP_REFERER']);
exit;
}