3 * @file mod/delegate.php
7 use Friendica\BaseModule;
8 use Friendica\Core\L10n;
9 use Friendica\Core\Protocol;
10 use Friendica\Core\Renderer;
11 use Friendica\Core\System;
12 use Friendica\Database\DBA;
13 use Friendica\Model\User;
14 use Friendica\Util\Strings;
16 require_once 'mod/settings.php';
18 function delegate_init(App $a)
23 function delegate_post(App $a)
29 if (count($a->user) && !empty($a->user['uid']) && $a->user['uid'] != local_user()) {
30 notice(L10n::t('Permission denied.') . EOL);
34 BaseModule::checkFormSecurityTokenRedirectOnError('/delegate', 'delegate');
36 $parent_uid = defaults($_POST, 'parent_user', 0);
37 $parent_password = defaults($_POST, 'parent_password', '');
39 if ($parent_uid != 0) {
40 $user = DBA::selectFirst('user', ['nickname'], ['uid' => $parent_uid]);
41 if (!DBA::isResult($user)) {
42 notice(L10n::t('Parent user not found.') . EOL);
46 $success = User::authenticate($user['nickname'], trim($parent_password));
48 notice(L10n::t('Permission denied.') . EOL);
53 DBA::update('user', ['parent-uid' => $parent_uid], ['uid' => local_user()]);
56 function delegate_content(App $a)
59 notice(L10n::t('Permission denied.') . EOL);
63 if ($a->argc > 2 && $a->argv[1] === 'add' && intval($a->argv[2])) {
64 // delegated admins can view but not change delegation permissions
65 if (!empty($_SESSION['submanage'])) {
66 $a->internalRedirect('delegate');
69 $user_id = $a->argv[2];
71 $user = DBA::selectFirst('user', ['nickname'], ['uid' => $user_id]);
72 if (DBA::isResult($user)) {
74 'uid' => local_user(),
75 'nurl' => Strings::normaliseLink(System::baseUrl() . '/profile/' . $user['nickname'])
77 if (DBA::exists('contact', $condition)) {
78 DBA::insert('manage', ['uid' => $user_id, 'mid' => local_user()]);
81 $a->internalRedirect('delegate');
84 if ($a->argc > 2 && $a->argv[1] === 'remove' && intval($a->argv[2])) {
85 // delegated admins can view but not change delegation permissions
86 if (!empty($_SESSION['submanage'])) {
87 $a->internalRedirect('delegate');
90 DBA::delete('manage', ['uid' => $a->argv[2], 'mid' => local_user()]);
91 $a->internalRedirect('delegate');
94 // find everybody that currently has delegated management to this account/page
96 $r = q("SELECT * FROM `user` WHERE `uid` IN (SELECT `uid` FROM `manage` WHERE `mid` = %d)",
99 if (DBA::isResult($r)) {
104 foreach ($delegates as $rr) {
105 $uids[] = $rr['uid'];
108 // find every contact who might be a candidate for delegation
111 $r = q("SELECT `nurl`
114 AND SUBSTRING_INDEX(`nurl`, '/', 3) = '%s'
116 AND `network` = '%s' ",
117 DBA::escape(Strings::normaliseLink(System::baseUrl())),
118 intval(local_user()),
119 DBA::escape(Protocol::DFRN)
121 if (DBA::isResult($r)) {
123 foreach ($r as $rr) {
124 $nicknames[] = "'" . DBA::escape(basename($rr['nurl'])) . "'";
127 $nicks = implode(',', $nicknames);
129 // get user records for all potential page delegates who are not already delegates or managers
130 $r = q("SELECT `uid`, `username`, `nickname` FROM `user` WHERE `nickname` IN ($nicks)");
131 if (DBA::isResult($r)) {
132 foreach ($r as $rr) {
133 if (!in_array($rr['uid'], $uids)) {
142 $user = DBA::selectFirst('user', ['parent-uid', 'email'], ['uid' => local_user()]);
146 if (DBA::isResult($user)) {
147 if (!DBA::exists('user', ['parent-uid' => local_user()])) {
148 $parent_uid = $user['parent-uid'];
149 $parents = [0 => L10n::t('No parent user')];
151 $fields = ['uid', 'username', 'nickname'];
152 $condition = ['email' => $user['email'], 'verified' => true, 'blocked' => false, 'parent-uid' => 0];
153 $parent_users = DBA::select('user', $fields, $condition);
154 while ($parent = DBA::fetch($parent_users)) {
155 if ($parent['uid'] != local_user()) {
156 $parents[$parent['uid']] = sprintf('%s (%s)', $parent['username'], $parent['nickname']);
159 $parent_user = ['parent_user', '', $parent_uid, '', $parents];
163 if (!is_null($parent_user)) {
164 $parent_password = ['parent_password', L10n::t('Parent Password:'), '', L10n::t('Please enter the password of the parent account to legitimize your request.')];
166 $parent_password = '';
169 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('delegate.tpl'), [
170 '$form_security_token' => BaseModule::getFormSecurityToken('delegate'),
171 '$parent_header' => L10n::t('Parent User'),
172 '$parent_user' => $parent_user,
173 '$parent_password' => $parent_password,
174 '$parent_desc' => L10n::t('Parent users have total control about this account, including the account settings. Please double check whom you give this access.'),
175 '$submit' => L10n::t('Save Settings'),
176 '$header' => L10n::t('Delegate Page Management'),
177 '$delegates_header' => L10n::t('Delegates'),
178 '$base' => System::baseUrl(),
179 '$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.'),
180 '$head_delegates' => L10n::t('Existing Page Delegates'),
181 '$delegates' => $delegates,
182 '$head_potentials' => L10n::t('Potential Delegates'),
183 '$potentials' => $potentials,
184 '$remove' => L10n::t('Remove'),
185 '$add' => L10n::t('Add'),
186 '$none' => L10n::t('No entries.')