<?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($_GET['node_id']) && (!empty($_GET['node_id']))) {
    $node_id = sanitize($_GET['node_id']);
} else {
    // redirect to error page
    header_location("comments.php?comments=error");
    exit;
}

include("header.php");

// node
$sql = "SELECT
            asset.asset_id,
            asset.asset_name,
            node.node_id AS id,
            node.node_ip AS ip,
            node.node_mac AS mac,
            node.node_dns1 AS dns1,
            node.node_dns2 AS dns2,
            node.node_info AS info,
            node.node_type AS type,
            subnet.subnet_id,
            subnet.subnet_address,
            subnet.subnet_mask,
            zone.zone_origin
        FROM
            node
            LEFT JOIN asset USING (asset_id)
            LEFT JOIN subnet USING (subnet_id)
            LEFT JOIN zone USING (zone_id)
        WHERE
            node.node_id=?";
$sth = $dbh->prepare($sql);
$sth->execute([$node_id]);

$node = $sth->fetch(PDO::FETCH_OBJ);
$node->mac = write_mac($node->mac);
$smarty->assign("node", $node);

// nat
$sql = "SELECT
            asset_ext.asset_id AS asset_id_ext,
            asset_int.asset_id AS asset_id_int,
            asset_ext.asset_name AS asset_name_ext,
            asset_int.asset_name AS asset_name_int,
            nat.nat_id AS nat_id,
            nat.nat_type AS nat_type,
            nat.nat_ext AS nat_ext,
            nat.nat_int AS nat_int,
            node_ext.node_ip AS node_ip_ext,
            node_int.node_ip AS node_ip_int,
            node_int.node_id AS node_id_int,
            node_ext.node_id AS node_id_ext
        FROM
            asset AS asset_ext,
            asset AS asset_int,
            nat,
            node AS node_ext,
            node AS node_int
        WHERE
            (nat.nat_ext=:node_id OR nat.nat_int=:node_id)
            AND node_ext.node_id=nat.nat_ext
            AND node_int.node_id=nat.nat_int
            AND asset_ext.asset_id=node_ext.asset_id
            AND asset_int.asset_id=node_int.asset_id
        ORDER BY
            INET_ATON(node_ext.node_ip),
            INET_ATON(node_int.node_ip)";

$sth = $dbh->prepare($sql);
$sth->execute(['node_id' => $node_id]);

$smarty->assign("natrules", $sth->fetchAll());

$smarty->display("nodeview.tpl");

include("footer.php");
?>