7 use Friendica\Content\Nav;
8 use Friendica\Content\Pager;
9 use Friendica\Content\Text\HTML;
10 use Friendica\Core\Cache;
11 use Friendica\Core\Config;
12 use Friendica\Core\L10n;
13 use Friendica\Core\Logger;
14 use Friendica\Core\Renderer;
15 use Friendica\Database\DBA;
16 use Friendica\Model\Item;
17 use Friendica\Module\BaseSearchModule;
18 use Friendica\Util\Strings;
20 function search_saved_searches() {
23 $search = (!empty($_GET['search']) ? Strings::escapeTags(trim(rawurldecode($_GET['search']))) : '');
25 $r = q("SELECT `id`,`term` FROM `search` WHERE `uid` = %d",
29 if (DBA::isResult($r)) {
34 'term' => $rr['term'],
35 'encodedterm' => urlencode($rr['term']),
36 'delete' => L10n::t('Remove term'),
37 'selected' => ($search==$rr['term']),
42 $tpl = Renderer::getMarkupTemplate("saved_searches_aside.tpl");
44 $o .= Renderer::replaceMacros($tpl, [
45 '$title' => L10n::t('Saved Searches'),
57 function search_init(App $a) {
59 $search = (!empty($_GET['search']) ? Strings::escapeTags(trim(rawurldecode($_GET['search']))) : '');
62 if (!empty($_GET['save']) && $search) {
63 $r = q("SELECT * FROM `search` WHERE `uid` = %d AND `term` = '%s' LIMIT 1",
67 if (!DBA::isResult($r)) {
68 DBA::insert('search', ['uid' => local_user(), 'term' => $search]);
71 if (!empty($_GET['remove']) && $search) {
72 DBA::delete('search', ['uid' => local_user(), 'term' => $search]);
75 /// @todo Check if there is a case at all that "aside" is prefilled here
76 if (!isset($a->page['aside'])) {
77 $a->page['aside'] = '';
80 $a->page['aside'] .= search_saved_searches();
83 unset($_SESSION['theme']);
84 unset($_SESSION['mobile-theme']);
91 function search_content(App $a) {
93 if (Config::get('system','block_public') && !local_user() && !remote_user()) {
94 notice(L10n::t('Public access denied.') . EOL);
98 if (Config::get('system','local_search') && !local_user() && !remote_user()) {
99 $e = new \Friendica\Network\HTTPException\ForbiddenException(L10n::t("Only logged in users are permitted to perform a search."));
100 $e->httpdesc = L10n::t("Public access denied.");
104 if (Config::get('system','permit_crawling') && !local_user() && !remote_user()) {
106 // 10 requests are "free", after the 11th only a call per minute is allowed
108 $free_crawls = intval(Config::get('system','free_crawls'));
109 if ($free_crawls == 0)
112 $crawl_permit_period = intval(Config::get('system','crawl_permit_period'));
113 if ($crawl_permit_period == 0)
114 $crawl_permit_period = 10;
116 $remote = $_SERVER["REMOTE_ADDR"];
117 $result = Cache::get("remote_search:".$remote);
118 if (!is_null($result)) {
119 $resultdata = json_decode($result);
120 if (($resultdata->time > (time() - $crawl_permit_period)) && ($resultdata->accesses > $free_crawls)) {
121 throw new \Friendica\Network\HTTPException\TooManyRequestsException(L10n::t("Only one search per minute is permitted for not logged in users."));
123 Cache::set("remote_search:".$remote, json_encode(["time" => time(), "accesses" => $resultdata->accesses + 1]), Cache::HOUR);
125 Cache::set("remote_search:".$remote, json_encode(["time" => time(), "accesses" => 1]), Cache::HOUR);
128 Nav::setSelected('search');
130 $search = (!empty($_REQUEST['search']) ? Strings::escapeTags(trim(rawurldecode($_REQUEST['search']))) : '');
133 if (!empty($_GET['tag'])) {
135 $search = (!empty($_GET['tag']) ? '#' . Strings::escapeTags(trim(rawurldecode($_GET['tag']))) : '');
138 // contruct a wrapper for the search header
139 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate("content_wrapper.tpl"),[
140 'name' => "search-header",
141 '$title' => L10n::t("Search"),
143 '$content' => HTML::search($search,'search-box','search', false)
146 if (strpos($search,'#') === 0) {
148 $search = substr($search,1);
150 if (strpos($search,'@') === 0) {
151 return BaseSearchModule::performSearch();
153 if (strpos($search,'!') === 0) {
154 return BaseSearchModule::performSearch();
157 if (!empty($_GET['search-option']))
158 switch($_GET['search-option']) {
165 return BaseSearchModule::performSearch('@');
167 return BaseSearchModule::performSearch('!');
173 if (Config::get('system','only_tag_search'))
176 // Here is the way permissions work in the search module...
177 // Only public posts can be shown
178 // OR your own posts if you are a logged in member
179 // No items will be shown if the member has a blocked profile wall.
181 $pager = new Pager($a->query_string);
184 Logger::log("Start tag search for '".$search."'", Logger::DEBUG);
186 $condition = ["(`uid` = 0 OR (`uid` = ? AND NOT `global`))
187 AND `otype` = ? AND `type` = ? AND `term` = ?",
188 local_user(), TERM_OBJ_POST, TERM_HASHTAG, $search];
189 $params = ['order' => ['received' => true],
190 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
191 $terms = DBA::select('term', ['oid'], $condition, $params);
194 while ($term = DBA::fetch($terms)) {
195 $itemids[] = $term['oid'];
199 if (!empty($itemids)) {
200 $params = ['order' => ['id' => true]];
201 $items = Item::selectForUser(local_user(), [], ['id' => $itemids], $params);
202 $r = Item::inArray($items);
207 Logger::log("Start fulltext search for '".$search."'", Logger::DEBUG);
209 $condition = ["(`uid` = 0 OR (`uid` = ? AND NOT `global`))
210 AND `body` LIKE CONCAT('%',?,'%')",
211 local_user(), $search];
212 $params = ['order' => ['id' => true],
213 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
214 $items = Item::selectForUser(local_user(), [], $condition, $params);
215 $r = Item::inArray($items);
218 if (!DBA::isResult($r)) {
219 info(L10n::t('No results.') . EOL);
225 $title = L10n::t('Items tagged with: %s', $search);
227 $title = L10n::t('Results for: %s', $search);
230 $o .= Renderer::replaceMacros(Renderer::getMarkupTemplate("section_title.tpl"),[
234 Logger::log("Start Conversation for '".$search."'", Logger::DEBUG);
235 $o .= conversation($a, $r, $pager, 'search', false, false, 'commented', local_user());
237 $o .= $pager->renderMinimal(count($r));
239 Logger::log("Done '".$search."'", Logger::DEBUG);