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