4 * @file include/acl_selectors.php
7 require_once "include/contact_selectors.php";
8 require_once "include/contact_widgets.php";
9 require_once "include/DirSearch.php";
10 require_once "include/features.php";
11 require_once "mod/proxy.php";
15 * @package acl_selectors
17 function group_select($selname,$selclass,$preselected = false,$size = 4) {
23 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"$size\" >\r\n";
25 $r = q("SELECT `id`, `name` FROM `group` WHERE NOT `deleted` AND `uid` = %d ORDER BY `name` ASC",
30 $arr = array('group' => $r, 'entry' => $o);
32 // e.g. 'network_pre_group_deny', 'profile_pre_group_allow'
34 call_hooks($a->module . '_pre_' . $selname, $arr);
36 if (dbm::is_result($r)) {
38 if ((is_array($preselected)) && in_array($rr['id'], $preselected)) {
39 $selected = " selected=\"selected\" ";
44 $trimmed = mb_substr($rr['name'],0,12);
46 $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}\" >$trimmed</option>\r\n";
50 $o .= "</select>\r\n";
52 call_hooks($a->module . '_post_' . $selname, $o);
58 /// @TODO after an optional parameter, no mandadory parameter can follow
59 /// @TODO find proper type-hints
60 function contact_selector($selname, $selclass, $preselected = false, $options) {
70 if (is_array($options)) {
71 if (x($options, 'size'))
72 $size = $options['size'];
74 if (x($options, 'mutual_friends')) {
77 if (x($options, 'single')) {
80 if (x($options, 'multiple')) {
83 if (x($options, 'exclude')) {
84 $exclude = $options['exclude'];
87 if (x($options, 'networks')) {
88 switch ($options['networks']) {
90 $networks = array(NETWORK_DFRN);
93 if (is_array($a->user) && $a->user['prvnets']) {
94 $networks = array(NETWORK_DFRN, NETWORK_MAIL, NETWORK_DIASPORA);
96 $networks = array(NETWORK_DFRN, NETWORK_FACEBOOK, NETWORK_MAIL, NETWORK_DIASPORA);
100 if (is_array($a->user) && $a->user['prvnets']) {
101 $networks = array(NETWORK_DFRN, NETWORK_MAIL, NETWORK_DIASPORA);
103 $networks = array(NETWORK_DFRN, NETWORK_FACEBOOK, NETWORK_MAIL, NETWORK_DIASPORA, NETWORK_OSTATUS);
106 default: /// @TODO Maybe log this call?
112 $x = array('options' => $options, 'size' => $size, 'single' => $single, 'mutual' => $mutual, 'exclude' => $exclude, 'networks' => $networks);
114 call_hooks('contact_select_options', $x);
120 if (x($x, 'mutual')) {
121 $sql_extra .= sprintf(" AND `rel` = %d ", intval(CONTACT_IS_FRIEND));
124 if (x($x, 'exclude')) {
125 $sql_extra .= sprintf(" AND `id` != %d ", intval($x['exclude']));
128 if (is_array($x['networks']) && count($x['networks'])) {
129 /// @TODO rewrite to foreach()
130 for ($y = 0; $y < count($x['networks']) ; $y ++) {
131 $x['networks'][$y] = "'" . dbesc($x['networks'][$y]) . "'";
133 $str_nets = implode(',', $x['networks']);
134 $sql_extra .= " AND `network` IN ( $str_nets ) ";
137 $tabindex = (x($options, 'tabindex') ? "tabindex=\"" . $options["tabindex"] . "\"" : "");
140 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"" . $x['size'] . "\" $tabindex >\r\n";
142 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"" . $x['size'] . "$\" $tabindex >\r\n";
145 $r = q("SELECT `id`, `name`, `url`, `network` FROM `contact`
146 WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
148 ORDER BY `name` ASC ",
153 $arr = array('contact' => $r, 'entry' => $o);
155 // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
157 call_hooks($a->module . '_pre_' . $selname, $arr);
159 if (dbm::is_result($r)) {
160 foreach ($r as $rr) {
161 if ((is_array($preselected)) && in_array($rr['id'], $preselected)) {
162 $selected = " selected=\"selected\" ";
167 $trimmed = mb_substr($rr['name'],0,20);
169 $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}|{$rr['url']}\" >$trimmed</option>\r\n";
174 $o .= "</select>\r\n";
176 call_hooks($a->module . '_post_' . $selname, $o);
183 function contact_select($selname, $selclass, $preselected = false, $size = 4, $privmail = false, $celeb = false, $privatenet = false, $tabindex = null) {
185 require_once "include/bbcode.php";
191 // When used for private messages, we limit correspondence to mutual DFRN/Friendica friends and the selector
192 // to one recipient. By default our selector allows multiple selects amongst all contacts.
196 if ($privmail || $celeb) {
197 $sql_extra .= sprintf(" AND `rel` = %d ", intval(CONTACT_IS_FRIEND));
201 $sql_extra .= sprintf(" AND `network` IN ('%s' , '%s') ",
202 NETWORK_DFRN, NETWORK_DIASPORA);
203 } elseif ($privatenet) {
204 $sql_extra .= sprintf(" AND `network` IN ('%s' , '%s', '%s', '%s') ",
205 NETWORK_DFRN, NETWORK_MAIL, NETWORK_FACEBOOK, NETWORK_DIASPORA);
208 $tabindex = ($tabindex > 0 ? "tabindex=\"$tabindex\"" : "");
210 if ($privmail AND $preselected) {
211 $sql_extra .= " AND `id` IN (".implode(",", $preselected).")";
212 $hidepreselected = ' style="display: none;"';
214 $hidepreselected = "";
218 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"$size\" $tabindex $hidepreselected>\r\n";
220 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"$size\" $tabindex >\r\n";
223 $r = q("SELECT `id`, `name`, `url`, `network` FROM `contact`
224 WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
226 ORDER BY `name` ASC ",
231 $arr = array('contact' => $r, 'entry' => $o);
233 // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
235 call_hooks($a->module . '_pre_' . $selname, $arr);
237 $receiverlist = array();
239 if (dbm::is_result($r)) {
240 foreach ($r as $rr) {
241 if ((is_array($preselected)) && in_array($rr['id'], $preselected)) {
242 $selected = " selected=\"selected\" ";
248 $trimmed = GetProfileUsername($rr['url'], $rr['name'], false);
250 $trimmed = mb_substr($rr['name'],0,20);
253 $receiverlist[] = $trimmed;
255 $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}|{$rr['url']}\" >$trimmed</option>\r\n";
260 $o .= "</select>\r\n";
262 if ($privmail AND $preselected) {
263 $o .= implode(", ", $receiverlist);
266 call_hooks($a->module . '_post_' . $selname, $o);
272 function fixacl(&$item) {
273 $item = intval(str_replace(array('<', '>'), array('', ''), $item));
276 function prune_deadguys($arr) {
282 $str = dbesc(implode(',', $arr));
284 $r = q("SELECT `id` FROM `contact` WHERE `id` IN ( " . $str . ") AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 ");
286 if (dbm::is_result($r)) {
288 foreach ($r as $rr) {
289 $ret[] = intval($rr['id']);
298 function get_acl_permissions($user = null) {
299 $allow_cid = $allow_gid = $deny_cid = $deny_gid = false;
301 if (is_array($user)) {
302 $allow_cid = ((strlen($user['allow_cid']))
303 ? explode('><', $user['allow_cid']) : array() );
304 $allow_gid = ((strlen($user['allow_gid']))
305 ? explode('><', $user['allow_gid']) : array() );
306 $deny_cid = ((strlen($user['deny_cid']))
307 ? explode('><', $user['deny_cid']) : array() );
308 $deny_gid = ((strlen($user['deny_gid']))
309 ? explode('><', $user['deny_gid']) : array() );
310 array_walk($allow_cid,'fixacl');
311 array_walk($allow_gid,'fixacl');
312 array_walk($deny_cid,'fixacl');
313 array_walk($deny_gid,'fixacl');
316 $allow_cid = prune_deadguys($allow_cid);
319 'allow_cid' => $allow_cid,
320 'allow_gid' => $allow_gid,
321 'deny_cid' => $deny_cid,
322 'deny_gid' => $deny_gid,
327 function populate_acl($user = null, $show_jotnets = false) {
329 $perms = get_acl_permissions($user);
333 $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
335 $mail_enabled = false;
336 $pubmail_enabled = false;
338 if (! $mail_disabled) {
339 $r = q("SELECT `pubmail` FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1",
342 if (dbm::is_result($r)) {
343 $mail_enabled = true;
344 if (intval($r[0]['pubmail'])) {
345 $pubmail_enabled = true;
350 if (!$user['hidewall']) {
352 $selected = (($pubmail_enabled) ? ' checked="checked" ' : '');
353 $jotnets .= '<div class="profile-jot-net"><input type="checkbox" name="pubmail_enable"' . $selected . ' value="1" /> ' . t("Post to Email") . '</div>';
356 call_hooks('jot_networks', $jotnets);
358 $jotnets .= sprintf(t('Connectors disabled, since "%s" is enabled.'),
359 t('Hide your profile details from unknown viewers?'));
363 $tpl = get_markup_template("acl_selector.tpl");
364 $o = replace_macros($tpl, array(
365 '$showall'=> t("Visible to everybody"),
366 '$show' => t("show"),
367 '$hide' => t("don't show"),
368 '$allowcid' => json_encode($perms['allow_cid']),
369 '$allowgid' => json_encode($perms['allow_gid']),
370 '$denycid' => json_encode($perms['deny_cid']),
371 '$denygid' => json_encode($perms['deny_gid']),
372 '$networks' => $show_jotnets,
373 '$emailcc' => t('CC: email addresses'),
374 '$emtitle' => t('Example: bob@example.com, mary@example.com'),
375 '$jotnets' => $jotnets,
376 '$aclModalTitle' => t('Permissions'),
377 '$aclModalDismiss' => t('Close'),
378 '$features' => array(
379 'aclautomention' => (feature_enabled($user['uid'], "aclautomention") ? "true" : "false")
388 function construct_acl_data(App $a, $user) {
390 // Get group and contact information for html ACL selector
391 $acl_data = acl_lookup($a, 'html');
393 $user_defaults = get_acl_permissions($user);
395 if ($acl_data['groups']) {
396 foreach ($acl_data['groups'] as $key => $group) {
397 // Add a "selected" flag to groups that are posted to by default
398 if ($user_defaults['allow_gid'] &&
399 in_array($group['id'], $user_defaults['allow_gid']) && !in_array($group['id'], $user_defaults['deny_gid']) ) {
400 $acl_data['groups'][$key]['selected'] = 1;
402 $acl_data['groups'][$key]['selected'] = 0;
406 if ($acl_data['contacts']) {
407 foreach ($acl_data['contacts'] as $key => $contact) {
408 // Add a "selected" flag to groups that are posted to by default
409 if ($user_defaults['allow_cid'] &&
410 in_array($contact['id'], $user_defaults['allow_cid']) && !in_array($contact['id'], $user_defaults['deny_cid']) ) {
411 $acl_data['contacts'][$key]['selected'] = 1;
413 $acl_data['contacts'][$key]['selected'] = 0;
422 function acl_lookup(App $a, $out_type = 'json') {
428 $start = (x($_REQUEST,'start') ? $_REQUEST['start'] : 0);
429 $count = (x($_REQUEST,'count') ? $_REQUEST['count'] : 100);
430 $search = (x($_REQUEST,'search') ? $_REQUEST['search'] : "");
431 $type = (x($_REQUEST,'type') ? $_REQUEST['type'] : "");
432 $mode = (x($_REQUEST,'smode') ? $_REQUEST['smode'] : "");
433 $conv_id = (x($_REQUEST,'conversation') ? $_REQUEST['conversation'] : null);
435 // For use with jquery.textcomplete for private mail completion
437 if (x($_REQUEST, 'query') && strlen($_REQUEST['query'])) {
441 $search = $_REQUEST['query'];
444 logger("Searching for ".$search." - type ".$type, LOGGER_DEBUG);
447 $sql_extra = "AND `name` LIKE '%%".dbesc($search)."%%'";
448 $sql_extra2 = "AND (`attag` LIKE '%%".dbesc($search)."%%' OR `name` LIKE '%%".dbesc($search)."%%' OR `nick` LIKE '%%".dbesc($search)."%%')";
450 /// @TODO Avoid these needless else blocks by putting variable-initialization atop of if()
451 $sql_extra = $sql_extra2 = "";
454 // count groups and contacts
455 if ($type == '' || $type == 'g') {
456 $r = q("SELECT COUNT(*) AS g FROM `group` WHERE `deleted` = 0 AND `uid` = %d $sql_extra",
459 $group_count = (int)$r[0]['g'];
464 $sql_extra2 .= " ".unavailable_networks();
466 if ($type == '' || $type == 'c') {
467 // autocomplete for editor mentions
468 $r = q("SELECT COUNT(*) AS c FROM `contact`
469 WHERE `uid` = %d AND NOT `self`
470 AND NOT `blocked` AND NOT `pending` AND NOT `archive`
471 AND `notify` != '' $sql_extra2" ,
474 $contact_count = (int)$r[0]['c'];
476 elseif ($type == 'm') {
478 // autocomplete for Private Messages
480 $r = q("SELECT COUNT(*) AS c FROM `contact`
481 WHERE `uid` = %d AND NOT `self`
482 AND NOT `blocked` AND NOT `pending` AND NOT `archive`
483 AND `network` IN ('%s','%s','%s') $sql_extra2" ,
484 intval(local_user()),
487 dbesc(NETWORK_DIASPORA)
489 $contact_count = (int)$r[0]['c'];
492 elseif ($type == 'a') {
494 // autocomplete for Contacts
496 $r = q("SELECT COUNT(*) AS c FROM `contact`
497 WHERE `uid` = %d AND NOT `self`
498 AND NOT `pending` $sql_extra2" ,
501 $contact_count = (int)$r[0]['c'];
508 $tot = $group_count+$contact_count;
513 if ($type == '' || $type == 'g') {
515 /// @todo We should cache this query.
516 // This can be done when we can delete cache entries via wildcard
517 $r = q("SELECT `group`.`id`, `group`.`name`, GROUP_CONCAT(DISTINCT `group_member`.`contact-id` SEPARATOR ',') AS uids
519 INNER JOIN `group_member` ON `group_member`.`gid`=`group`.`id` AND `group_member`.`uid` = `group`.`uid`
520 WHERE NOT `group`.`deleted` AND `group`.`uid` = %d
522 GROUP BY `group`.`name`, `group`.`id`
523 ORDER BY `group`.`name`
525 intval(local_user()),
531 // logger('acl: group: ' . $g['name'] . ' members: ' . $g['uids']);
534 "photo" => "images/twopeople.png",
535 "name" => htmlentities($g['name']),
536 "id" => intval($g['id']),
537 "uids" => array_map("intval", explode(",",$g['uids'])),
546 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `forum`, `prv` FROM `contact`
547 WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
548 AND NOT (`network` IN ('%s', '%s'))
550 ORDER BY `name` ASC ",
551 intval(local_user()),
552 dbesc(NETWORK_OSTATUS), dbesc(NETWORK_STATUSNET)
554 } elseif ($type == 'c') {
555 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `forum`, `prv` FROM `contact`
556 WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
557 AND NOT (`network` IN ('%s'))
559 ORDER BY `name` ASC ",
560 intval(local_user()),
561 dbesc(NETWORK_STATUSNET)
564 elseif ($type == 'm') {
565 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag` FROM `contact`
566 WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive`
567 AND `network` IN ('%s','%s','%s')
569 ORDER BY `name` ASC ",
570 intval(local_user()),
573 dbesc(NETWORK_DIASPORA)
575 } elseif ($type == 'a') {
576 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `forum`, `prv` FROM `contact`
577 WHERE `uid` = %d AND `pending` = 0
579 ORDER BY `name` ASC ",
582 } elseif ($type == 'x') {
583 // autocomplete for global contact search (e.g. navbar search)
584 $r = navbar_complete($a);
589 'photo' => proxy_url($g['photo'], false, PROXY_SIZE_MICRO),
590 'name' => $g['name'],
591 'nick' => (x($g['addr']) ? $g['addr'] : $g['url']),
592 'network' => $g['network'],
594 'forum' => (x($g['community']) ? 1 : 0),
601 'items' => $contacts,
603 echo json_encode($o);
610 if (dbm::is_result($r)) {
614 'photo' => proxy_url($g['micro'], false, PROXY_SIZE_MICRO),
615 'name' => htmlentities($g['name']),
616 'id' => intval($g['id']),
617 'network' => $g['network'],
619 'nick' => htmlentities(($g['attag']) ? $g['attag'] : $g['nick']),
620 'forum' => ((x($g, 'forum') || x($g, 'prv')) ? 1 : 0),
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 = get_contact_details_by_url($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 'forum' => $contact['forum']
670 $items = array_merge($items, $unknown_contacts);
671 $tot += count($unknown_contacts);
679 'contacts' => $contacts,
685 call_hooks('acl_lookup_end', $results);
687 if ($out_type === 'html') {
689 'tot' => $results['tot'],
690 'start' => $results['start'],
691 'count' => $results['count'],
692 'groups' => $results['groups'],
693 'contacts' => $results['contacts'],
699 'tot' => $results['tot'],
700 'start' => $results['start'],
701 'count' => $results['count'],
702 'items' => $results['items'],
705 echo json_encode($o);
710 * @brief Searching for global contacts for autocompletion
713 * @return array with the search results
715 function navbar_complete(App $a) {
717 // logger('navbar_complete');
719 if ((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
723 // check if searching in the local global contact table is enabled
724 $localsearch = get_config('system','poco_local_search');
726 $search = $prefix.notags(trim($_REQUEST['search']));
727 $mode = $_REQUEST['smode'];
729 // don't search if search term has less than 2 characters
730 if (! $search || mb_strlen($search) < 2) {
734 if (substr($search,0,1) === '@') {
735 $search = substr($search,1);
739 $x = DirSearch::global_search_by_name($search, $mode);
743 if (! $localsearch) {
744 $p = (($a->pager['page'] != 1) ? '&p=' . $a->pager['page'] : '');
746 $x = z_fetch_url(get_server().'/lsearch?f=' . $p . '&search=' . urlencode($search));
749 $j = json_decode($x['body'],true);
750 if ($j && $j['results']) {
751 return $j['results'];
756 /// @TODO Not needed here?