2016-04-19 03:38:38 +00:00
< ? php
namespace Zotlabs\Module ;
2018-06-01 04:05:09 +00:00
require_once ( " include/bbcode.php " );
require_once ( 'include/security.php' );
require_once ( 'include/conversation.php' );
2016-04-19 03:38:38 +00:00
class Search extends \Zotlabs\Web\Controller {
function init () {
if ( x ( $_REQUEST , 'search' ))
\App :: $data [ 'search' ] = $_REQUEST [ 'search' ];
}
function get ( $update = 0 , $load = false ) {
if (( get_config ( 'system' , 'block_public' )) || ( get_config ( 'system' , 'block_public_search' ))) {
if (( ! local_channel ()) && ( ! remote_channel ())) {
notice ( t ( 'Public access denied.' ) . EOL );
2017-05-31 00:06:50 +00:00
return ;
2016-04-19 03:38:38 +00:00
}
}
if ( $load )
$_SESSION [ 'loadtime' ] = datetime_convert ();
2017-10-09 19:22:57 +00:00
nav_set_selected ( 'Search' );
2016-04-19 03:38:38 +00:00
2018-06-01 04:05:09 +00:00
2016-04-19 03:38:38 +00:00
$format = (( $_REQUEST [ 'format' ]) ? $_REQUEST [ 'format' ] : '' );
if ( $format !== '' ) {
$update = $load = 1 ;
}
$observer = \App :: get_observer ();
$observer_hash = (( $observer ) ? $observer [ 'xchan_hash' ] : '' );
$o = '<div id="live-search"></div>' . " \r \n " ;
$o = '<div class="generic-content-wrapper-styled">' . " \r \n " ;
$o .= '<h3>' . t ( 'Search' ) . '</h3>' ;
if ( x ( \App :: $data , 'search' ))
$search = trim ( \App :: $data [ 'search' ]);
else
$search = (( x ( $_GET , 'search' )) ? trim ( rawurldecode ( $_GET [ 'search' ])) : '' );
$tag = false ;
if ( x ( $_GET , 'tag' )) {
$tag = true ;
$search = (( x ( $_GET , 'tag' )) ? trim ( rawurldecode ( $_GET [ 'tag' ])) : '' );
}
2016-11-02 22:48:29 +00:00
$static = (( array_key_exists ( 'static' , $_REQUEST )) ? intval ( $_REQUEST [ 'static' ]) : 0 );
2016-04-19 03:38:38 +00:00
2017-11-03 22:08:56 +00:00
$o .= search ( $search , 'search-box' , '/search' ,(( local_channel ()) ? true : false ));
2016-04-19 03:38:38 +00:00
if ( strpos ( $search , '#' ) === 0 ) {
$tag = true ;
$search = substr ( $search , 1 );
}
if ( strpos ( $search , '@' ) === 0 ) {
$search = substr ( $search , 1 );
goaway ( z_root () . '/directory' . '?f=1&navsearch=1&search=' . $search );
}
2018-04-09 00:30:46 +00:00
if ( strpos ( $search , '!' ) === 0 ) {
$search = substr ( $search , 1 );
goaway ( z_root () . '/directory' . '?f=1&navsearch=1&search=' . $search );
}
2016-04-19 03:38:38 +00:00
if ( strpos ( $search , '?' ) === 0 ) {
$search = substr ( $search , 1 );
goaway ( z_root () . '/help' . '?f=1&navsearch=1&search=' . $search );
}
// look for a naked webbie
if ( strpos ( $search , '@' ) !== false ) {
goaway ( z_root () . '/directory' . '?f=1&navsearch=1&search=' . $search );
}
if ( ! $search )
return $o ;
if ( $tag ) {
2017-07-21 01:30:48 +00:00
$wildtag = str_replace ( '*' , '%' , $search );
$sql_extra = sprintf ( " AND item.id IN (select oid from term where otype = %d and ttype in ( %d , %d) and term like '%s') " ,
2016-04-19 03:38:38 +00:00
intval ( TERM_OBJ_POST ),
intval ( TERM_HASHTAG ),
intval ( TERM_COMMUNITYTAG ),
2017-07-21 01:30:48 +00:00
dbesc ( protect_sprintf ( $wildtag ))
2016-04-19 03:38:38 +00:00
);
}
else {
$regstr = db_getfunc ( 'REGEXP' );
2018-02-09 22:08:29 +00:00
$sql_extra = sprintf ( " AND (item.title $regstr '%s' OR item.body $regstr '%s') " , dbesc ( protect_sprintf ( preg_quote ( $search ))), dbesc ( protect_sprintf ( preg_quote ( $search ))));
2016-04-19 03:38:38 +00:00
}
// Here is the way permissions work in the search module...
// Only public posts can be shown
// OR your own posts if you are a logged in member
// No items will be shown if the member has a blocked profile wall.
2016-11-02 03:21:39 +00:00
2016-04-19 03:38:38 +00:00
if (( ! $update ) && ( ! $load )) {
2016-11-03 01:40:50 +00:00
$static = (( local_channel ()) ? channel_manual_conv_update ( local_channel ()) : 0 );
2016-11-02 22:48:29 +00:00
2016-04-19 03:38:38 +00:00
// This is ugly, but we can't pass the profile_uid through the session to the ajax updater,
// because browser prefetching might change it on us. We have to deliver it with the page.
$o .= '<div id="live-search"></div>' . " \r \n " ;
$o .= " <script> var profile_uid = " . (( intval ( local_channel ())) ? local_channel () : ( - 1 ))
. " ; var netargs = '?f='; var profile_page = " . \App :: $pager [ 'page' ] . " ; </script> \r \n " ;
\App :: $page [ 'htmlhead' ] .= replace_macros ( get_markup_template ( " build_query.tpl " ), array (
'$baseurl' => z_root (),
'$pgtype' => 'search' ,
'$uid' => (( \App :: $profile [ 'profile_uid' ]) ? \App :: $profile [ 'profile_uid' ] : '0' ),
'$gid' => '0' ,
'$cid' => '0' ,
2018-06-15 22:21:19 +00:00
'$cmin' => '(-1)' ,
'$cmax' => '(-1)' ,
2016-04-19 03:38:38 +00:00
'$star' => '0' ,
'$liked' => '0' ,
'$conv' => '0' ,
'$spam' => '0' ,
'$fh' => '0' ,
'$nouveau' => '0' ,
'$wall' => '0' ,
2016-11-02 03:21:39 +00:00
'$static' => $static ,
2016-04-19 03:38:38 +00:00
'$list' => (( x ( $_REQUEST , 'list' )) ? intval ( $_REQUEST [ 'list' ]) : 0 ),
'$page' => (( \App :: $pager [ 'page' ] != 1 ) ? \App :: $pager [ 'page' ] : 1 ),
'$search' => (( $tag ) ? urlencode ( '#' ) : '' ) . $search ,
2017-03-03 01:32:43 +00:00
'$xchan' => '' ,
2016-04-19 03:38:38 +00:00
'$order' => '' ,
'$file' => '' ,
'$cats' => '' ,
'$tags' => '' ,
'$mid' => '' ,
'$verb' => '' ,
2017-11-01 09:53:12 +00:00
'$net' => '' ,
2016-04-19 03:38:38 +00:00
'$dend' => '' ,
'$dbegin' => ''
));
}
2017-05-31 00:06:50 +00:00
$item_normal = item_normal_search ();
2016-04-19 03:38:38 +00:00
$pub_sql = public_permissions_sql ( $observer_hash );
2016-05-22 23:54:30 +00:00
require_once ( 'include/channel.php' );
2016-04-19 03:38:38 +00:00
$sys = get_sys_channel ();
if (( $update ) && ( $load )) {
$itemspage = get_pconfig ( local_channel (), 'system' , 'itemspage' );
\App :: set_pager_itemspage ((( intval ( $itemspage )) ? $itemspage : 20 ));
$pager_sql = sprintf ( " LIMIT %d OFFSET %d " , intval ( \App :: $pager [ 'itemspage' ]), intval ( \App :: $pager [ 'start' ]));
// in case somebody turned off public access to sys channel content with permissions
if ( ! perm_is_allowed ( $sys [ 'channel_id' ], $observer_hash , 'view_stream' ))
$sys [ 'xchan_hash' ] .= 'disabled' ;
if ( $load ) {
$r = null ;
if ( local_channel ()) {
2018-01-22 01:06:38 +00:00
$r = q ( " SELECT mid, MAX(id) as item_id from item
2016-10-04 04:48:53 +00:00
WHERE ((( item . allow_cid = '' AND item . allow_gid = '' AND item . deny_cid = '' AND item . deny_gid = '' AND item_private = 0 )
OR ( item . uid = % d )) OR item . owner_xchan = '%s' )
2016-04-19 03:38:38 +00:00
$item_normal
$sql_extra
2018-05-08 02:33:58 +00:00
group by mid , created order by created desc $pager_sql " ,
2016-04-19 03:38:38 +00:00
intval ( local_channel ()),
dbesc ( $sys [ 'xchan_hash' ])
);
}
if ( $r === null ) {
2018-01-22 01:06:38 +00:00
$r = q ( " SELECT mid, MAX(id) as item_id from item
2016-10-04 04:48:53 +00:00
WHERE (((( item . allow_cid = '' AND item . allow_gid = '' AND item . deny_cid = ''
AND item . deny_gid = '' AND item_private = 0 )
2016-04-19 03:38:38 +00:00
and owner_xchan in ( " . stream_perms_xchans(( $observer ) ? (PERMS_NETWORK|PERMS_PUBLIC) : PERMS_PUBLIC) . " ))
$pub_sql ) OR owner_xchan = '%s' )
$item_normal
$sql_extra
2018-05-08 02:33:58 +00:00
group by mid , created order by created desc $pager_sql " ,
2016-04-19 03:38:38 +00:00
dbesc ( $sys [ 'xchan_hash' ])
);
}
2018-01-22 01:06:38 +00:00
if ( $r ) {
$str = ids_to_querystr ( $r , 'item_id' );
2018-01-22 02:11:04 +00:00
$r = q ( " select *, id as item_id from item where id in ( " . $str . " ) order by created desc " );
2018-01-22 01:06:38 +00:00
}
2016-04-19 03:38:38 +00:00
}
else {
$r = array ();
}
2018-01-22 01:06:38 +00:00
2016-04-19 03:38:38 +00:00
}
if ( $r ) {
xchan_query ( $r );
$items = fetch_post_tags ( $r , true );
} else {
$items = array ();
}
if ( $format == 'json' ) {
$result = array ();
require_once ( 'include/conversation.php' );
foreach ( $items as $item ) {
2016-12-20 00:06:49 +00:00
$item [ 'html' ] = zidify_links ( bbcode ( $item [ 'body' ]));
2016-04-19 03:38:38 +00:00
$x = encode_item ( $item );
$x [ 'html' ] = prepare_text ( $item [ 'body' ], $item [ 'mimetype' ]);
$result [] = $x ;
}
json_return_and_die ( array ( 'success' => true , 'messages' => $result ));
}
if ( $tag )
$o .= '<h2>' . sprintf ( t ( 'Items tagged with: %s' ), htmlspecialchars ( $search , ENT_COMPAT , 'UTF-8' )) . '</h2>' ;
else
$o .= '<h2>' . sprintf ( t ( 'Search results for: %s' ), htmlspecialchars ( $search , ENT_COMPAT , 'UTF-8' )) . '</h2>' ;
2017-06-20 22:53:01 +00:00
$o .= conversation ( $items , 'search' , $update , 'client' );
2016-04-19 03:38:38 +00:00
$o .= '</div>' ;
return $o ;
}
}