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