v1.0.0.9
Publisium
- <?php
-
- declare(strict_types=1);
-
- require_once __DIR__ . '/includes/config.php';
- require_once __DIR__ . '/includes/database.php';
- require_once __DIR__ . '/includes/site-front.php';
-
- $settings = SiteFront::settings();
-
- if ($settings['sitemap_rss_enabled'] !== '1') {
- http_response_code(404);
- exit;
- }
-
- $db = Database::site();
- $baseUrl = rtrim(Config::get('APP_URL', ''), '/') . Config::get('APP_BASE_PATH', '');
-
- $category = null;
- if (isset($_GET['category'])) {
- if ($settings['rss_category_feeds_enabled'] !== '1') {
- http_response_code(404);
- exit;
- }
-
- $categoryStatement = $db->prepare('SELECT * FROM categories WHERE slug = :slug LIMIT 1');
- $categoryStatement->execute(['slug' => trim((string) $_GET['category'])]);
- $category = $categoryStatement->fetch() ?: null;
- if ($category === null) {
- http_response_code(404);
- exit;
- }
- $category = Languages::categories([$category])[0];
- }
-
- $articlesStatement = $db->prepare(
- "SELECT a.id, a.title, a.slug, a.excerpt, a.published_at, a.cover_image_path, a.category_id, c.name AS category_name FROM articles a LEFT JOIN categories c ON c.id = a.category_id WHERE a.status = 'published'"
- . ($category !== null ? ' AND a.category_id = :category_id' : '')
- . Languages::articleFilterSql('a') . " ORDER BY a.published_at DESC LIMIT 30"
- );
- $articlesStatement->execute($category !== null ? ['category_id' => (int) $category['id']] : []);
- $articles = Languages::articles($articlesStatement->fetchAll());
-
- header('Content-Type: application/rss+xml; charset=utf-8');
-
- echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
- echo '<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/"><channel>' . "\n";
- echo '<title>' . htmlspecialchars($category !== null ? $category['name'] . ' - ' . $settings['site_name'] : $settings['site_name']) . '</title>' . "\n";
- $channelLanguage = Languages::current();
- $channelLink = $channelLanguage === '' ? rtrim((string) Config::get('APP_URL', ''), '/') . SiteFront::homeUrl('') : rtrim((string) Config::get('APP_URL', ''), '/') . Languages::homeUrl($channelLanguage);
- if ($category !== null) {
- $channelLink = rtrim((string) Config::get('APP_URL', ''), '/') . rtrim((string) Config::get('APP_BASE_PATH', ''), '/') . Languages::prefix() . '/category.php?slug=' . rawurlencode((string) $category['slug']);
- }
- echo '<link>' . htmlspecialchars($channelLink) . '</link>' . "\n";
- if ($channelLanguage !== '') {
- echo '<language>' . htmlspecialchars($channelLanguage) . '</language>' . "\n";
- }
- $channelDescription = $category !== null && !empty($category['description']) ? (string) $category['description'] : $settings['site_description'];
- echo '<description>' . htmlspecialchars($channelDescription) . '</description>' . "\n";
-
- if ($settings['site_logo_url'] !== '') {
- echo '<image>';
- echo '<url>' . htmlspecialchars(SiteFront::absoluteUrl($settings['site_logo_url'])) . '</url>';
- echo '<title>' . htmlspecialchars($settings['site_name']) . '</title>';
- echo '<link>' . htmlspecialchars($channelLink) . '</link>';
- echo '</image>' . "\n";
- }
-
- $imageTypes = ['jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'png' => 'image/png', 'gif' => 'image/gif', 'webp' => 'image/webp', 'avif' => 'image/avif'];
-
- foreach ($articles as $article) {
- $url = rtrim(Config::get('APP_URL', ''), '/') . SiteFront::articleUrl($article['slug']);
- echo '<item>';
- echo '<title>' . htmlspecialchars($article['title']) . '</title>';
- echo '<link>' . htmlspecialchars($url) . '</link>';
- echo '<guid>' . htmlspecialchars($url) . '</guid>';
- echo '<description>' . htmlspecialchars((string) ($article['excerpt'] ?? '')) . '</description>';
- if (!empty($article['category_name'])) {
- echo '<category>' . htmlspecialchars((string) $article['category_name']) . '</category>';
- }
- echo '<pubDate>' . htmlspecialchars(date(DATE_RSS, strtotime($article['published_at']))) . '</pubDate>';
- if (!empty($article['cover_image_path'])) {
- $coverUrl = SiteFront::absoluteUrl((string) $article['cover_image_path']);
- $coverExtension = strtolower(pathinfo((string) parse_url($coverUrl, PHP_URL_PATH), PATHINFO_EXTENSION));
- $coverType = isset($imageTypes[$coverExtension]) ? ' type="' . $imageTypes[$coverExtension] . '"' : '';
- echo '<media:content url="' . htmlspecialchars($coverUrl, ENT_QUOTES) . '" medium="image"' . $coverType . '/>';
- echo '<media:thumbnail url="' . htmlspecialchars($coverUrl, ENT_QUOTES) . '"/>';
- }
- echo '</item>' . "\n";
- }
-
- echo '</channel></rss>';
-