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)) {
148 foreach ($r as $rr) {
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)) {
224 foreach ($r as $rr) {
225 if ((is_array($preselected)) && in_array($rr['id'], $preselected)) {
226 $selected = " selected=\"selected\" ";
233 $trimmed = GetProfileUsername($rr['url'], $rr['name'], false);
236 $trimmed = mb_substr($rr['name'],0,20);
239 $receiverlist[] = $trimmed;
241 $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}|{$rr['url']}\" >$trimmed</option>\r\n";
246 $o .= "</select>\r\n";
248 if ($privmail AND $preselected)
249 $o .= implode(", ", $receiverlist);
251 call_hooks($a->module . '_post_' . $selname, $o);
257 function fixacl(&$item) {
258 $item = intval(str_replace(array('<','>'),array('',''),$item));
261 function prune_deadguys($arr) {
265 $str = dbesc(implode(',',$arr));
266 $r = q("SELECT `id` FROM `contact` WHERE `id` IN ( " . $str . ") AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 ");
270 $ret[] = intval($rr['id']);
277 function get_acl_permissions($user = null) {
278 $allow_cid = $allow_gid = $deny_cid = $deny_gid = false;
280 if(is_array($user)) {
281 $allow_cid = ((strlen($user['allow_cid']))
282 ? explode('><', $user['allow_cid']) : array() );
283 $allow_gid = ((strlen($user['allow_gid']))
284 ? explode('><', $user['allow_gid']) : array() );
285 $deny_cid = ((strlen($user['deny_cid']))
286 ? explode('><', $user['deny_cid']) : array() );
287 $deny_gid = ((strlen($user['deny_gid']))
288 ? explode('><', $user['deny_gid']) : array() );
289 array_walk($allow_cid,'fixacl');
290 array_walk($allow_gid,'fixacl');
291 array_walk($deny_cid,'fixacl');
292 array_walk($deny_gid,'fixacl');
295 $allow_cid = prune_deadguys($allow_cid);
298 'allow_cid' => $allow_cid,
299 'allow_gid' => $allow_gid,
300 'deny_cid' => $deny_cid,
301 'deny_gid' => $deny_gid,
306 function populate_acl($user = null, $show_jotnets = false) {
308 $perms = get_acl_permissions($user);
312 $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
314 $mail_enabled = false;
315 $pubmail_enabled = false;
317 if(! $mail_disabled) {
318 $r = q("SELECT `pubmail` FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1",
321 if (dbm::is_result($r)) {
322 $mail_enabled = true;
323 if(intval($r[0]['pubmail']))
324 $pubmail_enabled = true;
328 if (!$user['hidewall']) {
330 $selected = (($pubmail_enabled) ? ' checked="checked" ' : '');
331 $jotnets .= '<div class="profile-jot-net"><input type="checkbox" name="pubmail_enable"' . $selected . ' value="1" /> ' . t("Post to Email") . '</div>';
334 call_hooks('jot_networks', $jotnets);
336 $jotnets .= sprintf(t('Connectors disabled, since "%s" is enabled.'),
337 t('Hide your profile details from unknown viewers?'));
340 $tpl = get_markup_template("acl_selector.tpl");
341 $o = replace_macros($tpl, array(
342 '$showall'=> t("Visible to everybody"),
343 '$show' => t("show"),
344 '$hide' => t("don't show"),
345 '$allowcid' => json_encode($perms['allow_cid']),
346 '$allowgid' => json_encode($perms['allow_gid']),
347 '$denycid' => json_encode($perms['deny_cid']),
348 '$denygid' => json_encode($perms['deny_gid']),
349 '$networks' => $show_jotnets,
350 '$emailcc' => t('CC: email addresses'),
351 '$emtitle' => t('Example: bob@example.com, mary@example.com'),
352 '$jotnets' => $jotnets,
353 '$aclModalTitle' => t('Permissions'),
354 '$aclModalDismiss' => t('Close'),
355 '$features' => array(
356 "aclautomention"=>(feature_enabled($user['uid'],"aclautomention")?"true":"false")
365 function construct_acl_data(&$a, $user) {
367 // Get group and contact information for html ACL selector
368 $acl_data = acl_lookup($a, 'html');
370 $user_defaults = get_acl_permissions($user);
372 if($acl_data['groups']) {
373 foreach($acl_data['groups'] as $key=>$group) {
374 // Add a "selected" flag to groups that are posted to by default
375 if($user_defaults['allow_gid'] &&
376 in_array($group['id'], $user_defaults['allow_gid']) && !in_array($group['id'], $user_defaults['deny_gid']) )
377 $acl_data['groups'][$key]['selected'] = 1;
379 $acl_data['groups'][$key]['selected'] = 0;
382 if($acl_data['contacts']) {
383 foreach($acl_data['contacts'] as $key=>$contact) {
384 // Add a "selected" flag to groups that are posted to by default
385 if($user_defaults['allow_cid'] &&
386 in_array($contact['id'], $user_defaults['allow_cid']) && !in_array($contact['id'], $user_defaults['deny_cid']) )
387 $acl_data['contacts'][$key]['selected'] = 1;
389 $acl_data['contacts'][$key]['selected'] = 0;
397 function acl_lookup(&$a, $out_type = 'json') {
403 $start = (x($_REQUEST,'start') ? $_REQUEST['start'] : 0);
404 $count = (x($_REQUEST,'count') ? $_REQUEST['count'] : 100);
405 $search = (x($_REQUEST,'search') ? $_REQUEST['search'] : "");
406 $type = (x($_REQUEST,'type') ? $_REQUEST['type'] : "");
407 $mode = (x($_REQUEST,'smode') ? $_REQUEST['smode'] : "");
408 $conv_id = (x($_REQUEST,'conversation') ? $_REQUEST['conversation'] : null);
410 // For use with jquery.textcomplete for private mail completion
412 if(x($_REQUEST,'query') && strlen($_REQUEST['query'])) {
415 $search = $_REQUEST['query'];
418 logger("Searching for ".$search." - type ".$type, LOGGER_DEBUG);
421 $sql_extra = "AND `name` LIKE '%%".dbesc($search)."%%'";
422 $sql_extra2 = "AND (`attag` LIKE '%%".dbesc($search)."%%' OR `name` LIKE '%%".dbesc($search)."%%' OR `nick` LIKE '%%".dbesc($search)."%%')";
424 $sql_extra = $sql_extra2 = "";
427 // count groups and contacts
428 if ($type=='' || $type=='g'){
429 $r = q("SELECT COUNT(*) AS g FROM `group` WHERE `deleted` = 0 AND `uid` = %d $sql_extra",
432 $group_count = (int)$r[0]['g'];
437 $sql_extra2 .= " ".unavailable_networks();
439 // autocomplete for editor mentions
440 if ($type=='' || $type=='c'){
441 $r = q("SELECT COUNT(*) AS c FROM `contact`
442 WHERE `uid` = %d AND `self` = 0
443 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0
444 AND `notify` != '' $sql_extra2" ,
447 $contact_count = (int)$r[0]['c'];
449 elseif ($type == 'm') {
451 // autocomplete for Private Messages
453 $r = q("SELECT COUNT(*) AS c FROM `contact`
454 WHERE `uid` = %d AND `self` = 0
455 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0
456 AND `network` IN ('%s','%s','%s') $sql_extra2" ,
457 intval(local_user()),
460 dbesc(NETWORK_DIASPORA)
462 $contact_count = (int)$r[0]['c'];
465 elseif ($type == 'a') {
467 // autocomplete for Contacts
469 $r = q("SELECT COUNT(*) AS c FROM `contact`
470 WHERE `uid` = %d AND `self` = 0
471 AND `pending` = 0 $sql_extra2" ,
474 $contact_count = (int)$r[0]['c'];
481 $tot = $group_count+$contact_count;
486 if ($type=='' || $type=='g'){
488 $r = q("SELECT `group`.`id`, `group`.`name`, GROUP_CONCAT(DISTINCT `group_member`.`contact-id` SEPARATOR ',') AS uids
490 INNER JOIN `group_member` ON `group_member`.`gid`=`group`.`id` AND `group_member`.`uid` = `group`.`uid`
491 WHERE NOT `group`.`deleted` AND `group`.`uid` = %d
493 GROUP BY `group`.`name`
494 ORDER BY `group`.`name`
496 intval(local_user()),
502 // logger('acl: group: ' . $g['name'] . ' members: ' . $g['uids']);
505 "photo" => "images/twopeople.png",
506 "name" => htmlentities($g['name']),
507 "id" => intval($g['id']),
508 "uids" => array_map("intval", explode(",",$g['uids'])),
517 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `forum`, `prv` FROM `contact`
518 WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 AND `notify` != ''
519 AND NOT (`network` IN ('%s', '%s'))
521 ORDER BY `name` ASC ",
522 intval(local_user()),
523 dbesc(NETWORK_OSTATUS), dbesc(NETWORK_STATUSNET)
528 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `forum`, `prv` FROM `contact`
529 WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 AND `notify` != ''
530 AND NOT (`network` IN ('%s'))
532 ORDER BY `name` ASC ",
533 intval(local_user()),
534 dbesc(NETWORK_STATUSNET)
537 elseif($type == 'm') {
538 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag` FROM `contact`
539 WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0
540 AND `network` IN ('%s','%s','%s')
542 ORDER BY `name` ASC ",
543 intval(local_user()),
546 dbesc(NETWORK_DIASPORA)
549 elseif($type == 'a') {
550 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `forum`, `prv` FROM `contact`
551 WHERE `uid` = %d AND `pending` = 0
553 ORDER BY `name` ASC ",
557 elseif($type == 'x') {
558 // autocomplete for global contact search (e.g. navbar search)
559 $r = navbar_complete($a);
564 "photo" => proxy_url($g['photo'], false, PROXY_SIZE_MICRO),
565 "name" => $g['name'],
566 "nick" => (x($g['addr']) ? $g['addr'] : $g['url']),
567 "network" => $g['network'],
569 "forum" => (x($g['community']) ? 1 : 0),
576 'items' => $contacts,
578 echo json_encode($o);
585 if (dbm::is_result($r)) {
589 "photo" => proxy_url($g['micro'], false, PROXY_SIZE_MICRO),
590 "name" => htmlentities($g['name']),
591 "id" => intval($g['id']),
592 "network" => $g['network'],
594 "nick" => htmlentities(($g['attag']) ? $g['attag'] : $g['nick']),
595 "forum" => ((x($g['forum']) || x($g['prv'])) ? 1 : 0),
600 $items = array_merge($groups, $contacts);
603 /* if $conv_id is set, get unknow contacts in thread */
604 /* but first get know contacts url to filter them out */
605 function _contact_link($i){ return dbesc($i['link']); }
606 $known_contacts = array_map(_contact_link, $contacts);
607 $unknow_contacts=array();
608 $r = q("SELECT `author-avatar`,`author-name`,`author-link`
609 FROM `item` WHERE `parent` = %d
610 AND (`author-name` LIKE '%%%s%%' OR `author-link` LIKE '%%%s%%')
611 AND `author-link` NOT IN ('%s')
612 GROUP BY `author-link`
613 ORDER BY `author-name` ASC
618 implode("','", $known_contacts)
620 if (dbm::is_result($r)){
621 foreach($r as $row) {
623 $up = parse_url($row['author-link']);
624 $nick = explode("/",$up['path']);
625 $nick = $nick[count($nick)-1];
626 $nick .= "@".$up['host'];
628 $unknow_contacts[] = array(
630 "photo" => proxy_url($row['author-avatar'], false, PROXY_SIZE_MICRO),
631 "name" => htmlentities($row['author-name']),
633 "network" => "unknown",
634 "link" => $row['author-link'],
635 "nick" => htmlentities($nick),
641 $items = array_merge($items, $unknow_contacts);
642 $tot += count($unknow_contacts);
650 "contacts" => $contacts,
656 call_hooks('acl_lookup_end', $results);
658 if($out_type === 'html') {
660 'tot' => $results["tot"],
661 'start' => $results["start"],
662 'count' => $results["count"],
663 'groups' => $results["groups"],
664 'contacts' => $results["contacts"],
670 'tot' => $results["tot"],
671 'start' => $results["start"],
672 'count' => $results["count"],
673 'items' => $results["items"],
676 echo json_encode($o);
681 * @brief Searching for global contacts for autocompletion
684 * @return array with the search results
686 function navbar_complete(App &$a) {
688 // logger('navbar_complete');
690 if((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
694 // check if searching in the local global contact table is enabled
695 $localsearch = get_config('system','poco_local_search');
697 $search = $prefix.notags(trim($_REQUEST['search']));
698 $mode = $_REQUEST['smode'];
700 // don't search if search term has less than 2 characters
701 if(! $search || mb_strlen($search) < 2)
704 if(substr($search,0,1) === '@')
705 $search = substr($search,1);
708 $x = DirSearch::global_search_by_name($search, $mode);
713 $p = (($a->pager['page'] != 1) ? '&p=' . $a->pager['page'] : '');
715 $x = z_fetch_url(get_server().'/lsearch?f=' . $p . '&search=' . urlencode($search));
718 $j = json_decode($x['body'],true);
719 if($j && $j['results']) {
720 return $j['results'];