4 use Friendica\Core\System;
6 require_once('mod/settings.php');
8 function delegate_init(App $a) {
9 return settings_init($a);
12 function delegate_content(App $a) {
15 notice( t('Permission denied.') . EOL);
19 if ($a->argc > 2 && $a->argv[1] === 'add' && intval($a->argv[2])) {
21 // delegated admins can view but not change delegation permissions
23 if (x($_SESSION,'submanage') && intval($_SESSION['submanage'])) {
24 goaway(System::baseUrl() . '/delegate');
29 $r = q("select `nickname` from user where uid = %d limit 1",
32 if (dbm::is_result($r)) {
33 $r = q("select id from contact where uid = %d and nurl = '%s' limit 1",
35 dbesc(normalise_link(System::baseUrl() . '/profile/' . $r[0]['nickname']))
37 if (dbm::is_result($r)) {
38 q("insert into manage ( uid, mid ) values ( %d , %d ) ",
44 goaway(System::baseUrl() . '/delegate');
47 if ($a->argc > 2 && $a->argv[1] === 'remove' && intval($a->argv[2])) {
49 // delegated admins can view but not change delegation permissions
50 if (x($_SESSION,'submanage') && intval($_SESSION['submanage'])) {
51 goaway(System::baseUrl() . '/delegate');
54 q("DELETE FROM `manage` WHERE `uid` = %d AND `mid` = %d LIMIT 1",
58 goaway(System::baseUrl() . '/delegate');
62 $full_managers = array();
64 // These people can manage this account/page with full privilege
66 $r = q("SELECT * FROM `user` WHERE `email` = '%s' AND `password` = '%s' ",
67 dbesc($a->user['email']),
68 dbesc($a->user['password'])
70 if (dbm::is_result($r))
75 // find everybody that currently has delegated management to this account/page
77 $r = q("select * from user where uid in ( select uid from manage where mid = %d ) ",
81 if (dbm::is_result($r))
86 if(count($full_managers))
87 foreach($full_managers as $rr)
91 foreach($delegates as $rr)
94 // find every contact who might be a candidate for delegation
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())),
103 if (! dbm::is_result($r)) {
104 notice( t('No potential page delegates located.') . EOL);
108 $nicknames = array();
110 if (dbm::is_result($r)) {
111 foreach ($r as $rr) {
112 $nicknames[] = "'" . dbesc(basename($rr['nurl'])) . "'";
116 $potentials = array();
118 $nicks = implode(',',$nicknames);
120 // get user records for all potential page delegates who are not already delegates or managers
122 $r = q("select `uid`, `username`, `nickname` from user where nickname in ( $nicks )");
124 if (dbm::is_result($r))
126 if(! in_array($rr['uid'],$uids))
129 require_once("mod/settings.php");
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'),
144 '$none' => t('No entries.')