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