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 with all the contact of the local user
38 * @param string $selname Name attribute of the select input tag
39 * @param string $selclass Class attribute of the select input tag
40 * @param array $preselected Contact IDs that should be already selected
41 * @param int $size Length of the select box
42 * @param int $tabindex Select input tag tabindex attribute
46 public static function getMessageContactSelectHTML($selname, $selclass, array $preselected = [], $size = 4, $tabindex = null)
52 // When used for private messages, we limit correspondence to mutual DFRN/Friendica friends and the selector
53 // to one recipient. By default our selector allows multiple selects amongst all contacts.
54 $sql_extra = sprintf(" AND `rel` = %d ", intval(Contact::FRIEND));
55 $sql_extra .= sprintf(" AND `network` IN ('%s' , '%s') ", Protocol::DFRN, Protocol::DIASPORA);
57 $tabindex_attr = !empty($tabindex) ? ' tabindex="' . intval($tabindex) . '"' : '';
59 $hidepreselected = '';
61 $sql_extra .= " AND `id` IN (" . implode(",", $preselected) . ")";
62 $hidepreselected = ' style="display: none;"';
65 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"$size\"$tabindex_attr$hidepreselected>\r\n";
67 $stmt = DBA::p("SELECT `id`, `name`, `url`, `network` FROM `contact`
68 WHERE `uid` = ? AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND NOT `deleted` AND `notify` != ''
70 ORDER BY `name` ASC ", intval(local_user())
73 $contacts = DBA::toArray($stmt);
75 $arr = ['contact' => $contacts, 'entry' => $o];
77 // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
78 Hook::callAll(DI::module()->getName() . '_pre_' . $selname, $arr);
82 if (DBA::isResult($contacts)) {
83 foreach ($contacts as $contact) {
84 if (in_array($contact['id'], $preselected)) {
85 $selected = ' selected="selected"';
90 $trimmed = Protocol::formatMention($contact['url'], $contact['name']);
92 $receiverlist[] = $trimmed;
94 $o .= "<option value=\"{$contact['id']}\"$selected title=\"{$contact['name']}|{$contact['url']}\" >$trimmed</option>\r\n";
98 $o .= '</select>' . PHP_EOL;
101 $o .= implode(', ', $receiverlist);
104 Hook::callAll(DI::module()->getName() . '_post_' . $selname, $o);
110 * Return the default permission of the provided user array
113 * @return array Hash of contact id lists
116 public static function getDefaultUserPermissions(array $user = null)
118 $aclFormatter = DI::aclFormatter();
121 'allow_cid' => Contact::pruneUnavailable($aclFormatter->expand($user['allow_cid'] ?? '')),
122 'allow_gid' => $aclFormatter->expand($user['allow_gid'] ?? ''),
123 'deny_cid' => $aclFormatter->expand($user['deny_cid'] ?? ''),
124 'deny_gid' => $aclFormatter->expand($user['deny_gid'] ?? ''),
129 * Returns the ACL list of contacts for a given user id
131 * @param int $user_id
132 * @param array $condition Additional contact lookup table conditions
136 public static function getContactListByUserId(int $user_id, array $condition = [])
138 $fields = ['id', 'name', 'addr', 'micro'];
139 $params = ['order' => ['name']];
140 $acl_contacts = Contact::selectToArray(
149 'rel' => [Contact::FOLLOWER, Contact::FRIEND]
154 $acl_yourself = Contact::selectFirst($fields, ['uid' => $user_id, 'self' => true]);
155 $acl_yourself['name'] = DI::l10n()->t('Yourself');
157 $acl_contacts[] = $acl_yourself;
159 $acl_forums = Contact::selectToArray($fields,
160 ['uid' => $user_id, 'self' => false, 'blocked' => false, 'archive' => false, 'deleted' => false,
161 'pending' => false, 'contact-type' => Contact::TYPE_COMMUNITY], $params
164 $acl_contacts = array_merge($acl_forums, $acl_contacts);
166 array_walk($acl_contacts, function (&$value) {
167 $value['type'] = 'contact';
170 return $acl_contacts;
174 * Returns the ACL list of groups (including meta-groups) for a given user id
176 * @param int $user_id
179 public static function getGroupListByUserId(int $user_id)
183 'id' => Group::FOLLOWERS,
184 'name' => DI::l10n()->t('Followers'),
186 'micro' => 'images/twopeople.png',
190 'id' => Group::MUTUALS,
191 'name' => DI::l10n()->t('Mutuals'),
193 'micro' => 'images/twopeople.png',
197 foreach (Group::getByUserId($user_id) as $group) {
199 'id' => $group['id'],
200 'name' => $group['name'],
202 'micro' => 'images/twopeople.png',
211 * Return the full jot ACL selector HTML
214 * @param array $user User array
215 * @param bool $for_federation
216 * @param array $default_permissions Static defaults permission array:
223 * @param array $condition
224 * @param string $form_prefix
226 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
228 public static function getFullSelectorHTML(
231 bool $for_federation = false,
232 array $default_permissions = [],
233 array $condition = [],
236 if (empty($user['uid'])) {
240 static $input_group_id = 0;
244 $page->registerFooterScript(Theme::getPathForFile('asset/typeahead.js/dist/typeahead.bundle.js'));
245 $page->registerFooterScript(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput.js'));
246 $page->registerStylesheet(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput.css'));
247 $page->registerStylesheet(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput-typeahead.css'));
249 // Defaults user permissions
250 if (empty($default_permissions)) {
251 $default_permissions = self::getDefaultUserPermissions($user);
254 $default_permissions = [
255 'allow_cid' => $default_permissions['allow_cid'] ?? [],
256 'allow_gid' => $default_permissions['allow_gid'] ?? [],
257 'deny_cid' => $default_permissions['deny_cid'] ?? [],
258 'deny_gid' => $default_permissions['deny_gid'] ?? [],
261 if (count($default_permissions['allow_cid'])
262 + count($default_permissions['allow_gid'])
263 + count($default_permissions['deny_cid'])
264 + count($default_permissions['deny_gid'])) {
265 $visibility = 'custom';
267 $visibility = 'public';
268 // Default permission display for custom panel
269 $default_permissions['allow_gid'] = [Group::FOLLOWERS];
272 $jotnets_fields = [];
273 if ($for_federation) {
274 if (function_exists('imap_open') && !DI::config()->get('system', 'imap_disabled')) {
275 $mailacct = DBA::selectFirst('mailacct', ['pubmail'], ['`uid` = ? AND `server` != ""', $user['uid']]);
276 if (DBA::isResult($mailacct)) {
277 $jotnets_fields[] = [
278 'type' => 'checkbox',
281 DI::l10n()->t('Post to Email'),
282 !empty($mailacct['pubmail'])
288 Hook::callAll('jot_networks', $jotnets_fields);
291 $acl_contacts = self::getContactListByUserId($user['uid'], $condition);
293 $acl_groups = self::getGroupListByUserId($user['uid']);
295 $acl_list = array_merge($acl_groups, $acl_contacts);
298 'visibility' => $form_prefix ? $form_prefix . '[visibility]' : 'visibility',
299 'group_allow' => $form_prefix ? $form_prefix . '[group_allow]' : 'group_allow',
300 'contact_allow' => $form_prefix ? $form_prefix . '[contact_allow]' : 'contact_allow',
301 'group_deny' => $form_prefix ? $form_prefix . '[group_deny]' : 'group_deny',
302 'contact_deny' => $form_prefix ? $form_prefix . '[contact_deny]' : 'contact_deny',
303 'emailcc' => $form_prefix ? $form_prefix . '[emailcc]' : 'emailcc',
306 $tpl = Renderer::getMarkupTemplate('acl_selector.tpl');
307 $o = Renderer::replaceMacros($tpl, [
308 '$public_title' => DI::l10n()->t('Public'),
309 '$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.'),
310 '$custom_title' => DI::l10n()->t('Limited/Private'),
311 '$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.'),
312 '$allow_label' => DI::l10n()->t('Show to:'),
313 '$deny_label' => DI::l10n()->t('Except to:'),
314 '$emailcc' => DI::l10n()->t('CC: email addresses'),
315 '$emtitle' => DI::l10n()->t('Example: bob@example.com, mary@example.com'),
316 '$jotnets_summary' => DI::l10n()->t('Connectors'),
317 '$visibility' => $visibility,
318 '$acl_contacts' => $acl_contacts,
319 '$acl_groups' => $acl_groups,
320 '$acl_list' => $acl_list,
321 '$contact_allow' => implode(',', $default_permissions['allow_cid']),
322 '$group_allow' => implode(',', $default_permissions['allow_gid']),
323 '$contact_deny' => implode(',', $default_permissions['deny_cid']),
324 '$group_deny' => implode(',', $default_permissions['deny_gid']),
325 '$for_federation' => $for_federation,
326 '$jotnets_fields' => $jotnets_fields,
327 '$input_names' => $input_names,
328 '$input_group_id' => $input_group_id,