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'));
65 // When used for private messages, we limit correspondence to mutual DFRN/Friendica friends and the selector
66 // to one recipient. By default our selector allows multiple selects amongst all contacts.
68 'uid' => local_user(),
74 'rel' => [Contact::FOLLOWER, Contact::SHARING, Contact::FRIEND],
75 'network' => Protocol::FEDERATED,
78 $contacts = Contact::selectToArray(
79 ['id', 'name', 'addr', 'micro'],
80 DBA::mergeConditions($condition, ["`notify` != ''"])
83 $arr = ['contact' => $contacts, 'entry' => $o];
85 Hook::callAll(DI::module()->getName() . '_pre_recipient', $arr);
87 $tpl = Renderer::getMarkupTemplate('acl/message_recipient.tpl');
88 $o = Renderer::replaceMacros($tpl, [
89 '$contacts' => $contacts,
90 '$selected' => $selected,
93 Hook::callAll(DI::module()->getName() . '_post_recipient', $o);
99 * Returns a minimal ACL block for self-only permissions
101 * @param int $localUserId
102 * @param string $explanation
104 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
106 public static function getSelfOnlyHTML(int $localUserId, string $explanation)
108 $selfPublicContactId = Contact::getPublicIdByUserId($localUserId);
110 $tpl = Renderer::getMarkupTemplate('acl/self_only.tpl');
111 $o = Renderer::replaceMacros($tpl, [
112 '$selfPublicContactId' => $selfPublicContactId,
113 '$explanation' => $explanation,
120 * Return the default permission of the provided user array
123 * @return array Hash of contact id lists
126 public static function getDefaultUserPermissions(array $user = null)
128 $aclFormatter = DI::aclFormatter();
131 'allow_cid' => Contact::pruneUnavailable($aclFormatter->expand($user['allow_cid'] ?? '')),
132 'allow_gid' => $aclFormatter->expand($user['allow_gid'] ?? ''),
133 'deny_cid' => $aclFormatter->expand($user['deny_cid'] ?? ''),
134 'deny_gid' => $aclFormatter->expand($user['deny_gid'] ?? ''),
139 * Returns the ACL list of contacts for a given user id
141 * @param int $user_id
142 * @param array $condition Additional contact lookup table conditions
146 public static function getContactListByUserId(int $user_id, array $condition = [])
148 $fields = ['id', 'name', 'addr', 'micro'];
149 $params = ['order' => ['name']];
150 $acl_contacts = Contact::selectToArray(
159 'network' => Protocol::FEDERATED,
160 'rel' => [Contact::FOLLOWER, Contact::FRIEND]
165 $acl_yourself = Contact::selectFirst($fields, ['uid' => $user_id, 'self' => true]);
166 $acl_yourself['name'] = DI::l10n()->t('Yourself');
168 $acl_contacts[] = $acl_yourself;
170 $acl_forums = Contact::selectToArray($fields,
171 ['uid' => $user_id, 'self' => false, 'blocked' => false, 'archive' => false, 'deleted' => false,
172 'network' => Protocol::FEDERATED, 'pending' => false, 'contact-type' => Contact::TYPE_COMMUNITY], $params
175 $acl_contacts = array_merge($acl_forums, $acl_contacts);
177 array_walk($acl_contacts, function (&$value) {
178 $value['type'] = 'contact';
181 return $acl_contacts;
185 * Returns the ACL list of groups (including meta-groups) for a given user id
187 * @param int $user_id
190 public static function getGroupListByUserId(int $user_id)
194 'id' => Group::FOLLOWERS,
195 'name' => DI::l10n()->t('Followers'),
197 'micro' => 'images/twopeople.png',
201 'id' => Group::MUTUALS,
202 'name' => DI::l10n()->t('Mutuals'),
204 'micro' => 'images/twopeople.png',
208 foreach (Group::getByUserId($user_id) as $group) {
210 'id' => $group['id'],
211 'name' => $group['name'],
213 'micro' => 'images/twopeople.png',
222 * Return the full jot ACL selector HTML
225 * @param array $user User array
226 * @param bool $for_federation
227 * @param array $default_permissions Static defaults permission array:
234 * @param array $condition
235 * @param string $form_prefix
237 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
239 public static function getFullSelectorHTML(
242 bool $for_federation = false,
243 array $default_permissions = [],
244 array $condition = [],
247 if (empty($user['uid'])) {
251 static $input_group_id = 0;
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,