]> git.mxchange.org Git - friendica.git/blob - src/Module/Delegation.php
Added type hints
[friendica.git] / src / Module / Delegation.php
1 <?php
2
3 namespace Friendica\Module;
4
5 use Friendica\BaseModule;
6 use Friendica\Core\Hook;
7 use Friendica\Core\L10n;
8 use Friendica\Core\Renderer;
9 use Friendica\Core\Session;
10 use Friendica\Database\DBA;
11 use Friendica\DI;
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 = DI::app()->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                 DI::auth()->setForUser(DI::app(), $user, true, true);
84
85                 if ($limited_id) {
86                         Session::set('submanage', $original_id);
87                 }
88
89                 $ret = [];
90                 Hook::callAll('home_init', $ret);
91
92                 DI::baseUrl()->redirect('profile/' . DI::app()->user['nickname']);
93                 // NOTREACHED
94         }
95
96         public static function content(array $parameters = [])
97         {
98                 if (!local_user()) {
99                         throw new ForbiddenException(L10n::t('Permission denied.'));
100                 }
101
102                 $identities = DI::app()->identities;
103
104                 //getting additinal information for each identity
105                 foreach ($identities as $key => $identity) {
106                         $thumb = Contact::selectFirst(['thumb'], ['uid' => $identity['uid'], 'self' => true]);
107                         if (!DBA::isResult($thumb)) {
108                                 continue;
109                         }
110
111                         $identities[$key]['thumb'] = $thumb['thumb'];
112
113                         $identities[$key]['selected'] = ($identity['nickname'] === DI::app()->user['nickname']);
114
115                         $condition = ["`uid` = ? AND `msg` != '' AND NOT (`type` IN (?, ?)) AND NOT `seen`", $identity['uid'], NOTIFY_INTRO, NOTIFY_MAIL];
116                         $params = ['distinct' => true, 'expression' => 'parent'];
117                         $notifications = DBA::count('notify', $condition, $params);
118
119                         $params = ['distinct' => true, 'expression' => 'convid'];
120                         $notifications += DBA::count('mail', ['uid' => $identity['uid'], 'seen' => false], $params);
121
122                         $notifications += DBA::count('intro', ['blocked' => false, 'ignore' => false, 'uid' => $identity['uid']]);
123
124                         $identities[$key]['notifications'] = $notifications;
125                 }
126
127                 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('delegation.tpl'), [
128                         '$title'      => L10n::t('Manage Identities and/or Pages'),
129                         '$desc'       => L10n::t('Toggle between different identities or community/group pages which share your account details or which you have been granted "manage" permissions'),
130                         '$choose'     => L10n::t('Select an identity to manage: '),
131                         '$identities' => $identities,
132                         '$submit'     => L10n::t('Submit'),
133                 ]);
134
135                 return $o;
136         }
137 }