2010-11-09 01:30:00 +00:00
< ? php
2011-09-09 04:42:52 +00:00
function search_saved_searches () {
2012-11-03 23:40:05 +00:00
if ( ! feature_enabled ( local_user (), 'savedsearch' ))
return '' ;
2011-09-09 04:42:52 +00:00
$o = '' ;
2012-07-12 00:54:00 +00:00
$r = q ( " select `tid`,`term` from `term` WHERE `uid` = %d and type = %d " ,
intval ( local_user ()),
intval ( TERM_SAVEDSEARCH )
2011-09-09 04:42:52 +00:00
);
if ( count ( $r )) {
2011-09-25 21:01:05 +00:00
$o .= '<div id="saved-search-list" class="widget">' ;
2011-09-09 04:42:52 +00:00
$o .= '<h3>' . t ( 'Saved Searches' ) . '</h3>' . " \r \n " ;
2011-09-25 21:01:05 +00:00
$o .= '<ul id="saved-search-ul">' . " \r \n " ;
2011-09-09 04:42:52 +00:00
foreach ( $r as $rr ) {
2012-07-12 00:54:00 +00:00
$o .= '<li class="saved-search-li clear"><a href="search/?f=&remove=1&search=' . $rr [ 'term' ] . '" class="icon drophide savedsearchdrop" title="' . t ( 'Remove term' ) . '" onclick="return confirmDelete();" onmouseover="imgbright(this);" onmouseout="imgdull(this);" ></a> <a href="search/?f=&search=' . $rr [ 'term' ] . '" class="savedsearchterm" >' . htmlspecialchars ( $rr [ 'term' ]) . '</a></li>' . " \r \n " ;
2011-09-09 04:42:52 +00:00
}
2011-10-12 02:27:58 +00:00
$o .= '</ul><div class="clear"></div></div>' . " \r \n " ;
2011-09-09 04:42:52 +00:00
}
return $o ;
}
function search_init ( & $a ) {
2012-07-12 00:54:00 +00:00
$search = (( x ( $_GET , 'search' )) ? trim ( rawurldecode ( $_GET [ 'search' ])) : '' );
2011-09-09 04:42:52 +00:00
if ( local_user ()) {
if ( x ( $_GET , 'save' ) && $search ) {
2012-07-12 00:54:00 +00:00
$r = q ( " select `tid` from `term` where `uid` = %d and `type` = %d and `term` = '%s' limit 1 " ,
2011-09-09 04:42:52 +00:00
intval ( local_user ()),
2012-07-12 00:54:00 +00:00
intval ( TERM_SAVEDSEARCH ),
2011-09-09 04:42:52 +00:00
dbesc ( $search )
);
if ( ! count ( $r )) {
2012-07-12 00:54:00 +00:00
q ( " insert into `term` ( `uid`,`type`,`term` ) values ( %d, %d, '%s') " ,
2011-09-09 04:42:52 +00:00
intval ( local_user ()),
2012-07-12 00:54:00 +00:00
intval ( TERM_SAVEDSEARCH ),
2011-09-09 04:42:52 +00:00
dbesc ( $search )
);
}
}
if ( x ( $_GET , 'remove' ) && $search ) {
2012-07-12 00:54:00 +00:00
q ( " delete from `term` where `uid` = %d and `type` = %d and `term` = '%s' limit 1 " ,
2011-09-09 04:42:52 +00:00
intval ( local_user ()),
2012-07-12 00:54:00 +00:00
intval ( TERM_SAVEDSEARCH ),
2011-09-09 04:42:52 +00:00
dbesc ( $search )
);
}
$a -> page [ 'aside' ] .= search_saved_searches ();
}
2012-09-10 04:17:06 +00:00
else {
2011-10-12 02:27:58 +00:00
unset ( $_SESSION [ 'theme' ]);
2012-09-10 04:17:06 +00:00
unset ( $_SESSION [ 'mobile-theme' ]);
}
2011-10-12 02:27:58 +00:00
2011-09-09 04:42:52 +00:00
}
2010-11-09 01:30:00 +00:00
2010-12-13 02:43:32 +00:00
function search_post ( & $a ) {
if ( x ( $_POST , 'search' ))
$a -> data [ 'search' ] = $_POST [ 'search' ];
}
2013-05-10 01:08:00 +00:00
function search_content ( & $a , $update = 0 , $load = false ) {
2010-11-09 01:30:00 +00:00
2011-04-22 00:29:47 +00:00
if (( get_config ( 'system' , 'block_public' )) && ( ! local_user ()) && ( ! remote_user ())) {
notice ( t ( 'Public access denied.' ) . EOL );
return ;
}
2012-05-26 09:51:48 +00:00
2011-08-17 16:36:24 +00:00
nav_set_selected ( 'search' );
2011-04-22 00:29:47 +00:00
2011-04-13 00:58:16 +00:00
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' );
2011-04-13 00:58:16 +00:00
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 " ;
2012-05-20 04:53:27 +00:00
$o .= '<h3>' . t ( 'Search' ) . '</h3>' ;
2010-11-09 01:30:00 +00:00
2010-12-13 02:43:32 +00:00
if ( x ( $a -> data , 'search' ))
2012-07-12 00:54:00 +00:00
$search = trim ( $a -> data [ 'search' ]);
2010-12-13 02:43:32 +00:00
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
}
2011-09-09 04:42:52 +00:00
$o .= search ( $search , 'search-box' , '/search' ,(( local_user ()) ? true : false ));
2010-11-09 01:30:00 +00:00
2012-05-19 09:42:11 +00:00
if ( strpos ( $search , '#' ) === 0 ) {
$tag = true ;
$search = substr ( $search , 1 );
}
2012-05-20 04:53:27 +00:00
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 );
2012-05-20 04:53:27 +00:00
}
2012-05-19 09:42:11 +00:00
2013-01-22 10:56:32 +00:00
// 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 ;
2012-07-11 02:28:02 +00:00
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 {
if ( get_config ( 'system' , 'use_fulltext_engine' ))
2012-05-29 23:50:42 +00:00
$sql_extra = sprintf ( " AND MATCH (`item`.`body`) AGAINST (' " . '"%s"' . " ' in boolean mode) " , dbesc ( protect_sprintf ( $search )));
2012-05-26 01:21:07 +00:00
else
2012-05-29 23:50:42 +00:00
$sql_extra = sprintf ( " AND `item`.`body` REGEXP '%s' " , dbesc ( protect_sprintf ( preg_quote ( $search ))));
2012-05-26 01:21:07 +00:00
}
2012-04-24 05:41:32 +00:00
2011-07-03 12:00:11 +00:00
// Here is the way permissions work in the search module...
2012-03-20 21:55:18 +00:00
// Only public posts can be shown
2011-07-03 12:00:11 +00:00
// OR your own posts if you are a logged in member
2012-03-20 21:55:18 +00:00
// 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.
2013-02-10 12:55:29 +00:00
$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 ),
2013-06-21 00:09:11 +00:00
'$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' => ''
));
}
if (( $update ) && ( $load )) {
$pager_sql = sprintf ( " LIMIT %d, %d " , intval ( $a -> pager [ 'start' ]), intval ( $a -> pager [ 'itemspage' ]));
2013-05-10 01:08:00 +00:00
dbg ( 1 );
2013-01-03 07:07:46 +00:00
if ( $load ) {
2013-03-22 01:25:41 +00:00
$r = q ( " SELECT distinct(mid), item.* from item
2013-01-03 07:07:46 +00:00
WHERE item_restrict = 0
2013-01-20 06:21:00 +00:00
AND (( `item` . `allow_cid` = '' AND `item` . `allow_gid` = '' AND `item` . `deny_cid` = '' AND `item` . `deny_gid` = '' AND item_private = 0 )
2013-01-03 07:07:46 +00:00
OR ( `item` . `uid` = % d ))
$sql_extra
2013-03-22 01:25:41 +00:00
group by mid ORDER BY created DESC $pager_sql " ,
2013-01-03 07:07:46 +00:00
intval ( local_user ()),
intval ( ABOOK_FLAG_BLOCKED )
);
2013-05-10 01:08:00 +00:00
dbg ( 0 );
2013-01-03 07:07:46 +00:00
}
else {
$r = array ();
}
}
if ( $r ) {
$parents_str = ids_to_querystr ( $r , 'item_id' );
$items = q ( " SELECT `item`.*, `item`.`id` AS `item_id`
FROM `item`
2013-05-10 01:08:00 +00:00
WHERE item_restrict = 0
2013-01-03 07:07:46 +00:00
$sql_extra " ,
intval ( $a -> profile [ 'profile_uid' ]),
dbesc ( $parents_str )
);
xchan_query ( $items );
2013-02-11 08:20:14 +00:00
$items = fetch_post_tags ( $items , true );
2013-01-03 07:07:46 +00:00
$items = conv_sort ( $items , 'created' );
} else {
$items = array ();
2010-11-09 01:30:00 +00:00
}
2013-01-03 07:07:46 +00:00
2013-05-10 01:08:00 +00:00
// $r = q("SELECT distinct(`item`.`mid`), `item`.*, `item`.`id` AS `item_id`,
// `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`alias`, `contact`.`rel`,
// `contact`.`network`, `contact`.`thumb`, `contact`.`self`, `contact`.`writable`,
// `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`,
// `user`.`nickname`
// FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
// LEFT JOIN `user` ON `user`.`uid` = `item`.`uid`
// WHERE `item`.`visible` = 1 AND `item`.`deleted` = 0 and `item`.`moderated` = 0
// AND (( `item`.`allow_cid` = '' AND `item`.`allow_gid` = '' AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = '' AND `item`.`private` = 0 AND `user`.`hidewall` = 0 )
// OR `item`.`uid` = %d )
// AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
// $sql_extra
// group by `item`.`mid`
// ORDER BY `received` DESC LIMIT %d , %d ",
// intval(local_user()),
// intval($a->pager['start']),
// intval($a->pager['itemspage'])
2011-07-05 03:57:07 +00:00
2013-05-10 01:08:00 +00:00
// );
2010-11-09 01:30:00 +00:00
2012-07-11 02:28:02 +00:00
2013-02-11 08:20:14 +00:00
// $a = fetch_post_tags($a,true);
2012-07-11 02:28:02 +00:00
2013-05-10 01:08:00 +00:00
// if(! $items) {//
// info( t('No results.') . EOL);
// return $o;
// }
2012-07-15 03:39:46 +00:00
2012-04-24 05:41:32 +00:00
if ( $tag )
2012-07-12 00:54:00 +00:00
$o .= '<h2>Items tagged with: ' . htmlspecialchars ( $search ) . '</h2>' ;
2012-04-24 05:41:32 +00:00
else
2012-07-12 00:54:00 +00:00
$o .= '<h2>Search results for: ' . htmlspecialchars ( $search ) . '</h2>' ;
2011-04-11 08:31:04 +00:00
2013-05-10 01:08:00 +00:00
$o .= conversation ( $a , $items , 'search' , $update , 'client' );
2010-11-25 02:37:10 +00:00
2013-05-10 01:08:00 +00:00
// $o .= alt_pager($a,count($r));
2010-11-25 02:37:10 +00:00
2010-11-09 01:30:00 +00:00
return $o ;
}