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