3 * @copyright Copyright (C) 2020, Friendica
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;
31 * Handle ACL management and display
36 * Returns a select input tag for private message recipient
38 * @param int $selected Existing recipien contact ID
42 public static function getMessageContactSelectHTML(int $selected = null)
48 $page->registerFooterScript(Theme::getPathForFile('asset/typeahead.js/dist/typeahead.bundle.js'));
49 $page->registerFooterScript(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput.js'));
50 $page->registerStylesheet(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput.css'));
51 $page->registerStylesheet(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput-typeahead.css'));
53 // When used for private messages, we limit correspondence to mutual DFRN/Friendica friends and the selector
54 // to one recipient. By default our selector allows multiple selects amongst all contacts.
56 'uid' => local_user(),
62 'rel' => [Contact::FOLLOWER, Contact::SHARING, Contact::FRIEND],
63 'network' => Protocol::FEDERATED,
66 $contacts = Contact::selectToArray(
67 ['id', 'name', 'addr', 'micro'],
68 DBA::mergeConditions($condition, ["`notify` != ''"])
71 $arr = ['contact' => $contacts, 'entry' => $o];
73 Hook::callAll(DI::module()->getName() . '_pre_recipient', $arr);
75 $tpl = Renderer::getMarkupTemplate('acl/message_recipient.tpl');
76 $o = Renderer::replaceMacros($tpl, [
77 '$contacts' => $contacts,
78 '$selected' => $selected,
81 Hook::callAll(DI::module()->getName() . '_post_recipient', $o);
87 * Returns a minimal ACL block for self-only permissions
89 * @param int $localUserId
90 * @param string $explanation
92 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
94 public static function getSelfOnlyHTML(int $localUserId, string $explanation)
96 $selfPublicContactId = Contact::getPublicIdByUserId($localUserId);
98 $tpl = Renderer::getMarkupTemplate('acl/self_only.tpl');
99 $o = Renderer::replaceMacros($tpl, [
100 '$selfPublicContactId' => $selfPublicContactId,
101 '$explanation' => $explanation,
108 * Return the default permission of the provided user array
111 * @return array Hash of contact id lists
114 public static function getDefaultUserPermissions(array $user = null)
116 $aclFormatter = DI::aclFormatter();
119 'allow_cid' => Contact::pruneUnavailable($aclFormatter->expand($user['allow_cid'] ?? '')),
120 'allow_gid' => $aclFormatter->expand($user['allow_gid'] ?? ''),
121 'deny_cid' => $aclFormatter->expand($user['deny_cid'] ?? ''),
122 'deny_gid' => $aclFormatter->expand($user['deny_gid'] ?? ''),
127 * Returns the ACL list of contacts for a given user id
129 * @param int $user_id
130 * @param array $condition Additional contact lookup table conditions
134 public static function getContactListByUserId(int $user_id, array $condition = [])
136 $fields = ['id', 'name', 'addr', 'micro'];
137 $params = ['order' => ['name']];
138 $acl_contacts = Contact::selectToArray(
147 'rel' => [Contact::FOLLOWER, Contact::FRIEND]
152 $acl_yourself = Contact::selectFirst($fields, ['uid' => $user_id, 'self' => true]);
153 $acl_yourself['name'] = DI::l10n()->t('Yourself');
155 $acl_contacts[] = $acl_yourself;
157 $acl_forums = Contact::selectToArray($fields,
158 ['uid' => $user_id, 'self' => false, 'blocked' => false, 'archive' => false, 'deleted' => false,
159 'pending' => false, 'contact-type' => Contact::TYPE_COMMUNITY], $params
162 $acl_contacts = array_merge($acl_forums, $acl_contacts);
164 array_walk($acl_contacts, function (&$value) {
165 $value['type'] = 'contact';
168 return $acl_contacts;
172 * Returns the ACL list of groups (including meta-groups) for a given user id
174 * @param int $user_id
177 public static function getGroupListByUserId(int $user_id)
181 'id' => Group::FOLLOWERS,
182 'name' => DI::l10n()->t('Followers'),
184 'micro' => 'images/twopeople.png',
188 'id' => Group::MUTUALS,
189 'name' => DI::l10n()->t('Mutuals'),
191 'micro' => 'images/twopeople.png',
195 foreach (Group::getByUserId($user_id) as $group) {
197 'id' => $group['id'],
198 'name' => $group['name'],
200 'micro' => 'images/twopeople.png',
209 * Return the full jot ACL selector HTML
212 * @param array $user User array
213 * @param bool $for_federation
214 * @param array $default_permissions Static defaults permission array:
221 * @param array $condition
222 * @param string $form_prefix
224 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
226 public static function getFullSelectorHTML(
229 bool $for_federation = false,
230 array $default_permissions = [],
231 array $condition = [],
234 if (empty($user['uid'])) {
238 static $input_group_id = 0;
242 $page->registerFooterScript(Theme::getPathForFile('asset/typeahead.js/dist/typeahead.bundle.js'));
243 $page->registerFooterScript(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput.js'));
244 $page->registerStylesheet(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput.css'));
245 $page->registerStylesheet(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput-typeahead.css'));
247 // Defaults user permissions
248 if (empty($default_permissions)) {
249 $default_permissions = self::getDefaultUserPermissions($user);
252 $default_permissions = [
253 'allow_cid' => $default_permissions['allow_cid'] ?? [],
254 'allow_gid' => $default_permissions['allow_gid'] ?? [],
255 'deny_cid' => $default_permissions['deny_cid'] ?? [],
256 'deny_gid' => $default_permissions['deny_gid'] ?? [],
259 if (count($default_permissions['allow_cid'])
260 + count($default_permissions['allow_gid'])
261 + count($default_permissions['deny_cid'])
262 + count($default_permissions['deny_gid'])) {
263 $visibility = 'custom';
265 $visibility = 'public';
266 // Default permission display for custom panel
267 $default_permissions['allow_gid'] = [Group::FOLLOWERS];
270 $jotnets_fields = [];
271 if ($for_federation) {
272 if (function_exists('imap_open') && !DI::config()->get('system', 'imap_disabled')) {
273 $mailacct = DBA::selectFirst('mailacct', ['pubmail'], ['`uid` = ? AND `server` != ""', $user['uid']]);
274 if (DBA::isResult($mailacct)) {
275 $jotnets_fields[] = [
276 'type' => 'checkbox',
279 DI::l10n()->t('Post to Email'),
280 !empty($mailacct['pubmail'])
286 Hook::callAll('jot_networks', $jotnets_fields);
289 $acl_contacts = self::getContactListByUserId($user['uid'], $condition);
291 $acl_groups = self::getGroupListByUserId($user['uid']);
293 $acl_list = array_merge($acl_groups, $acl_contacts);
296 'visibility' => $form_prefix ? $form_prefix . '[visibility]' : 'visibility',
297 'group_allow' => $form_prefix ? $form_prefix . '[group_allow]' : 'group_allow',
298 'contact_allow' => $form_prefix ? $form_prefix . '[contact_allow]' : 'contact_allow',
299 'group_deny' => $form_prefix ? $form_prefix . '[group_deny]' : 'group_deny',
300 'contact_deny' => $form_prefix ? $form_prefix . '[contact_deny]' : 'contact_deny',
301 'emailcc' => $form_prefix ? $form_prefix . '[emailcc]' : 'emailcc',
304 $tpl = Renderer::getMarkupTemplate('acl/full_selector.tpl');
305 $o = Renderer::replaceMacros($tpl, [
306 '$public_title' => DI::l10n()->t('Public'),
307 '$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.'),
308 '$custom_title' => DI::l10n()->t('Limited/Private'),
309 '$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.'),
310 '$allow_label' => DI::l10n()->t('Show to:'),
311 '$deny_label' => DI::l10n()->t('Except to:'),
312 '$emailcc' => DI::l10n()->t('CC: email addresses'),
313 '$emtitle' => DI::l10n()->t('Example: bob@example.com, mary@example.com'),
314 '$jotnets_summary' => DI::l10n()->t('Connectors'),
315 '$visibility' => $visibility,
316 '$acl_contacts' => $acl_contacts,
317 '$acl_groups' => $acl_groups,
318 '$acl_list' => $acl_list,
319 '$contact_allow' => implode(',', $default_permissions['allow_cid']),
320 '$group_allow' => implode(',', $default_permissions['allow_gid']),
321 '$contact_deny' => implode(',', $default_permissions['deny_cid']),
322 '$group_deny' => implode(',', $default_permissions['deny_gid']),
323 '$for_federation' => $for_federation,
324 '$jotnets_fields' => $jotnets_fields,
325 '$input_names' => $input_names,
326 '$input_group_id' => $input_group_id,