]> git.mxchange.org Git - friendica.git/blob - include/acl_selectors.php
ca463d9433f5868004ea543ad4d669ca3df5f07c
[friendica.git] / include / acl_selectors.php
1 <?php
2
3 /**
4  * @file include/acl_selectors.php
5  */
6
7 use Friendica\App;
8
9 require_once "include/contact_selectors.php";
10 require_once "include/contact_widgets.php";
11 require_once "include/DirSearch.php";
12 require_once "include/features.php";
13 require_once "mod/proxy.php";
14
15
16 /**
17  * @package acl_selectors
18  */
19 function group_select($selname,$selclass,$preselected = false,$size = 4) {
20
21         $a = get_app();
22
23         $o = '';
24
25         $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"$size\" >\r\n";
26
27         $r = q("SELECT `id`, `name` FROM `group` WHERE NOT `deleted` AND `uid` = %d ORDER BY `name` ASC",
28                 intval(local_user())
29         );
30
31
32         $arr = array('group' => $r, 'entry' => $o);
33
34         // e.g. 'network_pre_group_deny', 'profile_pre_group_allow'
35
36         call_hooks($a->module . '_pre_' . $selname, $arr);
37
38         if (dbm::is_result($r)) {
39                 foreach ($r as $rr) {
40                         if ((is_array($preselected)) && in_array($rr['id'], $preselected)) {
41                                 $selected = " selected=\"selected\" ";
42                         } else {
43                                 $selected = '';
44                         }
45
46                         $trimmed = mb_substr($rr['name'],0,12);
47
48                         $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}\" >$trimmed</option>\r\n";
49                 }
50
51         }
52         $o .= "</select>\r\n";
53
54         call_hooks($a->module . '_post_' . $selname, $o);
55
56
57         return $o;
58 }
59
60 /// @TODO after an optional parameter, no mandadory parameter can follow
61 /// @TODO find proper type-hints
62 function contact_selector($selname, $selclass, $preselected = false, $options) {
63
64         $a = get_app();
65
66         $mutual = false;
67         $networks = null;
68         $single = false;
69         $exclude = false;
70         $size = 4;
71
72         if (is_array($options)) {
73                 if (x($options, 'size'))
74                         $size = $options['size'];
75
76                 if (x($options, 'mutual_friends')) {
77                         $mutual = true;
78                 }
79                 if (x($options, 'single')) {
80                         $single = true;
81                 }
82                 if (x($options, 'multiple')) {
83                         $single = false;
84                 }
85                 if (x($options, 'exclude')) {
86                         $exclude = $options['exclude'];
87                 }
88
89                 if (x($options, 'networks')) {
90                         switch ($options['networks']) {
91                                 case 'DFRN_ONLY':
92                                         $networks = array(NETWORK_DFRN);
93                                         break;
94                                 case 'PRIVATE':
95                                         if (is_array($a->user) && $a->user['prvnets']) {
96                                                 $networks = array(NETWORK_DFRN, NETWORK_MAIL, NETWORK_DIASPORA);
97                                         } else {
98                                                 $networks = array(NETWORK_DFRN, NETWORK_FACEBOOK, NETWORK_MAIL, NETWORK_DIASPORA);
99                                         }
100                                         break;
101                                 case 'TWO_WAY':
102                                         if (is_array($a->user) && $a->user['prvnets']) {
103                                                 $networks = array(NETWORK_DFRN, NETWORK_MAIL, NETWORK_DIASPORA);
104                                         } else {
105                                                 $networks = array(NETWORK_DFRN, NETWORK_FACEBOOK, NETWORK_MAIL, NETWORK_DIASPORA, NETWORK_OSTATUS);
106                                         }
107                                         break;
108                                 default: /// @TODO Maybe log this call?
109                                         break;
110                         }
111                 }
112         }
113
114         $x = array('options' => $options, 'size' => $size, 'single' => $single, 'mutual' => $mutual, 'exclude' => $exclude, 'networks' => $networks);
115
116         call_hooks('contact_select_options', $x);
117
118         $o = '';
119
120         $sql_extra = '';
121
122         if (x($x, 'mutual')) {
123                 $sql_extra .= sprintf(" AND `rel` = %d ", intval(CONTACT_IS_FRIEND));
124         }
125
126         if (x($x, 'exclude')) {
127                 $sql_extra .= sprintf(" AND `id` != %d ", intval($x['exclude']));
128         }
129
130         if (is_array($x['networks']) && count($x['networks'])) {
131                 /// @TODO rewrite to foreach()
132                 for ($y = 0; $y < count($x['networks']) ; $y ++) {
133                         $x['networks'][$y] = "'" . dbesc($x['networks'][$y]) . "'";
134                 }
135                 $str_nets = implode(',', $x['networks']);
136                 $sql_extra .= " AND `network` IN ( $str_nets ) ";
137         }
138
139         $tabindex = (x($options, 'tabindex') ? "tabindex=\"" . $options["tabindex"] . "\"" : "");
140
141         if ($x['single']) {
142                 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"" . $x['size'] . "\" $tabindex >\r\n";
143         } else {
144                 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"" . $x['size'] . "$\" $tabindex >\r\n";
145         }
146
147         $r = q("SELECT `id`, `name`, `url`, `network` FROM `contact`
148                 WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
149                 $sql_extra
150                 ORDER BY `name` ASC ",
151                 intval(local_user())
152         );
153
154
155         $arr = array('contact' => $r, 'entry' => $o);
156
157         // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
158
159         call_hooks($a->module . '_pre_' . $selname, $arr);
160
161         if (dbm::is_result($r)) {
162                 foreach ($r as $rr) {
163                         if ((is_array($preselected)) && in_array($rr['id'], $preselected)) {
164                                 $selected = " selected=\"selected\" ";
165                         } else {
166                                 $selected = '';
167                         }
168
169                         $trimmed = mb_substr($rr['name'],0,20);
170
171                         $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}|{$rr['url']}\" >$trimmed</option>\r\n";
172                 }
173
174         }
175
176         $o .= "</select>\r\n";
177
178         call_hooks($a->module . '_post_' . $selname, $o);
179
180         return $o;
181 }
182
183
184
185 function contact_select($selname, $selclass, $preselected = false, $size = 4, $privmail = false, $celeb = false, $privatenet = false, $tabindex = null) {
186
187         require_once "include/bbcode.php";
188
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 = array('contact' => $r, 'entry' => $o);
234
235         // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
236
237         call_hooks($a->module . '_pre_' . $selname, $arr);
238
239         $receiverlist = array();
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 = GetProfileUsername($rr['url'], $rr['name'], false);
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         call_hooks($a->module . '_post_' . $selname, $o);
269
270         return $o;
271 }
272
273
274 function fixacl(&$item) {
275         $item = intval(str_replace(array('<', '>'), array('', ''), $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 = array();
290                 foreach ($r as $rr) {
291                         $ret[] = intval($rr['id']);
292                 }
293                 return $ret;
294         }
295
296         return array();
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']) : array() );
306                 $allow_gid = ((strlen($user['allow_gid']))
307                         ? explode('><', $user['allow_gid']) : array() );
308                 $deny_cid  = ((strlen($user['deny_cid']))
309                         ? explode('><', $user['deny_cid']) : array() );
310                 $deny_gid  = ((strlen($user['deny_gid']))
311                         ? explode('><', $user['deny_gid']) : array() );
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 array(
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') && (! get_config('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" /> ' . t("Post to Email") . '</div>';
356                         }
357
358                         call_hooks('jot_networks', $jotnets);
359                 } else {
360                         $jotnets .= sprintf(t('Connectors disabled, since "%s" is enabled.'),
361                                             t('Hide your profile details from unknown viewers?'));
362                 }
363         }
364
365         $tpl = get_markup_template("acl_selector.tpl");
366         $o = replace_macros($tpl, array(
367                 '$showall'=> t("Visible to everybody"),
368                 '$show' => t("show"),
369                 '$hide'  => t("don't show"),
370                 '$allowcid' => json_encode($perms['allow_cid']),
371                 '$allowgid' => json_encode($perms['allow_gid']),
372                 '$denycid' => json_encode($perms['deny_cid']),
373                 '$denygid' => json_encode($perms['deny_gid']),
374                 '$networks' => $show_jotnets,
375                 '$emailcc' => t('CC: email addresses'),
376                 '$emtitle' => t('Example: bob@example.com, mary@example.com'),
377                 '$jotnets' => $jotnets,
378                 '$aclModalTitle' => t('Permissions'),
379                 '$aclModalDismiss' => t('Close'),
380                 '$features' => array(
381                 'aclautomention' => (feature_enabled($user['uid'], "aclautomention") ? "true" : "false")
382                 ),
383         ));
384
385
386         return $o;
387
388 }
389
390 function construct_acl_data(App $a, $user) {
391         // This function is now deactivated. It seems as if the generated data isn't used anywhere.
392         /// @todo Remove this function and all function calls before releasing Friendica 3.5.3
393         return;
394
395         // Get group and contact information for html ACL selector
396         $acl_data = acl_lookup($a, 'html');
397
398         $user_defaults = get_acl_permissions($user);
399
400         if ($acl_data['groups']) {
401                 foreach ($acl_data['groups'] as $key => $group) {
402                         // Add a "selected" flag to groups that are posted to by default
403                         if ($user_defaults['allow_gid'] &&
404                                         in_array($group['id'], $user_defaults['allow_gid']) && !in_array($group['id'], $user_defaults['deny_gid']) ) {
405                                 $acl_data['groups'][$key]['selected'] = 1;
406                         } else {
407                                 $acl_data['groups'][$key]['selected'] = 0;
408                         }
409                 }
410         }
411         if ($acl_data['contacts']) {
412                 foreach ($acl_data['contacts'] as $key => $contact) {
413                         // Add a "selected" flag to groups that are posted to by default
414                         if ($user_defaults['allow_cid'] &&
415                                         in_array($contact['id'], $user_defaults['allow_cid']) && !in_array($contact['id'], $user_defaults['deny_cid']) ) {
416                                 $acl_data['contacts'][$key]['selected'] = 1;
417                         } else {
418                                 $acl_data['contacts'][$key]['selected'] = 0;
419                         }
420                 }
421         }
422
423         return $acl_data;
424
425 }
426
427 function acl_lookup(App $a, $out_type = 'json') {
428
429         if (!local_user()) {
430                 return '';
431         }
432
433         $start  =       (x($_REQUEST,'start')           ? $_REQUEST['start']            : 0);
434         $count  =       (x($_REQUEST,'count')           ? $_REQUEST['count']            : 100);
435         $search  =      (x($_REQUEST,'search')          ? $_REQUEST['search']           : "");
436         $type   =       (x($_REQUEST,'type')            ? $_REQUEST['type']             : "");
437         $mode   =       (x($_REQUEST,'smode')           ? $_REQUEST['smode']            : "");
438         $conv_id =      (x($_REQUEST,'conversation')    ? $_REQUEST['conversation']     : null);
439
440         // For use with jquery.textcomplete for private mail completion
441
442         if (x($_REQUEST, 'query') && strlen($_REQUEST['query'])) {
443                 if (! $type) {
444                         $type = 'm';
445                 }
446                 $search = $_REQUEST['query'];
447         }
448
449         logger("Searching for ".$search." - type ".$type, LOGGER_DEBUG);
450
451         if ($search != "") {
452                 $sql_extra = "AND `name` LIKE '%%".dbesc($search)."%%'";
453                 $sql_extra2 = "AND (`attag` LIKE '%%".dbesc($search)."%%' OR `name` LIKE '%%".dbesc($search)."%%' OR `nick` LIKE '%%".dbesc($search)."%%')";
454         } else {
455                 /// @TODO Avoid these needless else blocks by putting variable-initialization atop of if()
456                 $sql_extra = $sql_extra2 = "";
457         }
458
459         // count groups and contacts
460         if ($type == '' || $type == 'g') {
461                 $r = q("SELECT COUNT(*) AS g FROM `group` WHERE `deleted` = 0 AND `uid` = %d $sql_extra",
462                         intval(local_user())
463                 );
464                 $group_count = (int)$r[0]['g'];
465         } else {
466                 $group_count = 0;
467         }
468
469         $sql_extra2 .= " ".unavailable_networks();
470
471         if ($type == '' || $type == 'c') {
472                 // autocomplete for editor mentions
473                 $r = q("SELECT COUNT(*) AS c FROM `contact`
474                                 WHERE `uid` = %d AND NOT `self`
475                                 AND NOT `blocked` AND NOT `pending` AND NOT `archive`
476                                 AND `success_update` >= `failure_update`
477                                 AND `notify` != '' $sql_extra2" ,
478                         intval(local_user())
479                 );
480                 $contact_count = (int)$r[0]['c'];
481         }
482         elseif ($type == 'm') {
483
484                 // autocomplete for Private Messages
485
486                 $r = q("SELECT COUNT(*) AS c FROM `contact`
487                                 WHERE `uid` = %d AND NOT `self`
488                                 AND NOT `blocked` AND NOT `pending` AND NOT `archive`
489                                 AND `success_update` >= `failure_update`
490                                 AND `network` IN ('%s','%s','%s') $sql_extra2" ,
491                         intval(local_user()),
492                         dbesc(NETWORK_DFRN),
493                         dbesc(NETWORK_ZOT),
494                         dbesc(NETWORK_DIASPORA)
495                 );
496                 $contact_count = (int)$r[0]['c'];
497
498         }
499         elseif ($type == 'a') {
500
501                 // autocomplete for Contacts
502
503                 $r = q("SELECT COUNT(*) AS c FROM `contact`
504                                 WHERE `uid` = %d AND NOT `self`
505                                 AND NOT `pending` $sql_extra2" ,
506                         intval(local_user())
507                 );
508                 $contact_count = (int)$r[0]['c'];
509
510         } else {
511                 $contact_count = 0;
512         }
513
514
515         $tot = $group_count+$contact_count;
516
517         $groups = array();
518         $contacts = array();
519
520         if ($type == '' || $type == 'g') {
521
522                 /// @todo We should cache this query.
523                 // This can be done when we can delete cache entries via wildcard
524                 $r = q("SELECT `group`.`id`, `group`.`name`, GROUP_CONCAT(DISTINCT `group_member`.`contact-id` SEPARATOR ',') AS uids
525                                 FROM `group`
526                                 INNER JOIN `group_member` ON `group_member`.`gid`=`group`.`id` AND `group_member`.`uid` = `group`.`uid`
527                                 WHERE NOT `group`.`deleted` AND `group`.`uid` = %d
528                                         $sql_extra
529                                 GROUP BY `group`.`name`, `group`.`id`
530                                 ORDER BY `group`.`name`
531                                 LIMIT %d,%d",
532                         intval(local_user()),
533                         intval($start),
534                         intval($count)
535                 );
536
537                 foreach ($r as $g) {
538 //              logger('acl: group: ' . $g['name'] . ' members: ' . $g['uids']);
539                         $groups[] = array(
540                                 "type"  => "g",
541                                 "photo" => "images/twopeople.png",
542                                 "name"  => htmlentities($g['name']),
543                                 "id"    => intval($g['id']),
544                                 "uids"  => array_map("intval", explode(",",$g['uids'])),
545                                 "link"  => '',
546                                 "forum" => '0'
547                         );
548                 }
549                 if (count($groups) > 0) {
550                         $groups[] = array("separator" => true);
551                 }
552         }
553
554         if ($type == '') {
555
556                 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `addr`, `forum`, `prv`, (`prv` OR `forum`) AS `frm` FROM `contact`
557                         WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
558                         AND `success_update` >= `failure_update` AND NOT (`network` IN ('%s', '%s'))
559                         $sql_extra2
560                         ORDER BY `frm` DESC, `name` ASC ",
561                         intval(local_user()),
562                         dbesc(NETWORK_OSTATUS), dbesc(NETWORK_STATUSNET)
563                 );
564         } elseif ($type == 'c') {
565                 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `addr`, `forum`, `prv` FROM `contact`
566                         WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
567                         AND `success_update` >= `failure_update` AND NOT (`network` IN ('%s'))
568                         $sql_extra2
569                         ORDER BY `name` ASC ",
570                         intval(local_user()),
571                         dbesc(NETWORK_STATUSNET)
572                 );
573         }
574         elseif ($type == 'm') {
575                 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `addr` FROM `contact`
576                         WHERE `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive`
577                         AND `success_update` >= `failure_update` AND `network` IN ('%s','%s','%s')
578                         $sql_extra2
579                         ORDER BY `name` ASC ",
580                         intval(local_user()),
581                         dbesc(NETWORK_DFRN),
582                         dbesc(NETWORK_ZOT),
583                         dbesc(NETWORK_DIASPORA)
584                 );
585         } elseif ($type == 'a') {
586                 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `addr`, `forum`, `prv` FROM `contact`
587                         WHERE `uid` = %d AND `pending` = 0 AND `success_update` >= `failure_update`
588                         $sql_extra2
589                         ORDER BY `name` ASC ",
590                         intval(local_user())
591                 );
592         } elseif ($type == 'x') {
593                 // autocomplete for global contact search (e.g. navbar search)
594                 $r = navbar_complete($a);
595                 $contacts = array();
596                 if ($r) {
597                         foreach ($r as $g) {
598                                 $contacts[] = array(
599                                         'photo'   => proxy_url($g['photo'], false, PROXY_SIZE_MICRO),
600                                         'name'    => $g['name'],
601                                         'nick'    => (x($g['addr']) ? $g['addr'] : $g['url']),
602                                         'network' => $g['network'],
603                                         'link'    => $g['url'],
604                                         'forum'   => (x($g['community']) ? 1 : 0),
605                                 );
606                         }
607                 }
608                 $o = array(
609                         'start' => $start,
610                         'count' => $count,
611                         'items' => $contacts,
612                 );
613                 echo json_encode($o);
614                 killme();
615         } else {
616                 $r = array();
617         }
618
619
620         if (dbm::is_result($r)) {
621                 $forums = array();
622                 foreach ($r as $g) {
623                         $entry = array(
624                                 'type'    => 'c',
625                                 'photo'   => proxy_url($g['micro'], false, PROXY_SIZE_MICRO),
626                                 'name'    => htmlentities($g['name']),
627                                 'id'      => intval($g['id']),
628                                 'network' => $g['network'],
629                                 'link'    => $g['url'],
630                                 'nick'    => htmlentities(($g['attag']) ? $g['attag'] : $g['nick']),
631                                 'addr'    => htmlentities(($g['addr']) ? $g['addr'] : $g['url']),
632                                 'forum'   => ((x($g, 'forum') || x($g, 'prv')) ? 1 : 0),
633                         );
634                         if ($entry['forum']) {
635                                 $forums[] = $entry;
636                         } else {
637                                 $contacts[] = $entry;
638                         }
639                 }
640                 if (count($forums) > 0) {
641                         $forums[] = array("separator" => true);
642                         $contacts = array_merge($forums, $contacts);
643                 }
644         }
645
646         $items = array_merge($groups, $contacts);
647
648         if ($conv_id) {
649                 /*
650                  * if $conv_id is set, get unknown contacts in thread
651                  * but first get known contacts url to filter them out
652                  */
653                 $known_contacts = array_map(
654                         function ($i) {
655                                 return dbesc($i['link']);
656                         }
657                 , $contacts);
658
659                 $unknown_contacts = array();
660                 $r = q("SELECT `author-link`
661                                 FROM `item` WHERE `parent` = %d
662                                         AND (`author-name` LIKE '%%%s%%' OR `author-link` LIKE '%%%s%%')
663                                         AND `author-link` NOT IN ('%s')
664                                 GROUP BY `author-link`, `author-avatar`, `author-name`
665                                 ORDER BY `author-name` ASC
666                                 ",
667                                 intval($conv_id),
668                                 dbesc($search),
669                                 dbesc($search),
670                                 implode("', '", $known_contacts)
671                 );
672                 if (dbm::is_result($r)) {
673                         foreach ($r as $row) {
674                                 $contact = get_contact_details_by_url($row['author-link']);
675
676                                 if (count($contact) > 0) {
677                                         $unknown_contacts[] = array(
678                                                 'type'    => 'c',
679                                                 'photo'   => proxy_url($contact['micro'], false, PROXY_SIZE_MICRO),
680                                                 'name'    => htmlentities($contact['name']),
681                                                 'id'      => intval($contact['cid']),
682                                                 'network' => $contact['network'],
683                                                 'link'    => $contact['url'],
684                                                 'nick'    => htmlentities($contact['nick'] ? : $contact['addr']),
685                                                 'addr'    => htmlentities(($contact['addr']) ? $contact['addr'] : $contact['url']),
686                                                 'forum'   => $contact['forum']
687                                         );
688                                 }
689                         }
690                 }
691
692                 $items = array_merge($items, $unknown_contacts);
693                 $tot += count($unknown_contacts);
694         }
695
696         $results = array(
697                 'tot'      => $tot,
698                 'start'    => $start,
699                 'count'    => $count,
700                 'groups'   => $groups,
701                 'contacts' => $contacts,
702                 'items'    => $items,
703                 'type'     => $type,
704                 'search'   => $search,
705         );
706
707         call_hooks('acl_lookup_end', $results);
708
709         if ($out_type === 'html') {
710                 $o = array(
711                         'tot'      => $results['tot'],
712                         'start'    => $results['start'],
713                         'count'    => $results['count'],
714                         'groups'   => $results['groups'],
715                         'contacts' => $results['contacts'],
716                 );
717                 return $o;
718         }
719
720         $o = array(
721                 'tot'   => $results['tot'],
722                 'start' => $results['start'],
723                 'count' => $results['count'],
724                 'items' => $results['items'],
725         );
726
727         echo json_encode($o);
728
729         killme();
730 }
731 /**
732  * @brief Searching for global contacts for autocompletion
733  *
734  * @param App $a
735  * @return array with the search results
736  */
737 function navbar_complete(App $a) {
738
739 //      logger('navbar_complete');
740
741         if ((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
742                 return;
743         }
744
745         // check if searching in the local global contact table is enabled
746         $localsearch = get_config('system','poco_local_search');
747
748         $search = $prefix.notags(trim($_REQUEST['search']));
749         $mode = $_REQUEST['smode'];
750
751         // don't search if search term has less than 2 characters
752         if (! $search || mb_strlen($search) < 2) {
753                 return array();
754         }
755
756         if (substr($search,0,1) === '@') {
757                 $search = substr($search,1);
758         }
759
760         if ($localsearch) {
761                 $x = DirSearch::global_search_by_name($search, $mode);
762                 return $x;
763         }
764
765         if (! $localsearch) {
766                 $p = (($a->pager['page'] != 1) ? '&p=' . $a->pager['page'] : '');
767
768                 $x = z_fetch_url(get_server().'/lsearch?f=' . $p .  '&search=' . urlencode($search));
769                 if ($x['success']) {
770                         $t = 0;
771                         $j = json_decode($x['body'],true);
772                         if ($j && $j['results']) {
773                                 return $j['results'];
774                         }
775                 }
776         }
777
778         /// @TODO Not needed here?
779         return;
780 }