]> git.mxchange.org Git - friendica.git/blob - include/acl_selectors.php
include jotnets in populate_acl
[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)
194                 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"$size\" $tabindex >\r\n";
195         else
196                 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"$size\" $tabindex >\r\n";
197
198         $r = q("SELECT `id`, `name`, `url`, `network` FROM `contact`
199                 WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 AND `notify` != ''
200                 $sql_extra
201                 ORDER BY `name` ASC ",
202                 intval(local_user())
203         );
204
205
206         $arr = array('contact' => $r, 'entry' => $o);
207
208         // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
209
210         call_hooks($a->module . '_pre_' . $selname, $arr);
211
212         if(count($r)) {
213                 foreach($r as $rr) {
214                         if((is_array($preselected)) && in_array($rr['id'], $preselected))
215                                 $selected = " selected=\"selected\" ";
216                         else
217                                 $selected = '';
218
219                         if($privmail)
220                                 $trimmed = GetProfileUsername($rr['url'], $rr['name'], false);
221                         else
222                                 $trimmed = mb_substr($rr['name'],0,20);
223
224                         $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}|{$rr['url']}\" >$trimmed</option>\r\n";
225                 }
226
227         }
228
229         $o .= "</select>\r\n";
230
231         call_hooks($a->module . '_post_' . $selname, $o);
232
233         return $o;
234 }
235
236
237 function fixacl(&$item) {
238         $item = intval(str_replace(array('<','>'),array('',''),$item));
239 }
240
241 function prune_deadguys($arr) {
242
243         if(! $arr)
244                 return $arr;
245         $str = dbesc(implode(',',$arr));
246         $r = q("select id from contact where id in ( " . $str . ") and blocked = 0 and pending = 0 and archive = 0 ");
247         if($r) {
248                 $ret = array();
249                 foreach($r as $rr)
250                         $ret[] = intval($rr['id']);
251                 return $ret;
252         }
253         return array();
254 }
255
256
257 function get_acl_permissions($user = null) {
258         $allow_cid = $allow_gid = $deny_cid = $deny_gid = false;
259
260         if(is_array($user)) {
261                 $allow_cid = ((strlen($user['allow_cid']))
262                         ? explode('><', $user['allow_cid']) : array() );
263                 $allow_gid = ((strlen($user['allow_gid']))
264                         ? explode('><', $user['allow_gid']) : array() );
265                 $deny_cid  = ((strlen($user['deny_cid']))
266                         ? explode('><', $user['deny_cid']) : array() );
267                 $deny_gid  = ((strlen($user['deny_gid']))
268                         ? explode('><', $user['deny_gid']) : array() );
269                 array_walk($allow_cid,'fixacl');
270                 array_walk($allow_gid,'fixacl');
271                 array_walk($deny_cid,'fixacl');
272                 array_walk($deny_gid,'fixacl');
273         }
274
275         $allow_cid = prune_deadguys($allow_cid);
276
277         return array(
278                 'allow_cid' => $allow_cid,
279                 'allow_gid' => $allow_gid,
280                 'deny_cid' => $deny_cid,
281                 'deny_gid' => $deny_gid,
282         );
283 }
284
285
286 function populate_acl($user = null,$celeb = false,$show_jotnets = true,$show_mail = false) {
287
288         $perms = get_acl_permissions($user);
289
290         $jotnets = '';
291         if($show_jotnets) {
292                 call_hooks('jot_networks', $jotnets);
293         }
294
295         // We shouldn't need to prune deadguys from the block list. Either way they can't get the message.
296         // Also no point enumerating groups and checking them, that will take place on delivery.
297
298 //      $deny_cid = prune_deadguys($deny_cid);
299
300
301         /*$o = '';
302         $o .= '<div id="acl-wrapper">';
303         $o .= '<div id="acl-permit-outer-wrapper">';
304         $o .= '<div id="acl-permit-text">' . t('Visible To:') . '</div><div id="jot-public">' . t('everybody') . '</div>';
305         $o .= '<div id="acl-permit-text-end"></div>';
306         $o .= '<div id="acl-permit-wrapper">';
307         $o .= '<div id="group_allow_wrapper">';
308         $o .= '<label id="acl-allow-group-label" for="group_allow" >' . t('Groups') . '</label>';
309         $o .= group_select('group_allow','group_allow',$allow_gid);
310         $o .= '</div>';
311         $o .= '<div id="contact_allow_wrapper">';
312         $o .= '<label id="acl-allow-contact-label" for="contact_allow" >' . t('Contacts') . '</label>';
313         $o .= contact_select('contact_allow','contact_allow',$allow_cid,4,false,$celeb,true);
314         $o .= '</div>';
315         $o .= '</div>' . "\r\n";
316         $o .= '<div id="acl-allow-end"></div>' . "\r\n";
317         $o .= '</div>';
318         $o .= '<div id="acl-deny-outer-wrapper">';
319         $o .= '<div id="acl-deny-text">' . t('Except For:') . '</div>';
320         $o .= '<div id="acl-deny-text-end"></div>';
321         $o .= '<div id="acl-deny-wrapper">';
322         $o .= '<div id="group_deny_wrapper" >';
323         $o .= '<label id="acl-deny-group-label" for="group_deny" >' . t('Groups') . '</label>';
324         $o .= group_select('group_deny','group_deny', $deny_gid);
325         $o .= '</div>';
326         $o .= '<div id="contact_deny_wrapper" >';
327         $o .= '<label id="acl-deny-contact-label" for="contact_deny" >' . t('Contacts') . '</label>';
328         $o .= contact_select('contact_deny','contact_deny', $deny_cid,4,false, $celeb,true);
329         $o .= '</div>';
330         $o .= '</div>' . "\r\n";
331         $o .= '<div id="acl-deny-end"></div>' . "\r\n";
332         $o .= '</div>';
333         $o .= '</div>' . "\r\n";
334         $o .= '<div id="acl-wrapper-end"></div>' . "\r\n";*/
335
336         $tpl = get_markup_template("acl_selector.tpl");
337         $o = replace_macros($tpl, array(
338                 '$showall'=> t("Visible to everybody"),
339                 '$show'          => t("show"),
340                 '$hide'          => t("don't show"),
341                 '$allowcid' => json_encode($perms['allow_cid']),
342                 '$allowgid' => json_encode($perms['allow_gid']),
343                 '$denycid' => json_encode($perms['deny_cid']),
344                 '$denygid' => json_encode($perms['deny_gid']),
345                 '$emailcc' => t('CC: email addresses'),
346                 '$emtitle' => t('Example: bob@example.com, mary@example.com'),
347                 '$jotnets' => $jotnets,
348                 '$aclModalTitle' => t('Permissions'),
349                 '$aclModalDismiss' => t('Close'),
350                 '$features' => array(
351                         "aclautomention"=>(feature_enabled($user['uid'],"aclautomention")?"true":"false")
352                 ),
353         ));
354
355
356         return $o;
357
358 }
359
360 function construct_acl_data(&$a, $user) {
361
362         // Get group and contact information for html ACL selector
363         $acl_data = acl_lookup($a, 'html');
364
365         $user_defaults = get_acl_permissions($user);
366
367         if($acl_data['groups']) {
368                 foreach($acl_data['groups'] as $key=>$group) {
369                         // Add a "selected" flag to groups that are posted to by default
370                         if($user_defaults['allow_gid'] &&
371                            in_array($group['id'], $user_defaults['allow_gid']) && !in_array($group['id'], $user_defaults['deny_gid']) )
372                                 $acl_data['groups'][$key]['selected'] = 1;
373                         else
374                                 $acl_data['groups'][$key]['selected'] = 0;
375                 }
376         }
377         if($acl_data['contacts']) {
378                 foreach($acl_data['contacts'] as $key=>$contact) {
379                         // Add a "selected" flag to groups that are posted to by default
380                         if($user_defaults['allow_cid'] &&
381                            in_array($contact['id'], $user_defaults['allow_cid']) && !in_array($contact['id'], $user_defaults['deny_cid']) )
382                                 $acl_data['contacts'][$key]['selected'] = 1;
383                         else
384                                 $acl_data['contacts'][$key]['selected'] = 0;
385                 }
386         }
387
388         return $acl_data;
389
390 }
391
392 function acl_lookup(&$a, $out_type = 'json') {
393
394         if(!local_user())
395                 return "";
396
397
398         $start = (x($_REQUEST,'start')?$_REQUEST['start']:0);
399         $count = (x($_REQUEST,'count')?$_REQUEST['count']:100);
400         $search = (x($_REQUEST,'search')?$_REQUEST['search']:"");
401         $type = (x($_REQUEST,'type')?$_REQUEST['type']:"");
402         $conv_id = (x($_REQUEST,'conversation')?$_REQUEST['conversation']:null);
403
404         // For use with jquery.autocomplete for private mail completion
405
406         if(x($_REQUEST,'query') && strlen($_REQUEST['query'])) {
407                 if(! $type)
408                         $type = 'm';
409                 $search = $_REQUEST['query'];
410         }
411
412
413         if ($search!=""){
414                 $sql_extra = "AND `name` LIKE '%%".dbesc($search)."%%'";
415                 $sql_extra2 = "AND (`attag` LIKE '%%".dbesc($search)."%%' OR `name` LIKE '%%".dbesc($search)."%%' OR `nick` LIKE '%%".dbesc($search)."%%')";
416         } else {
417                 $sql_extra = $sql_extra2 = "";
418         }
419
420         // count groups and contacts
421         if ($type=='' || $type=='g'){
422                 $r = q("SELECT COUNT(*) AS g FROM `group` WHERE `deleted` = 0 AND `uid` = %d $sql_extra",
423                         intval(local_user())
424                 );
425                 $group_count = (int)$r[0]['g'];
426         } else {
427                 $group_count = 0;
428         }
429
430         if ($type=='' || $type=='c'){
431                 $r = q("SELECT COUNT(*) AS c FROM `contact`
432                                 WHERE `uid` = %d AND `self` = 0
433                                 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0
434                                 AND `notify` != '' $sql_extra2" ,
435                         intval(local_user())
436                 );
437                 $contact_count = (int)$r[0]['c'];
438         }
439         elseif ($type == 'm') {
440
441                 // autocomplete for Private Messages
442
443                 $r = q("SELECT COUNT(*) AS c FROM `contact`
444                                 WHERE `uid` = %d AND `self` = 0
445                                 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0
446                                 AND `network` IN ('%s','%s','%s') $sql_extra2" ,
447                         intval(local_user()),
448                         dbesc(NETWORK_DFRN),
449                         dbesc(NETWORK_ZOT),
450                         dbesc(NETWORK_DIASPORA)
451                 );
452                 $contact_count = (int)$r[0]['c'];
453
454         }
455         elseif ($type == 'a') {
456
457                 // autocomplete for Contacts
458
459                 $r = q("SELECT COUNT(*) AS c FROM `contact`
460                                 WHERE `uid` = %d AND `self` = 0
461                                 AND `pending` = 0 $sql_extra2" ,
462                         intval(local_user())
463                 );
464                 $contact_count = (int)$r[0]['c'];
465
466         } else {
467                 $contact_count = 0;
468         }
469
470
471         $tot = $group_count+$contact_count;
472
473         $groups = array();
474         $contacts = array();
475
476         if ($type=='' || $type=='g'){
477
478                 $r = q("SELECT `group`.`id`, `group`.`name`, GROUP_CONCAT(DISTINCT `group_member`.`contact-id` SEPARATOR ',') as uids
479                                 FROM `group`,`group_member`
480                                 WHERE `group`.`deleted` = 0 AND `group`.`uid` = %d
481                                         AND `group_member`.`gid`=`group`.`id`
482                                         $sql_extra
483                                 GROUP BY `group`.`id`
484                                 ORDER BY `group`.`name`
485                                 LIMIT %d,%d",
486                         intval(local_user()),
487                         intval($start),
488                         intval($count)
489                 );
490
491                 foreach($r as $g){
492 //              logger('acl: group: ' . $g['name'] . ' members: ' . $g['uids']);
493                         $groups[] = array(
494                                 "type"  => "g",
495                                 "photo" => "images/twopeople.png",
496                                 "name"  => $g['name'],
497                                 "id"    => intval($g['id']),
498                                 "uids"  => array_map("intval", explode(",",$g['uids'])),
499                                 "link"  => '',
500                                 "forum" => '0'
501                         );
502                 }
503         }
504
505         if ($type=='' || $type=='c'){
506
507                 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, forum FROM `contact`
508                         WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 AND `notify` != ''
509                         $sql_extra2
510                         ORDER BY `name` ASC ",
511                         intval(local_user())
512                 );
513         }
514         elseif($type == 'm') {
515                 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag` FROM `contact`
516                         WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0
517                         AND `network` IN ('%s','%s','%s')
518                         $sql_extra2
519                         ORDER BY `name` ASC ",
520                         intval(local_user()),
521                         dbesc(NETWORK_DFRN),
522                         dbesc(NETWORK_ZOT),
523                         dbesc(NETWORK_DIASPORA)
524                 );
525         }
526         elseif($type == 'a') {
527                 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag` FROM `contact`
528                         WHERE `uid` = %d AND `pending` = 0
529                         $sql_extra2
530                         ORDER BY `name` ASC ",
531                         intval(local_user())
532                 );
533         }
534         else
535                 $r = array();
536
537
538         if($type == 'm' || $type == 'a') {
539                 $x = array();
540                 $x['query'] = $search;
541                 $x['photos'] = array();
542                 $x['links'] = array();
543                 $x['suggestions'] = array();
544                 $x['data'] = array();
545                 if(count($r)) {
546                         foreach($r as $g) {
547                                 $x['photos'][] = proxy_url($g['micro']);
548                                 $x['links'][] = $g['url'];
549                                 $x['suggestions'][] = $g['name'];
550                                 $x['data'][] = intval($g['id']);
551                         }
552                 }
553                 echo json_encode($x);
554                 killme();
555         }
556
557         if(count($r)) {
558                 foreach($r as $g){
559                         $contacts[] = array(
560                                 "type"  => "c",
561                                 "photo" => proxy_url($g['micro']),
562                                 "name"  => $g['name'],
563                                 "id"    => intval($g['id']),
564                                 "network" => $g['network'],
565                                 "link" => $g['url'],
566                                 "nick" => ($g['attag']) ? $g['attag'] : $g['nick'],
567                                 "forum" => $g['forum']
568                         );
569                 }
570         }
571
572         $items = array_merge($groups, $contacts);
573
574         if ($conv_id) {
575                 /* if $conv_id is set, get unknow contacts in thread */
576                 /* but first get know contacts url to filter them out */
577                 function _contact_link($i){ return dbesc($i['link']); }
578                 $known_contacts = array_map(_contact_link, $contacts);
579                 $unknow_contacts=array();
580                 $r = q("select
581                                         `author-avatar`,`author-name`,`author-link`
582                                 from item where parent=%d
583                                 and (
584                                         `author-name` LIKE '%%%s%%' OR
585                                         `author-link` LIKE '%%%s%%'
586                                 ) and
587                                 `author-link` NOT IN ('%s')
588                                 GROUP BY `author-link`
589                                 ORDER BY `author-name` ASC
590                                 ",
591                                 intval($conv_id),
592                                 dbesc($search),
593                                 dbesc($search),
594                                 implode("','", $known_contacts)
595                 );
596                 if (is_array($r) && count($r)){
597                         foreach($r as $row) {
598                                 // nickname..
599                                 $up = parse_url($row['author-link']);
600                                 $nick = explode("/",$up['path']);
601                                 $nick = $nick[count($nick)-1];
602                                 $nick .= "@".$up['host'];
603                                 // /nickname
604                                 $unknow_contacts[] = array(
605                                         "type"  => "c",
606                                         "photo" => proxy_url($row['author-avatar']),
607                                         "name"  => $row['author-name'],
608                                         "id"    => '',
609                                         "network" => "unknown",
610                                         "link" => $row['author-link'],
611                                         "nick" => $nick,
612                                         "forum" => false
613                                 );
614                         }
615                 }
616
617                 $items = array_merge($items, $unknow_contacts);
618                 $tot += count($unknow_contacts);
619         }
620
621         if($out_type === 'html') {
622                 $o = array(
623                         'tot'           => $tot,
624                         'start' => $start,
625                         'count' => $count,
626                         'groups'        => $groups,
627                         'contacts'      => $contacts,
628                 );
629                 return $o;
630         }
631
632         $o = array(
633                 'tot'   => $tot,
634                 'start' => $start,
635                 'count' => $count,
636                 'items' => $items,
637         );
638
639         echo json_encode($o);
640
641         killme();
642 }
643