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