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