]> git.mxchange.org Git - friendica.git/commitdiff
Fixed fetching private local images
authorMichael <heluecht@pirati.ca>
Sat, 3 Jul 2021 15:29:27 +0000 (15:29 +0000)
committerMichael <heluecht@pirati.ca>
Sat, 3 Jul 2021 15:29:27 +0000 (15:29 +0000)
src/Model/Post/Media.php
src/Model/Profile.php
src/Module/Photo.php

index 63bfe00345927f3c4967a01183b42a51c9628ef8..65cd099f7d3b5e9a13eb85854dbdf8af2f9d81c4 100644 (file)
@@ -28,8 +28,10 @@ use Friendica\Database\Database;
 use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Model\Item;
+use Friendica\Model\Photo;
 use Friendica\Model\Post;
 use Friendica\Util\Images;
+use Friendica\Util\Network;
 use Friendica\Util\ParseUrl;
 use Friendica\Util\Proxy;
 use Friendica\Util\Strings;
@@ -158,6 +160,10 @@ class Media
         */
        public static function fetchAdditionalData(array $media)
        {
+               if (Network::isLocalLink($media['url'])) {
+                       $media = self::fetchLocalData($media);
+               }
+
                // Fetch the mimetype or size if missing.
                if (empty($media['mimetype']) || empty($media['size'])) {
                        $timeout = DI::config()->get('system', 'xrd_timeout');
@@ -216,6 +222,36 @@ class Media
                return $media;
        }
 
+       /**
+        * Fetch media data from local resources
+        * @param array $media 
+        * @return array media with added data
+        */
+       private static function fetchLocalData(array $media)
+       {
+               if (!preg_match('|.*?/photo/(.*[a-fA-F0-9])\-(.*[0-9])\..*[\w]|', $media['url'], $matches)) {
+                       return $media;
+               }
+               $photo = Photo::selectFirst([], ['resource-id' => $matches[1], 'scale' => $matches[2]]);
+               if (!empty($photo)) {
+                       $media['mimetype'] = $photo['type'];
+                       $media['size'] = $photo['datasize'];
+                       $media['width'] = $photo['width'];
+                       $media['height'] = $photo['height'];
+               }
+
+               if (!preg_match('|.*?/photo/(.*[a-fA-F0-9])\-(.*[0-9])\..*[\w]|', $media['preview'], $matches)) {
+                       return $media;
+               }
+               $photo = Photo::selectFirst([], ['resource-id' => $matches[1], 'scale' => $matches[2]]);
+               if (!empty($photo)) {
+                       $media['preview-width'] = $photo['width'];
+                       $media['preview-height'] = $photo['height'];
+               }
+
+               return $media;
+       }
+
        /**
         * Add the detected type to the media array
         *
index 4aacc9f9b6341c37692e216447919d005d005974..a56aa05c0bd915f917c327f8f96d8177ecaddb00 100644 (file)
@@ -38,6 +38,7 @@ use Friendica\DI;
 use Friendica\Protocol\Activity;
 use Friendica\Protocol\Diaspora;
 use Friendica\Util\DateTimeFormat;
+use Friendica\Util\HTTPSignature;
 use Friendica\Util\Network;
 use Friendica\Util\Proxy as ProxyUtils;
 use Friendica\Util\Strings;
@@ -828,11 +829,11 @@ class Profile
                // Try to find the public contact entry of the visitor.
                $cid = Contact::getIdForURL($handle);
                if (!$cid) {
-                       Logger::log('unable to finger ' . $handle, Logger::DEBUG);
+                       Logger::info('Handle not found', ['handle' => $handle]);
                        return [];
                }
 
-               $visitor = DBA::selectFirst('contact', [], ['id' => $cid]);
+               $visitor = Contact::getById($cid);
 
                // Authenticate the visitor.
                $_SESSION['authenticated'] = 1;
@@ -851,6 +852,19 @@ class Profile
                return $visitor;
        }
 
+       /**
+        * Set the visitor cookies (see remote_user()) for signed HTTP requests
+        * @return array Visitor contact array
+        */
+       public static function addVisitorCookieForHTTPSigner()
+       {
+               $requester = HTTPSignature::getSigner('', $_SERVER);
+               if (empty($requester)) {
+                       return [];
+               }
+               return Profile::addVisitorCookieForHandle($requester);
+       }
+
        /**
         * OpenWebAuth authentication.
         *
index 57ccd1bddb51870e340f0bd306cee3ab458676f1..3ea7578ce19123a85c67de9f0ac9c7975b8c8634 100644 (file)
@@ -33,8 +33,8 @@ use Friendica\Model\Storage\ExternalResource;
 use Friendica\Model\Storage\SystemResource;
 use Friendica\Util\Proxy;
 use Friendica\Object\Image;
-use Friendica\Util\HTTPSignature;
 use Friendica\Util\Images;
+use Friendica\Util\Network;
 
 /**
  * Photo Module
@@ -67,10 +67,7 @@ class Photo extends BaseModule
                        exit;
                }
 
-               $requester = HTTPSignature::getSigner('', $_SERVER);
-               if (!empty($requester)) {
-                       Profile::addVisitorCookieForHandle($requester);
-               }
+               Profile::addVisitorCookieForHTTPSigner();
 
                $customsize = 0;
                $square_resize = true;
@@ -193,6 +190,10 @@ class Photo extends BaseModule
                                        return false;
                                }
 
+                               if (Network::isLocalLink($url) && preg_match('|.*?/photo/(.*[a-fA-F0-9])\-(.*[0-9])\..*[\w]|', $url, $matches)) {
+                                       return MPhoto::getPhoto($matches[1], $matches[2]);
+                               }
+               
                                return MPhoto::createPhotoForExternalResource($url, (int)local_user());
                        case "media":
                                $media = DBA::selectFirst('post-media', ['url', 'uri-id'], ['id' => $uid, 'type' => Post\Media::IMAGE]);
@@ -200,6 +201,10 @@ class Photo extends BaseModule
                                        return false;
                                }
 
+                               if (Network::isLocalLink($media['url']) && preg_match('|.*?/photo/(.*[a-fA-F0-9])\-(.*[0-9])\..*[\w]|', $media['url'], $matches)) {
+                                       return MPhoto::getPhoto($matches[1], $matches[2]);
+                               }
+
                                return MPhoto::createPhotoForExternalResource($media['url'], (int)local_user());
                        case "contact":
                                $contact = Contact::getById($uid, ['uid', 'url', 'avatar', 'photo', 'xmpp', 'addr']);