streams/mod/search.php

168 lines
4.7 KiB
PHP
Raw Normal View History

2010-11-09 01:30:00 +00:00
<?php
function search_init(&$a) {
if(x($_REQUEST,'search'))
$a->data['search'] = $_REQUEST['search'];
}
function search_content(&$a,$update = 0, $load = false) {
2010-11-09 01:30:00 +00:00
if((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
notice( t('Public access denied.') . EOL);
return;
}
nav_set_selected('search');
require_once("include/bbcode.php");
require_once('include/security.php');
require_once('include/conversation.php');
2012-09-10 04:17:06 +00:00
require_once('include/items.php');
2013-01-03 07:07:46 +00:00
$observer = $a->get_observer();
2010-11-09 01:30:00 +00:00
$o = '<div id="live-search"></div>' . "\r\n";
$o .= '<h3>' . t('Search') . '</h3>';
2010-11-09 01:30:00 +00:00
if(x($a->data,'search'))
2012-07-12 00:54:00 +00:00
$search = trim($a->data['search']);
else
2012-07-12 00:54:00 +00:00
$search = ((x($_GET,'search')) ? trim(rawurldecode($_GET['search'])) : '');
2010-11-09 01:30:00 +00:00
2012-04-24 05:41:32 +00:00
$tag = false;
if(x($_GET,'tag')) {
$tag = true;
2012-07-12 00:54:00 +00:00
$search = ((x($_GET,'tag')) ? trim(rawurldecode($_GET['tag'])) : '');
2012-04-24 05:41:32 +00:00
}
$o .= search($search,'search-box','/search',((local_user()) ? true : false));
2010-11-09 01:30:00 +00:00
if(strpos($search,'#') === 0) {
$tag = true;
$search = substr($search,1);
}
if(strpos($search,'@') === 0) {
2013-01-19 03:15:08 +00:00
$search = substr($search,1);
2013-01-19 03:16:27 +00:00
goaway(z_root() . '/directory' . '?f=1&search=' . $search);
}
// look for a naked webbie
if(strpos($search,'@') !== false) {
goaway(z_root() . '/directory' . '?f=1&search=' . $search);
}
2010-11-09 01:30:00 +00:00
if(! $search)
return $o;
if($tag) {
$sql_extra = sprintf(" AND `item`.`id` IN (select `oid` from term where otype = %d and type = %d and term = '%s') ",
intval(TERM_OBJ_POST),
intval(TERM_HASHTAG),
dbesc(protect_sprintf($search))
);
}
else {
$sql_extra = sprintf(" AND `item`.`body` REGEXP '%s' ", dbesc(protect_sprintf(preg_quote($search))));
}
2012-04-24 05:41:32 +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.
2010-11-09 01:30:00 +00:00
2013-01-03 07:07:46 +00:00
if((! $update) && (! $load)) {
// 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";
2013-06-18 09:06:16 +00:00
$o .= "<script> var profile_uid = " . ((intval(local_user())) ? local_user() : (-1))
2013-01-03 07:07:46 +00:00
. "; var netargs = '?f='; var profile_page = " . $a->pager['page'] . "; </script>\r\n";
$a->page['htmlhead'] .= replace_macros(get_markup_template("build_query.tpl"),array(
'$baseurl' => z_root(),
'$pgtype' => 'search',
'$uid' => (($a->profile['profile_uid']) ? $a->profile['profile_uid'] : '0'),
'$gid' => '0',
'$cid' => '0',
'$cmin' => '0',
'$cmax' => '0',
'$star' => '0',
'$liked' => '0',
'$conv' => '0',
'$spam' => '0',
'$nouveau' => '0',
'$wall' => '0',
'$page' => (($a->pager['page'] != 1) ? $a->pager['page'] : 1),
'$search' => (($tag) ? urlencode('#') : '') . $search,
2013-01-03 07:07:46 +00:00
'$order' => '',
'$file' => '',
'$cats' => '',
2013-03-25 19:20:12 +00:00
'$mid' => '',
2013-01-03 07:07:46 +00:00
'$dend' => '',
'$dbegin' => ''
));
}
$pub_sql = public_permissions_sql(get_observer_hash());
2013-01-03 07:07:46 +00:00
if(($update) && ($load)) {
$itemspage = get_pconfig(local_user(),'system','itemspage');
$a->set_pager_itemspage(((intval($itemspage)) ? $itemspage : 20));
2013-01-03 07:07:46 +00:00
$pager_sql = sprintf(" LIMIT %d, %d ",intval($a->pager['start']), intval($a->pager['itemspage']));
2013-01-03 07:07:46 +00:00
if($load) {
$r = null;
if(local_user()) {
$r = q("SELECT distinct mid, item.id as item_id, item.* from item
WHERE item_restrict = 0
AND (( `item`.`allow_cid` = '' AND `item`.`allow_gid` = '' AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = '' AND item_private = 0 )
OR ( `item`.`uid` = %d ))
$sql_extra
group by mid ORDER BY created DESC $pager_sql ",
intval(local_user()),
intval(ABOOK_FLAG_BLOCKED)
2013-01-03 07:07:46 +00:00
);
}
if($r === null) {
$r = q("SELECT distinct mid, item.id as item_id, item.* from item
WHERE item_restrict = 0
AND ((( `item`.`allow_cid` = '' AND `item`.`allow_gid` = '' AND `item`.`deny_cid` = ''
AND `item`.`deny_gid` = '' AND item_private = 0 )
and owner_xchan in ( " . stream_perms_xchans(($observer) ? PERMS_NETWORK : PERMS_PUBLIC) . " ))
$pub_sql )
$sql_extra
group by mid ORDER BY created DESC $pager_sql"
);
}
2013-01-03 07:07:46 +00:00
}
else {
$r = array();
}
}
if($r) {
xchan_query($r);
$items = fetch_post_tags($r,true);
2013-01-03 07:07:46 +00:00
} else {
$items = array();
2010-11-09 01:30:00 +00:00
}
2012-04-24 05:41:32 +00:00
if($tag)
$o .= '<h2>Items tagged with: ' . htmlspecialchars($search, ENT_COMPAT,'UTF-8') . '</h2>';
2012-04-24 05:41:32 +00:00
else
$o .= '<h2>Search results for: ' . htmlspecialchars($search, ENT_COMPAT,'UTF-8') . '</h2>';
$o .= conversation($a,$items,'search',$update,'client');
2010-11-25 02:37:10 +00:00
2010-11-09 01:30:00 +00:00
return $o;
}