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\Core\System;
12 use Friendica\Database\DBM;
14 require_once "include/bbcode.php";
15 require_once 'include/security.php';
16 require_once 'include/conversation.php';
17 require_once 'mod/dirfind.php';
19 function search_saved_searches() {
22 $search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : '');
24 if (! Feature::isEnabled(local_user(),'savedsearch'))
27 $r = q("SELECT `id`,`term` FROM `search` WHERE `uid` = %d",
31 if (DBM::is_result($r)) {
36 'term' => $rr['term'],
37 'encodedterm' => urlencode($rr['term']),
38 'delete' => L10n::t('Remove term'),
39 'selected' => ($search==$rr['term']),
44 $tpl = get_markup_template("saved_searches_aside.tpl");
46 $o .= replace_macros($tpl, [
47 '$title' => L10n::t('Saved Searches'),
59 function search_init(App $a) {
61 $search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : '');
64 if (x($_GET,'save') && $search) {
65 $r = q("SELECT * FROM `search` WHERE `uid` = %d AND `term` = '%s' LIMIT 1",
69 if (!DBM::is_result($r)) {
70 dba::insert('search', ['uid' => local_user(), 'term' => $search]);
73 if (x($_GET,'remove') && $search) {
74 dba::delete('search', ['uid' => local_user(), 'term' => $search]);
77 $a->page['aside'] .= search_saved_searches();
80 unset($_SESSION['theme']);
81 unset($_SESSION['mobile-theme']);
90 function search_post(App $a) {
91 if (x($_POST,'search'))
92 $a->data['search'] = $_POST['search'];
96 function search_content(App $a) {
98 if (Config::get('system','block_public') && !local_user() && !remote_user()) {
99 notice(L10n::t('Public access denied.') . EOL);
103 if (Config::get('system','local_search') && !local_user() && !remote_user()) {
104 System::httpExit(403,
105 ["title" => L10n::t("Public access denied."),
106 "description" => L10n::t("Only logged in users are permitted to perform a search.")]);
108 //notice(L10n::t('Public access denied.').EOL);
112 if (Config::get('system','permit_crawling') && !local_user() && !remote_user()) {
114 // 10 requests are "free", after the 11th only a call per minute is allowed
116 $free_crawls = intval(Config::get('system','free_crawls'));
117 if ($free_crawls == 0)
120 $crawl_permit_period = intval(Config::get('system','crawl_permit_period'));
121 if ($crawl_permit_period == 0)
122 $crawl_permit_period = 10;
124 $remote = $_SERVER["REMOTE_ADDR"];
125 $result = Cache::get("remote_search:".$remote);
126 if (!is_null($result)) {
127 $resultdata = json_decode($result);
128 if (($resultdata->time > (time() - $crawl_permit_period)) && ($resultdata->accesses > $free_crawls)) {
129 System::httpExit(429,
130 ["title" => L10n::t("Too Many Requests"),
131 "description" => L10n::t("Only one search per minute is permitted for not logged in users.")]);
134 Cache::set("remote_search:".$remote, json_encode(["time" => time(), "accesses" => $resultdata->accesses + 1]), CACHE_HOUR);
136 Cache::set("remote_search:".$remote, json_encode(["time" => time(), "accesses" => 1]), CACHE_HOUR);
139 Nav::setSelected('search');
142 if (x($a->data,'search'))
143 $search = notags(trim($a->data['search']));
145 $search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : '');
148 if (x($_GET,'tag')) {
150 $search = ((x($_GET,'tag')) ? notags(trim(rawurldecode($_GET['tag']))) : '');
153 // contruct a wrapper for the search header
154 $o = replace_macros(get_markup_template("content_wrapper.tpl"),[
155 'name' => "search-header",
156 '$title' => L10n::t("Search"),
158 '$content' => search($search,'search-box','search',((local_user()) ? true : false), false)
161 if (strpos($search,'#') === 0) {
163 $search = substr($search,1);
165 if (strpos($search,'@') === 0) {
166 return dirfind_content($a);
168 if (strpos($search,'!') === 0) {
169 return dirfind_content($a);
172 if (x($_GET,'search-option'))
173 switch($_GET['search-option']) {
180 return dirfind_content($a, "@");
183 return dirfind_content($a, "!");
190 if (Config::get('system','only_tag_search'))
193 // Here is the way permissions work in the search module...
194 // Only public posts can be shown
195 // OR your own posts if you are a logged in member
196 // No items will be shown if the member has a blocked profile wall.
199 logger("Start tag search for '".$search."'", LOGGER_DEBUG);
203 STRAIGHT_JOIN `item` ON `item`.`id`=`term`.`oid` %s
204 WHERE %s AND (`term`.`uid` = 0 OR (`term`.`uid` = %d AND NOT `term`.`global`))
205 AND `term`.`otype` = %d AND `term`.`type` = %d AND `term`.`term` = '%s' AND `item`.`verb` = '%s'
206 AND NOT `author`.`blocked` AND NOT `author`.`hidden`
207 ORDER BY term.created DESC LIMIT %d , %d ",
208 item_fieldlists(), item_joins(), item_condition(),
209 intval(local_user()),
210 intval(TERM_OBJ_POST), intval(TERM_HASHTAG), dbesc(protect_sprintf($search)), dbesc(ACTIVITY_POST),
211 intval($a->pager['start']), intval($a->pager['itemspage']));
213 logger("Start fulltext search for '".$search."'", LOGGER_DEBUG);
215 $sql_extra = sprintf(" AND `item`.`body` REGEXP '%s' ", dbesc(protect_sprintf(preg_quote($search))));
219 WHERE %s AND (`item`.`uid` = 0 OR (`item`.`uid` = %s AND NOT `item`.`global`))
220 AND NOT `author`.`blocked` AND NOT `author`.`hidden`
222 GROUP BY `item`.`uri`, `item`.`id` ORDER BY `item`.`id` DESC LIMIT %d , %d",
223 item_fieldlists(), item_joins(), item_condition(),
224 intval(local_user()),
225 intval($a->pager['start']), intval($a->pager['itemspage']));
228 if (! DBM::is_result($r)) {
229 info(L10n::t('No results.') . EOL);
235 $title = L10n::t('Items tagged with: %s', $search);
237 $title = L10n::t('Results for: %s', $search);
240 $o .= replace_macros(get_markup_template("section_title.tpl"),[
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);