3 * @file mod/delegate.php
6 use Friendica\Core\L10n;
7 use Friendica\Core\System;
8 use Friendica\Database\DBM;
9 use Friendica\Model\User;
11 require_once 'mod/settings.php';
13 function delegate_init(App $a)
15 return settings_init($a);
18 function delegate_post(App $a)
24 if (count($a->user) && x($a->user, 'uid') && $a->user['uid'] != local_user()) {
25 notice(L10n::t('Permission denied.') . EOL);
29 check_form_security_token_redirectOnErr('/delegate', 'delegate');
31 $parent_uid = defaults($_POST, 'parent_user', 0);
32 $parent_password = defaults($_POST, 'parent_password', '');
34 if ($parent_uid != 0) {
35 $user = dba::selectFirst('user', ['nickname'], ['uid' => $parent_uid]);
36 if (!DBM::is_result($user)) {
37 notice(L10n::t('Parent user not found.') . EOL);
41 $success = User::authenticate($user['nickname'], trim($parent_password));
43 notice(L10n::t('Permission denied.') . EOL);
48 dba::update('user', ['parent-uid' => $parent_uid], ['uid' => local_user()]);
51 function delegate_content(App $a)
54 notice(L10n::t('Permission denied.') . EOL);
58 if ($a->argc > 2 && $a->argv[1] === 'add' && intval($a->argv[2])) {
59 // delegated admins can view but not change delegation permissions
60 if (x($_SESSION, 'submanage')) {
61 goaway(System::baseUrl() . '/delegate');
64 $user_id = $a->argv[2];
66 $user = dba::selectFirst('user', ['nickname'], ['uid' => $user_id]);
67 if (DBM::is_result($user)) {
69 'uid' => local_user(),
70 'nurl' => normalise_link(System::baseUrl() . '/profile/' . $user['nickname'])
72 if (dba::exists('contact', $condition)) {
73 dba::insert('manage', ['uid' => $user_id, 'mid' => local_user()]);
76 goaway(System::baseUrl() . '/delegate');
79 if ($a->argc > 2 && $a->argv[1] === 'remove' && intval($a->argv[2])) {
80 // delegated admins can view but not change delegation permissions
81 if (x($_SESSION, 'submanage')) {
82 goaway(System::baseUrl() . '/delegate');
85 dba::delete('manage', ['uid' => $a->argv[2], 'mid' => local_user()]);
86 goaway(System::baseUrl() . '/delegate');
89 // find everybody that currently has delegated management to this account/page
91 $r = q("SELECT * FROM `user` WHERE `uid` IN (SELECT `uid` FROM `manage` WHERE `mid` = %d)",
94 if (DBM::is_result($r)) {
99 foreach ($delegates as $rr) {
100 $uids[] = $rr['uid'];
103 // find every contact who might be a candidate for delegation
106 $r = q("SELECT `nurl`
109 AND SUBSTRING_INDEX(`nurl`, '/', 3) = '%s'
111 AND `network` = '%s' ",
112 dbesc(normalise_link(System::baseUrl())),
113 intval(local_user()),
116 if (DBM::is_result($r)) {
118 foreach ($r as $rr) {
119 $nicknames[] = "'" . dbesc(basename($rr['nurl'])) . "'";
122 $nicks = implode(',', $nicknames);
124 // get user records for all potential page delegates who are not already delegates or managers
125 $r = q("SELECT `uid`, `username`, `nickname` FROM `user` WHERE `nickname` IN ($nicks)");
126 if (DBM::is_result($r)) {
127 foreach ($r as $rr) {
128 if (!in_array($rr['uid'], $uids)) {
137 $user = dba::selectFirst('user', ['parent-uid', 'email'], ['uid' => local_user()]);
141 if (DBM::is_result($user)) {
142 if (!dba::exists('user', ['parent-uid' => local_user()])) {
143 $parent_uid = $user['parent-uid'];
144 $parents = [0 => L10n::t('No parent user')];
146 $fields = ['uid', 'username', 'nickname'];
147 $condition = ['email' => $user['email'], 'verified' => true, 'blocked' => false, 'parent-uid' => 0];
148 $parent_users = dba::select('user', $fields, $condition);
149 while ($parent = dba::fetch($parent_users)) {
150 if ($parent['uid'] != local_user()) {
151 $parents[$parent['uid']] = sprintf('%s (%s)', $parent['username'], $parent['nickname']);
154 $parent_user = ['parent_user', '', $parent_uid, '', $parents];
158 if (!is_null($parent_user)) {
159 $parent_password = ['parent_password', L10n::t('Parent Password:'), '', L10n::t('Please enter the password of the parent account to legitimize your request.')];
162 $o = replace_macros(get_markup_template('delegate.tpl'), [
163 '$form_security_token' => get_form_security_token('delegate'),
164 '$parent_header' => L10n::t('Parent User'),
165 '$parent_user' => $parent_user,
166 '$parent_password' => $parent_password,
167 '$parent_desc' => L10n::t('Parent users have total control about this account, including the account settings. Please double check whom you give this access.'),
168 '$submit' => L10n::t('Save Settings'),
169 '$header' => L10n::t('Delegate Page Management'),
170 '$delegates_header' => L10n::t('Delegates'),
171 '$base' => System::baseUrl(),
172 '$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.'),
173 '$head_delegates' => L10n::t('Existing Page Delegates'),
174 '$delegates' => $delegates,
175 '$head_potentials' => L10n::t('Potential Delegates'),
176 '$potentials' => $potentials,
177 '$remove' => L10n::t('Remove'),
178 '$add' => L10n::t('Add'),
179 '$none' => L10n::t('No entries.')