From 7ef78b300a88b554e7681b4861216abdfe69f64f Mon Sep 17 00:00:00 2001 From: joshuaruesweg Date: Fri, 12 Mar 2021 11:06:03 +0100 Subject: [PATCH] Use XPath to determine values --- .../message/unfurl/UnfurlResponse.class.php | 60 ++++++++----------- 1 file changed, 26 insertions(+), 34 deletions(-) diff --git a/wcfsetup/install/files/lib/system/message/unfurl/UnfurlResponse.class.php b/wcfsetup/install/files/lib/system/message/unfurl/UnfurlResponse.class.php index 6f0f7b9ff9..c12ef57c7f 100644 --- a/wcfsetup/install/files/lib/system/message/unfurl/UnfurlResponse.class.php +++ b/wcfsetup/install/files/lib/system/message/unfurl/UnfurlResponse.class.php @@ -64,6 +64,11 @@ final class UnfurlResponse */ private $domDocument; + /** + * @var \DomXPath + */ + private $domXPath; + /** * Fetches a given Url and returns an UnfurlResponse instance. * @@ -189,6 +194,8 @@ final class UnfurlResponse if (!$this->domDocument->loadHTML('' . $this->body)) { throw new ParsingFailed("DOMDocument::loadHTML() failed"); } + + $this->domXPath = new \DOMXPath($this->domDocument); } finally { \libxml_use_internal_errors($useInternalErrors); \libxml_clear_errors(); @@ -201,25 +208,20 @@ final class UnfurlResponse public function getTitle(): ?string { if (!empty($this->body)) { - $metaTags = $this->domDocument->getElementsByTagName('meta'); - - // og - foreach ($metaTags as $metaTag) { - foreach ($metaTag->attributes as $attr) { - if ($attr->nodeName == 'property' && $attr->value == 'og:title') { - foreach ($attr->parentNode->attributes as $attr) { - if ($attr->nodeName == 'content') { - return $attr->value; - } - } + // og:title + $list = $this->domXPath->query("//meta[@property='og:title']"); + foreach ($list as $node) { + foreach ($node->attributes as $attribute) { + if ($attribute->nodeName === 'content') { + return $attribute->nodeValue; } } } // title tag - $title = $this->domDocument->getElementsByTagName('title'); - if ($title->length) { - return $title->item(0)->nodeValue; + $list = $this->domXPath->query("//title"); + foreach ($list as $node) { + return $node->nodeValue; } } @@ -232,17 +234,12 @@ final class UnfurlResponse public function getDescription(): ?string { if (!empty($this->body)) { - $metaTags = $this->domDocument->getElementsByTagName('meta'); - // og:description - foreach ($metaTags as $metaTag) { - foreach ($metaTag->attributes as $attr) { - if ($attr->nodeName == 'property' && $attr->value == 'og:description') { - foreach ($attr->parentNode->attributes as $attr) { - if ($attr->nodeName == 'content') { - return $attr->value; - } - } + $list = $this->domXPath->query("//meta[@property='og:description']"); + foreach ($list as $node) { + foreach ($node->attributes as $attribute) { + if ($attribute->nodeName === 'content') { + return $attribute->nodeValue; } } } @@ -257,17 +254,12 @@ final class UnfurlResponse public function getImageUrl(): ?string { if (!empty($this->body)) { - $metaTags = $this->domDocument->getElementsByTagName('meta'); - // og:image - foreach ($metaTags as $metaTag) { - foreach ($metaTag->attributes as $attr) { - if ($attr->nodeName == 'property' && ($attr->value == 'og:image' || $attr->value == 'og:image:url')) { - foreach ($attr->parentNode->attributes as $attr) { - if ($attr->nodeName == 'content') { - return $attr->value; - } - } + $list = $this->domXPath->query("//meta[@property='og:image']"); + foreach ($list as $node) { + foreach ($node->attributes as $attribute) { + if ($attribute->nodeName === 'content') { + return $attribute->nodeValue; } } } -- 2.20.1