]> git.mxchange.org Git - friendica.git/blob - include/acl_selectors.php
DBClean now has a smaller limit/Query improvements
[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(&$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(&$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                 $r = q("SELECT `group`.`id`, `group`.`name`, GROUP_CONCAT(DISTINCT `group_member`.`contact-id` SEPARATOR ',') AS uids
499                                 FROM `group`
500                                 INNER JOIN `group_member` ON `group_member`.`gid`=`group`.`id` AND `group_member`.`uid` = `group`.`uid`
501                                 WHERE NOT `group`.`deleted` AND `group`.`uid` = %d
502                                         $sql_extra
503                                 GROUP BY `group`.`name`
504                                 ORDER BY `group`.`name`
505                                 LIMIT %d,%d",
506                         intval(local_user()),
507                         intval($start),
508                         intval($count)
509                 );
510
511                 foreach($r as $g){
512 //              logger('acl: group: ' . $g['name'] . ' members: ' . $g['uids']);
513                         $groups[] = array(
514                                 "type"  => "g",
515                                 "photo" => "images/twopeople.png",
516                                 "name"  => htmlentities($g['name']),
517                                 "id"    => intval($g['id']),
518                                 "uids"  => array_map("intval", explode(",",$g['uids'])),
519                                 "link"  => '',
520                                 "forum" => '0'
521                         );
522                 }
523         }
524
525         if ($type==''){
526
527                 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `forum`, `prv` FROM `contact`
528                         WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
529                         AND NOT (`network` IN ('%s', '%s'))
530                         $sql_extra2
531                         ORDER BY `name` ASC ",
532                         intval(local_user()),
533                         dbesc(NETWORK_OSTATUS), dbesc(NETWORK_STATUSNET)
534                 );
535         }
536         elseif ($type=='c'){
537
538                 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `forum`, `prv` FROM `contact`
539                         WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
540                         AND NOT (`network` IN ('%s'))
541                         $sql_extra2
542                         ORDER BY `name` ASC ",
543                         intval(local_user()),
544                         dbesc(NETWORK_STATUSNET)
545                 );
546         }
547         elseif($type == 'm') {
548                 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag` FROM `contact`
549                         WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive`
550                         AND `network` IN ('%s','%s','%s')
551                         $sql_extra2
552                         ORDER BY `name` ASC ",
553                         intval(local_user()),
554                         dbesc(NETWORK_DFRN),
555                         dbesc(NETWORK_ZOT),
556                         dbesc(NETWORK_DIASPORA)
557                 );
558         } elseif ($type == 'a') {
559                 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `forum`, `prv` FROM `contact`
560                         WHERE `uid` = %d AND `pending` = 0
561                         $sql_extra2
562                         ORDER BY `name` ASC ",
563                         intval(local_user())
564                 );
565         } elseif ($type == 'x') {
566                 // autocomplete for global contact search (e.g. navbar search)
567                 $r = navbar_complete($a);
568                 $contacts = array();
569                 if ($r) {
570                         foreach ($r as $g) {
571                                 $contacts[] = array(
572                                         'photo'   => proxy_url($g['photo'], false, PROXY_SIZE_MICRO),
573                                         'name'    => $g['name'],
574                                         'nick'    => (x($g['addr']) ? $g['addr'] : $g['url']),
575                                         'network' => $g['network'],
576                                         'link'    => $g['url'],
577                                         'forum'   => (x($g['community']) ? 1 : 0),
578                                 );
579                         }
580                 }
581                 $o = array(
582                         'start' => $start,
583                         'count' => $count,
584                         'items' => $contacts,
585                 );
586                 echo json_encode($o);
587                 killme();
588         } else {
589                 $r = array();
590         }
591
592
593         if (dbm::is_result($r)) {
594                 foreach ($r as $g){
595                         $contacts[] = array(
596                                 'type'    => 'c',
597                                 'photo'   => proxy_url($g['micro'], false, PROXY_SIZE_MICRO),
598                                 'name'    => htmlentities($g['name']),
599                                 'id'      => intval($g['id']),
600                                 'network' => $g['network'],
601                                 'link'    => $g['url'],
602                                 'nick'    => htmlentities(($g['attag']) ? $g['attag'] : $g['nick']),
603                                 'forum'   => ((x($g['forum']) || x($g['prv'])) ? 1 : 0),
604                         );
605                 }
606         }
607
608         $items = array_merge($groups, $contacts);
609
610         if ($conv_id) {
611                 /* if $conv_id is set, get unknow contacts in thread */
612                 /* but first get know contacts url to filter them out */
613                 function _contact_link($i){ return dbesc($i['link']); }
614                 $known_contacts = array_map(_contact_link, $contacts);
615                 $unknow_contacts=array();
616                 $r = q("SELECT `author-avatar`,`author-name`,`author-link`
617                                 FROM `item` WHERE `parent` = %d
618                                         AND (`author-name` LIKE '%%%s%%' OR `author-link` LIKE '%%%s%%')
619                                         AND `author-link` NOT IN ('%s')
620                                 GROUP BY `author-link`
621                                 ORDER BY `author-name` ASC
622                                 ",
623                                 intval($conv_id),
624                                 dbesc($search),
625                                 dbesc($search),
626                                 implode("','", $known_contacts)
627                 );
628                 if (dbm::is_result($r)){
629                         foreach ($r as $row) {
630                                 // nickname..
631                                 $up = parse_url($row['author-link']);
632                                 $nick = explode("/",$up['path']);
633                                 $nick = $nick[count($nick)-1];
634                                 $nick .= "@".$up['host'];
635                                 // /nickname
636                                 $unknow_contacts[] = array(
637                                         'type'    => 'c',
638                                         'photo'   => proxy_url($row['author-avatar'], false, PROXY_SIZE_MICRO),
639                                         'name'    => htmlentities($row['author-name']),
640                                         'id'      => '',
641                                         'network' => 'unknown',
642                                         'link'    => $row['author-link'],
643                                         'nick'    => htmlentities($nick),
644                                         'forum'   => false
645                                 );
646                         }
647                 }
648
649                 $items = array_merge($items, $unknow_contacts);
650                 $tot += count($unknow_contacts);
651         }
652
653         $results = array(
654                 'tot'      => $tot,
655                 'start'    => $start,
656                 'count'    => $count,
657                 'groups'   => $groups,
658                 'contacts' => $contacts,
659                 'items'    => $items,
660                 'type'     => $type,
661                 'search'   => $search,
662         );
663
664         call_hooks('acl_lookup_end', $results);
665
666         if($out_type === 'html') {
667                 $o = array(
668                         'tot'      => $results['tot'],
669                         'start'    => $results['start'],
670                         'count'    => $results['count'],
671                         'groups'   => $results['groups'],
672                         'contacts' => $results['contacts'],
673                 );
674                 return $o;
675         }
676
677         $o = array(
678                 'tot'   => $results['tot'],
679                 'start' => $results['start'],
680                 'count' => $results['count'],
681                 'items' => $results['items'],
682         );
683
684         echo json_encode($o);
685
686         killme();
687 }
688 /**
689  * @brief Searching for global contacts for autocompletion
690  * 
691  * @param App $a
692  * @return array with the search results
693  */
694 function navbar_complete(App &$a) {
695
696 //      logger('navbar_complete');
697
698         if ((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
699                 return;
700         }
701
702         // check if searching in the local global contact table is enabled
703         $localsearch = get_config('system','poco_local_search');
704
705         $search = $prefix.notags(trim($_REQUEST['search']));
706         $mode = $_REQUEST['smode'];
707
708         // don't search if search term has less than 2 characters
709         if (! $search || mb_strlen($search) < 2) {
710                 return array();
711         }
712
713         if (substr($search,0,1) === '@') {
714                 $search = substr($search,1);
715         }
716
717         if ($localsearch) {
718                 $x = DirSearch::global_search_by_name($search, $mode);
719                 return $x;
720         }
721
722         if (! $localsearch) {
723                 $p = (($a->pager['page'] != 1) ? '&p=' . $a->pager['page'] : '');
724
725                 $x = z_fetch_url(get_server().'/lsearch?f=' . $p .  '&search=' . urlencode($search));
726                 if ($x['success']) {
727                         $t = 0;
728                         $j = json_decode($x['body'],true);
729                         if ($j && $j['results']) {
730                                 return $j['results'];
731                         }
732                 }
733         }
734
735         /// @TODO Not needed here?
736         return;
737 }