2012-12-27 08:04:54 +00:00
< ? php
require_once ( 'include/dir_fns.php' );
function dirsearch_init ( & $a ) {
2013-01-19 09:07:35 +00:00
$a -> set_pager_itemspage ( 80 );
2012-12-27 08:04:54 +00:00
}
function dirsearch_content ( & $a ) {
$ret = array ( 'success' => false );
// If you've got a public directory server, you probably shouldn't block public access
$dirmode = intval ( get_config ( 'system' , 'directory_mode' ));
if ( $dirmode == DIRECTORY_MODE_NORMAL ) {
$ret [ 'message' ] = t ( 'This site is not a directory server' );
2013-01-22 10:56:32 +00:00
json_return_and_die ( $ret );
2012-12-27 08:04:54 +00:00
}
2013-09-06 05:23:57 +00:00
if ( argc () > 1 && argv ( 1 ) === 'sites' ) {
2013-09-06 05:00:06 +00:00
$ret = list_public_sites ();
json_return_and_die ( $ret );
}
2013-04-29 06:13:50 +00:00
$name = (( x ( $_REQUEST , 'name' )) ? $_REQUEST [ 'name' ] : '' );
2013-06-16 12:28:49 +00:00
$hub = (( x ( $_REQUEST , 'hub' )) ? $_REQUEST [ 'hub' ] : '' );
2013-04-29 06:13:50 +00:00
$address = (( x ( $_REQUEST , 'address' )) ? $_REQUEST [ 'address' ] : '' );
$locale = (( x ( $_REQUEST , 'locale' )) ? $_REQUEST [ 'locale' ] : '' );
$region = (( x ( $_REQUEST , 'region' )) ? $_REQUEST [ 'region' ] : '' );
2012-12-27 08:04:54 +00:00
$postcode = (( x ( $_REQUEST , 'postcode' )) ? $_REQUEST [ 'postcode' ] : '' );
2013-04-29 06:13:50 +00:00
$country = (( x ( $_REQUEST , 'country' )) ? $_REQUEST [ 'country' ] : '' );
$gender = (( x ( $_REQUEST , 'gender' )) ? $_REQUEST [ 'gender' ] : '' );
$marital = (( x ( $_REQUEST , 'marital' )) ? $_REQUEST [ 'marital' ] : '' );
2012-12-27 08:04:54 +00:00
$keywords = (( x ( $_REQUEST , 'keywords' )) ? $_REQUEST [ 'keywords' ] : '' );
2013-06-30 07:49:53 +00:00
$agege = (( x ( $_REQUEST , 'agege' )) ? intval ( $_REQUEST [ 'agege' ]) : 0 );
$agele = (( x ( $_REQUEST , 'agele' )) ? intval ( $_REQUEST [ 'agele' ]) : 0 );
2013-09-19 00:47:26 +00:00
$kw = (( x ( $_REQUEST , 'kw' )) ? intval ( $_REQUEST [ 'kw' ]) : 0 );
2013-09-20 04:36:22 +00:00
2013-09-20 02:50:13 +00:00
// by default use a safe search
$safe = (( x ( $_REQUEST , 'safe' )) ? intval ( $_REQUEST [ 'safe' ]) : 1 );
2012-12-27 08:04:54 +00:00
2013-09-15 10:48:43 +00:00
$sync = (( x ( $_REQUEST , 'sync' )) ? datetime_convert ( 'UTC' , 'UTC' , $_REQUEST [ 'sync' ]) : '' );
2013-07-15 05:53:28 +00:00
$sort_order = (( x ( $_REQUEST , 'order' )) ? $_REQUEST [ 'order' ] : '' );
2013-01-19 09:07:35 +00:00
// TODO - a meta search which joins all of these things to one search string
2012-12-27 08:04:54 +00:00
$sql_extra = '' ;
if ( $name )
2013-01-19 08:43:05 +00:00
$sql_extra .= " OR xchan_name like ' " . protect_sprintf ( '%' . dbesc ( $name ) . '%' ) . " ' " ;
2013-06-16 12:28:49 +00:00
if ( $hub )
$sql_extra .= " OR xchan_hash in (select hubloc_hash from hubloc where hubloc_host = ' " . protect_sprintf ( dbesc ( $hub )) . " ') " ;
2013-01-19 08:43:05 +00:00
if ( $address )
$sql_extra .= " OR xchan_addr like ' " . protect_sprintf ( '%' . dbesc ( $address ) . '%' ) . " ' " ;
2012-12-27 08:04:54 +00:00
if ( $city )
2013-01-19 08:43:05 +00:00
$sql_extra .= " OR xprof_locale like ' " . protect_sprintf ( '%' . dbesc ( $city ) . '%' ) . " ' " ;
2012-12-27 08:04:54 +00:00
if ( $region )
2013-01-19 08:43:05 +00:00
$sql_extra .= " OR xprof_region like ' " . protect_sprintf ( '%' . dbesc ( $region ) . '%' ) . " ' " ;
2012-12-27 08:04:54 +00:00
if ( $post )
2013-01-19 08:43:05 +00:00
$sql_extra .= " OR xprof_postcode like ' " . protect_sprintf ( '%' . dbesc ( $post ) . '%' ) . " ' " ;
2012-12-27 08:04:54 +00:00
if ( $country )
2013-01-19 08:43:05 +00:00
$sql_extra .= " OR xprof_country like ' " . protect_sprintf ( '%' . dbesc ( $country ) . '%' ) . " ' " ;
2012-12-27 08:04:54 +00:00
if ( $gender )
2013-01-19 08:43:05 +00:00
$sql_extra .= " OR xprof_gender like ' " . protect_sprintf ( '%' . dbesc ( $gender ) . '%' ) . " ' " ;
2012-12-27 08:04:54 +00:00
if ( $marital )
2013-01-19 08:43:05 +00:00
$sql_extra .= " OR xprof_marital like ' " . protect_sprintf ( '%' . dbesc ( $marital ) . '%' ) . " ' " ;
2012-12-27 08:04:54 +00:00
if ( $keywords )
2013-01-19 08:43:05 +00:00
$sql_extra .= " OR xprof_keywords like ' " . protect_sprintf ( '%' . dbesc ( $keywords ) . '%' ) . " ' " ;
2012-12-27 08:04:54 +00:00
2013-06-30 07:49:53 +00:00
// we only support an age range currently. You must set both agege
// (greater than or equal) and agele (less than or equal)
if ( $agele && $agege ) {
$sql_extra .= " OR ( xprof_age <= " . intval ( $agele ) . " " ;
$sql_extra .= " AND xprof_age >= " . intval ( $agege ) . " ) " ;
}
2013-04-29 06:13:50 +00:00
$perpage = (( $_REQUEST [ 'n' ]) ? $_REQUEST [ 'n' ] : 80 );
$page = (( $_REQUEST [ 'p' ]) ? intval ( $_REQUEST [ 'p' ] - 1 ) : 0 );
$startrec = (( $page + 1 ) * $perpage ) - $perpage ;
$limit = (( $_REQUEST [ 'limit' ]) ? intval ( $_REQUEST [ 'limit' ]) : 0 );
2013-01-19 09:07:35 +00:00
$return_total = (( x ( $_REQUEST , 'return_total' )) ? intval ( $_REQUEST [ 'return_total' ]) : 0 );
2012-12-27 08:04:54 +00:00
2013-09-15 10:48:43 +00:00
// mtime is not currently working
2013-04-29 06:13:50 +00:00
$mtime = (( x ( $_REQUEST , 'mtime' )) ? datetime_convert ( 'UTC' , 'UTC' , $_REQUEST [ 'mtime' ]) : '' );
2012-12-27 08:04:54 +00:00
// ok a separate tag table won't work.
// merge them into xprof
$ret [ 'success' ] = true ;
2013-01-19 09:07:35 +00:00
// If &limit=n, return at most n entries
// If &return_total=1, we count matching entries and return that as 'total_items' for use in pagination.
// By default we return one page (default 80 items maximum) and do not count total entries
2013-01-19 08:43:05 +00:00
2013-01-22 08:20:25 +00:00
$logic = (( strlen ( $sql_extra )) ? 0 : 1 );
2013-01-26 00:18:35 +00:00
2013-09-20 04:36:22 +00:00
$safesql = (( $safe > 0 ) ? " and not ( xchan_flags & " . intval ( XCHAN_FLAGS_CENSORED ) . " ) " : '' );
if ( $safe < 0 )
$safesql = " and ( xchan_flags & " . intval ( XCHAN_FLAGS_CENSORED ) . " ) " ;
2013-09-20 02:50:13 +00:00
2013-01-19 08:43:05 +00:00
if ( $limit )
$qlimit = " LIMIT $limit " ;
else {
$qlimit = " LIMIT " . intval ( $startrec ) . " , " . intval ( $perpage );
2013-01-21 04:14:13 +00:00
if ( $return_total ) {
2013-09-20 02:50:13 +00:00
$r = q ( " SELECT COUNT(xchan_hash) AS `total` FROM xchan left join xprof on xchan_hash = xprof_hash where $logic $sql_extra and not ( xchan_flags & %d) and not ( xchan_flags & %d ) $safesql " ,
2013-08-07 00:15:41 +00:00
intval ( XCHAN_FLAGS_HIDDEN ),
intval ( XCHAN_FLAGS_ORPHAN )
2013-01-22 08:20:25 +00:00
);
2013-01-21 04:14:13 +00:00
if ( $r ) {
$ret [ 'total_items' ] = $r [ 0 ][ 'total' ];
}
2013-01-19 08:43:05 +00:00
}
2012-12-27 08:04:54 +00:00
}
2013-04-29 06:13:50 +00:00
if ( $mtime ) {
$qlimit = '' ;
2013-09-09 20:45:51 +00:00
// $sql_extra .= " and xchan_hash in ( select ud_hash from updates where ud_date > '" . dbesc($mtime) . "' ) ";
2013-04-29 06:13:50 +00:00
}
2013-07-15 05:53:28 +00:00
if ( $sort_order == 'date' )
2013-09-09 20:45:51 +00:00
$order = " " ; // " order by ud_date desc ";
2013-07-15 05:53:28 +00:00
elseif ( $sort_order == 'reverse' )
$order = " order by xchan_name desc " ;
else
$order = " order by xchan_name asc " ;
2013-09-15 10:48:43 +00:00
if ( $sync ) {
2013-01-19 09:07:35 +00:00
2013-09-15 10:48:43 +00:00
$r = q ( " select xchan.*, updates.* from xchan left join updates on ud_hash = xchan_hash where ud_date >= '%s' and ud_guid != '' order by ud_date desc " ,
dbesc ( $sync )
);
}
else {
2013-09-20 02:50:13 +00:00
$r = q ( " SELECT xchan.*, xprof.* from xchan left join xprof on xchan_hash = xprof_hash where $logic $sql_extra and not ( xchan_flags & %d ) and not ( xchan_flags & %d ) $safesql $order $qlimit " ,
2013-09-15 10:48:43 +00:00
intval ( XCHAN_FLAGS_HIDDEN ),
intval ( XCHAN_FLAGS_ORPHAN )
);
}
2013-01-26 00:18:35 +00:00
2012-12-27 08:04:54 +00:00
$ret [ 'page' ] = $page + 1 ;
$ret [ 'records' ] = count ( $r );
if ( $r ) {
$entries = array ();
foreach ( $r as $rr ) {
$entry = array ();
$entry [ 'name' ] = $rr [ 'xchan_name' ];
2013-08-03 23:35:55 +00:00
$entry [ 'hash' ] = $rr [ 'xchan_hash' ];
2013-07-15 05:53:28 +00:00
$entry [ 'updated' ] = (( $rr [ 'ud_date' ]) ? $rr [ 'ud_date' ] : '0000-00-00 00:00:00' );
2013-09-15 10:48:43 +00:00
$entry [ 'update_guid' ] = (( $rr [ 'ud_guid' ]) ? $rr [ 'ud_guid' ] : '' );
2012-12-27 08:04:54 +00:00
$entry [ 'url' ] = $rr [ 'xchan_url' ];
$entry [ 'photo' ] = $rr [ 'xchan_photo_m' ];
$entry [ 'address' ] = $rr [ 'xchan_addr' ];
$entry [ 'description' ] = $rr [ 'xprof_desc' ];
$entry [ 'locale' ] = $rr [ 'xprof_locale' ];
$entry [ 'region' ] = $rr [ 'xprof_region' ];
$entry [ 'postcode' ] = $rr [ 'xprof_postcode' ];
$entry [ 'country' ] = $rr [ 'xprof_country' ];
$entry [ 'birthday' ] = $rr [ 'xprof_dob' ];
2013-06-30 08:00:24 +00:00
$entry [ 'age' ] = $rr [ 'xprof_age' ];
2012-12-27 08:04:54 +00:00
$entry [ 'gender' ] = $rr [ 'xprof_gender' ];
$entry [ 'marital' ] = $rr [ 'xprof_marital' ];
$entry [ 'keywords' ] = $rr [ 'xprof_keywords' ];
$entries [] = $entry ;
}
$ret [ 'results' ] = $entries ;
2013-09-19 00:47:26 +00:00
if (( $kw ) && ( ! $sync )) {
$k = dir_tagadelic ( $kw );
2013-09-18 05:52:46 +00:00
if ( $k ) {
$ret [ 'keywords' ] = array ();
foreach ( $k as $kv ) {
2013-09-19 00:47:26 +00:00
$ret [ 'keywords' ][] = array ( 'term' => $kv [ 0 ], 'weight' => $kv [ 1 ], 'normalise' => $kv [ 2 ]);
2013-09-18 05:52:46 +00:00
}
}
}
2012-12-27 08:04:54 +00:00
}
json_return_and_die ( $ret );
}
2013-09-06 05:00:06 +00:00
function list_public_sites () {
$r = q ( " select * from site where site_access != 0 order by rand() " );
$ret = array ( 'success' => false );
if ( $r ) {
$ret [ 'success' ] = true ;
$ret [ 'sites' ] = array ();
foreach ( $r as $rr ) {
if ( $rr [ 'site_access' ] == ACCESS_FREE )
$access = 'free' ;
elseif ( $rr [ 'site_access' ] == ACCESS_PAID )
$access = 'paid' ;
2013-09-19 00:47:26 +00:00
elseif ( $rr [ 'site_access' ] == ACCESS_TIERED )
$access = 'tiered' ;
2013-09-06 05:00:06 +00:00
else
$access = 'private' ;
if ( $rr [ 'site_register' ] == REGISTER_OPEN )
$register = 'open' ;
elseif ( $rr [ 'site_register' ] == REGISTER_APPROVE )
$register = 'approve' ;
else
$register = 'closed' ;
2013-09-18 05:27:51 +00:00
$ret [ 'sites' ][] = array ( 'url' => $rr [ 'site_url' ], 'access' => $access , 'register' => $register , 'sellpage' => $rr [ 'site_sellpage' ]);
2013-09-06 05:00:06 +00:00
}
}
return $ret ;
}