<?php
function create_session()
{
    $name = substr(preg_replace("/[^a-z\d]/i", "", __DIR__), -30);
    session_name($name);
    session_start();
}

function destroy_session()
{
    $_SESSION = array();
    if (ini_get("session.use_cookies")) {
        $params = session_get_cookie_params();
        setcookie(
            session_name(),
            '',
            time() - 42000,
            $params["path"],
            $params["domain"],
            $params["secure"],
            $params["httponly"]
        );
    }
    session_destroy();
}

function get_page($pageArr)
{
    $page = isset($_GET["page"]) ? $_GET["page"] : "me";
    if (array_key_exists($page, $pageArr)) {
        return array("id" => $page, "file" => "pages/$page.php", "title" => $pageArr[$page]);
    } else {
        return array("id" => "404", "file" => "pages/404.php", "title" => "404");
    }
}

function get_style($styleArr)
{
    if (isset($_POST["style"])) {
        $_SESSION["style"] = $_POST["style"];
    }
    if (isset($_GET["style"])) {
        $_SESSION["style"] = $_GET["style"];
    }
    $style = isset($_SESSION["style"]) ? $_SESSION["style"] : "default";

    if (array_key_exists($style, $styleArr)) {
        return array("id" => $style, "file" => $styleArr[$style]);
    } else {
        return array("id" => "default", "file" => $styleArr["default"]);
    }
}

function get_usagedata()
{
    return array(
        "files" => count(get_included_files()),
        "memory" => round(memory_get_peak_usage(true) / pow(10, 6), 1),
        "time" => round((microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']) * 1000, 3)
    );
}

function create_navbar($pageArr, $pageId)
{
    $navButtons = "";
    foreach ($pageArr as $id => $name) {
        $class = $pageId == $id ? "class=\"selected\"" : "";
        $navButtons .= "<a $class href=\"?page=$id\">$name</a>";
    }
    return $navButtons;
}

function create_stylechooser($styleArr, $styleId)
{
    $styleOpts = "";
    foreach (array_keys($styleArr) as $id) {
        $selected = $styleId == $id ? "selected" : "";
        $text = ucfirst($id);
        $styleOpts .= "<option value=\"$id\" $selected>$text</option>";
    }
    return $styleOpts;
}
