2013-05-01 13:04:57 +00:00
|
|
|
<?php
|
2013-05-01 17:06:48 +00:00
|
|
|
class FeedItem_Atom extends FeedItem_Common {
|
2018-08-12 15:17:13 +00:00
|
|
|
const NS_XML = "http://www.w3.org/XML/1998/namespace";
|
2013-07-03 19:36:33 +00:00
|
|
|
|
2013-05-01 13:04:57 +00:00
|
|
|
function get_id() {
|
|
|
|
$id = $this->elem->getElementsByTagName("id")->item(0);
|
|
|
|
|
|
|
|
if ($id) {
|
|
|
|
return $id->nodeValue;
|
|
|
|
} else {
|
2018-12-26 07:16:11 +00:00
|
|
|
return clean($this->get_link());
|
2013-05-01 13:04:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_date() {
|
2013-05-01 13:38:16 +00:00
|
|
|
$updated = $this->elem->getElementsByTagName("updated")->item(0);
|
2013-05-01 13:04:57 +00:00
|
|
|
|
2013-05-01 13:38:16 +00:00
|
|
|
if ($updated) {
|
|
|
|
return strtotime($updated->nodeValue);
|
|
|
|
}
|
2013-06-01 05:49:56 +00:00
|
|
|
|
2013-06-10 09:25:34 +00:00
|
|
|
$published = $this->elem->getElementsByTagName("published")->item(0);
|
|
|
|
|
|
|
|
if ($published) {
|
|
|
|
return strtotime($published->nodeValue);
|
|
|
|
}
|
|
|
|
|
2013-06-01 05:49:56 +00:00
|
|
|
$date = $this->xpath->query("dc:date", $this->elem)->item(0);
|
|
|
|
|
|
|
|
if ($date) {
|
|
|
|
return strtotime($date->nodeValue);
|
|
|
|
}
|
2013-05-01 13:04:57 +00:00
|
|
|
}
|
2013-07-11 10:21:29 +00:00
|
|
|
|
|
|
|
|
2013-05-01 13:04:57 +00:00
|
|
|
function get_link() {
|
|
|
|
$links = $this->elem->getElementsByTagName("link");
|
|
|
|
|
|
|
|
foreach ($links as $link) {
|
2013-05-26 06:21:54 +00:00
|
|
|
if ($link && $link->hasAttribute("href") &&
|
|
|
|
(!$link->hasAttribute("rel")
|
|
|
|
|| $link->getAttribute("rel") == "alternate"
|
|
|
|
|| $link->getAttribute("rel") == "standout")) {
|
2013-07-11 10:21:29 +00:00
|
|
|
$base = $this->xpath->evaluate("string(ancestor-or-self::*[@xml:base][1]/@xml:base)", $link);
|
|
|
|
|
2013-07-29 12:16:47 +00:00
|
|
|
if ($base)
|
2018-12-26 07:16:11 +00:00
|
|
|
return rewrite_relative_url($base, clean(trim($link->getAttribute("href"))));
|
2013-07-29 12:16:47 +00:00
|
|
|
else
|
2018-12-26 07:16:11 +00:00
|
|
|
return clean(trim($link->getAttribute("href")));
|
2013-07-29 12:16:47 +00:00
|
|
|
|
2013-05-01 13:04:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_title() {
|
|
|
|
$title = $this->elem->getElementsByTagName("title")->item(0);
|
|
|
|
|
|
|
|
if ($title) {
|
2018-12-26 07:16:11 +00:00
|
|
|
return clean(trim($title->nodeValue));
|
2013-05-01 13:04:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-21 12:39:41 +00:00
|
|
|
/** $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;
|
|
|
|
}
|
|
|
|
|
2013-05-01 13:04:57 +00:00
|
|
|
function get_content() {
|
|
|
|
$content = $this->elem->getElementsByTagName("content")->item(0);
|
|
|
|
|
|
|
|
if ($content) {
|
2021-05-21 12:39:41 +00:00
|
|
|
$base = $this->xpath->evaluate("string(ancestor-or-self::*[@xml:base][1]/@xml:base)", $content);
|
|
|
|
|
2013-05-16 05:19:56 +00:00
|
|
|
if ($content->hasAttribute('type')) {
|
|
|
|
if ($content->getAttribute('type') == 'xhtml') {
|
2013-06-20 17:16:06 +00:00
|
|
|
for ($i = 0; $i < $content->childNodes->length; $i++) {
|
|
|
|
$child = $content->childNodes->item($i);
|
|
|
|
|
|
|
|
if ($child->hasChildNodes()) {
|
2021-05-21 12:39:41 +00:00
|
|
|
return $this->rewrite_content_to_base($base, $this->doc->saveHTML($child));
|
2013-06-20 17:16:06 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-15 19:06:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-21 12:39:41 +00:00
|
|
|
return $this->rewrite_content_to_base($base, $this->subtree_or_text($content));
|
2013-05-01 13:04:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-21 12:39:41 +00:00
|
|
|
// TODO: duplicate code should be merged with get_content()
|
2013-05-01 13:04:57 +00:00
|
|
|
function get_description() {
|
2013-05-16 05:19:56 +00:00
|
|
|
$content = $this->elem->getElementsByTagName("summary")->item(0);
|
|
|
|
|
|
|
|
if ($content) {
|
2021-05-21 12:39:41 +00:00
|
|
|
$base = $this->xpath->evaluate("string(ancestor-or-self::*[@xml:base][1]/@xml:base)", $content);
|
|
|
|
|
2013-05-16 05:19:56 +00:00
|
|
|
if ($content->hasAttribute('type')) {
|
|
|
|
if ($content->getAttribute('type') == 'xhtml') {
|
2013-06-20 17:16:06 +00:00
|
|
|
for ($i = 0; $i < $content->childNodes->length; $i++) {
|
|
|
|
$child = $content->childNodes->item($i);
|
|
|
|
|
|
|
|
if ($child->hasChildNodes()) {
|
2021-05-21 12:39:41 +00:00
|
|
|
return $this->rewrite_content_to_base($base, $this->doc->saveHTML($child));
|
2013-06-20 17:16:06 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-16 05:19:56 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-01 13:04:57 +00:00
|
|
|
|
2021-05-21 12:39:41 +00:00
|
|
|
return $this->rewrite_content_to_base($base, $this->subtree_or_text($content));
|
2013-05-01 13:04:57 +00:00
|
|
|
}
|
2013-05-16 05:19:56 +00:00
|
|
|
|
2013-05-01 13:04:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function get_categories() {
|
|
|
|
$categories = $this->elem->getElementsByTagName("category");
|
2019-11-20 15:56:34 +00:00
|
|
|
$cats = [];
|
2013-05-01 13:04:57 +00:00
|
|
|
|
|
|
|
foreach ($categories as $cat) {
|
|
|
|
if ($cat->hasAttribute("term"))
|
2019-11-20 15:56:34 +00:00
|
|
|
array_push($cats, $cat->getAttribute("term"));
|
2013-05-01 13:04:57 +00:00
|
|
|
}
|
|
|
|
|
2013-05-01 16:55:08 +00:00
|
|
|
$categories = $this->xpath->query("dc:subject", $this->elem);
|
|
|
|
|
|
|
|
foreach ($categories as $cat) {
|
2019-11-20 15:56:34 +00:00
|
|
|
array_push($cats, $cat->nodeValue);
|
2013-05-01 16:55:08 +00:00
|
|
|
}
|
2013-05-01 13:04:57 +00:00
|
|
|
|
2019-11-20 15:56:34 +00:00
|
|
|
return $this->normalize_categories($cats);
|
2013-05-01 13:04:57 +00:00
|
|
|
}
|
|
|
|
|
2021-03-05 06:35:17 +00:00
|
|
|
function get_enclosures() {
|
2013-05-01 13:04:57 +00:00
|
|
|
$links = $this->elem->getElementsByTagName("link");
|
|
|
|
|
2021-05-21 12:39:41 +00:00
|
|
|
$encs = [];
|
2013-05-01 13:04:57 +00:00
|
|
|
|
|
|
|
foreach ($links as $link) {
|
|
|
|
if ($link && $link->hasAttribute("href") && $link->hasAttribute("rel")) {
|
2021-05-21 12:39:41 +00:00
|
|
|
$base = $this->xpath->evaluate("string(ancestor-or-self::*[@xml:base][1]/@xml:base)", $link);
|
|
|
|
|
2013-05-01 13:04:57 +00:00
|
|
|
if ($link->getAttribute("rel") == "enclosure") {
|
|
|
|
$enc = new FeedEnclosure();
|
|
|
|
|
2018-12-26 07:16:11 +00:00
|
|
|
$enc->type = clean($link->getAttribute("type"));
|
|
|
|
$enc->length = clean($link->getAttribute("length"));
|
2021-05-21 12:39:41 +00:00
|
|
|
$enc->link = clean($link->getAttribute("href"));
|
|
|
|
|
|
|
|
if (!empty($base)) {
|
|
|
|
$enc->link = UrlHelper::rewrite_relative($base, $enc->link);
|
|
|
|
}
|
2013-05-01 13:04:57 +00:00
|
|
|
|
|
|
|
array_push($encs, $enc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-05 06:35:17 +00:00
|
|
|
$encs = array_merge($encs, parent::get_enclosures());
|
2013-12-15 08:35:30 +00:00
|
|
|
|
2013-05-01 13:04:57 +00:00
|
|
|
return $encs;
|
|
|
|
}
|
|
|
|
|
2018-08-12 13:54:13 +00:00
|
|
|
function get_language() {
|
2018-08-12 15:17:13 +00:00
|
|
|
$lang = $this->elem->getAttributeNS(self::NS_XML, "lang");
|
2018-08-12 13:54:13 +00:00
|
|
|
|
2018-08-12 15:17:13 +00:00
|
|
|
if (!empty($lang)) {
|
2018-12-26 07:16:11 +00:00
|
|
|
return clean($lang);
|
2018-08-12 15:17:13 +00:00
|
|
|
} else {
|
|
|
|
// Fall back to the language declared on the feed, if any.
|
2018-08-12 16:27:04 +00:00
|
|
|
foreach ($this->doc->childNodes as $child) {
|
|
|
|
if (method_exists($child, "getAttributeNS")) {
|
2018-12-26 07:16:11 +00:00
|
|
|
return clean($child->getAttributeNS(self::NS_XML, "lang"));
|
2018-08-12 16:27:04 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-12 15:17:13 +00:00
|
|
|
}
|
2018-08-12 13:54:13 +00:00
|
|
|
}
|
2017-09-09 18:51:59 +00:00
|
|
|
}
|