]> git.mxchange.org Git - friendica.git/blob - include/acl_selectors.php
Add network for unknown contacts in ACL
[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 (dbm::is_result($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                 }
75                 if (x($options,'single')) {
76                         $single = true;
77                 }
78                 if (x($options,'multiple')) {
79                         $single = false;
80                 }
81                 if (x($options,'exclude')) {
82                         $exclude = $options['exclude'];
83                 }
84
85                 if (x($options,'networks')) {
86                         switch($options['networks']) {
87                                 case 'DFRN_ONLY':
88                                         $networks = array(NETWORK_DFRN);
89                                         break;
90                                 case 'PRIVATE':
91                                         if(is_array($a->user) && $a->user['prvnets'])
92                                                 $networks = array(NETWORK_DFRN,NETWORK_MAIL,NETWORK_DIASPORA);
93                                         else
94                                                 $networks = array(NETWORK_DFRN,NETWORK_FACEBOOK,NETWORK_MAIL, NETWORK_DIASPORA);
95                                         break;
96                                 case 'TWO_WAY':
97                                         if(is_array($a->user) && $a->user['prvnets'])
98                                                 $networks = array(NETWORK_DFRN,NETWORK_MAIL,NETWORK_DIASPORA);
99                                         else
100                                                 $networks = array(NETWORK_DFRN,NETWORK_FACEBOOK,NETWORK_MAIL,NETWORK_DIASPORA,NETWORK_OSTATUS);
101                                         break;
102                                 default:
103                                         break;
104                         }
105                 }
106         }
107
108         $x = array('options' => $options, 'size' => $size, 'single' => $single, 'mutual' => $mutual, 'exclude' => $exclude, 'networks' => $networks);
109
110         call_hooks('contact_select_options', $x);
111
112         $o = '';
113
114         $sql_extra = '';
115
116         if($x['mutual']) {
117                 $sql_extra .= sprintf(" AND `rel` = %d ", intval(CONTACT_IS_FRIEND));
118         }
119
120         if(intval($x['exclude']))
121                 $sql_extra .= sprintf(" AND `id` != %d ", intval($x['exclude']));
122
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 ) ";
128         }
129
130         $tabindex = (x($options, 'tabindex') ? "tabindex=\"" . $options["tabindex"] . "\"" : "");
131
132         if($x['single'])
133                 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"" . $x['size'] . "\" $tabindex >\r\n";
134         else
135                 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"" . $x['size'] . "$\" $tabindex >\r\n";
136
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` != ''
139                 $sql_extra
140                 ORDER BY `name` ASC ",
141                 intval(local_user())
142         );
143
144
145         $arr = array('contact' => $r, 'entry' => $o);
146
147         // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
148
149         call_hooks($a->module . '_pre_' . $selname, $arr);
150
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\" ";
155                         } else {
156                                 $selected = '';
157                         }
158
159                         $trimmed = mb_substr($rr['name'],0,20);
160
161                         $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}|{$rr['url']}\" >$trimmed</option>\r\n";
162                 }
163
164         }
165
166         $o .= "</select>\r\n";
167
168         call_hooks($a->module . '_post_' . $selname, $o);
169
170         return $o;
171 }
172
173
174
175 function contact_select($selname, $selclass, $preselected = false, $size = 4, $privmail = false, $celeb = false, $privatenet = false, $tabindex = null) {
176
177         require_once("include/bbcode.php");
178
179         $a = get_app();
180
181         $o = '';
182
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.
185
186         $sql_extra = '';
187
188         if($privmail || $celeb) {
189                 $sql_extra .= sprintf(" AND `rel` = %d ", intval(CONTACT_IS_FRIEND));
190         }
191
192         if($privmail)
193                 $sql_extra .= sprintf(" AND `network` IN ('%s' , '%s') ",
194                                         NETWORK_DFRN, NETWORK_DIASPORA);
195         elseif($privatenet)
196                 $sql_extra .= sprintf(" AND `network` IN ('%s' , '%s', '%s', '%s') ",
197                                         NETWORK_DFRN, NETWORK_MAIL, NETWORK_FACEBOOK, NETWORK_DIASPORA);
198
199         $tabindex = ($tabindex > 0 ? "tabindex=\"$tabindex\"" : "");
200
201         if ($privmail AND $preselected) {
202                 $sql_extra .= " AND `id` IN (".implode(",", $preselected).")";
203                 $hidepreselected = ' style="display: none;"';
204         } else
205                 $hidepreselected = "";
206
207         if($privmail)
208                 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"$size\" $tabindex $hidepreselected>\r\n";
209         else
210                 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"$size\" $tabindex >\r\n";
211
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` != ''
214                 $sql_extra
215                 ORDER BY `name` ASC ",
216                 intval(local_user())
217         );
218
219
220         $arr = array('contact' => $r, 'entry' => $o);
221
222         // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
223
224         call_hooks($a->module . '_pre_' . $selname, $arr);
225
226         $receiverlist = array();
227
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\" ";
232                         }
233                         else {
234                                 $selected = '';
235                         }
236
237                         if ($privmail) {
238                                 $trimmed = GetProfileUsername($rr['url'], $rr['name'], false);
239                         } else {
240                                 $trimmed = mb_substr($rr['name'],0,20);
241                         }
242
243                         $receiverlist[] = $trimmed;
244
245                         $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}|{$rr['url']}\" >$trimmed</option>\r\n";
246                 }
247
248         }
249
250         $o .= "</select>\r\n";
251
252         if ($privmail AND $preselected)
253                 $o .= implode(", ", $receiverlist);
254
255         call_hooks($a->module . '_post_' . $selname, $o);
256
257         return $o;
258 }
259
260
261 function fixacl(&$item) {
262         $item = intval(str_replace(array('<','>'),array('',''),$item));
263 }
264
265 function prune_deadguys($arr) {
266
267         if (! $arr) {
268                 return $arr;
269         }
270
271         $str = dbesc(implode(',',$arr));
272
273         $r = q("SELECT `id` FROM `contact` WHERE `id` IN ( " . $str . ") AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 ");
274
275         if (dbm::is_result($r)) {
276                 $ret = array();
277                 foreach ($r as $rr) {
278                         $ret[] = intval($rr['id']);
279                 }
280                 return $ret;
281         }
282
283         return array();
284 }
285
286
287 function get_acl_permissions($user = null) {
288         $allow_cid = $allow_gid = $deny_cid = $deny_gid = false;
289
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');
303         }
304
305         $allow_cid = prune_deadguys($allow_cid);
306
307         return array(
308                 'allow_cid' => $allow_cid,
309                 'allow_gid' => $allow_gid,
310                 'deny_cid' => $deny_cid,
311                 'deny_gid' => $deny_gid,
312         );
313 }
314
315
316 function populate_acl($user = null, $show_jotnets = false) {
317
318         $perms = get_acl_permissions($user);
319
320         $jotnets = '';
321         if($show_jotnets) {
322                 $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
323
324                 $mail_enabled = false;
325                 $pubmail_enabled = false;
326
327                 if(! $mail_disabled) {
328                         $r = q("SELECT `pubmail` FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1",
329                                 intval(local_user())
330                         );
331                         if (dbm::is_result($r)) {
332                                 $mail_enabled = true;
333                                 if(intval($r[0]['pubmail']))
334                                         $pubmail_enabled = true;
335                         }
336                 }
337
338                 if (!$user['hidewall']) {
339                         if($mail_enabled) {
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>';
342                         }
343
344                         call_hooks('jot_networks', $jotnets);
345                 } else
346                         $jotnets .= sprintf(t('Connectors disabled, since "%s" is enabled.'),
347                                             t('Hide your profile details from unknown viewers?'));
348                 }
349
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")
367                 ),
368         ));
369
370
371         return $o;
372
373 }
374
375 function construct_acl_data(App $a, $user) {
376
377         // Get group and contact information for html ACL selector
378         $acl_data = acl_lookup($a, 'html');
379
380         $user_defaults = get_acl_permissions($user);
381
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;
388                         else
389                                 $acl_data['groups'][$key]['selected'] = 0;
390                 }
391         }
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;
398                         else
399                                 $acl_data['contacts'][$key]['selected'] = 0;
400                 }
401         }
402
403         return $acl_data;
404
405 }
406
407 function acl_lookup(App $a, $out_type = 'json') {
408
409         if (!local_user()) {
410                 return '';
411         }
412
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);
419
420         // For use with jquery.textcomplete for private mail completion
421
422         if(x($_REQUEST,'query') && strlen($_REQUEST['query'])) {
423                 if(! $type)
424                         $type = 'm';
425                 $search = $_REQUEST['query'];
426         }
427
428         logger("Searching for ".$search." - type ".$type, LOGGER_DEBUG);
429
430         if ($search!=""){
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)."%%')";
433         } else {
434                 $sql_extra = $sql_extra2 = "";
435         }
436
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",
440                         intval(local_user())
441                 );
442                 $group_count = (int)$r[0]['g'];
443         } else {
444                 $group_count = 0;
445         }
446
447         $sql_extra2 .= " ".unavailable_networks();
448
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" ,
455                         intval(local_user())
456                 );
457                 $contact_count = (int)$r[0]['c'];
458         }
459         elseif ($type == 'm') {
460
461                 // autocomplete for Private Messages
462
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()),
468                         dbesc(NETWORK_DFRN),
469                         dbesc(NETWORK_ZOT),
470                         dbesc(NETWORK_DIASPORA)
471                 );
472                 $contact_count = (int)$r[0]['c'];
473
474         }
475         elseif ($type == 'a') {
476
477                 // autocomplete for Contacts
478
479                 $r = q("SELECT COUNT(*) AS c FROM `contact`
480                                 WHERE `uid` = %d AND NOT `self`
481                                 AND NOT `pending` $sql_extra2" ,
482                         intval(local_user())
483                 );
484                 $contact_count = (int)$r[0]['c'];
485
486         } else {
487                 $contact_count = 0;
488         }
489
490
491         $tot = $group_count+$contact_count;
492
493         $groups = array();
494         $contacts = array();
495
496         if ($type=='' || $type=='g'){
497
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
501                                 FROM `group`
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
504                                         $sql_extra
505                                 GROUP BY `group`.`name`
506                                 ORDER BY `group`.`name`
507                                 LIMIT %d,%d",
508                         intval(local_user()),
509                         intval($start),
510                         intval($count)
511                 );
512
513                 foreach($r as $g){
514 //              logger('acl: group: ' . $g['name'] . ' members: ' . $g['uids']);
515                         $groups[] = array(
516                                 "type"  => "g",
517                                 "photo" => "images/twopeople.png",
518                                 "name"  => htmlentities($g['name']),
519                                 "id"    => intval($g['id']),
520                                 "uids"  => array_map("intval", explode(",",$g['uids'])),
521                                 "link"  => '',
522                                 "forum" => '0'
523                         );
524                 }
525         }
526
527         if ($type==''){
528
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'))
532                         $sql_extra2
533                         ORDER BY `name` ASC ",
534                         intval(local_user()),
535                         dbesc(NETWORK_OSTATUS), dbesc(NETWORK_STATUSNET)
536                 );
537         }
538         elseif ($type=='c'){
539
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'))
543                         $sql_extra2
544                         ORDER BY `name` ASC ",
545                         intval(local_user()),
546                         dbesc(NETWORK_STATUSNET)
547                 );
548         }
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')
553                         $sql_extra2
554                         ORDER BY `name` ASC ",
555                         intval(local_user()),
556                         dbesc(NETWORK_DFRN),
557                         dbesc(NETWORK_ZOT),
558                         dbesc(NETWORK_DIASPORA)
559                 );
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
563                         $sql_extra2
564                         ORDER BY `name` ASC ",
565                         intval(local_user())
566                 );
567         } elseif ($type == 'x') {
568                 // autocomplete for global contact search (e.g. navbar search)
569                 $r = navbar_complete($a);
570                 $contacts = array();
571                 if ($r) {
572                         foreach ($r as $g) {
573                                 $contacts[] = array(
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'],
578                                         'link'    => $g['url'],
579                                         'forum'   => (x($g['community']) ? 1 : 0),
580                                 );
581                         }
582                 }
583                 $o = array(
584                         'start' => $start,
585                         'count' => $count,
586                         'items' => $contacts,
587                 );
588                 echo json_encode($o);
589                 killme();
590         } else {
591                 $r = array();
592         }
593
594
595         if (dbm::is_result($r)) {
596                 foreach ($r as $g){
597                         $contacts[] = array(
598                                 'type'    => 'c',
599                                 'photo'   => proxy_url($g['micro'], false, PROXY_SIZE_MICRO),
600                                 'name'    => htmlentities($g['name']),
601                                 'id'      => intval($g['id']),
602                                 'network' => $g['network'],
603                                 'link'    => $g['url'],
604                                 'nick'    => htmlentities(($g['attag']) ? $g['attag'] : $g['nick']),
605                                 'forum'   => ((x($g['forum']) || x($g['prv'])) ? 1 : 0),
606                         );
607                 }
608         }
609
610         $items = array_merge($groups, $contacts);
611
612         if ($conv_id) {
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`, `network`
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
624                                 ",
625                                 intval($conv_id),
626                                 dbesc($search),
627                                 dbesc($search),
628                                 implode("','", $known_contacts)
629                 );
630                 if (dbm::is_result($r)) {
631                         foreach ($r as $row) {
632                                 $up = parse_url($row['author-link']);
633                                 $nick = explode('/', $up['path']);
634                                 // Fix for Mastodon URLs with format https://domain.tld/@nick
635                                 $nick = ltrim($nick[count($nick) - 1], '@');
636                                 $nick .= '@' . $up['host'];
637
638                                 $unknow_contacts[] = array(
639                                         'type'    => 'c',
640                                         'photo'   => proxy_url($row['author-avatar'], false, PROXY_SIZE_MICRO),
641                                         'name'    => htmlentities($row['author-name']),
642                                         'id'      => '',
643                                         'network' => $row['network'],
644                                         'link'    => $row['author-link'],
645                                         'nick'    => htmlentities($nick),
646                                         'forum'   => false
647                                 );
648                         }
649                 }
650
651                 $items = array_merge($items, $unknow_contacts);
652                 $tot += count($unknow_contacts);
653         }
654
655         $results = array(
656                 'tot'      => $tot,
657                 'start'    => $start,
658                 'count'    => $count,
659                 'groups'   => $groups,
660                 'contacts' => $contacts,
661                 'items'    => $items,
662                 'type'     => $type,
663                 'search'   => $search,
664         );
665
666         call_hooks('acl_lookup_end', $results);
667
668         if($out_type === 'html') {
669                 $o = array(
670                         'tot'      => $results['tot'],
671                         'start'    => $results['start'],
672                         'count'    => $results['count'],
673                         'groups'   => $results['groups'],
674                         'contacts' => $results['contacts'],
675                 );
676                 return $o;
677         }
678
679         $o = array(
680                 'tot'   => $results['tot'],
681                 'start' => $results['start'],
682                 'count' => $results['count'],
683                 'items' => $results['items'],
684         );
685
686         echo json_encode($o);
687
688         killme();
689 }
690 /**
691  * @brief Searching for global contacts for autocompletion
692  *
693  * @param App $a
694  * @return array with the search results
695  */
696 function navbar_complete(App $a) {
697
698 //      logger('navbar_complete');
699
700         if ((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
701                 return;
702         }
703
704         // check if searching in the local global contact table is enabled
705         $localsearch = get_config('system','poco_local_search');
706
707         $search = $prefix.notags(trim($_REQUEST['search']));
708         $mode = $_REQUEST['smode'];
709
710         // don't search if search term has less than 2 characters
711         if (! $search || mb_strlen($search) < 2) {
712                 return array();
713         }
714
715         if (substr($search,0,1) === '@') {
716                 $search = substr($search,1);
717         }
718
719         if ($localsearch) {
720                 $x = DirSearch::global_search_by_name($search, $mode);
721                 return $x;
722         }
723
724         if (! $localsearch) {
725                 $p = (($a->pager['page'] != 1) ? '&p=' . $a->pager['page'] : '');
726
727                 $x = z_fetch_url(get_server().'/lsearch?f=' . $p .  '&search=' . urlencode($search));
728                 if ($x['success']) {
729                         $t = 0;
730                         $j = json_decode($x['body'],true);
731                         if ($j && $j['results']) {
732                                 return $j['results'];
733                         }
734                 }
735         }
736
737         /// @TODO Not needed here?
738         return;
739 }