]> git.mxchange.org Git - friendica.git/commitdiff
Refactor duplicate code for parsing photo URL
authorMatthew Exon <github.mexon@spamgourmet.com>
Thu, 9 Jan 2020 20:41:35 +0000 (21:41 +0100)
committerMatthew Exon <git.mexon@spamgourmet.com>
Thu, 9 Jan 2020 20:48:37 +0000 (21:48 +0100)
src/Model/Mail.php
src/Model/Photo.php

index eeea130a60b8ca16e2c0c36fcf3694d6eb5d102c..da72d7d51f39d8c07916a6b3cb47adae5cd6d305 100644 (file)
@@ -215,12 +215,10 @@ class Mail
                        $images = $match[1];
                        if (count($images)) {
                                foreach ($images as $image) {
-                                       if (!stristr($image, DI::baseUrl() . '/photo/')) {
-                                               continue;
+                                       $image_rid = Photo::ridFromURI($image);
+                                       if ($image_rid) {
+                                               Photo::update(['allow-cid' => '<' . $recipient . '>'], ['resource-id' => $image_rid, 'album' => 'Wall Photos', 'uid' => local_user()]);
                                        }
-                                       $image_uri = substr($image, strrpos($image, '/') + 1);
-                                       $image_uri = substr($image_uri, 0, strpos($image_uri, '-'));
-                                       Photo::update(['allow-cid' => '<' . $recipient . '>'], ['resource-id' => $image_uri, 'album' => 'Wall Photos', 'uid' => local_user()]);
                                }
                        }
                }
index c4dbf2b30a1f53eda14d089cf2e8eb6da2babfad..faae9e8b99fe0ebb8cce1c504f252ebdc585eab5 100644 (file)
@@ -596,6 +596,25 @@ class Photo
                return System::createGUID(32, false);
        }
 
+       /**
+        * Extracts the rid from a local photo URI
+        *
+        * @param string $image_uri The URI of the photo
+        * @return string The rid of the photo, or an empty string if the URI is not local
+        */
+       public static function ridFromURI($image_uri)
+       {
+               if (!stristr($image_uri, DI::baseUrl() . '/photo/')) {
+                       return;
+               }
+               $image_uri = substr($image_uri, strrpos($image_uri, '/') + 1);
+               $image_uri = substr($image_uri, 0, strpos($image_uri, '-'));
+               if (!strlen($image_uri)) {
+                       return;
+               }
+               return $image_uri;
+       }
+
        /**
         * Changes photo permissions that had been embedded in a post
         *
@@ -622,12 +641,8 @@ class Photo
                }
 
                foreach ($images as $image) {
-                       if (!stristr($image, DI::baseUrl() . '/photo/')) {
-                               continue;
-                       }
-                       $image_uri = substr($image,strrpos($image,'/') + 1);
-                       $image_uri = substr($image_uri,0, strpos($image_uri,'-'));
-                       if (!strlen($image_uri)) {
+                       $image_rid = ridFromURI($image);
+                       if (!$image_rid) {
                                continue;
                        }
 
@@ -636,7 +651,7 @@ class Photo
 
                        $condition = [
                                'allow_cid' => $srch, 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => '',
-                               'resource-id' => $image_uri, 'uid' => $uid
+                               'resource-id' => $image_rid, 'uid' => $uid
                        ];
                        if (!Photo::exists($condition)) {
                                continue;
@@ -646,7 +661,7 @@ class Photo
 
                        $fields = ['allow_cid' => $str_contact_allow, 'allow_gid' => $str_group_allow,
                                        'deny_cid' => $str_contact_deny, 'deny_gid' => $str_group_deny];
-                       $condition = ['resource-id' => $image_uri, 'uid' => $uid];
+                       $condition = ['resource-id' => $image_rid, 'uid' => $uid];
                        Logger::info('Set permissions', ['condition' => $condition, 'permissions' => $fields]);
                        Photo::update($fields, $condition);
                }