diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/post.php | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/lib/post.php b/lib/post.php new file mode 100644 index 0000000..0aa41e2 --- /dev/null +++ b/lib/post.php @@ -0,0 +1,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) + ]; +}
\ No newline at end of file |
