]> git.mxchange.org Git - friendica.git/blob - src/Module/PermissionTooltip.php
Use the post language for the language detection / config for quality
[friendica.git] / src / Module / PermissionTooltip.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Module;
23
24 use Friendica\Core\Hook;
25 use Friendica\Core\Protocol;
26 use Friendica\Core\System;
27 use Friendica\Database\DBA;
28 use Friendica\DI;
29 use Friendica\Model\APContact;
30 use Friendica\Model\Circle;
31 use Friendica\Model\Item;
32 use Friendica\Model\Post;
33 use Friendica\Model\Tag;
34 use Friendica\Model\User;
35 use Friendica\Network\HTTPException;
36
37 /**
38  * Outputs the permission tooltip HTML content for the provided item, photo or event id.
39  */
40 class PermissionTooltip extends \Friendica\BaseModule
41 {
42         protected function rawContent(array $request = [])
43         {
44                 $type = $this->parameters['type'];
45                 $referenceId = $this->parameters['id'];
46
47                 $expectedTypes = ['item', 'photo', 'event'];
48                 if (!in_array($type, $expectedTypes)) {
49                         throw new HTTPException\BadRequestException(DI::l10n()->t('Wrong type "%s", expected one of: %s', $type, implode(', ', $expectedTypes)));
50                 }
51
52                 $condition = ['id' => $referenceId, 'uid' => [0, DI::userSession()->getLocalUserId()]];
53                 if ($type == 'item') {
54                         $fields = ['uid', 'psid', 'private', 'uri-id', 'origin', 'network'];
55                         $model = Post::selectFirst($fields, $condition, ['order' => ['uid' => true]]);
56
57                         if ($model['origin'] || ($model['network'] != Protocol::ACTIVITYPUB)) {
58                                 $permissionSet = DI::permissionSet()->selectOneById($model['psid'], $model['uid']);
59                                 $model['allow_cid'] = $permissionSet->allow_cid;
60                                 $model['allow_gid'] = $permissionSet->allow_gid;
61                                 $model['deny_cid']  = $permissionSet->deny_cid;
62                                 $model['deny_gid']  = $permissionSet->deny_gid;
63                         } else {
64                                 $model['allow_cid'] = [];
65                                 $model['allow_gid'] = [];
66                                 $model['deny_cid']  = [];
67                                 $model['deny_gid']  = [];
68                         }
69                 } else {
70                         $fields = ['uid', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid'];
71                         $model = DBA::selectFirst($type, $fields, $condition);
72                         $model['allow_cid'] = DI::aclFormatter()->expand($model['allow_cid']);
73                         $model['allow_gid'] = DI::aclFormatter()->expand($model['allow_gid']);
74                         $model['deny_cid']  = DI::aclFormatter()->expand($model['deny_cid']);
75                         $model['deny_gid']  = DI::aclFormatter()->expand($model['deny_gid']);
76                 }
77
78                 if (!DBA::isResult($model)) {
79                         throw new HttpException\NotFoundException(DI::l10n()->t('Model not found'));
80                 }
81
82                 // Kept for backwards compatibility
83                 Hook::callAll('lockview_content', $model);
84
85                 if ($type == 'item') {
86                         $receivers = $this->fetchReceivers($model['uri-id']);
87                         if (empty($receivers)) {
88                                 switch ($model['private']) {
89                                         case Item::PUBLIC:
90                                                 $receivers = DI::l10n()->t('Public');
91                                                 break;
92
93                                         case Item::UNLISTED:
94                                                 $receivers = DI::l10n()->t('Unlisted');
95                                                 break;
96
97                                         case Item::PRIVATE:
98                                                 $receivers = DI::l10n()->t('Limited/Private');
99                                                 break;
100                                 }
101                         }
102                 } else {
103                         $receivers = '';
104                 }
105
106                 if (empty($model['allow_cid'])
107                         && empty($model['allow_gid'])
108                         && empty($model['deny_cid'])
109                         && empty($model['deny_gid'])
110                         && empty($receivers))
111                 {
112                         echo DI::l10n()->t('Remote privacy information not available.');
113                         exit;
114                 }
115
116                 if (!empty($model['allow_cid']) || !empty($model['allow_gid']) || !empty($model['deny_cid']) || !empty($model['deny_gid'])) {
117                         $receivers = $this->fetchReceiversFromACL($model);
118                 }
119
120                 $this->httpExit(DI::l10n()->t('Visible to:') . '<br />' . $receivers);
121         }
122
123         /**
124          * Fetch a list of receivers based on the ACL data
125          *
126          * @param array $model
127          * @return string
128          */
129         private function fetchReceiversFromACL(array $model)
130         {
131                 $allowed_users   = $model['allow_cid'];
132                 $allowed_circles = $model['allow_gid'];
133                 $deny_users      = $model['deny_cid'];
134                 $deny_circles    = $model['deny_gid'];
135
136                 $l = [];
137
138                 if (count($allowed_circles)) {
139                         $key = array_search(Circle::FOLLOWERS, $allowed_circles);
140                         if ($key !== false) {
141                                 $l[] = '<b>' . DI::l10n()->t('Followers') . '</b>';
142                                 unset($allowed_circles[$key]);
143                         }
144
145                         $key = array_search(Circle::MUTUALS, $allowed_circles);
146                         if ($key !== false) {
147                                 $l[] = '<b>' . DI::l10n()->t('Mutuals') . '</b>';
148                                 unset($allowed_circles[$key]);
149                         }
150
151                         foreach (DI::dba()->selectToArray('group', ['name'], ['id' => $allowed_circles]) as $circle) {
152                                 $l[] = '<b>' . $circle['name'] . '</b>';
153                         }
154                 }
155
156                 foreach (DI::dba()->selectToArray('contact', ['name'], ['id' => $allowed_users]) as $contact) {
157                         $l[] = $contact['name'];
158                 }
159
160                 if (count($deny_circles)) {
161                         $key = array_search(Circle::FOLLOWERS, $deny_circles);
162                         if ($key !== false) {
163                                 $l[] = '<b><strike>' . DI::l10n()->t('Followers') . '</strike></b>';
164                                 unset($deny_circles[$key]);
165                         }
166
167                         $key = array_search(Circle::MUTUALS, $deny_circles);
168                         if ($key !== false) {
169                                 $l[] = '<b><strike>' . DI::l10n()->t('Mutuals') . '</strike></b>';
170                                 unset($deny_circles[$key]);
171                         }
172
173                         foreach (DI::dba()->selectToArray('group', ['name'], ['id' => $allowed_circles]) as $circle) {
174                                 $l[] = '<b><strike>' . $circle['name'] . '</strike></b>';
175                         }
176                 }
177
178                 foreach (DI::dba()->selectToArray('contact', ['name'], ['id' => $deny_users]) as $contact) {
179                         $l[] = '<strike>' . $contact['name'] . '</strike>';
180                 }
181
182                 return implode(', ', $l);
183         }
184
185         /**
186          * Fetch a list of receivers
187          *
188          * @param int $uriId
189          * @return string
190          */
191         private function fetchReceivers(int $uriId): string
192         {
193                 $own_url = '';
194                 $uid = DI::userSession()->getLocalUserId();
195                 if ($uid) {
196                         $owner = User::getOwnerDataById($uid);
197                         if (!empty($owner['url'])) {
198                                 $own_url = $owner['url'];
199                         }
200                 }
201
202                 $receivers = [];
203                 foreach (Tag::getByURIId($uriId, [Tag::TO, Tag::CC, Tag::BCC, Tag::AUDIENCE, Tag::ATTRIBUTED]) as $receiver) {
204                         // We only display BCC when it contains the current user
205                         if (($receiver['type'] == Tag::BCC) && ($receiver['url'] != $own_url)) {
206                                 continue;
207                         }
208
209                         switch (Tag::getTargetType($receiver['url'], false)) {
210                                 case Tag::PUBLIC_COLLECTION:
211                                         $receivers[$receiver['type']][] = DI::l10n()->t('Public');
212                                         break;
213                                 case Tag::GENERAL_COLLECTION:
214                                         $receivers[$receiver['type']][] = DI::l10n()->t('Collection (%s)', $receiver['name']);
215                                         break;
216                                 case Tag::FOLLOWER_COLLECTION:
217                                         $apcontact = DBA::selectFirst('apcontact', ['name'], ['followers' => $receiver['url']]);
218                                         $receivers[$receiver['type']][] = DI::l10n()->t('Followers (%s)', $apcontact['name'] ?? $receiver['name']);
219                                         break;
220                                 case Tag::ACCOUNT:
221                                         $apcontact = APContact::getByURL($receiver['url'], false);
222                                         $receivers[$receiver['type']][] = $apcontact['name'] ?? $receiver['name'];
223                                         break;
224                                 default:
225                                         $receivers[$receiver['type']][] = $receiver['name'];
226                                         break;
227                         }
228                 }
229
230                 $output = '';
231
232                 foreach ($receivers as $type => $receiver) {
233                         $max = DI::config()->get('system', 'max_receivers');
234                         $total = count($receiver);
235                         if ($total > $max) {
236                                 $receiver = array_slice($receiver, 0, $max);
237                                 $receiver[] = DI::l10n()->t('%d more', $total - $max);
238                         }
239                         switch ($type) {
240                                 case Tag::TO:
241                                         $output .= DI::l10n()->t('<b>To:</b> %s<br>', implode(', ', $receiver));
242                                         break;
243                                 case Tag::CC:
244                                         $output .= DI::l10n()->t('<b>CC:</b> %s<br>', implode(', ', $receiver));
245                                         break;
246                                 case Tag::BCC:
247                                         $output .= DI::l10n()->t('<b>BCC:</b> %s<br>', implode(', ', $receiver));
248                                         break;
249                                 case Tag::AUDIENCE:
250                                         $output .= DI::l10n()->t('<b>Audience:</b> %s<br>', implode(', ', $receiver));
251                                         break;
252                                 case Tag::ATTRIBUTED:
253                                         $output .= DI::l10n()->t('<b>Attributed To:</b> %s<br>', implode(', ', $receiver));
254                                         break;
255                         }
256                 }
257
258                 return $output;
259         }
260 }