diff options
| author | ilotterytea <iltsu@alright.party> | 2025-12-12 10:34:56 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2025-12-12 10:34:56 +0500 |
| commit | 853d253a7dc0c7de6438310cd94a7ece2da9397c (patch) | |
| tree | c0035cabd2253146a313bb023d0d05aafc12536f /lib | |
| parent | 125a28557b42cfadc5a8c237a9b8d1ea0260a206 (diff) | |
feat: store posts in text format
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 |
