4in1_ws_web/handlers/MainHandler.php
2025-04-15 01:54:31 +03:00

197 lines
6.4 KiB
PHP

<?php
class MainHandler extends request_handler {
public function GET_index() {
global $config;
$posts_lang = PostLanguage::English;
$posts = posts::getList(0, 3,
include_hidden: isAdmin(),
filter_by_lang: $posts_lang);
$this->skin->addMeta([
'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'
]);
$this->skin->setTitle('$site_title');
$this->skin->set([
'posts' => $posts,
'posts_lang' => $posts_lang,
'versions' => $config['book_versions']
]);
$this->skin->setRenderOptions(['is_index' => true]);
$this->skin->renderPage('index.twig');
}
public function GET_about() { self::redirect('/info/'); }
public function GET_contacts() { self::redirect('/info/'); }
public function GET_page() {
global $config;
list($name) = $this->input('name');
$page = pages::getByName($name);
if (!$page) {
if (isAdmin()) {
$this->skin->setTitle($name);
$this->skin->renderPage('admin_page_new.twig', [
'short_name' => $name
]);
}
self::notFound();
}
if (!isAdmin() && !$page->visible)
self::notFound();
$bc = null;
$render_opts = [];
if ($page) {
$this->skin->addMeta([
'@url' => 'https://'.$config['domain'].$page->getUrl(),
'@title' => $page->title,
]);
if ($page->parentId) {
$bc = [];
$parent = $page;
while ($parent?->parentId) {
$parent = pages::getById($parent->parentId);
if ($parent)
$bc[] = ['url' => $parent->getUrl(), 'text' => $parent->title];
}
if (empty($bc))
$bc = null;
}
if ($page->shortName == 'info')
$render_opts = ['head_section' => 'about'];
else if ($page->shortName == $config['wiki_root'])
$render_opts = ['head_section' => $page->shortName];
}
$this->skin->setRenderOptions($render_opts);
$this->skin->setTitle($page ? $page->title : '???');
$this->skin->renderPage('page.twig', [
'page' => $page,
'html' => $page->getHtml(isRetina(), themes::getUserTheme()),
'bc' => $bc,
'delete_token' => self::getCSRF('delpage'.$page->shortName)
]);
}
public function GET_post() {
global $config;
list($name, $input_lang) = $this->input('name, lang');
$lang = null;
try {
if ($input_lang)
$lang = PostLanguage::from($input_lang);
} catch (ValueError $e) {
self::notFound($e->getMessage());
}
if (!$lang)
$lang = PostLanguage::getDefault();
$post = posts::getByName($name);
if (!$post || (!$post->visible && !isAdmin()))
self::notFound();
if ($lang == PostLanguage::getDefault() && $input_lang == $lang->value)
self::redirect($post->getUrl());
if (!$post->hasLang($lang))
self::notFound('no text for language '.$lang->name);
if (!$post->visible && !isAdmin())
self::notFound();
$pt = $post->getText($lang);
$other_langs = [];
foreach (PostLanguage::cases() as $pl) {
if ($pl == $lang)
continue;
if ($post->hasLang($pl))
$other_langs[] = $pl->value;
}
$meta = [
'@title' => $pt->title,
'@url' => $config['domain'].$post->getUrl(),
'@description' => $pt->getDescriptionPreview(155)
];
if ($pt->keywords)
$meta['@keywords'] = $pt->keywords;
$this->skin->addMeta($meta);
if (($img = $pt->getFirstImage()) !== null)
$this->skin->addMeta(['@image' => $img->getDirectUrl()]);
$this->skin->setTitle($pt->title);
$this->skin->setRenderOptions(['articles_lang' => $lang->value, 'wide' => $pt->hasTableOfContents()]);
$this->skin->renderPage('post.twig', [
'post' => $post,
'pt' => $pt,
'html' => $pt->getHtml(isRetina(), themes::getUserTheme()),
'selected_lang' => $lang->value,
'other_langs' => $other_langs,
'delete_token' => self::getCSRF('delpost'.$post->id)
]);
}
public 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));
$body = $this->skin->render('rss.twig', [
'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;
}
public function GET_articles() {
list($lang) = $this->input('lang');
if ($lang) {
$lang = PostLanguage::tryFrom($lang);
if (!$lang || $lang == PostLanguage::getDefault())
self::redirect('/articles/');
} else {
$lang = PostLanguage::getDefault();
}
$posts = posts::getList(
include_hidden: isAdmin(),
filter_by_lang: $lang);
$this->skin->setTitle('$articles');
$this->skin->addMeta([
'@description' => lang('blog_expl_'.$lang->value)
]);
$this->skin->setRenderOptions(['head_section' => 'articles']);
$this->skin->renderPage('articles.twig', [
'posts' => $posts,
'selected_lang' => $lang->value
]);
}
}