$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).((strpos($search,'@')===false)?"%@%'":"%'")).") ";
// This horrible mess is needed because position also returns 0 if nothing is found. W/ould be MUCH easier if it instead returned a very large value
// Otherwise we could just order by LEAST(POSITION($search IN xchan_name),POSITION($search IN xchan_addr)).
$order_extra2="CASE WHEN xchan_name LIKE ".protect_sprintf("'%".dbesc($search)."%'")." then POSITION('".dbesc($search)."' IN xchan_name) else position('".dbesc($search)."' IN xchan_addr) end, ";
// Getting info from the abook is better for local users because it contains info about permissions
if(local_channel()){
if($extra_channels_sql!='')
$extra_channels_sql=" OR (abook_channel IN ($extra_channels_sql)) and abook_hidden = 0 ";
$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, abook_their_perms, abook_flags, abook_self
$r=q("SELECT xchan_hash as id, xchan_hash as hash, xchan_name as name, xchan_photo_s as micro, xchan_url as url, xchan_addr as nick, 0 as abook_their_perms, 0 as abook_flags, 0 as abook_self
// This is probably more complicated than it needs to be
if($extra_channels_sql){
// Build a list of hashes that we got previously so we don't get them again
$known_hashes=array("'".get_observer_hash()."'");
if($r)
foreach($ras$rr)
$known_hashes[]="'".$rr['hash']."'";
$known_hashes_sql='AND xchan_hash not in ('.join(',',$known_hashes).')';
$r2=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, abook_their_perms, abook_flags, abook_self
// Sort accoring to match position, then alphabetically. This could be avoided if the above two SQL queries could be combined into one, and the sorting could be done on the SQl server (like in the case of a local user)
$r2=q("SELECT substr(xchan_hash,1,18) as id, xchan_hash as hash, xchan_name as name, xchan_photo_s as micro, xchan_url as url, xchan_addr as nick, 0 as abook_their_perms, 0 as abook_flags, 0 as abook_self
$r=q("SELECT abook_id as id, xchan_name as name, xchan_hash as hash, xchan_addr as nick, xchan_photo_s as micro, xchan_network as network, xchan_url as url, xchan_addr as attag , abook_their_perms FROM abook left join xchan on abook_xchan = xchan_hash
WHEREabook_channel=%d
andxchan_deleted=0
$sql_extra3
ORDERBYxchan_nameASC",
intval(local_channel())
);
}
elseif($type=='x'){
$r=navbar_complete($a);
$contacts=array();
if($r){
foreach($ras$g){
$contacts[]=array(
"photo"=>$g['photo'],
"name"=>$g['name'],
"nick"=>$g['address'],
);
}
}
$o=array(
'start'=>$start,
'count'=>$count,
'items'=>$contacts,
);
echojson_encode($o);
killme();
}
else
$r=array();
if(count($r)){
foreach($ras$g){
// remove RSS feeds from ACLs - they are inaccessible