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