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 $user = dba::selectFirst('user', ['nickname'], ['uid' => $parent_uid]);
35 if (!DBM::is_result($user)) {
36 notice(L10n::t('Parent user not found.') . EOL);
40 $success = User::authenticate($user['nickname'], trim($parent_password));
42 notice(L10n::t('Permission denied.') . EOL);
46 dba::update('user', ['parent-uid' => $parent_uid], ['uid' => local_user()]);
49 function delegate_content(App $a)
52 notice(L10n::t('Permission denied.') . EOL);
56 if ($a->argc > 2 && $a->argv[1] === 'add' && intval($a->argv[2])) {
57 // delegated admins can view but not change delegation permissions
58 if (x($_SESSION, 'submanage')) {
59 goaway(System::baseUrl() . '/delegate');
62 $user_id = $a->argv[2];
64 $user = dba::selectFirst('user', ['nickname'], ['uid' => $user_id]);
65 if (DBM::is_result($user)) {
67 'uid' => local_user(),
68 'nurl' => normalise_link(System::baseUrl() . '/profile/' . $user['nickname'])
70 if (dba::exists('contact', $condition)) {
71 dba::insert('manage', ['uid' => $user_id, 'mid' => local_user()]);
74 goaway(System::baseUrl() . '/delegate');
77 if ($a->argc > 2 && $a->argv[1] === 'remove' && intval($a->argv[2])) {
78 // delegated admins can view but not change delegation permissions
79 if (x($_SESSION, 'submanage')) {
80 goaway(System::baseUrl() . '/delegate');
83 dba::delete('manage', ['uid' => $a->argv[2], 'mid' => local_user()]);
84 goaway(System::baseUrl() . '/delegate');
87 // find everybody that currently has delegated management to this account/page
89 $r = q("SELECT * FROM `user` WHERE `uid` IN (SELECT `uid` FROM `manage` WHERE `mid` = %d)",
92 if (DBM::is_result($r)) {
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 if (!is_null($parent_user)) {
157 $parent_password = ['parent_password', L10n::t('Parent Password:'), '', L10n::t('Please enter the password of the parent account to legitimize your request.')];
160 $o = replace_macros(get_markup_template('delegate.tpl'), [
161 '$form_security_token' => get_form_security_token('delegate'),
162 '$parent_header' => L10n::t('Parent User'),
163 '$parent_user' => $parent_user,
164 '$parent_password' => $parent_password,
165 '$parent_desc' => L10n::t('Parent users have total control about this account, including the account settings. Please double check whom you give this access.'),
166 '$submit' => L10n::t('Save Settings'),
167 '$header' => L10n::t('Delegate Page Management'),
168 '$delegates_header' => L10n::t('Delegates'),
169 '$base' => System::baseUrl(),
170 '$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.'),
171 '$head_delegates' => L10n::t('Existing Page Delegates'),
172 '$delegates' => $delegates,
173 '$head_potentials' => L10n::t('Potential Delegates'),
174 '$potentials' => $potentials,
175 '$remove' => L10n::t('Remove'),
176 '$add' => L10n::t('Add'),
177 '$none' => L10n::t('No entries.')