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_init(App $a) {
22 $search = (!empty($_GET['q']) ? Strings::escapeTags(trim(rawurldecode($_GET['q']))) : '');
25 /// @todo Check if there is a case at all that "aside" is prefilled here
26 if (!isset($a->page['aside'])) {
27 $a->page['aside'] = '';
30 $a->page['aside'] .= \Friendica\Content\Widget\SavedSearches::getHTML('search?q=' . $search, $search);
34 function search_content(App $a) {
35 if (Config::get('system','block_public') && !Session::isAuthenticated()) {
36 notice(L10n::t('Public access denied.') . EOL);
40 if (Config::get('system','local_search') && !Session::isAuthenticated()) {
41 $e = new \Friendica\Network\HTTPException\ForbiddenException(L10n::t("Only logged in users are permitted to perform a search."));
42 $e->httpdesc = L10n::t("Public access denied.");
46 if (Config::get('system','permit_crawling') && !Session::isAuthenticated()) {
48 // 10 requests are "free", after the 11th only a call per minute is allowed
50 $free_crawls = intval(Config::get('system','free_crawls'));
51 if ($free_crawls == 0)
54 $crawl_permit_period = intval(Config::get('system','crawl_permit_period'));
55 if ($crawl_permit_period == 0)
56 $crawl_permit_period = 10;
58 $remote = $_SERVER["REMOTE_ADDR"];
59 $result = Cache::get("remote_search:".$remote);
60 if (!is_null($result)) {
61 $resultdata = json_decode($result);
62 if (($resultdata->time > (time() - $crawl_permit_period)) && ($resultdata->accesses > $free_crawls)) {
63 throw new \Friendica\Network\HTTPException\TooManyRequestsException(L10n::t("Only one search per minute is permitted for not logged in users."));
65 Cache::set("remote_search:".$remote, json_encode(["time" => time(), "accesses" => $resultdata->accesses + 1]), Cache::HOUR);
67 Cache::set("remote_search:".$remote, json_encode(["time" => time(), "accesses" => 1]), Cache::HOUR);
70 Nav::setSelected('search');
72 $search = (!empty($_REQUEST['q']) ? Strings::escapeTags(trim(rawurldecode($_REQUEST['q']))) : '');
75 if (!empty($_GET['tag'])) {
77 $search = (!empty($_GET['tag']) ? '#' . Strings::escapeTags(trim(rawurldecode($_GET['tag']))) : '');
80 // contruct a wrapper for the search header
81 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate("content_wrapper.tpl"),[
82 'name' => "search-header",
83 '$title' => L10n::t("Search"),
85 '$content' => HTML::search($search,'search-box',false)
88 if (strpos($search,'#') === 0) {
90 $search = substr($search,1);
92 if (strpos($search,'@') === 0) {
93 return BaseSearchModule::performSearch();
95 if (strpos($search,'!') === 0) {
96 return BaseSearchModule::performSearch();
99 if (parse_url($search, PHP_URL_SCHEME) != '') {
100 $id = Item::fetchByLink($search);
102 $item = Item::selectFirst(['guid'], ['id' => $id]);
103 if (DBA::isResult($item)) {
104 $a->internalRedirect('display/' . $item['guid']);
109 if (!empty($_GET['search-option']))
110 switch($_GET['search-option']) {
117 return BaseSearchModule::performSearch('@');
119 return BaseSearchModule::performSearch('!');
125 if (Config::get('system','only_tag_search'))
128 // Here is the way permissions work in the search module...
129 // Only public posts can be shown
130 // OR your own posts if you are a logged in member
131 // No items will be shown if the member has a blocked profile wall.
133 $pager = new Pager($a->query_string);
136 Logger::log("Start tag search for '".$search."'", Logger::DEBUG);
138 $condition = ["(`uid` = 0 OR (`uid` = ? AND NOT `global`))
139 AND `otype` = ? AND `type` = ? AND `term` = ?",
140 local_user(), TERM_OBJ_POST, TERM_HASHTAG, $search];
141 $params = ['order' => ['received' => true],
142 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
143 $terms = DBA::select('term', ['oid'], $condition, $params);
146 while ($term = DBA::fetch($terms)) {
147 $itemids[] = $term['oid'];
151 if (!empty($itemids)) {
152 $params = ['order' => ['id' => true]];
153 $items = Item::selectForUser(local_user(), [], ['id' => $itemids], $params);
154 $r = Item::inArray($items);
159 Logger::log("Start fulltext search for '".$search."'", Logger::DEBUG);
161 $condition = ["(`uid` = 0 OR (`uid` = ? AND NOT `global`))
162 AND `body` LIKE CONCAT('%',?,'%')",
163 local_user(), $search];
164 $params = ['order' => ['id' => true],
165 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
166 $items = Item::selectForUser(local_user(), [], $condition, $params);
167 $r = Item::inArray($items);
170 if (!DBA::isResult($r)) {
171 info(L10n::t('No results.') . EOL);
177 $title = L10n::t('Items tagged with: %s', $search);
179 $title = L10n::t('Results for: %s', $search);
182 $o .= Renderer::replaceMacros(Renderer::getMarkupTemplate("section_title.tpl"),[
186 Logger::log("Start Conversation for '".$search."'", Logger::DEBUG);
187 $o .= conversation($a, $r, $pager, 'search', false, false, 'commented', local_user());
189 $o .= $pager->renderMinimal(count($r));
191 Logger::log("Done '".$search."'", Logger::DEBUG);