]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Post/Link.php
Don't cache local avatars
[friendica.git] / src / Model / Post / Link.php
index c1cc3ae1e2b59d56d210b6921c788c83b2607692..78f57425f890554f1cdd41452460110a43ba6cf2 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2021, the Friendica project
+ * @copyright Copyright (C) 2010-2022, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
 namespace Friendica\Model\Post;
 
 use Friendica\Core\Logger;
+use Friendica\Core\System;
+use Friendica\Database\Database;
 use Friendica\Database\DBA;
 use Friendica\DI;
+use Friendica\Network\HTTPClient\Client\HttpClientOptions;
 use Friendica\Util\Proxy;
 
 /**
@@ -33,22 +36,39 @@ use Friendica\Util\Proxy;
  */
 class Link
 {
+       /**
+        * Check if the link is stored
+        *
+        * @param int $uri_id
+        * @param string $url
+        * @return bool
+        */
+       public static function exists(int $uri_id, string $url)
+       {
+               return DBA::exists('post-link', ['uri-id' => $uri_id, 'url' => $url]);
+       }
+
        public static function getByLink(int $uri_id, string $url, $size = '')
        {
-               if (empty($url) || Proxy::isLocalImage($url)) {
+               if (empty($uri_id) || 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)]);
                        return $url;
                }
 
                $link = DBA::selectFirst('post-link', ['id'], ['uri-id' => $uri_id, 'url' => $url]);
                if (!empty($link['id'])) {
                        $id = $link['id'];
-                       Logger::info('Found', ['id' => $id, 'url' => $url]);
+                       Logger::info('Found', ['id' => $id, 'uri-id' => $uri_id, 'url' => $url]);
                } else {
                        $mime = self::fetchMimeType($url);
 
-                       DBA::insert('post-link', ['uri-id' => $uri_id, 'url' => $url, 'mimetype' => $mime]);
+                       DBA::insert('post-link', ['uri-id' => $uri_id, 'url' => $url, 'mimetype' => $mime], Database::INSERT_IGNORE);
                        $id = DBA::lastInsertId();
-                       Logger::info('Inserted', ['id' => $id, 'url' => $url]);
+                       Logger::info('Inserted', ['id' => $id, 'uri-id' => $uri_id, 'url' => $url]);
                }
 
                if (empty($id)) {
@@ -80,10 +100,10 @@ class Link
        {
                $timeout = DI::config()->get('system', 'xrd_timeout');
 
-               $curlResult = DI::httpRequest()->head($url, ['timeout' => $timeout]);
+               $curlResult = DI::httpClient()->head($url, [HttpClientOptions::TIMEOUT => $timeout]);
                if ($curlResult->isSuccess()) {
                        if (empty($media['mimetype'])) {
-                               return $curlResult->getHeader('Content-Type');
+                               return $curlResult->getHeader('Content-Type')[0] ?? '';
                        }
                }
                return '';
@@ -98,21 +118,19 @@ class Link
         */
        public static function insertFromBody(int $uriid, string $body)
        {
-               if (preg_match_all("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", $body, $pictures, PREG_SET_ORDER)) {
+               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);
                        }
                }
 
-               if (preg_match_all("/\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures, PREG_SET_ORDER)) {
+               if (preg_match_all("/\[img=(http[^\[\]]*)\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures, PREG_SET_ORDER)) {
                        foreach ($pictures as $picture) {
-                               if (parse_url($picture[1], PHP_URL_SCHEME)) {
-                                       $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\]([^\[\]]*)\[\/img\]/ism", $body, $pictures, PREG_SET_ORDER)) {
+               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);
                        }