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