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