summaryrefslogtreecommitdiff
path: root/classes/feeditem
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2021-05-21 15:39:41 +0300
committerAndrew Dolgov <[email protected]>2021-05-21 15:39:41 +0300
commitdff479af649005c819e16ed8e75278ab50c955b3 (patch)
tree76bba90f73c33267e62998d3a3d75dbfb9b47e75 /classes/feeditem
parentd09a64d6f9f5cb9c575986d32d1f8cfa3f183384 (diff)
feeditem_atom: support xml:base for enclosures and entry content
UrlHelper::rewrite_relative: use base URL path if relative url path is not absolute (experimental)
Diffstat (limited to 'classes/feeditem')
-rwxr-xr-xclasses/feeditem/atom.php51
1 files changed, 45 insertions, 6 deletions
diff --git a/classes/feeditem/atom.php b/classes/feeditem/atom.php
index a03080981..51358f36c 100755
--- a/classes/feeditem/atom.php
+++ b/classes/feeditem/atom.php
@@ -60,43 +60,76 @@ class FeedItem_Atom extends FeedItem_Common {
}
}
+ /** $base is optional (returns $content if $base is null), $content is an HTML string */
+ private function rewrite_content_to_base($base, $content) {
+
+ if (!empty($base) && !empty($content)) {
+
+ $tmpdoc = new DOMDocument();
+ if (@$tmpdoc->loadHTML('<?xml encoding="UTF-8">' . $content)) {
+ $tmpxpath = new DOMXPath($tmpdoc);
+
+ $elems = $tmpxpath->query("(//*[@href]|//*[@src])");
+
+ foreach ($elems as $elem) {
+ if ($elem->hasAttribute("href")) {
+ $elem->setAttribute("href",
+ UrlHelper::rewrite_relative($base, $elem->getAttribute("href")));
+ } else if ($elem->hasAttribute("src")) {
+ $elem->setAttribute("src",
+ UrlHelper::rewrite_relative($base, $elem->getAttribute("src")));
+ }
+ }
+
+ return $tmpdoc->saveXML();
+ }
+ }
+
+ return $content;
+ }
+
function get_content() {
$content = $this->elem->getElementsByTagName("content")->item(0);
if ($content) {
+ $base = $this->xpath->evaluate("string(ancestor-or-self::*[@xml:base][1]/@xml:base)", $content);
+
if ($content->hasAttribute('type')) {
if ($content->getAttribute('type') == 'xhtml') {
for ($i = 0; $i < $content->childNodes->length; $i++) {
$child = $content->childNodes->item($i);
if ($child->hasChildNodes()) {
- return $this->doc->saveHTML($child);
+ return $this->rewrite_content_to_base($base, $this->doc->saveHTML($child));
}
}
}
}
- return $this->subtree_or_text($content);
+ return $this->rewrite_content_to_base($base, $this->subtree_or_text($content));
}
}
+ // TODO: duplicate code should be merged with get_content()
function get_description() {
$content = $this->elem->getElementsByTagName("summary")->item(0);
if ($content) {
+ $base = $this->xpath->evaluate("string(ancestor-or-self::*[@xml:base][1]/@xml:base)", $content);
+
if ($content->hasAttribute('type')) {
if ($content->getAttribute('type') == 'xhtml') {
for ($i = 0; $i < $content->childNodes->length; $i++) {
$child = $content->childNodes->item($i);
if ($child->hasChildNodes()) {
- return $this->doc->saveHTML($child);
+ return $this->rewrite_content_to_base($base, $this->doc->saveHTML($child));
}
}
}
}
- return $this->subtree_or_text($content);
+ return $this->rewrite_content_to_base($base, $this->subtree_or_text($content));
}
}
@@ -122,16 +155,22 @@ class FeedItem_Atom extends FeedItem_Common {
function get_enclosures() {
$links = $this->elem->getElementsByTagName("link");
- $encs = array();
+ $encs = [];
foreach ($links as $link) {
if ($link && $link->hasAttribute("href") && $link->hasAttribute("rel")) {
+ $base = $this->xpath->evaluate("string(ancestor-or-self::*[@xml:base][1]/@xml:base)", $link);
+
if ($link->getAttribute("rel") == "enclosure") {
$enc = new FeedEnclosure();
$enc->type = clean($link->getAttribute("type"));
- $enc->link = clean($link->getAttribute("href"));
$enc->length = clean($link->getAttribute("length"));
+ $enc->link = clean($link->getAttribute("href"));
+
+ if (!empty($base)) {
+ $enc->link = UrlHelper::rewrite_relative($base, $enc->link);
+ }
array_push($encs, $enc);
}