summaryrefslogtreecommitdiff
path: root/handler/MainHandler.php
diff options
context:
space:
mode:
authorEvgeny Zinoviev <me@ch1p.io>2024-01-31 06:11:00 +0300
committerEvgeny Zinoviev <me@ch1p.io>2024-01-31 20:45:40 +0300
commitc0dc531ebefd8912819f3b6c8bda1fed3c7e750c (patch)
tree2c75aa9df182260aef09faf4befd81a4c2b9c5e2 /handler/MainHandler.php
parent48d688cdf7f9eae1bf11b8a6f0e5b98687c604cb (diff)
make it simple, but not simpler
Diffstat (limited to 'handler/MainHandler.php')
-rw-r--r--handler/MainHandler.php147
1 files changed, 147 insertions, 0 deletions
diff --git a/handler/MainHandler.php b/handler/MainHandler.php
new file mode 100644
index 0000000..889cb48
--- /dev/null
+++ b/handler/MainHandler.php
@@ -0,0 +1,147 @@
+<?php
+
+require_once 'lib/posts.php';
+require_once 'lib/themes.php';
+
+class MainHandler extends request_handler {
+
+ function GET_index() {
+ $posts = posts::getList(include_hidden: is_admin());
+ $tags = tags::getAll(include_hidden: is_admin());
+
+ set_title("ch1p's Blog");
+ set_skin_opts(['dynlogo_enabled' => false]);
+ render('main/index',
+ posts: $posts,
+ tags: $tags);
+ }
+
+ function GET_projects() {
+ redirect('/projects/');
+ }
+
+ function GET_contacts() {
+ global $config;
+ set_title(lang('contacts'));
+ render('main/contacts',
+ email: $config['admin_email']);
+ }
+
+ function GET_auto() {
+ list($name) = input('name');
+ if ($name == 'coreboot-mba51-flashing')
+ redirect('/coreboot-mba52-flashing/');
+
+ if (is_numeric($name)) {
+ $post = posts::get((int)$name);
+ } else {
+ $post = posts::getByName($name);
+ }
+ if ($post)
+ return $this->renderPost($post);
+
+ $tag = tags::get($name);
+ if ($tag)
+ return $this->renderTag($tag);
+
+ $page = pages::getByName($name);
+ if ($page)
+ return $this->renderPage($page);
+
+ if (is_admin()) {
+ set_title($name);
+ render('admin/pageNew',
+ short_name: $name);
+ }
+
+ not_found();
+ }
+
+ protected function renderPost(Post $post) {
+ global $config;
+
+ if (!$post->visible && !is_admin())
+ not_found();
+
+ $tags = $post->getTags();
+
+ add_meta(
+ ['property' => 'og:title', 'content' => $post->title],
+ ['property' => 'og:url', 'content' => $config['domain'].$post->getUrl()]
+ );
+ if (($img = $post->getFirstImage()) !== null)
+ add_meta(['property' => 'og:image', 'content' => $img->getDirectUrl()]);
+
+ add_meta([
+ 'name' => 'description',
+ 'property' => 'og:description',
+ 'content' => $post->getDescriptionPreview(155)
+ ]);
+
+ set_title($post->title);
+
+ if ($post->toc)
+ set_skin_opts(['wide' => true]);
+
+ render('main/post',
+ title: $post->title,
+ id: $post->id,
+ unsafe_html: $post->getHtml(is_retina(), getUserTheme()),
+ unsafe_toc_html: $post->getToc(),
+ date: $post->getFullDate(),
+ tags: $tags,
+ visible: $post->visible,
+ url: $post->getUrl(),
+ email: $config['admin_email'],
+ urlencoded_reply_subject: 'Re: '.$post->title);
+ }
+
+ protected function renderTag(Tag $tag) {
+ $tag = tags::get($tag);
+ if (!is_admin() && !$tag->visiblePostsCount)
+ not_found();
+
+ $count = posts::getCountByTagId($tag->id, is_admin());
+ $posts = $count ? posts::getPostsByTagId($tag->id, is_admin()) : [];
+
+ set_title('#'.$tag->tag);
+ render('main/tag',
+ count: $count,
+ posts: $posts,
+ tag: $tag->tag);
+ }
+
+ protected function renderPage(Page $page) {
+ if (!is_admin() && !$page->visible)
+ not_found();
+
+ set_title($page ? $page->title : '???');
+ render('main/page',
+ unsafe_html: $page->getHtml(is_retina(), getUserTheme()),
+ page_url: $page->getUrl(),
+ short_name: $page->shortName);
+ }
+
+ function GET_rss() {
+ global $config;
+
+ $items = array_map(fn(Post $post) => [
+ 'title' => $post->title,
+ 'link' => $post->getUrl(),
+ 'pub_date' => date(DATE_RSS, $post->ts),
+ 'description' => $post->getDescriptionPreview(500),
+ ], posts::getList(0, 20));
+
+ $ctx = new SkinContext('\\skin\\rss');
+ $body = $ctx->atom(
+ title: lang('site_title'),
+ link: 'https://'.$config['domain'],
+ rss_link: 'https://'.$config['domain'].'/feed.rss',
+ items: $items);
+
+ header('Content-Type: application/rss+xml; charset=utf-8');
+ echo $body;
+ exit;
+ }
+
+} \ No newline at end of file