3 require_once("include/contact_selectors.php");
4 require_once("include/features.php");
5 require_once("mod/proxy.php");
12 * @package acl_selectors
14 function group_select($selname,$selclass,$preselected = false,$size = 4) {
20 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"$size\" >\r\n";
22 $r = q("SELECT * FROM `group` WHERE `deleted` = 0 AND `uid` = %d ORDER BY `name` ASC",
27 $arr = array('group' => $r, 'entry' => $o);
29 // e.g. 'network_pre_group_deny', 'profile_pre_group_allow'
31 call_hooks($a->module . '_pre_' . $selname, $arr);
35 if((is_array($preselected)) && in_array($rr['id'], $preselected))
36 $selected = " selected=\"selected\" ";
40 $trimmed = mb_substr($rr['name'],0,12);
42 $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}\" >$trimmed</option>\r\n";
46 $o .= "</select>\r\n";
48 call_hooks($a->module . '_post_' . $selname, $o);
55 function contact_selector($selname, $selclass, $preselected = false, $options) {
65 if(is_array($options)) {
66 if(x($options,'size'))
67 $size = $options['size'];
69 if(x($options,'mutual_friends'))
71 if(x($options,'single'))
73 if(x($options,'multiple'))
75 if(x($options,'exclude'))
76 $exclude = $options['exclude'];
78 if(x($options,'networks')) {
79 switch($options['networks']) {
81 $networks = array(NETWORK_DFRN);
84 if(is_array($a->user) && $a->user['prvnets'])
85 $networks = array(NETWORK_DFRN,NETWORK_MAIL,NETWORK_DIASPORA);
87 $networks = array(NETWORK_DFRN,NETWORK_FACEBOOK,NETWORK_MAIL, NETWORK_DIASPORA);
90 if(is_array($a->user) && $a->user['prvnets'])
91 $networks = array(NETWORK_DFRN,NETWORK_MAIL,NETWORK_DIASPORA);
93 $networks = array(NETWORK_DFRN,NETWORK_FACEBOOK,NETWORK_MAIL,NETWORK_DIASPORA,NETWORK_OSTATUS);
101 $x = array('options' => $options, 'size' => $size, 'single' => $single, 'mutual' => $mutual, 'exclude' => $exclude, 'networks' => $networks);
103 call_hooks('contact_select_options', $x);
110 $sql_extra .= sprintf(" AND `rel` = %d ", intval(CONTACT_IS_FRIEND));
113 if(intval($x['exclude']))
114 $sql_extra .= sprintf(" AND `id` != %d ", intval($x['exclude']));
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 ) ";
123 $tabindex = (x($options, 'tabindex') ? "tabindex=\"" . $options["tabindex"] . "\"" : "");
126 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"" . $x['size'] . "\" $tabindex >\r\n";
128 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"" . $x['size'] . "$\" $tabindex >\r\n";
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` != ''
133 ORDER BY `name` ASC ",
138 $arr = array('contact' => $r, 'entry' => $o);
140 // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
142 call_hooks($a->module . '_pre_' . $selname, $arr);
146 if((is_array($preselected)) && in_array($rr['id'], $preselected))
147 $selected = " selected=\"selected\" ";
151 $trimmed = mb_substr($rr['name'],0,20);
153 $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}|{$rr['url']}\" >$trimmed</option>\r\n";
158 $o .= "</select>\r\n";
160 call_hooks($a->module . '_post_' . $selname, $o);
167 function contact_select($selname, $selclass, $preselected = false, $size = 4, $privmail = false, $celeb = false, $privatenet = false, $tabindex = null) {
169 require_once("include/bbcode.php");
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.
180 if($privmail || $celeb) {
181 $sql_extra .= sprintf(" AND `rel` = %d ", intval(CONTACT_IS_FRIEND));
185 $sql_extra .= sprintf(" AND `network` IN ('%s' , '%s') ",
186 NETWORK_DFRN, NETWORK_DIASPORA);
188 $sql_extra .= sprintf(" AND `network` IN ('%s' , '%s', '%s', '%s') ",
189 NETWORK_DFRN, NETWORK_MAIL, NETWORK_FACEBOOK, NETWORK_DIASPORA);
191 $tabindex = ($tabindex > 0 ? "tabindex=\"$tabindex\"" : "");
194 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"$size\" $tabindex >\r\n";
196 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"$size\" $tabindex >\r\n";
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` != ''
201 ORDER BY `name` ASC ",
206 $arr = array('contact' => $r, 'entry' => $o);
208 // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
210 call_hooks($a->module . '_pre_' . $selname, $arr);
214 if((is_array($preselected)) && in_array($rr['id'], $preselected))
215 $selected = " selected=\"selected\" ";
220 $trimmed = GetProfileUsername($rr['url'], $rr['name'], false);
222 $trimmed = mb_substr($rr['name'],0,20);
224 $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}|{$rr['url']}\" >$trimmed</option>\r\n";
229 $o .= "</select>\r\n";
231 call_hooks($a->module . '_post_' . $selname, $o);
237 function fixacl(&$item) {
238 $item = intval(str_replace(array('<','>'),array('',''),$item));
241 function prune_deadguys($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 ");
250 $ret[] = intval($rr['id']);
257 function get_acl_permissions($user = null) {
258 $allow_cid = $allow_gid = $deny_cid = $deny_gid = false;
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');
275 $allow_cid = prune_deadguys($allow_cid);
278 'allow_cid' => $allow_cid,
279 'allow_gid' => $allow_gid,
280 'deny_cid' => $deny_cid,
281 'deny_gid' => $deny_gid,
286 function populate_acl($user = null,$celeb = false,$show_jotnets = false) {
288 $perms = get_acl_permissions($user);
292 $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
294 $mail_enabled = false;
295 $pubmail_enabled = false;
297 if(! $mail_disabled) {
298 $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1",
302 $mail_enabled = true;
303 if(intval($r[0]['pubmail']))
304 $pubmail_enabled = true;
308 if (!$user['hidewall']) {
310 $selected = (($pubmail_enabled) ? ' checked="checked" ' : '');
311 $jotnets .= '<div class="profile-jot-net"><input type="checkbox" name="pubmail_enable"' . $selected . ' value="1" /> ' . t("Post to Email") . '</div>';
314 call_hooks('jot_networks', $jotnets);
316 $jotnets .= sprintf(t('Connectors disabled, since "%s" is enabled.'),
317 t('Hide your profile details from unknown viewers?'));
320 // We shouldn't need to prune deadguys from the block list. Either way they can't get the message.
321 // Also no point enumerating groups and checking them, that will take place on delivery.
323 // $deny_cid = prune_deadguys($deny_cid);
327 $o .= '<div id="acl-wrapper">';
328 $o .= '<div id="acl-permit-outer-wrapper">';
329 $o .= '<div id="acl-permit-text">' . t('Visible To:') . '</div><div id="jot-public">' . t('everybody') . '</div>';
330 $o .= '<div id="acl-permit-text-end"></div>';
331 $o .= '<div id="acl-permit-wrapper">';
332 $o .= '<div id="group_allow_wrapper">';
333 $o .= '<label id="acl-allow-group-label" for="group_allow" >' . t('Groups') . '</label>';
334 $o .= group_select('group_allow','group_allow',$allow_gid);
336 $o .= '<div id="contact_allow_wrapper">';
337 $o .= '<label id="acl-allow-contact-label" for="contact_allow" >' . t('Contacts') . '</label>';
338 $o .= contact_select('contact_allow','contact_allow',$allow_cid,4,false,$celeb,true);
340 $o .= '</div>' . "\r\n";
341 $o .= '<div id="acl-allow-end"></div>' . "\r\n";
343 $o .= '<div id="acl-deny-outer-wrapper">';
344 $o .= '<div id="acl-deny-text">' . t('Except For:') . '</div>';
345 $o .= '<div id="acl-deny-text-end"></div>';
346 $o .= '<div id="acl-deny-wrapper">';
347 $o .= '<div id="group_deny_wrapper" >';
348 $o .= '<label id="acl-deny-group-label" for="group_deny" >' . t('Groups') . '</label>';
349 $o .= group_select('group_deny','group_deny', $deny_gid);
351 $o .= '<div id="contact_deny_wrapper" >';
352 $o .= '<label id="acl-deny-contact-label" for="contact_deny" >' . t('Contacts') . '</label>';
353 $o .= contact_select('contact_deny','contact_deny', $deny_cid,4,false, $celeb,true);
355 $o .= '</div>' . "\r\n";
356 $o .= '<div id="acl-deny-end"></div>' . "\r\n";
358 $o .= '</div>' . "\r\n";
359 $o .= '<div id="acl-wrapper-end"></div>' . "\r\n";*/
361 $tpl = get_markup_template("acl_selector.tpl");
362 $o = replace_macros($tpl, array(
363 '$showall'=> t("Visible to everybody"),
364 '$show' => t("show"),
365 '$hide' => t("don't show"),
366 '$allowcid' => json_encode($perms['allow_cid']),
367 '$allowgid' => json_encode($perms['allow_gid']),
368 '$denycid' => json_encode($perms['deny_cid']),
369 '$denygid' => json_encode($perms['deny_gid']),
370 '$networks' => $show_jotnets,
371 '$emailcc' => t('CC: email addresses'),
372 '$emtitle' => t('Example: bob@example.com, mary@example.com'),
373 '$jotnets' => $jotnets,
374 '$aclModalTitle' => t('Permissions'),
375 '$aclModalDismiss' => t('Close'),
376 '$features' => array(
377 "aclautomention"=>(feature_enabled($user['uid'],"aclautomention")?"true":"false")
386 function construct_acl_data(&$a, $user) {
388 // Get group and contact information for html ACL selector
389 $acl_data = acl_lookup($a, 'html');
391 $user_defaults = get_acl_permissions($user);
393 if($acl_data['groups']) {
394 foreach($acl_data['groups'] as $key=>$group) {
395 // Add a "selected" flag to groups that are posted to by default
396 if($user_defaults['allow_gid'] &&
397 in_array($group['id'], $user_defaults['allow_gid']) && !in_array($group['id'], $user_defaults['deny_gid']) )
398 $acl_data['groups'][$key]['selected'] = 1;
400 $acl_data['groups'][$key]['selected'] = 0;
403 if($acl_data['contacts']) {
404 foreach($acl_data['contacts'] as $key=>$contact) {
405 // Add a "selected" flag to groups that are posted to by default
406 if($user_defaults['allow_cid'] &&
407 in_array($contact['id'], $user_defaults['allow_cid']) && !in_array($contact['id'], $user_defaults['deny_cid']) )
408 $acl_data['contacts'][$key]['selected'] = 1;
410 $acl_data['contacts'][$key]['selected'] = 0;
418 function acl_lookup(&$a, $out_type = 'json') {
424 $start = (x($_REQUEST,'start')?$_REQUEST['start']:0);
425 $count = (x($_REQUEST,'count')?$_REQUEST['count']:100);
426 $search = (x($_REQUEST,'search')?$_REQUEST['search']:"");
427 $type = (x($_REQUEST,'type')?$_REQUEST['type']:"");
428 $conv_id = (x($_REQUEST,'conversation')?$_REQUEST['conversation']:null);
430 // For use with jquery.autocomplete for private mail completion
432 if(x($_REQUEST,'query') && strlen($_REQUEST['query'])) {
435 $search = $_REQUEST['query'];
440 $sql_extra = "AND `name` LIKE '%%".dbesc($search)."%%'";
441 $sql_extra2 = "AND (`attag` LIKE '%%".dbesc($search)."%%' OR `name` LIKE '%%".dbesc($search)."%%' OR `nick` LIKE '%%".dbesc($search)."%%')";
443 $sql_extra = $sql_extra2 = "";
446 // count groups and contacts
447 if ($type=='' || $type=='g'){
448 $r = q("SELECT COUNT(*) AS g FROM `group` WHERE `deleted` = 0 AND `uid` = %d $sql_extra",
451 $group_count = (int)$r[0]['g'];
456 if ($type=='' || $type=='c'){
457 $r = q("SELECT COUNT(*) AS c FROM `contact`
458 WHERE `uid` = %d AND `self` = 0
459 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0
460 AND `notify` != '' $sql_extra2" ,
463 $contact_count = (int)$r[0]['c'];
465 elseif ($type == 'm') {
467 // autocomplete for Private Messages
469 $r = q("SELECT COUNT(*) AS c FROM `contact`
470 WHERE `uid` = %d AND `self` = 0
471 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0
472 AND `network` IN ('%s','%s','%s') $sql_extra2" ,
473 intval(local_user()),
476 dbesc(NETWORK_DIASPORA)
478 $contact_count = (int)$r[0]['c'];
481 elseif ($type == 'a') {
483 // autocomplete for Contacts
485 $r = q("SELECT COUNT(*) AS c FROM `contact`
486 WHERE `uid` = %d AND `self` = 0
487 AND `pending` = 0 $sql_extra2" ,
490 $contact_count = (int)$r[0]['c'];
497 $tot = $group_count+$contact_count;
502 if ($type=='' || $type=='g'){
504 $r = q("SELECT `group`.`id`, `group`.`name`, GROUP_CONCAT(DISTINCT `group_member`.`contact-id` SEPARATOR ',') as uids
505 FROM `group`,`group_member`
506 WHERE `group`.`deleted` = 0 AND `group`.`uid` = %d
507 AND `group_member`.`gid`=`group`.`id`
509 GROUP BY `group`.`id`
510 ORDER BY `group`.`name`
512 intval(local_user()),
518 // logger('acl: group: ' . $g['name'] . ' members: ' . $g['uids']);
521 "photo" => "images/twopeople.png",
522 "name" => $g['name'],
523 "id" => intval($g['id']),
524 "uids" => array_map("intval", explode(",",$g['uids'])),
531 if ($type=='' || $type=='c'){
533 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, forum FROM `contact`
534 WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 AND `notify` != ''
536 ORDER BY `name` ASC ",
540 elseif($type == 'm') {
541 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag` FROM `contact`
542 WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0
543 AND `network` IN ('%s','%s','%s')
545 ORDER BY `name` ASC ",
546 intval(local_user()),
549 dbesc(NETWORK_DIASPORA)
552 elseif($type == 'a') {
553 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag` FROM `contact`
554 WHERE `uid` = %d AND `pending` = 0
556 ORDER BY `name` ASC ",
564 if($type == 'm' || $type == 'a') {
566 $x['query'] = $search;
567 $x['photos'] = array();
568 $x['links'] = array();
569 $x['suggestions'] = array();
570 $x['data'] = array();
573 $x['photos'][] = proxy_url($g['micro']);
574 $x['links'][] = $g['url'];
575 $x['suggestions'][] = $g['name'];
576 $x['data'][] = intval($g['id']);
579 echo json_encode($x);
587 "photo" => proxy_url($g['micro']),
588 "name" => $g['name'],
589 "id" => intval($g['id']),
590 "network" => $g['network'],
592 "nick" => ($g['attag']) ? $g['attag'] : $g['nick'],
593 "forum" => $g['forum']
598 $items = array_merge($groups, $contacts);
601 /* if $conv_id is set, get unknow contacts in thread */
602 /* but first get know contacts url to filter them out */
603 function _contact_link($i){ return dbesc($i['link']); }
604 $known_contacts = array_map(_contact_link, $contacts);
605 $unknow_contacts=array();
607 `author-avatar`,`author-name`,`author-link`
608 from item where parent=%d
610 `author-name` LIKE '%%%s%%' OR
611 `author-link` LIKE '%%%s%%'
613 `author-link` NOT IN ('%s')
614 GROUP BY `author-link`
615 ORDER BY `author-name` ASC
620 implode("','", $known_contacts)
622 if (is_array($r) && count($r)){
623 foreach($r as $row) {
625 $up = parse_url($row['author-link']);
626 $nick = explode("/",$up['path']);
627 $nick = $nick[count($nick)-1];
628 $nick .= "@".$up['host'];
630 $unknow_contacts[] = array(
632 "photo" => proxy_url($row['author-avatar']),
633 "name" => $row['author-name'],
635 "network" => "unknown",
636 "link" => $row['author-link'],
643 $items = array_merge($items, $unknow_contacts);
644 $tot += count($unknow_contacts);
647 if($out_type === 'html') {
653 'contacts' => $contacts,
665 echo json_encode($o);