]> git.mxchange.org Git - friendica.git/commitdiff
Revert "IHTTPResult::getContentType is now a string again"
authorPhilipp <admin@philipp.info>
Sun, 11 Oct 2020 21:25:29 +0000 (23:25 +0200)
committerPhilipp <admin@philipp.info>
Sun, 11 Oct 2020 21:25:29 +0000 (23:25 +0200)
This reverts commit 40b11442

src/Model/Photo.php
src/Model/User.php
src/Network/CurlResult.php
src/Network/GuzzleResponse.php
src/Network/IHTTPResult.php
src/Network/Probe.php
src/Util/Images.php
src/Util/ParseUrl.php
tests/src/Network/CurlResultTest.php

index 5f8551fa2708a70c7c8338df36b81bb578988708..dc60f25f3726abf6fc277841f17df1ce56955144 100644 (file)
@@ -427,7 +427,7 @@ class Photo
                        $contType = $ret->getContentType();
                } else {
                        $img_str = '';
-                       $contType = '';
+                       $contType = [];
                }
 
                if ($quit_on_error && ($img_str == "")) {
index e6f20f738732d32524ced00a71f615224a5cc1a5..48d9348961f9d7a8b59e453e707636dd9eb9087d 100644 (file)
@@ -1008,7 +1008,7 @@ class User
                                $contType = $curlResult->getContentType();
                        } else {
                                $img_str = '';
-                               $contType = '';
+                               $contType = [];
                        }
 
                        $type = Images::getMimeTypeByData($img_str, $photo, $contType);
index 4ecdd68d10165a2293bf8372776378ebe4dfe5e4..ca1ead91d15305798b3d06c2b8799bdb47526164 100644 (file)
@@ -37,7 +37,7 @@ class CurlResult implements IHTTPResult
        private $returnCode;
 
        /**
-        * @var string the content type of the Curl call
+        * @var string[] the content type of the Curl call
         */
        private $contentType;
 
@@ -223,9 +223,9 @@ class CurlResult implements IHTTPResult
        private function checkInfo()
        {
                if (isset($this->info['content_type'])) {
-                       $this->contentType = $this->info['content_type'];
+                       $this->contentType = [$this->info['content_type']];
                } else {
-                       $this->contentType = '';
+                       $this->contentType = [];
                }
        }
 
index 72d87ae9fe94d1ec8145391fd0322efe999d0639..69e88b40250c3568cb3c4023e42c700cb00f9500 100644 (file)
@@ -88,7 +88,7 @@ class GuzzleResponse extends Response implements IHTTPResult, ResponseInterface
        /** {@inheritDoc} */
        public function getContentType()
        {
-               return implode($this->getHeader('Content-Type'));
+               return $this->getHeader('Content-Type');
        }
 
        /** {@inheritDoc} */
index 38a1176284368d05c93510a773de597f8c8c7d73..acee2dde983b6437754c0a044a5373d4e005a482 100644 (file)
@@ -19,7 +19,7 @@ interface IHTTPResult
        /**
         * Returns the Content Type
         *
-        * @return string the Content Type
+        * @return string[] the Content Types
         */
        public function getContentType();
 
index cfd03684397c9afb023dfdd55c139696c65310a9..14dcdea336e9f2e0ee985bcf6c3a22d212610c5b 100644 (file)
@@ -429,7 +429,7 @@ class Probe
                }
 
                // If it isn't a HTML file then exit
