3 * @file include/acl_selectors.php
6 use Friendica\Content\Feature;
7 use Friendica\Core\Config;
8 use Friendica\Database\DBM;
9 use Friendica\Model\Contact;
10 use Friendica\Model\GContact;
12 require_once "include/contact_selectors.php";
13 require_once "include/contact_widgets.php";
14 require_once "mod/proxy.php";
17 * @package acl_selectors
19 function group_select($selname,$selclass,$preselected = false,$size = 4) {
25 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"$size\" >\r\n";
27 $r = q("SELECT `id`, `name` FROM `group` WHERE NOT `deleted` AND `uid` = %d ORDER BY `name` ASC",
32 $arr = array('group' => $r, 'entry' => $o);
34 // e.g. 'network_pre_group_deny', 'profile_pre_group_allow'
36 call_hooks($a->module . '_pre_' . $selname, $arr);
38 if (DBM::is_result($r)) {
40 if ((is_array($preselected)) && in_array($rr['id'], $preselected)) {
41 $selected = " selected=\"selected\" ";
46 $trimmed = mb_substr($rr['name'],0,12);
48 $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}\" >$trimmed</option>\r\n";
52 $o .= "</select>\r\n";
54 call_hooks($a->module . '_post_' . $selname, $o);
60 /// @TODO find proper type-hints
61 function contact_selector($selname, $selclass, $options, $preselected = false)
71 if (is_array($options)) {
72 if (x($options, 'size'))
73 $size = $options['size'];
75 if (x($options, 'mutual_friends')) {
78 if (x($options, 'single')) {
81 if (x($options, 'multiple')) {
84 if (x($options, 'exclude')) {
85 $exclude = $options['exclude'];
88 if (x($options, 'networks')) {
89 switch ($options['networks']) {
91 $networks = array(NETWORK_DFRN);
94 if (is_array($a->user) && $a->user['prvnets']) {
95 $networks = array(NETWORK_DFRN, NETWORK_MAIL, NETWORK_DIASPORA);
97 $networks = array(NETWORK_DFRN, NETWORK_FACEBOOK, NETWORK_MAIL, NETWORK_DIASPORA);
101 if (is_array($a->user) && $a->user['prvnets']) {
102 $networks = array(NETWORK_DFRN, NETWORK_MAIL, NETWORK_DIASPORA);
104 $networks = array(NETWORK_DFRN, NETWORK_FACEBOOK, NETWORK_MAIL, NETWORK_DIASPORA, NETWORK_OSTATUS);
107 default: /// @TODO Maybe log this call?
113 $x = array('options' => $options, 'size' => $size, 'single' => $single, 'mutual' => $mutual, 'exclude' => $exclude, 'networks' => $networks);
115 call_hooks('contact_select_options', $x);
121 if (x($x, 'mutual')) {
122 $sql_extra .= sprintf(" AND `rel` = %d ", intval(CONTACT_IS_FRIEND));
125 if (x($x, 'exclude')) {
126 $sql_extra .= sprintf(" AND `id` != %d ", intval($x['exclude']));
129 if (is_array($x['networks']) && count($x['networks'])) {
130 /// @TODO rewrite to foreach()
131 for ($y = 0; $y < count($x['networks']) ; $y ++) {
132 $x['networks'][$y] = "'" . dbesc($x['networks'][$y]) . "'";
134 $str_nets = implode(',', $x['networks']);
135 $sql_extra .= " AND `network` IN ( $str_nets ) ";
138 $tabindex = (x($options, 'tabindex') ? "tabindex=\"" . $options["tabindex"] . "\"" : "");
141 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"" . $x['size'] . "\" $tabindex >\r\n";
143 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"" . $x['size'] . "$\" $tabindex >\r\n";
146 $r = q("SELECT `id`, `name`, `url`, `network` FROM `contact`
147 WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
149 ORDER BY `name` ASC ",
154 $arr = array('contact' => $r, 'entry' => $o);
156 // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
158 call_hooks($a->module . '_pre_' . $selname, $arr);
160 if (DBM::is_result($r)) {
161 foreach ($r as $rr) {
162 if ((is_array($preselected)) && in_array($rr['id'], $preselected)) {
163 $selected = " selected=\"selected\" ";
168 $trimmed = mb_substr($rr['name'],0,20);
170 $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}|{$rr['url']}\" >$trimmed</option>\r\n";
175 $o .= "</select>\r\n";
177 call_hooks($a->module . '_post_' . $selname, $o);
184 function contact_select($selname, $selclass, $preselected = false, $size = 4, $privmail = false, $celeb = false, $privatenet = false, $tabindex = null) {
186 require_once "include/bbcode.php";
192 // When used for private messages, we limit correspondence to mutual DFRN/Friendica friends and the selector
193 // to one recipient. By default our selector allows multiple selects amongst all contacts.
197 if ($privmail || $celeb) {
198 $sql_extra .= sprintf(" AND `rel` = %d ", intval(CONTACT_IS_FRIEND));
202 $sql_extra .= sprintf(" AND `network` IN ('%s' , '%s') ",
203 NETWORK_DFRN, NETWORK_DIASPORA);
204 } elseif ($privatenet) {
205 $sql_extra .= sprintf(" AND `network` IN ('%s' , '%s', '%s', '%s') ",
206 NETWORK_DFRN, NETWORK_MAIL, NETWORK_FACEBOOK, NETWORK_DIASPORA);
209 $tabindex = ($tabindex > 0 ? "tabindex=\"$tabindex\"" : "");
211 if ($privmail && $preselected) {
212 $sql_extra .= " AND `id` IN (".implode(",", $preselected).")";
213 $hidepreselected = ' style="display: none;"';
215 $hidepreselected = "";
219 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"$size\" $tabindex $hidepreselected>\r\n";
221 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"$size\" $tabindex >\r\n";
224 $r = q("SELECT `id`, `name`, `url`, `network` FROM `contact`
225 WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
227 ORDER BY `name` ASC ",
232 $arr = array('contact' => $r, 'entry' => $o);
234 // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
236 call_hooks($a->module . '_pre_' . $selname, $arr);
238 $receiverlist = array();
240 if (DBM::is_result($r)) {
241 foreach ($r as $rr) {
242 if ((is_array($preselected)) && in_array($rr['id'], $preselected)) {
243 $selected = " selected=\"selected\" ";
249 $trimmed = GetProfileUsername($rr['url'], $rr['name'], false);
251 $trimmed = mb_substr($rr['name'],0,20);
254 $receiverlist[] = $trimmed;
256 $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}|{$rr['url']}\" >$trimmed</option>\r\n";
261 $o .= "</select>\r\n";
263 if ($privmail && $preselected) {
264 $o .= implode(", ", $receiverlist);
267 call_hooks($a->module . '_post_' . $selname, $o);
273 function fixacl(&$item) {
274 $item = intval(str_replace(array('<', '>'), array('', ''), $item));
277 function prune_deadguys($arr) {
283 $str = dbesc(implode(',', $arr));
285 $r = q("SELECT `id` FROM `contact` WHERE `id` IN ( " . $str . ") AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 ");
287 if (DBM::is_result($r)) {
289 foreach ($r as $rr) {
290 $ret[] = intval($rr['id']);
299 function get_acl_permissions($user = null) {
300 $allow_cid = $allow_gid = $deny_cid = $deny_gid = false;
302 if (is_array($user)) {
303 $allow_cid = ((strlen($user['allow_cid']))
304 ? explode('><', $user['allow_cid']) : array() );
305 $allow_gid = ((strlen($user['allow_gid']))
306 ? explode('><', $user['allow_gid']) : array() );
307 $deny_cid = ((strlen($user['deny_cid']))
308 ? explode('><', $user['deny_cid']) : array() );
309 $deny_gid = ((strlen($user['deny_gid']))
310 ? explode('><', $user['deny_gid']) : array() );
311 array_walk($allow_cid,'fixacl');
312 array_walk($allow_gid,'fixacl');
313 array_walk($deny_cid,'fixacl');
314 array_walk($deny_gid,'fixacl');
317 $allow_cid = prune_deadguys($allow_cid);
320 'allow_cid' => $allow_cid,
321 'allow_gid' => $allow_gid,
322 'deny_cid' => $deny_cid,
323 'deny_gid' => $deny_gid,
328 function populate_acl($user = null, $show_jotnets = false) {
330 $perms = get_acl_permissions($user);
334 $mail_disabled = ((function_exists('imap_open') && (! Config::get('system','imap_disabled'))) ? 0 : 1);
336 $mail_enabled = false;
337 $pubmail_enabled = false;
339 if (! $mail_disabled) {
340 $r = q("SELECT `pubmail` FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1",
343 if (DBM::is_result($r)) {
344 $mail_enabled = true;
345 if (intval($r[0]['pubmail'])) {
346 $pubmail_enabled = true;
351 if (!$user['hidewall']) {
353 $selected = (($pubmail_enabled) ? ' checked="checked" ' : '');
354 $jotnets .= '<div class="profile-jot-net"><input type="checkbox" name="pubmail_enable"' . $selected . ' value="1" /> ' . t("Post to Email") . '</div>';
357 call_hooks('jot_networks', $jotnets);
359 $jotnets .= sprintf(t('Connectors disabled, since "%s" is enabled.'),
360 t('Hide your profile details from unknown viewers?'));
364 $tpl = get_markup_template("acl_selector.tpl");
365 $o = replace_macros($tpl, array(
366 '$showall'=> t("Visible to everybody"),
367 '$show' => t("show"),
368 '$hide' => t("don't show"),
369 '$allowcid' => json_encode($perms['allow_cid']),
370 '$allowgid' => json_encode($perms['allow_gid']),
371 '$denycid' => json_encode($perms['deny_cid']),
372 '$denygid' => json_encode($perms['deny_gid']),
373 '$networks' => $show_jotnets,
374 '$emailcc' => t('CC: email addresses'),
375 '$emtitle' => t('Example: bob@example.com, mary@example.com'),
376 '$jotnets' => $jotnets,
377 '$aclModalTitle' => t('Permissions'),
378 '$aclModalDismiss' => t('Close'),
379 '$features' => array(
380 'aclautomention' => (Feature::isEnabled($user['uid'], "aclautomention") ? "true" : "false")
389 function acl_lookup(App $a, $out_type = 'json') {
395 $start = (x($_REQUEST,'start') ? $_REQUEST['start'] : 0);
396 $count = (x($_REQUEST,'count') ? $_REQUEST['count'] : 100);
397 $search = (x($_REQUEST,'search') ? $_REQUEST['search'] : "");
398 $type = (x($_REQUEST,'type') ? $_REQUEST['type'] : "");
399 $mode = (x($_REQUEST,'smode') ? $_REQUEST['smode'] : "");
400 $conv_id = (x($_REQUEST,'conversation') ? $_REQUEST['conversation'] : null);
402 // For use with jquery.textcomplete for private mail completion
404 if (x($_REQUEST, 'query') && strlen($_REQUEST['query'])) {
408 $search = $_REQUEST['query'];
411 logger("Searching for ".$search." - type ".$type, LOGGER_DEBUG);
414 $sql_extra = "AND `name` LIKE '%%".dbesc($search)."%%'";
415 $sql_extra2 = "AND (`attag` LIKE '%%".dbesc($search)."%%' OR `name` LIKE '%%".dbesc($search)."%%' OR `nick` LIKE '%%".dbesc($search)."%%')";
417 /// @TODO Avoid these needless else blocks by putting variable-initialization atop of if()
418 $sql_extra = $sql_extra2 = "";
421 // count groups and contacts
422 if ($type == '' || $type == 'g') {
423 $r = q("SELECT COUNT(*) AS g FROM `group` WHERE `deleted` = 0 AND `uid` = %d $sql_extra",
426 $group_count = (int)$r[0]['g'];
431 $sql_extra2 .= " ".unavailable_networks();
433 if ($type == '' || $type == 'c') {
434 // autocomplete for editor mentions
435 $r = q("SELECT COUNT(*) AS c FROM `contact`
436 WHERE `uid` = %d AND NOT `self`
437 AND NOT `blocked` AND NOT `pending` AND NOT `archive`
438 AND `success_update` >= `failure_update`
439 AND `notify` != '' $sql_extra2" ,
442 $contact_count = (int)$r[0]['c'];
443 } elseif ($type == 'f') {
444 // autocomplete for editor mentions of forums
445 $r = q("SELECT COUNT(*) AS c FROM `contact`
446 WHERE `uid` = %d AND NOT `self`
447 AND NOT `blocked` AND NOT `pending` AND NOT `archive`
448 AND (`forum` OR `prv`)
449 AND `success_update` >= `failure_update`
450 AND `notify` != '' $sql_extra2" ,
453 $contact_count = (int)$r[0]['c'];
454 } elseif ($type == 'm') {
455 // 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') {
470 // autocomplete for Contacts
472 $r = q("SELECT COUNT(*) AS c FROM `contact`
473 WHERE `uid` = %d AND NOT `self`
474 AND NOT `pending` $sql_extra2" ,
477 $contact_count = (int)$r[0]['c'];
484 $tot = $group_count+$contact_count;
489 if ($type == '' || $type == 'g') {
491 /// @todo We should cache this query.
492 // This can be done when we can delete cache entries via wildcard
493 $r = q("SELECT `group`.`id`, `group`.`name`, GROUP_CONCAT(DISTINCT `group_member`.`contact-id` SEPARATOR ',') AS uids
495 INNER JOIN `group_member` ON `group_member`.`gid`=`group`.`id`
496 WHERE NOT `group`.`deleted` AND `group`.`uid` = %d
498 GROUP BY `group`.`name`, `group`.`id`
499 ORDER BY `group`.`name`
501 intval(local_user()),
507 // logger('acl: group: ' . $g['name'] . ' members: ' . $g['uids']);
510 "photo" => "images/twopeople.png",
511 "name" => htmlentities($g['name']),
512 "id" => intval($g['id']),
513 "uids" => array_map("intval", explode(",",$g['uids'])),
518 if ((count($groups) > 0) && ($search == "")) {
519 $groups[] = array("separator" => true);
525 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `addr`, `forum`, `prv`, (`prv` OR `forum`) AS `frm` FROM `contact`
526 WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
527 AND `success_update` >= `failure_update` AND NOT (`network` IN ('%s', '%s'))
529 ORDER BY `name` ASC ",
530 intval(local_user()),
531 dbesc(NETWORK_OSTATUS), dbesc(NETWORK_STATUSNET)
533 } elseif ($type == 'c') {
534 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `addr`, `forum`, `prv` FROM `contact`
535 WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
536 AND `success_update` >= `failure_update` AND NOT (`network` IN ('%s'))
538 ORDER BY `name` ASC ",
539 intval(local_user()),
540 dbesc(NETWORK_STATUSNET)
542 } elseif ($type == 'f') {
543 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `addr`, `forum`, `prv` FROM `contact`
544 WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
545 AND `success_update` >= `failure_update` AND NOT (`network` IN ('%s'))
546 AND (`forum` OR `prv`)
548 ORDER BY `name` ASC ",
549 intval(local_user()),
550 dbesc(NETWORK_STATUSNET)
552 } elseif ($type == 'm') {
553 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `addr` FROM `contact`
554 WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive`
555 AND `success_update` >= `failure_update` AND `network` IN ('%s', '%s')
557 ORDER BY `name` ASC ",
558 intval(local_user()),
560 dbesc(NETWORK_DIASPORA)
562 } elseif ($type == 'a') {
563 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `addr`, `forum`, `prv` FROM `contact`
564 WHERE `uid` = %d AND `pending` = 0 AND `success_update` >= `failure_update`
566 ORDER BY `name` ASC ",
569 } elseif ($type == 'x') {
570 // autocomplete for global contact search (e.g. navbar search)
571 $r = navbar_complete($a);
576 'photo' => proxy_url($g['photo'], false, PROXY_SIZE_MICRO),
577 'name' => $g['name'],
578 'nick' => (x($g['addr']) ? $g['addr'] : $g['url']),
579 'network' => $g['network'],
581 'forum' => (x($g['community']) ? 1 : 0),
588 'items' => $contacts,
590 echo json_encode($o);
597 if (DBM::is_result($r)) {
602 'photo' => proxy_url($g['micro'], false, PROXY_SIZE_MICRO),
603 'name' => htmlentities($g['name']),
604 'id' => intval($g['id']),
605 'network' => $g['network'],
607 'nick' => htmlentities(($g['attag']) ? $g['attag'] : $g['nick']),
608 'addr' => htmlentities(($g['addr']) ? $g['addr'] : $g['url']),
609 'forum' => ((x($g, 'forum') || x($g, 'prv')) ? 1 : 0),
611 if ($entry['forum']) {
614 $contacts[] = $entry;
617 if (count($forums) > 0) {
619 $forums[] = array("separator" => true);
621 $contacts = array_merge($forums, $contacts);
625 $items = array_merge($groups, $contacts);
629 * if $conv_id is set, get unknown contacts in thread
630 * but first get known contacts url to filter them out
632 $known_contacts = array_map(
634 return dbesc($i['link']);
638 $unknown_contacts = array();
639 $r = q("SELECT `author-link`
640 FROM `item` WHERE `parent` = %d
641 AND (`author-name` LIKE '%%%s%%' OR `author-link` LIKE '%%%s%%')
642 AND `author-link` NOT IN ('%s')
643 GROUP BY `author-link`, `author-avatar`, `author-name`
644 ORDER BY `author-name` ASC
649 implode("', '", $known_contacts)
651 if (DBM::is_result($r)) {
652 foreach ($r as $row) {
653 $contact = Contact::getDetailsByURL($row['author-link']);
655 if (count($contact) > 0) {
656 $unknown_contacts[] = array(
658 'photo' => proxy_url($contact['micro'], false, PROXY_SIZE_MICRO),
659 'name' => htmlentities($contact['name']),
660 'id' => intval($contact['cid']),
661 'network' => $contact['network'],
662 'link' => $contact['url'],
663 'nick' => htmlentities($contact['nick'] ? : $contact['addr']),
664 'addr' => htmlentities(($contact['addr']) ? $contact['addr'] : $contact['url']),
665 'forum' => $contact['forum']
671 $items = array_merge($items, $unknown_contacts);
672 $tot += count($unknown_contacts);
680 'contacts' => $contacts,
686 call_hooks('acl_lookup_end', $results);
688 if ($out_type === 'html') {
690 'tot' => $results['tot'],
691 'start' => $results['start'],
692 'count' => $results['count'],
693 'groups' => $results['groups'],
694 'contacts' => $results['contacts'],
700 'tot' => $results['tot'],
701 'start' => $results['start'],
702 'count' => $results['count'],
703 'items' => $results['items'],
706 echo json_encode($o);
711 * @brief Searching for global contacts for autocompletion
714 * @return array with the search results
716 function navbar_complete(App $a) {
718 // logger('navbar_complete');
720 if ((Config::get('system','block_public')) && (! local_user()) && (! remote_user())) {
724 // check if searching in the local global contact table is enabled
725 $localsearch = Config::get('system','poco_local_search');
727 $search = $prefix.notags(trim($_REQUEST['search']));
728 $mode = $_REQUEST['smode'];
730 // don't search if search term has less than 2 characters
731 if (! $search || mb_strlen($search) < 2) {
735 if (substr($search,0,1) === '@') {
736 $search = substr($search,1);
740 $x = GContact::searchByName($search, $mode);
744 if (! $localsearch) {
745 $p = (($a->pager['page'] != 1) ? '&p=' . $a->pager['page'] : '');
747 $x = z_fetch_url(get_server() . '/lsearch?f=' . $p . '&search=' . urlencode($search));
749 $j = json_decode($x['body'],true);
750 if ($j && isset($j['results'])) {
751 return $j['results'];
756 /// @TODO Not needed here?