4 use Friendica\Core\System;
5 use Friendica\Database\DBM;
7 require_once('mod/settings.php');
9 function delegate_init(App $a) {
10 return settings_init($a);
13 function delegate_content(App $a) {
16 notice( t('Permission denied.') . EOL);
20 if ($a->argc > 2 && $a->argv[1] === 'add' && intval($a->argv[2])) {
22 // delegated admins can view but not change delegation permissions
24 if (x($_SESSION,'submanage') && intval($_SESSION['submanage'])) {
25 goaway(System::baseUrl() . '/delegate');
30 $r = q("select `nickname` from user where uid = %d limit 1",
33 if (DBM::is_result($r)) {
34 $r = q("select id from contact where uid = %d and nurl = '%s' limit 1",
36 dbesc(normalise_link(System::baseUrl() . '/profile/' . $r[0]['nickname']))
38 if (DBM::is_result($r)) {
39 dba::insert('manage', array('uid' => $a->argv[2], 'mid' => local_user()));
42 goaway(System::baseUrl() . '/delegate');
45 if ($a->argc > 2 && $a->argv[1] === 'remove' && intval($a->argv[2])) {
47 // delegated admins can view but not change delegation permissions
48 if (x($_SESSION,'submanage') && intval($_SESSION['submanage'])) {
49 goaway(System::baseUrl() . '/delegate');
52 q("DELETE FROM `manage` WHERE `uid` = %d AND `mid` = %d LIMIT 1",
56 goaway(System::baseUrl() . '/delegate');
60 $full_managers = array();
62 // These people can manage this account/page with full privilege
64 $r = q("SELECT * FROM `user` WHERE `email` = '%s' AND `password` = '%s' ",
65 dbesc($a->user['email']),
66 dbesc($a->user['password'])
68 if (DBM::is_result($r))
73 // find everybody that currently has delegated management to this account/page
75 $r = q("select * from user where uid in ( select uid from manage where mid = %d ) ",
79 if (DBM::is_result($r))
84 if(count($full_managers))
85 foreach($full_managers as $rr)
89 foreach($delegates as $rr)
92 // find every contact who might be a candidate for delegation
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())),
101 if (! DBM::is_result($r)) {
102 notice( t('No potential page delegates located.') . EOL);
106 $nicknames = array();
108 if (DBM::is_result($r)) {
109 foreach ($r as $rr) {
110 $nicknames[] = "'" . dbesc(basename($rr['nurl'])) . "'";
114 $potentials = array();
116 $nicks = implode(',',$nicknames);
118 // get user records for all potential page delegates who are not already delegates or managers
120 $r = q("select `uid`, `username`, `nickname` from user where nickname in ( $nicks )");
122 if (DBM::is_result($r))
124 if(! in_array($rr['uid'],$uids))
127 require_once("mod/settings.php");
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'),
142 '$none' => t('No entries.')