5 require_once("include/bbcode.php");
6 require_once('include/security.php');
7 require_once('include/conversation.php');
8 require_once('mod/dirfind.php');
10 function search_saved_searches() {
14 if (! feature_enabled(local_user(),'savedsearch'))
17 $r = q("SELECT `id`,`term` FROM `search` WHERE `uid` = %d",
21 if (dbm::is_result($r)) {
26 'term' => $rr['term'],
27 'encodedterm' => urlencode($rr['term']),
28 'delete' => t('Remove term'),
29 'selected' => ($search==$rr['term']),
34 $tpl = get_markup_template("saved_searches_aside.tpl");
36 $o .= replace_macros($tpl, array(
37 '$title' => t('Saved Searches'),
49 function search_init(App $a) {
51 $search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : '');
54 if (x($_GET,'save') && $search) {
55 $r = q("SELECT * FROM `search` WHERE `uid` = %d AND `term` = '%s' LIMIT 1",
59 if (!dbm::is_result($r)) {
60 dba::insert('search', array('uid' => local_user(), 'term' => $search));
63 if (x($_GET,'remove') && $search) {
64 dba::delete('search', array('uid' => local_user(), 'term' => $search));
67 $a->page['aside'] .= search_saved_searches();
70 unset($_SESSION['theme']);
71 unset($_SESSION['mobile-theme']);
80 function search_post(App $a) {
81 if (x($_POST,'search'))
82 $a->data['search'] = $_POST['search'];
86 function search_content(App $a) {
88 if (get_config('system','block_public') && !local_user() && !remote_user()) {
89 notice(t('Public access denied.') . EOL);
93 if (get_config('system','local_search') && !local_user() && !remote_user()) {
95 array("title" => t("Public access denied."),
96 "description" => t("Only logged in users are permitted to perform a search.")));
98 //notice(t('Public access denied.').EOL);
102 if (get_config('system','permit_crawling') && !local_user() && !remote_user()) {
104 // 10 requests are "free", after the 11th only a call per minute is allowed
106 $free_crawls = intval(get_config('system','free_crawls'));
107 if ($free_crawls == 0)
110 $crawl_permit_period = intval(get_config('system','crawl_permit_period'));
111 if ($crawl_permit_period == 0)
112 $crawl_permit_period = 10;
114 $remote = $_SERVER["REMOTE_ADDR"];
115 $result = Cache::get("remote_search:".$remote);
116 if (!is_null($result)) {
117 $resultdata = json_decode($result);
118 if (($resultdata->time > (time() - $crawl_permit_period)) && ($resultdata->accesses > $free_crawls)) {
119 http_status_exit(429,
120 array("title" => t("Too Many Requests"),
121 "description" => t("Only one search per minute is permitted for not logged in users.")));
124 Cache::set("remote_search:".$remote, json_encode(array("time" => time(), "accesses" => $resultdata->accesses + 1)), CACHE_HOUR);
126 Cache::set("remote_search:".$remote, json_encode(array("time" => time(), "accesses" => 1)), CACHE_HOUR);
129 nav_set_selected('search');
131 if (x($a->data,'search'))
132 $search = notags(trim($a->data['search']));
134 $search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : '');
137 if (x($_GET,'tag')) {
139 $search = ((x($_GET,'tag')) ? notags(trim(rawurldecode($_GET['tag']))) : '');
142 // contruct a wrapper for the search header
143 $o .= replace_macros(get_markup_template("content_wrapper.tpl"),array(
144 'name' => "search-header",
145 '$title' => t("Search"),
147 '$content' => search($search,'search-box','search',((local_user()) ? true : false), false)
150 if (strpos($search,'#') === 0) {
152 $search = substr($search,1);
154 if (strpos($search,'@') === 0) {
155 return dirfind_content($a);
157 if (strpos($search,'!') === 0) {
158 return dirfind_content($a);
161 if (x($_GET,'search-option'))
162 switch($_GET['search-option']) {
169 return dirfind_content($a, "@");
172 return dirfind_content($a, "!");
179 if (get_config('system','only_tag_search'))
182 // Here is the way permissions work in the search module...
183 // Only public posts can be shown
184 // OR your own posts if you are a logged in member
185 // No items will be shown if the member has a blocked profile wall.
188 logger("Start tag search for '".$search."'", LOGGER_DEBUG);
192 STRAIGHT_JOIN `item` ON `item`.`id`=`term`.`oid` %s
193 WHERE %s AND (`term`.`uid` = 0 OR (`term`.`uid` = %d AND NOT `term`.`global`)) AND `term`.`otype` = %d AND `term`.`type` = %d AND `term`.`term` = '%s'
194 ORDER BY term.created DESC LIMIT %d , %d ",
195 item_fieldlists(), item_joins(), item_condition(),
196 intval(local_user()),
197 intval(TERM_OBJ_POST), intval(TERM_HASHTAG), dbesc(protect_sprintf($search)),
198 intval($a->pager['start']), intval($a->pager['itemspage']));
200 logger("Start fulltext search for '".$search."'", LOGGER_DEBUG);
202 $sql_extra = sprintf(" AND `item`.`body` REGEXP '%s' ", dbesc(protect_sprintf(preg_quote($search))));
206 WHERE %s AND (`item`.`uid` = 0 OR (`item`.`uid` = %s AND NOT `item`.`global`))
208 GROUP BY `item`.`uri`, `item`.`id` ORDER BY `item`.`id` DESC LIMIT %d , %d",
209 item_fieldlists(), item_joins(), item_condition(),
210 intval(local_user()),
211 intval($a->pager['start']), intval($a->pager['itemspage']));
214 if (! dbm::is_result($r)) {
215 info( t('No results.') . EOL);
221 $title = sprintf( t('Items tagged with: %s'), $search);
223 $title = sprintf( t('Results for: %s'), $search);
225 $o .= replace_macros(get_markup_template("section_title.tpl"),array(
229 logger("Start Conversation for '".$search."'", LOGGER_DEBUG);
230 $o .= conversation($a,$r,'search',false);
232 $o .= alt_pager($a,count($r));
234 logger("Done '".$search."'", LOGGER_DEBUG);