4 * @file include/acl_selectors.php
8 use Friendica\Core\Config;
9 use Friendica\Database\DBM;
10 use Friendica\Model\GlobalContact;
12 require_once "include/contact_selectors.php";
13 require_once "include/contact_widgets.php";
14 require_once "include/features.php";
15 require_once "mod/proxy.php";
19 * @package acl_selectors
21 function group_select($selname,$selclass,$preselected = false,$size = 4) {
27 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"$size\" >\r\n";
29 $r = q("SELECT `id`, `name` FROM `group` WHERE NOT `deleted` AND `uid` = %d ORDER BY `name` ASC",
34 $arr = array('group' => $r, 'entry' => $o);
36 // e.g. 'network_pre_group_deny', 'profile_pre_group_allow'
38 call_hooks($a->module . '_pre_' . $selname, $arr);
40 if (DBM::is_result($r)) {
42 if ((is_array($preselected)) && in_array($rr['id'], $preselected)) {
43 $selected = " selected=\"selected\" ";
48 $trimmed = mb_substr($rr['name'],0,12);
50 $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}\" >$trimmed</option>\r\n";
54 $o .= "</select>\r\n";
56 call_hooks($a->module . '_post_' . $selname, $o);
62 /// @TODO find proper type-hints
63 function contact_selector($selname, $selclass, $options, $preselected = false)
73 if (is_array($options)) {
74 if (x($options, 'size'))
75 $size = $options['size'];
77 if (x($options, 'mutual_friends')) {
80 if (x($options, 'single')) {
83 if (x($options, 'multiple')) {
86 if (x($options, 'exclude')) {
87 $exclude = $options['exclude'];
90 if (x($options, 'networks')) {
91 switch ($options['networks']) {
93 $networks = array(NETWORK_DFRN);
96 if (is_array($a->user) && $a->user['prvnets']) {
97 $networks = array(NETWORK_DFRN, NETWORK_MAIL, NETWORK_DIASPORA);
99 $networks = array(NETWORK_DFRN, NETWORK_FACEBOOK, NETWORK_MAIL, NETWORK_DIASPORA);
103 if (is_array($a->user) && $a->user['prvnets']) {
104 $networks = array(NETWORK_DFRN, NETWORK_MAIL, NETWORK_DIASPORA);
106 $networks = array(NETWORK_DFRN, NETWORK_FACEBOOK, NETWORK_MAIL, NETWORK_DIASPORA, NETWORK_OSTATUS);
109 default: /// @TODO Maybe log this call?
115 $x = array('options' => $options, 'size' => $size, 'single' => $single, 'mutual' => $mutual, 'exclude' => $exclude, 'networks' => $networks);
117 call_hooks('contact_select_options', $x);
123 if (x($x, 'mutual')) {
124 $sql_extra .= sprintf(" AND `rel` = %d ", intval(CONTACT_IS_FRIEND));
127 if (x($x, 'exclude')) {
128 $sql_extra .= sprintf(" AND `id` != %d ", intval($x['exclude']));
131 if (is_array($x['networks']) && count($x['networks'])) {
132 /// @TODO rewrite to foreach()
133 for ($y = 0; $y < count($x['networks']) ; $y ++) {
134 $x['networks'][$y] = "'" . dbesc($x['networks'][$y]) . "'";
136 $str_nets = implode(',', $x['networks']);
137 $sql_extra .= " AND `network` IN ( $str_nets ) ";
140 $tabindex = (x($options, 'tabindex') ? "tabindex=\"" . $options["tabindex"] . "\"" : "");
143 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"" . $x['size'] . "\" $tabindex >\r\n";
145 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"" . $x['size'] . "$\" $tabindex >\r\n";
148 $r = q("SELECT `id`, `name`, `url`, `network` FROM `contact`
149 WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
151 ORDER BY `name` ASC ",
156 $arr = array('contact' => $r, 'entry' => $o);
158 // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
160 call_hooks($a->module . '_pre_' . $selname, $arr);
162 if (DBM::is_result($r)) {
163 foreach ($r as $rr) {
164 if ((is_array($preselected)) && in_array($rr['id'], $preselected)) {
165 $selected = " selected=\"selected\" ";
170 $trimmed = mb_substr($rr['name'],0,20);
172 $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}|{$rr['url']}\" >$trimmed</option>\r\n";
177 $o .= "</select>\r\n";
179 call_hooks($a->module . '_post_' . $selname, $o);
186 function contact_select($selname, $selclass, $preselected = false, $size = 4, $privmail = false, $celeb = false, $privatenet = false, $tabindex = null) {
188 require_once "include/bbcode.php";
194 // When used for private messages, we limit correspondence to mutual DFRN/Friendica friends and the selector
195 // to one recipient. By default our selector allows multiple selects amongst all contacts.
199 if ($privmail || $celeb) {
200 $sql_extra .= sprintf(" AND `rel` = %d ", intval(CONTACT_IS_FRIEND));
204 $sql_extra .= sprintf(" AND `network` IN ('%s' , '%s') ",
205 NETWORK_DFRN, NETWORK_DIASPORA);
206 } elseif ($privatenet) {
207 $sql_extra .= sprintf(" AND `network` IN ('%s' , '%s', '%s', '%s') ",
208 NETWORK_DFRN, NETWORK_MAIL, NETWORK_FACEBOOK, NETWORK_DIASPORA);
211 $tabindex = ($tabindex > 0 ? "tabindex=\"$tabindex\"" : "");
213 if ($privmail && $preselected) {
214 $sql_extra .= " AND `id` IN (".implode(",", $preselected).")";
215 $hidepreselected = ' style="display: none;"';
217 $hidepreselected = "";
221 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"$size\" $tabindex $hidepreselected>\r\n";
223 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"$size\" $tabindex >\r\n";
226 $r = q("SELECT `id`, `name`, `url`, `network` FROM `contact`
227 WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
229 ORDER BY `name` ASC ",
234 $arr = array('contact' => $r, 'entry' => $o);
236 // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
238 call_hooks($a->module . '_pre_' . $selname, $arr);
240 $receiverlist = array();
242 if (DBM::is_result($r)) {
243 foreach ($r as $rr) {
244 if ((is_array($preselected)) && in_array($rr['id'], $preselected)) {
245 $selected = " selected=\"selected\" ";
251 $trimmed = GetProfileUsername($rr['url'], $rr['name'], false);
253 $trimmed = mb_substr($rr['name'],0,20);
256 $receiverlist[] = $trimmed;
258 $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}|{$rr['url']}\" >$trimmed</option>\r\n";
263 $o .= "</select>\r\n";
265 if ($privmail && $preselected) {
266 $o .= implode(", ", $receiverlist);
269 call_hooks($a->module . '_post_' . $selname, $o);
275 function fixacl(&$item) {
276 $item = intval(str_replace(array('<', '>'), array('', ''), $item));
279 function prune_deadguys($arr) {
285 $str = dbesc(implode(',', $arr));
287 $r = q("SELECT `id` FROM `contact` WHERE `id` IN ( " . $str . ") AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 ");
289 if (DBM::is_result($r)) {
291 foreach ($r as $rr) {
292 $ret[] = intval($rr['id']);
301 function get_acl_permissions($user = null) {
302 $allow_cid = $allow_gid = $deny_cid = $deny_gid = false;
304 if (is_array($user)) {
305 $allow_cid = ((strlen($user['allow_cid']))
306 ? explode('><', $user['allow_cid']) : array() );
307 $allow_gid = ((strlen($user['allow_gid']))
308 ? explode('><', $user['allow_gid']) : array() );
309 $deny_cid = ((strlen($user['deny_cid']))
310 ? explode('><', $user['deny_cid']) : array() );
311 $deny_gid = ((strlen($user['deny_gid']))
312 ? explode('><', $user['deny_gid']) : array() );
313 array_walk($allow_cid,'fixacl');
314 array_walk($allow_gid,'fixacl');
315 array_walk($deny_cid,'fixacl');
316 array_walk($deny_gid,'fixacl');
319 $allow_cid = prune_deadguys($allow_cid);
322 'allow_cid' => $allow_cid,
323 'allow_gid' => $allow_gid,
324 'deny_cid' => $deny_cid,
325 'deny_gid' => $deny_gid,
330 function populate_acl($user = null, $show_jotnets = false) {
332 $perms = get_acl_permissions($user);
336 $mail_disabled = ((function_exists('imap_open') && (! Config::get('system','imap_disabled'))) ? 0 : 1);
338 $mail_enabled = false;
339 $pubmail_enabled = false;
341 if (! $mail_disabled) {
342 $r = q("SELECT `pubmail` FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1",
345 if (DBM::is_result($r)) {
346 $mail_enabled = true;
347 if (intval($r[0]['pubmail'])) {
348 $pubmail_enabled = true;
353 if (!$user['hidewall']) {
355 $selected = (($pubmail_enabled) ? ' checked="checked" ' : '');
356 $jotnets .= '<div class="profile-jot-net"><input type="checkbox" name="pubmail_enable"' . $selected . ' value="1" /> ' . t("Post to Email") . '</div>';
359 call_hooks('jot_networks', $jotnets);
361 $jotnets .= sprintf(t('Connectors disabled, since "%s" is enabled.'),
362 t('Hide your profile details from unknown viewers?'));
366 $tpl = get_markup_template("acl_selector.tpl");
367 $o = replace_macros($tpl, array(
368 '$showall'=> t("Visible to everybody"),
369 '$show' => t("show"),
370 '$hide' => t("don't show"),
371 '$allowcid' => json_encode($perms['allow_cid']),
372 '$allowgid' => json_encode($perms['allow_gid']),
373 '$denycid' => json_encode($perms['deny_cid']),
374 '$denygid' => json_encode($perms['deny_gid']),
375 '$networks' => $show_jotnets,
376 '$emailcc' => t('CC: email addresses'),
377 '$emtitle' => t('Example: bob@example.com, mary@example.com'),
378 '$jotnets' => $jotnets,
379 '$aclModalTitle' => t('Permissions'),
380 '$aclModalDismiss' => t('Close'),
381 '$features' => array(
382 'aclautomention' => (feature_enabled($user['uid'], "aclautomention") ? "true" : "false")
391 function construct_acl_data(App $a, $user) {
392 // This function is now deactivated. It seems as if the generated data isn't used anywhere.
393 /// @todo Remove this function and all function calls before releasing Friendica 3.5.3
396 // Get group and contact information for html ACL selector
397 $acl_data = acl_lookup($a, 'html');
399 $user_defaults = get_acl_permissions($user);
401 if ($acl_data['groups']) {
402 foreach ($acl_data['groups'] as $key => $group) {
403 // Add a "selected" flag to groups that are posted to by default
404 if ($user_defaults['allow_gid'] &&
405 in_array($group['id'], $user_defaults['allow_gid']) && !in_array($group['id'], $user_defaults['deny_gid']) ) {
406 $acl_data['groups'][$key]['selected'] = 1;
408 $acl_data['groups'][$key]['selected'] = 0;
412 if ($acl_data['contacts']) {
413 foreach ($acl_data['contacts'] as $key => $contact) {
414 // Add a "selected" flag to groups that are posted to by default
415 if ($user_defaults['allow_cid'] &&
416 in_array($contact['id'], $user_defaults['allow_cid']) && !in_array($contact['id'], $user_defaults['deny_cid']) ) {
417 $acl_data['contacts'][$key]['selected'] = 1;
419 $acl_data['contacts'][$key]['selected'] = 0;
428 function acl_lookup(App $a, $out_type = 'json') {
434 $start = (x($_REQUEST,'start') ? $_REQUEST['start'] : 0);
435 $count = (x($_REQUEST,'count') ? $_REQUEST['count'] : 100);
436 $search = (x($_REQUEST,'search') ? $_REQUEST['search'] : "");
437 $type = (x($_REQUEST,'type') ? $_REQUEST['type'] : "");
438 $mode = (x($_REQUEST,'smode') ? $_REQUEST['smode'] : "");
439 $conv_id = (x($_REQUEST,'conversation') ? $_REQUEST['conversation'] : null);
441 // For use with jquery.textcomplete for private mail completion
443 if (x($_REQUEST, 'query') && strlen($_REQUEST['query'])) {
447 $search = $_REQUEST['query'];
450 logger("Searching for ".$search." - type ".$type, LOGGER_DEBUG);
453 $sql_extra = "AND `name` LIKE '%%".dbesc($search)."%%'";
454 $sql_extra2 = "AND (`attag` LIKE '%%".dbesc($search)."%%' OR `name` LIKE '%%".dbesc($search)."%%' OR `nick` LIKE '%%".dbesc($search)."%%')";
456 /// @TODO Avoid these needless else blocks by putting variable-initialization atop of if()
457 $sql_extra = $sql_extra2 = "";
460 // count groups and contacts
461 if ($type == '' || $type == 'g') {
462 $r = q("SELECT COUNT(*) AS g FROM `group` WHERE `deleted` = 0 AND `uid` = %d $sql_extra",
465 $group_count = (int)$r[0]['g'];
470 $sql_extra2 .= " ".unavailable_networks();
472 if ($type == '' || $type == 'c') {
473 // autocomplete for editor mentions
474 $r = q("SELECT COUNT(*) AS c FROM `contact`
475 WHERE `uid` = %d AND NOT `self`
476 AND NOT `blocked` AND NOT `pending` AND NOT `archive`
477 AND `success_update` >= `failure_update`
478 AND `notify` != '' $sql_extra2" ,
481 $contact_count = (int)$r[0]['c'];
482 } elseif ($type == 'f') {
483 // autocomplete for editor mentions of forums
484 $r = q("SELECT COUNT(*) AS c FROM `contact`
485 WHERE `uid` = %d AND NOT `self`
486 AND NOT `blocked` AND NOT `pending` AND NOT `archive`
487 AND (`forum` OR `prv`)
488 AND `success_update` >= `failure_update`
489 AND `notify` != '' $sql_extra2" ,
492 $contact_count = (int)$r[0]['c'];
493 } elseif ($type == 'm') {
494 // autocomplete for Private Messages
496 $r = q("SELECT COUNT(*) AS c FROM `contact`
497 WHERE `uid` = %d AND NOT `self`
498 AND NOT `blocked` AND NOT `pending` AND NOT `archive`
499 AND `success_update` >= `failure_update`
500 AND `network` IN ('%s','%s','%s') $sql_extra2" ,
501 intval(local_user()),
504 dbesc(NETWORK_DIASPORA)
506 $contact_count = (int)$r[0]['c'];
508 } elseif ($type == 'a') {
510 // autocomplete for Contacts
512 $r = q("SELECT COUNT(*) AS c FROM `contact`
513 WHERE `uid` = %d AND NOT `self`
514 AND NOT `pending` $sql_extra2" ,
517 $contact_count = (int)$r[0]['c'];
524 $tot = $group_count+$contact_count;
529 if ($type == '' || $type == 'g') {
531 /// @todo We should cache this query.
532 // This can be done when we can delete cache entries via wildcard
533 $r = q("SELECT `group`.`id`, `group`.`name`, GROUP_CONCAT(DISTINCT `group_member`.`contact-id` SEPARATOR ',') AS uids
535 INNER JOIN `group_member` ON `group_member`.`gid`=`group`.`id` AND `group_member`.`uid` = `group`.`uid`
536 WHERE NOT `group`.`deleted` AND `group`.`uid` = %d
538 GROUP BY `group`.`name`, `group`.`id`
539 ORDER BY `group`.`name`
541 intval(local_user()),
547 // logger('acl: group: ' . $g['name'] . ' members: ' . $g['uids']);
550 "photo" => "images/twopeople.png",
551 "name" => htmlentities($g['name']),
552 "id" => intval($g['id']),
553 "uids" => array_map("intval", explode(",",$g['uids'])),
558 if ((count($groups) > 0) && ($search == "")) {
559 $groups[] = array("separator" => true);
565 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `addr`, `forum`, `prv`, (`prv` OR `forum`) AS `frm` FROM `contact`
566 WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
567 AND `success_update` >= `failure_update` AND NOT (`network` IN ('%s', '%s'))
569 ORDER BY `name` ASC ",
570 intval(local_user()),
571 dbesc(NETWORK_OSTATUS), dbesc(NETWORK_STATUSNET)
573 } elseif ($type == 'c') {
574 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `addr`, `forum`, `prv` FROM `contact`
575 WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
576 AND `success_update` >= `failure_update` AND NOT (`network` IN ('%s'))
578 ORDER BY `name` ASC ",
579 intval(local_user()),
580 dbesc(NETWORK_STATUSNET)
582 } elseif ($type == 'f') {
583 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `addr`, `forum`, `prv` FROM `contact`
584 WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
585 AND `success_update` >= `failure_update` AND NOT (`network` IN ('%s'))
586 AND (`forum` OR `prv`)
588 ORDER BY `name` ASC ",
589 intval(local_user()),
590 dbesc(NETWORK_STATUSNET)
592 } elseif ($type == 'm') {
593 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `addr` FROM `contact`
594 WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive`
595 AND `success_update` >= `failure_update` AND `network` IN ('%s','%s','%s')
597 ORDER BY `name` ASC ",
598 intval(local_user()),
601 dbesc(NETWORK_DIASPORA)
603 } elseif ($type == 'a') {
604 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `addr`, `forum`, `prv` FROM `contact`
605 WHERE `uid` = %d AND `pending` = 0 AND `success_update` >= `failure_update`
607 ORDER BY `name` ASC ",
610 } elseif ($type == 'x') {
611 // autocomplete for global contact search (e.g. navbar search)
612 $r = navbar_complete($a);
617 'photo' => proxy_url($g['photo'], false, PROXY_SIZE_MICRO),
618 'name' => $g['name'],
619 'nick' => (x($g['addr']) ? $g['addr'] : $g['url']),
620 'network' => $g['network'],
622 'forum' => (x($g['community']) ? 1 : 0),
629 'items' => $contacts,
631 echo json_encode($o);
638 if (DBM::is_result($r)) {
643 'photo' => proxy_url($g['micro'], false, PROXY_SIZE_MICRO),
644 'name' => htmlentities($g['name']),
645 'id' => intval($g['id']),
646 'network' => $g['network'],
648 'nick' => htmlentities(($g['attag']) ? $g['attag'] : $g['nick']),
649 'addr' => htmlentities(($g['addr']) ? $g['addr'] : $g['url']),
650 'forum' => ((x($g, 'forum') || x($g, 'prv')) ? 1 : 0),
652 if ($entry['forum']) {
655 $contacts[] = $entry;
658 if (count($forums) > 0) {
660 $forums[] = array("separator" => true);
662 $contacts = array_merge($forums, $contacts);
666 $items = array_merge($groups, $contacts);
670 * if $conv_id is set, get unknown contacts in thread
671 * but first get known contacts url to filter them out
673 $known_contacts = array_map(
675 return dbesc($i['link']);
679 $unknown_contacts = array();
680 $r = q("SELECT `author-link`
681 FROM `item` WHERE `parent` = %d
682 AND (`author-name` LIKE '%%%s%%' OR `author-link` LIKE '%%%s%%')
683 AND `author-link` NOT IN ('%s')
684 GROUP BY `author-link`, `author-avatar`, `author-name`
685 ORDER BY `author-name` ASC
690 implode("', '", $known_contacts)
692 if (DBM::is_result($r)) {
693 foreach ($r as $row) {
694 $contact = get_contact_details_by_url($row['author-link']);
696 if (count($contact) > 0) {
697 $unknown_contacts[] = array(
699 'photo' => proxy_url($contact['micro'], false, PROXY_SIZE_MICRO),
700 'name' => htmlentities($contact['name']),
701 'id' => intval($contact['cid']),
702 'network' => $contact['network'],
703 'link' => $contact['url'],
704 'nick' => htmlentities($contact['nick'] ? : $contact['addr']),
705 'addr' => htmlentities(($contact['addr']) ? $contact['addr'] : $contact['url']),
706 'forum' => $contact['forum']
712 $items = array_merge($items, $unknown_contacts);
713 $tot += count($unknown_contacts);
721 'contacts' => $contacts,
727 call_hooks('acl_lookup_end', $results);
729 if ($out_type === 'html') {
731 'tot' => $results['tot'],
732 'start' => $results['start'],
733 'count' => $results['count'],
734 'groups' => $results['groups'],
735 'contacts' => $results['contacts'],
741 'tot' => $results['tot'],
742 'start' => $results['start'],
743 'count' => $results['count'],
744 'items' => $results['items'],
747 echo json_encode($o);
752 * @brief Searching for global contacts for autocompletion
755 * @return array with the search results
757 function navbar_complete(App $a) {
759 // logger('navbar_complete');
761 if ((Config::get('system','block_public')) && (! local_user()) && (! remote_user())) {
765 // check if searching in the local global contact table is enabled
766 $localsearch = Config::get('system','poco_local_search');
768 $search = $prefix.notags(trim($_REQUEST['search']));
769 $mode = $_REQUEST['smode'];
771 // don't search if search term has less than 2 characters
772 if (! $search || mb_strlen($search) < 2) {
776 if (substr($search,0,1) === '@') {
777 $search = substr($search,1);
781 $x = GlobalContact::searchByName($search, $mode);
785 if (! $localsearch) {
786 $p = (($a->pager['page'] != 1) ? '&p=' . $a->pager['page'] : '');
788 $x = z_fetch_url(get_server().'/lsearch?f=' . $p . '&search=' . urlencode($search));
791 $j = json_decode($x['body'],true);
792 if ($j && $j['results']) {
793 return $j['results'];
798 /// @TODO Not needed here?