Files
FreshRSS/app/views/index/rss.phtml
T
CaoMeiYouRenandAlexandre Alapetite 8e281292f9 RSS: emit <enclosure> for BitTorrent attachments (#9553)
* feat: add the <enclosure/> field to enhance compatibility with BitTorrent downloaders

Closes #6227

Changes proposed in this pull request:

- Emit an additional RSS 2.0 `<enclosure>` element for BitTorrent attachments in the
  generated RSS output (`p/api/query.php?...&f=rss` and the per-feed RSS view).
  The existing `<media:content>` output is unchanged.
- Emit it only for `application/x-bittorrent`, `magnet:` and `.torrent` URLs, and skip
  images / audio / video. Since 1.24 thumbnails are prepended to the enclosure list, a
  naive implementation would duplicate cover images (and podcast media) as attachments.
- Always set `type="application/x-bittorrent"`, and copy `length` when the feed provides it.
- Add unit tests in `tests/app/Views/rssViewTest.php`: torrent, magnet without type,
  image attachment skipped.

* fix(rss): emit <enclosure> only for BitTorrent attachments

The previous patch emitted <enclosure> for every enclosure. Since FreshRSS
1.24, thumbnails are prepended to the enclosures list, so cover images and
podcast audio were also duplicated as RSS attachments. Emit the tag only for
application/x-bittorrent, magnet: or .torrent URLs, and always set
type="application/x-bittorrent" so clients such as qBittorrent can use it.

Add three rssViewTest cases: torrent, magnet without type, image skipped.

* Improve regex

* Minor whitespace

---------

Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr>
2026-11-11 21:02:30 +01:00

123 lines
4.8 KiB
PHTML

<?php
declare(strict_types=1);
/** @var FreshRSS_View $this */
?>
<?= '<?xml version="1.0" encoding="UTF-8" ?>'; ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:media="http://search.yahoo.com/mrss/" <?=
$this->rss_base === '' ? '' : ' xml:base="' . $this->rss_base . '"' ?>>
<channel>
<title><?= $this->rss_title ?></title>
<link><?= $this->html_url ?></link>
<description><?= $this->description ?: _t('index.feed.rss_of', $this->rss_title) ?></description>
<pubDate><?= date('D, d M Y H:i:s O') ?></pubDate>
<lastBuildDate><?= gmdate('D, d M Y H:i:s') ?> GMT</lastBuildDate>
<atom:link href="<?= $this->rss_url ?>" rel="self" type="application/rss+xml" />
<?php if ($this->image_url !== ''): ?>
<image>
<url><?= $this->image_url ?></url>
<title><?= $this->rss_title ?></title>
<link><?= $this->html_url ?></link>
</image>
<?php endif; ?>
<?php
foreach ($this->entries as $item) {
if (!$this->internal_rendering) {
/** @var FreshRSS_Entry|null $item */
$item = Minz_ExtensionManager::callHook(Minz_HookType::EntryBeforeDisplay, $item);
if ($item === null) {
continue;
}
}
?>
<item>
<title><?= $item->title() ?></title>
<link><?= $item->link() ?></link>
<?php
$authors = $item->authors();
if (is_array($authors)) {
foreach ($authors as $author) {
echo "\t\t\t", '<dc:creator>', $author, '</dc:creator>', "\n";
}
}
if ($this->includeUserLabels) {
$categories = $this->entryIdsTagNames['e_' . $item->id()] ?? [];
if (is_array($categories)) {
foreach ($categories as $category) {
echo "\t\t\t", '<category>', htmlspecialchars($this->userLabelPrefix, ENT_NOQUOTES, 'UTF-8'), $category, '</category>', "\n";
}
}
}
if (!$this->excludeArticleTags) {
$categories = $item->tags();
if (is_array($categories)) {
foreach ($categories as $category) {
echo "\t\t\t", '<category>', $category, '</category>', "\n";
}
}
}
$enclosures = iterator_to_array($item->enclosures(false), preserve_keys: false); // TODO: Optimise: avoid iterator_to_array if possible
$thumbnail = $item->thumbnail(false);
if (!empty($thumbnail['url'])) {
// https://www.rssboard.org/media-rss#media-thumbnails
echo "\t\t\t", '<media:thumbnail url="' . $thumbnail['url']
. (empty($thumbnail['width']) ? '' : '" width="' . $thumbnail['width'])
. (empty($thumbnail['height']) ? '' : '" height="' . $thumbnail['height'])
. (empty($thumbnail['time']) ? '' : '" time="' . $thumbnail['time'])
. '" />', "\n";
// Mostly for MailChimp + Feedbro which do not support <media:thumbnail> https://mailchimp.com/help/rss-merge-tags/
$thumbnail['medium'] ??= 'image';
array_unshift($enclosures, $thumbnail);
}
$urls = [];
foreach ($enclosures as $enclosure) {
if (empty($enclosure['url']) || isset($urls[$enclosure['url']])) {
continue;
}
$urls[$enclosure['url']] = true;
$credits = $enclosure['credit'] ?? [];
if (!is_array($credits)) { // For entries < FreshRSS 1.24
$credits = [$credits];
}
$mediaCredits = '';
foreach ($credits as $credit) {
$mediaCredits .= '<media:credit>' . $credit . '</media:credit>';
}
// https://www.rssboard.org/media-rss
echo "\t\t\t", '<media:content url="' . $enclosure['url']
. (empty($enclosure['medium']) ? '' : '" medium="' . $enclosure['medium'])
. (empty($enclosure['type']) ? '' : '" type="' . $enclosure['type'])
. (empty($enclosure['length']) ? '' : '" length="' . $enclosure['length'])
. (empty($enclosure['height']) ? '' : '" height="' . $enclosure['height'])
. (empty($enclosure['width']) ? '' : '" width="' . $enclosure['width'])
. '">'
. (empty($enclosure['title']) ? '' : '<media:title type="html">' . $enclosure['title'] . '</media:title>')
. $mediaCredits
. '</media:content>', "\n";
// RSS 2.0 <enclosure> for BitTorrent clients that ignore Media RSS (e.g. qBittorrent)
// https://www.rssboard.org/rss-specification#ltenclosuregtSubelementOfLtitemgt
$enclosureUrl = (string)$enclosure['url'];
$enclosureType = strtolower((string)($enclosure['type'] ?? ''));
if ($enclosureType === 'application/x-bittorrent' || preg_match('~^magnet:|\.torrent(\?|#|$)~i', $enclosureUrl) === 1) {
echo "\t\t\t", '<enclosure url="' . $enclosureUrl . '" type="application/x-bittorrent"'
. (empty($enclosure['length']) ? '' : ' length="' . $enclosure['length'] . '"')
. '/>', "\n";
}
}
?>
<description><![CDATA[<?php
echo $item->content(false);
?>]]></description>
<pubDate><?= date('D, d M Y H:i:s O', $item->date(true)) ?></pubDate>
<guid isPermaLink="false"><?= $item->id() > 0 ? $item->id() : $item->guid() ?></guid>
</item>
<?php } ?>
</channel>
</rss>