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 'include/conversation.php';
22 require_once 'mod/dirfind.php';
24 function search_saved_searches() {
27 $search = ((x($_GET,'search')) ? Strings::escapeTags(trim(rawurldecode($_GET['search']))) : '');
29 $r = q("SELECT `id`,`term` FROM `search` WHERE `uid` = %d",
33 if (DBA::isResult($r)) {
38 'term' => $rr['term'],
39 'encodedterm' => urlencode($rr['term']),
40 'delete' => L10n::t('Remove term'),
41 'selected' => ($search==$rr['term']),
46 $tpl = Renderer::getMarkupTemplate("saved_searches_aside.tpl");
48 $o .= Renderer::replaceMacros($tpl, [
49 '$title' => L10n::t('Saved Searches'),
61 function search_init(App $a) {
63 $search = ((x($_GET,'search')) ? Strings::escapeTags(trim(rawurldecode($_GET['search']))) : '');
66 if (x($_GET,'save') && $search) {
67 $r = q("SELECT * FROM `search` WHERE `uid` = %d AND `term` = '%s' LIMIT 1",
71 if (!DBA::isResult($r)) {
72 DBA::insert('search', ['uid' => local_user(), 'term' => $search]);
75 if (x($_GET,'remove') && $search) {
76 DBA::delete('search', ['uid' => local_user(), 'term' => $search]);
79 /// @todo Check if there is a case at all that "aside" is prefilled here
80 if (!isset($a->page['aside'])) {
81 $a->page['aside'] = '';
84 $a->page['aside'] .= search_saved_searches();
87 unset($_SESSION['theme']);
88 unset($_SESSION['mobile-theme']);
97 function search_post(App $a) {
98 if (x($_POST,'search'))
99 $a->data['search'] = $_POST['search'];
103 function search_content(App $a) {
105 if (Config::get('system','block_public') && !local_user() && !remote_user()) {
106 notice(L10n::t('Public access denied.') . EOL);
110 if (Config::get('system','local_search') && !local_user() && !remote_user()) {
111 System::httpExit(403,
112 ["title" => L10n::t("Public access denied."),
113 "description" => L10n::t("Only logged in users are permitted to perform a search.")]);
115 //notice(L10n::t('Public access denied.').EOL);
119 if (Config::get('system','permit_crawling') && !local_user() && !remote_user()) {
121 // 10 requests are "free", after the 11th only a call per minute is allowed
123 $free_crawls = intval(Config::get('system','free_crawls'));
124 if ($free_crawls == 0)
127 $crawl_permit_period = intval(Config::get('system','crawl_permit_period'));
128 if ($crawl_permit_period == 0)
129 $crawl_permit_period = 10;
131 $remote = $_SERVER["REMOTE_ADDR"];
132 $result = Cache::get("remote_search:".$remote);
133 if (!is_null($result)) {
134 $resultdata = json_decode($result);
135 if (($resultdata->time > (time() - $crawl_permit_period)) && ($resultdata->accesses > $free_crawls)) {
136 System::httpExit(429,
137 ["title" => L10n::t("Too Many Requests"),
138 "description" => L10n::t("Only one search per minute is permitted for not logged in users.")]);
141 Cache::set("remote_search:".$remote, json_encode(["time" => time(), "accesses" => $resultdata->accesses + 1]), Cache::HOUR);
143 Cache::set("remote_search:".$remote, json_encode(["time" => time(), "accesses" => 1]), Cache::HOUR);
146 Nav::setSelected('search');
149 if (x($a->data,'search'))
150 $search = Strings::escapeTags(trim($a->data['search']));
152 $search = ((x($_GET,'search')) ? Strings::escapeTags(trim(rawurldecode($_GET['search']))) : '');
155 if (x($_GET,'tag')) {
157 $search = (x($_GET,'tag') ? '#' . Strings::escapeTags(trim(rawurldecode($_GET['tag']))) : '');
160 // contruct a wrapper for the search header
161 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate("content_wrapper.tpl"),[
162 'name' => "search-header",
163 '$title' => L10n::t("Search"),
165 '$content' => HTML::search($search,'search-box','search', false)
168 if (strpos($search,'#') === 0) {
170 $search = substr($search,1);
172 if (strpos($search,'@') === 0) {
173 return dirfind_content($a);
175 if (strpos($search,'!') === 0) {
176 return dirfind_content($a);
179 if (x($_GET,'search-option'))
180 switch($_GET['search-option']) {
187 return dirfind_content($a, "@");
190 return dirfind_content($a, "!");
197 if (Config::get('system','only_tag_search'))
200 // Here is the way permissions work in the search module...
201 // Only public posts can be shown
202 // OR your own posts if you are a logged in member
203 // No items will be shown if the member has a blocked profile wall.
205 $pager = new Pager($a->query_string);
208 Logger::log("Start tag search for '".$search."'", Logger::DEBUG);
210 $condition = ["(`uid` = 0 OR (`uid` = ? AND NOT `global`))
211 AND `otype` = ? AND `type` = ? AND `term` = ?",
212 local_user(), TERM_OBJ_POST, TERM_HASHTAG, $search];
213 $params = ['order' => ['created' => true],
214 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
215 $terms = DBA::select('term', ['oid'], $condition, $params);
218 while ($term = DBA::fetch($terms)) {
219 $itemids[] = $term['oid'];
223 if (!empty($itemids)) {
224 $params = ['order' => ['id' => true]];
225 $items = Item::selectForUser(local_user(), [], ['id' => $itemids], $params);
226 $r = Item::inArray($items);
231 Logger::log("Start fulltext search for '".$search."'", Logger::DEBUG);
233 $condition = ["(`uid` = 0 OR (`uid` = ? AND NOT `global`))
234 AND `body` LIKE CONCAT('%',?,'%')",
235 local_user(), $search];
236 $params = ['order' => ['id' => true],
237 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
238 $items = Item::selectForUser(local_user(), [], $condition, $params);
239 $r = Item::inArray($items);
242 if (!DBA::isResult($r)) {
243 info(L10n::t('No results.') . EOL);
249 $title = L10n::t('Items tagged with: %s', $search);
251 $title = L10n::t('Results for: %s', $search);
254 $o .= Renderer::replaceMacros(Renderer::getMarkupTemplate("section_title.tpl"),[
258 Logger::log("Start Conversation for '".$search."'", Logger::DEBUG);
259 $o .= conversation($a, $r, $pager, 'search', false, false, 'commented', local_user());
261 $o .= $pager->renderMinimal(count($r));
263 Logger::log("Done '".$search."'", Logger::DEBUG);