7 use Friendica\Content\Feature;
8 use Friendica\Content\Nav;
9 use Friendica\Core\Cache;
10 use Friendica\Core\Config;
11 use Friendica\Core\L10n;
12 use Friendica\Core\System;
13 use Friendica\Database\DBA;
14 use Friendica\Model\Item;
16 require_once 'include/security.php';
17 require_once 'include/conversation.php';
18 require_once 'mod/dirfind.php';
20 function search_saved_searches() {
23 $search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : '');
25 if (!Feature::isEnabled(local_user(),'savedsearch'))
28 $r = q("SELECT `id`,`term` FROM `search` WHERE `uid` = %d",
32 if (DBA::isResult($r)) {
37 'term' => $rr['term'],
38 'encodedterm' => urlencode($rr['term']),
39 'delete' => L10n::t('Remove term'),
40 'selected' => ($search==$rr['term']),
45 $tpl = get_markup_template("saved_searches_aside.tpl");
47 $o .= replace_macros($tpl, [
48 '$title' => L10n::t('Saved Searches'),
60 function search_init(App $a) {
62 $search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : '');
65 if (x($_GET,'save') && $search) {
66 $r = q("SELECT * FROM `search` WHERE `uid` = %d AND `term` = '%s' LIMIT 1",
70 if (!DBA::isResult($r)) {
71 DBA::insert('search', ['uid' => local_user(), 'term' => $search]);
74 if (x($_GET,'remove') && $search) {
75 DBA::delete('search', ['uid' => local_user(), 'term' => $search]);
78 /// @todo Check if there is a case at all that "aside" is prefilled here
79 if (!isset($a->page['aside'])) {
80 $a->page['aside'] = '';
83 $a->page['aside'] .= search_saved_searches();
86 unset($_SESSION['theme']);
87 unset($_SESSION['mobile-theme']);
96 function search_post(App $a) {
97 if (x($_POST,'search'))
98 $a->data['search'] = $_POST['search'];
102 function search_content(App $a) {
104 if (Config::get('system','block_public') && !local_user() && !remote_user()) {
105 notice(L10n::t('Public access denied.') . EOL);
109 if (Config::get('system','local_search') && !local_user() && !remote_user()) {
110 System::httpExit(403,
111 ["title" => L10n::t("Public access denied."),
112 "description" => L10n::t("Only logged in users are permitted to perform a search.")]);
114 //notice(L10n::t('Public access denied.').EOL);
118 if (Config::get('system','permit_crawling') && !local_user() && !remote_user()) {
120 // 10 requests are "free", after the 11th only a call per minute is allowed
122 $free_crawls = intval(Config::get('system','free_crawls'));
123 if ($free_crawls == 0)
126 $crawl_permit_period = intval(Config::get('system','crawl_permit_period'));
127 if ($crawl_permit_period == 0)
128 $crawl_permit_period = 10;
130 $remote = $_SERVER["REMOTE_ADDR"];
131 $result = Cache::get("remote_search:".$remote);
132 if (!is_null($result)) {
133 $resultdata = json_decode($result);
134 if (($resultdata->time > (time() - $crawl_permit_period)) && ($resultdata->accesses > $free_crawls)) {
135 System::httpExit(429,
136 ["title" => L10n::t("Too Many Requests"),
137 "description" => L10n::t("Only one search per minute is permitted for not logged in users.")]);
140 Cache::set("remote_search:".$remote, json_encode(["time" => time(), "accesses" => $resultdata->accesses + 1]), CACHE_HOUR);
142 Cache::set("remote_search:".$remote, json_encode(["time" => time(), "accesses" => 1]), CACHE_HOUR);
145 Nav::setSelected('search');
148 if (x($a->data,'search'))
149 $search = notags(trim($a->data['search']));
151 $search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : '');
154 if (x($_GET,'tag')) {
156 $search = (x($_GET,'tag') ? '#' . notags(trim(rawurldecode($_GET['tag']))) : '');
159 // contruct a wrapper for the search header
160 $o = replace_macros(get_markup_template("content_wrapper.tpl"),[
161 'name' => "search-header",
162 '$title' => L10n::t("Search"),
164 '$content' => search($search,'search-box','search',((local_user()) ? true : false), false)
167 if (strpos($search,'#') === 0) {
169 $search = substr($search,1);
171 if (strpos($search,'@') === 0) {
172 return dirfind_content($a);
174 if (strpos($search,'!') === 0) {
175 return dirfind_content($a);
178 if (x($_GET,'search-option'))
179 switch($_GET['search-option']) {
186 return dirfind_content($a, "@");
189 return dirfind_content($a, "!");
196 if (Config::get('system','only_tag_search'))
199 // Here is the way permissions work in the search module...
200 // Only public posts can be shown
201 // OR your own posts if you are a logged in member
202 // No items will be shown if the member has a blocked profile wall.
205 logger("Start tag search for '".$search."'", LOGGER_DEBUG);
207 $condition = ["(`uid` = 0 OR (`uid` = ? AND NOT `global`))
208 AND `otype` = ? AND `type` = ? AND `term` = ?",
209 local_user(), TERM_OBJ_POST, TERM_HASHTAG, $search];
210 $params = ['order' => ['created' => true],
211 'limit' => [$a->pager['start'], $a->pager['itemspage']]];
212 $terms = DBA::select('term', ['oid'], $condition, $params);
215 while ($term = DBA::fetch($terms)) {
216 $itemids[] = $term['oid'];
220 if (!empty($itemids)) {
221 $params = ['order' => ['id' => true]];
222 $items = Item::selectForUser(local_user(), [], ['id' => $itemids], $params);
223 $r = Item::inArray($items);
228 logger("Start fulltext search for '".$search."'", LOGGER_DEBUG);
230 $condition = ["(`uid` = 0 OR (`uid` = ? AND NOT `global`))
231 AND `body` LIKE CONCAT('%',?,'%')",
232 local_user(), $search];
233 $params = ['order' => ['id' => true],
234 'limit' => [$a->pager['start'], $a->pager['itemspage']]];
235 $items = Item::selectForUser(local_user(), [], $condition, $params);
236 $r = Item::inArray($items);
239 if (!DBA::isResult($r)) {
240 info(L10n::t('No results.') . EOL);
246 $title = L10n::t('Items tagged with: %s', $search);
248 $title = L10n::t('Results for: %s', $search);
251 $o .= replace_macros(get_markup_template("section_title.tpl"),[
255 logger("Start Conversation for '".$search."'", LOGGER_DEBUG);
256 $o .= conversation($a, $r, 'search', false, false, 'commented', local_user());
258 $o .= alt_pager($a,count($r));
260 logger("Done '".$search."'", LOGGER_DEBUG);