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