mirror of
https://git.tt-rss.org/fox/tt-rss.git
synced 2025-08-06 14:17:27 +02:00
Separate feed type detection from init, don't subscribe on failures.
Also some FeedParser tweaks.
This commit is contained in:
parent
46e05583a9
commit
0cd788220d
@ -14,8 +14,8 @@ class FeedParser {
|
|||||||
|
|
||||||
private ?string $title = null;
|
private ?string $title = null;
|
||||||
|
|
||||||
/** @var FeedParser::FEED_*|null */
|
/** @var FeedParser::FEED_* */
|
||||||
private ?int $type = null;
|
private int $type;
|
||||||
|
|
||||||
private ?DOMXPath $xpath = null;
|
private ?DOMXPath $xpath = null;
|
||||||
|
|
||||||
@ -27,6 +27,9 @@ class FeedParser {
|
|||||||
function __construct(string $data) {
|
function __construct(string $data) {
|
||||||
libxml_use_internal_errors(true);
|
libxml_use_internal_errors(true);
|
||||||
libxml_clear_errors();
|
libxml_clear_errors();
|
||||||
|
|
||||||
|
$this->type = $this::FEED_UNKNOWN;
|
||||||
|
|
||||||
$this->doc = new DOMDocument();
|
$this->doc = new DOMDocument();
|
||||||
$this->doc->loadXML($data);
|
$this->doc->loadXML($data);
|
||||||
|
|
||||||
@ -43,71 +46,54 @@ class FeedParser {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
libxml_clear_errors();
|
libxml_clear_errors();
|
||||||
|
|
||||||
|
if ($this->error)
|
||||||
|
return;
|
||||||
|
|
||||||
|
$this->xpath = new DOMXPath($this->doc);
|
||||||
|
$this->xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
|
||||||
|
$this->xpath->registerNamespace('atom03', 'http://purl.org/atom/ns#');
|
||||||
|
$this->xpath->registerNamespace('media', 'http://search.yahoo.com/mrss/');
|
||||||
|
$this->xpath->registerNamespace('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
|
||||||
|
$this->xpath->registerNamespace('slash', 'http://purl.org/rss/1.0/modules/slash/');
|
||||||
|
$this->xpath->registerNamespace('dc', 'http://purl.org/dc/elements/1.1/');
|
||||||
|
$this->xpath->registerNamespace('content', 'http://purl.org/rss/1.0/modules/content/');
|
||||||
|
$this->xpath->registerNamespace('thread', 'http://purl.org/syndication/thread/1.0');
|
||||||
}
|
}
|
||||||
|
|
||||||
function init() : void {
|
/**
|
||||||
$xpath = new DOMXPath($this->doc);
|
* @return bool false if initialization couldn't occur (e.g. parsing error or unrecognized feed type), otherwise true
|
||||||
$xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
|
*/
|
||||||
$xpath->registerNamespace('atom03', 'http://purl.org/atom/ns#');
|
function init(): bool {
|
||||||
$xpath->registerNamespace('media', 'http://search.yahoo.com/mrss/');
|
if ($this->error)
|
||||||
$xpath->registerNamespace('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
|
return false;
|
||||||
$xpath->registerNamespace('slash', 'http://purl.org/rss/1.0/modules/slash/');
|
|
||||||
$xpath->registerNamespace('dc', 'http://purl.org/dc/elements/1.1/');
|
|
||||||
$xpath->registerNamespace('content', 'http://purl.org/rss/1.0/modules/content/');
|
|
||||||
$xpath->registerNamespace('thread', 'http://purl.org/syndication/thread/1.0');
|
|
||||||
|
|
||||||
$this->xpath = $xpath;
|
$type = $this->get_type();
|
||||||
|
|
||||||
$root_list = $xpath->query("(//atom03:feed|//atom:feed|//channel|//rdf:rdf|//rdf:RDF)");
|
if ($type === self::FEED_UNKNOWN)
|
||||||
|
return false;
|
||||||
|
|
||||||
if (!empty($root_list) && $root_list->length > 0) {
|
$xpath = $this->xpath;
|
||||||
|
|
||||||
/** @var DOMElement|null $root */
|
switch ($type) {
|
||||||
$root = $root_list->item(0);
|
|
||||||
|
|
||||||
if ($root) {
|
|
||||||
$this->type = match (mb_strtolower($root->tagName)) {
|
|
||||||
'rdf:rdf' => $this::FEED_RDF,
|
|
||||||
'channel' => $this::FEED_RSS,
|
|
||||||
'feed', 'atom:feed' => $this::FEED_ATOM,
|
|
||||||
default => $this::FEED_UNKNOWN,
|
|
||||||
};
|
|
||||||
|
|
||||||
if ($this->type === $this::FEED_UNKNOWN) {
|
|
||||||
$this->error ??= 'Unknown/unsupported feed type';
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
switch ($this->type) {
|
|
||||||
case $this::FEED_ATOM:
|
case $this::FEED_ATOM:
|
||||||
|
$title = $xpath->query('//atom:feed/atom:title')->item(0)
|
||||||
$title = $xpath->query("//atom:feed/atom:title")->item(0);
|
?? $xpath->query('//atom03:feed/atom03:title')->item(0);
|
||||||
|
|
||||||
if (!$title)
|
|
||||||
$title = $xpath->query("//atom03:feed/atom03:title")->item(0);
|
|
||||||
|
|
||||||
|
|
||||||
if ($title) {
|
if ($title) {
|
||||||
$this->title = $title->nodeValue;
|
$this->title = $title->nodeValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$link = $xpath->query("//atom:feed/atom:link[not(@rel)]")->item(0);
|
|
||||||
|
|
||||||
if (!$link)
|
|
||||||
$link = $xpath->query("//atom:feed/atom:link[@rel='alternate']")->item(0);
|
|
||||||
|
|
||||||
if (!$link)
|
|
||||||
$link = $xpath->query("//atom03:feed/atom03:link[not(@rel)]")->item(0);
|
|
||||||
|
|
||||||
if (!$link)
|
|
||||||
$link = $xpath->query("//atom03:feed/atom03:link[@rel='alternate']")->item(0);
|
|
||||||
|
|
||||||
/** @var DOMElement|null $link */
|
/** @var DOMElement|null $link */
|
||||||
if ($link && $link->hasAttributes()) {
|
$link = $xpath->query('//atom:feed/atom:link[not(@rel)]')->item(0)
|
||||||
$this->link = $link->getAttribute("href");
|
?? $xpath->query("//atom:feed/atom:link[@rel='alternate']")->item(0)
|
||||||
}
|
?? $xpath->query('//atom03:feed/atom03:link[not(@rel)]')->item(0)
|
||||||
|
?? $xpath->query("//atom03:feed/atom03:link[@rel='alternate']")->item(0);
|
||||||
|
|
||||||
|
if ($link?->getAttribute('href'))
|
||||||
|
$this->link = $link->getAttribute('href');
|
||||||
|
|
||||||
$articles = $xpath->query("//atom:entry");
|
$articles = $xpath->query("//atom:entry");
|
||||||
|
|
||||||
@ -165,16 +151,15 @@ class FeedParser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->title) $this->title = trim($this->title);
|
|
||||||
if ($this->link) $this->link = trim($this->link);
|
|
||||||
|
|
||||||
} else {
|
|
||||||
$this->error ??= "Unknown/unsupported feed type";
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($this->title)
|
||||||
|
$this->title = trim($this->title);
|
||||||
|
|
||||||
|
if ($this->link)
|
||||||
|
$this->link = trim($this->link);
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @deprecated use Errors::format_libxml_error() instead */
|
/** @deprecated use Errors::format_libxml_error() instead */
|
||||||
@ -192,6 +177,33 @@ class FeedParser {
|
|||||||
return $this->libxml_errors;
|
return $this->libxml_errors;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return FeedParser::FEED_*
|
||||||
|
*/
|
||||||
|
function get_type(): int {
|
||||||
|
if ($this->type !== self::FEED_UNKNOWN || $this->error)
|
||||||
|
return $this->type;
|
||||||
|
|
||||||
|
$root_list = $this->xpath->query('(//atom03:feed|//atom:feed|//channel|//rdf:rdf|//rdf:RDF)');
|
||||||
|
|
||||||
|
if ($root_list && $root_list->length > 0) {
|
||||||
|
/** @var DOMElement $root */
|
||||||
|
$root = $root_list->item(0);
|
||||||
|
|
||||||
|
$this->type = match (mb_strtolower($root->tagName)) {
|
||||||
|
'rdf:rdf' => self::FEED_RDF,
|
||||||
|
'channel' => self::FEED_RSS,
|
||||||
|
'feed', 'atom:feed' => self::FEED_ATOM,
|
||||||
|
default => self::FEED_UNKNOWN,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->type === self::FEED_UNKNOWN)
|
||||||
|
$this->error ??= 'Unknown/unsupported feed type';
|
||||||
|
|
||||||
|
return $this->type;
|
||||||
|
}
|
||||||
|
|
||||||
function get_link() : string {
|
function get_link() : string {
|
||||||
return clean($this->link ?? '');
|
return clean($this->link ?? '');
|
||||||
}
|
}
|
||||||
|
@ -999,7 +999,7 @@ class Feeds extends Handler_Protected {
|
|||||||
* Here you should call extractfeedurls in rpc-backend
|
* Here you should call extractfeedurls in rpc-backend
|
||||||
* to get all possible feeds.
|
* to get all possible feeds.
|
||||||
* 5 - Couldn't download the URL content.
|
* 5 - Couldn't download the URL content.
|
||||||
* 6 - currently unused
|
* 6 - Feed parsing failure (invalid content)
|
||||||
* 7 - Error while creating feed database entry.
|
* 7 - Error while creating feed database entry.
|
||||||
* 8 - Permission denied (ACCESS_LEVEL_READONLY).
|
* 8 - Permission denied (ACCESS_LEVEL_READONLY).
|
||||||
*/
|
*/
|
||||||
@ -1059,6 +1059,11 @@ class Feeds extends Handler_Protected {
|
|||||||
$url = key($feedUrls);
|
$url = key($feedUrls);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Don't allow subscribing if the content is invalid
|
||||||
|
$fp = new FeedParser($contents);
|
||||||
|
if ($fp->error() || $fp->get_type() === FeedParser::FEED_UNKNOWN)
|
||||||
|
return ['code' => 6, 'message' => truncate_string(clean($contents), 250, '…')];
|
||||||
|
|
||||||
$feed = ORM::for_table('ttrss_feeds')
|
$feed = ORM::for_table('ttrss_feeds')
|
||||||
->where('feed_url', $url)
|
->where('feed_url', $url)
|
||||||
->where('owner_uid', $_SESSION['uid'])
|
->where('owner_uid', $_SESSION['uid'])
|
||||||
|
@ -305,9 +305,8 @@ class RSSUtils {
|
|||||||
|
|
||||||
if ($feed_data) {
|
if ($feed_data) {
|
||||||
$rss = new FeedParser($feed_data);
|
$rss = new FeedParser($feed_data);
|
||||||
$rss->init();
|
|
||||||
|
|
||||||
if (!$rss->error()) {
|
if ($rss->init()) {
|
||||||
$basic_info = [
|
$basic_info = [
|
||||||
'title' => mb_substr(clean($rss->get_title()), 0, 199),
|
'title' => mb_substr(clean($rss->get_title()), 0, 199),
|
||||||
'site_url' => mb_substr(UrlHelper::rewrite_relative($feed->feed_url, clean($rss->get_link())), 0, 245),
|
'site_url' => mb_substr(UrlHelper::rewrite_relative($feed->feed_url, clean($rss->get_link())), 0, 245),
|
||||||
@ -590,9 +589,8 @@ class RSSUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$rss = new FeedParser($feed_data);
|
$rss = new FeedParser($feed_data);
|
||||||
$rss->init();
|
|
||||||
|
|
||||||
if (!$rss->error()) {
|
if ($rss->init()) {
|
||||||
|
|
||||||
Debug::log("running HOOK_FEED_PARSED handlers...", Debug::LOG_VERBOSE);
|
Debug::log("running HOOK_FEED_PARSED handlers...", Debug::LOG_VERBOSE);
|
||||||
|
|
||||||
|
@ -195,6 +195,9 @@ const CommonDialogs = {
|
|||||||
case 5:
|
case 5:
|
||||||
dialog.show_error(__("Couldn't download the specified URL."), App.escapeHtml(rc['message']));
|
dialog.show_error(__("Couldn't download the specified URL."), App.escapeHtml(rc['message']));
|
||||||
break;
|
break;
|
||||||
|
case 6:
|
||||||
|
dialog.show_error(__("Invalid content."), App.escapeHtml(rc['message']));
|
||||||
|
break;
|
||||||
case 7:
|
case 7:
|
||||||
dialog.show_error(__("Error while creating feed database entry."));
|
dialog.show_error(__("Error while creating feed database entry."));
|
||||||
break;
|
break;
|
||||||
|
Loading…
Reference in New Issue
Block a user