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