diff options
| -rw-r--r-- | .gitignore | 3 | ||||
| -rw-r--r-- | blog/index.php | 62 | ||||
| -rw-r--r-- | blog/post.php (renamed from status/post.php) | 38 | ||||
| -rw-r--r-- | index.php | 19 | ||||
| -rw-r--r-- | lib/post.php | 33 | ||||
| -rw-r--r-- | rss.php | 25 | ||||
| -rw-r--r-- | status/index.php | 52 |
7 files changed, 134 insertions, 98 deletions
@@ -1,2 +1,3 @@ *.db -*.ini
\ No newline at end of file +*.ini +/postsources/
\ No newline at end of file diff --git a/blog/index.php b/blog/index.php new file mode 100644 index 0000000..773cc6f --- /dev/null +++ b/blog/index.php @@ -0,0 +1,62 @@ +<?php +include_once $_SERVER['DOCUMENT_ROOT'] . '/lib/time.php'; +include_once $_SERVER['DOCUMENT_ROOT'] . '/lib/post.php'; + +$post_id = null; +$url = parse_url($_SERVER['REQUEST_URI']); +if (strlen($url['path']) > 1) { + $post_id = explode("/", substr($url['path'], 1), 2); + $post_id = $post_id[count($post_id) - 1]; +} + +if ($post_id) { + $post = read_post(urldecode($post_id)); + if (!$post) { + http_response_code(404); + exit("Post not found"); + } +} else { + $posts = get_posts(); +} +?> +<!DOCTYPE html> +<html> + +<head> + <title>blog - ilt.su</title> + <meta name="description" content="my blog."> + <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> + <link rel="stylesheet" href="/static/style.css"> + <link rel="shortcut icon" href="favicon.ico" type="image/x-icon"> + <meta name="robots" content="noindex, nofollow"> +</head> + +<body> + <main> + <p><a href="/">ilt.su</a> - <a href="/blog/">blog</a></p> + <?php if (isset($post)): ?> + <h1><?= $post['name'] ?? '<i>No title.</i>' ?></h1> + <div> + <?= $post['contents'] ?? '<i>No contents.</i>' ?> + </div> + <p><i>Posted <?= format_timestamp(time() - $post['time']) ?> ago</i></p> + <?php endif; ?> + + <?php if (isset($posts)): ?> + <h1>Blog</h1> + <ul> + <?php foreach ($posts as $s): ?> + <li><a href="/blog/<?= urlencode($s['name']) ?>"><?= $s['name'] ?></a> + <i><?= format_timestamp(time() - $s['time']) ?> + ago</i> + </li> + <?php endforeach; ?> + <?php if (empty($posts)): ?> + <i>No posts yet.</i> + <?php endif; ?> + </ul> + <?php endif; ?> + </main> +</body> + +</html>
\ No newline at end of file diff --git a/status/post.php b/blog/post.php index 064d86f..90dc9ee 100644 --- a/status/post.php +++ b/blog/post.php @@ -21,37 +21,31 @@ function str_safe(string $s, int|null $max_length, bool $remove_new_lines = true return $output; } -$db = new PDO("sqlite:{$_SERVER['DOCUMENT_ROOT']}/database.db"); - if ($_SERVER['REQUEST_METHOD'] == 'POST') { - $title = str_safe($_POST['title'], 500, true) ?: null; - $contents = str_safe($_POST['contents'], null, false) ?: null; + $title = basename(str_safe($_POST['title'], 500, true) ?: null); + $contents = $_POST['contents'] ?: null; + $path = "{$_SERVER['DOCUMENT_ROOT']}/postsources"; - $db->prepare('INSERT INTO statuses(title, contents) VALUES (?, ?)') - ->execute([$title, $contents]); + if (!is_dir($path) && !mkdir($path, 0777, true)) { + http_response_code(500); + exit("Failed to create a directory for blog posts!"); + } - $id = $db->lastInsertId(); + if (!file_put_contents("$path/$title.txt", $contents)) { + http_response_code(500); + exit("Failed to save the blog post!"); + } - header("Location: /status/?id=$id"); + header("Location: /blog/" . urlencode($title)); exit; } - -if (isset($_GET['id']) && !empty(trim($_GET['id']))) { - $stmt = $db->prepare('SELECT * FROM statuses WHERE id = ?'); - $stmt->execute([$_GET['id']]); - - $status = $stmt->fetch(PDO::FETCH_ASSOC) ?: null; -} else { - $stmt = $db->query('SELECT id, title, posted_at FROM statuses ORDER BY posted_at DESC'); - $statuses = $stmt->fetchAll(PDO::FETCH_ASSOC); -} ?> <!DOCTYPE html> <html> <head> - <title>new status - ilt.su</title> - <meta name="description" content="my statuses."> + <title>new post - ilt.su</title> + <meta name="description" content="my blog."> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <link rel="stylesheet" href="/static/style.css"> <link rel="shortcut icon" href="favicon.ico" type="image/x-icon"> @@ -60,9 +54,9 @@ if (isset($_GET['id']) && !empty(trim($_GET['id']))) { <body> <main> - <p><a href="/">ilt.su</a> - <a href="/status/">statuses</a></p> + <p><a href="/">ilt.su</a> - <a href="/blog/">blog</a></p> <h1>post a new status</h1> - <form action="/status/post.php" method="post"> + <form action="/blog/post.php" method="post"> <table> <tr> <th>title:</th> @@ -1,14 +1,17 @@ <?php include_once $_SERVER['DOCUMENT_ROOT'] . '/lib/time.php'; +include_once $_SERVER['DOCUMENT_ROOT'] . '/lib/post.php'; if (file_exists("{$_SERVER['DOCUMENT_ROOT']}/projects.json") && $contents = file_get_contents("{$_SERVER['DOCUMENT_ROOT']}/projects.json")) { $projects = json_decode($contents, true); } if (file_exists("{$_SERVER['DOCUMENT_ROOT']}/links.json") && $contents = file_get_contents("{$_SERVER['DOCUMENT_ROOT']}/links.json")) { $links = json_decode($contents, true); } -$db = new PDO("sqlite:{$_SERVER['DOCUMENT_ROOT']}/database.db"); -$stmt = $db->query("SELECT id, title, posted_at FROM statuses ORDER BY posted_at DESC LIMIT 1"); -$last_status = $stmt->fetch(PDO::FETCH_ASSOC) ?: null; + +$posts = get_posts(); +if (!empty($posts)) { + $last_post = $posts[0]; +} ?> <!DOCTYPE html> <html> @@ -58,12 +61,12 @@ $last_status = $stmt->fetch(PDO::FETCH_ASSOC) ?: null; <?php endforeach; ?> </section> <?php endif; ?> - <?php if (isset($last_status)): ?> + <?php if (isset($last_post)): ?> <div class="status"> - <p>Last status posted <?= format_timestamp(time() - strtotime($last_status['posted_at'])) ?> ago: <a - href="/status/?id=<?= $last_status['id'] ?>"><?= $last_status['title'] ?? 'No title.' ?></a></p> - <p style="font-size:10px;"><a href="/status/">[more...]</a> <a href="/rss.php"><img - src="/static/img/rss.png" alt="[rss]"></a></p> + <p>Last post was <?= format_timestamp(time() - $last_post['time']) ?> ago: <a + href="/blog/<?= urlencode($last_post['name']) ?>"><?= $last_post['name'] ?? 'No title.' ?></a></p> + <p style="font-size:10px;"><a href="/blog/">[more...]</a> <a href="/rss.xml"><img src="/static/img/rss.png" + alt="[rss]"></a></p> </div> <?php endif; ?> <?php if (isset($links)): ?> 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 @@ -1,8 +1,8 @@ <?php include_once $_SERVER['DOCUMENT_ROOT'] . '/lib/time.php'; -$db = new PDO("sqlite:{$_SERVER['DOCUMENT_ROOT']}/database.db"); -$stmt = $db->query("SELECT * FROM statuses ORDER BY posted_at DESC"); -$statuses = $stmt->fetchAll(PDO::FETCH_ASSOC); +include_once $_SERVER['DOCUMENT_ROOT'] . '/lib/post.php'; + +$posts = get_posts(); header('Content-Type: application/xml'); @@ -15,35 +15,30 @@ echo "<link>https://ilt.su</link>"; echo "<description>ilotterytea's racist schizo thoughts. read and learn.</description>"; echo "<language>en-us</language>"; - -if (!empty($statuses)) { - $date = new DateTime($statuses[0]['posted_at']); +if (!empty($posts)) { echo "<lastBuildDate>"; - echo $date->format('D, d M Y H:i:s O'); + echo date("D, d M Y H:i:s O", $posts[0]['time']); echo "</lastBuildDate>"; } -foreach ($statuses as $s) { - $date = new DateTime($s['posted_at']); - +foreach ($posts as $p) { echo "<item>"; echo "<title>"; - echo $s['title'] ?: '-NO TITLE-'; + echo $p['name'] ?: '-NO TITLE-'; echo "</title>"; echo '<guid isPermaLink="true">'; - echo "https://ilt.su/status/?id=" . $s['id']; + echo "https://ilt.su/blog/" . urlencode($p['name']); echo "</guid>"; echo "<pubDate>"; - echo $date->format('D, d M Y H:i:s O'); + echo date('D, d M Y H:i:s O', $p['time']); echo "</pubDate>"; echo "<content:encoded><![CDATA["; - echo $s['contents'] ?: '-NO CONTENT-'; + echo $p['contents'] ?: '-NO CONTENT-'; echo "]]></content:encoded>"; echo "</item>"; } echo "</channel></rss>"; -?>
\ No newline at end of file diff --git a/status/index.php b/status/index.php deleted file mode 100644 index 13d5405..0000000 --- a/status/index.php +++ /dev/null @@ -1,52 +0,0 @@ -<?php -include_once $_SERVER['DOCUMENT_ROOT'] . '/lib/time.php'; -$db = new PDO("sqlite:{$_SERVER['DOCUMENT_ROOT']}/database.db"); - -if (isset($_GET['id']) && !empty(trim($_GET['id']))) { - $stmt = $db->prepare('SELECT * FROM statuses WHERE id = ?'); - $stmt->execute([$_GET['id']]); - - $status = $stmt->fetch(PDO::FETCH_ASSOC) ?: null; -} else { - $stmt = $db->query('SELECT id, title, posted_at FROM statuses ORDER BY posted_at DESC'); - $statuses = $stmt->fetchAll(PDO::FETCH_ASSOC); -} -?> -<!DOCTYPE html> -<html> - -<head> - <title>statuses - ilt.su</title> - <meta name="description" content="my statuses."> - <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> - <link rel="stylesheet" href="/static/style.css"> - <link rel="shortcut icon" href="favicon.ico" type="image/x-icon"> - <meta name="robots" content="noindex, nofollow"> -</head> - -<body> - <main> - <p><a href="/">ilt.su</a> - <a href="/status/">statuses</a></p> - <?php if (isset($status)): ?> - <h1><?= $status['title'] ?? '<i>No title.</i>' ?></h1> - <div> - <?= $status['contents'] ?? '<i>No contents.</i>' ?> - </div> - <p><i>Posted <?= format_timestamp(time() - strtotime($status['posted_at'])) ?> ago</i></p> - <?php endif; ?> - - <?php if (isset($statuses)): ?> - <h1>Statuses</h1> - <ul> - <?php foreach ($statuses as $s): ?> - <li><a href="/status/?id=<?= $s['id'] ?>"><?= $s['title'] ?></a> - <i><?= format_timestamp(time() - strtotime($s['posted_at'])) ?> - ago</i> - </li> - <?php endforeach; ?> - </ul> - <?php endif; ?> - </main> -</body> - -</html>
\ No newline at end of file |
