2 require_once("include/bbcode.php");
3 require_once('include/security.php');
4 require_once('include/conversation.php');
5 require_once('mod/dirfind.php');
7 if(! function_exists('search_saved_searches')) {
8 function search_saved_searches() {
12 if(! feature_enabled(local_user(),'savedsearch'))
15 $r = q("SELECT `id`,`term` FROM `search` WHERE `uid` = %d",
24 'term' => $rr['term'],
25 'encodedterm' => urlencode($rr['term']),
26 'delete' => t('Remove term'),
27 'selected' => ($search==$rr['term']),
32 $tpl = get_markup_template("saved_searches_aside.tpl");
34 $o .= replace_macros($tpl, array(
35 '$title' => t('Saved Searches'),
46 if(! function_exists('search_init')) {
47 function search_init(&$a) {
49 $search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : '');
52 if(x($_GET,'save') && $search) {
53 $r = q("SELECT * FROM `search` WHERE `uid` = %d AND `term` = '%s' LIMIT 1",
58 q("INSERT INTO `search` (`uid`,`term`) VALUES ( %d, '%s')",
64 if(x($_GET,'remove') && $search) {
65 q("delete from `search` where `uid` = %d and `term` = '%s' limit 1",
71 $a->page['aside'] .= search_saved_searches();
75 unset($_SESSION['theme']);
76 unset($_SESSION['mobile-theme']);
84 if(! function_exists('search_post')) {
85 function search_post(&$a) {
86 if(x($_POST,'search'))
87 $a->data['search'] = $_POST['search'];
91 if(! function_exists('search_content')) {
92 function search_content(&$a) {
94 if((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
95 notice( t('Public access denied.') . EOL);
99 if(get_config('system','local_search') AND !local_user()) {
100 http_status_exit(403,
101 array("title" => t("Public access denied."),
102 "description" => t("Only logged in users are permitted to perform a search.")));
104 //notice(t('Public access denied.').EOL);
108 if (get_config('system','permit_crawling') AND !local_user()) {
110 // 10 requests are "free", after the 11th only a call per minute is allowed
112 $free_crawls = intval(get_config('system','free_crawls'));
113 if ($free_crawls == 0)
116 $crawl_permit_period = intval(get_config('system','crawl_permit_period'));
117 if ($crawl_permit_period == 0)
118 $crawl_permit_period = 10;
120 $remote = $_SERVER["REMOTE_ADDR"];
121 $result = Cache::get("remote_search:".$remote);
122 if (!is_null($result)) {
123 $resultdata = json_decode($result);
124 if (($resultdata->time > (time() - $crawl_permit_period)) AND ($resultdata->accesses > $free_crawls)) {
125 http_status_exit(429,
126 array("title" => t("Too Many Requests"),
127 "description" => t("Only one search per minute is permitted for not logged in users.")));
130 Cache::set("remote_search:".$remote, json_encode(array("time" => time(), "accesses" => $resultdata->accesses + 1)), CACHE_HOUR);
132 Cache::set("remote_search:".$remote, json_encode(array("time" => time(), "accesses" => 1)), CACHE_HOUR);
135 nav_set_selected('search');
138 $o = '<h3>' . t('Search') . '</h3>';
140 if(x($a->data,'search'))
141 $search = notags(trim($a->data['search']));
143 $search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : '');
148 $search = ((x($_GET,'tag')) ? notags(trim(rawurldecode($_GET['tag']))) : '');
152 $o .= search($search,'search-box','/search',((local_user()) ? true : false), false);
154 if(strpos($search,'#') === 0) {
156 $search = substr($search,1);
158 if(strpos($search,'@') === 0) {
159 return dirfind_content($a);
161 if(strpos($search,'!') === 0) {
162 return dirfind_content($a);
165 if(x($_GET,'search-option'))
166 switch($_GET['search-option']) {
173 return dirfind_content($a, "@");
176 return dirfind_content($a, "!");
183 if (get_config('system','only_tag_search'))
186 // Here is the way permissions work in the search module...
187 // Only public posts can be shown
188 // OR your own posts if you are a logged in member
189 // No items will be shown if the member has a blocked profile wall.
192 logger("Start tag search for '".$search."'", LOGGER_DEBUG);
194 $r = q("SELECT STRAIGHT_JOIN `item`.`uri`, `item`.*, `item`.`id` AS `item_id`,
195 `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`alias`, `contact`.`rel`,
196 `contact`.`network`, `contact`.`thumb`, `contact`.`self`, `contact`.`writable`,
197 `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
199 INNER JOIN `item` ON `item`.`id`=`term`.`oid`
200 INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id` AND NOT `contact`.`blocked` AND NOT `contact`.`pending`
201 WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
202 AND (`term`.`uid` = 0 OR (`term`.`uid` = %d AND NOT `term`.`global`)) AND `term`.`otype` = %d AND `term`.`type` = %d AND `term`.`term` = '%s'
203 ORDER BY term.created DESC LIMIT %d , %d ",
204 intval(local_user()), intval(TERM_OBJ_POST), intval(TERM_HASHTAG), dbesc(protect_sprintf($search)),
205 intval($a->pager['start']), intval($a->pager['itemspage']));
207 logger("Start fulltext search for '".$search."'", LOGGER_DEBUG);
209 if (get_config('system','use_fulltext_engine')) {
210 $sql_extra = sprintf(" AND MATCH (`item`.`body`, `item`.`title`) AGAINST ('%s' in boolean mode) ", dbesc(protect_sprintf($search)));
212 $sql_extra = sprintf(" AND `item`.`body` REGEXP '%s' ", dbesc(protect_sprintf(preg_quote($search))));
215 $r = q("SELECT STRAIGHT_JOIN `item`.`uri`, `item`.*, `item`.`id` AS `item_id`,
216 `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`alias`, `contact`.`rel`,
217 `contact`.`network`, `contact`.`thumb`, `contact`.`self`, `contact`.`writable`,
218 `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
220 INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id` AND NOT `contact`.`blocked` AND NOT `contact`.`pending`
221 WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
222 AND (`item`.`uid` = 0 OR (`item`.`uid` = %s AND (`item`.`private` OR NOT `item`.`network` IN ('%s', '%s', '%s'))))
224 GROUP BY `item`.`uri` ORDER BY `item`.`id` DESC LIMIT %d , %d ",
225 intval(local_user()), dbesc(NETWORK_DFRN), dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DIASPORA),
226 intval($a->pager['start']), intval($a->pager['itemspage']));
230 info( t('No results.') . EOL);
236 $title = sprintf( t('Items tagged with: %s'), $search);
238 $title = sprintf( t('Search results for: %s'), $search);
240 $o .= replace_macros(get_markup_template("section_title.tpl"),array(
244 logger("Start Conversation for '".$search."'", LOGGER_DEBUG);
245 $o .= conversation($a,$r,'search',false);
247 $o .= alt_pager($a,count($r));
249 logger("Done '".$search."'", LOGGER_DEBUG);