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;
309 $selected = (($pubmail_enabled) ? ' checked="checked" ' : '');
310 $jotnets .= '<div class="profile-jot-net"><input type="checkbox" name="pubmail_enable"' . $selected . ' value="1" /> '
311 . t("Post to Email") . '</div>';
314 call_hooks('jot_networks', $jotnets);
317 // We shouldn't need to prune deadguys from the block list. Either way they can't get the message.
318 // Also no point enumerating groups and checking them, that will take place on delivery.
320 // $deny_cid = prune_deadguys($deny_cid);
324 $o .= '<div id="acl-wrapper">';
325 $o .= '<div id="acl-permit-outer-wrapper">';
326 $o .= '<div id="acl-permit-text">' . t('Visible To:') . '</div><div id="jot-public">' . t('everybody') . '</div>';
327 $o .= '<div id="acl-permit-text-end"></div>';
328 $o .= '<div id="acl-permit-wrapper">';
329 $o .= '<div id="group_allow_wrapper">';
330 $o .= '<label id="acl-allow-group-label" for="group_allow" >' . t('Groups') . '</label>';
331 $o .= group_select('group_allow','group_allow',$allow_gid);
333 $o .= '<div id="contact_allow_wrapper">';
334 $o .= '<label id="acl-allow-contact-label" for="contact_allow" >' . t('Contacts') . '</label>';
335 $o .= contact_select('contact_allow','contact_allow',$allow_cid,4,false,$celeb,true);
337 $o .= '</div>' . "\r\n";
338 $o .= '<div id="acl-allow-end"></div>' . "\r\n";
340 $o .= '<div id="acl-deny-outer-wrapper">';
341 $o .= '<div id="acl-deny-text">' . t('Except For:') . '</div>';
342 $o .= '<div id="acl-deny-text-end"></div>';
343 $o .= '<div id="acl-deny-wrapper">';
344 $o .= '<div id="group_deny_wrapper" >';
345 $o .= '<label id="acl-deny-group-label" for="group_deny" >' . t('Groups') . '</label>';
346 $o .= group_select('group_deny','group_deny', $deny_gid);
348 $o .= '<div id="contact_deny_wrapper" >';
349 $o .= '<label id="acl-deny-contact-label" for="contact_deny" >' . t('Contacts') . '</label>';
350 $o .= contact_select('contact_deny','contact_deny', $deny_cid,4,false, $celeb,true);
352 $o .= '</div>' . "\r\n";
353 $o .= '<div id="acl-deny-end"></div>' . "\r\n";
355 $o .= '</div>' . "\r\n";
356 $o .= '<div id="acl-wrapper-end"></div>' . "\r\n";*/
358 $tpl = get_markup_template("acl_selector.tpl");
359 $o = replace_macros($tpl, array(
360 '$showall'=> t("Visible to everybody"),
361 '$show' => t("show"),
362 '$hide' => t("don't show"),
363 '$allowcid' => json_encode($perms['allow_cid']),
364 '$allowgid' => json_encode($perms['allow_gid']),
365 '$denycid' => json_encode($perms['deny_cid']),
366 '$denygid' => json_encode($perms['deny_gid']),
367 '$networks' => $show_jotnets,
368 '$emailcc' => t('CC: email addresses'),
369 '$emtitle' => t('Example: bob@example.com, mary@example.com'),
370 '$jotnets' => $jotnets,
371 '$aclModalTitle' => t('Permissions'),
372 '$aclModalDismiss' => t('Close'),
373 '$features' => array(
374 "aclautomention"=>(feature_enabled($user['uid'],"aclautomention")?"true":"false")
383 function construct_acl_data(&$a, $user) {
385 // Get group and contact information for html ACL selector
386 $acl_data = acl_lookup($a, 'html');
388 $user_defaults = get_acl_permissions($user);
390 if($acl_data['groups']) {
391 foreach($acl_data['groups'] as $key=>$group) {
392 // Add a "selected" flag to groups that are posted to by default
393 if($user_defaults['allow_gid'] &&
394 in_array($group['id'], $user_defaults['allow_gid']) && !in_array($group['id'], $user_defaults['deny_gid']) )
395 $acl_data['groups'][$key]['selected'] = 1;
397 $acl_data['groups'][$key]['selected'] = 0;
400 if($acl_data['contacts']) {
401 foreach($acl_data['contacts'] as $key=>$contact) {
402 // Add a "selected" flag to groups that are posted to by default
403 if($user_defaults['allow_cid'] &&
404 in_array($contact['id'], $user_defaults['allow_cid']) && !in_array($contact['id'], $user_defaults['deny_cid']) )
405 $acl_data['contacts'][$key]['selected'] = 1;
407 $acl_data['contacts'][$key]['selected'] = 0;
415 function acl_lookup(&$a, $out_type = 'json') {
421 $start = (x($_REQUEST,'start')?$_REQUEST['start']:0);
422 $count = (x($_REQUEST,'count')?$_REQUEST['count']:100);
423 $search = (x($_REQUEST,'search')?$_REQUEST['search']:"");
424 $type = (x($_REQUEST,'type')?$_REQUEST['type']:"");
425 $conv_id = (x($_REQUEST,'conversation')?$_REQUEST['conversation']:null);
427 // For use with jquery.autocomplete for private mail completion
429 if(x($_REQUEST,'query') && strlen($_REQUEST['query'])) {
432 $search = $_REQUEST['query'];
437 $sql_extra = "AND `name` LIKE '%%".dbesc($search)."%%'";
438 $sql_extra2 = "AND (`attag` LIKE '%%".dbesc($search)."%%' OR `name` LIKE '%%".dbesc($search)."%%' OR `nick` LIKE '%%".dbesc($search)."%%')";
440 $sql_extra = $sql_extra2 = "";
443 // count groups and contacts
444 if ($type=='' || $type=='g'){
445 $r = q("SELECT COUNT(*) AS g FROM `group` WHERE `deleted` = 0 AND `uid` = %d $sql_extra",
448 $group_count = (int)$r[0]['g'];
453 if ($type=='' || $type=='c'){
454 $r = q("SELECT COUNT(*) AS c FROM `contact`
455 WHERE `uid` = %d AND `self` = 0
456 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0
457 AND `notify` != '' $sql_extra2" ,
460 $contact_count = (int)$r[0]['c'];
462 elseif ($type == 'm') {
464 // autocomplete for Private Messages
466 $r = q("SELECT COUNT(*) AS c FROM `contact`
467 WHERE `uid` = %d AND `self` = 0
468 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0
469 AND `network` IN ('%s','%s','%s') $sql_extra2" ,
470 intval(local_user()),
473 dbesc(NETWORK_DIASPORA)
475 $contact_count = (int)$r[0]['c'];
478 elseif ($type == 'a') {
480 // autocomplete for Contacts
482 $r = q("SELECT COUNT(*) AS c FROM `contact`
483 WHERE `uid` = %d AND `self` = 0
484 AND `pending` = 0 $sql_extra2" ,
487 $contact_count = (int)$r[0]['c'];
494 $tot = $group_count+$contact_count;
499 if ($type=='' || $type=='g'){
501 $r = q("SELECT `group`.`id`, `group`.`name`, GROUP_CONCAT(DISTINCT `group_member`.`contact-id` SEPARATOR ',') as uids
502 FROM `group`,`group_member`
503 WHERE `group`.`deleted` = 0 AND `group`.`uid` = %d
504 AND `group_member`.`gid`=`group`.`id`
506 GROUP BY `group`.`id`
507 ORDER BY `group`.`name`
509 intval(local_user()),
515 // logger('acl: group: ' . $g['name'] . ' members: ' . $g['uids']);
518 "photo" => "images/twopeople.png",
519 "name" => $g['name'],
520 "id" => intval($g['id']),
521 "uids" => array_map("intval", explode(",",$g['uids'])),
528 if ($type=='' || $type=='c'){
530 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, forum FROM `contact`
531 WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 AND `notify` != ''
533 ORDER BY `name` ASC ",
537 elseif($type == 'm') {
538 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag` FROM `contact`
539 WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0
540 AND `network` IN ('%s','%s','%s')
542 ORDER BY `name` ASC ",
543 intval(local_user()),
546 dbesc(NETWORK_DIASPORA)
549 elseif($type == 'a') {
550 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag` FROM `contact`
551 WHERE `uid` = %d AND `pending` = 0
553 ORDER BY `name` ASC ",
561 if($type == 'm' || $type == 'a') {
563 $x['query'] = $search;
564 $x['photos'] = array();
565 $x['links'] = array();
566 $x['suggestions'] = array();
567 $x['data'] = array();
570 $x['photos'][] = proxy_url($g['micro']);
571 $x['links'][] = $g['url'];
572 $x['suggestions'][] = $g['name'];
573 $x['data'][] = intval($g['id']);
576 echo json_encode($x);
584 "photo" => proxy_url($g['micro']),
585 "name" => $g['name'],
586 "id" => intval($g['id']),
587 "network" => $g['network'],
589 "nick" => ($g['attag']) ? $g['attag'] : $g['nick'],
590 "forum" => $g['forum']
595 $items = array_merge($groups, $contacts);
598 /* if $conv_id is set, get unknow contacts in thread */
599 /* but first get know contacts url to filter them out */
600 function _contact_link($i){ return dbesc($i['link']); }
601 $known_contacts = array_map(_contact_link, $contacts);
602 $unknow_contacts=array();
604 `author-avatar`,`author-name`,`author-link`
605 from item where parent=%d
607 `author-name` LIKE '%%%s%%' OR
608 `author-link` LIKE '%%%s%%'
610 `author-link` NOT IN ('%s')
611 GROUP BY `author-link`
612 ORDER BY `author-name` ASC
617 implode("','", $known_contacts)
619 if (is_array($r) && count($r)){
620 foreach($r as $row) {
622 $up = parse_url($row['author-link']);
623 $nick = explode("/",$up['path']);
624 $nick = $nick[count($nick)-1];
625 $nick .= "@".$up['host'];
627 $unknow_contacts[] = array(
629 "photo" => proxy_url($row['author-avatar']),
630 "name" => $row['author-name'],
632 "network" => "unknown",
633 "link" => $row['author-link'],
640 $items = array_merge($items, $unknow_contacts);
641 $tot += count($unknow_contacts);
644 if($out_type === 'html') {
650 'contacts' => $contacts,
662 echo json_encode($o);