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, $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 $tpl = get_markup_template("acl_selector.tpl");
321 $o = replace_macros($tpl, array(
322 '$showall'=> t("Visible to everybody"),
323 '$show' => t("show"),
324 '$hide' => t("don't show"),
325 '$allowcid' => json_encode($perms['allow_cid']),
326 '$allowgid' => json_encode($perms['allow_gid']),
327 '$denycid' => json_encode($perms['deny_cid']),
328 '$denygid' => json_encode($perms['deny_gid']),
329 '$networks' => $show_jotnets,
330 '$emailcc' => t('CC: email addresses'),
331 '$emtitle' => t('Example: bob@example.com, mary@example.com'),
332 '$jotnets' => $jotnets,
333 '$aclModalTitle' => t('Permissions'),
334 '$aclModalDismiss' => t('Close'),
335 '$features' => array(
336 "aclautomention"=>(feature_enabled($user['uid'],"aclautomention")?"true":"false")
345 function construct_acl_data(&$a, $user) {
347 // Get group and contact information for html ACL selector
348 $acl_data = acl_lookup($a, 'html');
350 $user_defaults = get_acl_permissions($user);
352 if($acl_data['groups']) {
353 foreach($acl_data['groups'] as $key=>$group) {
354 // Add a "selected" flag to groups that are posted to by default
355 if($user_defaults['allow_gid'] &&
356 in_array($group['id'], $user_defaults['allow_gid']) && !in_array($group['id'], $user_defaults['deny_gid']) )
357 $acl_data['groups'][$key]['selected'] = 1;
359 $acl_data['groups'][$key]['selected'] = 0;
362 if($acl_data['contacts']) {
363 foreach($acl_data['contacts'] as $key=>$contact) {
364 // Add a "selected" flag to groups that are posted to by default
365 if($user_defaults['allow_cid'] &&
366 in_array($contact['id'], $user_defaults['allow_cid']) && !in_array($contact['id'], $user_defaults['deny_cid']) )
367 $acl_data['contacts'][$key]['selected'] = 1;
369 $acl_data['contacts'][$key]['selected'] = 0;
377 function acl_lookup(&$a, $out_type = 'json') {
383 $start = (x($_REQUEST,'start')?$_REQUEST['start']:0);
384 $count = (x($_REQUEST,'count')?$_REQUEST['count']:100);
385 $search = (x($_REQUEST,'search')?$_REQUEST['search']:"");
386 $type = (x($_REQUEST,'type')?$_REQUEST['type']:"");
387 $conv_id = (x($_REQUEST,'conversation')?$_REQUEST['conversation']:null);
389 // For use with jquery.autocomplete for private mail completion
391 if(x($_REQUEST,'query') && strlen($_REQUEST['query'])) {
394 $search = $_REQUEST['query'];
399 $sql_extra = "AND `name` LIKE '%%".dbesc($search)."%%'";
400 $sql_extra2 = "AND (`attag` LIKE '%%".dbesc($search)."%%' OR `name` LIKE '%%".dbesc($search)."%%' OR `nick` LIKE '%%".dbesc($search)."%%')";
402 $sql_extra = $sql_extra2 = "";
405 // count groups and contacts
406 if ($type=='' || $type=='g'){
407 $r = q("SELECT COUNT(*) AS g FROM `group` WHERE `deleted` = 0 AND `uid` = %d $sql_extra",
410 $group_count = (int)$r[0]['g'];
415 if ($type=='' || $type=='c'){
416 $r = q("SELECT COUNT(*) AS c FROM `contact`
417 WHERE `uid` = %d AND `self` = 0
418 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0
419 AND `notify` != '' $sql_extra2" ,
422 $contact_count = (int)$r[0]['c'];
424 elseif ($type == 'm') {
426 // autocomplete for Private Messages
428 $r = q("SELECT COUNT(*) AS c FROM `contact`
429 WHERE `uid` = %d AND `self` = 0
430 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0
431 AND `network` IN ('%s','%s','%s') $sql_extra2" ,
432 intval(local_user()),
435 dbesc(NETWORK_DIASPORA)
437 $contact_count = (int)$r[0]['c'];
440 elseif ($type == 'a') {
442 // autocomplete for Contacts
444 $r = q("SELECT COUNT(*) AS c FROM `contact`
445 WHERE `uid` = %d AND `self` = 0
446 AND `pending` = 0 $sql_extra2" ,
449 $contact_count = (int)$r[0]['c'];
456 $tot = $group_count+$contact_count;
461 if ($type=='' || $type=='g'){
463 $r = q("SELECT `group`.`id`, `group`.`name`, GROUP_CONCAT(DISTINCT `group_member`.`contact-id` SEPARATOR ',') as uids
464 FROM `group`,`group_member`
465 WHERE `group`.`deleted` = 0 AND `group`.`uid` = %d
466 AND `group_member`.`gid`=`group`.`id`
468 GROUP BY `group`.`id`
469 ORDER BY `group`.`name`
471 intval(local_user()),
477 // logger('acl: group: ' . $g['name'] . ' members: ' . $g['uids']);
480 "photo" => "images/twopeople.png",
481 "name" => $g['name'],
482 "id" => intval($g['id']),
483 "uids" => array_map("intval", explode(",",$g['uids'])),
490 if ($type=='' || $type=='c'){
492 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, forum FROM `contact`
493 WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 AND `notify` != ''
495 ORDER BY `name` ASC ",
499 elseif($type == 'm') {
500 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag` FROM `contact`
501 WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0
502 AND `network` IN ('%s','%s','%s')
504 ORDER BY `name` ASC ",
505 intval(local_user()),
508 dbesc(NETWORK_DIASPORA)
511 elseif($type == 'a') {
512 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag` FROM `contact`
513 WHERE `uid` = %d AND `pending` = 0
515 ORDER BY `name` ASC ",
523 if($type == 'm' || $type == 'a') {
525 $x['query'] = $search;
526 $x['photos'] = array();
527 $x['links'] = array();
528 $x['suggestions'] = array();
529 $x['data'] = array();
532 $x['photos'][] = proxy_url($g['micro']);
533 $x['links'][] = $g['url'];
534 $x['suggestions'][] = $g['name'];
535 $x['data'][] = intval($g['id']);
538 echo json_encode($x);
546 "photo" => proxy_url($g['micro']),
547 "name" => $g['name'],
548 "id" => intval($g['id']),
549 "network" => $g['network'],
551 "nick" => ($g['attag']) ? $g['attag'] : $g['nick'],
552 "forum" => $g['forum']
557 $items = array_merge($groups, $contacts);
560 /* if $conv_id is set, get unknow contacts in thread */
561 /* but first get know contacts url to filter them out */
562 function _contact_link($i){ return dbesc($i['link']); }
563 $known_contacts = array_map(_contact_link, $contacts);
564 $unknow_contacts=array();
566 `author-avatar`,`author-name`,`author-link`
567 from item where parent=%d
569 `author-name` LIKE '%%%s%%' OR
570 `author-link` LIKE '%%%s%%'
572 `author-link` NOT IN ('%s')
573 GROUP BY `author-link`
574 ORDER BY `author-name` ASC
579 implode("','", $known_contacts)
581 if (is_array($r) && count($r)){
582 foreach($r as $row) {
584 $up = parse_url($row['author-link']);
585 $nick = explode("/",$up['path']);
586 $nick = $nick[count($nick)-1];
587 $nick .= "@".$up['host'];
589 $unknow_contacts[] = array(
591 "photo" => proxy_url($row['author-avatar']),
592 "name" => $row['author-name'],
594 "network" => "unknown",
595 "link" => $row['author-link'],
602 $items = array_merge($items, $unknow_contacts);
603 $tot += count($unknow_contacts);
606 if($out_type === 'html') {
612 'contacts' => $contacts,
624 echo json_encode($o);