template_dir = 'tpl';
$smarty->compile_dir = 'tpl_c';
$smarty->registerPlugin('function', 'treelist', 'print_tree');
$smarty->registerPlugin('function', 'msgout', 'msgout');
if (!empty($_SESSION['suser_id'])) {
    $smarty->assign("suser_name", $_SESSION['suser_displayname']);
    $smarty->assign("suser_tooltips", $_SESSION['suser_tooltips'] ?? 'off');
    $smarty->assign("suser_add", $_SESSION['suser_role_add']);
    $smarty->assign("suser_edit", $_SESSION['suser_role_edit']);
    $smarty->assign("suser_delete", $_SESSION['suser_role_delete']);
    $smarty->assign("suser_manage", $_SESSION['suser_role_manage']);
    $smarty->assign("suser_admin", $_SESSION['suser_role_admin']);
}
// prepare global message system
$g_message = new Message;
$g_warning = new MessageWarning;
$g_error = new MessageError;
$action = ACT_DEFAULT;
// ========== LANGUAGE FUNCTIONS ==============================================
function lang_getfrombrowser($allowed, $default) {
    // get browser most preferred language if possible
    if (empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
        return $default;
    }
    $accepted = preg_split('/,\s*/', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
    $current_lang = $default;
    $current_q = 0;
    foreach ($accepted as $lang) {
        $res = preg_match ('/^([a-z]{1,8}(?:-[a-z]{1,8})*)(?:;\s*q=(0(?:\.[0-9]{1,3})?|1(?:\.0{1,3})?))?$/i',
                   $lang, $matches);
        if (!$res) {
            continue;
        }
        $lang_code = explode ('-', $matches[1]);
        if (isset($matches[2])) {
            $lang_quality = (float)$matches[2];
        } else {
            $lang_quality = 1.0;
        }
        while (count($lang_code)) {
            if (in_array(strtolower(join ('-', $lang_code)), $allowed)) {
                if ($lang_quality > $current_q) {
                    $current_lang = strtolower (join ('-', $lang_code));
                    $current_q = $lang_quality;
                    break;
                }
            }
            array_pop($lang_code);
        }
    }
    return $current_lang;
}
// ========== FEEDBACK FUNCTIONS ==============================================
class Message {
    var $count = 0;
    var $text = array();
    var $caption;
    function Message() {
        $this->caption = 'Information';
    }
    function SetCaption($str) {
        $this->caption = $str;
    }
    function Add($msg) {
        $this->count++;
        $this->text[$this->count] = $msg;
    }
    function GetCount() {
        return $this->count;
    }
    function PrintOut() {
        if ($this->count > 0) {
            echo '
', "\n";
            echo '
', $this->caption, "
\n";
            echo "
\n";
            for ($i=1; $i<=$this->count; $i++) {
                echo "\t- ", $this->text[$i],"\n";
            }
            echo "
\n";
            echo "
', "\n";
            echo '
', $this->caption, "
\n";
            echo "
\n";
            for ($i=1; $i<=$this->count; $i++) {
                echo "\t- ", $this->text[$i],"\n";
            }
            echo "
\n";
            echo "
', "\n";
            echo '
', $this->caption, "
\n";
            echo "
\n";
            for ($i=1; $i<=$this->count; $i++) {
                echo "\t- ", $this->text[$i],"\n";
            }
            echo "
\n";
            echo "
 Please inform your administrator of the exact circumstances of how this situation came about.', strtoupper($action));
}
// ========== 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]);
    // Für PHP < 7.4
    // return array_map(function($x) { return trim($x, "'"); }, explode(',', $sth->fetchColumn()));
    return array_map(fn($x) => trim($x, "'"), explode(',', $sth->fetchColumn()));
}
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;
}
// ========== MISC FUCTIONS ===================================================
function strip_mac($mac, $caps=true) {
    // strip mac address to 12 char string
    // strip chars we don't need
    $mac = preg_replace('/[^a-fA-F0-9]/', '', $mac);
    if ($caps) {
        $mac = strtoupper($mac);
    } else {
        $mac = strtolower($mac);
    }
    return $mac;
}
function write_mac($mac, $user_mac='xx:xx:xx:xx:xx:xx') {
    // rebuild mac address using user supplied format
    if (strlen($mac) != 12) {
        // if the MAC is empty, or for whatever reason incorrect, just return
        return $mac;
    }
    // check format of user mac: count upper or lower char
    $chars = count_chars($user_mac, 1);
    if (array_key_exists(88, $chars) and $chars[88] == 12) {
        $pattern = '/X/';
        $mac = strtoupper($mac);
    } elseif (array_key_exists(120, $chars) and $chars[120] == 12) {
        $pattern = '/x/';
        $mac = strtolower($mac);
    } else {
        // invalid format
        return $mac;
    }
    for($i=0; $i<12; $i++) {
        $user_mac = preg_replace($pattern, $mac[$i], $user_mac, 1);
    }
    return $user_mac;
}
function header_location($location) {
    // redirect page
    header('location:' . $location);
    exit;
}