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