4 * @file src/Core/Acl.php
7 namespace Friendica\Core;
9 use Friendica\BaseObject;
10 use Friendica\Content\Feature;
11 use Friendica\Database\DBA;
12 use Friendica\Model\Contact;
13 use Friendica\Model\GContact;
14 use Friendica\Util\Network;
17 * Handle ACL management and display
19 * @author Hypolite Petovan <hypolite@mrpetovan.com>
21 class ACL extends BaseObject
24 * Returns a select input tag with all the contact of the local user
26 * @param string $selname Name attribute of the select input tag
27 * @param string $selclass Class attribute of the select input tag
28 * @param array $options Available options:
29 * - size: length of the select box
30 * - mutual_friends: Only used for the hook
31 * - single: Only used for the hook
32 * - exclude: Only used for the hook
33 * @param array $preselected Contact ID that should be already selected
37 public static function getSuggestContactSelectHTML($selname, $selclass, array $options = [], array $preselected = [])
43 $size = defaults($options, 'size', 4);
44 $mutual = !empty($options['mutual_friends']);
45 $single = !empty($options['single']) && empty($options['multiple']);
46 $exclude = defaults($options, 'exclude', false);
48 switch (defaults($options, 'networks', Protocol::PHANTOM)) {
50 $networks = [Protocol::DFRN];
54 $networks = [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::MAIL, Protocol::DIASPORA];
58 if (!empty($a->user['prvnets'])) {
59 $networks = [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::MAIL, Protocol::DIASPORA];
61 $networks = [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::MAIL, Protocol::DIASPORA, Protocol::OSTATUS];
65 default: /// @TODO Maybe log this call?
69 $x = ['options' => $options, 'size' => $size, 'single' => $single, 'mutual' => $mutual, 'exclude' => $exclude, 'networks' => $networks];
71 Hook::callAll('contact_select_options', $x);
77 if (!empty($x['mutual'])) {
78 $sql_extra .= sprintf(" AND `rel` = %d ", intval(Contact::FRIEND));
81 if (!empty($x['exclude'])) {
82 $sql_extra .= sprintf(" AND `id` != %d ", intval($x['exclude']));
85 if (!empty($x['networks'])) {
86 /// @TODO rewrite to foreach()
87 array_walk($x['networks'], function (&$value) {
88 $value = "'" . DBA::escape($value) . "'";
90 $str_nets = implode(',', $x['networks']);
91 $sql_extra .= " AND `network` IN ( $str_nets ) ";
94 $tabindex = (!empty($options['tabindex']) ? 'tabindex="' . $options["tabindex"] . '"' : '');
96 if (!empty($x['single'])) {
97 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"" . $x['size'] . "\" $tabindex >\r\n";
99 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"" . $x['size'] . "$\" $tabindex >\r\n";
102 $stmt = DBA::p("SELECT `id`, `name`, `url`, `network` FROM `contact`
103 WHERE `uid` = ? AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND NOT `deleted` AND `notify` != ''
105 ORDER BY `name` ASC ", intval(local_user())
108 $contacts = DBA::toArray($stmt);
110 $arr = ['contact' => $contacts, 'entry' => $o];
112 // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
113 Hook::callAll($a->module . '_pre_' . $selname, $arr);
115 if (DBA::isResult($contacts)) {
116 foreach ($contacts as $contact) {
117 if (in_array($contact['id'], $preselected)) {
118 $selected = ' selected="selected" ';
123 $trimmed = mb_substr($contact['name'], 0, 20);
125 $o .= "<option value=\"{$contact['id']}\" $selected title=\"{$contact['name']}|{$contact['url']}\" >$trimmed</option>\r\n";
129 $o .= '</select>' . PHP_EOL;
131 Hook::callAll($a->module . '_post_' . $selname, $o);
137 * Returns a select input tag with all the contact of the local user
139 * @param string $selname Name attribute of the select input tag
140 * @param string $selclass Class attribute of the select input tag
141 * @param array $preselected Contact IDs that should be already selected
142 * @param int $size Length of the select box
143 * @param int $tabindex Select input tag tabindex attribute
147 public static function getMessageContactSelectHTML($selname, $selclass, array $preselected = [], $size = 4, $tabindex = null)
153 // When used for private messages, we limit correspondence to mutual DFRN/Friendica friends and the selector
154 // to one recipient. By default our selector allows multiple selects amongst all contacts.
155 $sql_extra = sprintf(" AND `rel` = %d ", intval(Contact::FRIEND));
156 $sql_extra .= sprintf(" AND `network` IN ('%s' , '%s') ", Protocol::DFRN, Protocol::DIASPORA);
158 $tabindex_attr = !empty($tabindex) ? ' tabindex="' . intval($tabindex) . '"' : '';
160 $hidepreselected = '';
162 $sql_extra .= " AND `id` IN (" . implode(",", $preselected) . ")";
163 $hidepreselected = ' style="display: none;"';
166 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"$size\"$tabindex_attr$hidepreselected>\r\n";
168 $stmt = DBA::p("SELECT `id`, `name`, `url`, `network` FROM `contact`
169 WHERE `uid` = ? AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND NOT `deleted` AND `notify` != ''
171 ORDER BY `name` ASC ", intval(local_user())
174 $contacts = DBA::toArray($stmt);
176 $arr = ['contact' => $contacts, 'entry' => $o];
178 // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
179 Hook::callAll($a->module . '_pre_' . $selname, $arr);
183 if (DBA::isResult($contacts)) {
184 foreach ($contacts as $contact) {
185 if (in_array($contact['id'], $preselected)) {
186 $selected = ' selected="selected"';
191 $trimmed = Protocol::formatMention($contact['url'], $contact['name']);
193 $receiverlist[] = $trimmed;
195 $o .= "<option value=\"{$contact['id']}\"$selected title=\"{$contact['name']}|{$contact['url']}\" >$trimmed</option>\r\n";
199 $o .= '</select>' . PHP_EOL;
202 $o .= implode(', ', $receiverlist);
205 Hook::callAll($a->module . '_post_' . $selname, $o);
210 private static function fixACL(&$item)
212 $item = intval(str_replace(['<', '>'], ['', ''], $item));
216 * Return the default permission of the provided user array
219 * @return array Hash of contact id lists
222 public static function getDefaultUserPermissions(array $user = null)
226 $acl_regex = '/<([0-9]+)>/i';
228 preg_match_all($acl_regex, defaults($user, 'allow_cid', ''), $matches);
229 $allow_cid = $matches[1];
230 preg_match_all($acl_regex, defaults($user, 'allow_gid', ''), $matches);
231 $allow_gid = $matches[1];
232 preg_match_all($acl_regex, defaults($user, 'deny_cid', ''), $matches);
233 $deny_cid = $matches[1];
234 preg_match_all($acl_regex, defaults($user, 'deny_gid', ''), $matches);
235 $deny_gid = $matches[1];
237 // Reformats the ACL data so that it is accepted by the JS frontend
238 array_walk($allow_cid, 'self::fixACL');
239 array_walk($allow_gid, 'self::fixACL');
240 array_walk($deny_cid, 'self::fixACL');
241 array_walk($deny_gid, 'self::fixACL');
243 Contact::pruneUnavailable($allow_cid);
246 'allow_cid' => $allow_cid,
247 'allow_gid' => $allow_gid,
248 'deny_cid' => $deny_cid,
249 'deny_gid' => $deny_gid,
254 * Return the full jot ACL selector HTML
256 * @param array $user User array
257 * @param bool $show_jotnets
258 * @param array $default_permissions Static defaults permission array: ['allow_cid' => '', 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => '']
260 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
262 public static function getFullSelectorHTML(array $user = null, $show_jotnets = false, array $default_permissions = [])
264 // Defaults user permissions
265 if (empty($default_permissions)) {
266 $default_permissions = self::getDefaultUserPermissions($user);
269 $jotnets_fields = [];
271 $mail_enabled = false;
272 $pubmail_enabled = false;
274 if (function_exists('imap_open') && !Config::get('system', 'imap_disabled')) {
275 $mailacct = DBA::selectFirst('mailacct', ['pubmail'], ['`uid` = ? AND `server` != ""', local_user()]);
276 if (DBA::isResult($mailacct)) {
277 $mail_enabled = true;
278 $pubmail_enabled = !empty($mailacct['pubmail']);
282 if (empty($default_permissions['hidewall'])) {
284 $jotnets_fields[] = [
285 'type' => 'checkbox',
288 L10n::t('Post to Email'),
294 Hook::callAll('jot_networks', $jotnets_fields);
298 $tpl = Renderer::getMarkupTemplate('acl_selector.tpl');
299 $o = Renderer::replaceMacros($tpl, [
300 '$showall' => L10n::t('Visible to everybody'),
301 '$show' => L10n::t('show'),
302 '$hide' => L10n::t('don\'t show'),
303 '$allowcid' => json_encode(defaults($default_permissions, 'allow_cid', [])), // we need arrays for Javascript since we call .remove() and .push() on this values
304 '$allowgid' => json_encode(defaults($default_permissions, 'allow_gid', [])),
305 '$denycid' => json_encode(defaults($default_permissions, 'deny_cid', [])),
306 '$denygid' => json_encode(defaults($default_permissions, 'deny_gid', [])),
307 '$networks' => $show_jotnets,
308 '$emailcc' => L10n::t('CC: email addresses'),
309 '$emtitle' => L10n::t('Example: bob@example.com, mary@example.com'),
310 '$jotnets_enabled' => empty($default_permissions['hidewall']),
311 '$jotnets_summary' => L10n::t('Connectors'),
312 '$jotnets_fields' => $jotnets_fields,
313 '$jotnets_disabled_label' => L10n::t('Connectors disabled, since "%s" is enabled.', L10n::t('Hide your profile details from unknown viewers?')),
314 '$aclModalTitle' => L10n::t('Permissions'),
315 '$aclModalDismiss' => L10n::t('Close'),
317 'aclautomention' => !empty($user['uid']) && Feature::isEnabled($user['uid'], 'aclautomention') ? 'true' : 'false'
325 * Searching for global contacts for autocompletion
327 * @brief Searching for global contacts for autocompletion
328 * @param string $search Name or part of a name or nick
329 * @param string $mode Search mode (e.g. "community")
330 * @param int $page Page number (starts at 1)
331 * @return array with the search results
332 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
334 public static function contactAutocomplete($search, $mode, int $page = 1)
336 if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
340 // don't search if search term has less than 2 characters
341 if (!$search || mb_strlen($search) < 2) {
345 if (substr($search, 0, 1) === '@') {
346 $search = substr($search, 1);
349 // check if searching in the local global contact table is enabled
350 if (Config::get('system', 'poco_local_search')) {
351 $return = GContact::searchByName($search, $mode);
353 $p = $page > 1 ? 'p=' . $page : '';
355 $curlResult = Network::curl(get_server() . '/lsearch?' . $p . '&search=' . urlencode($search));
356 if ($curlResult->isSuccess()) {
357 $lsearch = json_decode($curlResult->getBody(), true);
358 if (!empty($lsearch['results'])) {
359 $return = $lsearch['results'];
364 return $return ?? [];