]> git.mxchange.org Git - friendica.git/blob - src/Module/PermissionTooltip.php
Merge pull request #11278 from annando/receiver-name
[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\Database\DBA;
26 use Friendica\DI;
27 use Friendica\Model\Group;
28 use Friendica\Model\Post;
29 use Friendica\Model\Tag;
30 use Friendica\Network\HTTPException;
31 use Friendica\Protocol\ActivityPub;
32
33 /**
34  * Outputs the permission tooltip HTML content for the provided item, photo or event id.
35  */
36 class PermissionTooltip extends \Friendica\BaseModule
37 {
38         protected function rawContent(array $request = [])
39         {
40                 $type = $this->parameters['type'];
41                 $referenceId = $this->parameters['id'];
42
43                 $expectedTypes = ['item', 'photo', 'event'];
44                 if (!in_array($type, $expectedTypes)) {
45                         throw new HTTPException\BadRequestException(DI::l10n()->t('Wrong type "%s", expected one of: %s', $type, implode(', ', $expectedTypes)));
46                 }
47
48                 $condition = ['id' => $referenceId];
49                 if ($type == 'item') {
50                         $fields = ['uid', 'psid', 'private', 'uri-id'];
51                         $model = Post::selectFirst($fields, $condition);
52                 } else {
53                         $fields = ['uid', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid'];
54                         $model = DBA::selectFirst($type, $fields, $condition);
55                         $model['allow_cid'] = DI::aclFormatter()->expand($model['allow_cid']);
56                         $model['allow_gid'] = DI::aclFormatter()->expand($model['allow_gid']);
57                         $model['deny_cid']  = DI::aclFormatter()->expand($model['deny_cid']);
58                         $model['deny_gid']  = DI::aclFormatter()->expand($model['deny_gid']);
59                 }
60
61                 if (!DBA::isResult($model)) {
62                         throw new HttpException\NotFoundException(DI::l10n()->t('Model not found'));
63                 }
64
65                 if (isset($model['psid'])) {
66                         $permissionSet = DI::permissionSet()->selectOneById($model['psid'], $model['uid']);
67                         $model['allow_cid'] = $permissionSet->allow_cid;
68                         $model['allow_gid'] = $permissionSet->allow_gid;
69                         $model['deny_cid']  = $permissionSet->deny_cid;
70                         $model['deny_gid']  = $permissionSet->deny_gid;
71                 }
72
73                 // Kept for backwards compatiblity
74                 Hook::callAll('lockview_content', $model);
75
76                 if ($type == 'item') {
77                         $receivers = $this->fetchReceivers($model['uri-id']);
78                 } else {
79                         $receivers = '';
80                 }
81
82                 if ($model['uid'] != local_user() ||
83                         empty($model['allow_cid'])
84                         && empty($model['allow_gid'])
85                         && empty($model['deny_cid'])
86                         && empty($model['deny_gid'])
87                         && empty($receivers))
88                 {
89                         echo DI::l10n()->t('Remote privacy information not available.');
90                         exit;
91                 }
92
93                 $allowed_users  = $model['allow_cid'];
94                 $allowed_groups = $model['allow_gid'];
95                 $deny_users     = $model['deny_cid'];
96                 $deny_groups    = $model['deny_gid'];
97
98                 $o = DI::l10n()->t('Visible to:') . '<br />';
99                 $l = [];
100
101                 if (count($allowed_groups)) {
102                         $key = array_search(Group::FOLLOWERS, $allowed_groups);
103                         if ($key !== false) {
104                                 $l[] = '<b>' . DI::l10n()->t('Followers') . '</b>';
105                                 unset($allowed_groups[$key]);
106                         }
107
108                         $key = array_search(Group::MUTUALS, $allowed_groups);
109                         if ($key !== false) {
110                                 $l[] = '<b>' . DI::l10n()->t('Mutuals') . '</b>';
111                                 unset($allowed_groups[$key]);
112                         }
113
114                         foreach (DI::dba()->selectToArray('group', ['name'], ['id' => $allowed_groups]) as $group) {
115                                 $l[] = '<b>' . $group['name'] . '</b>';
116                         }
117                 }
118
119                 foreach (DI::dba()->selectToArray('contact', ['name'], ['id' => $allowed_users]) as $contact) {
120                         $l[] = $contact['name'];
121                 }
122
123                 if (count($deny_groups)) {
124                         $key = array_search(Group::FOLLOWERS, $deny_groups);
125                         if ($key !== false) {
126                                 $l[] = '<b><strike>' . DI::l10n()->t('Followers') . '</strike></b>';
127                                 unset($deny_groups[$key]);
128                         }
129
130                         $key = array_search(Group::MUTUALS, $deny_groups);
131                         if ($key !== false) {
132                                 $l[] = '<b><strike>' . DI::l10n()->t('Mutuals') . '</strike></b>';
133                                 unset($deny_groups[$key]);
134                         }
135
136                         foreach (DI::dba()->selectToArray('group', ['name'], ['id' => $allowed_groups]) as $group) {
137                                 $l[] = '<b><strike>' . $group['name'] . '</strike></b>';
138                         }
139                 }
140
141                 foreach (DI::dba()->selectToArray('contact', ['name'], ['id' => $deny_users]) as $contact) {
142                         $l[] = '<strike>' . $contact['name'] . '</strike>';
143                 }
144
145                 if (!empty($l)) {
146                         echo $o . implode(', ', $l);
147                 } else {
148                         echo $o . $receivers;
149                 }
150
151                 exit();
152         }
153
154         /**
155          * Fetch a list of receivers
156          *
157          * @param int $uriId
158          * @return string 
159          */
160         private function fetchReceivers(int $uriId):string
161         {
162                 // We only fetch "to" and "cc", because "bcc" should never be displayed
163                 $receivers = [];
164                 foreach (Tag::getByURIId($uriId, [Tag::TO, Tag::CC]) as $receiver) {
165                         if ($receiver['url'] == ActivityPub::PUBLIC_COLLECTION) {
166                                 $receivers[$receiver['type']][] = DI::l10n()->t('Public');
167                         } else {
168                                 $apcontact = DBA::selectFirst('apcontact', ['name'], ['followers' => $receiver['url']]);
169                                 if (!empty($apcontact['name'])) {
170                                         $receivers[$receiver['type']][] = DI::l10n()->t('Followers (%s)', $apcontact['name']);
171                                 } else {
172                                         $receivers[$receiver['type']][] = $receiver['name'];
173                                 }
174                         }
175                 }
176
177                 $output = '';
178
179                 foreach ($receivers as $type => $receiver) {
180                         $max = DI::config()->get('system', 'max_receivers');
181                         $total = count($receiver);
182                         if ($total > $max) {
183                                 $receiver = array_slice($receiver, 0, $max);
184                                 $receiver[] = DI::l10n()->t('%d more', $total - $max);
185                         }
186                         switch ($type) {
187                                 case Tag::TO:
188                                         $output .= DI::l10n()->t('<b>To:</b> %s<br>', implode(', ', $receiver));
189                                         break;
190                                 case Tag::CC:
191                                         $output .= DI::l10n()->t('<b>CC:</b> %s<br>', implode(', ', $receiver));
192                                         break;
193                         }
194                 }
195
196                 return $output;
197         }
198 }