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