]> git.mxchange.org Git - friendica.git/blob - include/acl_selectors.php
72c434692c0c6087f432f56402049ba41c248b70
[friendica.git] / include / acl_selectors.php
1 <?php
2
3 require_once("include/contact_selectors.php");
4 require_once("include/features.php");
5
6 /**
7  * 
8  */
9
10 /**
11  * @package acl_selectors 
12  */
13 function group_select($selname,$selclass,$preselected = false,$size = 4) {
14
15         $a = get_app();
16
17         $o = '';
18
19         $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"$size\" >\r\n";
20
21         $r = q("SELECT * FROM `group` WHERE `deleted` = 0 AND `uid` = %d ORDER BY `name` ASC",
22                 intval(local_user())
23         );
24
25
26         $arr = array('group' => $r, 'entry' => $o);
27
28         // e.g. 'network_pre_group_deny', 'profile_pre_group_allow'
29
30         call_hooks($a->module . '_pre_' . $selname, $arr);
31
32         if(count($r)) {
33                 foreach($r as $rr) {
34                         if((is_array($preselected)) && in_array($rr['id'], $preselected))
35                                 $selected = " selected=\"selected\" ";
36                         else
37                                 $selected = '';
38                         $trimmed = mb_substr($rr['name'],0,12);
39
40                         $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}\" >$trimmed</option>\r\n";
41                 }
42         
43         }
44         $o .= "</select>\r\n";
45
46         call_hooks($a->module . '_post_' . $selname, $o);
47
48
49         return $o;
50 }
51
52
53 function contact_selector($selname, $selclass, $preselected = false, $options) {
54
55         $a = get_app();
56
57         $mutual = false;
58         $networks = null;
59         $single = false;
60         $exclude = false;
61         $size = 4;
62
63         if(is_array($options)) {
64                 if(x($options,'size'))
65                         $size = $options['size'];
66
67                 if(x($options,'mutual_friends'))
68                         $mutual = true;
69                 if(x($options,'single'))
70                         $single = true;
71                 if(x($options,'multiple'))
72                         $single = false;
73                 if(x($options,'exclude'))
74                         $exclude = $options['exclude'];
75
76                 if(x($options,'networks')) {
77                         switch($options['networks']) {
78                                 case 'DFRN_ONLY':
79                                         $networks = array('dfrn');
80                                         break;
81                                 case 'PRIVATE':
82                                         if(is_array($a->user) && $a->user['prvnets'])
83                                                 $networks = array('dfrn','mail','dspr');
84                                         else
85                                                 $networks = array('dfrn','face','mail', 'dspr');
86                                         break;
87                                 case 'TWO_WAY':
88                                         if(is_array($a->user) && $a->user['prvnets'])
89                                                 $networks = array('dfrn','mail','dspr');
90                                         else
91                                                 $networks = array('dfrn','face','mail','dspr','stat');
92                                         break;                                  
93                                 default:
94                                         break;
95                         }
96                 }
97         }
98                 
99         $x = array('options' => $options, 'size' => $size, 'single' => $single, 'mutual' => $mutual, 'exclude' => $exclude, 'networks' => $networks);
100
101         call_hooks('contact_select_options', $x);
102
103         $o = '';
104
105         $sql_extra = '';
106
107         if($x['mutual']) {
108                 $sql_extra .= sprintf(" AND `rel` = %d ", intval(CONTACT_IS_FRIEND));
109         }
110
111         if(intval($x['exclude']))
112                 $sql_extra .= sprintf(" AND `id` != %d ", intval($x['exclude']));
113
114         if(is_array($x['networks']) && count($x['networks'])) {
115                 for($y = 0; $y < count($x['networks']) ; $y ++)
116                         $x['networks'][$y] = "'" . dbesc($x['networks'][$y]) . "'";
117                 $str_nets = implode(',',$x['networks']);
118                 $sql_extra .= " AND `network` IN ( $str_nets ) ";
119         }
120         
121         $tabindex = (x($options, 'tabindex') ? "tabindex=\"" . $options["tabindex"] . "\"" : "");
122
123         if($x['single'])
124                 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"" . $x['size'] . "\" $tabindex >\r\n";
125         else 
126                 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"" . $x['size'] . "$\" $tabindex >\r\n";
127
128         $r = q("SELECT `id`, `name`, `url`, `network` FROM `contact` 
129                 WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 AND `notify` != ''
130                 $sql_extra
131                 ORDER BY `name` ASC ",
132                 intval(local_user())
133         );
134
135
136         $arr = array('contact' => $r, 'entry' => $o);
137
138         // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
139
140         call_hooks($a->module . '_pre_' . $selname, $arr);
141
142         if(count($r)) {
143                 foreach($r as $rr) {
144                         if((is_array($preselected)) && in_array($rr['id'], $preselected))
145                                 $selected = " selected=\"selected\" ";
146                         else
147                                 $selected = '';
148
149                         $trimmed = mb_substr($rr['name'],0,20);
150
151                         $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}|{$rr['url']}\" >$trimmed</option>\r\n";
152                 }
153         
154         }
155
156         $o .= "</select>\r\n";
157
158         call_hooks($a->module . '_post_' . $selname, $o);
159
160         return $o;
161 }
162
163
164
165 function contact_select($selname, $selclass, $preselected = false, $size = 4, $privmail = false, $celeb = false, $privatenet = false, $tabindex = null) {
166
167         $a = get_app();
168
169         $o = '';
170
171         // When used for private messages, we limit correspondence to mutual DFRN/Friendica friends and the selector
172         // to one recipient. By default our selector allows multiple selects amongst all contacts.
173
174         $sql_extra = '';
175
176         if($privmail || $celeb) {
177                 $sql_extra .= sprintf(" AND `rel` = %d ", intval(CONTACT_IS_FRIEND));
178         }
179
180         if($privmail) {
181                 $sql_extra .= " AND `network` IN ( 'dfrn', 'dspr' ) ";
182         }
183         elseif($privatenet) {   
184                 $sql_extra .= " AND `network` IN ( 'dfrn', 'mail', 'face', 'dspr' ) ";
185         }
186
187         $tabindex = ($tabindex > 0 ? "tabindex=\"$tabindex\"" : "");
188
189         if($privmail)
190                 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"$size\" $tabindex >\r\n";
191         else 
192                 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"$size\" $tabindex >\r\n";
193
194         $r = q("SELECT `id`, `name`, `url`, `network` FROM `contact` 
195                 WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 AND `notify` != ''
196                 $sql_extra
197                 ORDER BY `name` ASC ",
198                 intval(local_user())
199         );
200
201
202         $arr = array('contact' => $r, 'entry' => $o);
203
204         // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
205
206         call_hooks($a->module . '_pre_' . $selname, $arr);
207
208         if(count($r)) {
209                 foreach($r as $rr) {
210                         if((is_array($preselected)) && in_array($rr['id'], $preselected))
211                                 $selected = " selected=\"selected\" ";
212                         else
213                                 $selected = '';
214
215                         $trimmed = mb_substr($rr['name'],0,20);
216
217                         $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}|{$rr['url']}\" >$trimmed</option>\r\n";
218                 }
219         
220         }
221
222         $o .= "</select>\r\n";
223
224         call_hooks($a->module . '_post_' . $selname, $o);
225
226         return $o;
227 }
228
229
230 function fixacl(&$item) {
231         $item = intval(str_replace(array('<','>'),array('',''),$item));
232 }
233
234 function prune_deadguys($arr) {
235
236         if(! $arr)
237                 return $arr;
238         $str = dbesc(implode(',',$arr));
239         $r = q("select id from contact where id in ( " . $str . ") and blocked = 0 and pending = 0 and archive = 0 ");
240         if($r) {
241                 $ret = array();
242                 foreach($r as $rr) 
243                         $ret[] = intval($rr['id']);
244                 return $ret;
245         }
246         return array();
247 }
248
249
250 function get_acl_permissions($user = null) {
251         $allow_cid = $allow_gid = $deny_cid = $deny_gid = false;
252
253         if(is_array($user)) {
254                 $allow_cid = ((strlen($user['allow_cid'])) 
255                         ? explode('><', $user['allow_cid']) : array() );
256                 $allow_gid = ((strlen($user['allow_gid']))
257                         ? explode('><', $user['allow_gid']) : array() );
258                 $deny_cid  = ((strlen($user['deny_cid']))
259                         ? explode('><', $user['deny_cid']) : array() );
260                 $deny_gid  = ((strlen($user['deny_gid']))
261                         ? explode('><', $user['deny_gid']) : array() );
262                 array_walk($allow_cid,'fixacl');
263                 array_walk($allow_gid,'fixacl');
264                 array_walk($deny_cid,'fixacl');
265                 array_walk($deny_gid,'fixacl');
266         }
267
268         $allow_cid = prune_deadguys($allow_cid);
269
270         return array(
271                 'allow_cid' => $allow_cid,
272                 'allow_gid' => $allow_gid,
273                 'deny_cid' => $deny_cid,
274                 'deny_gid' => $deny_gid,
275         );
276 }
277
278
279 function populate_acl($user = null,$celeb = false) {
280
281         $perms = get_acl_permissions($user);
282
283         // We shouldn't need to prune deadguys from the block list. Either way they can't get the message.
284         // Also no point enumerating groups and checking them, that will take place on delivery.
285
286 //      $deny_cid = prune_deadguys($deny_cid);
287
288
289         /*$o = '';
290         $o .= '<div id="acl-wrapper">';
291         $o .= '<div id="acl-permit-outer-wrapper">';
292         $o .= '<div id="acl-permit-text">' . t('Visible To:') . '</div><div id="jot-public">' . t('everybody') . '</div>';
293         $o .= '<div id="acl-permit-text-end"></div>';
294         $o .= '<div id="acl-permit-wrapper">';
295         $o .= '<div id="group_allow_wrapper">';
296         $o .= '<label id="acl-allow-group-label" for="group_allow" >' . t('Groups') . '</label>';
297         $o .= group_select('group_allow','group_allow',$allow_gid);
298         $o .= '</div>';
299         $o .= '<div id="contact_allow_wrapper">';
300         $o .= '<label id="acl-allow-contact-label" for="contact_allow" >' . t('Contacts') . '</label>';
301         $o .= contact_select('contact_allow','contact_allow',$allow_cid,4,false,$celeb,true);
302         $o .= '</div>';
303         $o .= '</div>' . "\r\n";
304         $o .= '<div id="acl-allow-end"></div>' . "\r\n";
305         $o .= '</div>';
306         $o .= '<div id="acl-deny-outer-wrapper">';
307         $o .= '<div id="acl-deny-text">' . t('Except For:') . '</div>';
308         $o .= '<div id="acl-deny-text-end"></div>';
309         $o .= '<div id="acl-deny-wrapper">';
310         $o .= '<div id="group_deny_wrapper" >';
311         $o .= '<label id="acl-deny-group-label" for="group_deny" >' . t('Groups') . '</label>';
312         $o .= group_select('group_deny','group_deny', $deny_gid);
313         $o .= '</div>';
314         $o .= '<div id="contact_deny_wrapper" >';
315         $o .= '<label id="acl-deny-contact-label" for="contact_deny" >' . t('Contacts') . '</label>';
316         $o .= contact_select('contact_deny','contact_deny', $deny_cid,4,false, $celeb,true);
317         $o .= '</div>';
318         $o .= '</div>' . "\r\n";
319         $o .= '<div id="acl-deny-end"></div>' . "\r\n";
320         $o .= '</div>';
321         $o .= '</div>' . "\r\n";
322         $o .= '<div id="acl-wrapper-end"></div>' . "\r\n";*/
323
324         $tpl = get_markup_template("acl_selector.tpl");
325         $o = replace_macros($tpl, array(
326                 '$showall'=> t("Visible to everybody"),
327                 '$show'          => t("show"),
328                 '$hide'          => t("don't show"),
329                 '$allowcid' => json_encode($perms['allow_cid']),
330                 '$allowgid' => json_encode($perms['allow_gid']),
331                 '$denycid' => json_encode($perms['deny_cid']),
332                 '$denygid' => json_encode($perms['deny_gid']),
333                 '$features' => array(
334                         "aclautomention"=>(feature_enabled($user['uid'],"aclautomention")?"true":"false")
335                 ),
336         ));
337
338
339         return $o;
340
341 }
342
343 function construct_acl_data(&$a, $user) {
344
345         // Get group and contact information for html ACL selector
346         $acl_data = acl_lookup($a, 'html');
347
348         $user_defaults = get_acl_permissions($user);
349
350         if($acl_data['groups']) {
351                 foreach($acl_data['groups'] as $key=>$group) {
352                         // Add a "selected" flag to groups that are posted to by default
353                         if($user_defaults['allow_gid'] &&
354                            in_array($group['id'], $user_defaults['allow_gid']) && !in_array($group['id'], $user_defaults['deny_gid']) )
355                                 $acl_data['groups'][$key]['selected'] = 1;
356                         else
357                                 $acl_data['groups'][$key]['selected'] = 0;
358                 }
359         }
360         if($acl_data['contacts']) {
361                 foreach($acl_data['contacts'] as $key=>$contact) {
362                         // Add a "selected" flag to groups that are posted to by default
363                         if($user_defaults['allow_cid'] &&
364                            in_array($contact['id'], $user_defaults['allow_cid']) && !in_array($contact['id'], $user_defaults['deny_cid']) )
365                                 $acl_data['contacts'][$key]['selected'] = 1;
366                         else
367                                 $acl_data['contacts'][$key]['selected'] = 0;
368                 }
369         }
370
371         return $acl_data;
372
373 }
374
375 function acl_lookup(&$a, $out_type = 'json') {
376
377         if(!local_user())
378                 return "";
379
380
381         $start = (x($_REQUEST,'start')?$_REQUEST['start']:0);
382         $count = (x($_REQUEST,'count')?$_REQUEST['count']:100);
383         $search = (x($_REQUEST,'search')?$_REQUEST['search']:"");
384         $type = (x($_REQUEST,'type')?$_REQUEST['type']:"");
385         
386
387         // For use with jquery.autocomplete for private mail completion
388
389         if(x($_REQUEST,'query') && strlen($_REQUEST['query'])) {
390                 if(! $type)
391                         $type = 'm';
392                 $search = $_REQUEST['query'];
393         }
394
395
396         if ($search!=""){
397                 $sql_extra = "AND `name` LIKE '%%".dbesc($search)."%%'";
398                 $sql_extra2 = "AND (`attag` LIKE '%%".dbesc($search)."%%' OR `name` LIKE '%%".dbesc($search)."%%' OR `nick` LIKE '%%".dbesc($search)."%%')";
399         } else {
400                 $sql_extra = $sql_extra2 = "";
401         }
402         
403         // count groups and contacts
404         if ($type=='' || $type=='g'){
405                 $r = q("SELECT COUNT(`id`) AS g FROM `group` WHERE `deleted` = 0 AND `uid` = %d $sql_extra",
406                         intval(local_user())
407                 );
408                 $group_count = (int)$r[0]['g'];
409         } else {
410                 $group_count = 0;
411         }
412         
413         if ($type=='' || $type=='c'){
414                 $r = q("SELECT COUNT(`id`) AS c FROM `contact` 
415                                 WHERE `uid` = %d AND `self` = 0 
416                                 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0
417                                 AND `notify` != '' $sql_extra2" ,
418                         intval(local_user())
419                 );
420                 $contact_count = (int)$r[0]['c'];
421         } 
422         elseif ($type == 'm') {
423
424                 // autocomplete for Private Messages
425
426                 $r = q("SELECT COUNT(`id`) AS c FROM `contact` 
427                                 WHERE `uid` = %d AND `self` = 0 
428                                 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 
429                                 AND `network` IN ('%s','%s','%s') $sql_extra2" ,
430                         intval(local_user()),
431                         dbesc(NETWORK_DFRN),
432                         dbesc(NETWORK_ZOT),
433                         dbesc(NETWORK_DIASPORA)
434                 );
435                 $contact_count = (int)$r[0]['c'];
436
437         }
438         elseif ($type == 'a') {
439
440                 // autocomplete for Contacts
441
442                 $r = q("SELECT COUNT(`id`) AS c FROM `contact` 
443                                 WHERE `uid` = %d AND `self` = 0 
444                                 AND `pending` = 0 $sql_extra2" ,
445                         intval(local_user())
446                 );
447                 $contact_count = (int)$r[0]['c'];
448
449         } else {
450                 $contact_count = 0;
451         }
452         
453         $tot = $group_count+$contact_count;
454         
455         $groups = array();
456         $contacts = array();
457         
458         if ($type=='' || $type=='g'){
459                 
460                 $r = q("SELECT `group`.`id`, `group`.`name`, GROUP_CONCAT(DISTINCT `group_member`.`contact-id` SEPARATOR ',') as uids
461                                 FROM `group`,`group_member` 
462                                 WHERE `group`.`deleted` = 0 AND `group`.`uid` = %d 
463                                         AND `group_member`.`gid`=`group`.`id`
464                                         $sql_extra
465                                 GROUP BY `group`.`id`
466                                 ORDER BY `group`.`name` 
467                                 LIMIT %d,%d",
468                         intval(local_user()),
469                         intval($start),
470                         intval($count)
471                 );
472
473                 foreach($r as $g){
474 //              logger('acl: group: ' . $g['name'] . ' members: ' . $g['uids']);                
475                         $groups[] = array(
476                                 "type"  => "g",
477                                 "photo" => "images/twopeople.png",
478                                 "name"  => $g['name'],
479                                 "id"    => intval($g['id']),
480                                 "uids"  => array_map("intval", explode(",",$g['uids'])),
481                                 "link"  => '',
482                                 "forum" => '0'
483                         );
484                 }
485         }
486         
487         if ($type=='' || $type=='c'){
488         
489                 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, forum FROM `contact` 
490                         WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 AND `notify` != ''
491                         $sql_extra2
492                         ORDER BY `name` ASC ",
493                         intval(local_user())
494                 );
495         }
496         elseif($type == 'm') {
497                 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag` FROM `contact` 
498                         WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0
499                         AND `network` IN ('%s','%s','%s')
500                         $sql_extra2
501                         ORDER BY `name` ASC ",
502                         intval(local_user()),
503                         dbesc(NETWORK_DFRN),
504                         dbesc(NETWORK_ZOT),
505                         dbesc(NETWORK_DIASPORA)
506                 );
507         }
508         elseif($type == 'a') {
509                 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag` FROM `contact` 
510                         WHERE `uid` = %d AND `pending` = 0
511                         $sql_extra2
512                         ORDER BY `name` ASC ",
513                         intval(local_user())
514                 );
515         }
516         else
517                 $r = array();
518
519
520         if($type == 'm' || $type == 'a') {
521                 $x = array();
522                 $x['query'] = $search;
523                 $x['photos'] = array();
524                 $x['links'] = array();
525                 $x['suggestions'] = array();
526                 $x['data'] = array();
527                 if(count($r)) {
528                         foreach($r as $g) {
529                                 $x['photos'][] = $g['micro'];
530                                 $x['links'][] = $g['url'];
531                                 $x['suggestions'][] = $g['name'];
532                                 $x['data'][] = intval($g['id']);
533                         }
534                 }
535                 echo json_encode($x);
536                 killme();
537         }
538
539         if(count($r)) {
540                 foreach($r as $g){
541                         $contacts[] = array(
542                                 "type"  => "c",
543                                 "photo" => $g['micro'],
544                                 "name"  => $g['name'],
545                                 "id"    => intval($g['id']),
546                                 "network" => $g['network'],
547                                 "link" => $g['url'],
548                                 "nick" => ($g['attag']) ? $g['attag'] : $g['nick'],
549                                 "forum" => $g['forum']
550                         );
551                 }                       
552         }
553                 
554         $items = array_merge($groups, $contacts);
555
556
557         if($out_type === 'html') {
558                 $o = array(
559                         'tot'           => $tot,
560                         'start' => $start,
561                         'count' => $count,
562                         'groups'        => $groups,
563                         'contacts'      => $contacts,
564                 );
565                 return $o;
566         }
567         
568         $o = array(
569                 'tot'   => $tot,
570                 'start' => $start,
571                 'count' => $count,
572                 'items' => $items,
573         );
574         
575         echo json_encode($o);
576
577         killme();
578 }
579