7 use Friendica\Content\Feature;
8 use Friendica\Content\Nav;
9 use Friendica\Content\Pager;
10 use Friendica\Content\Text\HTML;
11 use Friendica\Core\Cache;
12 use Friendica\Core\Config;
13 use Friendica\Core\L10n;
14 use Friendica\Core\Logger;
15 use Friendica\Core\Renderer;
16 use Friendica\Core\System;
17 use Friendica\Database\DBA;
18 use Friendica\Model\Item;
19 use Friendica\Util\Strings;
21 require_once 'mod/dirfind.php';
23 function search_saved_searches() {
26 $search = (!empty($_GET['search']) ? Strings::escapeTags(trim(rawurldecode($_GET['search']))) : '');
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 = Renderer::getMarkupTemplate("saved_searches_aside.tpl");
47 $o .= Renderer::replaceMacros($tpl, [
48 '$title' => L10n::t('Saved Searches'),
60 function search_init(App $a) {
62 $search = (!empty($_GET['search']) ? Strings::escapeTags(trim(rawurldecode($_GET['search']))) : '');
65 if (!empty($_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 (!empty($_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']);
94 function search_content(App $a) {
96 if (Config::get('system','block_public') && !local_user() && !remote_user()) {
97 notice(L10n::t('Public access denied.') . EOL);
101 if (Config::get('system','local_search') && !local_user() && !remote_user()) {
102 System::httpExit(403,
103 ["title" => L10n::t("Public access denied."),
104 "description" => L10n::t("Only logged in users are permitted to perform a search.")]);
106 //notice(L10n::t('Public access denied.').EOL);
110 if (Config::get('system','permit_crawling') && !local_user() && !remote_user()) {
112 // 10 requests are "free", after the 11th only a call per minute is allowed
114 $free_crawls = intval(Config::get('system','free_crawls'));
115 if ($free_crawls == 0)
118 $crawl_permit_period = intval(Config::get('system','crawl_permit_period'));
119 if ($crawl_permit_period == 0)
120 $crawl_permit_period = 10;
122 $remote = $_SERVER["REMOTE_ADDR"];
123 $result = Cache::get("remote_search:".$remote);
124 if (!is_null($result)) {
125 $resultdata = json_decode($result);
126 if (($resultdata->time > (time() - $crawl_permit_period)) && ($resultdata->accesses > $free_crawls)) {
127 System::httpExit(429,
128 ["title" => L10n::t("Too Many Requests"),
129 "description" => L10n::t("Only one search per minute is permitted for not logged in users.")]);
132 Cache::set("remote_search:".$remote, json_encode(["time" => time(), "accesses" => $resultdata->accesses + 1]), Cache::HOUR);
134 Cache::set("remote_search:".$remote, json_encode(["time" => time(), "accesses" => 1]), Cache::HOUR);
137 Nav::setSelected('search');
139 $search = (!empty($_REQUEST['search']) ? Strings::escapeTags(trim(rawurldecode($_REQUEST['search']))) : '');
142 if (!empty($_GET['tag'])) {
144 $search = (!empty($_GET['tag']) ? '#' . Strings::escapeTags(trim(rawurldecode($_GET['tag']))) : '');
147 // contruct a wrapper for the search header
148 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate("content_wrapper.tpl"),[
149 'name' => "search-header",
150 '$title' => L10n::t("Search"),
152 '$content' => HTML::search($search,'search-box','search', false)
155 if (strpos($search,'#') === 0) {
157 $search = substr($search,1);
159 if (strpos($search,'@') === 0) {
160 return dirfind_content($a);
162 if (strpos($search,'!') === 0) {
163 return dirfind_content($a);
166 if (!empty($_GET['search-option']))
167 switch($_GET['search-option']) {
174 return dirfind_content($a, "@");
177 return dirfind_content($a, "!");
184 if (Config::get('system','only_tag_search'))
187 // Here is the way permissions work in the search module...
188 // Only public posts can be shown
189 // OR your own posts if you are a logged in member
190 // No items will be shown if the member has a blocked profile wall.
192 $pager = new Pager($a->query_string);
195 Logger::log("Start tag search for '".$search."'", Logger::DEBUG);
197 $condition = ["(`uid` = 0 OR (`uid` = ? AND NOT `global`))
198 AND `otype` = ? AND `type` = ? AND `term` = ?",
199 local_user(), TERM_OBJ_POST, TERM_HASHTAG, $search];
200 $params = ['order' => ['created' => true],
201 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
202 $terms = DBA::select('term', ['oid'], $condition, $params);
205 while ($term = DBA::fetch($terms)) {
206 $itemids[] = $term['oid'];
210 if (!empty($itemids)) {
211 $params = ['order' => ['id' => true]];
212 $items = Item::selectForUser(local_user(), [], ['id' => $itemids], $params);
213 $r = Item::inArray($items);
218 Logger::log("Start fulltext search for '".$search."'", Logger::DEBUG);
220 $condition = ["(`uid` = 0 OR (`uid` = ? AND NOT `global`))
221 AND `body` LIKE CONCAT('%',?,'%')",
222 local_user(), $search];
223 $params = ['order' => ['id' => true],
224 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
225 $items = Item::selectForUser(local_user(), [], $condition, $params);
226 $r = Item::inArray($items);
229 if (!DBA::isResult($r)) {
230 info(L10n::t('No results.') . EOL);
236 $title = L10n::t('Items tagged with: %s', $search);
238 $title = L10n::t('Results for: %s', $search);
241 $o .= Renderer::replaceMacros(Renderer::getMarkupTemplate("section_title.tpl"),[
245 Logger::log("Start Conversation for '".$search."'", Logger::DEBUG);
246 $o .= conversation($a, $r, $pager, 'search', false, false, 'commented', local_user());
248 $o .= $pager->renderMinimal(count($r));
250 Logger::log("Done '".$search."'", Logger::DEBUG);