english

blog

posts

new post
4 minutes / 645 words

some php to test...


<?php

// akelei 0.7 (prerelease)
// akelei.app
// © avi sato (avisato.com)
// free to use under the creative commons attribution-noncommercial-noderivatives license (cc-by-nc-nd)

include "phpHtml/phpHtml.php";

array_map(fn($p) => include $php . "functions" . $p . ".php", ["Language", "Helper", "Add", "Make", "Get", "Ask", "Do", "Template", "Json"]);
array_map(fn($p) => include $php . $p . ".php", ["adminFunctions", "Parsedown", "ParsedownExtra", "html2markdown"]);
function parseQuerystring()
{
    global $action, $parameters, $html, $querystring, $qs;
    $parameters = [];
    if (isset($_GET["parameters"])) {
        $querystring = rtrim($_GET["parameters"], "/");
        $qs = explode("/", $querystring);
        $qs[0] ? ($action = array_shift($qs)) : ($action = "home");
        if ($action == "api" && isset($qs[0])) {
            $qs = array_chunk($qs, 2);
            foreach ($qs as $p) {
                count($p) == 1 && ($p[1] = null);
                count($p) > 1 && ($parameters[$p[0]] = $p[1]);
            }
        }
    } else {
        $action = "home";
    }
    $html["action"] = $action;
}
function startSession()
{
    global $pagination, $output, $html, $errors;
    session_id() === "" && session_start();
    empty($_SESSION) && ($_SESSION["adminStatus"] = false);
    !isset($pagination) && ($pagination = []);
    !isset($output) && ($output = []);
    !isset($html) && ($html = []);
    !isset($html["content"]) && ($html["content"] = "");
    isset($_COOKIE["mode"]) && $html["mode"]=$_COOKIE["mode"];
}
function processOutput()
{
    global $output, $html;
    header("Expires: " . gmdate("D, d M Y H:i:s \G\M\T", time() + 60 * 60 * 24));
    if (count($output) > 0) {
        addOutput("html", $html["content"]);
        print_r(json_encode($output, JSON_UNESCAPED_UNICODE));
    } else {
        strlen($html["content"] < 1) && addError("nopage");
        print_r(str_replace("{{", "{{", parseTemplate("primary")));
    }
}
function checkLink()
{
    // check to see if there's a permalink code
    global $action, $querystring, $pageStructure, $postStructure;
    // is parameter the right length to be a permalink code?
    if ($action && strlen($action) == 24) {
        // is it a page?
        if (isset($pageStructure[$action])) {
            show("page", $action);
            return;
        }
        // is it a post?
        if (isset($postStructure[$action])) {
            show("post", $action);
            return;
        }
    }
    // check to see if there's a page link
    if ($i = isPage($querystring)) {
        show("page", $i);
        return;
    }
}
function isPage($link)
{
    global $pageStructure;
    $i = array_search($link, array_column($pageStructure, "fullLink"));
    return $i !== false && isset(array_keys($pageStructure)[$i]) ? array_keys($pageStructure)[$i] : false;
}
function checkPost()
{
    // check to see if there's a post link
    global $qs, $postStructure;
    isset($qs[0]) && ($i = array_search($qs[0], array_column($postStructure, "link"))) !== false && isset(array_keys($postStructure)[$i]) ? show("post", array_keys($postStructure)[$i]) : addError("nopost");
}
function checkTag()
{
    // check to see if there's a tag link
    global $qs, $tagStructure;
    isset($qs[0]) && isset($tagStructure[$qs[0]]) ? show("tag", $qs[0]) : addError("notag");
}
function buildPagination($source, $page = 1)
{
    global $pagination;
    $pagination["posts"] = count($source);
    $pagination["number"] = $page;
    $pagination["number"] < 1 && ($pagination["number"] = 1);
    $pagination["pages"] = ceil($pagination["posts"] / $pagination["size"]);
    $pagination["number"] > $pagination["pages"] && ($pagination["number"] = $pagination["pages"]);
    $pagination["previous"] = $pagination["number"] < 2 ? 1 : $pagination["number"] - 1;
    $pagination["next"] = $pagination["number"] >= $pagination["pages"] - 1 ? $pagination["pages"] : $pagination["number"] + 1;
    $pagination["start"] = ($pagination["number"] - 1) * $pagination["size"];
    return array_slice($source, $pagination["start"], $pagination["size"]);
}
function createDefaultUser()
{
    $userData = [];
    $userData["admin"]["name"] = "admin";
    $userData["admin"]["email"] = "admin@admin.org";
    $userData["admin"]["password"] = password_hash("admin", PASSWORD_DEFAULT);
    adminUserWrite($userData);
}
function parseMarkdown($md = "")
{
    $parsedown = new ParsedownExtra();
    $parsedown->setSafeMode(true);
    $h = $parsedown->text($md);
    $h = wrapHeadings($h);
    return $h;
}
function show($type, $f)
{
    global $pageStructure, $postStructure, $tagStructure;

    if ($type == "page" && ($p = $pageStructure[$f])) {
        isset($p["menu"]) && $p["menu"] && setItem("pageMenu", menuString($p["menu"]));
        addTitle($p["name"]);
        addItem($f, ["tags" => false, "date" => false]);
        addMapParents($f);
        addSelf($type, $f);
        addMapChildren($f);
        return;
    }
    if ($type == "post" && ($p = $postStructure[$f])) {
        addTitle($p["name"]);
        addItem($f, ["tagLinks" => true]);
        addHome();
        addBreadcrumb("parent", "posts", "blog");
        addSelf($type, $f);
        return;
    }
    if ($type == "tag" && ($p = $tagStructure[$f])) {
        addTitle($p["name"]);
        addH($p["name"]);
        doTag($f);
        return;
    }
}
function sortStructures()
{
    global $pageStructure, $postStructure, $tagStructure, $languageStructure;
    $pageStructure = sortByElement($pageStructure, "name", true);
    $postStructure = sortByElement($postStructure, "datetime", false);
    $tagStructure = sortByElement($tagStructure, "name", true);
    $languageStructure = sortByElement($languageStructure, "name", true);
}
function fixPost()
{
    if (!count($_POST)) {
        return;
    }
    $post = [];
    foreach ($_POST as $k => $v) {
        $k = str_replace("_", ".", $k);
        $post[$k] = $v;
    }
    $_POST = $post;
}

blog

powered by akelei. an avi sato project.