]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Post/Link.php
Use the owner, not the author
[friendica.git] / src / Model / Post / Link.php
index 50ed12a36999250d3ba20af6655f5d1dfca0f11a..3fafdd7eaf7b93f04e175b7124600be812dcec9f 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2021, the Friendica project
+ * @copyright Copyright (C) 2010-2023, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -26,8 +26,12 @@ use Friendica\Core\System;
 use Friendica\Database\Database;
 use Friendica\Database\DBA;
 use Friendica\DI;
+use Friendica\Network\HTTPClient\Client\HttpClientAccept;
 use Friendica\Network\HTTPClient\Client\HttpClientOptions;
+use Friendica\Util\HTTPSignature;
+use Friendica\Util\Images;
 use Friendica\Util\Proxy;
+use Friendica\Object\Image;
 
 /**
  * Class Link
@@ -39,36 +43,46 @@ class Link
        /**
         * Check if the link is stored
         *
-        * @param int $uri_id
-        * @param string $url
-        * @return bool
+        * @param int $uriId
+        * @param string $url URL
+        * @return bool Whether record has been found
         */
-       public static function exists(int $uri_id, string $url)
+       public static function exists(int $uriId, string $url): bool
        {
-               return DBA::exists('post-link', ['uri-id' => $uri_id, 'url' => $url]);
+               return DBA::exists('post-link', ['uri-id' => $uriId, 'url' => $url]);
        }
 
