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