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\" ";
43 $trimmed = mb_substr($rr['name'],0,12);
45 $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}\" >$trimmed</option>\r\n";
49 $o .= "</select>\r\n";
51 call_hooks($a->module . '_post_' . $selname, $o);
58 function contact_selector($selname, $selclass, $preselected = false, $options) {
68 if (is_array($options)) {
69 if (x($options,'size'))
70 $size = $options['size'];
72 if (x($options,'mutual_friends')) {
75 if (x($options,'single')) {
78 if (x($options,'multiple')) {
81 if (x($options,'exclude')) {
82 $exclude = $options['exclude'];
85 if (x($options,'networks')) {
86 switch($options['networks']) {
88 $networks = array(NETWORK_DFRN);
91 if (is_array($a->user) && $a->user['prvnets'])
92 $networks = array(NETWORK_DFRN,NETWORK_MAIL,NETWORK_DIASPORA);
94 $networks = array(NETWORK_DFRN,NETWORK_FACEBOOK,NETWORK_MAIL, NETWORK_DIASPORA);
97 if (is_array($a->user) && $a->user['prvnets'])
98 $networks = array(NETWORK_DFRN,NETWORK_MAIL,NETWORK_DIASPORA);
100 $networks = array(NETWORK_DFRN,NETWORK_FACEBOOK,NETWORK_MAIL,NETWORK_DIASPORA,NETWORK_OSTATUS);
108 $x = array('options' => $options, 'size' => $size, 'single' => $single, 'mutual' => $mutual, 'exclude' => $exclude, 'networks' => $networks);
110 call_hooks('contact_select_options', $x);
117 $sql_extra .= sprintf(" AND `rel` = %d ", intval(CONTACT_IS_FRIEND));
120 if (intval($x['exclude']))
121 $sql_extra .= sprintf(" AND `id` != %d ", intval($x['exclude']));
123 if (is_array($x['networks']) && count($x['networks'])) {
124 for ($y = 0; $y < count($x['networks']) ; $y ++) {
125 $x['networks'][$y] = "'" . dbesc($x['networks'][$y]) . "'";
127 $str_nets = implode(',',$x['networks']);
128 $sql_extra .= " AND `network` IN ( $str_nets ) ";
131 $tabindex = (x($options, 'tabindex') ? "tabindex=\"" . $options["tabindex"] . "\"" : "");
134 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"" . $x['size'] . "\" $tabindex >\r\n";
136 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"" . $x['size'] . "$\" $tabindex >\r\n";
138 $r = q("SELECT `id`, `name`, `url`, `network` FROM `contact`
139 WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
141 ORDER BY `name` ASC ",
146 $arr = array('contact' => $r, 'entry' => $o);
148 // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
150 call_hooks($a->module . '_pre_' . $selname, $arr);
152 if (dbm::is_result($r)) {
153 foreach ($r as $rr) {
154 if ((is_array($preselected)) && in_array($rr['id'], $preselected)) {
155 $selected = " selected=\"selected\" ";
160 $trimmed = mb_substr($rr['name'],0,20);
162 $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}|{$rr['url']}\" >$trimmed</option>\r\n";
167 $o .= "</select>\r\n";
169 call_hooks($a->module . '_post_' . $selname, $o);
176 function contact_select($selname, $selclass, $preselected = false, $size = 4, $privmail = false, $celeb = false, $privatenet = false, $tabindex = null) {
178 require_once("include/bbcode.php");
184 // When used for private messages, we limit correspondence to mutual DFRN/Friendica friends and the selector
185 // to one recipient. By default our selector allows multiple selects amongst all contacts.
189 if ($privmail || $celeb) {
190 $sql_extra .= sprintf(" AND `rel` = %d ", intval(CONTACT_IS_FRIEND));
194 $sql_extra .= sprintf(" AND `network` IN ('%s' , '%s') ",
195 NETWORK_DFRN, NETWORK_DIASPORA);
197 $sql_extra .= sprintf(" AND `network` IN ('%s' , '%s', '%s', '%s') ",
198 NETWORK_DFRN, NETWORK_MAIL, NETWORK_FACEBOOK, NETWORK_DIASPORA);
200 $tabindex = ($tabindex > 0 ? "tabindex=\"$tabindex\"" : "");
202 if ($privmail AND $preselected) {
203 $sql_extra .= " AND `id` IN (".implode(",", $preselected).")";
204 $hidepreselected = ' style="display: none;"';
206 $hidepreselected = "";
209 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"$size\" $tabindex $hidepreselected>\r\n";
211 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"$size\" $tabindex >\r\n";
213 $r = q("SELECT `id`, `name`, `url`, `network` FROM `contact`
214 WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
216 ORDER BY `name` ASC ",
221 $arr = array('contact' => $r, 'entry' => $o);
223 // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
225 call_hooks($a->module . '_pre_' . $selname, $arr);
227 $receiverlist = array();
229 if (dbm::is_result($r)) {
230 foreach ($r as $rr) {
231 if ((is_array($preselected)) && in_array($rr['id'], $preselected)) {
232 $selected = " selected=\"selected\" ";
239 $trimmed = GetProfileUsername($rr['url'], $rr['name'], false);
241 $trimmed = mb_substr($rr['name'],0,20);
244 $receiverlist[] = $trimmed;
246 $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}|{$rr['url']}\" >$trimmed</option>\r\n";
251 $o .= "</select>\r\n";
253 if ($privmail AND $preselected)
254 $o .= implode(", ", $receiverlist);
256 call_hooks($a->module . '_post_' . $selname, $o);
262 function fixacl(&$item) {
263 $item = intval(str_replace(array('<','>'),array('',''),$item));
266 function prune_deadguys($arr) {
272 $str = dbesc(implode(',',$arr));
274 $r = q("SELECT `id` FROM `contact` WHERE `id` IN ( " . $str . ") AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 ");
276 if (dbm::is_result($r)) {
278 foreach ($r as $rr) {
279 $ret[] = intval($rr['id']);
288 function get_acl_permissions($user = null) {
289 $allow_cid = $allow_gid = $deny_cid = $deny_gid = false;
291 if (is_array($user)) {
292 $allow_cid = ((strlen($user['allow_cid']))
293 ? explode('><', $user['allow_cid']) : array() );
294 $allow_gid = ((strlen($user['allow_gid']))
295 ? explode('><', $user['allow_gid']) : array() );
296 $deny_cid = ((strlen($user['deny_cid']))
297 ? explode('><', $user['deny_cid']) : array() );
298 $deny_gid = ((strlen($user['deny_gid']))
299 ? explode('><', $user['deny_gid']) : array() );
300 array_walk($allow_cid,'fixacl');
301 array_walk($allow_gid,'fixacl');
302 array_walk($deny_cid,'fixacl');
303 array_walk($deny_gid,'fixacl');
306 $allow_cid = prune_deadguys($allow_cid);
309 'allow_cid' => $allow_cid,
310 'allow_gid' => $allow_gid,
311 'deny_cid' => $deny_cid,
312 'deny_gid' => $deny_gid,
317 function populate_acl($user = null, $show_jotnets = false) {
319 $perms = get_acl_permissions($user);
323 $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
325 $mail_enabled = false;
326 $pubmail_enabled = false;
328 if (! $mail_disabled) {
329 $r = q("SELECT `pubmail` FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1",
332 if (dbm::is_result($r)) {
333 $mail_enabled = true;
334 if (intval($r[0]['pubmail']))
335 $pubmail_enabled = true;
339 if (!$user['hidewall']) {
341 $selected = (($pubmail_enabled) ? ' checked="checked" ' : '');
342 $jotnets .= '<div class="profile-jot-net"><input type="checkbox" name="pubmail_enable"' . $selected . ' value="1" /> ' . t("Post to Email") . '</div>';
345 call_hooks('jot_networks', $jotnets);
347 $jotnets .= sprintf(t('Connectors disabled, since "%s" is enabled.'),
348 t('Hide your profile details from unknown viewers?'));
351 $tpl = get_markup_template("acl_selector.tpl");
352 $o = replace_macros($tpl, array(
353 '$showall'=> t("Visible to everybody"),
354 '$show' => t("show"),
355 '$hide' => t("don't show"),
356 '$allowcid' => json_encode($perms['allow_cid']),
357 '$allowgid' => json_encode($perms['allow_gid']),
358 '$denycid' => json_encode($perms['deny_cid']),
359 '$denygid' => json_encode($perms['deny_gid']),
360 '$networks' => $show_jotnets,
361 '$emailcc' => t('CC: email addresses'),
362 '$emtitle' => t('Example: bob@example.com, mary@example.com'),
363 '$jotnets' => $jotnets,
364 '$aclModalTitle' => t('Permissions'),
365 '$aclModalDismiss' => t('Close'),
366 '$features' => array(
367 "aclautomention"=>(feature_enabled($user['uid'],"aclautomention")?"true":"false")
376 function construct_acl_data(App $a, $user) {
378 // Get group and contact information for html ACL selector
379 $acl_data = acl_lookup($a, 'html');
381 $user_defaults = get_acl_permissions($user);
383 if ($acl_data['groups']) {
384 foreach ($acl_data['groups'] as $key=>$group) {
385 // Add a "selected" flag to groups that are posted to by default
386 if ($user_defaults['allow_gid'] &&
387 in_array($group['id'], $user_defaults['allow_gid']) && !in_array($group['id'], $user_defaults['deny_gid']) )
388 $acl_data['groups'][$key]['selected'] = 1;
390 $acl_data['groups'][$key]['selected'] = 0;
393 if ($acl_data['contacts']) {
394 foreach ($acl_data['contacts'] as $key=>$contact) {
395 // Add a "selected" flag to groups that are posted to by default
396 if ($user_defaults['allow_cid'] &&
397 in_array($contact['id'], $user_defaults['allow_cid']) && !in_array($contact['id'], $user_defaults['deny_cid']) )
398 $acl_data['contacts'][$key]['selected'] = 1;
400 $acl_data['contacts'][$key]['selected'] = 0;
408 function acl_lookup(App $a, $out_type = 'json') {
414 $start = (x($_REQUEST,'start') ? $_REQUEST['start'] : 0);
415 $count = (x($_REQUEST,'count') ? $_REQUEST['count'] : 100);
416 $search = (x($_REQUEST,'search') ? $_REQUEST['search'] : "");
417 $type = (x($_REQUEST,'type') ? $_REQUEST['type'] : "");
418 $mode = (x($_REQUEST,'smode') ? $_REQUEST['smode'] : "");
419 $conv_id = (x($_REQUEST,'conversation') ? $_REQUEST['conversation'] : null);
421 // For use with jquery.textcomplete for private mail completion
423 if (x($_REQUEST,'query') && strlen($_REQUEST['query'])) {
426 $search = $_REQUEST['query'];
429 logger("Searching for ".$search." - type ".$type, LOGGER_DEBUG);
432 $sql_extra = "AND `name` LIKE '%%".dbesc($search)."%%'";
433 $sql_extra2 = "AND (`attag` LIKE '%%".dbesc($search)."%%' OR `name` LIKE '%%".dbesc($search)."%%' OR `nick` LIKE '%%".dbesc($search)."%%')";
435 $sql_extra = $sql_extra2 = "";
438 // count groups and contacts
439 if ($type=='' || $type=='g'){
440 $r = q("SELECT COUNT(*) AS g FROM `group` WHERE `deleted` = 0 AND `uid` = %d $sql_extra",
443 $group_count = (int)$r[0]['g'];
448 $sql_extra2 .= " ".unavailable_networks();
450 // autocomplete for editor mentions
451 if ($type=='' || $type=='c'){
452 $r = q("SELECT COUNT(*) AS c FROM `contact`
453 WHERE `uid` = %d AND NOT `self`
454 AND NOT `blocked` AND NOT `pending` AND NOT `archive`
455 AND `notify` != '' $sql_extra2" ,
458 $contact_count = (int)$r[0]['c'];
460 elseif ($type == 'm') {
462 // autocomplete for Private Messages
464 $r = q("SELECT COUNT(*) AS c FROM `contact`
465 WHERE `uid` = %d AND NOT `self`
466 AND NOT `blocked` AND NOT `pending` AND NOT `archive`
467 AND `network` IN ('%s','%s','%s') $sql_extra2" ,
468 intval(local_user()),
471 dbesc(NETWORK_DIASPORA)
473 $contact_count = (int)$r[0]['c'];
476 elseif ($type == 'a') {
478 // autocomplete for Contacts
480 $r = q("SELECT COUNT(*) AS c FROM `contact`
481 WHERE `uid` = %d AND NOT `self`
482 AND NOT `pending` $sql_extra2" ,
485 $contact_count = (int)$r[0]['c'];
492 $tot = $group_count+$contact_count;
497 if ($type=='' || $type=='g'){
499 /// @todo We should cache this query.
500 // This can be done when we can delete cache entries via wildcard
501 $r = q("SELECT `group`.`id`, `group`.`name`, GROUP_CONCAT(DISTINCT `group_member`.`contact-id` SEPARATOR ',') AS uids
503 INNER JOIN `group_member` ON `group_member`.`gid`=`group`.`id` AND `group_member`.`uid` = `group`.`uid`
504 WHERE NOT `group`.`deleted` AND `group`.`uid` = %d
506 GROUP BY `group`.`name`
507 ORDER BY `group`.`name`
509 intval(local_user()),
515 // logger('acl: group: ' . $g['name'] . ' members: ' . $g['uids']);
518 "photo" => "images/twopeople.png",
519 "name" => htmlentities($g['name']),
520 "id" => intval($g['id']),
521 "uids" => array_map("intval", explode(",",$g['uids'])),
530 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `forum`, `prv` FROM `contact`
531 WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
532 AND NOT (`network` IN ('%s', '%s'))
534 ORDER BY `name` ASC ",
535 intval(local_user()),
536 dbesc(NETWORK_OSTATUS), dbesc(NETWORK_STATUSNET)
541 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `forum`, `prv` FROM `contact`
542 WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
543 AND NOT (`network` IN ('%s'))
545 ORDER BY `name` ASC ",
546 intval(local_user()),
547 dbesc(NETWORK_STATUSNET)
550 elseif ($type == 'm') {
551 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag` FROM `contact`
552 WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive`
553 AND `network` IN ('%s','%s','%s')
555 ORDER BY `name` ASC ",
556 intval(local_user()),
559 dbesc(NETWORK_DIASPORA)
561 } elseif ($type == 'a') {
562 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `forum`, `prv` FROM `contact`
563 WHERE `uid` = %d AND `pending` = 0
565 ORDER BY `name` ASC ",
568 } elseif ($type == 'x') {
569 // autocomplete for global contact search (e.g. navbar search)
570 $r = navbar_complete($a);
575 'photo' => proxy_url($g['photo'], false, PROXY_SIZE_MICRO),
576 'name' => $g['name'],
577 'nick' => (x($g['addr']) ? $g['addr'] : $g['url']),
578 'network' => $g['network'],
580 'forum' => (x($g['community']) ? 1 : 0),
587 'items' => $contacts,
589 echo json_encode($o);
596 if (dbm::is_result($r)) {
600 'photo' => proxy_url($g['micro'], false, PROXY_SIZE_MICRO),
601 'name' => htmlentities($g['name']),
602 'id' => intval($g['id']),
603 'network' => $g['network'],
605 'nick' => htmlentities(($g['attag']) ? $g['attag'] : $g['nick']),
606 'forum' => ((x($g['forum']) || x($g['prv'])) ? 1 : 0),
611 $items = array_merge($groups, $contacts);
614 /* if $conv_id is set, get unknow contacts in thread */
615 /* but first get know contacts url to filter them out */
616 function _contact_link($i){ return dbesc($i['link']); }
617 $known_contacts = array_map(_contact_link, $contacts);
618 $unknow_contacts=array();
619 $r = q("SELECT `author-avatar`,`author-name`,`author-link`
620 FROM `item` WHERE `parent` = %d
621 AND (`author-name` LIKE '%%%s%%' OR `author-link` LIKE '%%%s%%')
622 AND `author-link` NOT IN ('%s')
623 GROUP BY `author-link`
624 ORDER BY `author-name` ASC
629 implode("','", $known_contacts)
631 if (dbm::is_result($r)){
632 foreach ($r as $row) {
634 $up = parse_url($row['author-link']);
635 $nick = explode("/",$up['path']);
636 $nick = $nick[count($nick)-1];
637 $nick .= "@".$up['host'];
639 $unknow_contacts[] = array(
641 'photo' => proxy_url($row['author-avatar'], false, PROXY_SIZE_MICRO),
642 'name' => htmlentities($row['author-name']),
644 'network' => 'unknown',
645 'link' => $row['author-link'],
646 'nick' => htmlentities($nick),
652 $items = array_merge($items, $unknow_contacts);
653 $tot += count($unknow_contacts);
661 'contacts' => $contacts,
667 call_hooks('acl_lookup_end', $results);
669 if ($out_type === 'html') {
671 'tot' => $results['tot'],
672 'start' => $results['start'],
673 'count' => $results['count'],
674 'groups' => $results['groups'],
675 'contacts' => $results['contacts'],
681 'tot' => $results['tot'],
682 'start' => $results['start'],
683 'count' => $results['count'],
684 'items' => $results['items'],
687 echo json_encode($o);
692 * @brief Searching for global contacts for autocompletion
695 * @return array with the search results
697 function navbar_complete(App $a) {
699 // logger('navbar_complete');
701 if ((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
705 // check if searching in the local global contact table is enabled
706 $localsearch = get_config('system','poco_local_search');
708 $search = $prefix.notags(trim($_REQUEST['search']));
709 $mode = $_REQUEST['smode'];
711 // don't search if search term has less than 2 characters
712 if (! $search || mb_strlen($search) < 2) {
716 if (substr($search,0,1) === '@') {
717 $search = substr($search,1);
721 $x = DirSearch::global_search_by_name($search, $mode);
725 if (! $localsearch) {
726 $p = (($a->pager['page'] != 1) ? '&p=' . $a->pager['page'] : '');
728 $x = z_fetch_url(get_server().'/lsearch?f=' . $p . '&search=' . urlencode($search));
731 $j = json_decode($x['body'],true);
732 if ($j && $j['results']) {
733 return $j['results'];
738 /// @TODO Not needed here?