]> git.mxchange.org Git - friendica.git/blob - mod/delegate.php
Merge pull request #4229 from annando/database
[friendica.git] / mod / delegate.php
1 <?php
2
3 use Friendica\App;
4 use Friendica\Core\System;
5 use Friendica\Database\DBM;
6
7 require_once 'mod/settings.php';
8
9 function delegate_init(App $a)
10 {
11         return settings_init($a);
12 }
13
14 function delegate_content(App $a)
15 {
16         if (!local_user()) {
17                 notice(t('Permission denied.') . EOL);
18                 return;
19         }
20
21         if ($a->argc > 2 && $a->argv[1] === 'add' && intval($a->argv[2])) {
22                 // delegated admins can view but not change delegation permissions
23                 if (x($_SESSION, 'submanage')) {
24                         goaway(System::baseUrl() . '/delegate');
25                 }
26
27                 $user_id = $a->argv[2];
28
29                 $user = dba::selectFirst('user', ['nickname'], ['uid' => $user_id]);
30                 if (DBM::is_result($user)) {
31                         $condition = [
32                                 'uid' => local_user(),
33                                 'nurl' => normalise_link(System::baseUrl() . '/profile/' . $user['nickname'])
34                         ];
35                         if (dba::exists('contact', $condition)) {
36                                 dba::insert('manage', ['uid' => $user_id, 'mid' => local_user()]);
37                         }
38                 }
39                 goaway(System::baseUrl() . '/delegate');
40         }
41
42         if ($a->argc > 2 && $a->argv[1] === 'remove' && 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                 dba::delete('manage', ['uid' => $a->argv[2], 'mid' => local_user()]);
49                 goaway(System::baseUrl() . '/delegate');
50         }
51
52         // These people can manage this account/page with full privilege
53         $full_managers = [];
54         $r = q("SELECT * FROM `user` WHERE `email` = '%s' AND `password` = '%s' ",
55                 dbesc($a->user['email']),
56                 dbesc($a->user['password'])
57         );
58         if (DBM::is_result($r)) {
59                 $full_managers = $r;
60         }
61
62         // find everybody that currently has delegated management to this account/page
63         $delegates = [];
64         $r = q("SELECT * FROM `user` WHERE `uid` IN (SELECT `uid` FROM `manage` WHERE `mid` = %d)",
65                 intval(local_user())
66         );
67         if (DBM::is_result($r)) {
68                 $delegates = $r;
69         }
70
71         $uids = [];
72         foreach ($full_managers as $rr) {
73                 $uids[] = $rr['uid'];
74         }
75
76         foreach ($delegates as $rr) {
77                 $uids[] = $rr['uid'];
78         }
79
80         // find every contact who might be a candidate for delegation
81
82         $r = q("SELECT `nurl`
83                 FROM `contact`
84                 WHERE `self` = 0
85                 AND SUBSTRING_INDEX(`nurl`, '/', 3) = '%s'
86                 AND `uid` = %d
87                 AND `network` = '%s' ",
88                 dbesc(normalise_link(System::baseUrl())),
89                 intval(local_user()),
90                 dbesc(NETWORK_DFRN)
91         );
92         if (!DBM::is_result($r)) {
93                 notice(t('No potential page delegates located.') . EOL);
94                 return;
95         }
96
97         $nicknames = [];
98         foreach ($r as $rr) {
99                 $nicknames[] = "'" . dbesc(basename($rr['nurl'])) . "'";
100         }
101
102         $potentials = [];
103
104         $nicks = implode(',', $nicknames);
105
106         // get user records for all potential page delegates who are not already delegates or managers
107         $r = q("SELECT `uid`, `username`, `nickname` FROM `user` WHERE `nickname` IN ($nicks)");
108         if (DBM::is_result($r)) {
109                 foreach ($r as $rr) {
110                         if (!in_array($rr['uid'], $uids)) {
111                                 $potentials[] = $rr;
112                         }
113                 }
114         }
115
116         settings_init($a);
117
118         $o = replace_macros(get_markup_template('delegate.tpl'), [
119                 '$header' => t('Delegate Page Management'),
120                 '$base' => System::baseUrl(),
121                 '$desc' => 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.'),
122                 '$head_managers' => t('Existing Page Managers'),
123                 '$managers' => $full_managers,
124                 '$head_delegates' => t('Existing Page Delegates'),
125                 '$delegates' => $delegates,
126                 '$head_potentials' => t('Potential Delegates'),
127                 '$potentials' => $potentials,
128                 '$remove' => t('Remove'),
129                 '$add' => t('Add'),
130                 '$none' => t('No entries.')
131         ]);
132
133
134         return $o;
135 }