From: Dan Brown Date: Wed, 23 Jun 2021 19:11:07 +0000 (+0100) Subject: Merge branch 'create-content-meta-tags' of https://github.com/james-geiger/BookStack... X-Git-Tag: v21.08~1^2~35 X-Git-Url: http://source.bookstackapp.com/bookstack/commitdiff_plain/58fa7679bccafd00f9a50bcd4a87e96876331b03 Merge branch 'create-content-meta-tags' of https://github.com/james-geiger/BookStack into james-geiger-create-content-meta-tags --- 58fa7679bccafd00f9a50bcd4a87e96876331b03 diff --cc app/Entities/Tools/PageContent.php index 381ef172b,84506f671..d178dc040 --- a/app/Entities/Tools/PageContent.php +++ b/app/Entities/Tools/PageContent.php @@@ -357,14 -299,77 +357,28 @@@ class PageConten } /** - * Escape script tags within HTML content. + * Create and load a DOMDocument from the given html content. */ - protected function escapeScripts(string $html) : string + protected function loadDocumentFromHtml(string $html): DOMDocument { - if (empty($html)) { - return $html; - } - libxml_use_internal_errors(true); $doc = new DOMDocument(); + $html = '' . $html . ''; $doc->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8')); - $xPath = new DOMXPath($doc); - - // Remove standard script tags - $scriptElems = $xPath->query('//script'); - foreach ($scriptElems as $scriptElem) { - $scriptElem->parentNode->removeChild($scriptElem); - } - - // Remove clickable links to JavaScript URI - $badLinks = $xPath->query('//*[contains(@href, \'javascript:\')]'); - foreach ($badLinks as $badLink) { - $badLink->parentNode->removeChild($badLink); - } - - // Remove forms with calls to JavaScript URI - $badForms = $xPath->query('//*[contains(@action, \'javascript:\')] | //*[contains(@formaction, \'javascript:\')]'); - foreach ($badForms as $badForm) { - $badForm->parentNode->removeChild($badForm); - } - - // Remove meta tag to prevent external redirects - $metaTags = $xPath->query('//meta[contains(@content, \'url\')]'); - foreach ($metaTags as $metaTag) { - $metaTag->parentNode->removeChild($metaTag); - } - - // Remove data or JavaScript iFrames - $badIframes = $xPath->query('//*[contains(@src, \'data:\')] | //*[contains(@src, \'javascript:\')] | //*[@srcdoc]'); - foreach ($badIframes as $badIframe) { - $badIframe->parentNode->removeChild($badIframe); - } - - // Remove 'on*' attributes - $onAttributes = $xPath->query('//@*[starts-with(name(), \'on\')]'); - foreach ($onAttributes as $attr) { - /** @var \DOMAttr $attr*/ - $attrName = $attr->nodeName; - $attr->parentNode->removeAttribute($attrName); - } - - $html = ''; - $topElems = $doc->documentElement->childNodes->item(0)->childNodes; - foreach ($topElems as $child) { - $html .= $doc->saveHTML($child); - } - - return $html; + return $doc; } + + /** + * Retrieve first image in page content and return the source URL. + */ + public function fetchFirstImage() + { + $htmlContent = $this->page->html; + + $dom = new \DomDocument(); + $dom->loadHTML($htmlContent); + $images = $dom->getElementsByTagName('img'); + + return $images->length > 0 ? $images[0]->getAttribute('src') : null; + } }