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