3 * @file mod/delegate.php
6 use Friendica\Core\L10n;
7 use Friendica\Core\System;
8 use Friendica\Database\DBM;
10 require_once 'mod/settings.php';
12 function delegate_init(App $a)
14 return settings_init($a);
17 function delegate_content(App $a)
20 notice(L10n::t('Permission denied.') . EOL);
24 if ($a->argc > 2 && $a->argv[1] === 'add' && intval($a->argv[2])) {
25 // delegated admins can view but not change delegation permissions
26 if (x($_SESSION, 'submanage')) {
27 goaway(System::baseUrl() . '/delegate');
30 $user_id = $a->argv[2];
32 $user = dba::selectFirst('user', ['nickname'], ['uid' => $user_id]);
33 if (DBM::is_result($user)) {
35 'uid' => local_user(),
36 'nurl' => normalise_link(System::baseUrl() . '/profile/' . $user['nickname'])
38 if (dba::exists('contact', $condition)) {
39 dba::insert('manage', ['uid' => $user_id, 'mid' => local_user()]);
42 goaway(System::baseUrl() . '/delegate');
45 if ($a->argc > 2 && $a->argv[1] === 'remove' && intval($a->argv[2])) {
46 // delegated admins can view but not change delegation permissions
47 if (x($_SESSION, 'submanage')) {
48 goaway(System::baseUrl() . '/delegate');
51 dba::delete('manage', ['uid' => $a->argv[2], 'mid' => local_user()]);
52 goaway(System::baseUrl() . '/delegate');
55 // These people can manage this account/page with full privilege
57 $r = q("SELECT * FROM `user` WHERE `email` = '%s' AND `password` = '%s' ",
58 dbesc($a->user['email']),
59 dbesc($a->user['password'])
61 if (DBM::is_result($r)) {
65 // find everybody that currently has delegated management to this account/page
67 $r = q("SELECT * FROM `user` WHERE `uid` IN (SELECT `uid` FROM `manage` WHERE `mid` = %d)",
70 if (DBM::is_result($r)) {
75 foreach ($full_managers as $rr) {
79 foreach ($delegates as $rr) {
83 // find every contact who might be a candidate for delegation
88 AND SUBSTRING_INDEX(`nurl`, '/', 3) = '%s'
90 AND `network` = '%s' ",
91 dbesc(normalise_link(System::baseUrl())),
95 if (!DBM::is_result($r)) {
96 notice(L10n::t('No potential page delegates located.') . EOL);
101 foreach ($r as $rr) {
102 $nicknames[] = "'" . dbesc(basename($rr['nurl'])) . "'";
107 $nicks = implode(',', $nicknames);
109 // get user records for all potential page delegates who are not already delegates or managers
110 $r = q("SELECT `uid`, `username`, `nickname` FROM `user` WHERE `nickname` IN ($nicks)");
111 if (DBM::is_result($r)) {
112 foreach ($r as $rr) {
113 if (!in_array($rr['uid'], $uids)) {
121 $o = replace_macros(get_markup_template('delegate.tpl'), [
122 '$header' => L10n::t('Delegate Page Management'),
123 '$base' => System::baseUrl(),
124 '$desc' => L10n::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.'),
125 '$head_managers' => L10n::t('Existing Page Managers'),
126 '$managers' => $full_managers,
127 '$head_delegates' => L10n::t('Existing Page Delegates'),
128 '$delegates' => $delegates,
129 '$head_potentials' => L10n::t('Potential Delegates'),
130 '$potentials' => $potentials,
131 '$remove' => L10n::t('Remove'),
132 '$add' => L10n::t('Add'),
133 '$none' => L10n::t('No entries.')