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