template_dir = 'tpl';
$smarty->compile_dir = 'tpl_c';
$smarty->registerPlugin('function', 'treelist', 'print_tree');
$smarty->registerPlugin('function', 'msgout', 'msgout');
$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;
// ========== 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";
        }
    }
}
class MessageWarning extends Message {
    function MessageWarning() {
        $this->caption = 'Warning';
    }
    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";
        }
    }
}
class MessageError extends Message {
    function MessageError() {
        $this->caption = 'Error';
    }
    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";
        }
    }
}
// ========== FORM FUNCTIONS ==================================================
function form_get_action() {
    if (!isset($_POST['submit'])) {
        if (isset($_GET['f'])) {
            $submit = $_GET['f'];
        } else {
            $submit = NULL;
        }
    } else {
        $submit = $_POST['submit'];
    }
    if (is_array($submit)) {
        $submit = key($submit);
    }
    return strtolower($submit);
}
function submit_error($action) {
    /* Submit buttons that return an unknown value end up in this
       function by default. An exit() is conscious here *not* installed,
       since it could be that despite such an error the program
       execution should be continued. */
    return sprintf('The action "%s" is unknown. It is probably a program error.
 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;
}
?>