]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
Fix warning if the encoding retrieved from external headers is invalid 1569/head
authorArthurHoaro <arthur@hoa.ro>
Sat, 26 Sep 2020 11:28:38 +0000 (13:28 +0200)
committerArthurHoaro <arthur@hoa.ro>
Wed, 30 Sep 2020 09:11:19 +0000 (11:11 +0200)
Also fixed the regex to support this failing header: charset="utf-8"\r\n"

application/bookmark/LinkUtils.php
application/front/controller/admin/ManageShaareController.php
tests/bookmark/LinkUtilsTest.php

index 68914fcab749a19b1ba15193decd99247158375a..e7af4d552b409b77595985cc635273a6a1d8e448 100644 (file)
@@ -26,7 +26,7 @@ function html_extract_title($html)
  */
 function header_extract_charset($header)
 {
-    preg_match('/charset="?([^; ]+)/i', $header, $match);
+    preg_match('/charset=["\']?([^; "\']+)/i', $header, $match);
     if (! empty($match[1])) {
         return strtolower(trim($match[1]));
     }
index ca2da9b53b7edb4e545ba7b337f04138313ab3f3..ffb0dae430ca69ec2929b14a323add0f554d562e 100644 (file)
@@ -69,7 +69,7 @@ class ManageShaareController extends ShaarliAdminController
                         $retrieveDescription
                     )
                 );
-                if (! empty($title) && strtolower($charset) !== 'utf-8') {
+                if (! empty($title) && strtolower($charset) !== 'utf-8' && mb_check_encoding($charset)) {
                     $title = mb_convert_encoding($title, 'utf-8', $charset);
                 }
             }
index 7d4a7b89a20131d9827112ffbd85fb905c1dffd3..0d07897b40ee16913206aab94dcf87dd6b74c3af 100644 (file)
@@ -42,6 +42,19 @@ class LinkUtilsTest extends TestCase
         $this->assertEquals(strtolower($charset), header_extract_charset($headers));
     }
 
+    /**
+     * Test headers_extract_charset() when the charset is found with odd quotes.
+     */
+    public function testHeadersExtractExistentCharsetWithQuotes()
+    {
+        $charset = 'x-MacCroatian';
+        $headers = 'text/html; charset="' . $charset . '"otherstuff="test"';
+        $this->assertEquals(strtolower($charset), header_extract_charset($headers));
+
+        $headers = 'text/html; charset=\'' . $charset . '\'otherstuff="test"';
+        $this->assertEquals(strtolower($charset), header_extract_charset($headers));
+    }
+
     /**
      * Test headers_extract_charset() when the charset is not found.
      */