4in1_ws_web/handler/MainHandler.php
2024-02-02 01:47:16 +00:00

138 lines
3.8 KiB
PHP

<?php
class MainHandler extends request_handler {
function GET_index() {
set_title('$site_title');
render('main/index');
}
function GET_about() { redirect('/info/'); }
function GET_contacts() { redirect('/info/'); }
function GET_auto() {
list($name) = input('name');
if (is_admin()) {
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) {
global $config;
if (!is_admin() && !$page->visible && $page->get_id() != $config['index_page_id'])
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;
}
function GET_articles() {
$posts = posts::getList(0, 1000);
set_title('$articles');
render('main/articles', posts: $posts);
}
}