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