<?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
*****************************************************************************/

// global version string
$config_version = 'v0.8';

// available languages
$config_lang = array('de', 'en');

include("lib/functions.php");

//require("lib/db.class.php");
//$db = new Db($dblink);

//require("lib/user.class.php");
// $user = new User();

require_once('smarty3/Smarty.class.php');
$smarty = new Smarty();
$smarty->template_dir = 'tpl';
$smarty->compile_dir = 'tpl_c';
$smarty->registerPlugin('function', 'treelist', 'print_tree');

$smarty->assign("suser_tooltips", $_SESSION['suser_tooltips'] ?? 'off');

// ========== DATABASE FUCTIONS ===============================================

function db_load_enum($table, $column) {
    // returns array of enum-values as defined in database
    global $dbh;
    $sql = "SELECT TRIM(TRAILING ')' FROM SUBSTRING(column_type,6))
            FROM information_schema.columns
            WHERE table_name=? AND column_name=?";
    $sth = $dbh->prepare($sql);
    $sth->execute([$table, $column]);
    return array_map(fn($x) => trim($x, "'"), explode(',', $sth->fetch(PDO::FETCH_NUM)));
}

function db_get_options_asset() {
    global $dbh;
    $sql = "SELECT asset_id, asset_name FROM asset ORDER BY asset_name";
    $sth = $dbh->query($sql);
    foreach ($sth->fetchAll(PDO::FETCH_NUM) as $rec) {
        $options[$rec[0]] = $rec[1];
    }
    return $options;
}

function db_get_options_assetclass() {
    global $dbh;
    $sql = "SELECT assetclass_id, assetclass_name FROM assetclass ORDER BY assetclass_name";
    $sth = $dbh->query($sql);
    foreach ($sth->fetchAll(PDO::FETCH_NUM) as $rec) {
        $options[$rec[0]] = $rec[1];
    }
    return $options;
}

function db_get_options_assetclassgroup() {
    global $dbh;
    $sql = "SELECT assetclassgroup_id, assetclassgroup_name FROM assetclassgroup ORDER BY assetclassgroup_name";
    $sth = $dbh->query($sql);
    foreach ($sth->fetchAll(PDO::FETCH_NUM) as $rec) {
        $options[$rec[0]] = $rec[1];
    }
    return $options;
}

function db_get_options_location($default = NULL) {
    global $dbh;
    $options = array();
    if ($default != NULL) {
        $options[0] = $default;
    }
    $sql = "SELECT location_id, location_name FROM location ORDER BY location_name";
    $sth = $dbh->query($sql);
    foreach ($sth->fetchAll(PDO::FETCH_NUM) as $rec) {
        $options[$rec[0]] = $rec[1];
    }
    return $options;
}

function db_get_options_subnet() {
    global $dbh;
    $sql = "SELECT subnet_id,
                CONCAT_WS('/', subnet_address, subnet_mask) AS subnet_name
            FROM subnet
            ORDER BY INET_ATON(subnet_address)";
    $sth = $dbh->query($sql);
    foreach ($sth->fetchAll(PDO::FETCH_NUM) as $rec) {
        $options[$rec[0]] = $rec[1];
    }
    return $options;
}

function db_get_options_vlan($default = NULL) {
    global $dbh;
    $options = array();
    if ($default != NULL) {
        $options[0] = $default;
    }
    $sql = "SELECT vlan_id, vlan_name FROM vlan ORDER BY vlan_name";
    $sth = $dbh->query($sql);
    foreach ($sth->fetchAll(PDO::FETCH_NUM) as $rec) {
        $options[$rec[0]] = $rec[1];
    }
    return $options;
}

function db_get_options_zone($default = NULL) {
    global $dbh;
    $options = array();
    if ($default != NULL) {
        $options[0] = $default;
    }
    $sql = "SELECT zone_id, zone_origin FROM zone ORDER BY zone_origin";
    $sth = $dbh->query($sql);
    foreach ($sth->fetchAll(PDO::FETCH_NUM) as $rec) {
        $options[$rec[0]] = $rec[1];
    }
    return $options;
}

?>