ch1p_io_web/handler/MainHandler.php
2024-01-31 20:45:40 +03:00

147 lines
4.0 KiB
PHP

<?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;
}
}