forked from laysense/laysense.cn
288 lines
12 KiB
PHP
288 lines
12 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Controller;
|
|
|
|
use App\Content\Html;
|
|
use App\Content\Repository;
|
|
use App\Content\View;
|
|
use support\Request;
|
|
use support\Response;
|
|
|
|
final class SiteController
|
|
{
|
|
public function home(Request $request): Response
|
|
{
|
|
return $this->page($request, 'home');
|
|
}
|
|
|
|
public function page(Request $request, string $slug): Response
|
|
{
|
|
$repo = new Repository();
|
|
$page = $repo->page($slug);
|
|
if (!$page || $page['status'] !== 'published' || $slug === 'home' && $request->path() !== '/') {
|
|
return $this->missing($request);
|
|
}
|
|
return $this->renderPage($repo, $page);
|
|
}
|
|
|
|
public function renderPage(Repository $repo, array $page, bool $preview = false, bool $canvas = false): Response
|
|
{
|
|
$slug = $page['slug'];
|
|
$isHome = $slug === 'home';
|
|
$categories = $repo->categories();
|
|
foreach ($page['sections'] as &$section) {
|
|
if (!$section['feed']) {
|
|
continue;
|
|
}
|
|
$feed = json_decode($section['feed'], true, flags: JSON_THROW_ON_ERROR);
|
|
$tabs = $feed['mode'] === 'tabs' && count($feed['categories']) > 1;
|
|
$section['articleFeed'] = [];
|
|
$section['archivePath'] = $page['status'] === 'published' ? '/articles/feed/' . $slug . '/' . $section['key'] : null;
|
|
foreach ($tabs ? $feed['categories'] : [''] as $category) {
|
|
$selected = $category === '' ? $feed['categories'] : [$category];
|
|
$section['articleFeed'][] = [
|
|
'categories' => $selected, 'label' => $categories[$category] ?? '',
|
|
'articles' => $repo->articles($selected, limit: $feed['limit']),
|
|
];
|
|
}
|
|
}
|
|
unset($section);
|
|
|
|
return View::render('page', [
|
|
'site' => $repo->site(),
|
|
'page' => $page,
|
|
'path' => $isHome ? '/' : '/' . $slug,
|
|
'title' => $page['title'],
|
|
'description' => $page['description'],
|
|
'socialImage' => $page['image'],
|
|
'socialAlt' => $page['alt'],
|
|
'network' => $isHome ? $this->networkGroups($repo) : [],
|
|
'previewMode' => $preview, 'canvas' => $preview && $canvas, 'noindex' => $preview,
|
|
]);
|
|
}
|
|
|
|
public function services(Request $request): Response
|
|
{
|
|
$repo = new Repository();
|
|
|
|
return View::render('services', [
|
|
'site' => $repo->site(), 'path' => '/services',
|
|
'title' => '产品与服务 · 来笙 Laysense',
|
|
'description' => '浏览来笙的建站工具、智能 DNS 和软件著作权服务。',
|
|
'products' => $repo->articles('services', limit: -1, directoryOrder: true),
|
|
]);
|
|
}
|
|
|
|
public function network(Request $request): Response
|
|
{
|
|
$repo = new Repository();
|
|
|
|
return View::render('network', [
|
|
'site' => $repo->site(), 'path' => '/network', 'title' => '支持项目与合作伙伴 · 来笙',
|
|
'description' => '浏览来笙支持的开源项目和合作伙伴。', 'network' => $this->networkGroups($repo),
|
|
]);
|
|
}
|
|
|
|
private function networkGroups(Repository $repo): array
|
|
{
|
|
$groups = [];
|
|
foreach (array_diff_key(Repository::DIRECTORIES, ['services' => true]) as $slug => $name) {
|
|
$groups[$slug] = ['name' => $name, 'articles' => $repo->articles($slug, limit: -1, directoryOrder: true)];
|
|
}
|
|
|
|
return $groups;
|
|
}
|
|
|
|
public function archive(Request $request, ?string $category = null, ?string $tag = null): Response
|
|
{
|
|
$repo = new Repository();
|
|
$categories = $repo->categories();
|
|
if ($category !== null && !isset($categories[$category])) {
|
|
return $this->missing($request);
|
|
}
|
|
$title = $category ? $categories[$category] : ($tag ?: '来笙动态');
|
|
$path = '/articles' . ($category ? '/category/' . $category : ($tag ? '/tag/' . rawurlencode($tag) : ''));
|
|
|
|
$categoryLinks = [];
|
|
foreach (array_keys($categories) as $key) {
|
|
$categoryLinks[$key] = '/articles/category/' . $key;
|
|
}
|
|
|
|
return $this->renderArchive($request, $repo, $category, $tag, [
|
|
'headline' => $title, 'description' => '查看来笙的公告、新闻和产品动态。',
|
|
'archivePath' => $path, 'allPath' => '/articles',
|
|
'categoryLinks' => $categoryLinks,
|
|
'categories' => $categories, 'category' => $category, 'tag' => $tag,
|
|
'backPath' => $category || $tag ? '/articles' : null, 'backLabel' => '全部动态',
|
|
]);
|
|
}
|
|
|
|
public function feedArchive(Request $request, string $slug, string $key): Response
|
|
{
|
|
$repo = new Repository();
|
|
$section = $repo->publishedFeed($slug, $key);
|
|
if (!$section) {
|
|
return $this->missing($request);
|
|
}
|
|
$feed = json_decode($section['feed'], true, flags: JSON_THROW_ON_ERROR);
|
|
$labels = $repo->categories();
|
|
$categories = [];
|
|
foreach ($feed['categories'] as $value) {
|
|
if (isset($labels[$value])) {
|
|
$categories[$value] = $labels[$value];
|
|
}
|
|
}
|
|
$category = $request->get('category');
|
|
if (!$categories || ($category !== null && (!is_string($category) || !isset($categories[$category])))) {
|
|
return $this->missing($request);
|
|
}
|
|
|
|
$allPath = '/articles/feed/' . $slug . '/' . $key;
|
|
$returnPath = ($slug === 'home' ? '/' : '/' . $slug) . '#' . $key;
|
|
$categoryLinks = [];
|
|
foreach (array_keys($categories) as $value) {
|
|
$categoryLinks[$value] = $allPath . '?category=' . rawurlencode((string) $value);
|
|
}
|
|
|
|
return $this->renderArchive($request, $repo, $category ?? array_keys($categories), null, [
|
|
'headline' => $section['title'] . ($category !== null ? ' · ' . $categories[$category] : ''),
|
|
'description' => '查看' . implode('、', $categories) . '分类下的文章。',
|
|
'archivePath' => $category === null ? $allPath : $categoryLinks[$category], 'allPath' => $allPath,
|
|
'categoryLinks' => $categoryLinks, 'categories' => $categories, 'category' => $category, 'tag' => null,
|
|
'categoryScope' => $category === null ? array_keys($categories) : [$category],
|
|
'backPath' => $returnPath, 'backLabel' => '返回原页面', 'noindex' => true,
|
|
]);
|
|
}
|
|
|
|
private function renderArchive(Request $request, Repository $repo, string|array|null $category, ?string $tag, array $view): Response
|
|
{
|
|
$number = $request->get('page', '1');
|
|
if (!is_scalar($number) || !preg_match('/^[1-9][0-9]{0,4}$/D', (string) $number)) {
|
|
return $this->missing($request);
|
|
}
|
|
$page = (int) $number;
|
|
$articles = $repo->articles($category, $tag, limit: 21, offset: ($page - 1) * 20);
|
|
if ($page > 1 && $articles === []) {
|
|
return $this->missing($request);
|
|
}
|
|
$path = $view['archivePath'];
|
|
|
|
return View::render('archive', $view + [
|
|
'site' => $repo->site(), 'title' => $view['headline'] . ' · 来笙',
|
|
'path' => $page > 1 ? View::archivePage($path, $page) : $path,
|
|
'pageNumber' => $page, 'hasNext' => count($articles) > 20, 'articles' => array_slice($articles, 0, 20),
|
|
'noindex' => $articles === [],
|
|
]);
|
|
}
|
|
|
|
public function tagged(Request $request, string $tag): Response
|
|
{
|
|
$tag = rawurldecode($tag);
|
|
if (mb_strlen($tag) > 80 || $tag === '') {
|
|
return $this->missing($request);
|
|
}
|
|
|
|
return $this->archive($request, tag: $tag);
|
|
}
|
|
|
|
public function article(Request $request, string $slug): Response
|
|
{
|
|
$repo = new Repository();
|
|
$article = $repo->published($slug);
|
|
if (!$article) {
|
|
$redirect = $repo->articleRedirect($slug);
|
|
|
|
return $redirect ? View::redirect('/articles/' . $redirect['slug'], 301) : $this->missing($request);
|
|
}
|
|
$articleHtml = Html::articleBody($article);
|
|
|
|
return View::render('article', [
|
|
'site' => $repo->site(), 'article' => $article, 'articleHtml' => $articleHtml,
|
|
'title' => $article['title'] . ' · 来笙',
|
|
'description' => $article['summary'] ?: mb_substr(trim(strip_tags($articleHtml)), 0, 150) ?: $article['title'],
|
|
'path' => '/articles/' . $article['slug'],
|
|
'socialImage' => $article['card']['image'] ?? '', 'socialAlt' => $article['card']['alt'] ?? '',
|
|
'noindex' => trim($articleHtml) === '' && !$article['summary'] && empty($article['card']['url']) && empty($article['card']['image']),
|
|
]);
|
|
}
|
|
|
|
public function legacyArticle(Request $request, string $id): Response
|
|
{
|
|
$article = (new Repository())->legacyArticle('/news/id/' . $id . '.html');
|
|
|
|
return $article ? View::redirect('/articles/' . $article['slug'], 301) : $this->missing($request);
|
|
}
|
|
|
|
public function legacyCategory(Request $request, string $id): Response
|
|
{
|
|
$target = [
|
|
'1' => '/articles/category/product', '2' => '/articles/category/news', '3' => '/articles/category/announcement',
|
|
'5' => '/#supported-projects', '6' => '/#partners',
|
|
][$id] ?? null;
|
|
|
|
return $target ? View::redirect($target, 301) : $this->missing($request);
|
|
}
|
|
|
|
public function legacyTag(Request $request, string $tag): Response
|
|
{
|
|
return View::redirect('/articles/tag/' . rawurlencode(rawurldecode($tag)), 301);
|
|
}
|
|
|
|
public function sitemap(Request $request): Response
|
|
{
|
|
$repo = new Repository();
|
|
$xml = ['<?xml version="1.0" encoding="UTF-8"?>', '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'];
|
|
$pages = $repo->pages(publishedOnly: true);
|
|
$pages[] = ['slug' => 'services', 'updated_at' => null];
|
|
$pages[] = ['slug' => 'network', 'updated_at' => null];
|
|
if ($repo->articles(limit: 1)) {
|
|
$pages[] = ['slug' => 'articles', 'updated_at' => null];
|
|
}
|
|
foreach ($pages as $page) {
|
|
$xml[] = $this->sitemapUrl($page['slug'] === 'home' ? '/' : '/' . $page['slug'], $page['updated_at']);
|
|
}
|
|
foreach ($repo->sitemapArticles() as $article) {
|
|
$xml[] = $this->sitemapUrl('/articles/' . $article['slug'], $article['updated_at']);
|
|
}
|
|
$xml[] = '</urlset>';
|
|
|
|
return new Response(200, ['Content-Type' => 'application/xml; charset=utf-8'], implode("\n", $xml) . "\n");
|
|
}
|
|
|
|
public function robots(Request $request): Response
|
|
{
|
|
return new Response(200, ['Content-Type' => 'text/plain; charset=utf-8'],
|
|
"User-agent: *\nAllow: /\nDisallow: /admin\nSitemap: " . View::absolute('/sitemap.xml') . "\n");
|
|
}
|
|
|
|
public function missing(Request $request): Response
|
|
{
|
|
$path = $request->path();
|
|
if ($request->method() === 'GET' && strlen($path) > 1 && str_ends_with($path, '/')) {
|
|
$normal = rtrim($path, '/');
|
|
$single = preg_match('~^/[a-z0-9-]+$~D', $normal);
|
|
$known = $single && ((new Repository())->page(substr($normal, 1))['status'] ?? null) === 'published' && $normal !== '/home';
|
|
if ($known || in_array($normal, ['/services', '/network'], true) || preg_match('~^/articles(?:/[a-z0-9-]+){0,2}$~D', $normal)) {
|
|
$query = $request->queryString();
|
|
|
|
return View::redirect($normal . ($query !== '' ? '?' . $query : ''), 301);
|
|
}
|
|
}
|
|
|
|
return View::render('missing', [
|
|
'site' => (new Repository())->site(), 'title' => '页面未找到 · 来笙', 'description' => '',
|
|
'path' => null, 'noindex' => true,
|
|
], 404);
|
|
}
|
|
|
|
private function sitemapUrl(string $path, ?string $updated): string
|
|
{
|
|
$url = htmlspecialchars(View::absolute($path), ENT_XML1 | ENT_QUOTES, 'UTF-8');
|
|
$date = $updated ? '<lastmod>' . View::date($updated, 'Y-m-d') . '</lastmod>' : '';
|
|
|
|
return ' <url><loc>' . $url . '</loc>' . $date . '</url>';
|
|
}
|
|
}
|