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