]> git.mxchange.org Git - friendica.git/blob - mod/delegate.php
e9760fa3f5cafd0c06d2c991576d5c06cf9a32b0
[friendica.git] / mod / delegate.php
1 <?php
2 /**
3  * @file mod/delegate.php
4  */
5
6 use Friendica\App;
7 use Friendica\Core\L10n;
8 use Friendica\Core\System;
9 use Friendica\Database\DBA;
10 use Friendica\Model\User;
11
12 require_once 'mod/settings.php';
13
14 function delegate_init(App $a)
15 {
16         return settings_init($a);
17 }
18
19 function delegate_post(App $a)
20 {
21         if (!local_user()) {
22                 return;
23         }
24
25         if (count($a->user) && x($a->user, 'uid') && $a->user['uid'] != local_user()) {
26                 notice(L10n::t('Permission denied.') . EOL);
27                 return;
28         }
29
30         check_form_security_token_redirectOnErr('/delegate', 'delegate');
31
32         $parent_uid = defaults($_POST, 'parent_user', 0);
33         $parent_password = defaults($_POST, 'parent_password', '');
34
35         if ($parent_uid != 0) {
36                 $user = DBA::selectFirst('user', ['nickname'], ['uid' => $parent_uid]);
37                 if (!DBA::isResult($user)) {
38                         notice(L10n::t('Parent user not found.') . EOL);
39                         return;
40                 }
41
42                 $success = User::authenticate($user['nickname'], trim($parent_password));
43                 if (!$success) {
44                         notice(L10n::t('Permission denied.') . EOL);
45                         return;
46                 }
47         }
48
49         DBA::update('user', ['parent-uid' => $parent_uid], ['uid' => local_user()]);
50 }
51
52 function delegate_content(App $a)
53 {
54         if (!local_user()) {
55                 notice(L10n::t('Permission denied.') . EOL);
56                 return;
57         }
58
59         if ($a->argc > 2 && $a->argv[1] === 'add' && intval($a->argv[2])) {
60                 // delegated admins can view but not change delegation permissions
61                 if (x($_SESSION, 'submanage')) {
62                         goaway(System::baseUrl() . '/delegate');
63                 }
64
65                 $user_id = $a->argv[2];
66
67                 $user = DBA::selectFirst('user', ['nickname'], ['uid' => $user_id]);
68                 if (DBA::isResult($user)) {
69                         $condition = [
70                                 'uid' => local_user(),
71                                 'nurl' => normalise_link(System::baseUrl() . '/profile/' . $user['nickname'])
72                         ];
73                         if (DBA::exists('contact', $condition)) {
74                                 DBA::insert('manage', ['uid' => $user_id, 'mid' => local_user()]);
75                         }
76                 }
77                 goaway(System::baseUrl() . '/delegate');
78         }
79
80         if ($a->argc > 2 && $a->argv[1] === 'remove' && intval($a->argv[2])) {
81                 // delegated admins can view but not change delegation permissions
82                 if (x($_SESSION, 'submanage')) {
83                         goaway(System::baseUrl() . '/delegate');
84                 }
85
86                 DBA::delete('manage', ['uid' => $a->argv[2], 'mid' => local_user()]);
87                 goaway(System::baseUrl() . '/delegate');
88         }
89
90         // find everybody that currently has delegated management to this account/page
91         $delegates = [];
92         $r = q("SELECT * FROM `user` WHERE `uid` IN (SELECT `uid` FROM `manage` WHERE `mid` = %d)",
93                 intval(local_user())
94         );
95         if (DBA::isResult($r)) {
96                 $delegates = $r;
97         }
98
99         $uids = [];
100         foreach ($delegates as $rr) {
101                 $uids[] = $rr['uid'];
102         }
103
104         // find every contact who might be a candidate for delegation
105         $potentials = [];
106
107         $r = q("SELECT `nurl`
108                 FROM `contact`
109                 WHERE `self` = 0
110                 AND SUBSTRING_INDEX(`nurl`, '/', 3) = '%s'
111                 AND `uid` = %d
112                 AND `network` = '%s' ",
113                 dbesc(normalise_link(System::baseUrl())),
114                 intval(local_user()),
115                 dbesc(NETWORK_DFRN)
116         );
117         if (DBA::isResult($r)) {
118                 $nicknames = [];
119                 foreach ($r as $rr) {
120                         $nicknames[] = "'" . dbesc(basename($rr['nurl'])) . "'";
121                 }
122
123                 $nicks = implode(',', $nicknames);
124
125                 // get user records for all potential page delegates who are not already delegates or managers
126                 $r = q("SELECT `uid`, `username`, `nickname` FROM `user` WHERE `nickname` IN ($nicks)");
127                 if (DBA::isResult($r)) {
128                         foreach ($r as $rr) {
129                                 if (!in_array($rr['uid'], $uids)) {
130                                         $potentials[] = $rr;
131                                 }
132                         }
133                 }
134         }
135
136         settings_init($a);
137
138         $user = DBA::selectFirst('user', ['parent-uid', 'email'], ['uid' => local_user()]);
139
140         $parent_user = null;
141
142         if (DBA::isResult($user)) {
143                 if (!DBA::exists('user', ['parent-uid' => local_user()])) {
144                         $parent_uid = $user['parent-uid'];
145                         $parents = [0 => L10n::t('No parent user')];
146
147                         $fields = ['uid', 'username', 'nickname'];
148                         $condition = ['email' => $user['email'], 'verified' => true, 'blocked' => false, 'parent-uid' => 0];
149                         $parent_users = DBA::select('user', $fields, $condition);
150                         while ($parent = DBA::fetch($parent_users)) {
151                                 if ($parent['uid'] != local_user()) {
152                                         $parents[$parent['uid']] = sprintf('%s (%s)', $parent['username'], $parent['nickname']);
153                                 }
154                         }
155                         $parent_user = ['parent_user', '', $parent_uid, '', $parents];
156                 }
157         }
158
159         if (!is_null($parent_user)) {
160                 $parent_password = ['parent_password', L10n::t('Parent Password:'), '', L10n::t('Please enter the password of the parent account to legitimize your request.')];
161         }
162
163         $o = replace_macros(get_markup_template('delegate.tpl'), [
164                 '$form_security_token' => get_form_security_token('delegate'),
165                 '$parent_header' => L10n::t('Parent User'),
166                 '$parent_user' => $parent_user,
167                 '$parent_password' => $parent_password,
168                 '$parent_desc' => L10n::t('Parent users have total control about this account, including the account settings. Please double check whom you give this access.'),
169                 '$submit' => L10n::t('Save Settings'),
170                 '$header' => L10n::t('Delegate Page Management'),
171                 '$delegates_header' => L10n::t('Delegates'),
172                 '$base' => System::baseUrl(),
173                 '$desc' => L10n::t('Delegates are able to manage all aspects of this account/page except for basic account settings. Please do not delegate your personal account to anybody that you do not trust completely.'),
174                 '$head_delegates' => L10n::t('Existing Page Delegates'),
175                 '$delegates' => $delegates,
176                 '$head_potentials' => L10n::t('Potential Delegates'),
177                 '$potentials' => $potentials,
178                 '$remove' => L10n::t('Remove'),
179                 '$add' => L10n::t('Add'),
180                 '$none' => L10n::t('No entries.')
181         ]);
182
183
184         return $o;
185 }