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