]> git.mxchange.org Git - friendica.git/blobdiff - src/Content/Text/HTML.php
Replace deprecated use of "self" in callables
[friendica.git] / src / Content / Text / HTML.php
index 00d609cb609665c30e7f141643d58982805e1a33..f9f340135cf51cf22cab2bf62c941166305b2f4c 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2022, the Friendica project
+ * @copyright Copyright (C) 2010-2023, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -23,6 +23,7 @@ namespace Friendica\Content\Text;
 
 use DOMDocument;
 use DOMXPath;
+use Friendica\Protocol\HTTP\MediaType;
 use Friendica\Content\Widget\ContactBlock;
 use Friendica\Core\Hook;
 use Friendica\Core\Renderer;
@@ -1031,7 +1032,7 @@ class HTML
                // the quotes, e.g.:
                //
                //    concat("'foo'", '"', "bar")
-               return 'concat(' . implode(', \'"\', ', array_map(['self', 'xpathQuote'], explode('"', $value))) . ')';
+               return 'concat(' . implode(', \'"\', ', array_map([self::class, 'xpathQuote'], explode('"', $value))) . ')';
        }
 
        /**
@@ -1055,4 +1056,30 @@ class HTML
 
                return $result !== false && $result->length > 0;
        }
+
+       /**
+        * @param DOMDocument $doc
+        * @return string|null Lowercase charset
+        */
+       public static function extractCharset(DOMDocument $doc): ?string
+       {
+               $xpath = new DOMXPath($doc);
+
+               $expression = "string(//meta[@charset]/@charset)";
+               if ($charset = $xpath->evaluate($expression)) {
+                       return strtolower($charset);
+               }
+
+               try {
+                       // This expression looks for a meta tag with the http-equiv attribute set to "content-type" ignoring case
+                       // whose content attribute contains a "charset" string and returns its value
+                       $expression = "string(//meta[@http-equiv][translate(@http-equiv, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'content-type'][contains(translate(@content, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'charset')]/@content)";
+                       $mediaType = MediaType::fromContentType($xpath->evaluate($expression));
+                       if (isset($mediaType->parameters['charset'])) {
+                               return strtolower($mediaType->parameters['charset']);
+                       }
+               } catch(\InvalidArgumentException $e) {}
+
+               return null;
+       }
 }