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/security.php';
15 require_once 'include/conversation.php';
16 require_once 'mod/dirfind.php';
18 function search_saved_searches() {
21 $search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : '');
23 if (! Feature::isEnabled(local_user(),'savedsearch'))
26 $r = q("SELECT `id`,`term` FROM `search` WHERE `uid` = %d",
30 if (DBM::is_result($r)) {
35 'term' => $rr['term'],
36 'encodedterm' => urlencode($rr['term']),
37 'delete' => L10n::t('Remove term'),
38 'selected' => ($search==$rr['term']),
43 $tpl = get_markup_template("saved_searches_aside.tpl");
45 $o .= replace_macros($tpl, [
46 '$title' => L10n::t('Saved Searches'),
58 function search_init(App $a) {
60 $search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : '');
63 if (x($_GET,'save') && $search) {
64 $r = q("SELECT * FROM `search` WHERE `uid` = %d AND `term` = '%s' LIMIT 1",
68 if (!DBM::is_result($r)) {
69 dba::insert('search', ['uid' => local_user(), 'term' => $search]);
72 if (x($_GET,'remove') && $search) {
73 dba::delete('search', ['uid' => local_user(), 'term' => $search]);
76 $a->page['aside'] .= search_saved_searches();
79 unset($_SESSION['theme']);
80 unset($_SESSION['mobile-theme']);
89 function search_post(App $a) {
90 if (x($_POST,'search'))
91 $a->data['search'] = $_POST['search'];
95 function search_content(App $a) {
97 if (Config::get('system','block_public') && !local_user() && !remote_user()) {
98 notice(L10n::t('Public access denied.') . EOL);
102 if (Config::get('system','local_search') && !local_user() && !remote_user()) {
103 System::httpExit(403,
104 ["title" => L10n::t("Public access denied."),
105 "description" => L10n::t("Only logged in users are permitted to perform a search.")]);
107 //notice(L10n::t('Public access denied.').EOL);
111 if (Config::get('system','permit_crawling') && !local_user() && !remote_user()) {
113 // 10 requests are "free", after the 11th only a call per minute is allowed
115 $free_crawls = intval(Config::get('system','free_crawls'));
116 if ($free_crawls == 0)
119 $crawl_permit_period = intval(Config::get('system','crawl_permit_period'));
120 if ($crawl_permit_period == 0)
121 $crawl_permit_period = 10;
123 $remote = $_SERVER["REMOTE_ADDR"];
124 $result = Cache::get("remote_search:".$remote);
125 if (!is_null($result)) {
126 $resultdata = json_decode($result);
127 if (($resultdata->time > (time() - $crawl_permit_period)) && ($resultdata->accesses > $free_crawls)) {
128 System::httpExit(429,
129 ["title" => L10n::t("Too Many Requests"),
130 "description" => L10n::t("Only one search per minute is permitted for not logged in users.")]);
133 Cache::set("remote_search:".$remote, json_encode(["time" => time(), "accesses" => $resultdata->accesses + 1]), CACHE_HOUR);
135 Cache::set("remote_search:".$remote, json_encode(["time" => time(), "accesses" => 1]), CACHE_HOUR);
138 Nav::setSelected('search');
141 if (x($a->data,'search'))
142 $search = notags(trim($a->data['search']));
144 $search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : '');
147 if (x($_GET,'tag')) {
149 $search = (x($_GET,'tag') ? '#' . notags(trim(rawurldecode($_GET['tag']))) : '');
152 // contruct a wrapper for the search header
153 $o = replace_macros(get_markup_template("content_wrapper.tpl"),[
154 'name' => "search-header",
155 '$title' => L10n::t("Search"),
157 '$content' => search($search,'search-box','search',((local_user()) ? true : false), false)
160 if (strpos($search,'#') === 0) {
162 $search = substr($search,1);
164 if (strpos($search,'@') === 0) {
165 return dirfind_content($a);
167 if (strpos($search,'!') === 0) {
168 return dirfind_content($a);
171 if (x($_GET,'search-option'))
172 switch($_GET['search-option']) {
179 return dirfind_content($a, "@");
182 return dirfind_content($a, "!");
189 if (Config::get('system','only_tag_search'))
192 // Here is the way permissions work in the search module...
193 // Only public posts can be shown
194 // OR your own posts if you are a logged in member
195 // No items will be shown if the member has a blocked profile wall.
198 logger("Start tag search for '".$search."'", LOGGER_DEBUG);
202 STRAIGHT_JOIN `item` ON `item`.`id`=`term`.`oid` %s
203 WHERE %s AND (`term`.`uid` = 0 OR (`term`.`uid` = %d AND NOT `term`.`global`))
204 AND `term`.`otype` = %d AND `term`.`type` = %d AND `term`.`term` = '%s' AND `item`.`verb` = '%s'
205 AND NOT `author`.`blocked` AND NOT `author`.`hidden`
206 ORDER BY term.created DESC LIMIT %d , %d ",
207 item_fieldlists(), item_joins(), item_condition(),
208 intval(local_user()),
209 intval(TERM_OBJ_POST), intval(TERM_HASHTAG), dbesc(protect_sprintf($search)), dbesc(ACTIVITY_POST),
210 intval($a->pager['start']), intval($a->pager['itemspage']));
212 logger("Start fulltext search for '".$search."'", LOGGER_DEBUG);
214 $sql_extra = sprintf(" AND `item`.`body` REGEXP '%s' ", dbesc(protect_sprintf(preg_quote($search))));
218 WHERE %s AND (`item`.`uid` = 0 OR (`item`.`uid` = %s AND NOT `item`.`global`))
219 AND NOT `author`.`blocked` AND NOT `author`.`hidden`
221 GROUP BY `item`.`uri`, `item`.`id` ORDER BY `item`.`id` DESC LIMIT %d , %d",
222 item_fieldlists(), item_joins(), item_condition(),
223 intval(local_user()),
224 intval($a->pager['start']), intval($a->pager['itemspage']));
227 if (! DBM::is_result($r)) {
228 info(L10n::t('No results.') . EOL);
234 $title = L10n::t('Items tagged with: %s', $search);
236 $title = L10n::t('Results for: %s', $search);
239 $o .= replace_macros(get_markup_template("section_title.tpl"),[
243 logger("Start Conversation for '".$search."'", LOGGER_DEBUG);
244 $o .= conversation($a,$r,'search',false);
246 $o .= alt_pager($a,count($r));
248 logger("Done '".$search."'", LOGGER_DEBUG);