]> git.mxchange.org Git - friendica.git/blobdiff - src/Module/PermissionTooltip.php
Allow the search for contacts on blocked servers via web
[friendica.git] / src / Module / PermissionTooltip.php
index 032dc9c380827b8e7fd54001647257cb9fc24f9e..ad5c11cc05740e886e426017fdd2ef8ce5492503 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2022, the Friendica project
+ * @copyright Copyright (C) 2010-2023, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
 namespace Friendica\Module;
 
 use Friendica\Core\Hook;
+use Friendica\Core\Protocol;
+use Friendica\Core\System;
 use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Model\APContact;
 use Friendica\Model\Group;
+use Friendica\Model\Item;
 use Friendica\Model\Post;
 use Friendica\Model\Tag;
 use Friendica\Model\User;
 use Friendica\Network\HTTPException;
-use Friendica\Protocol\ActivityPub;
 
 /**
  * Outputs the permission tooltip HTML content for the provided item, photo or event id.
@@ -47,10 +49,23 @@ class PermissionTooltip extends \Friendica\BaseModule
                        throw new HTTPException\BadRequestException(DI::l10n()->t('Wrong type "%s", expected one of: %s', $type, implode(', ', $expectedTypes)));
                }
 
-               $condition = ['id' => $referenceId];
+               $condition = ['id' => $referenceId, 'uid' => [0, DI::userSession()->getLocalUserId()]];
                if ($type == 'item') {
-                       $fields = ['uid', 'psid', 'private', 'uri-id'];
-                       $model = Post::selectFirst($fields, $condition);
+                       $fields = ['uid', 'psid', 'private', 'uri-id', 'origin', 'network'];
+                       $model = Post::selectFirst($fields, $condition, ['order' => ['uid' => true]]);
+
+                       if ($model['origin'] || ($model['network'] != Protocol::ACTIVITYPUB)) {
+                               $permissionSet = DI::permissionSet()->selectOneById($model['psid'], $model['uid']);
+                               $model['allow_cid'] = $permissionSet->allow_cid;
+                               $model['allow_gid'] = $permissionSet->allow_gid;
+                               $model['deny_cid']  = $permissionSet->deny_cid;
+                               $model['deny_gid']  = $permissionSet->deny_gid;
+                       } else {
+                               $model['allow_cid'] = [];
+                               $model['allow_gid'] = [];
+                               $model['deny_cid']  = [];
+                               $model['deny_gid']  = [];
+                       }
                } else {
                        $fields = ['uid', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid'];
                        $model = DBA::selectFirst($type, $fields, $condition);
@@ -64,25 +79,31 @@ class PermissionTooltip extends \Friendica\BaseModule
                        throw new HttpException\NotFoundException(DI::l10n()->t('Model not found'));
                }
 
-               if (isset($model['psid'])) {
-                       $permissionSet = DI::permissionSet()->selectOneById($model['psid'], $model['uid']);
-                       $model['allow_cid'] = $permissionSet->allow_cid;
-                       $model['allow_gid'] = $permissionSet->allow_gid;
-                       $model['deny_cid']  = $permissionSet->deny_cid;
-                       $model['deny_gid']  = $permissionSet->deny_gid;
-               }
-
-               // Kept for backwards compatiblity
+               // Kept for backwards compatibility
                Hook::callAll('lockview_content', $model);
 
                if ($type == 'item') {
                        $receivers = $this->fetchReceivers($model['uri-id']);
+                       if (empty($receivers)) {
+                               switch ($model['private']) {
+                                       case Item::PUBLIC:
+                                               $receivers = DI::l10n()->t('Public');
+                                               break;
+
+                                       case Item::UNLISTED:
+                                               $receivers = DI::l10n()->t('Unlisted');
+                                               break;
+
+                                       case Item::PRIVATE:
+                                               $receivers = DI::l10n()->t('Limited/Private');
+                                               break;
+                               }
+                       }
                } else {
                        $receivers = '';
                }
 
-               if ($model['uid'] != local_user() ||
-                       empty($model['allow_cid'])
+               if (empty($model['allow_cid'])
                        && empty($model['allow_gid'])
                        && empty($model['deny_cid'])
                        && empty($model['deny_gid'])
@@ -145,24 +166,22 @@ class PermissionTooltip extends \Friendica\BaseModule
                }
 
                if (!empty($l)) {
-                       echo $o . implode(', ', $l);
+                       System::httpExit($o . implode(', ', $l));
                } else {
-                       echo $o . $receivers;
+                       System::httpExit($o . $receivers);;
                }
-
-               exit();
        }
 
        /**
         * Fetch a list of receivers
         *
         * @param int $uriId
-        * @return string 
+        * @return string
         */
-       private function fetchReceivers(int $uriId):string
+       private function fetchReceivers(int $uriId): string
        {
                $own_url = '';
-               $uid = local_user();
+               $uid = DI::userSession()->getLocalUserId();
                if ($uid) {
                        $owner = User::getOwnerDataById($uid);
                        if (!empty($owner['url'])) {
@@ -177,17 +196,24 @@ class PermissionTooltip extends \Friendica\BaseModule
                                continue;
                        }
 
-                       if ($receiver['url'] == ActivityPub::PUBLIC_COLLECTION) {
-                               $receivers[$receiver['type']][] = DI::l10n()->t('Public');
-                       } else {
-                               $apcontact = DBA::selectFirst('apcontact', ['name'], ['followers' => $receiver['url']]);
-                               if (!empty($apcontact['name'])) {
-                                       $receivers[$receiver['type']][] = DI::l10n()->t('Followers (%s)', $apcontact['name']);
-                               } elseif ($apcontact = APContact::getByURL($receiver['url'], false)) {
-                                       $receivers[$receiver['type']][] = $apcontact['name'];
-                               } else {
+                       switch (Tag::getTargetType($receiver['url'], false)) {
+                               case Tag::PUBLIC_COLLECTION:
+                                       $receivers[$receiver['type']][] = DI::l10n()->t('Public');
+                                       break;
+                               case Tag::GENERAL_COLLECTION:
+                                       $receivers[$receiver['type']][] = DI::l10n()->t('Collection (%s)', $receiver['name']);
+                                       break;
+                               case Tag::FOLLOWER_COLLECTION:
+                                       $apcontact = DBA::selectFirst('apcontact', ['name'], ['followers' => $receiver['url']]);
+                                       $receivers[$receiver['type']][] = DI::l10n()->t('Followers (%s)', $apcontact['name'] ?? $receiver['name']);
+                                       break;
+                               case Tag::ACCOUNT:
+                                       $apcontact = APContact::getByURL($receiver['url'], false);
+                                       $receivers[$receiver['type']][] = $apcontact['name'] ?? $receiver['name'];
+                                       break;
+                               default:
                                        $receivers[$receiver['type']][] = $receiver['name'];
-                               }
+                                       break;
                        }
                }