-       public static function getByLink(int $uri_id, string $url, $size = '')
+       /**
+        * Returns URL by URI id and other URL
+        *
+        * @param int $uriId
+        * @param string $url
+        * @param string $size
+        * @return string Found link URL + id on success, $url on failure
+        */
+       public static function getByLink(int $uriId, string $url, string $size = ''): string
        {
-               if (empty($uri_id) || empty($url) || Proxy::isLocalImage($url)) {
+               if (empty($uriId) || empty($url) || Proxy::isLocalImage($url)) {
                        return $url;
                }
 
                if (!in_array(parse_url($url, PHP_URL_SCHEME), ['http', 'https'])) {
-                       Logger::info('Bad URL, quitting', ['uri-id' => $uri_id, 'url' => $url, 'callstack' => System::callstack(20)]);
+                       Logger::info('Bad URL, quitting', ['uri-id' => $uriId, 'url' => $url, 'callstack' => System::callstack(20)]);
                        return $url;
                }
 
-               $link = DBA::selectFirst('post-link', ['id'], ['uri-id' => $uri_id, 'url' => $url]);
+               $link = DBA::selectFirst('post-link', ['id'], ['uri-id' => $uriId, 'url' => $url]);
                if (!empty($link['id'])) {
                        $id = $link['id'];
-                       Logger::info('Found', ['id' => $id, 'uri-id' => $uri_id, 'url' => $url]);
+                       Logger::info('Found', ['id' => $id, 'uri-id' => $uriId, 'url' => $url]);
                } else {
-                       $mime = self::fetchMimeType($url);
+                       $fields = self::fetchMimeType($url);
+                       $fields['uri-id'] = $uriId;
+                       $fields['url'] = $url;
 
-                       DBA::insert('post-link', ['uri-id' => $uri_id, 'url' => $url, 'mimetype' => $mime], Database::INSERT_IGNORE);
+                       DBA::insert('post-link', $fields, Database::INSERT_IGNORE);
                        $id = DBA::lastInsertId();
-                       Logger::info('Inserted', ['id' => $id, 'uri-id' => $uri_id, 'url' => $url]);
+                       Logger::info('Inserted', $fields);
                }
 
                if (empty($id)) {
@@ -80,15 +94,19 @@ class Link
                        case Proxy::SIZE_MICRO:
                                $url .= Proxy::PIXEL_MICRO . '/';
                                break;
+
                        case Proxy::SIZE_THUMB:
                                $url .= Proxy::PIXEL_THUMB . '/';
                                break;
+
                        case Proxy::SIZE_SMALL:
                                $url .= Proxy::PIXEL_SMALL . '/';
                                break;
+
                        case Proxy::SIZE_MEDIUM:
                                $url .= Proxy::PIXEL_MEDIUM . '/';
                                break;
+
                        case Proxy::SIZE_LARGE:
                                $url .= Proxy::PIXEL_LARGE . '/';
                                break;
@@ -96,43 +114,64 @@ class Link
                return $url . $id;
        }
 
-       private static function fetchMimeType(string $url)
+       /**
+        * Fetches MIME type by URL and Accept: header
+        *
+        * @param string $url URL to fetch
+        * @param string $accept Comma-separated list of expected response MIME type(s)
+        * @return array Discovered MIME type and blurhash or empty array on failure
+        */
+       private static function fetchMimeType(string $url, string $accept = HttpClientAccept::DEFAULT): array
        {
                $timeout = DI::config()->get('system', 'xrd_timeout');
 
-               $curlResult = DI::httpClient()->head($url, [HttpClientOptions::TIMEOUT => $timeout]);
-               if ($curlResult->isSuccess()) {
-                       if (empty($media['mimetype'])) {
-                               return $curlResult->getHeader('Content-Type')[0] ?? '';
+               try {
+                       $curlResult = HTTPSignature::fetchRaw($url, 0, [HttpClientOptions::TIMEOUT => $timeout, HttpClientOptions::ACCEPT_CONTENT => $accept]);
+                       if (empty($curlResult) || !$curlResult->isSuccess()) {
+                               return [];
                        }
+               } catch (\Exception $exception) {
+                       Logger::notice('Error fetching url', ['url' => $url, 'exception' => $exception]);
+                       return [];
                }
-               return '';
+               $fields = ['mimetype' => $curlResult->getHeader('Content-Type')[0]];
+
+               $img_str = $curlResult->getBody();
+               $image = new Image($img_str, Images::getMimeTypeByData($img_str));
+               if ($image->isValid()) {
+                       $fields['mimetype'] = $image->getType();
+                       $fields['width']    = $image->getWidth();
+                       $fields['height']   = $image->getHeight();
+                       $fields['blurhash'] = $image->getBlurHash();
+               }
+
+               return $fields;
        }
 
        /**
         * Add external links and replace them in the body
         *
-        * @param integer $uriid
-        * @param string $body
+        * @param integer $uriId
+        * @param string $body Item body formatted with BBCodes
         * @return string Body with replaced links
         */
-       public static function insertFromBody(int $uriid, string $body)
+       public static function insertFromBody(int $uriId, string $body): string
        {
                if (preg_match_all("/\[img\=([0-9]*)x([0-9]*)\](http.*?)\[\/img\]/ism", $body, $pictures, PREG_SET_ORDER)) {
                        foreach ($pictures as $picture) {
-                               $body = str_replace($picture[3], self::getByLink($uriid, $picture[3]), $body);
+                               $body = str_replace($picture[3], self::getByLink($uriId, $picture[3]), $body);
                        }
                }
 
                if (preg_match_all("/\[img=(http[^\[\]]*)\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures, PREG_SET_ORDER)) {
                        foreach ($pictures as $picture) {
-                               $body = str_replace($picture[1], self::getByLink($uriid, $picture[1]), $body);
+                               $body = str_replace($picture[1], self::getByLink($uriId, $picture[1]), $body);
                        }
                }
 
                if (preg_match_all("/\[img\](http[^\[\]]*)\[\/img\]/ism", $body, $pictures, PREG_SET_ORDER)) {
                        foreach ($pictures as $picture) {
-                               $body = str_replace($picture[1], self::getByLink($uriid, $picture[1]), $body);
+                               $body = str_replace($picture[1], self::getByLink($uriId, $picture[1]), $body);
                        }
                }