summaryrefslogtreecommitdiff
path: root/lib/post.php
blob: 0aa41e26d64e031206840dc87ad82221c3aae4bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
function get_posts(): array
{
    $posts = [];
    $files = glob("{$_SERVER['DOCUMENT_ROOT']}/postsources/*.txt");

    foreach ($files as $file) {
        array_push($posts, [
            'name' => pathinfo($file, PATHINFO_FILENAME),
            'time' => filemtime($file),
            'contents' => file_get_contents($file)
        ]);
    }

    usort($posts, fn($a, $b) => $a['time'] == $b['time'] ? 0 : ($a['time'] > $b['time'] ? -1 : 1));

    return $posts;
}

function read_post(string $name)
{
    $name = basename($name);
    $path = "{$_SERVER['DOCUMENT_ROOT']}/postsources/$name.txt";
    if (!file_exists($path)) {
        return null;
    }

    return [
        'name' => $name,
        'time' => filemtime($path),
        'contents' => file_get_contents($path)
    ];
}