2016-04-19 03:38:38 +00:00
< ? php
namespace Zotlabs\Module ;
2019-05-30 00:32:31 +00:00
use App ;
use Zotlabs\Web\Controller ;
2020-04-17 11:08:04 +00:00
use Zotlabs\Lib\Activity ;
use Zotlabs\Lib\ActivityStreams ;
2019-05-30 00:32:31 +00:00
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
2020-04-17 11:08:04 +00:00
class Search extends Controller {
2016-04-19 03:38:38 +00:00
function init () {
2019-05-30 00:32:31 +00:00
if ( x ( $_REQUEST , 'search' )) {
App :: $data [ 'search' ] = escape_tags ( $_REQUEST [ 'search' ]);
}
2016-04-19 03:38:38 +00:00
}
function get ( $update = 0 , $load = false ) {
2019-11-22 01:34:49 +00:00
2019-05-30 00:32:31 +00:00
if (( get_config ( 'system' , 'block_public' )) || ( get_config ( 'system' , 'block_public_search' , 1 ))) {
2016-04-19 03:38:38 +00:00
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
}
}
2019-05-30 00:32:31 +00:00
if ( $load ) {
2016-04-19 03:38:38 +00:00
$_SESSION [ 'loadtime' ] = datetime_convert ();
2019-05-30 00:32:31 +00:00
}
2017-10-09 19:22:57 +00:00
nav_set_selected ( 'Search' );
2016-04-19 03:38:38 +00:00
$format = (( $_REQUEST [ 'format' ]) ? $_REQUEST [ 'format' ] : '' );
2019-05-30 00:32:31 +00:00
if ( $format !== '' ) {
2016-04-19 03:38:38 +00:00
$update = $load = 1 ;
}
2019-05-30 00:32:31 +00:00
$observer = App :: get_observer ();
2016-04-19 03:38:38 +00:00
$observer_hash = (( $observer ) ? $observer [ 'xchan_hash' ] : '' );
$o = '<div id="live-search"></div>' . " \r \n " ;
2019-05-30 00:32:31 +00:00
$o .= '<div class="generic-content-wrapper-styled">' . " \r \n " ;
2016-04-19 03:38:38 +00:00
$o .= '<h3>' . t ( 'Search' ) . '</h3>' ;
2019-05-30 00:32:31 +00:00
if ( x ( App :: $data , 'search' )) {
$search = trim ( App :: $data [ 'search' ]);
}
else {
2018-11-16 04:52:08 +00:00
$search = (( x ( $_GET , 'search' )) ? trim ( escape_tags ( rawurldecode ( $_GET [ 'search' ]))) : '' );
2019-05-30 00:32:31 +00:00
}
2016-04-19 03:38:38 +00:00
$tag = false ;
2019-05-30 00:32:31 +00:00
if ( x ( $_GET , 'tag' )) {
2016-04-19 03:38:38 +00:00
$tag = true ;
2018-11-16 04:52:08 +00:00
$search = (( x ( $_GET , 'tag' )) ? trim ( escape_tags ( rawurldecode ( $_GET [ 'tag' ]))) : '' );
2016-04-19 03:38:38 +00:00
}
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 ));
2019-07-26 01:48:35 +00:00
2020-11-28 10:18:14 +00:00
if ( local_channel () && strpos ( $search , 'https://' ) === 0 ) {
2020-04-17 11:08:04 +00:00
$j = Activity :: fetch ( $search , App :: get_channel ());
if ( $j ) {
2020-12-10 01:40:29 +00:00
$AS = new ActivityStreams ( $j , null , true );
2021-03-14 23:04:13 +00:00
if ( $AS -> is_valid () && isset ( $AS -> data [ 'type' ])) {
if ( ActivityStreams :: is_an_actor ( $AS -> data [ 'type' ])) {
Activity :: actor_store ( $AS -> data [ 'id' ], $AS -> data );
goaway ( z_root () . '/directory' . '?f=1&navsearch=1&search=' . $search );
}
if ( is_array ( $AS -> obj )) {
if ( isset ( $AS -> obj [ 'type' ]) && strpos ( $AS -> obj [ 'type' ], 'Collection' )) {
// @TODO implement collection fetch here (?) - the issue is
// what kind of limits to provide as to how much you are willing
// to import
}
else {
// The boolean flag enables html cache of the item
$item = Activity :: decode_note ( $AS , true );
if ( $item ) {
logger ( 'parsed_item: ' . print_r ( $item , true ), LOGGER_DATA );
Activity :: store ( App :: get_channel (), get_observer_hash (), $AS , $item , true , true );
goaway ( z_root () . '/display/' . gen_link_id ( $item [ 'mid' ]));
}
2020-11-28 10:18:14 +00:00
}
}
2020-04-17 11:08:04 +00:00
}
}
}
2019-05-30 00:32:31 +00:00
if ( strpos ( $search , '#' ) === 0 ) {
2016-04-19 03:38:38 +00:00
$tag = true ;
$search = substr ( $search , 1 );
}
2019-05-30 00:32:31 +00:00
if ( strpos ( $search , '@' ) === 0 ) {
2016-04-19 03:38:38 +00:00
$search = substr ( $search , 1 );
goaway ( z_root () . '/directory' . '?f=1&navsearch=1&search=' . $search );
}
2019-05-30 00:32:31 +00:00
if ( strpos ( $search , '!' ) === 0 ) {
2018-04-09 00:30:46 +00:00
$search = substr ( $search , 1 );
goaway ( z_root () . '/directory' . '?f=1&navsearch=1&search=' . $search );
}
2019-05-30 00:32:31 +00:00
if ( strpos ( $search , '?' ) === 0 ) {
2016-04-19 03:38:38 +00:00
$search = substr ( $search , 1 );
goaway ( z_root () . '/help' . '?f=1&navsearch=1&search=' . $search );
}
// look for a naked webbie
2021-03-22 00:44:24 +00:00
if ( strpos ( $search , '@' ) !== false && strpos ( $search , 'http' ) !== 0 ) {
2016-04-19 03:38:38 +00:00
goaway ( z_root () . '/directory' . '?f=1&navsearch=1&search=' . $search );
}
2019-05-30 00:32:31 +00:00
if ( ! $search ) {
2016-04-19 03:38:38 +00:00
return $o ;
2019-05-30 00:32:31 +00:00
}
2016-04-19 03:38:38 +00:00
2019-05-30 00:32:31 +00:00
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
2019-05-30 00:32:31 +00:00
if (( ! $update ) && ( ! $load )) {
2016-04-19 03:38:38 +00:00
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 ))
2019-05-30 00:32:31 +00:00
. " ; var netargs = '?f='; var profile_page = " . App :: $pager [ 'page' ] . " ; </script> \r \n " ;
2016-04-19 03:38:38 +00:00
2019-05-30 00:32:31 +00:00
App :: $page [ 'htmlhead' ] .= replace_macros ( get_markup_template ( " build_query.tpl " ), [
2016-04-19 03:38:38 +00:00
'$baseurl' => z_root (),
'$pgtype' => 'search' ,
2020-04-17 11:08:04 +00:00
'$uid' => (( App :: $profile [ 'profile_uid' ]) ? App :: $profile [ 'profile_uid' ] : '0' ),
2016-04-19 03:38:38 +00:00
'$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' ,
2019-06-19 02:21:31 +00:00
'$dm' => '0' ,
2016-04-19 03:38:38 +00:00
'$nouveau' => '0' ,
'$wall' => '0' ,
2020-11-30 01:33:22 +00:00
'$draft' => '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 ),
2019-05-30 00:32:31 +00:00
'$page' => (( App :: $pager [ 'page' ] != 1 ) ? App :: $pager [ 'page' ] : 1 ),
2016-04-19 03:38:38 +00:00
'$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' => ''
2019-05-30 00:32:31 +00:00
]);
2016-04-19 03:38:38 +00:00
}
2017-05-31 00:06:50 +00:00
$item_normal = item_normal_search ();
2020-04-17 11:08:04 +00:00
$pub_sql = item_permissions_sql ( 0 , $observer_hash );
2019-05-30 00:32:31 +00:00
2016-04-19 03:38:38 +00:00
$sys = get_sys_channel ();
2019-05-30 00:32:31 +00:00
if (( $update ) && ( $load )) {
2016-04-19 03:38:38 +00:00
$itemspage = get_pconfig ( local_channel (), 'system' , 'itemspage' );
2019-05-30 00:32:31 +00:00
App :: set_pager_itemspage ((( intval ( $itemspage )) ? $itemspage : 20 ));
2020-04-17 11:08:04 +00:00
$pager_sql = sprintf ( " LIMIT %d OFFSET %d " , intval ( App :: $pager [ 'itemspage' ]), intval ( App :: $pager [ 'start' ]));
2019-10-06 20:07:41 +00:00
2019-05-30 00:32:31 +00:00
if ( $load ) {
2016-04-19 03:38:38 +00:00
$r = null ;
2019-05-30 00:32:31 +00:00
if ( local_channel ()) {
2019-10-06 20:29:01 +00:00
$r = q ( " SELECT mid, MAX(id) as item_id from item where uid = %d
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 " ,
2019-10-06 20:07:41 +00:00
intval ( local_channel ())
2016-04-19 03:38:38 +00:00
);
}
2019-05-30 00:32:31 +00:00
if ( $r === null ) {
2019-11-22 01:34:49 +00:00
$r = q ( " SELECT mid, MAX(id) as item_id from item WHERE true $pub_sql
2016-04-19 03:38:38 +00:00
$item_normal
$sql_extra
2019-10-06 20:07:41 +00:00
group by mid , created order by created desc $pager_sql "
2016-04-19 03:38:38 +00:00
);
}
2019-05-30 00:32:31 +00:00
if ( $r ) {
2018-01-22 01:06:38 +00:00
$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 {
2019-05-30 00:32:31 +00:00
$r = [];
2016-04-19 03:38:38 +00:00
}
2018-01-22 01:06:38 +00:00
2016-04-19 03:38:38 +00:00
}
2019-05-30 00:32:31 +00:00
if ( $r ) {
2016-04-19 03:38:38 +00:00
xchan_query ( $r );
$items = fetch_post_tags ( $r , true );
} else {
2019-05-30 00:32:31 +00:00
$items = [];
2016-04-19 03:38:38 +00:00
}
2019-05-30 00:32:31 +00:00
if ( $format == 'json' ) {
2021-03-15 05:10:44 +00:00
$result = [];
2016-04-19 03:38:38 +00:00
require_once ( 'include/conversation.php' );
2019-05-30 00:32:31 +00:00
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 ));
}
2019-05-30 00:32:31 +00:00
if ( $tag )
2018-11-16 04:52:08 +00:00
$o .= '<h2>' . sprintf ( t ( 'Items tagged with: %s' ), $search ) . '</h2>' ;
2016-04-19 03:38:38 +00:00
else
2018-11-16 04:52:08 +00:00
$o .= '<h2>' . sprintf ( t ( 'Search results for: %s' ), $search ) . '</h2>' ;
2016-04-19 03:38:38 +00:00
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 ;
}
}