Some feed fixes. Think atom is still broken.

This commit is contained in:
Greg Sarjeant 2025-06-14 14:17:49 -04:00
parent 427558bd8c
commit 0f0cfb5b22
3 changed files with 23 additions and 25 deletions

View File

@ -1,13 +1,15 @@
<?php <?php
class Util { class Util {
public static function escape_and_linkify(string $text): string { public static function escape_and_linkify(string $text, int $flags = ENT_NOQUOTES | ENT_HTML5, bool $new_window = true ): string {
// escape dangerous characters, but preserve quotes // escape dangerous characters, but preserve quotes
$safe = htmlspecialchars($text, ENT_NOQUOTES | ENT_HTML5, 'UTF-8'); $safe = htmlspecialchars($text, $flags, 'UTF-8');
$link_attrs = $new_window ? ' target="_blank" rel="noopener noreferrer"' : '';
// convert URLs to links // convert URLs to links
$safe = preg_replace_callback( $safe = preg_replace_callback(
'~(https?://[^\s<>"\'()]+)~i', '~(https?://[^\s<>"\'()]+)~i',
fn($matches) => '<a href="' . htmlspecialchars($matches[1], ENT_QUOTES, 'UTF-8') . '" target="_blank" rel="noopener noreferrer">' . $matches[1] . '</a>', fn($matches) => '<a href="' . htmlspecialchars($matches[1], ENT_QUOTES, 'UTF-8') . '"' . $link_attrs . '>' . $matches[1] . '</a>',
$safe $safe
); );

View File

@ -13,11 +13,11 @@ echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";
<title><?= "$siteTitle Atom Feed" ?></title> <title><?= "$siteTitle Atom Feed" ?></title>
<link rel="self" <link rel="self"
type="application/atom+xml" type="application/atom+xml"
title="<?php echo htmlspecialchars($config->siteTitle) ?> Atom Feed" title="<?php echo htmlspecialchars($config->siteTitle, ENT_XML1, 'UTF-8') ?> Atom Feed"
href="<?php echo htmlspecialchars($siteUrl . $basePath) ?>feed/atom"> href="<?php echo htmlspecialchars($siteUrl . $basePath, ENT_XML1, 'UTF-8') ?>feed/atom">
<link rel="alternate" href="<?= $siteUrl ?>"> <link rel="alternate" href="<?= $siteUrl . $basePath ?>">
<updated><?= $updated ?></updated> <updated><?= $updated ?></updated>
<id><?= $siteUrl ?></id> <id><?= $siteUrl . $basePath . 'feed/atom'?></id>
<author> <author>
<name><?= $siteTitle ?></name> <name><?= $siteTitle ?></name>
</author> </author>
@ -30,16 +30,17 @@ echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";
[$hour, $minute, $second] = $timeParts; [$hour, $minute, $second] = $timeParts;
$tickPath = "$year/$month/$day/$hour/$minute/$second"; $tickPath = "$year/$month/$day/$hour/$minute/$second";
$tickUrl = htmlspecialchars($siteUrl . $basePath . "tick/$tickPath"); $tickUrl = htmlspecialchars($siteUrl . $basePath . "tick/$tickPath", ENT_XML1, 'UTF-8');
$tickTime = date(DATE_ATOM, strtotime($tick['timestamp'])); $tickTime = date(DATE_ATOM, strtotime($tick['timestamp']));
$tickText = htmlspecialchars($tick['tick']); $tickTitle = htmlspecialchars($tick['tick'], ENT_XML1, 'UTF-8');
$tickContent = Util::escape_and_linkify($tick['tick'], ENT_XML1, false);
?> ?>
<entry> <entry>
<title><?= $tickText ?></title> <title><?= $tickTitle ?></title>
<link href="<?= $tickUrl ?>"/> <link rel="alternate" type="text/html" href="<?= $tickUrl ?>"/>
<id><?= $tickUrl ?></id> <id><?= $tickUrl ?></id>
<updated><?= $tickTime ?></updated> <updated><?= $tickTime ?></updated>
<content type="html"><?= $tickText ?></content> <content type="html"><?= $tickContent ?></content>
</entry> </entry>
<?php endforeach; ?> <?php endforeach; ?>
</feed> </feed>

View File

@ -9,15 +9,9 @@ echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
?> ?>
<rss version="2.0"> <rss version="2.0">
<channel> <channel>
<title><?php echo htmlspecialchars($config->siteTitle) ?> RSS Feed</title> <title><?php echo htmlspecialchars($config->siteTitle, ENT_XML1, 'UTF-8') ?> RSS Feed</title>
<link rel="self" <link><?php echo htmlspecialchars($config->baseUrl . $config->basePath, ENT_XML1, 'UTF-8')?></link>
type="application/rss+xml" <description><?php echo htmlspecialchars($config->siteDescription, ENT_XML1, 'UTF-8') ?></description>
title="<?php echo htmlspecialchars($config->siteTitle) ?> RSS Feed"
href="<?php echo htmlspecialchars($config->baseUrl . $config->basePath)?>feed/rss/">
<link rel="alternate"
type="text/html"
href="<?php echo htmlspecialchars($config->baseUrl . $config->basePath) ?>">
<description><?php echo htmlspecialchars($config->siteDescription) ?></description>
<language>en-us</language> <language>en-us</language>
<lastBuildDate><?php echo date(DATE_RSS); ?></lastBuildDate> <lastBuildDate><?php echo date(DATE_RSS); ?></lastBuildDate>
<?php foreach ($ticks as $tick): <?php foreach ($ticks as $tick):
@ -29,13 +23,14 @@ echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
[$hour, $minute, $second] = $timeParts; [$hour, $minute, $second] = $timeParts;
$tickPath = "$year/$month/$day/$hour/$minute/$second"; $tickPath = "$year/$month/$day/$hour/$minute/$second";
$tickUrl = $config->baseUrl . $config->basePath . $tickPath;
?> ?>
<item> <item>
<title><?php echo htmlspecialchars($tick['tick']); ?></title> <title><?php echo htmlspecialchars($tick['tick'], ENT_XML1, 'UTF-8'); ?></title>
<link><?php echo htmlspecialchars($config->baseUrl . $config->basePath . "tick/$tickPath"); ?></link> <link><?php echo htmlspecialchars($config->baseUrl . $config->basePath . "tick/$tickPath", ENT_XML1, 'UTF-8'); ?></link>
<description><?php echo htmlspecialchars($tick['tick']); ?></description> <description><?php echo Util::escape_and_linkify($tick['tick'], ENT_XML1, false); ?></description>
<pubDate><?php echo date(DATE_RSS, strtotime($tick['timestamp'])); ?></pubDate> <pubDate><?php echo date(DATE_RSS, strtotime($tick['timestamp'])); ?></pubDate>
<guid><?php echo htmlspecialchars($tickPath); ?></guid> <guid><?php echo htmlspecialchars($tickUrl, ENT_XML1, 'UTF-8'); ?></guid>
</item> </item>
<?php endforeach; ?> <?php endforeach; ?>
</channel> </channel>