]> BookStack Code Mirror - bookstack/commitdiff
Merge branch 'create-content-meta-tags' of https://github.com/james-geiger/BookStack...
authorDan Brown <redacted>
Wed, 23 Jun 2021 19:11:07 +0000 (20:11 +0100)
committerDan Brown <redacted>
Wed, 23 Jun 2021 19:11:07 +0000 (20:11 +0100)
1  2 
app/Entities/Models/Page.php
app/Entities/Tools/PageContent.php
resources/views/base.blade.php
resources/views/books/show.blade.php
resources/views/chapters/show.blade.php
resources/views/pages/show.blade.php
resources/views/shelves/show.blade.php

Simple merge
index 381ef172b47105939740a31ae807a7fbff865f10,84506f6718aedc7bbb7e48aead482e47df78beef..d178dc040c075e923bd64889b7954ba4270f8fb6
@@@ -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 = '<body>' . $html . '</body>';
          $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;
+     }
  }
Simple merge
Simple merge
Simple merge
Morty Proxy This is a proxified and sanitized view of the page, visit original site.