3 * @file include/acl_selectors.php
7 use Friendica\Content\Feature;
8 use Friendica\Content\Widget;
9 use Friendica\Core\Addon;
10 use Friendica\Core\Config;
11 use Friendica\Core\L10n;
12 use Friendica\Core\Protocol;
13 use Friendica\Database\DBM;
14 use Friendica\Model\Contact;
15 use Friendica\Model\GContact;
16 use Friendica\Util\Network;
18 require_once "mod/proxy.php";
21 * @package acl_selectors
23 function group_select($selname,$selclass,$preselected = false,$size = 4) {
29 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"$size\" >\r\n";
31 $r = q("SELECT `id`, `name` FROM `group` WHERE NOT `deleted` AND `uid` = %d ORDER BY `name` ASC",
36 $arr = ['group' => $r, 'entry' => $o];
38 // e.g. 'network_pre_group_deny', 'profile_pre_group_allow'
40 Addon::callHooks($a->module . '_pre_' . $selname, $arr);
42 if (DBM::is_result($r)) {
44 if ((is_array($preselected)) && in_array($rr['id'], $preselected)) {
45 $selected = " selected=\"selected\" ";
50 $trimmed = mb_substr($rr['name'],0,12);
52 $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}\" >$trimmed</option>\r\n";
56 $o .= "</select>\r\n";
58 Addon::callHooks($a->module . '_post_' . $selname, $o);
64 /// @TODO find proper type-hints
65 function contact_selector($selname, $selclass, $options, $preselected = false)
75 if (is_array($options)) {
76 if (x($options, 'size'))
77 $size = $options['size'];
79 if (x($options, 'mutual_friends')) {
82 if (x($options, 'single')) {
85 if (x($options, 'multiple')) {
88 if (x($options, 'exclude')) {
89 $exclude = $options['exclude'];
92 if (x($options, 'networks')) {
93 switch ($options['networks']) {
95 $networks = [NETWORK_DFRN];
98 if (is_array($a->user) && $a->user['prvnets']) {
99 $networks = [NETWORK_DFRN, NETWORK_MAIL, NETWORK_DIASPORA];
101 $networks = [NETWORK_DFRN, NETWORK_FACEBOOK, NETWORK_MAIL, NETWORK_DIASPORA];
105 if (is_array($a->user) && $a->user['prvnets']) {
106 $networks = [NETWORK_DFRN, NETWORK_MAIL, NETWORK_DIASPORA];
108 $networks = [NETWORK_DFRN, NETWORK_FACEBOOK, NETWORK_MAIL, NETWORK_DIASPORA, NETWORK_OSTATUS];
111 default: /// @TODO Maybe log this call?
117 $x = ['options' => $options, 'size' => $size, 'single' => $single, 'mutual' => $mutual, 'exclude' => $exclude, 'networks' => $networks];
119 Addon::callHooks('contact_select_options', $x);
125 if (x($x, 'mutual')) {
126 $sql_extra .= sprintf(" AND `rel` = %d ", intval(CONTACT_IS_FRIEND));
129 if (x($x, 'exclude')) {
130 $sql_extra .= sprintf(" AND `id` != %d ", intval($x['exclude']));
133 if (is_array($x['networks']) && count($x['networks'])) {
134 /// @TODO rewrite to foreach()
135 for ($y = 0; $y < count($x['networks']) ; $y ++) {
136 $x['networks'][$y] = "'" . dbesc($x['networks'][$y]) . "'";
138 $str_nets = implode(',', $x['networks']);
139 $sql_extra .= " AND `network` IN ( $str_nets ) ";
142 $tabindex = (x($options, 'tabindex') ? "tabindex=\"" . $options["tabindex"] . "\"" : "");
145 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"" . $x['size'] . "\" $tabindex >\r\n";
147 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"" . $x['size'] . "$\" $tabindex >\r\n";
150 $r = q("SELECT `id`, `name`, `url`, `network` FROM `contact`
151 WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
153 ORDER BY `name` ASC ",
158 $arr = ['contact' => $r, 'entry' => $o];
160 // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
162 Addon::callHooks($a->module . '_pre_' . $selname, $arr);
164 if (DBM::is_result($r)) {
165 foreach ($r as $rr) {
166 if ((is_array($preselected)) && in_array($rr['id'], $preselected)) {
167 $selected = " selected=\"selected\" ";
172 $trimmed = mb_substr($rr['name'],0,20);
174 $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}|{$rr['url']}\" >$trimmed</option>\r\n";
179 $o .= "</select>\r\n";
181 Addon::callHooks($a->module . '_post_' . $selname, $o);
188 function contact_select($selname, $selclass, $preselected = false, $size = 4, $privmail = false, $celeb = false, $privatenet = false, $tabindex = null) {
190 require_once "include/bbcode.php";
196 // When used for private messages, we limit correspondence to mutual DFRN/Friendica friends and the selector
197 // to one recipient. By default our selector allows multiple selects amongst all contacts.
201 if ($privmail || $celeb) {
202 $sql_extra .= sprintf(" AND `rel` = %d ", intval(CONTACT_IS_FRIEND));
206 $sql_extra .= sprintf(" AND `network` IN ('%s' , '%s') ",
207 NETWORK_DFRN, NETWORK_DIASPORA);
208 } elseif ($privatenet) {
209 $sql_extra .= sprintf(" AND `network` IN ('%s' , '%s', '%s', '%s') ",
210 NETWORK_DFRN, NETWORK_MAIL, NETWORK_FACEBOOK, NETWORK_DIASPORA);
213 $tabindex = ($tabindex > 0 ? "tabindex=\"$tabindex\"" : "");
215 if ($privmail && $preselected) {
216 $sql_extra .= " AND `id` IN (".implode(",", $preselected).")";
217 $hidepreselected = ' style="display: none;"';
219 $hidepreselected = "";
223 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"$size\" $tabindex $hidepreselected>\r\n";
225 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"$size\" $tabindex >\r\n";
228 $r = q("SELECT `id`, `name`, `url`, `network` FROM `contact`
229 WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
231 ORDER BY `name` ASC ",
236 $arr = ['contact' => $r, 'entry' => $o];
238 // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
240 Addon::callHooks($a->module . '_pre_' . $selname, $arr);
244 if (DBM::is_result($r)) {
245 foreach ($r as $rr) {
246 if ((is_array($preselected)) && in_array($rr['id'], $preselected)) {
247 $selected = " selected=\"selected\" ";
253 $trimmed = Protocol::formatMention($rr['url'], $rr['name']);
255 $trimmed = mb_substr($rr['name'],0,20);
258 $receiverlist[] = $trimmed;
260 $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}|{$rr['url']}\" >$trimmed</option>\r\n";
265 $o .= "</select>\r\n";
267 if ($privmail && $preselected) {
268 $o .= implode(", ", $receiverlist);
271 Addon::callHooks($a->module . '_post_' . $selname, $o);
277 function fixacl(&$item) {
278 $item = intval(str_replace(['<', '>'], ['', ''], $item));
281 function prune_deadguys($arr) {
287 $str = dbesc(implode(',', $arr));
289 $r = q("SELECT `id` FROM `contact` WHERE `id` IN ( " . $str . ") AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 ");
291 if (DBM::is_result($r)) {
293 foreach ($r as $rr) {
294 $ret[] = intval($rr['id']);
303 function get_acl_permissions($user = null) {
304 $allow_cid = $allow_gid = $deny_cid = $deny_gid = false;
306 if (is_array($user)) {
307 $allow_cid = ((strlen($user['allow_cid']))
308 ? explode('><', $user['allow_cid']) : [] );
309 $allow_gid = ((strlen($user['allow_gid']))
310 ? explode('><', $user['allow_gid']) : [] );
311 $deny_cid = ((strlen($user['deny_cid']))
312 ? explode('><', $user['deny_cid']) : [] );
313 $deny_gid = ((strlen($user['deny_gid']))
314 ? explode('><', $user['deny_gid']) : [] );
315 array_walk($allow_cid,'fixacl');
316 array_walk($allow_gid,'fixacl');
317 array_walk($deny_cid,'fixacl');
318 array_walk($deny_gid,'fixacl');
321 $allow_cid = prune_deadguys($allow_cid);
324 'allow_cid' => $allow_cid,
325 'allow_gid' => $allow_gid,
326 'deny_cid' => $deny_cid,
327 'deny_gid' => $deny_gid,
332 function populate_acl($user = null, $show_jotnets = false) {
334 $perms = get_acl_permissions($user);
338 $mail_disabled = ((function_exists('imap_open') && (! Config::get('system','imap_disabled'))) ? 0 : 1);
340 $mail_enabled = false;
341 $pubmail_enabled = false;
343 if (! $mail_disabled) {
344 $r = q("SELECT `pubmail` FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1",
347 if (DBM::is_result($r)) {
348 $mail_enabled = true;
349 if (intval($r[0]['pubmail'])) {
350 $pubmail_enabled = true;
355 if (!$user['hidewall']) {
357 $selected = (($pubmail_enabled) ? ' checked="checked" ' : '');
358 $jotnets .= '<div class="profile-jot-net"><input type="checkbox" name="pubmail_enable"' . $selected . ' value="1" /> ' . L10n::t("Post to Email") . '</div>';
361 Addon::callHooks('jot_networks', $jotnets);
363 $jotnets .= L10n::t('Connectors disabled, since "%s" is enabled.', L10n::t('Hide your profile details from unknown viewers?'));
367 $tpl = get_markup_template("acl_selector.tpl");
368 $o = replace_macros($tpl, [
369 '$showall'=> L10n::t("Visible to everybody"),
370 '$show' => L10n::t("show"),
371 '$hide' => L10n::t("don't show"),
372 '$allowcid' => json_encode($perms['allow_cid']),
373 '$allowgid' => json_encode($perms['allow_gid']),
374 '$denycid' => json_encode($perms['deny_cid']),
375 '$denygid' => json_encode($perms['deny_gid']),
376 '$networks' => $show_jotnets,
377 '$emailcc' => L10n::t('CC: email addresses'),
378 '$emtitle' => L10n::t('Example: bob@example.com, mary@example.com'),
379 '$jotnets' => $jotnets,
380 '$aclModalTitle' => L10n::t('Permissions'),
381 '$aclModalDismiss' => L10n::t('Close'),
383 'aclautomention' => (Feature::isEnabled($user['uid'], "aclautomention") ? "true" : "false")
392 function acl_lookup(App $a, $out_type = 'json')
398 $start = defaults($_REQUEST, 'start' , 0);
399 $count = defaults($_REQUEST, 'count' , 100);
400 $search = defaults($_REQUEST, 'search' , '');
401 $type = defaults($_REQUEST, 'type' , '');
402 $conv_id = defaults($_REQUEST, 'conversation', null);
404 // For use with jquery.textcomplete for private mail completion
405 if (x($_REQUEST, 'query')) {
409 $search = $_REQUEST['query'];
412 logger("Searching for ".$search." - type ".$type, LOGGER_DEBUG);
415 $sql_extra = "AND `name` LIKE '%%".dbesc($search)."%%'";
416 $sql_extra2 = "AND (`attag` LIKE '%%".dbesc($search)."%%' OR `name` LIKE '%%".dbesc($search)."%%' OR `nick` LIKE '%%".dbesc($search)."%%')";
418 /// @TODO Avoid these needless else blocks by putting variable-initialization atop of if()
419 $sql_extra = $sql_extra2 = "";
422 // count groups and contacts
423 if ($type == '' || $type == 'g') {
424 $r = q("SELECT COUNT(*) AS g FROM `group` WHERE `deleted` = 0 AND `uid` = %d $sql_extra",
427 $group_count = (int)$r[0]['g'];
432 $sql_extra2 .= " ".Widget::unavailableNetworks();
434 if ($type == '' || $type == 'c') {
435 // autocomplete for editor mentions
436 $r = q("SELECT COUNT(*) AS c FROM `contact`
437 WHERE `uid` = %d AND NOT `self`
438 AND NOT `blocked` AND NOT `pending` AND NOT `archive`
439 AND `success_update` >= `failure_update`
440 AND `notify` != '' $sql_extra2" ,
443 $contact_count = (int)$r[0]['c'];
444 } elseif ($type == 'f') {
445 // autocomplete for editor mentions of forums
446 $r = q("SELECT COUNT(*) AS c FROM `contact`
447 WHERE `uid` = %d AND NOT `self`
448 AND NOT `blocked` AND NOT `pending` AND NOT `archive`
449 AND (`forum` OR `prv`)
450 AND `success_update` >= `failure_update`
451 AND `notify` != '' $sql_extra2" ,
454 $contact_count = (int)$r[0]['c'];
455 } elseif ($type == 'm') {
456 // autocomplete for Private Messages
457 $r = q("SELECT COUNT(*) AS c FROM `contact`
458 WHERE `uid` = %d AND NOT `self`
459 AND NOT `blocked` AND NOT `pending` AND NOT `archive`
460 AND `success_update` >= `failure_update`
461 AND `network` IN ('%s', '%s') $sql_extra2" ,
462 intval(local_user()),
464 dbesc(NETWORK_DIASPORA)
466 $contact_count = (int)$r[0]['c'];
468 } elseif ($type == 'a') {
469 // autocomplete for Contacts
470 $r = q("SELECT COUNT(*) AS c FROM `contact`
471 WHERE `uid` = %d AND NOT `self`
472 AND NOT `pending` $sql_extra2" ,
475 $contact_count = (int)$r[0]['c'];
480 $tot = $group_count + $contact_count;
485 if ($type == '' || $type == 'g') {
486 /// @todo We should cache this query.
487 // This can be done when we can delete cache entries via wildcard
488 $r = q("SELECT `group`.`id`, `group`.`name`, GROUP_CONCAT(DISTINCT `group_member`.`contact-id` SEPARATOR ',') AS uids
490 INNER JOIN `group_member` ON `group_member`.`gid`=`group`.`id`
491 WHERE NOT `group`.`deleted` AND `group`.`uid` = %d
493 GROUP BY `group`.`name`, `group`.`id`
494 ORDER BY `group`.`name`
496 intval(local_user()),
504 "photo" => "images/twopeople.png",
505 "name" => htmlentities($g['name']),
506 "id" => intval($g['id']),
507 "uids" => array_map("intval", explode(",",$g['uids'])),
512 if ((count($groups) > 0) && ($search == "")) {
513 $groups[] = ["separator" => true];
518 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `addr`, `forum`, `prv`, (`prv` OR `forum`) AS `frm` FROM `contact`
519 WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
520 AND `success_update` >= `failure_update` AND NOT (`network` IN ('%s', '%s'))
522 ORDER BY `name` ASC ",
523 intval(local_user()),
524 dbesc(NETWORK_OSTATUS), dbesc(NETWORK_STATUSNET)
526 } elseif ($type == 'c') {
527 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `addr`, `forum`, `prv` FROM `contact`
528 WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
529 AND `success_update` >= `failure_update` AND NOT (`network` IN ('%s'))
531 ORDER BY `name` ASC ",
532 intval(local_user()),
533 dbesc(NETWORK_STATUSNET)
535 } elseif ($type == 'f') {
536 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `addr`, `forum`, `prv` FROM `contact`
537 WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
538 AND `success_update` >= `failure_update` AND NOT (`network` IN ('%s'))
539 AND (`forum` OR `prv`)
541 ORDER BY `name` ASC ",
542 intval(local_user()),
543 dbesc(NETWORK_STATUSNET)
545 } elseif ($type == 'm') {
546 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `addr` FROM `contact`
547 WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive`
548 AND `success_update` >= `failure_update` AND `network` IN ('%s', '%s')
550 ORDER BY `name` ASC ",
551 intval(local_user()),
553 dbesc(NETWORK_DIASPORA)
555 } elseif ($type == 'a') {
556 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `addr`, `forum`, `prv` FROM `contact`
557 WHERE `uid` = %d AND `pending` = 0 AND `success_update` >= `failure_update`
559 ORDER BY `name` ASC ",
562 } elseif ($type == 'x') {
563 // autocomplete for global contact search (e.g. navbar search)
564 $r = navbar_complete($a);
569 'photo' => proxy_url($g['photo'], false, PROXY_SIZE_MICRO),
570 'name' => $g['name'],
571 'nick' => (x($g['addr']) ? $g['addr'] : $g['url']),
572 'network' => $g['network'],
574 'forum' => (x($g['community']) ? 1 : 0),
581 'items' => $contacts,
583 echo json_encode($o);
589 if (DBM::is_result($r)) {
594 'photo' => proxy_url($g['micro'], false, PROXY_SIZE_MICRO),
595 'name' => htmlentities($g['name']),
596 'id' => intval($g['id']),
597 'network' => $g['network'],
599 'nick' => htmlentities(($g['attag']) ? $g['attag'] : $g['nick']),
600 'addr' => htmlentities(($g['addr']) ? $g['addr'] : $g['url']),
601 'forum' => ((x($g, 'forum') || x($g, 'prv')) ? 1 : 0),
603 if ($entry['forum']) {
606 $contacts[] = $entry;
609 if (count($forums) > 0) {
611 $forums[] = ["separator" => true];
613 $contacts = array_merge($forums, $contacts);
617 $items = array_merge($groups, $contacts);
621 * if $conv_id is set, get unknown contacts in thread
622 * but first get known contacts url to filter them out
624 $known_contacts = array_map(
626 return dbesc($i['link']);
630 $unknown_contacts = [];
631 $r = q("SELECT `author-link`
632 FROM `item` WHERE `parent` = %d
633 AND (`author-name` LIKE '%%%s%%' OR `author-link` LIKE '%%%s%%')
634 AND `author-link` NOT IN ('%s')
635 GROUP BY `author-link`, `author-avatar`, `author-name`
636 ORDER BY `author-name` ASC
641 implode("', '", $known_contacts)
643 if (DBM::is_result($r)) {
644 foreach ($r as $row) {
645 $contact = Contact::getDetailsByURL($row['author-link']);
647 if (count($contact) > 0) {
648 $unknown_contacts[] = [
650 'photo' => proxy_url($contact['micro'], false, PROXY_SIZE_MICRO),
651 'name' => htmlentities($contact['name']),
652 'id' => intval($contact['cid']),
653 'network' => $contact['network'],
654 'link' => $contact['url'],
655 'nick' => htmlentities($contact['nick'] ? : $contact['addr']),
656 'addr' => htmlentities(($contact['addr']) ? $contact['addr'] : $contact['url']),
657 'forum' => $contact['forum']
663 $items = array_merge($items, $unknown_contacts);
664 $tot += count($unknown_contacts);
672 'contacts' => $contacts,
678 Addon::callHooks('acl_lookup_end', $results);
680 if ($out_type === 'html') {
682 'tot' => $results['tot'],
683 'start' => $results['start'],
684 'count' => $results['count'],
685 'groups' => $results['groups'],
686 'contacts' => $results['contacts'],
692 'tot' => $results['tot'],
693 'start' => $results['start'],
694 'count' => $results['count'],
695 'items' => $results['items'],
698 echo json_encode($o);
703 * @brief Searching for global contacts for autocompletion
706 * @return array with the search results
708 function navbar_complete(App $a) {
710 // logger('navbar_complete');
712 if ((Config::get('system','block_public')) && (! local_user()) && (! remote_user())) {
716 // check if searching in the local global contact table is enabled
717 $localsearch = Config::get('system','poco_local_search');
719 $search = $prefix.notags(trim($_REQUEST['search']));
720 $mode = $_REQUEST['smode'];
722 // don't search if search term has less than 2 characters
723 if (! $search || mb_strlen($search) < 2) {
727 if (substr($search,0,1) === '@') {
728 $search = substr($search,1);
732 $x = GContact::searchByName($search, $mode);
736 if (! $localsearch) {
737 $p = (($a->pager['page'] != 1) ? '&p=' . $a->pager['page'] : '');
739 $x = Network::curl(get_server() . '/lsearch?f=' . $p . '&search=' . urlencode($search));
741 $j = json_decode($x['body'],true);
742 if ($j && isset($j['results'])) {
743 return $j['results'];
748 /// @TODO Not needed here?