4 use Friendica\Core\System;
5 use Friendica\Database\DBM;
7 require_once 'mod/settings.php';
9 function delegate_init(App $a)
11 return settings_init($a);
14 function delegate_content(App $a)
17 notice(t('Permission denied.') . EOL);
21 if ($a->argc > 2 && $a->argv[1] === 'add' && intval($a->argv[2])) {
22 // delegated admins can view but not change delegation permissions
23 if (x($_SESSION, 'submanage')) {
24 goaway(System::baseUrl() . '/delegate');
27 $user_id = $a->argv[2];
29 $user = dba::selectFirst('user', ['nickname'], ['uid' => $user_id]);
30 if (DBM::is_result($user)) {
32 'uid' => local_user(),
33 'nurl' => normalise_link(System::baseUrl() . '/profile/' . $user['nickname'])
35 if (dba::exists('contact', $condition)) {
36 dba::insert('manage', ['uid' => $user_id, 'mid' => local_user()]);
39 goaway(System::baseUrl() . '/delegate');
42 if ($a->argc > 2 && $a->argv[1] === 'remove' && intval($a->argv[2])) {
43 // delegated admins can view but not change delegation permissions
44 if (x($_SESSION, 'submanage')) {
45 goaway(System::baseUrl() . '/delegate');
48 dba::delete('manage', ['uid' => $a->argv[2], 'mid' => local_user()]);
49 goaway(System::baseUrl() . '/delegate');
52 // These people can manage this account/page with full privilege
54 $r = q("SELECT * FROM `user` WHERE `email` = '%s' AND `password` = '%s' ",
55 dbesc($a->user['email']),
56 dbesc($a->user['password'])
58 if (DBM::is_result($r)) {
62 // find everybody that currently has delegated management to this account/page
64 $r = q("SELECT * FROM `user` WHERE `uid` IN (SELECT `uid` FROM `manage` WHERE `mid` = %d)",
67 if (DBM::is_result($r)) {
72 foreach ($full_managers as $rr) {
76 foreach ($delegates as $rr) {
80 // find every contact who might be a candidate for delegation
85 AND SUBSTRING_INDEX(`nurl`, '/', 3) = '%s'
87 AND `network` = '%s' ",
88 dbesc(normalise_link(System::baseUrl())),
92 if (!DBM::is_result($r)) {
93 notice(t('No potential page delegates located.') . EOL);
99 $nicknames[] = "'" . dbesc(basename($rr['nurl'])) . "'";
104 $nicks = implode(',', $nicknames);
106 // get user records for all potential page delegates who are not already delegates or managers
107 $r = q("SELECT `uid`, `username`, `nickname` FROM `user` WHERE `nickname` IN ($nicks)");
108 if (DBM::is_result($r)) {
109 foreach ($r as $rr) {
110 if (!in_array($rr['uid'], $uids)) {
118 $o = replace_macros(get_markup_template('delegate.tpl'), [
119 '$header' => t('Delegate Page Management'),
120 '$base' => System::baseUrl(),
121 '$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.'),
122 '$head_managers' => t('Existing Page Managers'),
123 '$managers' => $full_managers,
124 '$head_delegates' => t('Existing Page Delegates'),
125 '$delegates' => $delegates,
126 '$head_potentials' => t('Potential Delegates'),
127 '$potentials' => $potentials,
128 '$remove' => t('Remove'),
130 '$none' => t('No entries.')