From 8255f71c2eaf8df42ab70a70c0bbcdec6b241de6 Mon Sep 17 00:00:00 2001 From: wn_ Date: Mon, 2 Jun 2025 19:11:55 +0000 Subject: [PATCH 1/4] Fail early in 'Handler_Public#generate_syndicated_feed()' on unrecognized format. --- classes/Handler_Public.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/classes/Handler_Public.php b/classes/Handler_Public.php index 347a38381..e46ce2e5f 100644 --- a/classes/Handler_Public.php +++ b/classes/Handler_Public.php @@ -8,6 +8,13 @@ class Handler_Public extends Handler { int $limit, int $offset, string $search, string $view_mode = "", string $format = 'atom', string $order = "", string $orig_guid = "", string $start_ts = ""): void { + // fail early if the requested format isn't recognized + if (!in_array($format, ['atom', 'json'])) { + header('Content-Type: text/plain; charset=utf-8'); + print "Unknown format: $format."; + return; + } + $note_style = "background-color : #fff7d5; border-width : 1px; ". "padding : 5px; border-style : dashed; border-color : #e7d796;". @@ -251,9 +258,6 @@ class Handler_Public extends Handler { header("Content-Type: application/json; charset=utf-8"); print json_encode($feed); - } else { - header("Content-Type: text/plain; charset=utf-8"); - print "Unknown format: $format."; } } From 446f9dcb23ea2d2802016e9452517d92fbd8543c Mon Sep 17 00:00:00 2001 From: wn_ Date: Mon, 2 Jun 2025 20:31:23 +0000 Subject: [PATCH 2/4] Style tweaks in 'Handler_Public#generate_syndicated_feed()' --- classes/Handler_Public.php | 75 ++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 40 deletions(-) diff --git a/classes/Handler_Public.php b/classes/Handler_Public.php index e46ce2e5f..51cdb8266 100644 --- a/classes/Handler_Public.php +++ b/classes/Handler_Public.php @@ -25,13 +25,11 @@ class Handler_Public extends Handler { list($override_order, $skip_first_id_check) = Feeds::_order_to_override_query($order); if (!$override_order) { - $override_order = "date_entered DESC, updated DESC"; - - if ($feed == Feeds::FEED_PUBLISHED && !$is_cat) { - $override_order = "last_published DESC"; - } else if ($feed == Feeds::FEED_STARRED && !$is_cat) { - $override_order = "last_marked DESC"; - } + $override_order = match (true) { + $feed == Feeds::FEED_PUBLISHED && !$is_cat => 'last_published DESC', + $feed == Feeds::FEED_STARRED && !$is_cat => 'last_marked DESC', + default => 'date_entered DESC, updated DESC', + }; } $params = array( @@ -94,6 +92,7 @@ class Handler_Public extends Handler { $tpl->setVariable('FEED_URL', htmlspecialchars($feed_self_url), true); $tpl->setVariable('SELF_URL', htmlspecialchars(Config::get_self_url()), true); + while ($line = $result->fetch()) { $line["content_preview"] = Sanitizer::sanitize(truncate_string(strip_tags($line["content"]), 100, '...')); @@ -187,14 +186,14 @@ class Handler_Public extends Handler { } print $tmp; - } else if ($format == 'json') { + } else { // $format == 'json' - $feed = array(); - - $feed['title'] = $feed_title; - $feed['feed_url'] = $feed_self_url; - $feed['self_url'] = Config::get_self_url(); - $feed['articles'] = []; + $feed = [ + 'title' => $feed_title, + 'feed_url' => $feed_self_url, + 'self_url' => Config::get_self_url(), + 'articles' => [], + ]; while ($line = $result->fetch()) { @@ -213,30 +212,26 @@ class Handler_Public extends Handler { }, $line, $feed, $is_cat, $owner_uid); - $article = array(); - - $article['id'] = $line['link']; - $article['link'] = $line['link']; - $article['title'] = $line['title']; - $article['excerpt'] = $line["content_preview"]; - $article['content'] = Sanitizer::sanitize($line["content"], false, $owner_uid, $feed_site_url, null, $line["id"]); - $article['updated'] = date('c', strtotime($line["updated"] ?? '')); - - if (!empty($line['note'])) $article['note'] = $line['note']; - if (!empty($line['author'])) $article['author'] = $line['author']; - - $article['source'] = [ - 'link' => $line['site_url'] ? $line["site_url"] : Config::get_self_url(), - 'title' => $line['feed_title'] ?? $feed_title + $article = [ + 'id' => $line['link'], + 'link' => $line['link'], + 'title' => $line['title'], + 'content' => Sanitizer::sanitize($line['content'], false, $owner_uid, $feed_site_url, null, $line['id']), + 'updated' => date('c', strtotime($line['updated'] ?? '')), + 'source' => [ + 'link' => $line['site_url'] ?: Config::get_self_url(), + 'title' => $line['feed_title'] ?? $feed_title, + ], ]; - if (count($line["tags"]) > 0) { - $article['tags'] = array(); + if (!empty($line['note'])) + $article['note'] = $line['note']; - foreach ($line["tags"] as $tag) { - array_push($article['tags'], $tag); - } - } + if (!empty($line['author'])) + $article['author'] = $line['author']; + + if (count($line['tags']) > 0) + $article['tags'] = $line['tags']; $enclosures = Article::_get_enclosures($line["id"]); @@ -244,11 +239,11 @@ class Handler_Public extends Handler { $article['enclosures'] = array(); foreach ($enclosures as $e) { - $type = $e['content_type']; - $url = $e['content_url']; - $length = $e['duration']; - - array_push($article['enclosures'], array("url" => $url, "type" => $type, "length" => $length)); + $article['enclosures'][] = [ + 'url' => $e['content_url'], + 'type' => $e['content_type'], + 'length' => $e['duration'], + ]; } } From 814ab4816968d304069e7b4ee83856db2398c84f Mon Sep 17 00:00:00 2001 From: wn_ Date: Mon, 2 Jun 2025 21:17:21 +0000 Subject: [PATCH 3/4] Use the current timestamp for feed-level 'updated' in 'Handler_Public#generate_syndicated_feed()'. The last article's 'updated' value was not a good indicator of when the feed updated for various reasons, so we'll just use the current timestamp to represent the dynamic nature of the content. --- classes/Handler_Public.php | 1 + templates/generated_feed.txt | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/classes/Handler_Public.php b/classes/Handler_Public.php index 51cdb8266..17478c371 100644 --- a/classes/Handler_Public.php +++ b/classes/Handler_Public.php @@ -88,6 +88,7 @@ class Handler_Public extends Handler { $tpl->readTemplateFromFile("generated_feed.txt"); $tpl->setVariable('FEED_TITLE', $feed_title, true); + $tpl->setVariable('FEED_UPDATED', date('c'), true); $tpl->setVariable('VERSION', Config::get_version(), true); $tpl->setVariable('FEED_URL', htmlspecialchars($feed_self_url), true); diff --git a/templates/generated_feed.txt b/templates/generated_feed.txt index 617162652..82e780554 100644 --- a/templates/generated_feed.txt +++ b/templates/generated_feed.txt @@ -3,7 +3,7 @@ ${FEED_TITLE} Tiny Tiny RSS/${VERSION} -${ARTICLE_UPDATED_ATOM} +${FEED_UPDATED} ${FEED_URL} From 3047b294a6206e0333638694f2f2f6cd8b50593b Mon Sep 17 00:00:00 2001 From: wn_ Date: Tue, 3 Jun 2025 15:17:27 +0000 Subject: [PATCH 4/4] Minor CSS+style tweaks in 'Handler_Public#generate_syndicated_feed()'. --- classes/Handler_Public.php | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/classes/Handler_Public.php b/classes/Handler_Public.php index 17478c371..405d16d24 100644 --- a/classes/Handler_Public.php +++ b/classes/Handler_Public.php @@ -15,12 +15,11 @@ class Handler_Public extends Handler { return; } - $note_style = "background-color : #fff7d5; - border-width : 1px; ". - "padding : 5px; border-style : dashed; border-color : #e7d796;". - "margin-bottom : 1em; color : #9a8c59;"; + $note_style = 'color: #9a8c59; background-color: #fff7d5; ' + . 'border: 1px dashed #e7d796; padding: 5px; margin-bottom: 1em;'; - if (!$limit) $limit = 60; + if (!$limit) + $limit = 60; list($override_order, $skip_first_id_check) = Feeds::_order_to_override_query($order); @@ -80,7 +79,8 @@ class Handler_Public extends Handler { "/public.php?op=rss&id=$feed&key=" . Feeds::_get_access_key($feed, false, $owner_uid); - if (!$feed_site_url) $feed_site_url = Config::get_self_url(); + if (!$feed_site_url) + $feed_site_url = Config::get_self_url(); if ($format == 'atom') { $tpl = new Templator(); @@ -126,8 +126,7 @@ class Handler_Public extends Handler { $content = DiskCache::rewrite_urls($content); if ($line['note']) { - $content = "
Article note: " . $line['note'] . "
" . - $content; + $content = "
Article note: " . $line['note'] . "
" . $content; $tpl->setVariable('ARTICLE_NOTE', htmlspecialchars($line['note']), true); } @@ -154,7 +153,7 @@ class Handler_Public extends Handler { foreach ($enclosures as $e) { $type = htmlspecialchars($e['content_type']); $url = htmlspecialchars($e['content_url']); - $length = $e['duration'] ? $e['duration'] : 1; + $length = $e['duration'] ?: 1; $tpl->setVariable('ARTICLE_ENCLOSURE_URL', $url, true); $tpl->setVariable('ARTICLE_ENCLOSURE_TYPE', $type, true);