-               if (($curlResult->getContentType() != '') && !strstr(strtolower($curlResult->getContentType()), 'html')) {
+               if (!in_array('html', $curlResult->getContentType())) {
                        return false;
                }
 
index 1b52b91a133e62b359cc3cecd0e8ddcde2034565..f0aefb9f2c298822a315555e2d4b76a6d8fddbba 100644 (file)
@@ -75,23 +75,25 @@ class Images
        /**
         * Fetch image mimetype from the image data or guessing from the file name
         *
-        * @param string $image_data Image data
-        * @param string $filename   File name (for guessing the type via the extension)
-        * @param string $mimeType   possible mime type
+        * @param string   $image_data Image data
+        * @param string   $filename   File name (for guessing the type via the extension)
+        * @param string[] $mimeTypes  possible mime types
         *
         * @return string
         * @throws \Exception
         */
-       public static function getMimeTypeByData(string $image_data, string $filename = '', string $mimeType = '')
+       public static function getMimeTypeByData(string $image_data, string $filename = '', array $mimeTypes = [])
        {
-               if (substr($mimeType, 0, 6) == 'image/') {
-                       Logger::info('Using default mime type', ['filename' => $filename, 'mime' => $mimeType]);
-                       return $mimeType;
+               foreach ($mimeTypes as $mimeType) {
+                       if (substr($mimeType, 0, 6) == 'image/') {
+                               Logger::info('Using default mime type', ['filename' => $filename, 'mime' => $mimeTypes]);
+                               return $mimeType;
+                       }
                }
 
                $image = @getimagesizefromstring($image_data);
                if (!empty($image['mime'])) {
-                       Logger::info('Mime type detected via data', ['filename' => $filename, 'default' => $mimeType, 'mime' => $image['mime']]);
+                       Logger::info('Mime type detected via data', ['filename' => $filename, 'default' => $mimeTypes, 'mime' => $image['mime']]);
                        return $image['mime'];
                }
 
index 1a81a256ca36ae5a82a92c5b72f5ce334a25c28f..ee2bedd1ee7081c6a37272dc6e2302be6d2b9e9a 100644 (file)
@@ -166,7 +166,7 @@ class ParseUrl
                }
 
                // If it isn't a HTML file then exit
-               if (($curlResult->getContentType() != '') && !strstr(strtolower($curlResult->getContentType()), 'html')) {
+               if (!in_array('html', $curlResult->getContentType())) {
                        return $siteinfo;
                }
 
@@ -198,8 +198,10 @@ class ParseUrl
                $charset = '';
                // Look for a charset, first in headers
                // Expected form: Content-Type: text/html; charset=ISO-8859-4
-               if (preg_match('/charset=([a-z0-9-_.\/]+)/i', $curlResult->getContentType(), $matches)) {
-                       $charset = trim(trim(trim(array_pop($matches)), ';,'));
+               foreach ($curlResult->getContentType() as $type) {
+                       if (preg_match('/charset=([a-z0-9-_.\/]+)/i', $type, $matches)) {
+                               $charset = trim(trim(trim(array_pop($matches)), ';,'));
+                       }
                }
 
                // Then in body that gets precedence
index 03a5288124bb3a77707e5f129db23c1133e59e27..4bc066b800d3938c8009a506c6dadbb3db3ece43 100644 (file)
@@ -68,7 +68,7 @@ class CurlResultTest extends TestCase
                $this->assertFalse($curlResult->isRedirectUrl());
                $this->assertSame($headerArray, $curlResult->getHeaders());
                $this->assertSame($body, $curlResult->getBody());
-               $this->assertSame('text/html; charset=utf-8', $curlResult->getContentType());
+               $this->assertSame(['text/html; charset=utf-8'], $curlResult->getContentType());
                $this->assertSame('https://test.local', $curlResult->getUrl());
                $this->assertSame('https://test.local', $curlResult->getRedirectUrl());
        }
@@ -97,7 +97,7 @@ class CurlResultTest extends TestCase
                $this->assertTrue($curlResult->isRedirectUrl());
                $this->assertSame($headerArray, $curlResult->getHeaders());
                $this->assertSame($body, $curlResult->getBody());
-               $this->assertSame('text/html; charset=utf-8', $curlResult->getContentType());
+               $this->assertSame(['text/html; charset=utf-8'], $curlResult->getContentType());
                $this->assertSame('https://test.local/test/it', $curlResult->getUrl());
                $this->assertSame('https://test.other/test/it', $curlResult->getRedirectUrl());
        }
@@ -124,7 +124,7 @@ class CurlResultTest extends TestCase
                $this->assertFalse($curlResult->isRedirectUrl());
                $this->assertSame($headerArray, $curlResult->getHeaders());
                $this->assertSame($body, $curlResult->getBody());
-               $this->assertSame('text/html; charset=utf-8', $curlResult->getContentType());
+               $this->assertSame(['text/html; charset=utf-8'], $curlResult->getContentType());
                $this->assertSame('https://test.local/test/it', $curlResult->getRedirectUrl());
                $this->assertSame('Tested error', $curlResult->getError());
        }
@@ -152,7 +152,7 @@ class CurlResultTest extends TestCase
                $this->assertTrue($curlResult->isRedirectUrl());
                $this->assertSame($headerArray, $curlResult->getHeaders());
                $this->assertSame($body, $curlResult->getBody());
-               $this->assertSame('text/html; charset=utf-8', $curlResult->getContentType());
+               $this->assertSame(['text/html; charset=utf-8'], $curlResult->getContentType());
                $this->assertSame('https://test.local/test/it?key=value', $curlResult->getUrl());
                $this->assertSame('https://test.other/some/?key=value', $curlResult->getRedirectUrl());
        }