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;
13 use Friendica\Model\Item;
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 /// @todo Check if there is a case at all that "aside" is prefilled here
78 if (!isset($a->page['aside'])) {
79 $a->page['aside'] = '';
82 $a->page['aside'] .= search_saved_searches();
85 unset($_SESSION['theme']);
86 unset($_SESSION['mobile-theme']);
95 function search_post(App $a) {
96 if (x($_POST,'search'))
97 $a->data['search'] = $_POST['search'];
101 function search_content(App $a) {
103 if (Config::get('system','block_public') && !local_user() && !remote_user()) {
104 notice(L10n::t('Public access denied.') . EOL);
108 if (Config::get('system','local_search') && !local_user() && !remote_user()) {
109 System::httpExit(403,
110 ["title" => L10n::t("Public access denied."),
111 "description" => L10n::t("Only logged in users are permitted to perform a search.")]);
113 //notice(L10n::t('Public access denied.').EOL);
117 if (Config::get('system','permit_crawling') && !local_user() && !remote_user()) {
119 // 10 requests are "free", after the 11th only a call per minute is allowed
121 $free_crawls = intval(Config::get('system','free_crawls'));
122 if ($free_crawls == 0)
125 $crawl_permit_period = intval(Config::get('system','crawl_permit_period'));
126 if ($crawl_permit_period == 0)
127 $crawl_permit_period = 10;
129 $remote = $_SERVER["REMOTE_ADDR"];
130 $result = Cache::get("remote_search:".$remote);
131 if (!is_null($result)) {
132 $resultdata = json_decode($result);
133 if (($resultdata->time > (time() - $crawl_permit_period)) && ($resultdata->accesses > $free_crawls)) {
134 System::httpExit(429,
135 ["title" => L10n::t("Too Many Requests"),
136 "description" => L10n::t("Only one search per minute is permitted for not logged in users.")]);
139 Cache::set("remote_search:".$remote, json_encode(["time" => time(), "accesses" => $resultdata->accesses + 1]), CACHE_HOUR);
141 Cache::set("remote_search:".$remote, json_encode(["time" => time(), "accesses" => 1]), CACHE_HOUR);
144 Nav::setSelected('search');
147 if (x($a->data,'search'))
148 $search = notags(trim($a->data['search']));
150 $search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : '');
153 if (x($_GET,'tag')) {
155 $search = (x($_GET,'tag') ? '#' . notags(trim(rawurldecode($_GET['tag']))) : '');
158 // contruct a wrapper for the search header
159 $o = replace_macros(get_markup_template("content_wrapper.tpl"),[
160 'name' => "search-header",
161 '$title' => L10n::t("Search"),
163 '$content' => search($search,'search-box','search',((local_user()) ? true : false), false)
166 if (strpos($search,'#') === 0) {
168 $search = substr($search,1);
170 if (strpos($search,'@') === 0) {
171 return dirfind_content($a);
173 if (strpos($search,'!') === 0) {
174 return dirfind_content($a);
177 if (x($_GET,'search-option'))
178 switch($_GET['search-option']) {
185 return dirfind_content($a, "@");
188 return dirfind_content($a, "!");
195 if (Config::get('system','only_tag_search'))
198 // Here is the way permissions work in the search module...
199 // Only public posts can be shown
200 // OR your own posts if you are a logged in member
201 // No items will be shown if the member has a blocked profile wall.
204 logger("Start tag search for '".$search."'", LOGGER_DEBUG);
206 $condition = ["(`uid` = 0 OR (`uid` = ? AND NOT `global`))
207 AND `otype` = ? AND `type` = ? AND `term` = ?",
208 local_user(), TERM_OBJ_POST, TERM_HASHTAG, $search];
209 $params = ['order' => ['created' => true],
210 'limit' => [$a->pager['start'], $a->pager['itemspage']]];
211 $terms = dba::select('term', ['oid'], $condition, $params);
214 while ($term = dba::fetch($terms)) {
215 $itemids[] = $term['oid'];
219 if (!empty($itemids)) {
220 $params = ['order' => ['id' => true]];
221 $items = Item::selectForUser(local_user(), [], ['id' => $itemids], $params);
222 $r = Item::inArray($items);
227 logger("Start fulltext search for '".$search."'", LOGGER_DEBUG);
229 $condition = ["(`uid` = 0 OR (`uid` = ? AND NOT `global`))
230 AND `body` LIKE CONCAT('%',?,'%')",
231 local_user(), $search];
232 $params = ['order' => ['id' => true],
233 'limit' => [$a->pager['start'], $a->pager['itemspage']]];
234 $items = Item::selectForUser(local_user(), [], $condition, $params);
235 $r = Item::inArray($items);
238 if (!DBM::is_result($r)) {
239 info(L10n::t('No results.') . EOL);
245 $title = L10n::t('Items tagged with: %s', $search);
247 $title = L10n::t('Results for: %s', $search);
250 $o .= replace_macros(get_markup_template("section_title.tpl"),[
254 logger("Start Conversation for '".$search."'", LOGGER_DEBUG);
255 $o .= conversation($a, $r, 'search', false, false, 'commented', local_user());
257 $o .= alt_pager($a,count($r));
259 logger("Done '".$search."'", LOGGER_DEBUG);