<?php
	/*****************************************************************************
	IP Reg, a PHP/MySQL IPAM tool
	Copyright (C) 2007-2009 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
	*****************************************************************************/
	
	// start page
		// includes
		include("includes.php");
		
		// get id
		$node_id = sanitize($_GET['node_id']);
		
		// start output
		include("header.php");
		
		// set template
		$tp = new Template("tpl/nodeview.tpl", $config_yapter_error);
		
		// set language variables
		$tp->setvars($lang);
	
	// setup node
		// build query
		$query = "SELECT
				asset.asset_id AS asset_id,
				asset.asset_name AS asset_name,
				node.node_id AS node_id,
				node.node_ip AS node_ip,
				node.node_mac AS node_mac,
				node.node_dns1 AS node_dns1,
				node.node_dns2 AS node_dns2,
				node.node_info AS node_info,
				subnet.subnet_id AS subnet_id,
				subnet.subnet_address AS subnet_address,
				subnet.subnet_mask AS subnet_mask
			FROM
				asset,
				node,
				subnet
			WHERE
				asset.asset_id=node.asset_id
				AND node.node_id=" . $node_id . "
				AND subnet.subnet_id=node.subnet_id";
		
		// run query
		$nodes = $db->db_select($query);
		
		// count results
		$node_counter = count($nodes);
		
		// counter to tpl
		$tp->set("node_counter", $node_counter);
		
		// any nodes?
		if ($node_counter>0) {
			// get objects
			foreach($nodes AS $node) {
				// send to tpl
				$tp->set("asset_id", $node['asset_id']);
				$tp->set("asset_name", $node['asset_name']);
				
				$tp->set("node_id", $node['node_id']);
				$tp->set("node_ip", $node['node_ip']);
				$tp->set("node_mac", write_mac($node['node_mac']));
				$tp->set("node_dns1", $node['node_dns1']);
				$tp->set("node_dns2", $node['node_dns2']);
				$tp->set("node_info", nl2br($node['node_info']));
				
				$tp->set("subnet_id", $node['subnet_id']);
				$tp->set("subnet_address", $node['subnet_address']);
				$tp->set("subnet_mask", $node['subnet_mask']);
				
				// parse row
				$tp->parse("node_row");
			}
			
			// parse block
			$tp->parse("node_table");
		} else {
			// hide block
			$tp->hide("node_table");
		}
	
	// setup nat
		// build query
		$query = "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
			FROM
				asset asset_ext,
				asset asset_int,
				nat,
				node node_ext,
				node 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)";
		
		// run query
		$nats = $db->db_select($query);
		
		// count results
		$nat_counter = count($nats);
		
		// counter to tpl
		$tp->set("nat_counter", $nat_counter);
		
		// any nodes?
		if ($nat_counter>0) {
			// get objects
			foreach($nats AS $nat) {
				if($node_id==$nat['nat_ext']) {
					// send to tpl
					$tp->set("nat_asset_id", $nat['asset_id_int']);
					$tp->set("nat_asset_name", $nat['asset_name_int']);
					
					$tp->set("nat_type", $lang['lang_nat_type_' . $nat['nat_type']]);
					
					$tp->set("nat_node_id", $nat['nat_int']);
					$tp->set("nat_node_ip", $nat['node_ip_int']);
				} else if($node_id==$nat['nat_int']) {
					// send to tpl
					$tp->set("nat_asset_id", $nat['asset_id_ext']);
					$tp->set("nat_asset_name", $nat['asset_name_ext']);
					
					$tp->set("nat_type", $lang['lang_nat_type_' . $nat['nat_type']]);
					
					$tp->set("nat_node_id", $nat['nat_ext']);
					$tp->set("nat_node_ip", $nat['node_ip_ext']);
				}
				
				// parse row
				$tp->parse("nat_row");
			}
			
			// parse block
			$tp->parse("nat_table");
		} else {
			// hide block
			$tp->hide("nat_table");
		}
	
	// end page
		// output
		$tp->parse();
		$tp->spit();
			
		include("footer.php");
?>