]> git.mxchange.org Git - friendica.git/blob - include/acl_selectors.php
Some small Diaspora changes
[friendica.git] / include / acl_selectors.php
1 <?php
2
3 /**
4  * @file include/acl_selectors.php
5  */
6
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");
12
13
14 /**
15  * @package acl_selectors
16  */
17 function group_select($selname,$selclass,$preselected = false,$size = 4) {
18
19         $a = get_app();
20
21         $o = '';
22
23         $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"$size\" >\r\n";
24
25         $r = q("SELECT `id`, `name` FROM `group` WHERE NOT `deleted` AND `uid` = %d ORDER BY `name` ASC",
26                 intval(local_user())
27         );
28
29
30         $arr = array('group' => $r, 'entry' => $o);
31
32         // e.g. 'network_pre_group_deny', 'profile_pre_group_allow'
33
34         call_hooks($a->module . '_pre_' . $selname, $arr);
35
36         if(count($r)) {
37                 foreach($r as $rr) {
38                         if((is_array($preselected)) && in_array($rr['id'], $preselected))
39                                 $selected = " selected=\"selected\" ";
40                         else
41                                 $selected = '';
42
43                         $trimmed = mb_substr($rr['name'],0,12);
44
45                         $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}\" >$trimmed</option>\r\n";
46                 }
47
48         }
49         $o .= "</select>\r\n";
50
51         call_hooks($a->module . '_post_' . $selname, $o);
52
53
54         return $o;
55 }
56
57
58 function contact_selector($selname, $selclass, $preselected = false, $options) {
59
60         $a = get_app();
61
62         $mutual = false;
63         $networks = null;
64         $single = false;
65         $exclude = false;
66         $size = 4;
67
68         if(is_array($options)) {
69                 if(x($options,'size'))
70                         $size = $options['size'];
71
72                 if(x($options,'mutual_friends'))
73                         $mutual = true;
74                 if(x($options,'single'))
75                         $single = true;
76                 if(x($options,'multiple'))
77                         $single = false;
78                 if(x($options,'exclude'))
79                         $exclude = $options['exclude'];
80
81                 if(x($options,'networks')) {
82                         switch($options['networks']) {
83                                 case 'DFRN_ONLY':
84                                         $networks = array(NETWORK_DFRN);
85                                         break;
86                                 case 'PRIVATE':
87                                         if(is_array($a->user) && $a->user['prvnets'])
88                                                 $networks = array(NETWORK_DFRN,NETWORK_MAIL,NETWORK_DIASPORA);
89                                         else
90                                                 $networks = array(NETWORK_DFRN,NETWORK_FACEBOOK,NETWORK_MAIL, NETWORK_DIASPORA);
91                                         break;
92                                 case 'TWO_WAY':
93                                         if(is_array($a->user) && $a->user['prvnets'])
94                                                 $networks = array(NETWORK_DFRN,NETWORK_MAIL,NETWORK_DIASPORA);
95                                         else
96                                                 $networks = array(NETWORK_DFRN,NETWORK_FACEBOOK,NETWORK_MAIL,NETWORK_DIASPORA,NETWORK_OSTATUS);
97                                         break;
98                                 default:
99                                         break;
100                         }
101                 }
102         }
103
104         $x = array('options' => $options, 'size' => $size, 'single' => $single, 'mutual' => $mutual, 'exclude' => $exclude, 'networks' => $networks);
105
106         call_hooks('contact_select_options', $x);
107
108         $o = '';
109
110         $sql_extra = '';
111
112         if($x['mutual']) {
113                 $sql_extra .= sprintf(" AND `rel` = %d ", intval(CONTACT_IS_FRIEND));
114         }
115
116         if(intval($x['exclude']))
117                 $sql_extra .= sprintf(" AND `id` != %d ", intval($x['exclude']));
118
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 ) ";
124         }
125
126         $tabindex = (x($options, 'tabindex') ? "tabindex=\"" . $options["tabindex"] . "\"" : "");
127
128         if($x['single'])
129                 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"" . $x['size'] . "\" $tabindex >\r\n";
130         else
131                 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"" . $x['size'] . "$\" $tabindex >\r\n";
132
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` != ''
135                 $sql_extra
136                 ORDER BY `name` ASC ",
137                 intval(local_user())
138         );
139
140
141         $arr = array('contact' => $r, 'entry' => $o);
142
143         // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
144
145         call_hooks($a->module . '_pre_' . $selname, $arr);
146
147         if(count($r)) {
148                 foreach($r as $rr) {
149                         if((is_array($preselected)) && in_array($rr['id'], $preselected))
150                                 $selected = " selected=\"selected\" ";
151                         else
152                                 $selected = '';
153
154                         $trimmed = mb_substr($rr['name'],0,20);
155
156                         $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}|{$rr['url']}\" >$trimmed</option>\r\n";
157                 }
158
159         }
160
161         $o .= "</select>\r\n";
162
163         call_hooks($a->module . '_post_' . $selname, $o);
164
165         return $o;
166 }
167
168
169
170 function contact_select($selname, $selclass, $preselected = false, $size = 4, $privmail = false, $celeb = false, $privatenet = false, $tabindex = null) {
171
172         require_once("include/bbcode.php");
173
174         $a = get_app();
175
176         $o = '';
177
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.
180
181         $sql_extra = '';
182
183         if($privmail || $celeb) {
184                 $sql_extra .= sprintf(" AND `rel` = %d ", intval(CONTACT_IS_FRIEND));
185         }
186
187         if($privmail)
188                 $sql_extra .= sprintf(" AND `network` IN ('%s' , '%s') ",
189                                         NETWORK_DFRN, NETWORK_DIASPORA);
190         elseif($privatenet)
191                 $sql_extra .= sprintf(" AND `network` IN ('%s' , '%s', '%s', '%s') ",
192                                         NETWORK_DFRN, NETWORK_MAIL, NETWORK_FACEBOOK, NETWORK_DIASPORA);
193
194         $tabindex = ($tabindex > 0 ? "tabindex=\"$tabindex\"" : "");
195
196         if ($privmail AND $preselected) {
197                 $sql_extra .= " AND `id` IN (".implode(",", $preselected).")";
198                 $hidepreselected = ' style="display: none;"';
199         } else
200                 $hidepreselected = "";
201
202         if($privmail)
203                 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"$size\" $tabindex $hidepreselected>\r\n";
204         else
205                 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"$size\" $tabindex >\r\n";
206
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` != ''
209                 $sql_extra
210                 ORDER BY `name` ASC ",
211                 intval(local_user())
212         );
213
214
215         $arr = array('contact' => $r, 'entry' => $o);
216
217         // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
218
219         call_hooks($a->module . '_pre_' . $selname, $arr);
220
221         $receiverlist = array();
222
223         if(count($r)) {
224                 foreach($r as $rr) {
225                         if((is_array($preselected)) && in_array($rr['id'], $preselected))
226                                 $selected = " selected=\"selected\" ";
227                         else
228                                 $selected = '';
229
230                         if($privmail)
231                                 $trimmed = GetProfileUsername($rr['url'], $rr['name'], false);
232                         else
233                                 $trimmed = mb_substr($rr['name'],0,20);
234
235                         $receiverlist[] = $trimmed;
236
237                         $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}|{$rr['url']}\" >$trimmed</option>\r\n";
238                 }
239
240         }
241
242         $o .= "</select>\r\n";
243
244         if ($privmail AND $preselected)
245                 $o .= implode(", ", $receiverlist);
246
247         call_hooks($a->module . '_post_' . $selname, $o);
248
249         return $o;
250 }
251
252
253 function fixacl(&$item) {
254         $item = intval(str_replace(array('<','>'),array('',''),$item));
255 }
256
257 function prune_deadguys($arr) {
258
259         if(! $arr)
260                 return $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 ");
263         if($r) {
264                 $ret = array();
265                 foreach($r as $rr)
266                         $ret[] = intval($rr['id']);
267                 return $ret;
268         }
269         return array();
270 }
271
272
273 function get_acl_permissions($user = null) {
274         $allow_cid = $allow_gid = $deny_cid = $deny_gid = false;
275
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');
289         }
290
291         $allow_cid = prune_deadguys($allow_cid);
292
293         return array(
294                 'allow_cid' => $allow_cid,
295                 'allow_gid' => $allow_gid,
296                 'deny_cid' => $deny_cid,
297                 'deny_gid' => $deny_gid,
298         );
299 }
300
301
302 function populate_acl($user = null, $show_jotnets = false) {
303
304         $perms = get_acl_permissions($user);
305
306         $jotnets = '';
307         if($show_jotnets) {
308                 $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
309
310                 $mail_enabled = false;
311                 $pubmail_enabled = false;
312
313                 if(! $mail_disabled) {
314                         $r = q("SELECT `pubmail` FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1",
315                                 intval(local_user())
316                         );
317                         if(count($r)) {
318                                 $mail_enabled = true;
319                                 if(intval($r[0]['pubmail']))
320                                         $pubmail_enabled = true;
321                         }
322                 }
323
324                 if (!$user['hidewall']) {
325                         if($mail_enabled) {
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>';
328                         }
329
330                         call_hooks('jot_networks', $jotnets);
331                 } else
332                         $jotnets .= sprintf(t('Connectors disabled, since "%s" is enabled.'),
333                                             t('Hide your profile details from unknown viewers?'));
334                 }
335
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")
353                 ),
354         ));
355
356
357         return $o;
358
359 }
360
361 function construct_acl_data(&$a, $user) {
362
363         // Get group and contact information for html ACL selector
364         $acl_data = acl_lookup($a, 'html');
365
366         $user_defaults = get_acl_permissions($user);
367
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;
374                         else
375                                 $acl_data['groups'][$key]['selected'] = 0;
376                 }
377         }
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;
384                         else
385                                 $acl_data['contacts'][$key]['selected'] = 0;
386                 }
387         }
388
389         return $acl_data;
390
391 }
392
393 function acl_lookup(&$a, $out_type = 'json') {
394
395         if(!local_user())
396                 return "";
397
398         $start  =       (x($_REQUEST,'start')           ? $_REQUEST['start']            : 0);
399         $count  =       (x($_REQUEST,'count')           ? $_REQUEST['count']            : 100);
400         $search  =      (x($_REQUEST,'search')          ? $_REQUEST['search']           : "");
401         $type   =       (x($_REQUEST,'type')            ? $_REQUEST['type']             : "");
402         $mode   =       (x($_REQUEST,'mode')            ? $_REQUEST['mode']             : "");
403         $conv_id =      (x($_REQUEST,'conversation')    ? $_REQUEST['conversation']     : null);
404
405         // For use with jquery.textcomplete for private mail completion
406
407         if(x($_REQUEST,'query') && strlen($_REQUEST['query'])) {
408                 if(! $type)
409                         $type = 'm';
410                 $search = $_REQUEST['query'];
411         }
412
413         logger("Searching for ".$search." - type ".$type, LOGGER_DEBUG);
414
415         if ($search!=""){
416                 $sql_extra = "AND `name` LIKE '%%".dbesc($search)."%%'";
417                 $sql_extra2 = "AND (`attag` LIKE '%%".dbesc($search)."%%' OR `name` LIKE '%%".dbesc($search)."%%' OR `nick` LIKE '%%".dbesc($search)."%%')";
418         } else {
419                 $sql_extra = $sql_extra2 = "";
420         }
421
422         // count groups and contacts
423         if ($type=='' || $type=='g'){
424                 $r = q("SELECT COUNT(*) AS g FROM `group` WHERE `deleted` = 0 AND `uid` = %d $sql_extra",
425                         intval(local_user())
426                 );
427                 $group_count = (int)$r[0]['g'];
428         } else {
429                 $group_count = 0;
430         }
431
432         $sql_extra2 .= " ".unavailable_networks();
433
434         // autocomplete for editor mentions
435         if ($type=='' || $type=='c'){
436                 $r = q("SELECT COUNT(*) AS c FROM `contact`
437                                 WHERE `uid` = %d AND `self` = 0
438                                 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0
439                                 AND `notify` != '' $sql_extra2" ,
440                         intval(local_user())
441                 );
442                 $contact_count = (int)$r[0]['c'];
443         }
444         elseif ($type == 'm') {
445
446                 // autocomplete for Private Messages
447
448                 $r = q("SELECT COUNT(*) AS c FROM `contact`
449                                 WHERE `uid` = %d AND `self` = 0
450                                 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0
451                                 AND `network` IN ('%s','%s','%s') $sql_extra2" ,
452                         intval(local_user()),
453                         dbesc(NETWORK_DFRN),
454                         dbesc(NETWORK_ZOT),
455                         dbesc(NETWORK_DIASPORA)
456                 );
457                 $contact_count = (int)$r[0]['c'];
458
459         }
460         elseif ($type == 'a') {
461
462                 // autocomplete for Contacts
463
464                 $r = q("SELECT COUNT(*) AS c FROM `contact`
465                                 WHERE `uid` = %d AND `self` = 0
466                                 AND `pending` = 0 $sql_extra2" ,
467                         intval(local_user())
468                 );
469                 $contact_count = (int)$r[0]['c'];
470
471         } else {
472                 $contact_count = 0;
473         }
474
475
476         $tot = $group_count+$contact_count;
477
478         $groups = array();
479         $contacts = array();
480
481         if ($type=='' || $type=='g'){
482
483                 $r = q("SELECT `group`.`id`, `group`.`name`, GROUP_CONCAT(DISTINCT `group_member`.`contact-id` SEPARATOR ',') AS uids
484                                 FROM `group`,`group_member`
485                                 WHERE `group`.`deleted` = 0 AND `group`.`uid` = %d
486                                         AND `group_member`.`gid`=`group`.`id`
487                                         $sql_extra
488                                 GROUP BY `group`.`id`
489                                 ORDER BY `group`.`name`
490                                 LIMIT %d,%d",
491                         intval(local_user()),
492                         intval($start),
493                         intval($count)
494                 );
495
496                 foreach($r as $g){
497 //              logger('acl: group: ' . $g['name'] . ' members: ' . $g['uids']);
498                         $groups[] = array(
499                                 "type"  => "g",
500                                 "photo" => "images/twopeople.png",
501                                 "name"  => htmlentities($g['name']),
502                                 "id"    => intval($g['id']),
503                                 "uids"  => array_map("intval", explode(",",$g['uids'])),
504                                 "link"  => '',
505                                 "forum" => '0'
506                         );
507                 }
508         }
509
510         if ($type==''){
511
512                 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `forum`, `prv` FROM `contact`
513                         WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 AND `notify` != ''
514                         AND NOT (`network` IN ('%s', '%s'))
515                         $sql_extra2
516                         ORDER BY `name` ASC ",
517                         intval(local_user()),
518                         dbesc(NETWORK_OSTATUS), dbesc(NETWORK_STATUSNET)
519                 );
520         }
521         elseif ($type=='c'){
522
523                 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `forum`, `prv` FROM `contact`
524                         WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 AND `notify` != ''
525                         AND NOT (`network` IN ('%s'))
526                         $sql_extra2
527                         ORDER BY `name` ASC ",
528                         intval(local_user()),
529                         dbesc(NETWORK_STATUSNET)
530                 );
531         }
532         elseif($type == 'm') {
533                 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag` FROM `contact`
534                         WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0
535                         AND `network` IN ('%s','%s','%s')
536                         $sql_extra2
537                         ORDER BY `name` ASC ",
538                         intval(local_user()),
539                         dbesc(NETWORK_DFRN),
540                         dbesc(NETWORK_ZOT),
541                         dbesc(NETWORK_DIASPORA)
542                 );
543         }
544         elseif($type == 'a') {
545                 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `forum`, `prv` FROM `contact`
546                         WHERE `uid` = %d AND `pending` = 0
547                         $sql_extra2
548                         ORDER BY `name` ASC ",
549                         intval(local_user())
550                 );
551         }
552         elseif($type == 'x') {
553                 // autocomplete for global contact search (e.g. navbar search)
554                 $r = navbar_complete($a);
555                 $contacts = array();
556                 if($r) {
557                         foreach($r as $g) {
558                                 $contacts[] = array(
559                                         "photo"    => proxy_url($g['photo'], false, PROXY_SIZE_MICRO),
560                                         "name"     => $g['name'],
561                                         "nick"     => (x($g['addr']) ? $g['addr'] : $g['url']),
562                                         "network" => $g['network'],
563                                         "link" => $g['url'],
564                                         "forum"    => (x($g['community']) ? 1 : 0),
565                                 );
566                         }
567                 }
568                 $o = array(
569                         'start' => $start,
570                         'count' => $count,
571                         'items' => $contacts,
572                 );
573                 echo json_encode($o);
574                 killme();
575         }
576         else
577                 $r = array();
578
579
580         if(count($r)) {
581                 foreach($r as $g){
582                         $contacts[] = array(
583                                 "type"  => "c",
584                                 "photo" => proxy_url($g['micro'], false, PROXY_SIZE_MICRO),
585                                 "name"  => htmlentities($g['name']),
586                                 "id"    => intval($g['id']),
587                                 "network" => $g['network'],
588                                 "link" => $g['url'],
589                                 "nick" => htmlentities(($g['attag']) ? $g['attag'] : $g['nick']),
590                                 "forum" => ((x($g['forum']) || x($g['prv'])) ? 1 : 0),
591                         );
592                 }
593         }
594
595         $items = array_merge($groups, $contacts);
596
597         if ($conv_id) {
598                 /* if $conv_id is set, get unknow contacts in thread */
599                 /* but first get know contacts url to filter them out */
600                 function _contact_link($i){ return dbesc($i['link']); }
601                 $known_contacts = array_map(_contact_link, $contacts);
602                 $unknow_contacts=array();
603                 $r = q("SELECT `author-avatar`,`author-name`,`author-link`
604                                 FROM `item` WHERE `parent` = %d
605                                         AND (`author-name` LIKE '%%%s%%' OR `author-link` LIKE '%%%s%%')
606                                         AND `author-link` NOT IN ('%s')
607                                 GROUP BY `author-link`
608                                 ORDER BY `author-name` ASC
609                                 ",
610                                 intval($conv_id),
611                                 dbesc($search),
612                                 dbesc($search),
613                                 implode("','", $known_contacts)
614                 );
615                 if (is_array($r) && count($r)){
616                         foreach($r as $row) {
617                                 // nickname..
618                                 $up = parse_url($row['author-link']);
619                                 $nick = explode("/",$up['path']);
620                                 $nick = $nick[count($nick)-1];
621                                 $nick .= "@".$up['host'];
622                                 // /nickname
623                                 $unknow_contacts[] = array(
624                                         "type"  => "c",
625                                         "photo" => proxy_url($row['author-avatar'], false, PROXY_SIZE_MICRO),
626                                         "name"  => htmlentities($row['author-name']),
627                                         "id"    => '',
628                                         "network" => "unknown",
629                                         "link" => $row['author-link'],
630                                         "nick" => htmlentities($nick),
631                                         "forum" => false
632                                 );
633                         }
634                 }
635
636                 $items = array_merge($items, $unknow_contacts);
637                 $tot += count($unknow_contacts);
638         }
639
640         if($out_type === 'html') {
641                 $o = array(
642                         'tot'           => $tot,
643                         'start' => $start,
644                         'count' => $count,
645                         'groups'        => $groups,
646                         'contacts'      => $contacts,
647                 );
648                 return $o;
649         }
650
651         $o = array(
652                 'tot'   => $tot,
653                 'start' => $start,
654                 'count' => $count,
655                 'items' => $items,
656         );
657
658         echo json_encode($o);
659
660         killme();
661 }
662 /**
663  * @brief Searching for global contacts for autocompletion
664  * 
665  * @param App $a
666  * @return array with the search results
667  */
668 function navbar_complete(&$a) {
669
670 //      logger('navbar_complete');
671
672         if((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
673                 return;
674         }
675
676         // check if searching in the local global contact table is enabled
677         $localsearch = get_config('system','poco_local_search');
678
679         $search = $prefix.notags(trim($_REQUEST['search']));
680         $mode = $_REQUEST['mode'];
681
682         // don't search if search term has less than 2 characters
683         if(! $search || mb_strlen($search) < 2)
684                 return array();
685
686         if(substr($search,0,1) === '@')
687                 $search = substr($search,1);
688
689         if($localsearch) {
690                 $x = DirSearch::global_search_by_name($search, $mode);
691                 return $x;
692         }
693
694         if(! $localsearch) {
695                 $p = (($a->pager['page'] != 1) ? '&p=' . $a->pager['page'] : '');
696
697                 $x = z_fetch_url(get_server().'/lsearch?f=' . $p .  '&search=' . urlencode($search));
698                 if($x['success']) {
699                         $t = 0;
700                         $j = json_decode($x['body'],true);
701                         if($j && $j['results']) {
702                                 return $j['results'];
703                         }
704                 }
705         }
706         return;
707 }