]> git.mxchange.org Git - friendica.git/blobdiff - src/Util/ParseUrl.php
Update src/App/Page.php
[friendica.git] / src / Util / ParseUrl.php
index 77611168d4fed8c2d7708762787d6238b0029c59..6c3c4a0259e80873e1130d0b243531097f9fa3c1 100644 (file)
@@ -30,6 +30,7 @@ use Friendica\Database\Database;
 use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Network\HTTPException;
+use Friendica\Network\HTTPClient\Client\HttpClientOptions;
 
 /**
  * Get information about a given URL
@@ -58,12 +59,12 @@ class ParseUrl
         */
        public static function getContentType(string $url)
        {
-               $curlResult = DI::httpRequest()->head($url);
+               $curlResult = DI::httpClient()->head($url);
                if (!$curlResult->isSuccess()) {
                        return [];
                }
 
-               $contenttype =  $curlResult->getHeader('Content-Type');
+               $contenttype =  $curlResult->getHeader('Content-Type')[0] ?? '';
                if (empty($contenttype)) {
                        return [];
                }
@@ -213,26 +214,20 @@ class ParseUrl
                        return $siteinfo;
                }
 
-               $curlResult = DI::httpRequest()->get($url);
+               $curlResult = DI::httpClient()->get($url, [HttpClientOptions::CONTENT_LENGTH => 1000000]);
                if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
                        return $siteinfo;
                }
 
                $siteinfo['expires'] = DateTimeFormat::utc(self::DEFAULT_EXPIRATION_SUCCESS);
 
-               // If the file is too large then exit
-               if (($curlResult->getInfo()['download_content_length'] ?? 0) > 1000000) {
-                       return $siteinfo;
-               }
-
-               if ($cacheControlHeader = $curlResult->getHeader('Cache-Control')) {
+               if ($cacheControlHeader = $curlResult->getHeader('Cache-Control')[0] ?? '') {
                        if (preg_match('/max-age=([0-9]+)/i', $cacheControlHeader, $matches)) {
                                $maxAge = max(86400, (int)array_pop($matches));
                                $siteinfo['expires'] = DateTimeFormat::utc("now + $maxAge seconds");
                        }
                }
 
-               $header = $curlResult->getHeader();
                $body = $curlResult->getBody();
 
                if ($do_oembed) {
@@ -273,7 +268,7 @@ 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', $header, $matches)) {
+               if (preg_match('/charset=([a-z0-9-_.\/]+)/i', $curlResult->getContentType(), $matches)) {
                        $charset = trim(trim(trim(array_pop($matches)), ';,'));
                }
 
@@ -695,7 +690,7 @@ class ParseUrl
        {
                if (!empty($jsonld['@graph']) && is_array($jsonld['@graph'])) {
                        foreach ($jsonld['@graph'] as $part) {
-                               if (!empty($part)) {
+                               if (!empty($part) && is_array($part)) {
                                        $siteinfo = self::parseParts($siteinfo, $part);
                                }
                        }
@@ -711,7 +706,7 @@ class ParseUrl
                        }
                        if ($numeric_keys) {
                                foreach ($jsonld as $part) {
-                                       if (!empty($part)) {
+                                       if (!empty($part) && is_array($part)) {
                                                $siteinfo = self::parseParts($siteinfo, $part);
                                        }
                                }
@@ -1121,7 +1116,11 @@ class ParseUrl
                }
 
                $content = JsonLD::fetchElement($jsonld, 'image', 'url', '@type', 'ImageObject');
-               if (!empty($content)) {
+               if (!empty($content) && !is_string($content)) {
+                       Logger::notice('Unexpected return value for the author image', ['content' => $content]);
+               }
+
+               if (!empty($content) && is_string($content)) {
                        $jsonldinfo['author_img'] = trim($content);
                }