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