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