]> git.mxchange.org Git - friendica.git/commitdiff
Fix exception, when a provided URI is invalid
authorMichael <heluecht@pirati.ca>
Tue, 5 Dec 2023 05:24:33 +0000 (05:24 +0000)
committerMichael <heluecht@pirati.ca>
Tue, 5 Dec 2023 05:24:33 +0000 (05:24 +0000)
src/Content/Post/Factory/PostMedia.php
src/Util/Network.php

index fe71b21973ecad251a2b8ddfdbfa3c48a0a73168..38c5f28ebf64b1cdea7dd5e9eb7d2a4783c20a7d 100644 (file)
@@ -25,6 +25,7 @@ use Friendica\BaseFactory;
 use Friendica\Capabilities\ICanCreateFromTableRow;
 use Friendica\Content\Post\Entity;
 use Friendica\Network;
+use Friendica\Util\Network as UtilNetwork;
 use GuzzleHttp\Psr7\Uri;
 use Psr\Log\LoggerInterface;
 use stdClass;
@@ -48,24 +49,24 @@ class PostMedia extends BaseFactory implements ICanCreateFromTableRow
        {
                return new Entity\PostMedia(
                        $row['uri-id'],
-                       $row['url'] ? new Uri($row['url']) : null,
+                       UtilNetwork::isValidUri($row['url']) ? new Uri($row['url']) : '',
                        $row['type'],
                        $this->mimeTypeFactory->createFromContentType($row['mimetype']),
                        $row['media-uri-id'],
                        $row['width'],
                        $row['height'],
                        $row['size'],
-                       $row['preview'] ? new Uri($row['preview']) : null,
+                       UtilNetwork::isValidUri($row['preview']) ? new Uri($row['preview']) : null,
                        $row['preview-width'],
                        $row['preview-height'],
                        $row['description'],
                        $row['name'],
-                       $row['author-url'] ? new Uri($row['author-url']) : null,
+                       UtilNetwork::isValidUri($row['author-url']) ? new Uri($row['author-url']) : null,
                        $row['author-name'],
-                       $row['author-image'] ? new Uri($row['author-image']) : null,
-                       $row['publisher-url'] ? new Uri($row['publisher-url']) : null,
+                       UtilNetwork::isValidUri($row['author-image']) ? new Uri($row['author-image']) : null,
+                       UtilNetwork::isValidUri($row['publisher-url']) ? new Uri($row['publisher-url']) : null,
                        $row['publisher-name'],
-                       $row['publisher-image'] ? new Uri($row['publisher-image']) : null,
+                       UtilNetwork::isValidUri($row['publisher-image']) ? new Uri($row['publisher-image']) : null,
                        $row['blurhash'],
                        $row['id']
                );
index 495510189f1a2a849f61d69ce9aacbc283750b3d..e18173c72cd6c5afcd39a06b36b1e59a70262096 100644 (file)
@@ -658,4 +658,25 @@ class Network
                $scheme = parse_url($url, PHP_URL_SCHEME);
                return !empty($scheme) && in_array($scheme, ['http', 'https']) && parse_url($url, PHP_URL_HOST);
        }
+
+       /**
+        * Check if a provided URI is valid
+        *
+        * @param string|null $uri
+        * @return boolean
+        */
+       public static function isValidUri(string $uri = null): bool
+       {
+               if (empty($uri)) {
+                       return false;
+               }
+
+               try {
+                       new Uri($uri);
+               } catch (\Exception $e) {
+                       Logger::debug('Invalid URI', ['code' => $e->getCode(), 'message' => $e->getMessage(), 'uri' => $uri]);
+                       return false;
+               }
+               return true;
+       }
 }