4in1_ws_web/handler/MainHandler.php
2024-04-14 01:16:29 +00:00

171 lines
4.9 KiB
PHP

<?php
class MainHandler extends request_handler {
function GET_index() {
global $config;
add_meta([
'og:type' => 'website',
'$url' => 'https://'.$config['domain'].'/',
'$title' => lang('meta_index_title'),
'$description' => lang('meta_index_description'),
'$image' => 'https://'.$config['domain'].'/img/4in1-preview.jpg'
]);
set_title('$site_title');
set_skin_opts(['is_index' => true]);
render('main/index');
}
function GET_about() { redirect('/info/'); }
function GET_contacts() { redirect('/info/'); }
function GET_page() {
global $config;
list($name) = input('name');
$page = pages::getByName($name);
if (!$page) {
if (is_admin()) {
set_title($name);
render('admin/pageNew',
short_name: $name);
}
not_found();
}
if (!is_admin() && !$page->visible)
not_found();
if ($page->shortName == 'info')
set_skin_opts(['head_section' => 'about']);
if ($page) {
add_meta([
'$url' => 'https://'.$config['domain'].$page->getUrl(),
'$title' => $page->title,
]);
}
set_title($page ? $page->title : '???');
render('main/page',
unsafe_html: $page->getHtml(is_retina(), getUserTheme()),
page_url: $page->getUrl(),
short_name: $page->shortName);
not_found();
}
function GET_post() {
global $config;
list($name, $input_lang) = input('name, lang');
$lang = null;
try {
if ($input_lang)
$lang = PostLanguage::from($input_lang);
} catch (ValueError $e) {
not_found($e->getMessage());
}
if (!$lang)
$lang = PostLanguage::getDefault();
$post = posts::getByName($name);
if (!$post || (!$post->visible && !is_admin()))
not_found();
if ($lang == PostLanguage::getDefault() && $input_lang == $lang->value)
redirect($post->getUrl());
if (!$post->hasLang($lang))
not_found('no text for language '.$lang->name);
if (!$post->visible && !is_admin())
not_found();
$pt = $post->getText($lang);
$other_langs = [];
foreach (PostLanguage::cases() as $pl) {
if ($pl == $lang)
continue;
if ($post->hasLang($pl))
$other_langs[] = $pl->value;
}
add_meta([
'$title' => $pt->title,
'$url' => $config['domain'].$post->getUrl(),
'$description' => $pt->getDescriptionPreview(155)
]);
if (($img = $pt->getFirstImage()) !== null)
add_meta(['$image' => $img->getDirectUrl()]);
set_skin_opts(['articles_lang' => $lang->value]);
set_title($pt->title);
if ($pt->hasTableOfContents())
set_skin_opts(['wide' => true]);
render('main/post',
title: $pt->title,
id: $post->id,
source_url: $post->sourceUrl,
unsafe_html: $pt->getHtml(is_retina(), getUserTheme()),
unsafe_toc_html: $pt->getTableOfContentsHtml(),
date: $post->getFullDate(),
visible: $post->visible,
url: $post->getUrl(),
lang: $lang->value,
other_langs: $other_langs);
}
function GET_rss() {
global $config;
$lang = PostLanguage::getDefault();
$items = array_map(function(Post $post) use ($lang) {
$pt = $post->getText($lang);
return [
'title' => $pt->title,
'link' => $post->getUrl(),
'pub_date' => date(DATE_RSS, $post->getTimestamp()),
'description' => $pt->getDescriptionPreview(500)
];
}, posts::getList(0, 20, filter_by_lang: $lang));
$ctx = 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() {
list($lang) = input('lang');
if ($lang) {
$lang = PostLanguage::tryFrom($lang);
if (!$lang || $lang == PostLanguage::getDefault())
redirect('/articles/');
} else {
$lang = PostLanguage::getDefault();
}
$posts = posts::getList(
include_hidden: is_admin(),
filter_by_lang: $lang);
set_title('$articles');
set_skin_opts(['head_section' => 'articles']);
render('main/articles',
posts: $posts,
selected_lang: $lang);
}
}