Replaces img-element in the message with the alt attribute value
authorCyperghost <olaf_schmitz_1@t-online.de>
Thu, 15 Aug 2024 08:15:40 +0000 (10:15 +0200)
committerCyperghost <olaf_schmitz_1@t-online.de>
Thu, 15 Aug 2024 08:15:40 +0000 (10:15 +0200)
wcfsetup/install/files/lib/system/background/job/ServiceWorkerDeliveryBackgroundJob.class.php

index 25863ec13ecfb48e602689286885d108c55e86f3..8be21b399fee677d79d915604a5ea93afcfd5e10 100644 (file)
@@ -98,7 +98,7 @@ final class ServiceWorkerDeliveryBackgroundJob extends AbstractUniqueBackgroundJ
 
             $content = [
                 "title" => $event->getTitle(),
-                "message" => StringUtil::stripHTML($event->getMessage()),
+                "message" => StringUtil::stripHTML($this->replaceImagesWithAltText($event->getMessage())),
                 "url" => $event->getLink(),
                 "notificationID" => $notification->notificationID,
                 "time" => $notification->time,
@@ -124,4 +124,23 @@ final class ServiceWorkerDeliveryBackgroundJob extends AbstractUniqueBackgroundJ
 
         return $statement->fetchSingleColumn() > 0;
     }
+
+    private function replaceImagesWithAltText(string $message): string
+    {
+        $document = new \DOMDocument('1.0', 'UTF-8');
+
+        $useInternalErrors = \libxml_use_internal_errors(true);
+        $document->loadHTML(
+            '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body>' . $message . '</body></html>'
+        );
+        \libxml_clear_errors();
+        \libxml_use_internal_errors($useInternalErrors);
+
+        foreach ($document->getElementsByTagName('img') as $image) {
+            \assert($image instanceof \DOMElement);
+            $image->replaceWith($image->getAttribute('alt'));
+        }
+
+        return $document->textContent;
+    }
 }