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\Session;
15 use Friendica\Core\Renderer;
16 use Friendica\Database\DBA;
17 use Friendica\Model\Item;
18 use Friendica\Module\BaseSearchModule;
19 use Friendica\Util\Strings;
21 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'),
56 function search_init(App $a) {
57 $search = (!empty($_GET['search']) ? Strings::escapeTags(trim(rawurldecode($_GET['search']))) : '');
60 if (!empty($_GET['save']) && $search) {
61 $r = q("SELECT * FROM `search` WHERE `uid` = %d AND `term` = '%s' LIMIT 1",
65 if (!DBA::isResult($r)) {
66 DBA::insert('search', ['uid' => local_user(), 'term' => $search]);
69 if (!empty($_GET['remove']) && $search) {
70 DBA::delete('search', ['uid' => local_user(), 'term' => $search]);
73 /// @todo Check if there is a case at all that "aside" is prefilled here
74 if (!isset($a->page['aside'])) {
75 $a->page['aside'] = '';
78 $a->page['aside'] .= search_saved_searches();
82 function search_content(App $a) {
83 if (Config::get('system','block_public') && !Session::isAuthenticated()) {
84 notice(L10n::t('Public access denied.') . EOL);
88 if (Config::get('system','local_search') && !Session::isAuthenticated()) {
89 $e = new \Friendica\Network\HTTPException\ForbiddenException(L10n::t("Only logged in users are permitted to perform a search."));
90 $e->httpdesc = L10n::t("Public access denied.");
94 if (Config::get('system','permit_crawling') && !Session::isAuthenticated()) {
96 // 10 requests are "free", after the 11th only a call per minute is allowed
98 $free_crawls = intval(Config::get('system','free_crawls'));
99 if ($free_crawls == 0)
102 $crawl_permit_period = intval(Config::get('system','crawl_permit_period'));
103 if ($crawl_permit_period == 0)
104 $crawl_permit_period = 10;
106 $remote = $_SERVER["REMOTE_ADDR"];
107 $result = Cache::get("remote_search:".$remote);
108 if (!is_null($result)) {
109 $resultdata = json_decode($result);
110 if (($resultdata->time > (time() - $crawl_permit_period)) && ($resultdata->accesses > $free_crawls)) {
111 throw new \Friendica\Network\HTTPException\TooManyRequestsException(L10n::t("Only one search per minute is permitted for not logged in users."));
113 Cache::set("remote_search:".$remote, json_encode(["time" => time(), "accesses" => $resultdata->accesses + 1]), Cache::HOUR);
115 Cache::set("remote_search:".$remote, json_encode(["time" => time(), "accesses" => 1]), Cache::HOUR);
118 Nav::setSelected('search');
120 $search = (!empty($_REQUEST['search']) ? Strings::escapeTags(trim(rawurldecode($_REQUEST['search']))) : '');
123 if (!empty($_GET['tag'])) {
125 $search = (!empty($_GET['tag']) ? '#' . Strings::escapeTags(trim(rawurldecode($_GET['tag']))) : '');
128 // contruct a wrapper for the search header
129 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate("content_wrapper.tpl"),[
130 'name' => "search-header",
131 '$title' => L10n::t("Search"),
133 '$content' => HTML::search($search,'search-box','search', false)
136 if (strpos($search,'#') === 0) {
138 $search = substr($search,1);
140 if (strpos($search,'@') === 0) {
141 return BaseSearchModule::performSearch();
143 if (strpos($search,'!') === 0) {
144 return BaseSearchModule::performSearch();
147 if (parse_url($search, PHP_URL_SCHEME) != '') {
148 $id = Item::fetchByLink($search);
150 $item = Item::selectFirst(['guid'], ['id' => $id]);
151 if (DBA::isResult($item)) {
152 $a->internalRedirect('display/' . $item['guid']);
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);