GitGram — rss.php — GitGram
<?php
ob_start();
require_once __DIR__ . '/config.php';
if (!IS_INSTALLED) { http_response_code(404); exit; }
require_once ROOT_PATH . '/includes/db.php';
require_once ROOT_PATH . '/includes/functions.php';
require_once ROOT_PATH . '/includes/auth.php';
require_once ROOT_PATH . '/includes/markdown.php';

$topicSlug = trim($_GET['topic'] ?? '');
$limit     = (int)get_setting('rss_items', '20');

// text/xml displays inline in browsers; application/rss+xml triggers download in modern browsers
ob_end_clean();
header('Content-Type: text/xml; charset=UTF-8');
header('Content-Disposition: inline');
header('X-Content-Type-Options: nosniff');

$siteTitle = get_setting('site_title', SITE_NAME);
$siteDesc  = get_setting('site_description');
$siteUrl   = base_url();

if ($topicSlug) {
    $topic = get_topic($topicSlug);
    if (!$topic) { http_response_code(404); echo '<?xml version="1.0"?><rss><channel></channel></rss>'; exit; }
    $feedTitle = $siteTitle . ' — ' . $topic['title'];
    $feedDesc  = $topic['description'] ?: $siteDesc;
    $feedUrl   = rss_url($topicSlug);
    $docs      = get_documents($topic['id']);
    $docs      = array_slice($docs, 0, $limit);
} else {
    $feedTitle = $siteTitle . ' — All Documents';
    $feedDesc  = $siteDesc;
    $feedUrl   = rss_url();
    $topic     = null;
    $docs      = get_recent_documents($limit);
}

function rss_date(string $dt): string {
    return date('D, d M Y H:i:s O', strtotime($dt));
}
?>
<?= '<?xml version="1.0" encoding="UTF-8"?>' . "\n" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title><?= htmlspecialchars($feedTitle) ?></title>
    <link><?= htmlspecialchars($siteUrl) ?></link>
    <description><?= htmlspecialchars($feedDesc) ?></description>
    <language>en-us</language>
    <lastBuildDate><?= rss_date(date('Y-m-d H:i:s')) ?></lastBuildDate>
    <generator>IndexGram v<?= SITE_VERSION ?></generator>
    <atom:link href="<?= htmlspecialchars($feedUrl) ?>" rel="self" type="application/rss+xml"/>
<?php foreach ($docs as $d):
    $dTopic = $topic ?? get_topic_by_id((int)$d['topic_id']);
    if (!$dTopic) continue;
    $itemUrl   = htmlspecialchars(doc_url($dTopic['slug'], $d['slug']));
    $itemTitle = htmlspecialchars($d['title']);
    $itemDesc  = htmlspecialchars($d['meta_description'] ?: substr(strip_tags(md($d['content'])), 0, 300));
    $itemDate  = rss_date($d['updated_at']);
    $isNew     = ($d['updated_at'] === $d['created_at']);
    $dtags     = get_document_tags($d['id']);
?>
    <item>
      <title><?= $itemTitle ?> <?= $isNew ? '' : '[Updated]' ?></title>
      <link><?= $itemUrl ?></link>
      <guid isPermaLink="true"><?= $itemUrl ?></guid>
      <pubDate><?= $itemDate ?></pubDate>
      <description><![CDATA[<?= md($d['content']) ?>]]></description>
      <?php if (!empty($d['author_name'])): ?>
      <dc:creator><?= htmlspecialchars($d['author_name']) ?></dc:creator>
      <?php endif; ?>
      <?php foreach ($dtags as $tg): ?>
      <category><?= htmlspecialchars($tg['name']) ?></category>
      <?php endforeach; ?>
    </item>
<?php endforeach; ?>
  </channel>
</rss>
Ready
GitGram