]> git.mxchange.org Git - friendica.git/blob - mod/delegate.php
page delegation
[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 `uid` = %d AND `email` = '%s' AND `password` = '%s' LIMIT 1",
58                 intval(local_user()),
59                 dbesc($a->user['email']),
60                 dbesc($a->user['password'])
61         );
62         if(count($r))
63                 $full_managers = $r;
64
65         $delegates = array();
66
67         // find everybody that currently has delegated management to this account/page
68
69         $r = q("select * from user where uid in ( select uid from manage where mid = %d ) ",
70                 intval(local_user())
71         );
72
73         if(count($r))
74                 $delegates = $r;
75
76         $uids = array();
77
78         if(count($full_managers))
79                 foreach($full_managers as $rr)
80                         $uids[] = $rr['uid'];
81
82         if(count($delegates))
83                 foreach($delegates as $rr)
84                         $uids[] = $rr['uid'];
85
86         // find every contact who might be a candidate for delegation
87
88         $r = q("select nurl from contact where substring_index(contact.nurl,'/',3) = '%s' 
89                 and contact.uid = %d and contact.self = 0 and network = '%s' ",
90                 dbesc($a->get_baseurl()),
91                 intval(local_user()),
92                 dbesc(NETWORK_DFRN)
93         ); 
94
95         if(! count($r)) {
96                 notice( t('No potential page delegates located.') . EOL);
97                 return;
98         }
99
100         $nicknames = array();
101
102         if(count($r)) {
103                 foreach($r as $rr) {
104                         $nicknames[] = "'" . dbesc(basename($rr['nurl'])) . "'";
105                 }
106         }
107
108         $potentials = array();
109
110         $nicks = implode(',',$nicknames);
111
112         // get user records for all potential page delegates who are not already delegates or managers
113
114         $r = q("select `uid`, `username`, `nickname` from user where nickname in ( $nicks )");
115
116         if(count($r))
117                 foreach($r as $rr)
118                         if(! in_array($rr['uid'],$uids))
119                                 $potentials[] = $rr;
120
121         $o = replace_macros(get_markup_template('delegate.tpl'),array(
122                 '$header' => t('Delegate Page Management'),
123                 '$base' => $a->get_baseurl(),
124                 '$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.'),
125                 '$head_managers' => t('Existing Page Managers'),
126                 '$managers' => $full_managers,
127                 '$head_delegates' => t('Existing Page Delegates'),
128                 '$delegates' => $delegates,
129                 '$head_potentials' => t('Potential Delegates'),
130                 '$potentials' => $potentials,
131                 '$remove' => t('Remove'),
132                 '$add' => t('Add'),
133                 '$none' => t('No entries.')
134         ));
135
136
137         return $o;
138
139
140 }