page($request, 'home');
}
public function page(Request $request, string $slug): Response
{
$repo = new Repository();
$page = $repo->page($slug);
if (!$page) {
return $this->missing($request);
}
$isHome = $slug === 'home';
return View::render('page', [
'site' => $repo->site(),
'page' => $page,
'path' => $isHome ? '/' : '/' . $slug,
'title' => $page['title'],
'description' => $page['description'],
'socialImage' => $page['image'],
'socialAlt' => $page['alt'],
'articles' => $isHome ? $repo->articles(limit: 3) : [],
]);
}
public function archive(Request $request, ?string $category = null, ?string $tag = null): Response
{
$repo = new Repository();
$title = $category ? Repository::CATEGORIES[$category] : ($tag ?: '来笙动态');
$path = '/articles' . ($category ? '/category/' . $category : ($tag ? '/tag/' . rawurlencode($tag) : ''));
$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);
}
return View::render('archive', [
'site' => $repo->site(), 'title' => $title . ' · 来笙', 'headline' => $title,
'description' => '查看来笙的公告、新闻和产品动态。',
'path' => $path . ($page > 1 ? '?page=' . $page : ''), 'archivePath' => $path,
'category' => $category, 'tag' => $tag, '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::markdown($article['body']);
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'], 'noindex' => trim($articleHtml) === '',
]);
}
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 = ['', ''];
$pages = $repo->pages();
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[] = '';
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, '/');
if (preg_match('~^/(?:pagepan|highspeaker|sc|shnu-sc|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 ? '' . View::date($updated, 'Y-m-d') . '' : '';
return ' ' . $url . '' . $date . '';
}
}