3 require_once("include/contact_selectors.php");
10 * @package acl_selectors
12 function group_select($selname,$selclass,$preselected = false,$size = 4) {
18 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"$size\" >\r\n";
20 $r = q("SELECT * FROM `group` WHERE `deleted` = 0 AND `uid` = %d ORDER BY `name` ASC",
25 $arr = array('group' => $r, 'entry' => $o);
27 // e.g. 'network_pre_group_deny', 'profile_pre_group_allow'
29 call_hooks($a->module . '_pre_' . $selname, $arr);
33 if((is_array($preselected)) && in_array($rr['id'], $preselected))
34 $selected = " selected=\"selected\" ";
37 $trimmed = mb_substr($rr['name'],0,12);
39 $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}\" >$trimmed</option>\r\n";
43 $o .= "</select>\r\n";
45 call_hooks($a->module . '_post_' . $selname, $o);
52 function contact_selector($selname, $selclass, $preselected = false, $options) {
62 if(is_array($options)) {
63 if(x($options,'size'))
64 $size = $options['size'];
66 if(x($options,'mutual_friends'))
68 if(x($options,'single'))
70 if(x($options,'multiple'))
72 if(x($options,'exclude'))
73 $exclude = $options['exclude'];
75 if(x($options,'networks')) {
76 switch($options['networks']) {
78 $networks = array('dfrn');
81 if(is_array($a->user) && $a->user['prvnets'])
82 $networks = array('dfrn','mail','dspr');
84 $networks = array('dfrn','face','mail', 'dspr');
87 if(is_array($a->user) && $a->user['prvnets'])
88 $networks = array('dfrn','mail','dspr');
90 $networks = array('dfrn','face','mail','dspr','stat');
98 $x = array('options' => $options, 'size' => $size, 'single' => $single, 'mutual' => $mutual, 'exclude' => $exclude, 'networks' => $networks);
100 call_hooks('contact_select_options', $x);
107 $sql_extra .= sprintf(" AND `rel` = %d ", intval(CONTACT_IS_FRIEND));
110 if(intval($x['exclude']))
111 $sql_extra .= sprintf(" AND `id` != %d ", intval($x['exclude']));
113 if(is_array($x['networks']) && count($x['networks'])) {
114 for($y = 0; $y < count($x['networks']) ; $y ++)
115 $x['networks'][$y] = "'" . dbesc($x['networks'][$y]) . "'";
116 $str_nets = implode(',',$x['networks']);
117 $sql_extra .= " AND `network` IN ( $str_nets ) ";
120 $tabindex = (x($options, 'tabindex') ? "tabindex=\"" . $options["tabindex"] . "\"" : "");
123 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"" . $x['size'] . "\" $tabindex >\r\n";
125 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"" . $x['size'] . "$\" $tabindex >\r\n";
127 $r = q("SELECT `id`, `name`, `url`, `network` FROM `contact`
128 WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 AND `notify` != ''
130 ORDER BY `name` ASC ",
135 $arr = array('contact' => $r, 'entry' => $o);
137 // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
139 call_hooks($a->module . '_pre_' . $selname, $arr);
143 if((is_array($preselected)) && in_array($rr['id'], $preselected))
144 $selected = " selected=\"selected\" ";
148 $trimmed = mb_substr($rr['name'],0,20);
150 $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}|{$rr['url']}\" >$trimmed</option>\r\n";
155 $o .= "</select>\r\n";
157 call_hooks($a->module . '_post_' . $selname, $o);
164 function contact_select($selname, $selclass, $preselected = false, $size = 4, $privmail = false, $celeb = false, $privatenet = false, $tabindex = null) {
170 // When used for private messages, we limit correspondence to mutual DFRN/Friendica friends and the selector
171 // to one recipient. By default our selector allows multiple selects amongst all contacts.
175 if($privmail || $celeb) {
176 $sql_extra .= sprintf(" AND `rel` = %d ", intval(CONTACT_IS_FRIEND));
180 $sql_extra .= " AND `network` IN ( 'dfrn', 'dspr' ) ";
182 elseif($privatenet) {
183 $sql_extra .= " AND `network` IN ( 'dfrn', 'mail', 'face', 'dspr' ) ";
186 $tabindex = ($tabindex > 0 ? "tabindex=\"$tabindex\"" : "");
189 $o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"$size\" $tabindex >\r\n";
191 $o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"$size\" $tabindex >\r\n";
193 $r = q("SELECT `id`, `name`, `url`, `network` FROM `contact`
194 WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 AND `notify` != ''
196 ORDER BY `name` ASC ",
201 $arr = array('contact' => $r, 'entry' => $o);
203 // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
205 call_hooks($a->module . '_pre_' . $selname, $arr);
209 if((is_array($preselected)) && in_array($rr['id'], $preselected))
210 $selected = " selected=\"selected\" ";
214 $trimmed = mb_substr($rr['name'],0,20);
216 $o .= "<option value=\"{$rr['id']}\" $selected title=\"{$rr['name']}|{$rr['url']}\" >$trimmed</option>\r\n";
221 $o .= "</select>\r\n";
223 call_hooks($a->module . '_post_' . $selname, $o);
229 function fixacl(&$item) {
230 $item = intval(str_replace(array('<','>'),array('',''),$item));
233 function prune_deadguys($arr) {
237 $str = dbesc(implode(',',$arr));
238 $r = q("select id from contact where id in ( " . $str . ") and blocked = 0 and pending = 0 and archive = 0 ");
242 $ret[] = intval($rr['id']);
249 function get_acl_permissions($user = null) {
250 $allow_cid = $allow_gid = $deny_cid = $deny_gid = false;
252 if(is_array($user)) {
253 $allow_cid = ((strlen($user['allow_cid']))
254 ? explode('><', $user['allow_cid']) : array() );
255 $allow_gid = ((strlen($user['allow_gid']))
256 ? explode('><', $user['allow_gid']) : array() );
257 $deny_cid = ((strlen($user['deny_cid']))
258 ? explode('><', $user['deny_cid']) : array() );
259 $deny_gid = ((strlen($user['deny_gid']))
260 ? explode('><', $user['deny_gid']) : array() );
261 array_walk($allow_cid,'fixacl');
262 array_walk($allow_gid,'fixacl');
263 array_walk($deny_cid,'fixacl');
264 array_walk($deny_gid,'fixacl');
267 $allow_cid = prune_deadguys($allow_cid);
270 'allow_cid' => $allow_cid,
271 'allow_gid' => $allow_gid,
272 'deny_cid' => $deny_cid,
273 'deny_gid' => $deny_gid,
278 function populate_acl($user = null,$celeb = false) {
280 $perms = get_acl_permissions($user);
282 // We shouldn't need to prune deadguys from the block list. Either way they can't get the message.
283 // Also no point enumerating groups and checking them, that will take place on delivery.
285 // $deny_cid = prune_deadguys($deny_cid);
289 $o .= '<div id="acl-wrapper">';
290 $o .= '<div id="acl-permit-outer-wrapper">';
291 $o .= '<div id="acl-permit-text">' . t('Visible To:') . '</div><div id="jot-public">' . t('everybody') . '</div>';
292 $o .= '<div id="acl-permit-text-end"></div>';
293 $o .= '<div id="acl-permit-wrapper">';
294 $o .= '<div id="group_allow_wrapper">';
295 $o .= '<label id="acl-allow-group-label" for="group_allow" >' . t('Groups') . '</label>';
296 $o .= group_select('group_allow','group_allow',$allow_gid);
298 $o .= '<div id="contact_allow_wrapper">';
299 $o .= '<label id="acl-allow-contact-label" for="contact_allow" >' . t('Contacts') . '</label>';
300 $o .= contact_select('contact_allow','contact_allow',$allow_cid,4,false,$celeb,true);
302 $o .= '</div>' . "\r\n";
303 $o .= '<div id="acl-allow-end"></div>' . "\r\n";
305 $o .= '<div id="acl-deny-outer-wrapper">';
306 $o .= '<div id="acl-deny-text">' . t('Except For:') . '</div>';
307 $o .= '<div id="acl-deny-text-end"></div>';
308 $o .= '<div id="acl-deny-wrapper">';
309 $o .= '<div id="group_deny_wrapper" >';
310 $o .= '<label id="acl-deny-group-label" for="group_deny" >' . t('Groups') . '</label>';
311 $o .= group_select('group_deny','group_deny', $deny_gid);
313 $o .= '<div id="contact_deny_wrapper" >';
314 $o .= '<label id="acl-deny-contact-label" for="contact_deny" >' . t('Contacts') . '</label>';
315 $o .= contact_select('contact_deny','contact_deny', $deny_cid,4,false, $celeb,true);
317 $o .= '</div>' . "\r\n";
318 $o .= '<div id="acl-deny-end"></div>' . "\r\n";
320 $o .= '</div>' . "\r\n";
321 $o .= '<div id="acl-wrapper-end"></div>' . "\r\n";*/
323 $tpl = get_markup_template("acl_selector.tpl");
324 $o = replace_macros($tpl, array(
325 '$showall'=> t("Visible to everybody"),
326 '$show' => t("show"),
327 '$hide' => t("don't show"),
328 '$allowcid' => json_encode($perms['allow_cid']),
329 '$allowgid' => json_encode($perms['allow_gid']),
330 '$denycid' => json_encode($perms['deny_cid']),
331 '$denygid' => json_encode($perms['deny_gid']),
339 function construct_acl_data(&$a, $user) {
341 // Get group and contact information for html ACL selector
342 $acl_data = acl_lookup($a, 'html');
344 $user_defaults = get_acl_permissions($user);
346 if($acl_data['groups']) {
347 foreach($acl_data['groups'] as $key=>$group) {
348 // Add a "selected" flag to groups that are posted to by default
349 if($user_defaults['allow_gid'] &&
350 in_array($group['id'], $user_defaults['allow_gid']) && !in_array($group['id'], $user_defaults['deny_gid']) )
351 $acl_data['groups'][$key]['selected'] = 1;
353 $acl_data['groups'][$key]['selected'] = 0;
356 if($acl_data['contacts']) {
357 foreach($acl_data['contacts'] as $key=>$contact) {
358 // Add a "selected" flag to groups that are posted to by default
359 if($user_defaults['allow_cid'] &&
360 in_array($contact['id'], $user_defaults['allow_cid']) && !in_array($contact['id'], $user_defaults['deny_cid']) )
361 $acl_data['contacts'][$key]['selected'] = 1;
363 $acl_data['contacts'][$key]['selected'] = 0;
371 function acl_lookup(&$a, $out_type = 'json') {
377 $start = (x($_REQUEST,'start')?$_REQUEST['start']:0);
378 $count = (x($_REQUEST,'count')?$_REQUEST['count']:100);
379 $search = (x($_REQUEST,'search')?$_REQUEST['search']:"");
380 $type = (x($_REQUEST,'type')?$_REQUEST['type']:"");
383 // For use with jquery.autocomplete for private mail completion
385 if(x($_REQUEST,'query') && strlen($_REQUEST['query'])) {
388 $search = $_REQUEST['query'];
393 $sql_extra = "AND `name` LIKE '%%".dbesc($search)."%%'";
394 $sql_extra2 = "AND (`attag` LIKE '%%".dbesc($search)."%%' OR `name` LIKE '%%".dbesc($search)."%%' OR `nick` LIKE '%%".dbesc($search)."%%')";
396 $sql_extra = $sql_extra2 = "";
399 // count groups and contacts
400 if ($type=='' || $type=='g'){
401 $r = q("SELECT COUNT(`id`) AS g FROM `group` WHERE `deleted` = 0 AND `uid` = %d $sql_extra",
404 $group_count = (int)$r[0]['g'];
409 if ($type=='' || $type=='c'){
410 $r = q("SELECT COUNT(`id`) AS c FROM `contact`
411 WHERE `uid` = %d AND `self` = 0
412 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0
413 AND `notify` != '' $sql_extra2" ,
416 $contact_count = (int)$r[0]['c'];
418 elseif ($type == 'm') {
420 // autocomplete for Private Messages
422 $r = q("SELECT COUNT(`id`) AS c FROM `contact`
423 WHERE `uid` = %d AND `self` = 0
424 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0
425 AND `network` IN ('%s','%s','%s') $sql_extra2" ,
426 intval(local_user()),
429 dbesc(NETWORK_DIASPORA)
431 $contact_count = (int)$r[0]['c'];
434 elseif ($type == 'a') {
436 // autocomplete for Contacts
438 $r = q("SELECT COUNT(`id`) AS c FROM `contact`
439 WHERE `uid` = %d AND `self` = 0
440 AND `pending` = 0 $sql_extra2" ,
443 $contact_count = (int)$r[0]['c'];
449 $tot = $group_count+$contact_count;
454 if ($type=='' || $type=='g'){
456 $r = q("SELECT `group`.`id`, `group`.`name`, GROUP_CONCAT(DISTINCT `group_member`.`contact-id` SEPARATOR ',') as uids
457 FROM `group`,`group_member`
458 WHERE `group`.`deleted` = 0 AND `group`.`uid` = %d
459 AND `group_member`.`gid`=`group`.`id`
461 GROUP BY `group`.`id`
462 ORDER BY `group`.`name`
464 intval(local_user()),
470 // logger('acl: group: ' . $g['name'] . ' members: ' . $g['uids']);
473 "photo" => "images/twopeople.png",
474 "name" => $g['name'],
475 "id" => intval($g['id']),
476 "uids" => array_map("intval", explode(",",$g['uids'])),
482 if ($type=='' || $type=='c'){
484 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag` FROM `contact`
485 WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 AND `notify` != ''
487 ORDER BY `name` ASC ",
491 elseif($type == 'm') {
492 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag` FROM `contact`
493 WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0
494 AND `network` IN ('%s','%s','%s')
496 ORDER BY `name` ASC ",
497 intval(local_user()),
500 dbesc(NETWORK_DIASPORA)
503 elseif($type == 'a') {
504 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag` FROM `contact`
505 WHERE `uid` = %d AND `pending` = 0
507 ORDER BY `name` ASC ",
515 if($type == 'm' || $type == 'a') {
517 $x['query'] = $search;
518 $x['photos'] = array();
519 $x['links'] = array();
520 $x['suggestions'] = array();
521 $x['data'] = array();
524 $x['photos'][] = $g['micro'];
525 $x['links'][] = $g['url'];
526 $x['suggestions'][] = $g['name'];
527 $x['data'][] = intval($g['id']);
530 echo json_encode($x);
538 "photo" => $g['micro'],
539 "name" => $g['name'],
540 "id" => intval($g['id']),
541 "network" => $g['network'],
543 "nick" => ($g['attag']) ? $g['attag'] : $g['nick'],
548 $items = array_merge($groups, $contacts);
551 if($out_type === 'html') {
557 'contacts' => $contacts,
569 echo json_encode($o);