6 use Friendica\Content\Feature;
7 use Friendica\Content\Nav;
8 use Friendica\Core\Cache;
9 use Friendica\Core\Config;
10 use Friendica\Core\L10n;
11 use Friendica\Database\DBM;
13 require_once "include/bbcode.php";
14 require_once 'include/security.php';
15 require_once 'include/conversation.php';
16 require_once 'mod/dirfind.php';
18 function search_saved_searches() {
22 if (! Feature::isEnabled(local_user(),'savedsearch'))
25 $r = q("SELECT `id`,`term` FROM `search` WHERE `uid` = %d",
29 if (DBM::is_result($r)) {
34 'term' => $rr['term'],
35 'encodedterm' => urlencode($rr['term']),
36 'delete' => L10n::t('Remove term'),
37 'selected' => ($search==$rr['term']),
42 $tpl = get_markup_template("saved_searches_aside.tpl");
44 $o .= replace_macros($tpl, [
45 '$title' => L10n::t('Saved Searches'),
57 function search_init(App $a) {
59 $search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : '');
62 if (x($_GET,'save') && $search) {
63 $r = q("SELECT * FROM `search` WHERE `uid` = %d AND `term` = '%s' LIMIT 1",
67 if (!DBM::is_result($r)) {
68 dba::insert('search', ['uid' => local_user(), 'term' => $search]);
71 if (x($_GET,'remove') && $search) {
72 dba::delete('search', ['uid' => local_user(), 'term' => $search]);
75 $a->page['aside'] .= search_saved_searches();
78 unset($_SESSION['theme']);
79 unset($_SESSION['mobile-theme']);
88 function search_post(App $a) {
89 if (x($_POST,'search'))
90 $a->data['search'] = $_POST['search'];
94 function search_content(App $a) {
96 if (Config::get('system','block_public') && !local_user() && !remote_user()) {
97 notice(L10n::t('Public access denied.') . EOL);
101 if (Config::get('system','local_search') && !local_user() && !remote_user()) {
102 http_status_exit(403,
103 ["title" => L10n::t("Public access denied."),
104 "description" => L10n::t("Only logged in users are permitted to perform a search.")]);
106 //notice(L10n::t('Public access denied.').EOL);
110 if (Config::get('system','permit_crawling') && !local_user() && !remote_user()) {
112 // 10 requests are "free", after the 11th only a call per minute is allowed
114 $free_crawls = intval(Config::get('system','free_crawls'));
115 if ($free_crawls == 0)
118 $crawl_permit_period = intval(Config::get('system','crawl_permit_period'));
119 if ($crawl_permit_period == 0)
120 $crawl_permit_period = 10;
122 $remote = $_SERVER["REMOTE_ADDR"];
123 $result = Cache::get("remote_search:".$remote);
124 if (!is_null($result)) {
125 $resultdata = json_decode($result);
126 if (($resultdata->time > (time() - $crawl_permit_period)) && ($resultdata->accesses > $free_crawls)) {
127 http_status_exit(429,
128 ["title" => L10n::t("Too Many Requests"),
129 "description" => L10n::t("Only one search per minute is permitted for not logged in users.")]);
132 Cache::set("remote_search:".$remote, json_encode(["time" => time(), "accesses" => $resultdata->accesses + 1]), CACHE_HOUR);
134 Cache::set("remote_search:".$remote, json_encode(["time" => time(), "accesses" => 1]), CACHE_HOUR);
137 Nav::setSelected('search');
140 if (x($a->data,'search'))
141 $search = notags(trim($a->data['search']));
143 $search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : '');
146 if (x($_GET,'tag')) {
148 $search = ((x($_GET,'tag')) ? notags(trim(rawurldecode($_GET['tag']))) : '');
151 // contruct a wrapper for the search header
152 $o .= replace_macros(get_markup_template("content_wrapper.tpl"),[
153 'name' => "search-header",
154 '$title' => L10n::t("Search"),
156 '$content' => search($search,'search-box','search',((local_user()) ? true : false), false)
159 if (strpos($search,'#') === 0) {
161 $search = substr($search,1);
163 if (strpos($search,'@') === 0) {
164 return dirfind_content($a);
166 if (strpos($search,'!') === 0) {
167 return dirfind_content($a);
170 if (x($_GET,'search-option'))
171 switch($_GET['search-option']) {
178 return dirfind_content($a, "@");
181 return dirfind_content($a, "!");
188 if (Config::get('system','only_tag_search'))
191 // Here is the way permissions work in the search module...
192 // Only public posts can be shown
193 // OR your own posts if you are a logged in member
194 // No items will be shown if the member has a blocked profile wall.
197 logger("Start tag search for '".$search."'", LOGGER_DEBUG);
201 STRAIGHT_JOIN `item` ON `item`.`id`=`term`.`oid` %s
202 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'
203 ORDER BY term.created DESC LIMIT %d , %d ",
204 item_fieldlists(), item_joins(), item_condition(),
205 intval(local_user()),
206 intval(TERM_OBJ_POST), intval(TERM_HASHTAG), dbesc(protect_sprintf($search)),
207 intval($a->pager['start']), intval($a->pager['itemspage']));
209 logger("Start fulltext search for '".$search."'", LOGGER_DEBUG);
211 $sql_extra = sprintf(" AND `item`.`body` REGEXP '%s' ", dbesc(protect_sprintf(preg_quote($search))));
215 WHERE %s AND (`item`.`uid` = 0 OR (`item`.`uid` = %s AND NOT `item`.`global`))
217 GROUP BY `item`.`uri`, `item`.`id` ORDER BY `item`.`id` DESC LIMIT %d , %d",
218 item_fieldlists(), item_joins(), item_condition(),
219 intval(local_user()),
220 intval($a->pager['start']), intval($a->pager['itemspage']));
223 if (! DBM::is_result($r)) {
224 info(L10n::t('No results.') . EOL);
230 $title = sprintf(L10n::t('Items tagged with: %s'), $search);
232 $title = sprintf(L10n::t('Results for: %s'), $search);
234 $o .= replace_macros(get_markup_template("section_title.tpl"),[
238 logger("Start Conversation for '".$search."'", LOGGER_DEBUG);
239 $o .= conversation($a,$r,'search',false);
241 $o .= alt_pager($a,count($r));
243 logger("Done '".$search."'", LOGGER_DEBUG);