streams/Zotlabs/Module/Search.php

228 lines
6.2 KiB
PHP
Raw Normal View History

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;
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() {
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-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);
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
}
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
}
$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
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
2019-05-30 00:32:31 +00:00
if (strpos($search,'@') !== false) {
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) {
$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),
dbesc(protect_sprintf($wildtag))
2016-04-19 03:38:38 +00:00
);
}
else {
$regstr = db_getfunc('REGEXP');
$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.
2019-05-30 00:32:31 +00:00
if ((! $update) && (! $load)) {
2016-04-19 03:38:38 +00:00
$static = ((local_channel()) ? channel_manual_conv_update(local_channel()) : 0);
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',
2019-05-30 00:32:31 +00:00
'$uid' => ((\App::$profile['profile_uid']) ? App::$profile['profile_uid'] : '0'),
2016-04-19 03:38:38 +00:00
'$gid' => '0',
'$cid' => '0',
'$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',
'$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,
'$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
}
$item_normal = item_normal_search();
2016-04-19 03:38:38 +00:00
$pub_sql = public_permissions_sql($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));
2016-04-19 03:38:38 +00:00
$pager_sql = sprintf(" LIMIT %d OFFSET %d ", intval(\App::$pager['itemspage']), intval(\App::$pager['start']));
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 ",
intval(local_channel())
2016-04-19 03:38:38 +00:00
);
}
2019-05-30 00:32:31 +00:00
if ($r === null) {
2019-10-06 20:29:01 +00:00
$r = q("SELECT mid, MAX(id) as item_id from item $pub_sql
2016-04-19 03:38:38 +00:00
$item_normal
$sql_extra
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');
$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') {
2016-04-19 03:38:38 +00:00
$result = array();
require_once('include/conversation.php');
2019-05-30 00:32:31 +00:00
foreach ($items as $item) {
$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
$o .= conversation($items,'search',$update,'client');
2016-04-19 03:38:38 +00:00
$o .= '</div>';
return $o;
}
}