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_post(App $a)
23 if (count($a->user) && x($a->user, 'uid') && $a->user['uid'] != local_user()) {
24 notice(L10n::t('Permission denied.') . EOL);
28 check_form_security_token_redirectOnErr('/delegate', 'delegate');
30 $parent_uid = defaults($_POST, 'parent_user', 0);
32 dba::update('user', ['parent-uid' => $parent_uid], ['uid' => local_user()]);
35 function delegate_content(App $a)
38 notice(L10n::t('Permission denied.') . EOL);
42 if ($a->argc > 2 && $a->argv[1] === 'add' && 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 $user_id = $a->argv[2];
50 $user = dba::selectFirst('user', ['nickname'], ['uid' => $user_id]);
51 if (DBM::is_result($user)) {
53 'uid' => local_user(),
54 'nurl' => normalise_link(System::baseUrl() . '/profile/' . $user['nickname'])
56 if (dba::exists('contact', $condition)) {
57 dba::insert('manage', ['uid' => $user_id, 'mid' => local_user()]);
60 goaway(System::baseUrl() . '/delegate');
63 if ($a->argc > 2 && $a->argv[1] === 'remove' && intval($a->argv[2])) {
64 // delegated admins can view but not change delegation permissions
65 if (x($_SESSION, 'submanage')) {
66 goaway(System::baseUrl() . '/delegate');
69 dba::delete('manage', ['uid' => $a->argv[2], 'mid' => local_user()]);
70 goaway(System::baseUrl() . '/delegate');
73 // These people can manage this account/page with full privilege
75 $r = q("SELECT * FROM `user` WHERE `email` = '%s' AND `password` = '%s' ",
76 dbesc($a->user['email']),
77 dbesc($a->user['password'])
79 if (DBM::is_result($r)) {
83 // find everybody that currently has delegated management to this account/page
85 $r = q("SELECT * FROM `user` WHERE `uid` IN (SELECT `uid` FROM `manage` WHERE `mid` = %d)",
88 if (DBM::is_result($r)) {
93 foreach ($full_managers as $rr) {
97 foreach ($delegates as $rr) {
101 // find every contact who might be a candidate for delegation
104 $r = q("SELECT `nurl`
107 AND SUBSTRING_INDEX(`nurl`, '/', 3) = '%s'
109 AND `network` = '%s' ",
110 dbesc(normalise_link(System::baseUrl())),
111 intval(local_user()),
114 if (DBM::is_result($r)) {
116 foreach ($r as $rr) {
117 $nicknames[] = "'" . dbesc(basename($rr['nurl'])) . "'";
120 $nicks = implode(',', $nicknames);
122 // get user records for all potential page delegates who are not already delegates or managers
123 $r = q("SELECT `uid`, `username`, `nickname` FROM `user` WHERE `nickname` IN ($nicks)");
124 if (DBM::is_result($r)) {
125 foreach ($r as $rr) {
126 if (!in_array($rr['uid'], $uids)) {
135 $user = dba::selectFirst('user', ['parent-uid', 'email'], ['uid' => local_user()]);
139 if (DBM::is_result($user)) {
140 if (!dba::exists('user', ['parent-uid' => local_user()])) {
141 $parent_uid = $user['parent-uid'];
142 $parents = [0 => L10n::t('No parent user')];
144 $fields = ['uid', 'username', 'nickname'];
145 $condition = ['email' => $user['email'], 'verified' => true, 'blocked' => false, 'parent-uid' => 0];
146 $parent_users = dba::select('user', $fields, $condition);
147 while ($parent = dba::fetch($parent_users)) {
148 if ($parent['uid'] != local_user()) {
149 $parents[$parent['uid']] = sprintf('%s (%s)', $parent['username'], $parent['nickname']);
152 $parent_user = ['parent_user', '', $parent_uid, '', $parents];
156 $o = replace_macros(get_markup_template('delegate.tpl'), [
157 '$form_security_token' => get_form_security_token('delegate'),
158 '$parent_header' => L10n::t('Parent User'),
159 '$parent_user' => $parent_user,
160 '$parent_desc' => L10n::t('Parent users have total control about this account, including the account settings. Please double check whom you give this access.'),
161 '$submit' => L10n::t('Save Settings'),
162 '$header' => L10n::t('Delegate Page Management'),
163 '$delegates_header' => L10n::t('Delegates'),
164 '$base' => System::baseUrl(),
165 '$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.'),
166 '$head_managers' => L10n::t('Existing Page Managers'),
167 '$managers' => $full_managers,
168 '$head_delegates' => L10n::t('Existing Page Delegates'),
169 '$delegates' => $delegates,
170 '$head_potentials' => L10n::t('Potential Delegates'),
171 '$potentials' => $potentials,
172 '$remove' => L10n::t('Remove'),
173 '$add' => L10n::t('Add'),
174 '$none' => L10n::t('No entries.')