]> git.mxchange.org Git - friendica.git/blob - src/Module/Delegation.php
fix
[friendica.git] / src / Module / Delegation.php
1 <?php
2
3 namespace Friendica\Module;
4
5 use Friendica\BaseModule;
6 use Friendica\App\Authentication;
7 use Friendica\Core\Hook;
8 use Friendica\Core\L10n;
9 use Friendica\Core\Renderer;
10 use Friendica\Core\Session;
11 use Friendica\Database\DBA;
12 use Friendica\Model\Contact;
13 use Friendica\Model\User;
14 use Friendica\Network\HTTPException\ForbiddenException;
15
16 /**
17  * Switches current user between delegates/parent user
18  */
19 class Delegation extends BaseModule
20 {
21         public static function post(array $parameters = [])
22         {
23                 if (!local_user()) {
24                         return;
25                 }
26
27                 $uid = local_user();
28                 $orig_record = self::getApp()->user;
29
30                 if (Session::get('submanage')) {
31                         $user = User::getById(Session::get('submanage'));
32                         if (DBA::isResult($user)) {
33                                 $uid = intval($user['uid']);
34                                 $orig_record = $user;
35                         }
36                 }
37
38                 $identity = intval($_POST['identity'] ?? 0);
39                 if (!$identity) {
40                         return;
41                 }
42
43                 $limited_id = 0;
44                 $original_id = $uid;
45
46                 $manages = DBA::selectToArray('manage', ['mid'], ['uid' => $uid]);
47                 foreach ($manages as $manage) {
48                         if ($identity == $manage['mid']) {
49                                 $limited_id = $manage['mid'];
50                                 break;
51                         }
52                 }
53
54                 if ($limited_id) {
55                         $user = User::getById($limited_id);
56                 } else {
57                         // Check if the target user is one of our children
58                         $user = DBA::selectFirst('user', [], ['uid' => $identity, 'parent-uid' => $orig_record['uid']]);
59
60                         // Check if the target user is one of our siblings
61                         if (!DBA::isResult($user) && ($orig_record['parent-uid'] != 0)) {
62                                 $user = DBA::selectFirst('user', [], ['uid' => $identity, 'parent-uid' => $orig_record['parent-uid']]);
63                         }
64
65                         // Check if it's our parent or our own user
66                         if (!DBA::isResult($user)
67                                 && (
68                                         $orig_record['parent-uid'] != 0 && $orig_record['parent-uid'] == $identity
69                                         ||
70                                         $orig_record['uid'] != 0 && $orig_record['uid'] == $identity
71                                 )
72                         ) {
73                                 $user = User::getById($identity);
74                         }
75                 }
76
77                 if (!DBA::isResult($user)) {
78                         return;
79                 }
80
81                 Session::clear();
82
83                 /** @var Authentication $authentication */
84                 $authentication = self::getClass(Authentication::class);
85                 $authentication->setForUser(self::getApp(), $user, true, true);
86
87                 if ($limited_id) {
88                         Session::set('submanage', $original_id);
89                 }
90
91                 $ret = [];
92                 Hook::callAll('home_init', $ret);
93
94                 self::getApp()->internalRedirect('profile/' . self::getApp()->user['nickname']);
95                 // NOTREACHED
96         }
97
98         public static function content(array $parameters = [])
99         {
100                 if (!local_user()) {
101                         throw new ForbiddenException(L10n::t('Permission denied.'));
102                 }
103
104                 $identities = self::getApp()->identities;
105
106                 //getting additinal information for each identity
107                 foreach ($identities as $key => $identity) {
108                         $thumb = Contact::selectFirst(['thumb'], ['uid' => $identity['uid'], 'self' => true]);
109                         if (!DBA::isResult($thumb)) {
110                                 continue;
111                         }
112
113                         $identities[$key]['thumb'] = $thumb['thumb'];
114
115                         $identities[$key]['selected'] = ($identity['nickname'] === self::getApp()->user['nickname']);
116
117                         $condition = ["`uid` = ? AND `msg` != '' AND NOT (`type` IN (?, ?)) AND NOT `seen`", $identity['uid'], NOTIFY_INTRO, NOTIFY_MAIL];
118                         $params = ['distinct' => true, 'expression' => 'parent'];
119                         $notifications = DBA::count('notify', $condition, $params);
120
121                         $params = ['distinct' => true, 'expression' => 'convid'];
122                         $notifications += DBA::count('mail', ['uid' => $identity['uid'], 'seen' => false], $params);
123
124                         $notifications += DBA::count('intro', ['blocked' => false, 'ignore' => false, 'uid' => $identity['uid']]);
125
126                         $identities[$key]['notifications'] = $notifications;
127                 }
128
129                 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('delegation.tpl'), [
130                         '$title'      => L10n::t('Manage Identities and/or Pages'),
131                         '$desc'       => L10n::t('Toggle between different identities or community/group pages which share your account details or which you have been granted "manage" permissions'),
132                         '$choose'     => L10n::t('Select an identity to manage: '),
133                         '$identities' => $identities,
134                         '$submit'     => L10n::t('Submit'),
135                 ]);
136
137                 return $o;
138         }
139 }