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