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