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