3 require_once("include/contact_selectors.php");
4 require_once("include/features.php");
11 * @package acl_selectors
13 function group_select($selname,$selclass,$preselected = false,$size = 4) {
19 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"$size\" >\r\n";
21 $r = q("SELECT * FROM `group` WHERE `deleted` = 0 AND `uid` = %d ORDER BY `name` ASC",
26 $arr = array('group' => $r, 'entry' => $o);
28 // e.g. 'network_pre_group_deny', 'profile_pre_group_allow'
30 call_hooks($a->module . '_pre_' . $selname, $arr);
34 if((is_array($preselected)) && in_array($rr['id'], $preselected))
35 $selected = " selected=\"selected\" ";
38 $trimmed = mb_substr($rr['name'],0,12);
40 $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}\" >$trimmed</option>\r\n";
44 $o .= "</select>\r\n";
46 call_hooks($a->module . '_post_' . $selname, $o);
53 function contact_selector($selname, $selclass, $preselected = false, $options) {
63 if(is_array($options)) {
64 if(x($options,'size'))
65 $size = $options['size'];
67 if(x($options,'mutual_friends'))
69 if(x($options,'single'))
71 if(x($options,'multiple'))
73 if(x($options,'exclude'))
74 $exclude = $options['exclude'];
76 if(x($options,'networks')) {
77 switch($options['networks']) {
79 $networks = array('dfrn');
82 if(is_array($a->user) && $a->user['prvnets'])
83 $networks = array('dfrn','mail','dspr');
85 $networks = array('dfrn','face','mail', 'dspr');
88 if(is_array($a->user) && $a->user['prvnets'])
89 $networks = array('dfrn','mail','dspr');
91 $networks = array('dfrn','face','mail','dspr','stat');
99 $x = array('options' => $options, 'size' => $size, 'single' => $single, 'mutual' => $mutual, 'exclude' => $exclude, 'networks' => $networks);
101 call_hooks('contact_select_options', $x);
108 $sql_extra .= sprintf(" AND `rel` = %d ", intval(CONTACT_IS_FRIEND));
111 if(intval($x['exclude']))
112 $sql_extra .= sprintf(" AND `id` != %d ", intval($x['exclude']));
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 ) ";
121 $tabindex = (x($options, 'tabindex') ? "tabindex=\"" . $options["tabindex"] . "\"" : "");
124 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"" . $x['size'] . "\" $tabindex >\r\n";
126 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"" . $x['size'] . "$\" $tabindex >\r\n";
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` != ''
131 ORDER BY `name` ASC ",
136 $arr = array('contact' => $r, 'entry' => $o);
138 // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
140 call_hooks($a->module . '_pre_' . $selname, $arr);
144 if((is_array($preselected)) && in_array($rr['id'], $preselected))
145 $selected = " selected=\"selected\" ";
149 $trimmed = mb_substr($rr['name'],0,20);
151 $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}|{$rr['url']}\" >$trimmed</option>\r\n";
156 $o .= "</select>\r\n";
158 call_hooks($a->module . '_post_' . $selname, $o);
165 function contact_select($selname, $selclass, $preselected = false, $size = 4, $privmail = false, $celeb = false, $privatenet = false, $tabindex = null) {
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.
176 if($privmail || $celeb) {
177 $sql_extra .= sprintf(" AND `rel` = %d ", intval(CONTACT_IS_FRIEND));
181 $sql_extra .= " AND `network` IN ( 'dfrn', 'dspr' ) ";
183 elseif($privatenet) {
184 $sql_extra .= " AND `network` IN ( 'dfrn', 'mail', 'face', 'dspr' ) ";
187 $tabindex = ($tabindex > 0 ? "tabindex=\"$tabindex\"" : "");
190 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"$size\" $tabindex >\r\n";
192 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"$size\" $tabindex >\r\n";
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` != ''
197 ORDER BY `name` ASC ",
202 $arr = array('contact' => $r, 'entry' => $o);
204 // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
206 call_hooks($a->module . '_pre_' . $selname, $arr);
210 if((is_array($preselected)) && in_array($rr['id'], $preselected))
211 $selected = " selected=\"selected\" ";
215 $trimmed = mb_substr($rr['name'],0,20);
217 $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}|{$rr['url']}\" >$trimmed</option>\r\n";
222 $o .= "</select>\r\n";
224 call_hooks($a->module . '_post_' . $selname, $o);
230 function fixacl(&$item) {
231 $item = intval(str_replace(array('<','>'),array('',''),$item));
234 function prune_deadguys($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 ");
243 $ret[] = intval($rr['id']);
250 function get_acl_permissions($user = null) {
251 $allow_cid = $allow_gid = $deny_cid = $deny_gid = false;
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');
268 $allow_cid = prune_deadguys($allow_cid);
271 'allow_cid' => $allow_cid,
272 'allow_gid' => $allow_gid,
273 'deny_cid' => $deny_cid,
274 'deny_gid' => $deny_gid,
279 function populate_acl($user = null,$celeb = false) {
281 $perms = get_acl_permissions($user);
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.
286 // $deny_cid = prune_deadguys($deny_cid);
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);
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);
303 $o .= '</div>' . "\r\n";
304 $o .= '<div id="acl-allow-end"></div>' . "\r\n";
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);
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);
318 $o .= '</div>' . "\r\n";
319 $o .= '<div id="acl-deny-end"></div>' . "\r\n";
321 $o .= '</div>' . "\r\n";
322 $o .= '<div id="acl-wrapper-end"></div>' . "\r\n";*/
324 // Not totally sure what input values can come
325 // This is a hotfix to prevent massive php warnings
326 if (is_array($user) AND isset($user["uid"]))
329 $uid = intval($user);
331 $tpl = get_markup_template("acl_selector.tpl");
332 $o = replace_macros($tpl, array(
333 '$showall'=> t("Visible to everybody"),
334 '$show' => t("show"),
335 '$hide' => t("don't show"),
336 '$allowcid' => json_encode($perms['allow_cid']),
337 '$allowgid' => json_encode($perms['allow_gid']),
338 '$denycid' => json_encode($perms['deny_cid']),
339 '$denygid' => json_encode($perms['deny_gid']),
340 '$features' => array(
341 "aclautomention"=>(feature_enabled($user,"aclautomention")?"true":"false")
350 function construct_acl_data(&$a, $user) {
352 // Get group and contact information for html ACL selector
353 $acl_data = acl_lookup($a, 'html');
355 $user_defaults = get_acl_permissions($user);
357 if($acl_data['groups']) {
358 foreach($acl_data['groups'] as $key=>$group) {
359 // Add a "selected" flag to groups that are posted to by default
360 if($user_defaults['allow_gid'] &&
361 in_array($group['id'], $user_defaults['allow_gid']) && !in_array($group['id'], $user_defaults['deny_gid']) )
362 $acl_data['groups'][$key]['selected'] = 1;
364 $acl_data['groups'][$key]['selected'] = 0;
367 if($acl_data['contacts']) {
368 foreach($acl_data['contacts'] as $key=>$contact) {
369 // Add a "selected" flag to groups that are posted to by default
370 if($user_defaults['allow_cid'] &&
371 in_array($contact['id'], $user_defaults['allow_cid']) && !in_array($contact['id'], $user_defaults['deny_cid']) )
372 $acl_data['contacts'][$key]['selected'] = 1;
374 $acl_data['contacts'][$key]['selected'] = 0;
382 function acl_lookup(&$a, $out_type = 'json') {
388 $start = (x($_REQUEST,'start')?$_REQUEST['start']:0);
389 $count = (x($_REQUEST,'count')?$_REQUEST['count']:100);
390 $search = (x($_REQUEST,'search')?$_REQUEST['search']:"");
391 $type = (x($_REQUEST,'type')?$_REQUEST['type']:"");
394 // For use with jquery.autocomplete for private mail completion
396 if(x($_REQUEST,'query') && strlen($_REQUEST['query'])) {
399 $search = $_REQUEST['query'];
404 $sql_extra = "AND `name` LIKE '%%".dbesc($search)."%%'";
405 $sql_extra2 = "AND (`attag` LIKE '%%".dbesc($search)."%%' OR `name` LIKE '%%".dbesc($search)."%%' OR `nick` LIKE '%%".dbesc($search)."%%')";
407 $sql_extra = $sql_extra2 = "";
410 // count groups and contacts
411 if ($type=='' || $type=='g'){
412 $r = q("SELECT COUNT(`id`) AS g FROM `group` WHERE `deleted` = 0 AND `uid` = %d $sql_extra",
415 $group_count = (int)$r[0]['g'];
420 if ($type=='' || $type=='c'){
421 $r = q("SELECT COUNT(`id`) AS c FROM `contact`
422 WHERE `uid` = %d AND `self` = 0
423 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0
424 AND `notify` != '' $sql_extra2" ,
427 $contact_count = (int)$r[0]['c'];
429 elseif ($type == 'm') {
431 // autocomplete for Private Messages
433 $r = q("SELECT COUNT(`id`) AS c FROM `contact`
434 WHERE `uid` = %d AND `self` = 0
435 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0
436 AND `network` IN ('%s','%s','%s') $sql_extra2" ,
437 intval(local_user()),
440 dbesc(NETWORK_DIASPORA)
442 $contact_count = (int)$r[0]['c'];
445 elseif ($type == 'a') {
447 // autocomplete for Contacts
449 $r = q("SELECT COUNT(`id`) AS c FROM `contact`
450 WHERE `uid` = %d AND `self` = 0
451 AND `pending` = 0 $sql_extra2" ,
454 $contact_count = (int)$r[0]['c'];
460 $tot = $group_count+$contact_count;
465 if ($type=='' || $type=='g'){
467 $r = q("SELECT `group`.`id`, `group`.`name`, GROUP_CONCAT(DISTINCT `group_member`.`contact-id` SEPARATOR ',') as uids
468 FROM `group`,`group_member`
469 WHERE `group`.`deleted` = 0 AND `group`.`uid` = %d
470 AND `group_member`.`gid`=`group`.`id`
472 GROUP BY `group`.`id`
473 ORDER BY `group`.`name`
475 intval(local_user()),
481 // logger('acl: group: ' . $g['name'] . ' members: ' . $g['uids']);
484 "photo" => "images/twopeople.png",
485 "name" => $g['name'],
486 "id" => intval($g['id']),
487 "uids" => array_map("intval", explode(",",$g['uids'])),
494 if ($type=='' || $type=='c'){
496 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, forum FROM `contact`
497 WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 AND `notify` != ''
499 ORDER BY `name` ASC ",
503 elseif($type == 'm') {
504 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag` FROM `contact`
505 WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0
506 AND `network` IN ('%s','%s','%s')
508 ORDER BY `name` ASC ",
509 intval(local_user()),
512 dbesc(NETWORK_DIASPORA)
515 elseif($type == 'a') {
516 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag` FROM `contact`
517 WHERE `uid` = %d AND `pending` = 0
519 ORDER BY `name` ASC ",
527 if($type == 'm' || $type == 'a') {
529 $x['query'] = $search;
530 $x['photos'] = array();
531 $x['links'] = array();
532 $x['suggestions'] = array();
533 $x['data'] = array();
536 $x['photos'][] = $g['micro'];
537 $x['links'][] = $g['url'];
538 $x['suggestions'][] = $g['name'];
539 $x['data'][] = intval($g['id']);
542 echo json_encode($x);
550 "photo" => $g['micro'],
551 "name" => $g['name'],
552 "id" => intval($g['id']),
553 "network" => $g['network'],
555 "nick" => ($g['attag']) ? $g['attag'] : $g['nick'],
556 "forum" => $g['forum']
561 $items = array_merge($groups, $contacts);
564 if($out_type === 'html') {
570 'contacts' => $contacts,
582 echo json_encode($o);