]> git.mxchange.org Git - friendica.git/blob - src/Module/PermissionTooltip.php
Merge pull request #12025 from annando/no-boot-src-module
[friendica.git] / src / Module / PermissionTooltip.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\Session;
26 use Friendica\Core\System;
27 use Friendica\Database\DBA;
28 use Friendica\DI;
29 use Friendica\Model\APContact;
30 use Friendica\Model\Group;
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 use Friendica\Protocol\ActivityPub;
37
38 /**
39  * Outputs the permission tooltip HTML content for the provided item, photo or event id.
40  */
41 class PermissionTooltip extends \Friendica\BaseModule
42 {
43         protected function rawContent(array $request = [])
44         {
45                 $type = $this->parameters['type'];
46                 $referenceId = $this->parameters['id'];
47
48                 $expectedTypes = ['item', 'photo', 'event'];
49                 if (!in_array($type, $expectedTypes)) {
50                         throw new HTTPException\BadRequestException(DI::l10n()->t('Wrong type "%s", expected one of: %s', $type, implode(', ', $expectedTypes)));
51                 }
52
53                 $condition = ['id' => $referenceId, 'uid' => [0, Session::getLocalUser()]];
54                 if ($type == 'item') {
55                         $fields = ['uid', 'psid', 'private', 'uri-id'];
56                         $model = Post::selectFirst($fields, $condition);
57                 } else {
58                         $fields = ['uid', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid'];
59                         $model = DBA::selectFirst($type, $fields, $condition);
60                         $model['allow_cid'] = DI::aclFormatter()->expand($model['allow_cid']);
61                         $model['allow_gid'] = DI::aclFormatter()->expand($model['allow_gid']);
62                         $model['deny_cid']  = DI::aclFormatter()->expand($model['deny_cid']);
63                         $model['deny_gid']  = DI::aclFormatter()->expand($model['deny_gid']);
64                 }
65
66                 if (!DBA::isResult($model)) {
67                         throw new HttpException\NotFoundException(DI::l10n()->t('Model not found'));
68                 }
69
70                 if (isset($model['psid'])) {
71                         $permissionSet = DI::permissionSet()->selectOneById($model['psid'], $model['uid']);
72                         $model['allow_cid'] = $permissionSet->allow_cid;
73                         $model['allow_gid'] = $permissionSet->allow_gid;
74                         $model['deny_cid']  = $permissionSet->deny_cid;
75                         $model['deny_gid']  = $permissionSet->deny_gid;
76                 }
77
78                 // Kept for backwards compatiblity
79                 Hook::callAll('lockview_content', $model);
80
81                 if ($type == 'item') {
82                         $receivers = $this->fetchReceivers($model['uri-id']);
83                         if (empty($receivers)) {
84                                 switch ($model['private']) {
85                                         case Item::PUBLIC:
86                                                 $receivers = DI::l10n()->t('Public');
87                                                 break;
88
89                                         case Item::UNLISTED:
90                                                 $receivers = DI::l10n()->t('Unlisted');
91                                                 break;
92
93                                         case Item::PRIVATE:
94                                                 $receivers = DI::l10n()->t('Limited/Private');
95                                                 break;
96                                 }
97                         }
98                 } else {
99                         $receivers = '';
100                 }
101
102                 if (empty($model['allow_cid'])
103                         && empty($model['allow_gid'])
104                         && empty($model['deny_cid'])
105                         && empty($model['deny_gid'])
106                         && empty($receivers))
107                 {
108                         echo DI::l10n()->t('Remote privacy information not available.');
109                         exit;
110                 }
111
112                 $allowed_users  = $model['allow_cid'];
113                 $allowed_groups = $model['allow_gid'];
114                 $deny_users     = $model['deny_cid'];
115                 $deny_groups    = $model['deny_gid'];
116
117                 $o = DI::l10n()->t('Visible to:') . '<br />';
118                 $l = [];
119
120                 if (count($allowed_groups)) {
121                         $key = array_search(Group::FOLLOWERS, $allowed_groups);
122                         if ($key !== false) {
123                                 $l[] = '<b>' . DI::l10n()->t('Followers') . '</b>';
124                                 unset($allowed_groups[$key]);
125                         }
126
127                         $key = array_search(Group::MUTUALS, $allowed_groups);
128                         if ($key !== false) {
129                                 $l[] = '<b>' . DI::l10n()->t('Mutuals') . '</b>';
130                                 unset($allowed_groups[$key]);
131                         }
132
133                         foreach (DI::dba()->selectToArray('group', ['name'], ['id' => $allowed_groups]) as $group) {
134                                 $l[] = '<b>' . $group['name'] . '</b>';
135                         }
136                 }
137
138                 foreach (DI::dba()->selectToArray('contact', ['name'], ['id' => $allowed_users]) as $contact) {
139                         $l[] = $contact['name'];
140                 }
141
142                 if (count($deny_groups)) {
143                         $key = array_search(Group::FOLLOWERS, $deny_groups);
144                         if ($key !== false) {
145                                 $l[] = '<b><strike>' . DI::l10n()->t('Followers') . '</strike></b>';
146                                 unset($deny_groups[$key]);
147                         }
148
149                         $key = array_search(Group::MUTUALS, $deny_groups);
150                         if ($key !== false) {
151                                 $l[] = '<b><strike>' . DI::l10n()->t('Mutuals') . '</strike></b>';
152                                 unset($deny_groups[$key]);
153                         }
154
155                         foreach (DI::dba()->selectToArray('group', ['name'], ['id' => $allowed_groups]) as $group) {
156                                 $l[] = '<b><strike>' . $group['name'] . '</strike></b>';
157                         }
158                 }
159
160                 foreach (DI::dba()->selectToArray('contact', ['name'], ['id' => $deny_users]) as $contact) {
161                         $l[] = '<strike>' . $contact['name'] . '</strike>';
162                 }
163
164                 if (!empty($l)) {
165                         echo $o . implode(', ', $l);
166                 } else {
167                         echo $o . $receivers;
168                 }
169                 System::exit();
170         }
171
172         /**
173          * Fetch a list of receivers
174          *
175          * @param int $uriId
176          * @return string
177          */
178         private function fetchReceivers(int $uriId): string
179         {
180                 $own_url = '';
181                 $uid = Session::getLocalUser();
182                 if ($uid) {
183                         $owner = User::getOwnerDataById($uid);
184                         if (!empty($owner['url'])) {
185                                 $own_url = $owner['url'];
186                         }
187                 }
188
189                 $receivers = [];
190                 foreach (Tag::getByURIId($uriId, [Tag::TO, Tag::CC, Tag::BCC]) as $receiver) {
191                         // We only display BCC when it contains the current user
192                         if (($receiver['type'] == Tag::BCC) && ($receiver['url'] != $own_url)) {
193                                 continue;
194                         }
195
196                         switch (Tag::getTargetType($receiver['url'], false)) {
197                                 case Tag::PUBLIC_COLLECTION:
198                                         $receivers[$receiver['type']][] = DI::l10n()->t('Public');
199                                         break;
200                                 case Tag::GENERAL_COLLECTION:
201                                         $receivers[$receiver['type']][] = DI::l10n()->t('Collection (%s)', $receiver['name']);
202                                         break;
203                                 case Tag::FOLLOWER_COLLECTION:
204                                         $apcontact = DBA::selectFirst('apcontact', ['name'], ['followers' => $receiver['url']]);
205                                         $receivers[$receiver['type']][] = DI::l10n()->t('Followers (%s)', $apcontact['name'] ?? $receiver['name']);
206                                         break;
207                                 case Tag::ACCOUNT:
208                                         $apcontact = APContact::getByURL($receiver['url'], false);
209                                         $receivers[$receiver['type']][] = $apcontact['name'] ?? $receiver['name'];
210                                         break;
211                                 default:
212                                         $receivers[$receiver['type']][] = $receiver['name'];
213                                         break;
214                         }
215                 }
216
217                 $output = '';
218
219                 foreach ($receivers as $type => $receiver) {
220                         $max = DI::config()->get('system', 'max_receivers');
221                         $total = count($receiver);
222                         if ($total > $max) {
223                                 $receiver = array_slice($receiver, 0, $max);
224                                 $receiver[] = DI::l10n()->t('%d more', $total - $max);
225                         }
226                         switch ($type) {
227                                 case Tag::TO:
228                                         $output .= DI::l10n()->t('<b>To:</b> %s<br>', implode(', ', $receiver));
229                                         break;
230                                 case Tag::CC:
231                                         $output .= DI::l10n()->t('<b>CC:</b> %s<br>', implode(', ', $receiver));
232                                         break;
233                                 case Tag::BCC:
234                                         $output .= DI::l10n()->t('<b>BCC:</b> %s<br>', implode(', ', $receiver));
235                                         break;
236                         }
237                 }
238
239                 return $output;
240         }
241 }