3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Core;
24 use Friendica\App\Page;
25 use Friendica\Database\DBA;
27 use Friendica\Model\Contact;
28 use Friendica\Model\Group;
29 use Friendica\Model\User;
32 * Handle ACL management and display
37 * Returns the default lock state for the given user id
39 * @return bool "true" if the default settings are non public
41 public static function getLockstateForUserId(int $uid)
43 $user = User::getById($uid, ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid']);
44 return !empty($user['allow_cid']) || !empty($user['allow_gid']) || !empty($user['deny_cid']) || !empty($user['deny_gid']);
48 * Returns a select input tag for private message recipient
50 * @param int $selected Existing recipien contact ID
54 public static function getMessageContactSelectHTML(int $selected = null)
60 $page->registerFooterScript(Theme::getPathForFile('asset/typeahead.js/dist/typeahead.bundle.js'));
61 $page->registerFooterScript(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput.js'));
62 $page->registerStylesheet(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput.css'));
63 $page->registerStylesheet(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput-typeahead.css'));
66 'uid' => local_user(),
72 'rel' => [Contact::FOLLOWER, Contact::SHARING, Contact::FRIEND],
73 'network' => Protocol::SUPPORT_PRIVATE,
76 $contacts = Contact::selectToArray(
77 ['id', 'name', 'addr', 'micro'],
78 DBA::mergeConditions($condition, ["`notify` != ''"])
81 $arr = ['contact' => $contacts, 'entry' => $o];
83 Hook::callAll(DI::module()->getName() . '_pre_recipient', $arr);
85 $tpl = Renderer::getMarkupTemplate('acl/message_recipient.tpl');
86 $o = Renderer::replaceMacros($tpl, [
87 '$contacts' => $contacts,
88 '$selected' => $selected,
91 Hook::callAll(DI::module()->getName() . '_post_recipient', $o);
97 * Returns a minimal ACL block for self-only permissions
99 * @param int $localUserId
100 * @param string $explanation
102 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
104 public static function getSelfOnlyHTML(int $localUserId, string $explanation)
106 $selfPublicContactId = Contact::getPublicIdByUserId($localUserId);
108 $tpl = Renderer::getMarkupTemplate('acl/self_only.tpl');
109 $o = Renderer::replaceMacros($tpl, [
110 '$selfPublicContactId' => $selfPublicContactId,
111 '$explanation' => $explanation,
118 * Return the default permission of the provided user array
121 * @return array Hash of contact id lists
124 public static function getDefaultUserPermissions(array $user = null)
126 $aclFormatter = DI::aclFormatter();
129 'allow_cid' => Contact::pruneUnavailable($aclFormatter->expand($user['allow_cid'] ?? '')),
130 'allow_gid' => $aclFormatter->expand($user['allow_gid'] ?? ''),
131 'deny_cid' => $aclFormatter->expand($user['deny_cid'] ?? ''),
132 'deny_gid' => $aclFormatter->expand($user['deny_gid'] ?? ''),
137 * Returns the ACL list of contacts for a given user id
139 * @param int $user_id
140 * @param array $condition Additional contact lookup table conditions
144 public static function getContactListByUserId(int $user_id, array $condition = [])
146 $fields = ['id', 'name', 'addr', 'micro'];
147 $params = ['order' => ['name']];
148 $acl_contacts = Contact::selectToArray(
157 'network' => Protocol::FEDERATED,
158 'rel' => [Contact::FOLLOWER, Contact::FRIEND]
163 $acl_yourself = Contact::selectFirst($fields, ['uid' => $user_id, 'self' => true]);
164 $acl_yourself['name'] = DI::l10n()->t('Yourself');
166 $acl_contacts[] = $acl_yourself;
168 $acl_forums = Contact::selectToArray($fields,
169 ['uid' => $user_id, 'self' => false, 'blocked' => false, 'archive' => false, 'deleted' => false,
170 'network' => Protocol::FEDERATED, 'pending' => false, 'contact-type' => Contact::TYPE_COMMUNITY], $params
173 $acl_contacts = array_merge($acl_forums, $acl_contacts);
175 array_walk($acl_contacts, function (&$value) {
176 $value['type'] = 'contact';
179 return $acl_contacts;
183 * Returns the ACL list of groups (including meta-groups) for a given user id
185 * @param int $user_id
188 public static function getGroupListByUserId(int $user_id)
192 'id' => Group::FOLLOWERS,
193 'name' => DI::l10n()->t('Followers'),
195 'micro' => 'images/twopeople.png',
199 'id' => Group::MUTUALS,
200 'name' => DI::l10n()->t('Mutuals'),
202 'micro' => 'images/twopeople.png',
206 foreach (Group::getByUserId($user_id) as $group) {
208 'id' => $group['id'],
209 'name' => $group['name'],
211 'micro' => 'images/twopeople.png',
220 * Return the full jot ACL selector HTML
223 * @param int $uid User ID
224 * @param bool $for_federation
225 * @param array $default_permissions Static defaults permission array:
232 * @param array $condition
233 * @param string $form_prefix
235 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
237 public static function getFullSelectorHTML(
240 bool $for_federation = false,
241 array $default_permissions = [],
242 array $condition = [],
249 static $input_group_id = 0;
251 $user = User::getById($uid);
255 $page->registerFooterScript(Theme::getPathForFile('asset/typeahead.js/dist/typeahead.bundle.js'));
256 $page->registerFooterScript(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput.js'));
257 $page->registerStylesheet(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput.css'));
258 $page->registerStylesheet(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput-typeahead.css'));
260 // Defaults user permissions
261 if (empty($default_permissions)) {
262 $default_permissions = self::getDefaultUserPermissions($user);
265 $default_permissions = [
266 'allow_cid' => $default_permissions['allow_cid'] ?? [],
267 'allow_gid' => $default_permissions['allow_gid'] ?? [],
268 'deny_cid' => $default_permissions['deny_cid'] ?? [],
269 'deny_gid' => $default_permissions['deny_gid'] ?? [],
272 if (count($default_permissions['allow_cid'])
273 + count($default_permissions['allow_gid'])
274 + count($default_permissions['deny_cid'])
275 + count($default_permissions['deny_gid'])) {
276 $visibility = 'custom';
278 $visibility = 'public';
279 // Default permission display for custom panel
280 $default_permissions['allow_gid'] = [Group::FOLLOWERS];
283 $jotnets_fields = [];
284 if ($for_federation) {
285 if (function_exists('imap_open') && !DI::config()->get('system', 'imap_disabled')) {
286 $mailacct = DBA::selectFirst('mailacct', ['pubmail'], ['`uid` = ? AND `server` != ""', $user['uid']]);
287 if (DBA::isResult($mailacct)) {
288 $jotnets_fields[] = [
289 'type' => 'checkbox',
292 DI::l10n()->t('Post to Email'),
293 !empty($mailacct['pubmail'])
299 Hook::callAll('jot_networks', $jotnets_fields);
302 $acl_contacts = self::getContactListByUserId($user['uid'], $condition);
304 $acl_groups = self::getGroupListByUserId($user['uid']);
306 $acl_list = array_merge($acl_groups, $acl_contacts);
309 'visibility' => $form_prefix ? $form_prefix . '[visibility]' : 'visibility',
310 'group_allow' => $form_prefix ? $form_prefix . '[group_allow]' : 'group_allow',
311 'contact_allow' => $form_prefix ? $form_prefix . '[contact_allow]' : 'contact_allow',
312 'group_deny' => $form_prefix ? $form_prefix . '[group_deny]' : 'group_deny',
313 'contact_deny' => $form_prefix ? $form_prefix . '[contact_deny]' : 'contact_deny',
314 'emailcc' => $form_prefix ? $form_prefix . '[emailcc]' : 'emailcc',
317 $tpl = Renderer::getMarkupTemplate('acl/full_selector.tpl');
318 $o = Renderer::replaceMacros($tpl, [
319 '$public_title' => DI::l10n()->t('Public'),
320 '$public_desc' => DI::l10n()->t('This content will be shown to all your followers and can be seen in the community pages and by anyone with its link.'),
321 '$custom_title' => DI::l10n()->t('Limited/Private'),
322 '$custom_desc' => DI::l10n()->t('This content will be shown only to the people in the first box, to the exception of the people mentioned in the second box. It won\'t appear anywhere public.'),
323 '$allow_label' => DI::l10n()->t('Show to:'),
324 '$deny_label' => DI::l10n()->t('Except to:'),
325 '$emailcc' => DI::l10n()->t('CC: email addresses'),
326 '$emtitle' => DI::l10n()->t('Example: bob@example.com, mary@example.com'),
327 '$jotnets_summary' => DI::l10n()->t('Connectors'),
328 '$visibility' => $visibility,
329 '$acl_contacts' => $acl_contacts,
330 '$acl_groups' => $acl_groups,
331 '$acl_list' => $acl_list,
332 '$contact_allow' => implode(',', $default_permissions['allow_cid']),
333 '$group_allow' => implode(',', $default_permissions['allow_gid']),
334 '$contact_deny' => implode(',', $default_permissions['deny_cid']),
335 '$group_deny' => implode(',', $default_permissions['deny_gid']),
336 '$for_federation' => $for_federation,
337 '$jotnets_fields' => $jotnets_fields,
338 '$input_names' => $input_names,
339 '$input_group_id' => $input_group_id,