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

// sanitize input
function sanitize($input) {
    global $dblink;

    // trim whitespaces
    $input = @trim($input);

    // magic quotes enabled?
    if(get_magic_quotes_gpc()) {
        // strip slashes
        $input = stripslashes($input);
    }

    // convert to utf-8
    iconv("UTF-8", "UTF-8", $input);

    // convert special chars
    $input = htmlentities($input,ENT_QUOTES,'UTF-8');

    // and return
    return $input;
}

function print_tree_rec($tree, $level) {
    $output = '<ul class="treelvl' . $level. '">' . "\n";
    foreach ($tree as $node) {
        $output .= '<li><a href="' . $node['href'] . '">' . $node['value'] . '</a>';
        if ($node['info']) {
            $output .= ' - ' . $node['info'];
        }
        if ($node['children']) {
            $output .= "\n" . print_tree_rec($node['children'], $level+1);
        }
        $output .= "</li>\n";
    }
    $output .= "</ul>\n";
    return $output;
}

function print_tree($params, Smarty_Internal_Template $template) {
    if (empty($params['level'])) {
        $level = 0;
    } else {
        $level = $params['level'];
    }
    if (empty($params['tree'])) {
        return '';
    } else {
        return print_tree_rec($params['tree'], $level);
    }
}