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