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