3 * @file mod/delegate.php
7 use Friendica\Core\L10n;
8 use Friendica\Core\Protocol;
9 use Friendica\Core\System;
10 use Friendica\Database\DBA;
11 use Friendica\Model\User;
13 require_once 'mod/settings.php';
15 function delegate_init(App $a)
17 return settings_init($a);
20 function delegate_post(App $a)
26 if (count($a->user) && x($a->user, 'uid') && $a->user['uid'] != local_user()) {
27 notice(L10n::t('Permission denied.') . EOL);
31 check_form_security_token_redirectOnErr('/delegate', 'delegate');
33 $parent_uid = defaults($_POST, 'parent_user', 0);
34 $parent_password = defaults($_POST, 'parent_password', '');
36 if ($parent_uid != 0) {
37 $user = DBA::selectFirst('user', ['nickname'], ['uid' => $parent_uid]);
38 if (!DBA::isResult($user)) {
39 notice(L10n::t('Parent user not found.') . EOL);
43 $success = User::authenticate($user['nickname'], trim($parent_password));
45 notice(L10n::t('Permission denied.') . EOL);
50 DBA::update('user', ['parent-uid' => $parent_uid], ['uid' => local_user()]);
53 function delegate_content(App $a)
56 notice(L10n::t('Permission denied.') . EOL);
60 if ($a->argc > 2 && $a->argv[1] === 'add' && intval($a->argv[2])) {
61 // delegated admins can view but not change delegation permissions
62 if (x($_SESSION, 'submanage')) {
63 goaway(System::baseUrl() . '/delegate');
66 $user_id = $a->argv[2];
68 $user = DBA::selectFirst('user', ['nickname'], ['uid' => $user_id]);
69 if (DBA::isResult($user)) {
71 'uid' => local_user(),
72 'nurl' => normalise_link(System::baseUrl() . '/profile/' . $user['nickname'])
74 if (DBA::exists('contact', $condition)) {
75 DBA::insert('manage', ['uid' => $user_id, 'mid' => local_user()]);
78 goaway(System::baseUrl() . '/delegate');
81 if ($a->argc > 2 && $a->argv[1] === 'remove' && intval($a->argv[2])) {
82 // delegated admins can view but not change delegation permissions
83 if (x($_SESSION, 'submanage')) {
84 goaway(System::baseUrl() . '/delegate');
87 DBA::delete('manage', ['uid' => $a->argv[2], 'mid' => local_user()]);
88 goaway(System::baseUrl() . '/delegate');
91 // find everybody that currently has delegated management to this account/page
93 $r = q("SELECT * FROM `user` WHERE `uid` IN (SELECT `uid` FROM `manage` WHERE `mid` = %d)",
96 if (DBA::isResult($r)) {
101 foreach ($delegates as $rr) {
102 $uids[] = $rr['uid'];
105 // find every contact who might be a candidate for delegation
108 $r = q("SELECT `nurl`
111 AND SUBSTRING_INDEX(`nurl`, '/', 3) = '%s'
113 AND `network` = '%s' ",
114 DBA::escape(normalise_link(System::baseUrl())),
115 intval(local_user()),
116 DBA::escape(Protocol::DFRN)
118 if (DBA::isResult($r)) {
120 foreach ($r as $rr) {
121 $nicknames[] = "'" . DBA::escape(basename($rr['nurl'])) . "'";
124 $nicks = implode(',', $nicknames);
126 // get user records for all potential page delegates who are not already delegates or managers
127 $r = q("SELECT `uid`, `username`, `nickname` FROM `user` WHERE `nickname` IN ($nicks)");
128 if (DBA::isResult($r)) {
129 foreach ($r as $rr) {
130 if (!in_array($rr['uid'], $uids)) {
139 $user = DBA::selectFirst('user', ['parent-uid', 'email'], ['uid' => local_user()]);
143 if (DBA::isResult($user)) {
144 if (!DBA::exists('user', ['parent-uid' => local_user()])) {
145 $parent_uid = $user['parent-uid'];
146 $parents = [0 => L10n::t('No parent user')];
148 $fields = ['uid', 'username', 'nickname'];
149 $condition = ['email' => $user['email'], 'verified' => true, 'blocked' => false, 'parent-uid' => 0];
150 $parent_users = DBA::select('user', $fields, $condition);
151 while ($parent = DBA::fetch($parent_users)) {
152 if ($parent['uid'] != local_user()) {
153 $parents[$parent['uid']] = sprintf('%s (%s)', $parent['username'], $parent['nickname']);
156 $parent_user = ['parent_user', '', $parent_uid, '', $parents];
160 if (!is_null($parent_user)) {
161 $parent_password = ['parent_password', L10n::t('Parent Password:'), '', L10n::t('Please enter the password of the parent account to legitimize your request.')];
164 $o = replace_macros(get_markup_template('delegate.tpl'), [
165 '$form_security_token' => get_form_security_token('delegate'),
166 '$parent_header' => L10n::t('Parent User'),
167 '$parent_user' => $parent_user,
168 '$parent_password' => $parent_password,
169 '$parent_desc' => L10n::t('Parent users have total control about this account, including the account settings. Please double check whom you give this access.'),
170 '$submit' => L10n::t('Save Settings'),
171 '$header' => L10n::t('Delegate Page Management'),
172 '$delegates_header' => L10n::t('Delegates'),
173 '$base' => System::baseUrl(),
174 '$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.'),
175 '$head_delegates' => L10n::t('Existing Page Delegates'),
176 '$delegates' => $delegates,
177 '$head_potentials' => L10n::t('Potential Delegates'),
178 '$potentials' => $potentials,
179 '$remove' => L10n::t('Remove'),
180 '$add' => L10n::t('Add'),
181 '$none' => L10n::t('No entries.')