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 if (!Feature::isEnabled(local_user(),'savedsearch'))
32 $r = q("SELECT `id`,`term` FROM `search` WHERE `uid` = %d",
36 if (DBA::isResult($r)) {
41 'term' => $rr['term'],
42 'encodedterm' => urlencode($rr['term']),
43 'delete' => L10n::t('Remove term'),
44 'selected' => ($search==$rr['term']),
49 $tpl = Renderer::getMarkupTemplate("saved_searches_aside.tpl");
51 $o .= Renderer::replaceMacros($tpl, [
52 '$title' => L10n::t('Saved Searches'),
64 function search_init(App $a) {
66 $search = ((x($_GET,'search')) ? Strings::escapeTags(trim(rawurldecode($_GET['search']))) : '');
69 if (x($_GET,'save') && $search) {
70 $r = q("SELECT * FROM `search` WHERE `uid` = %d AND `term` = '%s' LIMIT 1",
74 if (!DBA::isResult($r)) {
75 DBA::insert('search', ['uid' => local_user(), 'term' => $search]);
78 if (x($_GET,'remove') && $search) {
79 DBA::delete('search', ['uid' => local_user(), 'term' => $search]);
82 /// @todo Check if there is a case at all that "aside" is prefilled here
83 if (!isset($a->page['aside'])) {
84 $a->page['aside'] = '';
87 $a->page['aside'] .= search_saved_searches();
90 unset($_SESSION['theme']);
91 unset($_SESSION['mobile-theme']);
100 function search_post(App $a) {
101 if (x($_POST,'search'))
102 $a->data['search'] = $_POST['search'];
106 function search_content(App $a) {
108 if (Config::get('system','block_public') && !local_user() && !remote_user()) {
109 notice(L10n::t('Public access denied.') . EOL);
113 if (Config::get('system','local_search') && !local_user() && !remote_user()) {
114 System::httpExit(403,
115 ["title" => L10n::t("Public access denied."),
116 "description" => L10n::t("Only logged in users are permitted to perform a search.")]);
118 //notice(L10n::t('Public access denied.').EOL);
122 if (Config::get('system','permit_crawling') && !local_user() && !remote_user()) {
124 // 10 requests are "free", after the 11th only a call per minute is allowed
126 $free_crawls = intval(Config::get('system','free_crawls'));
127 if ($free_crawls == 0)
130 $crawl_permit_period = intval(Config::get('system','crawl_permit_period'));
131 if ($crawl_permit_period == 0)
132 $crawl_permit_period = 10;
134 $remote = $_SERVER["REMOTE_ADDR"];
135 $result = Cache::get("remote_search:".$remote);
136 if (!is_null($result)) {
137 $resultdata = json_decode($result);
138 if (($resultdata->time > (time() - $crawl_permit_period)) && ($resultdata->accesses > $free_crawls)) {
139 System::httpExit(429,
140 ["title" => L10n::t("Too Many Requests"),
141 "description" => L10n::t("Only one search per minute is permitted for not logged in users.")]);
144 Cache::set("remote_search:".$remote, json_encode(["time" => time(), "accesses" => $resultdata->accesses + 1]), Cache::HOUR);
146 Cache::set("remote_search:".$remote, json_encode(["time" => time(), "accesses" => 1]), Cache::HOUR);
149 Nav::setSelected('search');
152 if (x($a->data,'search'))
153 $search = Strings::escapeTags(trim($a->data['search']));
155 $search = ((x($_GET,'search')) ? Strings::escapeTags(trim(rawurldecode($_GET['search']))) : '');
158 if (x($_GET,'tag')) {
160 $search = (x($_GET,'tag') ? '#' . Strings::escapeTags(trim(rawurldecode($_GET['tag']))) : '');
163 // contruct a wrapper for the search header
164 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate("content_wrapper.tpl"),[
165 'name' => "search-header",
166 '$title' => L10n::t("Search"),
168 '$content' => HTML::search($search,'search-box','search',((local_user()) ? true : false), false)
171 if (strpos($search,'#') === 0) {
173 $search = substr($search,1);
175 if (strpos($search,'@') === 0) {
176 return dirfind_content($a);
178 if (strpos($search,'!') === 0) {
179 return dirfind_content($a);
182 if (x($_GET,'search-option'))
183 switch($_GET['search-option']) {
190 return dirfind_content($a, "@");
193 return dirfind_content($a, "!");
200 if (Config::get('system','only_tag_search'))
203 // Here is the way permissions work in the search module...
204 // Only public posts can be shown
205 // OR your own posts if you are a logged in member
206 // No items will be shown if the member has a blocked profile wall.
208 $pager = new Pager($a->query_string);
211 Logger::log("Start tag search for '".$search."'", Logger::DEBUG);
213 $condition = ["(`uid` = 0 OR (`uid` = ? AND NOT `global`))
214 AND `otype` = ? AND `type` = ? AND `term` = ?",
215 local_user(), TERM_OBJ_POST, TERM_HASHTAG, $search];
216 $params = ['order' => ['created' => true],
217 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
218 $terms = DBA::select('term', ['oid'], $condition, $params);
221 while ($term = DBA::fetch($terms)) {
222 $itemids[] = $term['oid'];
226 if (!empty($itemids)) {
227 $params = ['order' => ['id' => true]];
228 $items = Item::selectForUser(local_user(), [], ['id' => $itemids], $params);
229 $r = Item::inArray($items);
234 Logger::log("Start fulltext search for '".$search."'", Logger::DEBUG);
236 $condition = ["(`uid` = 0 OR (`uid` = ? AND NOT `global`))
237 AND `body` LIKE CONCAT('%',?,'%')",
238 local_user(), $search];
239 $params = ['order' => ['id' => true],
240 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
241 $items = Item::selectForUser(local_user(), [], $condition, $params);
242 $r = Item::inArray($items);
245 if (!DBA::isResult($r)) {
246 info(L10n::t('No results.') . EOL);
252 $title = L10n::t('Items tagged with: %s', $search);
254 $title = L10n::t('Results for: %s', $search);
257 $o .= Renderer::replaceMacros(Renderer::getMarkupTemplate("section_title.tpl"),[
261 Logger::log("Start Conversation for '".$search."'", Logger::DEBUG);
262 $o .= conversation($a, $r, $pager, 'search', false, false, 'commented', local_user());
264 $o .= $pager->renderMinimal(count($r));
266 Logger::log("Done '".$search."'", Logger::DEBUG);