2011-07-19 14:17:58 +00:00
< ? php
/* ACL selector json backend */
2012-06-06 03:33:11 +00:00
2011-07-19 14:17:58 +00:00
require_once ( " include/acl_selectors.php " );
function acl_init ( & $a ){
2012-03-27 04:48:04 +00:00
$start = ( x ( $_REQUEST , 'start' ) ? $_REQUEST [ 'start' ] : 0 );
$count = ( x ( $_REQUEST , 'count' ) ? $_REQUEST [ 'count' ] : 100 );
$search = ( x ( $_REQUEST , 'search' ) ? $_REQUEST [ 'search' ] : " " );
$type = ( x ( $_REQUEST , 'type' ) ? $_REQUEST [ 'type' ] : " " );
2011-10-25 13:49:53 +00:00
2011-07-19 14:17:58 +00:00
2012-05-07 02:53:34 +00:00
// For use with jquery.autocomplete for private mail completion
if ( x ( $_REQUEST , 'query' ) && strlen ( $_REQUEST [ 'query' ])) {
2012-06-06 03:33:11 +00:00
if ( ! $type )
$type = 'm' ;
2012-05-07 02:53:34 +00:00
$search = $_REQUEST [ 'query' ];
}
2012-10-25 02:13:29 +00:00
if ( ! ( local_user () || $type == 'x' ))
return " " ;
2012-05-07 02:53:34 +00:00
2011-07-19 14:17:58 +00:00
if ( $search != " " ){
2012-12-07 03:18:38 +00:00
$sql_extra = " AND `name` LIKE " . protect_sprintf ( " '% " . dbesc ( $search ) . " %' " ) . " " ;
$sql_extra2 = " AND ( xchan_name LIKE " . protect_sprintf ( " '% " . dbesc ( $search ) . " %' " ) . " OR xchan_addr LIKE " . protect_sprintf ( " '% " . dbesc ( $search ) . " %' " ) . " ) " ;
2012-12-06 02:39:07 +00:00
$col = (( strpos ( $search , '@' ) !== false ) ? 'xchan_addr' : 'xchan_name' );
$sql_extra3 = " AND $col like " . protect_sprintf ( " '% " . dbesc ( $search ) . " %' " ) . " " ;
2012-02-26 20:40:41 +00:00
} else {
2012-10-25 02:13:29 +00:00
$sql_extra = $sql_extra2 = $sql_extra3 = " " ;
2011-07-19 14:17:58 +00:00
}
// count groups and contacts
2011-10-25 13:49:53 +00:00
if ( $type == '' || $type == 'g' ){
$r = q ( " SELECT COUNT(`id`) AS g FROM `group` WHERE `deleted` = 0 AND `uid` = %d $sql_extra " ,
intval ( local_user ())
);
$group_count = ( int ) $r [ 0 ][ 'g' ];
} else {
$group_count = 0 ;
}
if ( $type == '' || $type == 'c' ){
2012-12-07 03:18:38 +00:00
$r = q ( " SELECT COUNT(abook_id) AS c FROM abook left join xchan on abook_xchan = xchan_hash
WHERE abook_channel = % d AND not ( abook_flags & % d ) $sql_extra2 " ,
intval ( local_user ()),
intval ( ABOOK_FLAG_SELF | ABOOK_FLAG_BLOCKED | ABOOK_FLAG_PENDING | ABOOK_FLAG_ARCHIVE )
2011-10-25 13:49:53 +00:00
);
$contact_count = ( int ) $r [ 0 ][ 'c' ];
2012-04-24 01:28:33 +00:00
}
2012-12-07 03:18:38 +00:00
2012-04-24 01:28:33 +00:00
elseif ( $type == 'm' ) {
// autocomplete for Private Messages
$r = q ( " SELECT COUNT(`id`) AS c FROM `contact`
WHERE `uid` = % d AND `self` = 0
2012-04-28 00:17:58 +00:00
AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0
2012-04-24 01:28:33 +00:00
AND `network` IN ( '%s' , '%s' , '%s' ) $sql_extra2 " ,
intval ( local_user ()),
dbesc ( NETWORK_DFRN ),
dbesc ( NETWORK_ZOT ),
dbesc ( NETWORK_DIASPORA )
);
$contact_count = ( int ) $r [ 0 ][ 'c' ];
2012-06-06 03:33:11 +00:00
}
elseif ( $type == 'a' ) {
// autocomplete for Contacts
$r = q ( " SELECT COUNT(`id`) AS c FROM `contact`
WHERE `uid` = % d AND `self` = 0
AND `pending` = 0 $sql_extra2 " ,
intval ( local_user ())
);
$contact_count = ( int ) $r [ 0 ][ 'c' ];
2011-10-25 13:49:53 +00:00
} else {
$contact_count = 0 ;
}
2011-07-19 14:17:58 +00:00
$tot = $group_count + $contact_count ;
$groups = array ();
$contacts = array ();
2011-10-25 13:49:53 +00:00
if ( $type == '' || $type == 'g' ){
2012-12-07 03:18:38 +00:00
$r = q ( " SELECT `group`.`id`, `group`.`hash`, `group`.`name`,
GROUP_CONCAT ( DISTINCT `group_member` . `xchan` SEPARATOR ',' ) as uids
2011-10-25 13:49:53 +00:00
FROM `group` , `group_member`
WHERE `group` . `deleted` = 0 AND `group` . `uid` = % d
AND `group_member` . `gid` = `group` . `id`
$sql_extra
GROUP BY `group` . `id`
ORDER BY `group` . `name`
LIMIT % d , % d " ,
intval ( local_user ()),
intval ( $start ),
intval ( $count )
2011-07-19 14:17:58 +00:00
);
2011-10-25 13:49:53 +00:00
foreach ( $r as $g ){
2011-12-21 01:01:41 +00:00
// logger('acl: group: ' . $g['name'] . ' members: ' . $g['uids']);
2011-10-25 13:49:53 +00:00
$groups [] = array (
" type " => " g " ,
2012-01-11 05:09:38 +00:00
" photo " => " images/twopeople.png " ,
2011-10-25 13:49:53 +00:00
" name " => $g [ 'name' ],
2012-12-07 21:35:45 +00:00
" id " => $g [ 'id' ],
" xid " => $g [ 'hash' ],
2012-12-07 03:18:38 +00:00
" uids " => explode ( " , " , $g [ 'uids' ]),
2011-10-25 13:49:53 +00:00
" link " => ''
);
}
2011-07-19 14:17:58 +00:00
}
2012-12-07 03:18:38 +00:00
if ( $type == '' || $type == 'c' ) {
2012-12-07 21:35:45 +00:00
$r = q ( " SELECT abook_id as id, xchan_hash as hash, xchan_name as name, xchan_photo_s as micro, xchan_url as url, xchan_addr as nick
2012-12-07 03:18:38 +00:00
FROM abook left join xchan on abook_xchan = xchan_hash
WHERE abook_channel = % d AND not ( abook_flags & % d ) $sql_extra2 order by xchan_name asc " ,
intval ( local_user ()),
intval ( ABOOK_FLAG_SELF | ABOOK_FLAG_BLOCKED | ABOOK_FLAG_PENDING | ABOOK_FLAG_ARCHIVE )
2011-07-19 14:17:58 +00:00
);
2012-04-24 01:28:33 +00:00
}
elseif ( $type == 'm' ) {
2012-12-07 03:18:38 +00:00
2012-12-06 02:39:07 +00:00
$r = q ( " SELECT xchan_hash as id, xchan_name as name, xchan_addr as nick, xchan_photo_s as micro, xchan_url as url
FROM abook left join xchan on abook_xchan = xchan_hash
WHERE abook_channel = % d and ( ( abook_their_perms = null ) or ( abook_their_perms & % d ))
2012-12-05 02:24:46 +00:00
$sql_extra3
ORDER BY `xchan_name` ASC " ,
2012-12-06 02:39:07 +00:00
intval ( local_user ()),
intval ( PERMS_W_MAIL )
2012-04-24 01:28:33 +00:00
);
}
2012-06-06 03:33:11 +00:00
elseif ( $type == 'a' ) {
2012-11-13 10:57:15 +00:00
$r = q ( " SELECT abook_id as id, xchan_name as name, xchan_addr as nick, xchan_photo_s as micro, xchan_network as network, xchan_url as url, xchan_addr as attag FROM abook left join xchan on abook_xchan = xchan_hash
WHERE abook_channel = % d
$sql_extra3
ORDER BY xchan_name ASC " ,
2012-06-06 03:33:11 +00:00
intval ( local_user ())
);
}
2012-10-25 02:13:29 +00:00
elseif ( $type == 'x' ) {
2012-10-30 02:31:38 +00:00
$r = q ( " SELECT xchan_name as id, xchan_name as name, xchan_photo_s as micro, xchan_url as url from xchan
2012-10-25 02:13:29 +00:00
where 1
$sql_extra3
ORDER BY `xchan_name` ASC " ,
intval ( local_user ())
);
}
2012-04-24 01:28:33 +00:00
else
$r = array ();
2012-05-07 02:53:34 +00:00
2012-10-25 02:13:29 +00:00
if ( $type == 'm' || $type == 'a' || $type == 'x' ) {
2012-05-07 02:53:34 +00:00
$x = array ();
2012-11-13 10:57:15 +00:00
$x [ 'query' ] = $search ;
$x [ 'photos' ] = array ();
$x [ 'links' ] = array ();
2012-05-07 02:53:34 +00:00
$x [ 'suggestions' ] = array ();
2012-11-13 10:57:15 +00:00
$x [ 'data' ] = array ();
2012-05-07 02:53:34 +00:00
if ( count ( $r )) {
foreach ( $r as $g ) {
2012-11-13 10:57:15 +00:00
$x [ 'photos' ][] = $g [ 'micro' ];
$x [ 'links' ][] = $g [ 'url' ];
2012-10-25 03:43:04 +00:00
$x [ 'suggestions' ][] = (( $type === 'x' ) ? '@' : '' ) . $g [ 'name' ];
2012-12-05 02:24:46 +00:00
$x [ 'data' ][] = $g [ 'id' ];
2012-05-07 02:53:34 +00:00
}
}
echo json_encode ( $x );
killme ();
}
2012-04-24 01:28:33 +00:00
if ( count ( $r )) {
2011-10-25 13:49:53 +00:00
foreach ( $r as $g ){
$contacts [] = array (
2012-11-13 10:57:15 +00:00
" type " => " c " ,
" photo " => $g [ 'micro' ],
" name " => $g [ 'name' ],
2012-12-07 03:18:38 +00:00
" id " => $g [ 'id' ],
2012-12-07 21:35:45 +00:00
" xid " => $g [ 'hash' ],
2012-11-13 10:57:15 +00:00
" link " => $g [ 'url' ],
" nick " => $g [ 'nick' ],
2011-10-25 13:49:53 +00:00
);
2012-04-24 01:28:33 +00:00
}
2011-07-19 14:17:58 +00:00
}
2012-04-24 01:28:33 +00:00
2011-07-19 14:17:58 +00:00
$items = array_merge ( $groups , $contacts );
$o = array (
'tot' => $tot ,
'start' => $start ,
'count' => $count ,
'items' => $items ,
);
echo json_encode ( $o );
killme ();
}