]> git.mxchange.org Git - friendica.git/blob - src/Module/PermissionTooltip.php
59478b326ccf069a1f806c6296707062330b7412
[friendica.git] / src / Module / PermissionTooltip.php
1 <?php
2
3 namespace Friendica\Module;
4
5 use Friendica\Core\Hook;
6 use Friendica\Database\DBA;
7 use Friendica\DI;
8 use Friendica\Model\Item;
9 use Friendica\Model\Group;
10 use Friendica\Network\HTTPException;
11
12 /**
13  * Outputs the permission tooltip HTML content for the provided item, photo or event id.
14  */
15 class PermissionTooltip extends \Friendica\BaseModule
16 {
17         public static function rawContent(array $parameters = [])
18         {
19                 parent::rawContent($parameters); // TODO: Change the autogenerated stub
20
21                 $type = $parameters['type'];
22                 $referenceId = $parameters['id'];
23
24                 $expectedTypes = ['item', 'photo', 'event'];
25                 if (!in_array($type, $expectedTypes)) {
26                         throw new HTTPException\BadRequestException(DI::l10n()->t('Wrong type "%s", expected one of: %s', $type, implode(', ', $expectedTypes)));
27                 }
28
29                 $condition = ['id' => $referenceId];
30                 if ($type == 'item') {
31                         $fields = ['uid', 'psid', 'private'];
32                         $model = Item::selectFirst($fields, $condition);
33                 } else {
34                         $fields = ['uid', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid'];
35                         $model = DBA::selectFirst($type, $fields, $condition);
36                 }
37
38                 if (!DBA::isResult($model)) {
39                         throw new HttpException\NotFoundException(DI::l10n()->t('Model not found'));
40                 }
41
42                 if (isset($model['psid'])) {
43                         $permissionSet = DI::permissionSet()->selectFirst(['id' => $model['psid']]);
44                         $model['allow_cid'] = $permissionSet->allow_cid;
45                         $model['allow_gid'] = $permissionSet->allow_gid;
46                         $model['deny_cid']  = $permissionSet->deny_cid;
47                         $model['deny_gid']  = $permissionSet->deny_gid;
48                 }
49
50                 // Kept for backwards compatiblity
51                 Hook::callAll('lockview_content', $model);
52
53                 if ($model['uid'] != local_user() ||
54                         isset($model['private'])
55                         && $model['private'] == Item::PRIVATE
56                         && empty($model['allow_cid'])
57                         && empty($model['allow_gid'])
58                         && empty($model['deny_cid'])
59                         && empty($model['deny_gid']))
60                 {
61                         echo DI::l10n()->t('Remote privacy information not available.');
62                         exit;
63                 }
64
65                 $aclFormatter = DI::aclFormatter();
66
67                 $allowed_users  = $aclFormatter->expand($model['allow_cid']);
68                 $allowed_groups = $aclFormatter->expand($model['allow_gid']);
69                 $deny_users     = $aclFormatter->expand($model['deny_cid']);
70                 $deny_groups    = $aclFormatter->expand($model['deny_gid']);
71
72                 $o = DI::l10n()->t('Visible to:') . '<br />';
73                 $l = [];
74
75                 if (count($allowed_groups)) {
76                         $key = array_search(Group::FOLLOWERS, $allowed_groups);
77                         if ($key !== false) {
78                                 $l[] = '<b>' . DI::l10n()->t('Followers') . '</b>';
79                                 unset($allowed_groups[$key]);
80                         }
81
82                         $key = array_search(Group::MUTUALS, $allowed_groups);
83                         if ($key !== false) {
84                                 $l[] = '<b>' . DI::l10n()->t('Mutuals') . '</b>';
85                                 unset($allowed_groups[$key]);
86                         }
87
88                         foreach (DI::dba()->selectToArray('group', ['name'], ['id' => $allowed_groups]) as $group) {
89                                 $l[] = '<b>' . $group['name'] . '</b>';
90                         }
91                 }
92
93                 foreach (DI::dba()->selectToArray('contact', ['name'], ['id' => $allowed_users]) as $contact) {
94                         $l[] = $contact['name'];
95                 }
96
97                 if (count($deny_groups)) {
98                         $key = array_search(Group::FOLLOWERS, $deny_groups);
99                         if ($key !== false) {
100                                 $l[] = '<b><strike>' . DI::l10n()->t('Followers') . '</strike></b>';
101                                 unset($deny_groups[$key]);
102                         }
103
104                         $key = array_search(Group::MUTUALS, $deny_groups);
105                         if ($key !== false) {
106                                 $l[] = '<b><strike>' . DI::l10n()->t('Mutuals') . '</strike></b>';
107                                 unset($deny_groups[$key]);
108                         }
109
110                         foreach (DI::dba()->selectToArray('group', ['name'], ['id' => $allowed_groups]) as $group) {
111                                 $l[] = '<b><strike>' . $group['name'] . '</strike></b>';
112                         }
113                 }
114
115                 foreach (DI::dba()->selectToArray('contact', ['name'], ['id' => $deny_users]) as $contact) {
116                         $l[] = '<strike>' . $contact['name'] . '</strike>';
117                 }
118
119                 echo $o . implode(', ', $l);
120                 exit();
121         }
122 }