]> git.mxchange.org Git - friendica.git/commitdiff
Detection of local requests
authorMichael <heluecht@pirati.ca>
Mon, 19 Jul 2021 06:14:14 +0000 (06:14 +0000)
committerMichael <heluecht@pirati.ca>
Mon, 19 Jul 2021 06:14:14 +0000 (06:14 +0000)
src/Model/Photo.php
src/Network/HTTPRequest.php
src/Util/Images.php

index 61fc2df4ea1f2d7bcf7909797a4866acd2415caf..30e666898777c5b4a9c245d5a31b611b9373b932 100644 (file)
@@ -804,30 +804,33 @@ class Photo
        }
 
        /**
-        * Returns the GUID from picture links
+        * Fetch the guid and scale from picture links
         *
         * @param string $name Picture link
-        * @return string GUID
-        * @throws \Exception
+        * @return array
         */
-       public static function getGUID($name)
+       public static function getResourceData(string $name):array
        {
                $base = DI::baseUrl()->get();
 
                $guid = str_replace([Strings::normaliseLink($base), '/photo/'], '', Strings::normaliseLink($name));
 
+               if (parse_url($guid, PHP_URL_SCHEME)) {
+                       return [];
+               }
+
                $guid = self::stripExtension($guid);
                if (substr($guid, -2, 1) != "-") {
-                       return '';
+                       return [];
                }
 
                $scale = intval(substr($guid, -1, 1));
                if (!is_numeric($scale)) {
-                       return '';
+                       return [];
                }
 
                $guid = substr($guid, 0, -2);
-               return $guid;
+               return ['guid' => $guid, 'scale' => $scale];
        }
 
        /**
@@ -839,13 +842,12 @@ class Photo
         */
        public static function isLocal($name)
        {
-               $guid = self::getGUID($name);
-
-               if (empty($guid)) {
+               $data = self::getResourceData($name);
+               if (empty($data)) {
                        return false;
                }
 
-               return DBA::exists('photo', ['resource-id' => $guid]);
+               return DBA::exists('photo', ['resource-id' => $data['guid'], 'scale' => $data['scale']]);
        }
 
        /**
index bd31ac1e143702ff597f2f4038c69e73ab104bdf..622828b434ce31d0fd66af97cec5fcbe7177cd40 100644 (file)
@@ -74,6 +74,10 @@ class HTTPRequest implements IHTTPRequest
        {
                $stamp1 = microtime(true);
 
+               if (Network::isLocalLink($url)) {
+                       $this->logger->info('Local link', ['url' => $url, 'callstack' => System::callstack(20)]);
+               }
+
                if (strlen($url) > 1000) {
                        $this->logger->debug('URL is longer than 1000 characters.', ['url' => $url, 'callstack' => System::callstack(20)]);
                        $this->profiler->saveTimestamp($stamp1, 'network');
@@ -226,6 +230,10 @@ class HTTPRequest implements IHTTPRequest
        {
                $stamp1 = microtime(true);
 
+               if (Network::isLocalLink($url)) {
+                       $this->logger->info('Local link', ['url' => $url, 'callstack' => System::callstack(20)]);
+               }
+
                if (Network::isUrlBlocked($url)) {
                        $this->logger->info('Domain is blocked.' . ['url' => $url]);
                        $this->profiler->saveTimestamp($stamp1, 'network');
@@ -328,6 +336,10 @@ class HTTPRequest implements IHTTPRequest
         */
        public function finalUrl(string $url, int $depth = 1, bool $fetchbody = false)
        {
+               if (Network::isLocalLink($url)) {
+                       $this->logger->info('Local link', ['url' => $url, 'callstack' => System::callstack(20)]);
+               }
+
                if (Network::isUrlBlocked($url)) {
                        $this->logger->info('Domain is blocked.', ['url' => $url]);
                        return $url;
index 7b11ea3f6b580e57286d33beab41c04c409c6153..3b07aee2fca1399e35bc6a994ed3459d70b2ce46 100644 (file)
@@ -22,8 +22,8 @@
 namespace Friendica\Util;
 
 use Friendica\Core\Logger;
-use Friendica\Core\System;
 use Friendica\DI;
+use Friendica\Model\Photo;
 
 /**
  * Image utilities
@@ -184,7 +184,16 @@ class Images
                        return $data;
                }
 
-               $img_str = DI::httpRequest()->fetch($url, 4);
+               if (Network::isLocalLink($url) && ($data = Photo::getResourceData($url))) {
+                       $photo = Photo::getPhoto($data['guid'], $data['scale']);
+                       if (!empty($photo)) {
+                               $img_str = Photo::getImageDataForPhoto($photo);
+                       }
+               }
+
+               if (empty($img_str)) {
+                       $img_str = DI::httpRequest()->fetch($url, 4);
+               }
 
                if (!$img_str) {
                        return [];