4 use Friendica\Core\Config;
6 require_once("include/bbcode.php");
7 require_once('include/security.php');
8 require_once('include/conversation.php');
9 require_once('mod/dirfind.php');
11 function search_saved_searches() {
15 if (! feature_enabled(local_user(),'savedsearch'))
18 $r = q("SELECT `id`,`term` FROM `search` WHERE `uid` = %d",
22 if (dbm::is_result($r)) {
27 'term' => $rr['term'],
28 'encodedterm' => urlencode($rr['term']),
29 'delete' => t('Remove term'),
30 'selected' => ($search==$rr['term']),
35 $tpl = get_markup_template("saved_searches_aside.tpl");
37 $o .= replace_macros($tpl, array(
38 '$title' => t('Saved Searches'),
50 function search_init(App $a) {
52 $search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : '');
55 if (x($_GET,'save') && $search) {
56 $r = q("SELECT * FROM `search` WHERE `uid` = %d AND `term` = '%s' LIMIT 1",
60 if (!dbm::is_result($r)) {
61 dba::insert('search', array('uid' => local_user(), 'term' => $search));
64 if (x($_GET,'remove') && $search) {
65 dba::delete('search', array('uid' => local_user(), 'term' => $search));
68 $a->page['aside'] .= search_saved_searches();
71 unset($_SESSION['theme']);
72 unset($_SESSION['mobile-theme']);
81 function search_post(App $a) {
82 if (x($_POST,'search'))
83 $a->data['search'] = $_POST['search'];
87 function search_content(App $a) {
89 if (Config::get('system','block_public') && !local_user() && !remote_user()) {
90 notice(t('Public access denied.') . EOL);
94 if (Config::get('system','local_search') && !local_user() && !remote_user()) {
96 array("title" => t("Public access denied."),
97 "description" => t("Only logged in users are permitted to perform a search.")));
99 //notice(t('Public access denied.').EOL);
103 if (Config::get('system','permit_crawling') && !local_user() && !remote_user()) {
105 // 10 requests are "free", after the 11th only a call per minute is allowed
107 $free_crawls = intval(Config::get('system','free_crawls'));
108 if ($free_crawls == 0)
111 $crawl_permit_period = intval(Config::get('system','crawl_permit_period'));
112 if ($crawl_permit_period == 0)
113 $crawl_permit_period = 10;
115 $remote = $_SERVER["REMOTE_ADDR"];
116 $result = Cache::get("remote_search:".$remote);
117 if (!is_null($result)) {
118 $resultdata = json_decode($result);
119 if (($resultdata->time > (time() - $crawl_permit_period)) && ($resultdata->accesses > $free_crawls)) {
120 http_status_exit(429,
121 array("title" => t("Too Many Requests"),
122 "description" => t("Only one search per minute is permitted for not logged in users.")));
125 Cache::set("remote_search:".$remote, json_encode(array("time" => time(), "accesses" => $resultdata->accesses + 1)), CACHE_HOUR);
127 Cache::set("remote_search:".$remote, json_encode(array("time" => time(), "accesses" => 1)), CACHE_HOUR);
130 nav_set_selected('search');
132 if (x($a->data,'search'))
133 $search = notags(trim($a->data['search']));
135 $search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : '');
138 if (x($_GET,'tag')) {
140 $search = ((x($_GET,'tag')) ? notags(trim(rawurldecode($_GET['tag']))) : '');
143 // contruct a wrapper for the search header
144 $o .= replace_macros(get_markup_template("content_wrapper.tpl"),array(
145 'name' => "search-header",
146 '$title' => t("Search"),
148 '$content' => search($search,'search-box','search',((local_user()) ? true : false), false)
151 if (strpos($search,'#') === 0) {
153 $search = substr($search,1);
155 if (strpos($search,'@') === 0) {
156 return dirfind_content($a);
158 if (strpos($search,'!') === 0) {
159 return dirfind_content($a);
162 if (x($_GET,'search-option'))
163 switch($_GET['search-option']) {
170 return dirfind_content($a, "@");
173 return dirfind_content($a, "!");
180 if (Config::get('system','only_tag_search'))
183 // Here is the way permissions work in the search module...
184 // Only public posts can be shown
185 // OR your own posts if you are a logged in member
186 // No items will be shown if the member has a blocked profile wall.
189 logger("Start tag search for '".$search."'", LOGGER_DEBUG);
193 STRAIGHT_JOIN `item` ON `item`.`id`=`term`.`oid` %s
194 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'
195 ORDER BY term.created DESC LIMIT %d , %d ",
196 item_fieldlists(), item_joins(), item_condition(),
197 intval(local_user()),
198 intval(TERM_OBJ_POST), intval(TERM_HASHTAG), dbesc(protect_sprintf($search)),
199 intval($a->pager['start']), intval($a->pager['itemspage']));
201 logger("Start fulltext search for '".$search."'", LOGGER_DEBUG);
203 $sql_extra = sprintf(" AND `item`.`body` REGEXP '%s' ", dbesc(protect_sprintf(preg_quote($search))));
207 WHERE %s AND (`item`.`uid` = 0 OR (`item`.`uid` = %s AND NOT `item`.`global`))
209 GROUP BY `item`.`uri`, `item`.`id` ORDER BY `item`.`id` DESC LIMIT %d , %d",
210 item_fieldlists(), item_joins(), item_condition(),
211 intval(local_user()),
212 intval($a->pager['start']), intval($a->pager['itemspage']));
215 if (! dbm::is_result($r)) {
216 info( t('No results.') . EOL);
222 $title = sprintf( t('Items tagged with: %s'), $search);
224 $title = sprintf( t('Results for: %s'), $search);
226 $o .= replace_macros(get_markup_template("section_title.tpl"),array(
230 logger("Start Conversation for '".$search."'", LOGGER_DEBUG);
231 $o .= conversation($a,$r,'search',false);
233 $o .= alt_pager($a,count($r));
235 logger("Done '".$search."'", LOGGER_DEBUG);