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'))
74 if(x($options,'single'))
76 if(x($options,'multiple'))
78 if(x($options,'exclude'))
79 $exclude = $options['exclude'];
81 if(x($options,'networks')) {
82 switch($options['networks']) {
84 $networks = array(NETWORK_DFRN);
87 if(is_array($a->user) && $a->user['prvnets'])
88 $networks = array(NETWORK_DFRN,NETWORK_MAIL,NETWORK_DIASPORA);
90 $networks = array(NETWORK_DFRN,NETWORK_FACEBOOK,NETWORK_MAIL, NETWORK_DIASPORA);
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,NETWORK_OSTATUS);
104 $x = array('options' => $options, 'size' => $size, 'single' => $single, 'mutual' => $mutual, 'exclude' => $exclude, 'networks' => $networks);
106 call_hooks('contact_select_options', $x);
113 $sql_extra .= sprintf(" AND `rel` = %d ", intval(CONTACT_IS_FRIEND));
116 if(intval($x['exclude']))
117 $sql_extra .= sprintf(" AND `id` != %d ", intval($x['exclude']));
119 if(is_array($x['networks']) && count($x['networks'])) {
120 for($y = 0; $y < count($x['networks']) ; $y ++)
121 $x['networks'][$y] = "'" . dbesc($x['networks'][$y]) . "'";
122 $str_nets = implode(',',$x['networks']);
123 $sql_extra .= " AND `network` IN ( $str_nets ) ";
126 $tabindex = (x($options, 'tabindex') ? "tabindex=\"" . $options["tabindex"] . "\"" : "");
129 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"" . $x['size'] . "\" $tabindex >\r\n";
131 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"" . $x['size'] . "$\" $tabindex >\r\n";
133 $r = q("SELECT `id`, `name`, `url`, `network` FROM `contact`
134 WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 AND `notify` != ''
136 ORDER BY `name` ASC ",
141 $arr = array('contact' => $r, 'entry' => $o);
143 // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
145 call_hooks($a->module . '_pre_' . $selname, $arr);
147 if (dbm::is_result($r)) {
149 if((is_array($preselected)) && in_array($rr['id'], $preselected))
150 $selected = " selected=\"selected\" ";
154 $trimmed = mb_substr($rr['name'],0,20);
156 $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}|{$rr['url']}\" >$trimmed</option>\r\n";
161 $o .= "</select>\r\n";
163 call_hooks($a->module . '_post_' . $selname, $o);
170 function contact_select($selname, $selclass, $preselected = false, $size = 4, $privmail = false, $celeb = false, $privatenet = false, $tabindex = null) {
172 require_once("include/bbcode.php");
178 // When used for private messages, we limit correspondence to mutual DFRN/Friendica friends and the selector
179 // to one recipient. By default our selector allows multiple selects amongst all contacts.
183 if($privmail || $celeb) {
184 $sql_extra .= sprintf(" AND `rel` = %d ", intval(CONTACT_IS_FRIEND));
188 $sql_extra .= sprintf(" AND `network` IN ('%s' , '%s') ",
189 NETWORK_DFRN, NETWORK_DIASPORA);
191 $sql_extra .= sprintf(" AND `network` IN ('%s' , '%s', '%s', '%s') ",
192 NETWORK_DFRN, NETWORK_MAIL, NETWORK_FACEBOOK, NETWORK_DIASPORA);
194 $tabindex = ($tabindex > 0 ? "tabindex=\"$tabindex\"" : "");
196 if ($privmail AND $preselected) {
197 $sql_extra .= " AND `id` IN (".implode(",", $preselected).")";
198 $hidepreselected = ' style="display: none;"';
200 $hidepreselected = "";
203 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"$size\" $tabindex $hidepreselected>\r\n";
205 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"$size\" $tabindex >\r\n";
207 $r = q("SELECT `id`, `name`, `url`, `network` FROM `contact`
208 WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 AND `notify` != ''
210 ORDER BY `name` ASC ",
215 $arr = array('contact' => $r, 'entry' => $o);
217 // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
219 call_hooks($a->module . '_pre_' . $selname, $arr);
221 $receiverlist = array();
223 if (dbm::is_result($r)) {
225 if((is_array($preselected)) && in_array($rr['id'], $preselected))
226 $selected = " selected=\"selected\" ";
231 $trimmed = GetProfileUsername($rr['url'], $rr['name'], false);
233 $trimmed = mb_substr($rr['name'],0,20);
235 $receiverlist[] = $trimmed;
237 $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}|{$rr['url']}\" >$trimmed</option>\r\n";
242 $o .= "</select>\r\n";
244 if ($privmail AND $preselected)
245 $o .= implode(", ", $receiverlist);
247 call_hooks($a->module . '_post_' . $selname, $o);
253 function fixacl(&$item) {
254 $item = intval(str_replace(array('<','>'),array('',''),$item));
257 function prune_deadguys($arr) {
261 $str = dbesc(implode(',',$arr));
262 $r = q("SELECT `id` FROM `contact` WHERE `id` IN ( " . $str . ") AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 ");
266 $ret[] = intval($rr['id']);
273 function get_acl_permissions($user = null) {
274 $allow_cid = $allow_gid = $deny_cid = $deny_gid = false;
276 if(is_array($user)) {
277 $allow_cid = ((strlen($user['allow_cid']))
278 ? explode('><', $user['allow_cid']) : array() );
279 $allow_gid = ((strlen($user['allow_gid']))
280 ? explode('><', $user['allow_gid']) : array() );
281 $deny_cid = ((strlen($user['deny_cid']))
282 ? explode('><', $user['deny_cid']) : array() );
283 $deny_gid = ((strlen($user['deny_gid']))
284 ? explode('><', $user['deny_gid']) : array() );
285 array_walk($allow_cid,'fixacl');
286 array_walk($allow_gid,'fixacl');
287 array_walk($deny_cid,'fixacl');
288 array_walk($deny_gid,'fixacl');
291 $allow_cid = prune_deadguys($allow_cid);
294 'allow_cid' => $allow_cid,
295 'allow_gid' => $allow_gid,
296 'deny_cid' => $deny_cid,
297 'deny_gid' => $deny_gid,
302 function populate_acl($user = null, $show_jotnets = false) {
304 $perms = get_acl_permissions($user);
308 $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
310 $mail_enabled = false;
311 $pubmail_enabled = false;
313 if(! $mail_disabled) {
314 $r = q("SELECT `pubmail` FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1",
317 if (dbm::is_result($r)) {
318 $mail_enabled = true;
319 if(intval($r[0]['pubmail']))
320 $pubmail_enabled = true;
324 if (!$user['hidewall']) {
326 $selected = (($pubmail_enabled) ? ' checked="checked" ' : '');
327 $jotnets .= '<div class="profile-jot-net"><input type="checkbox" name="pubmail_enable"' . $selected . ' value="1" /> ' . t("Post to Email") . '</div>';
330 call_hooks('jot_networks', $jotnets);
332 $jotnets .= sprintf(t('Connectors disabled, since "%s" is enabled.'),
333 t('Hide your profile details from unknown viewers?'));
336 $tpl = get_markup_template("acl_selector.tpl");
337 $o = replace_macros($tpl, array(
338 '$showall'=> t("Visible to everybody"),
339 '$show' => t("show"),
340 '$hide' => t("don't show"),
341 '$allowcid' => json_encode($perms['allow_cid']),
342 '$allowgid' => json_encode($perms['allow_gid']),
343 '$denycid' => json_encode($perms['deny_cid']),
344 '$denygid' => json_encode($perms['deny_gid']),
345 '$networks' => $show_jotnets,
346 '$emailcc' => t('CC: email addresses'),
347 '$emtitle' => t('Example: bob@example.com, mary@example.com'),
348 '$jotnets' => $jotnets,
349 '$aclModalTitle' => t('Permissions'),
350 '$aclModalDismiss' => t('Close'),
351 '$features' => array(
352 "aclautomention"=>(feature_enabled($user['uid'],"aclautomention")?"true":"false")
361 function construct_acl_data(&$a, $user) {
363 // Get group and contact information for html ACL selector
364 $acl_data = acl_lookup($a, 'html');
366 $user_defaults = get_acl_permissions($user);
368 if($acl_data['groups']) {
369 foreach($acl_data['groups'] as $key=>$group) {
370 // Add a "selected" flag to groups that are posted to by default
371 if($user_defaults['allow_gid'] &&
372 in_array($group['id'], $user_defaults['allow_gid']) && !in_array($group['id'], $user_defaults['deny_gid']) )
373 $acl_data['groups'][$key]['selected'] = 1;
375 $acl_data['groups'][$key]['selected'] = 0;
378 if($acl_data['contacts']) {
379 foreach($acl_data['contacts'] as $key=>$contact) {
380 // Add a "selected" flag to groups that are posted to by default
381 if($user_defaults['allow_cid'] &&
382 in_array($contact['id'], $user_defaults['allow_cid']) && !in_array($contact['id'], $user_defaults['deny_cid']) )
383 $acl_data['contacts'][$key]['selected'] = 1;
385 $acl_data['contacts'][$key]['selected'] = 0;
393 function acl_lookup(&$a, $out_type = 'json') {
399 $start = (x($_REQUEST,'start') ? $_REQUEST['start'] : 0);
400 $count = (x($_REQUEST,'count') ? $_REQUEST['count'] : 100);
401 $search = (x($_REQUEST,'search') ? $_REQUEST['search'] : "");
402 $type = (x($_REQUEST,'type') ? $_REQUEST['type'] : "");
403 $mode = (x($_REQUEST,'smode') ? $_REQUEST['smode'] : "");
404 $conv_id = (x($_REQUEST,'conversation') ? $_REQUEST['conversation'] : null);
406 // For use with jquery.textcomplete for private mail completion
408 if(x($_REQUEST,'query') && strlen($_REQUEST['query'])) {
411 $search = $_REQUEST['query'];
414 logger("Searching for ".$search." - type ".$type, LOGGER_DEBUG);
417 $sql_extra = "AND `name` LIKE '%%".dbesc($search)."%%'";
418 $sql_extra2 = "AND (`attag` LIKE '%%".dbesc($search)."%%' OR `name` LIKE '%%".dbesc($search)."%%' OR `nick` LIKE '%%".dbesc($search)."%%')";
420 $sql_extra = $sql_extra2 = "";
423 // count groups and contacts
424 if ($type=='' || $type=='g'){
425 $r = q("SELECT COUNT(*) AS g FROM `group` WHERE `deleted` = 0 AND `uid` = %d $sql_extra",
428 $group_count = (int)$r[0]['g'];
433 $sql_extra2 .= " ".unavailable_networks();
435 // autocomplete for editor mentions
436 if ($type=='' || $type=='c'){
437 $r = q("SELECT COUNT(*) AS c FROM `contact`
438 WHERE `uid` = %d AND `self` = 0
439 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0
440 AND `notify` != '' $sql_extra2" ,
443 $contact_count = (int)$r[0]['c'];
445 elseif ($type == 'm') {
447 // autocomplete for Private Messages
449 $r = q("SELECT COUNT(*) AS c FROM `contact`
450 WHERE `uid` = %d AND `self` = 0
451 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0
452 AND `network` IN ('%s','%s','%s') $sql_extra2" ,
453 intval(local_user()),
456 dbesc(NETWORK_DIASPORA)
458 $contact_count = (int)$r[0]['c'];
461 elseif ($type == 'a') {
463 // autocomplete for Contacts
465 $r = q("SELECT COUNT(*) AS c FROM `contact`
466 WHERE `uid` = %d AND `self` = 0
467 AND `pending` = 0 $sql_extra2" ,
470 $contact_count = (int)$r[0]['c'];
477 $tot = $group_count+$contact_count;
482 if ($type=='' || $type=='g'){
484 $r = q("SELECT `group`.`id`, `group`.`name`, GROUP_CONCAT(DISTINCT `group_member`.`contact-id` SEPARATOR ',') AS uids
486 INNER JOIN `group_member` ON `group_member`.`gid`=`group`.`id` AND `group_member`.`uid` = `group`.`uid`
487 WHERE NOT `group`.`deleted` AND `group`.`uid` = %d
489 GROUP BY `group`.`name`
490 ORDER BY `group`.`name`
492 intval(local_user()),
498 // logger('acl: group: ' . $g['name'] . ' members: ' . $g['uids']);
501 "photo" => "images/twopeople.png",
502 "name" => htmlentities($g['name']),
503 "id" => intval($g['id']),
504 "uids" => array_map("intval", explode(",",$g['uids'])),
513 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `forum`, `prv` FROM `contact`
514 WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 AND `notify` != ''
515 AND NOT (`network` IN ('%s', '%s'))
517 ORDER BY `name` ASC ",
518 intval(local_user()),
519 dbesc(NETWORK_OSTATUS), dbesc(NETWORK_STATUSNET)
524 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `forum`, `prv` FROM `contact`
525 WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 AND `notify` != ''
526 AND NOT (`network` IN ('%s'))
528 ORDER BY `name` ASC ",
529 intval(local_user()),
530 dbesc(NETWORK_STATUSNET)
533 elseif($type == 'm') {
534 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag` FROM `contact`
535 WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0
536 AND `network` IN ('%s','%s','%s')
538 ORDER BY `name` ASC ",
539 intval(local_user()),
542 dbesc(NETWORK_DIASPORA)
545 elseif($type == 'a') {
546 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `forum`, `prv` FROM `contact`
547 WHERE `uid` = %d AND `pending` = 0
549 ORDER BY `name` ASC ",
553 elseif($type == 'x') {
554 // autocomplete for global contact search (e.g. navbar search)
555 $r = navbar_complete($a);
560 "photo" => proxy_url($g['photo'], false, PROXY_SIZE_MICRO),
561 "name" => $g['name'],
562 "nick" => (x($g['addr']) ? $g['addr'] : $g['url']),
563 "network" => $g['network'],
565 "forum" => (x($g['community']) ? 1 : 0),
572 'items' => $contacts,
574 echo json_encode($o);
581 if (dbm::is_result($r)) {
585 "photo" => proxy_url($g['micro'], false, PROXY_SIZE_MICRO),
586 "name" => htmlentities($g['name']),
587 "id" => intval($g['id']),
588 "network" => $g['network'],
590 "nick" => htmlentities(($g['attag']) ? $g['attag'] : $g['nick']),
591 "forum" => ((x($g['forum']) || x($g['prv'])) ? 1 : 0),
596 $items = array_merge($groups, $contacts);
599 /* if $conv_id is set, get unknow contacts in thread */
600 /* but first get know contacts url to filter them out */
601 function _contact_link($i){ return dbesc($i['link']); }
602 $known_contacts = array_map(_contact_link, $contacts);
603 $unknow_contacts=array();
604 $r = q("SELECT `author-avatar`,`author-name`,`author-link`
605 FROM `item` WHERE `parent` = %d
606 AND (`author-name` LIKE '%%%s%%' OR `author-link` LIKE '%%%s%%')
607 AND `author-link` NOT IN ('%s')
608 GROUP BY `author-link`
609 ORDER BY `author-name` ASC
614 implode("','", $known_contacts)
616 if (dbm::is_result($r)){
617 foreach($r as $row) {
619 $up = parse_url($row['author-link']);
620 $nick = explode("/",$up['path']);
621 $nick = $nick[count($nick)-1];
622 $nick .= "@".$up['host'];
624 $unknow_contacts[] = array(
626 "photo" => proxy_url($row['author-avatar'], false, PROXY_SIZE_MICRO),
627 "name" => htmlentities($row['author-name']),
629 "network" => "unknown",
630 "link" => $row['author-link'],
631 "nick" => htmlentities($nick),
637 $items = array_merge($items, $unknow_contacts);
638 $tot += count($unknow_contacts);
646 "contacts" => $contacts,
652 call_hooks('acl_lookup_end', $results);
654 if($out_type === 'html') {
656 'tot' => $results["tot"],
657 'start' => $results["start"],
658 'count' => $results["count"],
659 'groups' => $results["groups"],
660 'contacts' => $results["contacts"],
666 'tot' => $results["tot"],
667 'start' => $results["start"],
668 'count' => $results["count"],
669 'items' => $results["items"],
672 echo json_encode($o);
677 * @brief Searching for global contacts for autocompletion
680 * @return array with the search results
682 function navbar_complete(&$a) {
684 // logger('navbar_complete');
686 if((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
690 // check if searching in the local global contact table is enabled
691 $localsearch = get_config('system','poco_local_search');
693 $search = $prefix.notags(trim($_REQUEST['search']));
694 $mode = $_REQUEST['smode'];
696 // don't search if search term has less than 2 characters
697 if(! $search || mb_strlen($search) < 2)
700 if(substr($search,0,1) === '@')
701 $search = substr($search,1);
704 $x = DirSearch::global_search_by_name($search, $mode);
709 $p = (($a->pager['page'] != 1) ? '&p=' . $a->pager['page'] : '');
711 $x = z_fetch_url(get_server().'/lsearch?f=' . $p . '&search=' . urlencode($search));
714 $j = json_decode($x['body'],true);
715 if($j && $j['results']) {
716 return $j['results'];