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 $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);
201 $condition = ["(`uid` = 0 OR (`uid` = ? AND NOT `global`))
202 AND `otype` = ? AND `type` = ? AND `term` = ?",
203 local_user(), TERM_OBJ_POST, TERM_HASHTAG, $search];
204 $params = ['order' => ['created' => true],
205 'limit' => [$a->pager['start'], $a->pager['itemspage']]];
206 $terms = dba::select('term', ['oid'], $condition, $params);
209 while ($term = dba::fetch($terms)) {
210 $itemids[] = $term['oid'];
214 if (!empty($itemids)) {
215 $params = ['order' => ['id' => true]];
216 $items = Item::selectForUser(local_user(), [], ['id' => $itemids], $params);
217 $r = dba::inArray($items);
222 logger("Start fulltext search for '".$search."'", LOGGER_DEBUG);
224 $condition = ["(`uid` = 0 OR (`uid` = ? AND NOT `global`))
225 AND `body` LIKE CONCAT('%',?,'%')",
226 local_user(), $search];
227 $params = ['order' => ['id' => true],
228 'limit' => [$a->pager['start'], $a->pager['itemspage']]];
229 $items = Item::selectForUser(local_user(), [], $condition, $params);
230 $r = dba::inArray($items);
233 if (!DBM::is_result($r)) {
234 info(L10n::t('No results.') . EOL);
240 $title = L10n::t('Items tagged with: %s', $search);
242 $title = L10n::t('Results for: %s', $search);
245 $o .= replace_macros(get_markup_template("section_title.tpl"),[
249 logger("Start Conversation for '".$search."'", LOGGER_DEBUG);
250 $o .= conversation($a, $r, 'search', false, false, 'commented', local_user());
252 $o .= alt_pager($a,count($r));
254 logger("Done '".$search."'", LOGGER_DEBUG);