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]) . "'";
126 $str_nets = implode(',',$x['networks']);
127 $sql_extra .= " AND `network` IN ( $str_nets ) ";
130 $tabindex = (x($options, 'tabindex') ? "tabindex=\"" . $options["tabindex"] . "\"" : "");
133 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"" . $x['size'] . "\" $tabindex >\r\n";
135 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"" . $x['size'] . "$\" $tabindex >\r\n";
137 $r = q("SELECT `id`, `name`, `url`, `network` FROM `contact`
138 WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
140 ORDER BY `name` ASC ",
145 $arr = array('contact' => $r, 'entry' => $o);
147 // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
149 call_hooks($a->module . '_pre_' . $selname, $arr);
151 if (dbm::is_result($r)) {
152 foreach ($r as $rr) {
153 if ((is_array($preselected)) && in_array($rr['id'], $preselected)) {
154 $selected = " selected=\"selected\" ";
159 $trimmed = mb_substr($rr['name'],0,20);
161 $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}|{$rr['url']}\" >$trimmed</option>\r\n";
166 $o .= "</select>\r\n";
168 call_hooks($a->module . '_post_' . $selname, $o);
175 function contact_select($selname, $selclass, $preselected = false, $size = 4, $privmail = false, $celeb = false, $privatenet = false, $tabindex = null) {
177 require_once("include/bbcode.php");
183 // When used for private messages, we limit correspondence to mutual DFRN/Friendica friends and the selector
184 // to one recipient. By default our selector allows multiple selects amongst all contacts.
188 if($privmail || $celeb) {
189 $sql_extra .= sprintf(" AND `rel` = %d ", intval(CONTACT_IS_FRIEND));
193 $sql_extra .= sprintf(" AND `network` IN ('%s' , '%s') ",
194 NETWORK_DFRN, NETWORK_DIASPORA);
196 $sql_extra .= sprintf(" AND `network` IN ('%s' , '%s', '%s', '%s') ",
197 NETWORK_DFRN, NETWORK_MAIL, NETWORK_FACEBOOK, NETWORK_DIASPORA);
199 $tabindex = ($tabindex > 0 ? "tabindex=\"$tabindex\"" : "");
201 if ($privmail AND $preselected) {
202 $sql_extra .= " AND `id` IN (".implode(",", $preselected).")";
203 $hidepreselected = ' style="display: none;"';
205 $hidepreselected = "";
208 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"$size\" $tabindex $hidepreselected>\r\n";
210 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"$size\" $tabindex >\r\n";
212 $r = q("SELECT `id`, `name`, `url`, `network` FROM `contact`
213 WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
215 ORDER BY `name` ASC ",
220 $arr = array('contact' => $r, 'entry' => $o);
222 // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
224 call_hooks($a->module . '_pre_' . $selname, $arr);
226 $receiverlist = array();
228 if (dbm::is_result($r)) {
229 foreach ($r as $rr) {
230 if ((is_array($preselected)) && in_array($rr['id'], $preselected)) {
231 $selected = " selected=\"selected\" ";
238 $trimmed = GetProfileUsername($rr['url'], $rr['name'], false);
240 $trimmed = mb_substr($rr['name'],0,20);
243 $receiverlist[] = $trimmed;
245 $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}|{$rr['url']}\" >$trimmed</option>\r\n";
250 $o .= "</select>\r\n";
252 if ($privmail AND $preselected)
253 $o .= implode(", ", $receiverlist);
255 call_hooks($a->module . '_post_' . $selname, $o);
261 function fixacl(&$item) {
262 $item = intval(str_replace(array('<','>'),array('',''),$item));
265 function prune_deadguys($arr) {
271 $str = dbesc(implode(',',$arr));
273 $r = q("SELECT `id` FROM `contact` WHERE `id` IN ( " . $str . ") AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 ");
275 if (dbm::is_result($r)) {
277 foreach ($r as $rr) {
278 $ret[] = intval($rr['id']);
287 function get_acl_permissions($user = null) {
288 $allow_cid = $allow_gid = $deny_cid = $deny_gid = false;
290 if(is_array($user)) {
291 $allow_cid = ((strlen($user['allow_cid']))
292 ? explode('><', $user['allow_cid']) : array() );
293 $allow_gid = ((strlen($user['allow_gid']))
294 ? explode('><', $user['allow_gid']) : array() );
295 $deny_cid = ((strlen($user['deny_cid']))
296 ? explode('><', $user['deny_cid']) : array() );
297 $deny_gid = ((strlen($user['deny_gid']))
298 ? explode('><', $user['deny_gid']) : array() );
299 array_walk($allow_cid,'fixacl');
300 array_walk($allow_gid,'fixacl');
301 array_walk($deny_cid,'fixacl');
302 array_walk($deny_gid,'fixacl');
305 $allow_cid = prune_deadguys($allow_cid);
308 'allow_cid' => $allow_cid,
309 'allow_gid' => $allow_gid,
310 'deny_cid' => $deny_cid,
311 'deny_gid' => $deny_gid,
316 function populate_acl($user = null, $show_jotnets = false) {
318 $perms = get_acl_permissions($user);
322 $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
324 $mail_enabled = false;
325 $pubmail_enabled = false;
327 if(! $mail_disabled) {
328 $r = q("SELECT `pubmail` FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1",
331 if (dbm::is_result($r)) {
332 $mail_enabled = true;
333 if(intval($r[0]['pubmail']))
334 $pubmail_enabled = true;
338 if (!$user['hidewall']) {
340 $selected = (($pubmail_enabled) ? ' checked="checked" ' : '');
341 $jotnets .= '<div class="profile-jot-net"><input type="checkbox" name="pubmail_enable"' . $selected . ' value="1" /> ' . t("Post to Email") . '</div>';
344 call_hooks('jot_networks', $jotnets);
346 $jotnets .= sprintf(t('Connectors disabled, since "%s" is enabled.'),
347 t('Hide your profile details from unknown viewers?'));
350 $tpl = get_markup_template("acl_selector.tpl");
351 $o = replace_macros($tpl, array(
352 '$showall'=> t("Visible to everybody"),
353 '$show' => t("show"),
354 '$hide' => t("don't show"),
355 '$allowcid' => json_encode($perms['allow_cid']),
356 '$allowgid' => json_encode($perms['allow_gid']),
357 '$denycid' => json_encode($perms['deny_cid']),
358 '$denygid' => json_encode($perms['deny_gid']),
359 '$networks' => $show_jotnets,
360 '$emailcc' => t('CC: email addresses'),
361 '$emtitle' => t('Example: bob@example.com, mary@example.com'),
362 '$jotnets' => $jotnets,
363 '$aclModalTitle' => t('Permissions'),
364 '$aclModalDismiss' => t('Close'),
365 '$features' => array(
366 "aclautomention"=>(feature_enabled($user['uid'],"aclautomention")?"true":"false")
375 function construct_acl_data(App $a, $user) {
377 // Get group and contact information for html ACL selector
378 $acl_data = acl_lookup($a, 'html');
380 $user_defaults = get_acl_permissions($user);
382 if($acl_data['groups']) {
383 foreach($acl_data['groups'] as $key=>$group) {
384 // Add a "selected" flag to groups that are posted to by default
385 if($user_defaults['allow_gid'] &&
386 in_array($group['id'], $user_defaults['allow_gid']) && !in_array($group['id'], $user_defaults['deny_gid']) )
387 $acl_data['groups'][$key]['selected'] = 1;
389 $acl_data['groups'][$key]['selected'] = 0;
392 if($acl_data['contacts']) {
393 foreach($acl_data['contacts'] as $key=>$contact) {
394 // Add a "selected" flag to groups that are posted to by default
395 if($user_defaults['allow_cid'] &&
396 in_array($contact['id'], $user_defaults['allow_cid']) && !in_array($contact['id'], $user_defaults['deny_cid']) )
397 $acl_data['contacts'][$key]['selected'] = 1;
399 $acl_data['contacts'][$key]['selected'] = 0;
407 function acl_lookup(App $a, $out_type = 'json') {
413 $start = (x($_REQUEST,'start') ? $_REQUEST['start'] : 0);
414 $count = (x($_REQUEST,'count') ? $_REQUEST['count'] : 100);
415 $search = (x($_REQUEST,'search') ? $_REQUEST['search'] : "");
416 $type = (x($_REQUEST,'type') ? $_REQUEST['type'] : "");
417 $mode = (x($_REQUEST,'smode') ? $_REQUEST['smode'] : "");
418 $conv_id = (x($_REQUEST,'conversation') ? $_REQUEST['conversation'] : null);
420 // For use with jquery.textcomplete for private mail completion
422 if(x($_REQUEST,'query') && strlen($_REQUEST['query'])) {
425 $search = $_REQUEST['query'];
428 logger("Searching for ".$search." - type ".$type, LOGGER_DEBUG);
431 $sql_extra = "AND `name` LIKE '%%".dbesc($search)."%%'";
432 $sql_extra2 = "AND (`attag` LIKE '%%".dbesc($search)."%%' OR `name` LIKE '%%".dbesc($search)."%%' OR `nick` LIKE '%%".dbesc($search)."%%')";
434 $sql_extra = $sql_extra2 = "";
437 // count groups and contacts
438 if ($type=='' || $type=='g'){
439 $r = q("SELECT COUNT(*) AS g FROM `group` WHERE `deleted` = 0 AND `uid` = %d $sql_extra",
442 $group_count = (int)$r[0]['g'];
447 $sql_extra2 .= " ".unavailable_networks();
449 // autocomplete for editor mentions
450 if ($type=='' || $type=='c'){
451 $r = q("SELECT COUNT(*) AS c FROM `contact`
452 WHERE `uid` = %d AND NOT `self`
453 AND NOT `blocked` AND NOT `pending` AND NOT `archive`
454 AND `notify` != '' $sql_extra2" ,
457 $contact_count = (int)$r[0]['c'];
459 elseif ($type == 'm') {
461 // autocomplete for Private Messages
463 $r = q("SELECT COUNT(*) AS c FROM `contact`
464 WHERE `uid` = %d AND NOT `self`
465 AND NOT `blocked` AND NOT `pending` AND NOT `archive`
466 AND `network` IN ('%s','%s','%s') $sql_extra2" ,
467 intval(local_user()),
470 dbesc(NETWORK_DIASPORA)
472 $contact_count = (int)$r[0]['c'];
475 elseif ($type == 'a') {
477 // autocomplete for Contacts
479 $r = q("SELECT COUNT(*) AS c FROM `contact`
480 WHERE `uid` = %d AND NOT `self`
481 AND NOT `pending` $sql_extra2" ,
484 $contact_count = (int)$r[0]['c'];
491 $tot = $group_count+$contact_count;
496 if ($type=='' || $type=='g'){
498 /// @todo We should cache this query.
499 // This can be done when we can delete cache entries via wildcard
500 $r = q("SELECT `group`.`id`, `group`.`name`, GROUP_CONCAT(DISTINCT `group_member`.`contact-id` SEPARATOR ',') AS uids
502 INNER JOIN `group_member` ON `group_member`.`gid`=`group`.`id` AND `group_member`.`uid` = `group`.`uid`
503 WHERE NOT `group`.`deleted` AND `group`.`uid` = %d
505 GROUP BY `group`.`name`
506 ORDER BY `group`.`name`
508 intval(local_user()),
514 // logger('acl: group: ' . $g['name'] . ' members: ' . $g['uids']);
517 "photo" => "images/twopeople.png",
518 "name" => htmlentities($g['name']),
519 "id" => intval($g['id']),
520 "uids" => array_map("intval", explode(",",$g['uids'])),
529 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `forum`, `prv` FROM `contact`
530 WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
531 AND NOT (`network` IN ('%s', '%s'))
533 ORDER BY `name` ASC ",
534 intval(local_user()),
535 dbesc(NETWORK_OSTATUS), dbesc(NETWORK_STATUSNET)
540 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `forum`, `prv` FROM `contact`
541 WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
542 AND NOT (`network` IN ('%s'))
544 ORDER BY `name` ASC ",
545 intval(local_user()),
546 dbesc(NETWORK_STATUSNET)
549 elseif($type == 'm') {
550 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag` FROM `contact`
551 WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive`
552 AND `network` IN ('%s','%s','%s')
554 ORDER BY `name` ASC ",
555 intval(local_user()),
558 dbesc(NETWORK_DIASPORA)
560 } elseif ($type == 'a') {
561 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `forum`, `prv` FROM `contact`
562 WHERE `uid` = %d AND `pending` = 0
564 ORDER BY `name` ASC ",
567 } elseif ($type == 'x') {
568 // autocomplete for global contact search (e.g. navbar search)
569 $r = navbar_complete($a);
574 'photo' => proxy_url($g['photo'], false, PROXY_SIZE_MICRO),
575 'name' => $g['name'],
576 'nick' => (x($g['addr']) ? $g['addr'] : $g['url']),
577 'network' => $g['network'],
579 'forum' => (x($g['community']) ? 1 : 0),
586 'items' => $contacts,
588 echo json_encode($o);
595 if (dbm::is_result($r)) {
599 'photo' => proxy_url($g['micro'], false, PROXY_SIZE_MICRO),
600 'name' => htmlentities($g['name']),
601 'id' => intval($g['id']),
602 'network' => $g['network'],
604 'nick' => htmlentities(($g['attag']) ? $g['attag'] : $g['nick']),
605 'forum' => ((x($g['forum']) || x($g['prv'])) ? 1 : 0),
610 $items = array_merge($groups, $contacts);
613 /* if $conv_id is set, get unknow contacts in thread */
614 /* but first get know contacts url to filter them out */
615 function _contact_link($i){ return dbesc($i['link']); }
616 $known_contacts = array_map(_contact_link, $contacts);
617 $unknow_contacts=array();
618 $r = q("SELECT `author-avatar`,`author-name`,`author-link`
619 FROM `item` WHERE `parent` = %d
620 AND (`author-name` LIKE '%%%s%%' OR `author-link` LIKE '%%%s%%')
621 AND `author-link` NOT IN ('%s')
622 GROUP BY `author-link`
623 ORDER BY `author-name` ASC
628 implode("','", $known_contacts)
630 if (dbm::is_result($r)){
631 foreach ($r as $row) {
633 $up = parse_url($row['author-link']);
634 $nick = explode("/",$up['path']);
635 $nick = $nick[count($nick)-1];
636 $nick .= "@".$up['host'];
638 $unknow_contacts[] = array(
640 'photo' => proxy_url($row['author-avatar'], false, PROXY_SIZE_MICRO),
641 'name' => htmlentities($row['author-name']),
643 'network' => 'unknown',
644 'link' => $row['author-link'],
645 'nick' => htmlentities($nick),
651 $items = array_merge($items, $unknow_contacts);
652 $tot += count($unknow_contacts);
660 'contacts' => $contacts,
666 call_hooks('acl_lookup_end', $results);
668 if($out_type === 'html') {
670 'tot' => $results['tot'],
671 'start' => $results['start'],
672 'count' => $results['count'],
673 'groups' => $results['groups'],
674 'contacts' => $results['contacts'],
680 'tot' => $results['tot'],
681 'start' => $results['start'],
682 'count' => $results['count'],
683 'items' => $results['items'],
686 echo json_encode($o);
691 * @brief Searching for global contacts for autocompletion
694 * @return array with the search results
696 function navbar_complete(App $a) {
698 // logger('navbar_complete');
700 if ((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
704 // check if searching in the local global contact table is enabled
705 $localsearch = get_config('system','poco_local_search');
707 $search = $prefix.notags(trim($_REQUEST['search']));
708 $mode = $_REQUEST['smode'];
710 // don't search if search term has less than 2 characters
711 if (! $search || mb_strlen($search) < 2) {
715 if (substr($search,0,1) === '@') {
716 $search = substr($search,1);
720 $x = DirSearch::global_search_by_name($search, $mode);
724 if (! $localsearch) {
725 $p = (($a->pager['page'] != 1) ? '&p=' . $a->pager['page'] : '');
727 $x = z_fetch_url(get_server().'/lsearch?f=' . $p . '&search=' . urlencode($search));
730 $j = json_decode($x['body'],true);
731 if ($j && $j['results']) {
732 return $j['results'];
737 /// @TODO Not needed here?