]> git.mxchange.org Git - friendica.git/blob - src/Module/PermissionTooltip.php
Merge pull request #10913 from MrPetovan/bug/10903-profile-edit
[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\Group;
9 use Friendica\Model\Item;
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                         $model['allow_cid'] = DI::aclFormatter()->expand($model['allow_cid']);
36                         $model['allow_gid'] = DI::aclFormatter()->expand($model['allow_gid']);
37                         $model['deny_cid']  = DI::aclFormatter()->expand($model['deny_cid']);
38                         $model['deny_gid']  = DI::aclFormatter()->expand($model['deny_gid']);
39                 }
40
41                 if (!DBA::isResult($model)) {
42                         throw new HttpException\NotFoundException(DI::l10n()->t('Model not found'));
43                 }
44
45                 if (isset($model['psid'])) {
46                         $permissionSet = DI::permissionSet()->selectOneById($model['psid'], $model['uid']);
47                         $model['allow_cid'] = $permissionSet->allow_cid;
48                         $model['allow_gid'] = $permissionSet->allow_gid;
49                         $model['deny_cid']  = $permissionSet->deny_cid;
50                         $model['deny_gid']  = $permissionSet->deny_gid;
51                 }
52
53                 // Kept for backwards compatiblity
54                 Hook::callAll('lockview_content', $model);
55
56                 if ($model['uid'] != local_user() ||
57                         isset($model['private'])
58                         && $model['private'] == Item::PRIVATE
59                         && empty($model['allow_cid'])
60                         && empty($model['allow_gid'])
61                         && empty($model['deny_cid'])
62                         && empty($model['deny_gid']))
63                 {
64                         echo DI::l10n()->t('Remote privacy information not available.');
65                         exit;
66                 }
67
68                 $allowed_users  = $model['allow_cid'];
69                 $allowed_groups = $model['allow_gid'];
70                 $deny_users     = $model['deny_cid'];
71                 $deny_groups    = $model['deny_gid'];
72
73                 $o = DI::l10n()->t('Visible to:') . '<br />';
74                 $l = [];
75
76                 if (count($allowed_groups)) {
77                         $key = array_search(Group::FOLLOWERS, $allowed_groups);
78                         if ($key !== false) {
79                                 $l[] = '<b>' . DI::l10n()->t('Followers') . '</b>';
80                                 unset($allowed_groups[$key]);
81                         }
82
83                         $key = array_search(Group::MUTUALS, $allowed_groups);
84                         if ($key !== false) {
85                                 $l[] = '<b>' . DI::l10n()->t('Mutuals') . '</b>';
86                                 unset($allowed_groups[$key]);
87                         }
88
89                         foreach (DI::dba()->selectToArray('group', ['name'], ['id' => $allowed_groups]) as $group) {
90                                 $l[] = '<b>' . $group['name'] . '</b>';
91                         }
92                 }
93
94                 foreach (DI::dba()->selectToArray('contact', ['name'], ['id' => $allowed_users]) as $contact) {
95                         $l[] = $contact['name'];
96                 }
97
98                 if (count($deny_groups)) {
99                         $key = array_search(Group::FOLLOWERS, $deny_groups);
100                         if ($key !== false) {
101                                 $l[] = '<b><strike>' . DI::l10n()->t('Followers') . '</strike></b>';
102                                 unset($deny_groups[$key]);
103                         }
104
105                         $key = array_search(Group::MUTUALS, $deny_groups);
106                         if ($key !== false) {
107                                 $l[] = '<b><strike>' . DI::l10n()->t('Mutuals') . '</strike></b>';
108                                 unset($deny_groups[$key]);
109                         }
110
111                         foreach (DI::dba()->selectToArray('group', ['name'], ['id' => $allowed_groups]) as $group) {
112                                 $l[] = '<b><strike>' . $group['name'] . '</strike></b>';
113                         }
114                 }
115
116                 foreach (DI::dba()->selectToArray('contact', ['name'], ['id' => $deny_users]) as $contact) {
117                         $l[] = '<strike>' . $contact['name'] . '</strike>';
118                 }
119
120                 echo $o . implode(', ', $l);
121                 exit();
122         }
123 }