3 * @copyright Copyright (C) 2010-2022, 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): string
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 $contacts = self::getValidMessageRecipientsForUser(DI::userSession()->getLocalUserId());
67 $tpl = Renderer::getMarkupTemplate('acl/message_recipient.tpl');
68 $o = Renderer::replaceMacros($tpl, [
69 '$contacts' => $contacts,
70 '$selected' => $selected,
73 Hook::callAll(DI::args()->getModuleName() . '_post_recipient', $o);
78 public static function getValidMessageRecipientsForUser(int $uid): array
87 'rel' => [Contact::FOLLOWER, Contact::SHARING, Contact::FRIEND],
88 'network' => Protocol::SUPPORT_PRIVATE,
91 return Contact::selectToArray(
92 ['id', 'name', 'addr', 'micro', 'url', 'nick'],
93 DBA::mergeConditions($condition, ["`notify` != ''"])
98 * Returns a minimal ACL block for self-only permissions
100 * @param int $localUserId
101 * @param string $explanation
103 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
105 public static function getSelfOnlyHTML(int $localUserId, string $explanation)
107 $selfPublicContactId = Contact::getPublicIdByUserId($localUserId);
109 $tpl = Renderer::getMarkupTemplate('acl/self_only.tpl');
110 $o = Renderer::replaceMacros($tpl, [
111 '$selfPublicContactId' => $selfPublicContactId,
112 '$explanation' => $explanation,
119 * Return the default permission of the provided user array
122 * @return array Hash of contact id lists
125 public static function getDefaultUserPermissions(array $user = null)
127 $aclFormatter = DI::aclFormatter();
130 'allow_cid' => Contact::pruneUnavailable($aclFormatter->expand($user['allow_cid'] ?? '')),
131 'allow_gid' => $aclFormatter->expand($user['allow_gid'] ?? ''),
132 'deny_cid' => $aclFormatter->expand($user['deny_cid'] ?? ''),
133 'deny_gid' => $aclFormatter->expand($user['deny_gid'] ?? ''),
138 * Returns the ACL list of contacts for a given user id
140 * @param int $user_id
141 * @param array $condition Additional contact lookup table conditions
145 public static function getContactListByUserId(int $user_id, array $condition = [])
147 $fields = ['id', 'name', 'addr', 'micro'];
148 $params = ['order' => ['name']];
149 $acl_contacts = Contact::selectToArray(
158 'network' => Protocol::FEDERATED,
159 'rel' => [Contact::FOLLOWER, Contact::FRIEND]
164 $acl_yourself = Contact::selectFirst($fields, ['uid' => $user_id, 'self' => true]);
165 $acl_yourself['name'] = DI::l10n()->t('Yourself');
167 $acl_contacts[] = $acl_yourself;
169 $acl_forums = Contact::selectToArray($fields,
170 ['uid' => $user_id, 'self' => false, 'blocked' => false, 'archive' => false, 'deleted' => false,
171 'network' => Protocol::FEDERATED, 'pending' => false, 'contact-type' => Contact::TYPE_COMMUNITY], $params
174 $acl_contacts = array_merge($acl_forums, $acl_contacts);
176 array_walk($acl_contacts, function (&$value) {
177 $value['type'] = 'contact';
180 return $acl_contacts;
184 * Returns the ACL list of groups (including meta-groups) for a given user id
186 * @param int $user_id
189 public static function getGroupListByUserId(int $user_id)
193 'id' => Group::FOLLOWERS,
194 'name' => DI::l10n()->t('Followers'),
196 'micro' => 'images/twopeople.png',
200 'id' => Group::MUTUALS,
201 'name' => DI::l10n()->t('Mutuals'),
203 'micro' => 'images/twopeople.png',
207 foreach (Group::getByUserId($user_id) as $group) {
209 'id' => $group['id'],
210 'name' => $group['name'],
212 'micro' => 'images/twopeople.png',
221 * Return the full jot ACL selector HTML
224 * @param int $uid User ID
225 * @param bool $for_federation
226 * @param array $default_permissions Static defaults permission array:
233 * @param array $condition
234 * @param string $form_prefix
236 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
238 public static function getFullSelectorHTML(
241 bool $for_federation = false,
242 array $default_permissions = [],
243 array $condition = [],
250 static $input_group_id = 0;
252 $user = User::getById($uid);
256 $page->registerFooterScript(Theme::getPathForFile('asset/typeahead.js/dist/typeahead.bundle.js'));
257 $page->registerFooterScript(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput.js'));
258 $page->registerStylesheet(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput.css'));
259 $page->registerStylesheet(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput-typeahead.css'));
261 // Defaults user permissions
262 if (empty($default_permissions)) {
263 $default_permissions = self::getDefaultUserPermissions($user);
266 $default_permissions = [
267 'allow_cid' => $default_permissions['allow_cid'] ?? [],
268 'allow_gid' => $default_permissions['allow_gid'] ?? [],
269 'deny_cid' => $default_permissions['deny_cid'] ?? [],
270 'deny_gid' => $default_permissions['deny_gid'] ?? [],
273 if (count($default_permissions['allow_cid'])
274 + count($default_permissions['allow_gid'])
275 + count($default_permissions['deny_cid'])
276 + count($default_permissions['deny_gid'])) {
277 $visibility = 'custom';
279 $visibility = 'public';
280 // Default permission display for custom panel
281 $default_permissions['allow_gid'] = [Group::FOLLOWERS];
284 $jotnets_fields = [];
285 if ($for_federation) {
286 if (function_exists('imap_open') && !DI::config()->get('system', 'imap_disabled')) {
287 $mailacct = DBA::selectFirst('mailacct', ['pubmail'], ['`uid` = ? AND `server` != ""', $user['uid']]);
288 if (DBA::isResult($mailacct)) {
289 $jotnets_fields[] = [
290 'type' => 'checkbox',
293 DI::l10n()->t('Post to Email'),
294 !empty($mailacct['pubmail'])
300 Hook::callAll('jot_networks', $jotnets_fields);
303 $acl_contacts = self::getContactListByUserId($user['uid'], $condition);
305 $acl_groups = self::getGroupListByUserId($user['uid']);
307 $acl_list = array_merge($acl_groups, $acl_contacts);
310 'visibility' => $form_prefix ? $form_prefix . '[visibility]' : 'visibility',
311 'group_allow' => $form_prefix ? $form_prefix . '[group_allow]' : 'group_allow',
312 'contact_allow' => $form_prefix ? $form_prefix . '[contact_allow]' : 'contact_allow',
313 'group_deny' => $form_prefix ? $form_prefix . '[group_deny]' : 'group_deny',
314 'contact_deny' => $form_prefix ? $form_prefix . '[contact_deny]' : 'contact_deny',
315 'emailcc' => $form_prefix ? $form_prefix . '[emailcc]' : 'emailcc',
318 $tpl = Renderer::getMarkupTemplate('acl/full_selector.tpl');
319 $o = Renderer::replaceMacros($tpl, [
320 '$public_title' => DI::l10n()->t('Public'),
321 '$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.'),
322 '$custom_title' => DI::l10n()->t('Limited/Private'),
323 '$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.') . DI::l10n()->t('Start typing the name of a contact or a group to show a filtered list. You can also mention the special groups "Followers" and "Mutuals".'),
324 '$allow_label' => DI::l10n()->t('Show to:'),
325 '$deny_label' => DI::l10n()->t('Except to:'),
326 '$emailcc' => DI::l10n()->t('CC: email addresses'),
327 '$emtitle' => DI::l10n()->t('Example: bob@example.com, mary@example.com'),
328 '$jotnets_summary' => DI::l10n()->t('Connectors'),
329 '$visibility' => $visibility,
330 '$acl_contacts' => $acl_contacts,
331 '$acl_groups' => $acl_groups,
332 '$acl_list' => $acl_list,
333 '$contact_allow' => implode(',', $default_permissions['allow_cid']),
334 '$group_allow' => implode(',', $default_permissions['allow_gid']),
335 '$contact_deny' => implode(',', $default_permissions['deny_cid']),
336 '$group_deny' => implode(',', $default_permissions['deny_gid']),
337 '$for_federation' => $for_federation,
338 '$jotnets_fields' => $jotnets_fields,
339 '$input_names' => $input_names,
340 '$input_group_id' => $input_group_id,
347 * Checks the validity of the given ACL string
349 * @param string $acl_string
354 public static function isValidContact($acl_string, $uid)
356 if (empty($acl_string)) {
360 // split <x><y><z> into array of cids
361 preg_match_all('/<[A-Za-z0-9]+>/', $acl_string, $array);
363 // check for each cid if the contact is valid for the given user
364 $cid_array = $array[0];
365 foreach ($cid_array as $cid) {
366 $cid = str_replace(['<', '>'], ['', ''], $cid);
367 if (!DBA::exists('contact', ['id' => $cid, 'uid' => $uid])) {
376 * Checks the validity of the given ACL string
378 * @param string $acl_string
383 public static function isValidGroup($acl_string, $uid)
385 if (empty($acl_string)) {
389 // split <x><y><z> into array of cids
390 preg_match_all('/<[A-Za-z0-9]+>/', $acl_string, $array);
392 // check for each cid if the contact is valid for the given user
393 $gid_array = $array[0];
394 foreach ($gid_array as $gid) {
395 $gid = str_replace(['<', '>'], ['', ''], $gid);
396 if (!DBA::exists('group', ['id' => $gid, 'uid' => $uid, 'deleted' => false])) {