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\" ";
39 $trimmed = mb_substr($rr['name'],0,12);
41 $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}\" >$trimmed</option>\r\n";
45 $o .= "</select>\r\n";
47 call_hooks($a->module . '_post_' . $selname, $o);
54 function contact_selector($selname, $selclass, $preselected = false, $options) {
64 if(is_array($options)) {
65 if(x($options,'size'))
66 $size = $options['size'];
68 if(x($options,'mutual_friends'))
70 if(x($options,'single'))
72 if(x($options,'multiple'))
74 if(x($options,'exclude'))
75 $exclude = $options['exclude'];
77 if(x($options,'networks')) {
78 switch($options['networks']) {
80 $networks = array('dfrn');
83 if(is_array($a->user) && $a->user['prvnets'])
84 $networks = array('dfrn','mail','dspr');
86 $networks = array('dfrn','face','mail', 'dspr');
89 if(is_array($a->user) && $a->user['prvnets'])
90 $networks = array('dfrn','mail','dspr');
92 $networks = array('dfrn','face','mail','dspr','stat');
100 $x = array('options' => $options, 'size' => $size, 'single' => $single, 'mutual' => $mutual, 'exclude' => $exclude, 'networks' => $networks);
102 call_hooks('contact_select_options', $x);
109 $sql_extra .= sprintf(" AND `rel` = %d ", intval(CONTACT_IS_FRIEND));
112 if(intval($x['exclude']))
113 $sql_extra .= sprintf(" AND `id` != %d ", intval($x['exclude']));
115 if(is_array($x['networks']) && count($x['networks'])) {
116 for($y = 0; $y < count($x['networks']) ; $y ++)
117 $x['networks'][$y] = "'" . dbesc($x['networks'][$y]) . "'";
118 $str_nets = implode(',',$x['networks']);
119 $sql_extra .= " AND `network` IN ( $str_nets ) ";
122 $tabindex = (x($options, 'tabindex') ? "tabindex=\"" . $options["tabindex"] . "\"" : "");
125 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"" . $x['size'] . "\" $tabindex >\r\n";
127 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"" . $x['size'] . "$\" $tabindex >\r\n";
129 $r = q("SELECT `id`, `name`, `url`, `network` FROM `contact`
130 WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 AND `notify` != ''
132 ORDER BY `name` ASC ",
137 $arr = array('contact' => $r, 'entry' => $o);
139 // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
141 call_hooks($a->module . '_pre_' . $selname, $arr);
145 if((is_array($preselected)) && in_array($rr['id'], $preselected))
146 $selected = " selected=\"selected\" ";
150 $trimmed = mb_substr($rr['name'],0,20);
152 $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}|{$rr['url']}\" >$trimmed</option>\r\n";
157 $o .= "</select>\r\n";
159 call_hooks($a->module . '_post_' . $selname, $o);
166 function contact_select($selname, $selclass, $preselected = false, $size = 4, $privmail = false, $celeb = false, $privatenet = false, $tabindex = null) {
168 require_once("include/bbcode.php");
174 // When used for private messages, we limit correspondence to mutual DFRN/Friendica friends and the selector
175 // to one recipient. By default our selector allows multiple selects amongst all contacts.
179 if($privmail || $celeb) {
180 $sql_extra .= sprintf(" AND `rel` = %d ", intval(CONTACT_IS_FRIEND));
184 $sql_extra .= " AND `network` IN ( 'dfrn', 'dspr' ) ";
186 elseif($privatenet) {
187 $sql_extra .= " AND `network` IN ( 'dfrn', 'mail', 'face', 'dspr' ) ";
190 $tabindex = ($tabindex > 0 ? "tabindex=\"$tabindex\"" : "");
193 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"$size\" $tabindex >\r\n";
195 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"$size\" $tabindex >\r\n";
197 $r = q("SELECT `id`, `name`, `url`, `network` FROM `contact`
198 WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 AND `notify` != ''
200 ORDER BY `name` ASC ",
205 $arr = array('contact' => $r, 'entry' => $o);
207 // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
209 call_hooks($a->module . '_pre_' . $selname, $arr);
213 if((is_array($preselected)) && in_array($rr['id'], $preselected))
214 $selected = " selected=\"selected\" ";
219 $trimmed = GetProfileUsername($rr['url'], $rr['name'], false);
221 $trimmed = mb_substr($rr['name'],0,20);
223 $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}|{$rr['url']}\" >$trimmed</option>\r\n";
228 $o .= "</select>\r\n";
230 call_hooks($a->module . '_post_' . $selname, $o);
236 function fixacl(&$item) {
237 $item = intval(str_replace(array('<','>'),array('',''),$item));
240 function prune_deadguys($arr) {
244 $str = dbesc(implode(',',$arr));
245 $r = q("select id from contact where id in ( " . $str . ") and blocked = 0 and pending = 0 and archive = 0 ");
249 $ret[] = intval($rr['id']);
256 function get_acl_permissions($user = null) {
257 $allow_cid = $allow_gid = $deny_cid = $deny_gid = false;
259 if(is_array($user)) {
260 $allow_cid = ((strlen($user['allow_cid']))
261 ? explode('><', $user['allow_cid']) : array() );
262 $allow_gid = ((strlen($user['allow_gid']))
263 ? explode('><', $user['allow_gid']) : array() );
264 $deny_cid = ((strlen($user['deny_cid']))
265 ? explode('><', $user['deny_cid']) : array() );
266 $deny_gid = ((strlen($user['deny_gid']))
267 ? explode('><', $user['deny_gid']) : array() );
268 array_walk($allow_cid,'fixacl');
269 array_walk($allow_gid,'fixacl');
270 array_walk($deny_cid,'fixacl');
271 array_walk($deny_gid,'fixacl');
274 $allow_cid = prune_deadguys($allow_cid);
277 'allow_cid' => $allow_cid,
278 'allow_gid' => $allow_gid,
279 'deny_cid' => $deny_cid,
280 'deny_gid' => $deny_gid,
285 function populate_acl($user = null,$celeb = false) {
287 $perms = get_acl_permissions($user);
289 // We shouldn't need to prune deadguys from the block list. Either way they can't get the message.
290 // Also no point enumerating groups and checking them, that will take place on delivery.
292 // $deny_cid = prune_deadguys($deny_cid);
296 $o .= '<div id="acl-wrapper">';
297 $o .= '<div id="acl-permit-outer-wrapper">';
298 $o .= '<div id="acl-permit-text">' . t('Visible To:') . '</div><div id="jot-public">' . t('everybody') . '</div>';
299 $o .= '<div id="acl-permit-text-end"></div>';
300 $o .= '<div id="acl-permit-wrapper">';
301 $o .= '<div id="group_allow_wrapper">';
302 $o .= '<label id="acl-allow-group-label" for="group_allow" >' . t('Groups') . '</label>';
303 $o .= group_select('group_allow','group_allow',$allow_gid);
305 $o .= '<div id="contact_allow_wrapper">';
306 $o .= '<label id="acl-allow-contact-label" for="contact_allow" >' . t('Contacts') . '</label>';
307 $o .= contact_select('contact_allow','contact_allow',$allow_cid,4,false,$celeb,true);
309 $o .= '</div>' . "\r\n";
310 $o .= '<div id="acl-allow-end"></div>' . "\r\n";
312 $o .= '<div id="acl-deny-outer-wrapper">';
313 $o .= '<div id="acl-deny-text">' . t('Except For:') . '</div>';
314 $o .= '<div id="acl-deny-text-end"></div>';
315 $o .= '<div id="acl-deny-wrapper">';
316 $o .= '<div id="group_deny_wrapper" >';
317 $o .= '<label id="acl-deny-group-label" for="group_deny" >' . t('Groups') . '</label>';
318 $o .= group_select('group_deny','group_deny', $deny_gid);
320 $o .= '<div id="contact_deny_wrapper" >';
321 $o .= '<label id="acl-deny-contact-label" for="contact_deny" >' . t('Contacts') . '</label>';
322 $o .= contact_select('contact_deny','contact_deny', $deny_cid,4,false, $celeb,true);
324 $o .= '</div>' . "\r\n";
325 $o .= '<div id="acl-deny-end"></div>' . "\r\n";
327 $o .= '</div>' . "\r\n";
328 $o .= '<div id="acl-wrapper-end"></div>' . "\r\n";*/
330 $tpl = get_markup_template("acl_selector.tpl");
331 $o = replace_macros($tpl, array(
332 '$showall'=> t("Visible to everybody"),
333 '$show' => t("show"),
334 '$hide' => t("don't show"),
335 '$allowcid' => json_encode($perms['allow_cid']),
336 '$allowgid' => json_encode($perms['allow_gid']),
337 '$denycid' => json_encode($perms['deny_cid']),
338 '$denygid' => json_encode($perms['deny_gid']),
339 '$features' => array(
340 "aclautomention"=>(feature_enabled($user['uid'],"aclautomention")?"true":"false")
349 function construct_acl_data(&$a, $user) {
351 // Get group and contact information for html ACL selector
352 $acl_data = acl_lookup($a, 'html');
354 $user_defaults = get_acl_permissions($user);
356 if($acl_data['groups']) {
357 foreach($acl_data['groups'] as $key=>$group) {
358 // Add a "selected" flag to groups that are posted to by default
359 if($user_defaults['allow_gid'] &&
360 in_array($group['id'], $user_defaults['allow_gid']) && !in_array($group['id'], $user_defaults['deny_gid']) )
361 $acl_data['groups'][$key]['selected'] = 1;
363 $acl_data['groups'][$key]['selected'] = 0;
366 if($acl_data['contacts']) {
367 foreach($acl_data['contacts'] as $key=>$contact) {
368 // Add a "selected" flag to groups that are posted to by default
369 if($user_defaults['allow_cid'] &&
370 in_array($contact['id'], $user_defaults['allow_cid']) && !in_array($contact['id'], $user_defaults['deny_cid']) )
371 $acl_data['contacts'][$key]['selected'] = 1;
373 $acl_data['contacts'][$key]['selected'] = 0;
381 function acl_lookup(&$a, $out_type = 'json') {
387 $start = (x($_REQUEST,'start')?$_REQUEST['start']:0);
388 $count = (x($_REQUEST,'count')?$_REQUEST['count']:100);
389 $search = (x($_REQUEST,'search')?$_REQUEST['search']:"");
390 $type = (x($_REQUEST,'type')?$_REQUEST['type']:"");
391 $conv_id = (x($_REQUEST,'conversation')?$_REQUEST['conversation']:null);
393 // For use with jquery.autocomplete for private mail completion
395 if(x($_REQUEST,'query') && strlen($_REQUEST['query'])) {
398 $search = $_REQUEST['query'];
403 $sql_extra = "AND `name` LIKE '%%".dbesc($search)."%%'";
404 $sql_extra2 = "AND (`attag` LIKE '%%".dbesc($search)."%%' OR `name` LIKE '%%".dbesc($search)."%%' OR `nick` LIKE '%%".dbesc($search)."%%')";
406 $sql_extra = $sql_extra2 = "";
409 // count groups and contacts
410 if ($type=='' || $type=='g'){
411 $r = q("SELECT COUNT(*) AS g FROM `group` WHERE `deleted` = 0 AND `uid` = %d $sql_extra",
414 $group_count = (int)$r[0]['g'];
419 if ($type=='' || $type=='c'){
420 $r = q("SELECT COUNT(*) AS c FROM `contact`
421 WHERE `uid` = %d AND `self` = 0
422 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0
423 AND `notify` != '' $sql_extra2" ,
426 $contact_count = (int)$r[0]['c'];
428 elseif ($type == 'm') {
430 // autocomplete for Private Messages
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 `network` IN ('%s','%s','%s') $sql_extra2" ,
436 intval(local_user()),
439 dbesc(NETWORK_DIASPORA)
441 $contact_count = (int)$r[0]['c'];
444 elseif ($type == 'a') {
446 // autocomplete for Contacts
448 $r = q("SELECT COUNT(*) AS c FROM `contact`
449 WHERE `uid` = %d AND `self` = 0
450 AND `pending` = 0 $sql_extra2" ,
453 $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 $conv_id is set, get unknow contacts in thread */
565 /* but first get know contacts url to filter them out */
566 function _contact_link($i){ return dbesc($i['link']); }
567 $known_contacts = array_map(_contact_link, $contacts);
568 $unknow_contacts=array();
570 `author-avatar`,`author-name`,`author-link`
571 from item where parent=%d
573 `author-name` LIKE '%%%s%%' OR
574 `author-link` LIKE '%%%s%%'
576 `author-link` NOT IN ('%s')
577 GROUP BY `author-link`
578 ORDER BY `author-name` ASC
583 implode("','", $known_contacts)
585 if (is_array($r) && count($r)){
586 foreach($r as $row) {
588 $up = parse_url($row['author-link']);
589 $nick = explode("/",$up['path']);
590 $nick = $nick[count($nick)-1];
591 $nick .= "@".$up['host'];
593 $unknow_contacts[] = array(
595 "photo" => $row['author-avatar'],
596 "name" => $row['author-name'],
598 "network" => "unknown",
599 "link" => $row['author-link'],
606 $items = array_merge($items, $unknow_contacts);
607 $tot += count($unknow_contacts);
610 if($out_type === 'html') {
616 'contacts' => $contacts,
628 echo json_encode($o);