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