<?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");
include("header.php");


// get string that was searched for ($search is already set in header.php)
if (empty($search)) {
    // parse nosearch box
    $smarty->assign("nosearch", TRUE);
    $smarty->display("search.tpl");
    include("footer.php");
    exit;
}

// hide nosearch box
$smarty->assign("nosearch", FALSE);
$smarty->assign("search", $search);

$needle = '%' . $search . '%';
$resultcounter = 0;

// asset
$sql = "SELECT asset_id AS id, asset_name AS name, asset_info AS description
        FROM asset
        WHERE asset_name LIKE :needle OR asset_hostname LIKE :needle
		   OR asset_info LIKE :needle
        ORDER BY asset_name";
$sth = $dbh->prepare($sql);
$sth->execute(['needle' => $needle]);

$assets = $sth->fetchAll();
$resultcounter += count($assets);
$smarty->assign("assets", $assets);

// location
$sql = "SELECT location_id AS id, location_name AS name
        FROM location
        WHERE location_name LIKE :needle OR location_info LIKE :needle
        ORDER BY location_name";
$sth = $dbh->prepare($sql);
$sth->execute(['needle' => $needle]);

$locations = $sth->fetchAll();
$resultcounter += count($locations);
$smarty->assign("locations", $locations);

// node
$sql = "SELECT node_id AS id, node_ip AS ip
        FROM node
	    WHERE node_ip LIKE :needle OR node_mac LIKE :needle
           OR node_dns1 LIKE :needle OR node_dns2 LIKE :needle
           OR node_info LIKE :needle
        ORDER BY node_ip";
$sth = $dbh->prepare($sql);
$sth->execute(['needle' => $needle]);

$nodes = $sth->fetchAll();
$resultcounter += count($nodes);
$smarty->assign("nodes", $nodes);

// subnet
$sql = "SELECT subnet_id AS id, subnet_address AS address
        FROM subnet
        WHERE subnet_address LIKE :needle OR subnet_info LIKE :needle
        ORDER BY subnet_address";
$sth = $dbh->prepare($sql);
$sth->execute(['needle' => $needle]);

$subnets = $sth->fetchAll();
$resultcounter += count($subnets);
$smarty->assign("subnets", $subnets);

// vlan
$sql = "SELECT vlan_id AS id, vlan_name AS name
        FROM vlan
        WHERE vlan_name LIKE :needle OR vlan_info LIKE :needle
        ORDER BY vlan_name";
$sth = $dbh->prepare($sql);
$sth->execute(['needle' => $needle]);

$vlans = $sth->fetchAll();
$resultcounter += count($vlans);
$smarty->assign("vlans", $vlans);

// setup zone
$sql = "SELECT zone_id AS id, zone_origin AS origin
        FROM zone
        WHERE zone_origin LIKE :needle OR zone_soa LIKE :needle
           OR zone_hostmaster LIKE :needle OR zone_ns1 LIKE :needle
           OR zone_ns2 LIKE :needle OR zone_ns3 LIKE :needle
           OR zone_mx1 LIKE :needle OR zone_mx2 LIKE :needle
           OR zone_info LIKE :needle
        ORDER BY zone_origin";
$sth = $dbh->prepare($sql);
$sth->execute(['needle' => $needle]);

$zones = $sth->fetchAll();
$resultcounter += count($zones);
$smarty->assign("zones", $zones);

// grand totals
$smarty->assign("resultcounter", $resultcounter);

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

include("footer.php");
?>