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