3 * @file mod/delegate.php
7 use Friendica\Core\L10n;
8 use Friendica\Core\System;
9 use Friendica\Database\DBA;
10 use Friendica\Model\User;
12 require_once 'mod/settings.php';
14 function delegate_init(App $a)
16 return settings_init($a);
19 function delegate_post(App $a)
25 if (count($a->user) && x($a->user, 'uid') && $a->user['uid'] != local_user()) {
26 notice(L10n::t('Permission denied.') . EOL);
30 check_form_security_token_redirectOnErr('/delegate', 'delegate');
32 $parent_uid = defaults($_POST, 'parent_user', 0);
33 $parent_password = defaults($_POST, 'parent_password', '');
35 if ($parent_uid != 0) {
36 $user = DBA::selectFirst('user', ['nickname'], ['uid' => $parent_uid]);
37 if (!DBA::isResult($user)) {
38 notice(L10n::t('Parent user not found.') . EOL);
42 $success = User::authenticate($user['nickname'], trim($parent_password));
44 notice(L10n::t('Permission denied.') . EOL);
49 DBA::update('user', ['parent-uid' => $parent_uid], ['uid' => local_user()]);
52 function delegate_content(App $a)
55 notice(L10n::t('Permission denied.') . EOL);
59 if ($a->argc > 2 && $a->argv[1] === 'add' && intval($a->argv[2])) {
60 // delegated admins can view but not change delegation permissions
61 if (x($_SESSION, 'submanage')) {
62 goaway(System::baseUrl() . '/delegate');
65 $user_id = $a->argv[2];
67 $user = DBA::selectFirst('user', ['nickname'], ['uid' => $user_id]);
68 if (DBA::isResult($user)) {
70 'uid' => local_user(),
71 'nurl' => normalise_link(System::baseUrl() . '/profile/' . $user['nickname'])
73 if (DBA::exists('contact', $condition)) {
74 DBA::insert('manage', ['uid' => $user_id, 'mid' => local_user()]);
77 goaway(System::baseUrl() . '/delegate');
80 if ($a->argc > 2 && $a->argv[1] === 'remove' && intval($a->argv[2])) {
81 // delegated admins can view but not change delegation permissions
82 if (x($_SESSION, 'submanage')) {
83 goaway(System::baseUrl() . '/delegate');
86 DBA::delete('manage', ['uid' => $a->argv[2], 'mid' => local_user()]);
87 goaway(System::baseUrl() . '/delegate');
90 // find everybody that currently has delegated management to this account/page
92 $r = q("SELECT * FROM `user` WHERE `uid` IN (SELECT `uid` FROM `manage` WHERE `mid` = %d)",
95 if (DBA::isResult($r)) {
100 foreach ($delegates as $rr) {
101 $uids[] = $rr['uid'];
104 // find every contact who might be a candidate for delegation
107 $r = q("SELECT `nurl`
110 AND SUBSTRING_INDEX(`nurl`, '/', 3) = '%s'
112 AND `network` = '%s' ",
113 DBA::escape(normalise_link(System::baseUrl())),
114 intval(local_user()),
115 DBA::escape(NETWORK_DFRN)
117 if (DBA::isResult($r)) {
119 foreach ($r as $rr) {
120 $nicknames[] = "'" . DBA::escape(basename($rr['nurl'])) . "'";
123 $nicks = implode(',', $nicknames);
125 // get user records for all potential page delegates who are not already delegates or managers
126 $r = q("SELECT `uid`, `username`, `nickname` FROM `user` WHERE `nickname` IN ($nicks)");
127 if (DBA::isResult($r)) {
128 foreach ($r as $rr) {
129 if (!in_array($rr['uid'], $uids)) {
138 $user = DBA::selectFirst('user', ['parent-uid', 'email'], ['uid' => local_user()]);
142 if (DBA::isResult($user)) {
143 if (!DBA::exists('user', ['parent-uid' => local_user()])) {
144 $parent_uid = $user['parent-uid'];
145 $parents = [0 => L10n::t('No parent user')];
147 $fields = ['uid', 'username', 'nickname'];
148 $condition = ['email' => $user['email'], 'verified' => true, 'blocked' => false, 'parent-uid' => 0];
149 $parent_users = DBA::select('user', $fields, $condition);
150 while ($parent = DBA::fetch($parent_users)) {
151 if ($parent['uid'] != local_user()) {
152 $parents[$parent['uid']] = sprintf('%s (%s)', $parent['username'], $parent['nickname']);
155 $parent_user = ['parent_user', '', $parent_uid, '', $parents];
159 if (!is_null($parent_user)) {
160 $parent_password = ['parent_password', L10n::t('Parent Password:'), '', L10n::t('Please enter the password of the parent account to legitimize your request.')];
163 $o = replace_macros(get_markup_template('delegate.tpl'), [
164 '$form_security_token' => get_form_security_token('delegate'),
165 '$parent_header' => L10n::t('Parent User'),
166 '$parent_user' => $parent_user,
167 '$parent_password' => $parent_password,
168 '$parent_desc' => L10n::t('Parent users have total control about this account, including the account settings. Please double check whom you give this access.'),
169 '$submit' => L10n::t('Save Settings'),
170 '$header' => L10n::t('Delegate Page Management'),
171 '$delegates_header' => L10n::t('Delegates'),
172 '$base' => System::baseUrl(),
173 '$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.'),
174 '$head_delegates' => L10n::t('Existing Page Delegates'),
175 '$delegates' => $delegates,
176 '$head_potentials' => L10n::t('Potential Delegates'),
177 '$potentials' => $potentials,
178 '$remove' => L10n::t('Remove'),
179 '$add' => L10n::t('Add'),
180 '$none' => L10n::t('No entries.')