]> git.mxchange.org Git - friendica.git/blob - include/acl_selectors.php
Merge remote-tracking branch 'upstream/develop' into rewrites/coding-convention
[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                 }
127                 $str_nets = implode(',',$x['networks']);
128                 $sql_extra .= " AND `network` IN ( $str_nets ) ";
129         }
130
131         $tabindex = (x($options, 'tabindex') ? "tabindex=\"" . $options["tabindex"] . "\"" : "");
132
133         if ($x['single'])
134                 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"" . $x['size'] . "\" $tabindex >\r\n";
135         else
136                 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"" . $x['size'] . "$\" $tabindex >\r\n";
137
138         $r = q("SELECT `id`, `name`, `url`, `network` FROM `contact`
139                 WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
140                 $sql_extra
141                 ORDER BY `name` ASC ",
142                 intval(local_user())
143         );
144
145
146         $arr = array('contact' => $r, 'entry' => $o);
147
148         // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
149
150         call_hooks($a->module . '_pre_' . $selname, $arr);
151
152         if (dbm::is_result($r)) {
153                 foreach ($r as $rr) {
154                         if ((is_array($preselected)) && in_array($rr['id'], $preselected)) {
155                                 $selected = " selected=\"selected\" ";
156                         } else {
157                                 $selected = '';
158                         }
159
160                         $trimmed = mb_substr($rr['name'],0,20);
161
162                         $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}|{$rr['url']}\" >$trimmed</option>\r\n";
163                 }
164
165         }
166
167         $o .= "</select>\r\n";
168
169         call_hooks($a->module . '_post_' . $selname, $o);
170
171         return $o;
172 }
173
174
175
176 function contact_select($selname, $selclass, $preselected = false, $size = 4, $privmail = false, $celeb = false, $privatenet = false, $tabindex = null) {
177
178         require_once("include/bbcode.php");
179
180         $a = get_app();
181
182         $o = '';
183
184         // When used for private messages, we limit correspondence to mutual DFRN/Friendica friends and the selector
185         // to one recipient. By default our selector allows multiple selects amongst all contacts.
186
187         $sql_extra = '';
188
189         if ($privmail || $celeb) {
190                 $sql_extra .= sprintf(" AND `rel` = %d ", intval(CONTACT_IS_FRIEND));
191         }
192
193         if ($privmail)
194                 $sql_extra .= sprintf(" AND `network` IN ('%s' , '%s') ",
195                                         NETWORK_DFRN, NETWORK_DIASPORA);
196         elseif ($privatenet)
197                 $sql_extra .= sprintf(" AND `network` IN ('%s' , '%s', '%s', '%s') ",
198                                         NETWORK_DFRN, NETWORK_MAIL, NETWORK_FACEBOOK, NETWORK_DIASPORA);
199
200         $tabindex = ($tabindex > 0 ? "tabindex=\"$tabindex\"" : "");
201
202         if ($privmail AND $preselected) {
203                 $sql_extra .= " AND `id` IN (".implode(",", $preselected).")";
204                 $hidepreselected = ' style="display: none;"';
205         } else
206                 $hidepreselected = "";
207
208         if ($privmail)
209                 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"$size\" $tabindex $hidepreselected>\r\n";
210         else
211                 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"$size\" $tabindex >\r\n";
212
213         $r = q("SELECT `id`, `name`, `url`, `network` FROM `contact`
214                 WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
215                 $sql_extra
216                 ORDER BY `name` ASC ",
217                 intval(local_user())
218         );
219
220
221         $arr = array('contact' => $r, 'entry' => $o);
222
223         // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
224
225         call_hooks($a->module . '_pre_' . $selname, $arr);
226
227         $receiverlist = array();
228
229         if (dbm::is_result($r)) {
230                 foreach ($r as $rr) {
231                         if ((is_array($preselected)) && in_array($rr['id'], $preselected)) {
232                                 $selected = " selected=\"selected\" ";
233                         }
234                         else {
235                                 $selected = '';
236                         }
237
238                         if ($privmail) {
239                                 $trimmed = GetProfileUsername($rr['url'], $rr['name'], false);
240                         } else {
241                                 $trimmed = mb_substr($rr['name'],0,20);
242                         }
243
244                         $receiverlist[] = $trimmed;
245
246                         $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}|{$rr['url']}\" >$trimmed</option>\r\n";
247                 }
248
249         }
250
251         $o .= "</select>\r\n";
252
253         if ($privmail AND $preselected)
254                 $o .= implode(", ", $receiverlist);
255
256         call_hooks($a->module . '_post_' . $selname, $o);
257
258         return $o;
259 }
260
261
262 function fixacl(&$item) {
263         $item = intval(str_replace(array('<','>'),array('',''),$item));
264 }
265
266 function prune_deadguys($arr) {
267
268         if (! $arr) {
269                 return $arr;
270         }
271
272         $str = dbesc(implode(',',$arr));
273
274         $r = q("SELECT `id` FROM `contact` WHERE `id` IN ( " . $str . ") AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 ");
275
276         if (dbm::is_result($r)) {
277                 $ret = array();
278                 foreach ($r as $rr) {
279                         $ret[] = intval($rr['id']);
280                 }
281                 return $ret;
282         }
283
284         return array();
285 }
286
287
288 function get_acl_permissions($user = null) {
289         $allow_cid = $allow_gid = $deny_cid = $deny_gid = false;
290
291         if (is_array($user)) {
292                 $allow_cid = ((strlen($user['allow_cid']))
293                         ? explode('><', $user['allow_cid']) : array() );
294                 $allow_gid = ((strlen($user['allow_gid']))
295                         ? explode('><', $user['allow_gid']) : array() );
296                 $deny_cid  = ((strlen($user['deny_cid']))
297                         ? explode('><', $user['deny_cid']) : array() );
298                 $deny_gid  = ((strlen($user['deny_gid']))
299                         ? explode('><', $user['deny_gid']) : array() );
300                 array_walk($allow_cid,'fixacl');
301                 array_walk($allow_gid,'fixacl');
302                 array_walk($deny_cid,'fixacl');
303                 array_walk($deny_gid,'fixacl');
304         }
305
306         $allow_cid = prune_deadguys($allow_cid);
307
308         return array(
309                 'allow_cid' => $allow_cid,
310                 'allow_gid' => $allow_gid,
311                 'deny_cid' => $deny_cid,
312                 'deny_gid' => $deny_gid,
313         );
314 }
315
316
317 function populate_acl($user = null, $show_jotnets = false) {
318
319         $perms = get_acl_permissions($user);
320
321         $jotnets = '';
322         if ($show_jotnets) {
323                 $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
324
325                 $mail_enabled = false;
326                 $pubmail_enabled = false;
327
328                 if (! $mail_disabled) {
329                         $r = q("SELECT `pubmail` FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1",
330                                 intval(local_user())
331                         );
332                         if (dbm::is_result($r)) {
333                                 $mail_enabled = true;
334                                 if (intval($r[0]['pubmail']))
335                                         $pubmail_enabled = true;
336                         }
337                 }
338
339                 if (!$user['hidewall']) {
340                         if ($mail_enabled) {
341                                 $selected = (($pubmail_enabled) ? ' checked="checked" ' : '');
342                                 $jotnets .= '<div class="profile-jot-net"><input type="checkbox" name="pubmail_enable"' . $selected . ' value="1" /> ' . t("Post to Email") . '</div>';
343                         }
344
345                         call_hooks('jot_networks', $jotnets);
346                 } else
347                         $jotnets .= sprintf(t('Connectors disabled, since "%s" is enabled.'),
348                                             t('Hide your profile details from unknown viewers?'));
349                 }
350
351         $tpl = get_markup_template("acl_selector.tpl");
352         $o = replace_macros($tpl, array(
353                 '$showall'=> t("Visible to everybody"),
354                 '$show' => t("show"),
355                 '$hide'  => t("don't show"),
356                 '$allowcid' => json_encode($perms['allow_cid']),
357                 '$allowgid' => json_encode($perms['allow_gid']),
358                 '$denycid' => json_encode($perms['deny_cid']),
359                 '$denygid' => json_encode($perms['deny_gid']),
360                 '$networks' => $show_jotnets,
361                 '$emailcc' => t('CC: email addresses'),
362                 '$emtitle' => t('Example: bob@example.com, mary@example.com'),
363                 '$jotnets' => $jotnets,
364                 '$aclModalTitle' => t('Permissions'),
365                 '$aclModalDismiss' => t('Close'),
366                 '$features' => array(
367                 "aclautomention"=>(feature_enabled($user['uid'],"aclautomention")?"true":"false")
368                 ),
369         ));
370
371
372         return $o;
373
374 }
375
376 function construct_acl_data(App $a, $user) {
377
378         // Get group and contact information for html ACL selector
379         $acl_data = acl_lookup($a, 'html');
380
381         $user_defaults = get_acl_permissions($user);
382
383         if ($acl_data['groups']) {
384                 foreach ($acl_data['groups'] as $key=>$group) {
385                         // Add a "selected" flag to groups that are posted to by default
386                         if ($user_defaults['allow_gid'] &&
387                            in_array($group['id'], $user_defaults['allow_gid']) && !in_array($group['id'], $user_defaults['deny_gid']) )
388                                 $acl_data['groups'][$key]['selected'] = 1;
389                         else
390                                 $acl_data['groups'][$key]['selected'] = 0;
391                 }
392         }
393         if ($acl_data['contacts']) {
394                 foreach ($acl_data['contacts'] as $key=>$contact) {
395                         // Add a "selected" flag to groups that are posted to by default
396                         if ($user_defaults['allow_cid'] &&
397                            in_array($contact['id'], $user_defaults['allow_cid']) && !in_array($contact['id'], $user_defaults['deny_cid']) )
398                                 $acl_data['contacts'][$key]['selected'] = 1;
399                         else
400                                 $acl_data['contacts'][$key]['selected'] = 0;
401                 }
402         }
403
404         return $acl_data;
405
406 }
407
408 function acl_lookup(App $a, $out_type = 'json') {
409
410         if (!local_user()) {
411                 return '';
412         }
413
414         $start  =       (x($_REQUEST,'start')           ? $_REQUEST['start']            : 0);
415         $count  =       (x($_REQUEST,'count')           ? $_REQUEST['count']            : 100);
416         $search  =      (x($_REQUEST,'search')          ? $_REQUEST['search']           : "");
417         $type   =       (x($_REQUEST,'type')            ? $_REQUEST['type']             : "");
418         $mode   =       (x($_REQUEST,'smode')           ? $_REQUEST['smode']            : "");
419         $conv_id =      (x($_REQUEST,'conversation')    ? $_REQUEST['conversation']     : null);
420
421         // For use with jquery.textcomplete for private mail completion
422
423         if (x($_REQUEST,'query') && strlen($_REQUEST['query'])) {
424                 if (! $type)
425                         $type = 'm';
426                 $search = $_REQUEST['query'];
427         }
428
429         logger("Searching for ".$search." - type ".$type, LOGGER_DEBUG);
430
431         if ($search!=""){
432                 $sql_extra = "AND `name` LIKE '%%".dbesc($search)."%%'";
433                 $sql_extra2 = "AND (`attag` LIKE '%%".dbesc($search)."%%' OR `name` LIKE '%%".dbesc($search)."%%' OR `nick` LIKE '%%".dbesc($search)."%%')";
434         } else {
435                 $sql_extra = $sql_extra2 = "";
436         }
437
438         // count groups and contacts
439         if ($type=='' || $type=='g'){
440                 $r = q("SELECT COUNT(*) AS g FROM `group` WHERE `deleted` = 0 AND `uid` = %d $sql_extra",
441                         intval(local_user())
442                 );
443                 $group_count = (int)$r[0]['g'];
444         } else {
445                 $group_count = 0;
446         }
447
448         $sql_extra2 .= " ".unavailable_networks();
449
450         // autocomplete for editor mentions
451         if ($type=='' || $type=='c'){
452                 $r = q("SELECT COUNT(*) AS c FROM `contact`
453                                 WHERE `uid` = %d AND NOT `self`
454                                 AND NOT `blocked` AND NOT `pending` AND NOT `archive`
455                                 AND `notify` != '' $sql_extra2" ,
456                         intval(local_user())
457                 );
458                 $contact_count = (int)$r[0]['c'];
459         }
460         elseif ($type == 'm') {
461
462                 // autocomplete for Private Messages
463
464                 $r = q("SELECT COUNT(*) AS c FROM `contact`
465                                 WHERE `uid` = %d AND NOT `self`
466                                 AND NOT `blocked` AND NOT `pending` AND NOT `archive`
467                                 AND `network` IN ('%s','%s','%s') $sql_extra2" ,
468                         intval(local_user()),
469                         dbesc(NETWORK_DFRN),
470                         dbesc(NETWORK_ZOT),
471                         dbesc(NETWORK_DIASPORA)
472                 );
473                 $contact_count = (int)$r[0]['c'];
474
475         }
476         elseif ($type == 'a') {
477
478                 // autocomplete for Contacts
479
480                 $r = q("SELECT COUNT(*) AS c FROM `contact`
481                                 WHERE `uid` = %d AND NOT `self`
482                                 AND NOT `pending` $sql_extra2" ,
483                         intval(local_user())
484                 );
485                 $contact_count = (int)$r[0]['c'];
486
487         } else {
488                 $contact_count = 0;
489         }
490
491
492         $tot = $group_count+$contact_count;
493
494         $groups = array();
495         $contacts = array();
496
497         if ($type=='' || $type=='g'){
498
499                 /// @todo We should cache this query.
500                 // This can be done when we can delete cache entries via wildcard
501                 $r = q("SELECT `group`.`id`, `group`.`name`, GROUP_CONCAT(DISTINCT `group_member`.`contact-id` SEPARATOR ',') AS uids
502                                 FROM `group`
503                                 INNER JOIN `group_member` ON `group_member`.`gid`=`group`.`id` AND `group_member`.`uid` = `group`.`uid`
504                                 WHERE NOT `group`.`deleted` AND `group`.`uid` = %d
505                                         $sql_extra
506                                 GROUP BY `group`.`name`
507                                 ORDER BY `group`.`name`
508                                 LIMIT %d,%d",
509                         intval(local_user()),
510                         intval($start),
511                         intval($count)
512                 );
513
514                 foreach ($r as $g){
515 //              logger('acl: group: ' . $g['name'] . ' members: ' . $g['uids']);
516                         $groups[] = array(
517                                 "type"  => "g",
518                                 "photo" => "images/twopeople.png",
519                                 "name"  => htmlentities($g['name']),
520                                 "id"    => intval($g['id']),
521                                 "uids"  => array_map("intval", explode(",",$g['uids'])),
522                                 "link"  => '',
523                                 "forum" => '0'
524                         );
525                 }
526         }
527
528         if ($type==''){
529
530                 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `forum`, `prv` FROM `contact`
531                         WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
532                         AND NOT (`network` IN ('%s', '%s'))
533                         $sql_extra2
534                         ORDER BY `name` ASC ",
535                         intval(local_user()),
536                         dbesc(NETWORK_OSTATUS), dbesc(NETWORK_STATUSNET)
537                 );
538         }
539         elseif ($type=='c'){
540
541                 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `forum`, `prv` FROM `contact`
542                         WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
543                         AND NOT (`network` IN ('%s'))
544                         $sql_extra2
545                         ORDER BY `name` ASC ",
546                         intval(local_user()),
547                         dbesc(NETWORK_STATUSNET)
548                 );
549         }
550         elseif ($type == 'm') {
551                 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag` FROM `contact`
552                         WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive`
553                         AND `network` IN ('%s','%s','%s')
554                         $sql_extra2
555                         ORDER BY `name` ASC ",
556                         intval(local_user()),
557                         dbesc(NETWORK_DFRN),
558                         dbesc(NETWORK_ZOT),
559                         dbesc(NETWORK_DIASPORA)
560                 );
561         } elseif ($type == 'a') {
562                 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `forum`, `prv` FROM `contact`
563                         WHERE `uid` = %d AND `pending` = 0
564                         $sql_extra2
565                         ORDER BY `name` ASC ",
566                         intval(local_user())
567                 );
568         } elseif ($type == 'x') {
569                 // autocomplete for global contact search (e.g. navbar search)
570                 $r = navbar_complete($a);
571                 $contacts = array();
572                 if ($r) {
573                         foreach ($r as $g) {
574                                 $contacts[] = array(
575                                         'photo'   => proxy_url($g['photo'], false, PROXY_SIZE_MICRO),
576                                         'name'    => $g['name'],
577                                         'nick'    => (x($g['addr']) ? $g['addr'] : $g['url']),
578                                         'network' => $g['network'],
579                                         'link'    => $g['url'],
580                                         'forum'   => (x($g['community']) ? 1 : 0),
581                                 );
582                         }
583                 }
584                 $o = array(
585                         'start' => $start,
586                         'count' => $count,
587                         'items' => $contacts,
588                 );
589                 echo json_encode($o);
590                 killme();
591         } else {
592                 $r = array();
593         }
594
595
596         if (dbm::is_result($r)) {
597                 foreach ($r as $g){
598                         $contacts[] = array(
599                                 'type'    => 'c',
600                                 'photo'   => proxy_url($g['micro'], false, PROXY_SIZE_MICRO),
601                                 'name'    => htmlentities($g['name']),
602                                 'id'      => intval($g['id']),
603                                 'network' => $g['network'],
604                                 'link'    => $g['url'],
605                                 'nick'    => htmlentities(($g['attag']) ? $g['attag'] : $g['nick']),
606                                 'forum'   => ((x($g['forum']) || x($g['prv'])) ? 1 : 0),
607                         );
608                 }
609         }
610
611         $items = array_merge($groups, $contacts);
612
613         if ($conv_id) {
614                 /* if $conv_id is set, get unknow contacts in thread */
615                 /* but first get know contacts url to filter them out */
616                 function _contact_link($i){ return dbesc($i['link']); }
617                 $known_contacts = array_map(_contact_link, $contacts);
618                 $unknow_contacts=array();
619                 $r = q("SELECT `author-avatar`,`author-name`,`author-link`
620                                 FROM `item` WHERE `parent` = %d
621                                         AND (`author-name` LIKE '%%%s%%' OR `author-link` LIKE '%%%s%%')
622                                         AND `author-link` NOT IN ('%s')
623                                 GROUP BY `author-link`
624                                 ORDER BY `author-name` ASC
625                                 ",
626                                 intval($conv_id),
627                                 dbesc($search),
628                                 dbesc($search),
629                                 implode("','", $known_contacts)
630                 );
631                 if (dbm::is_result($r)){
632                         foreach ($r as $row) {
633                                 // nickname..
634                                 $up = parse_url($row['author-link']);
635                                 $nick = explode("/",$up['path']);
636                                 $nick = $nick[count($nick)-1];
637                                 $nick .= "@".$up['host'];
638                                 // /nickname
639                                 $unknow_contacts[] = array(
640                                         'type'    => 'c',
641                                         'photo'   => proxy_url($row['author-avatar'], false, PROXY_SIZE_MICRO),
642                                         'name'    => htmlentities($row['author-name']),
643                                         'id'      => '',
644                                         'network' => 'unknown',
645                                         'link'    => $row['author-link'],
646                                         'nick'    => htmlentities($nick),
647                                         'forum'   => false
648                                 );
649                         }
650                 }
651
652                 $items = array_merge($items, $unknow_contacts);
653                 $tot += count($unknow_contacts);
654         }
655
656         $results = array(
657                 'tot'      => $tot,
658                 'start'    => $start,
659                 'count'    => $count,
660                 'groups'   => $groups,
661                 'contacts' => $contacts,
662                 'items'    => $items,
663                 'type'     => $type,
664                 'search'   => $search,
665         );
666
667         call_hooks('acl_lookup_end', $results);
668
669         if ($out_type === 'html') {
670                 $o = array(
671                         'tot'      => $results['tot'],
672                         'start'    => $results['start'],
673                         'count'    => $results['count'],
674                         'groups'   => $results['groups'],
675                         'contacts' => $results['contacts'],
676                 );
677                 return $o;
678         }
679
680         $o = array(
681                 'tot'   => $results['tot'],
682                 'start' => $results['start'],
683                 'count' => $results['count'],
684                 'items' => $results['items'],
685         );
686
687         echo json_encode($o);
688
689         killme();
690 }
691 /**
692  * @brief Searching for global contacts for autocompletion
693  *
694  * @param App $a
695  * @return array with the search results
696  */
697 function navbar_complete(App $a) {
698
699 //      logger('navbar_complete');
700
701         if ((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
702                 return;
703         }
704
705         // check if searching in the local global contact table is enabled
706         $localsearch = get_config('system','poco_local_search');
707
708         $search = $prefix.notags(trim($_REQUEST['search']));
709         $mode = $_REQUEST['smode'];
710
711         // don't search if search term has less than 2 characters
712         if (! $search || mb_strlen($search) < 2) {
713                 return array();
714         }
715
716         if (substr($search,0,1) === '@') {
717                 $search = substr($search,1);
718         }
719
720         if ($localsearch) {
721                 $x = DirSearch::global_search_by_name($search, $mode);
722                 return $x;
723         }
724
725         if (! $localsearch) {
726                 $p = (($a->pager['page'] != 1) ? '&p=' . $a->pager['page'] : '');
727
728                 $x = z_fetch_url(get_server().'/lsearch?f=' . $p .  '&search=' . urlencode($search));
729                 if ($x['success']) {
730                         $t = 0;
731                         $j = json_decode($x['body'],true);
732                         if ($j && $j['results']) {
733                                 return $j['results'];
734                         }
735                 }
736         }
737
738         /// @TODO Not needed here?
739         return;
740 }