78f200810c65a12a215a2e266d08ce694305f972
[friendica.git/.git] / src / Module / Search / Index.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Module\Search;
23
24 use Friendica\Content\Nav;
25 use Friendica\Content\Pager;
26 use Friendica\Content\Text\HTML;
27 use Friendica\Content\Widget;
28 use Friendica\Core\Cache\Duration;
29 use Friendica\Core\Logger;
30 use Friendica\Core\Renderer;
31 use Friendica\Core\Search;
32 use Friendica\Core\Session;
33 use Friendica\Database\DBA;
34 use Friendica\DI;
35 use Friendica\Model\Contact;
36 use Friendica\Model\Item;
37 use Friendica\Model\ItemContent;
38 use Friendica\Model\Post;
39 use Friendica\Model\Tag;
40 use Friendica\Module\BaseSearch;
41 use Friendica\Network\HTTPException;
42 use Friendica\Util\Strings;
43
44 class Index extends BaseSearch
45 {
46         public static function content(array $parameters = [])
47         {
48                 $search = (!empty($_GET['q']) ? Strings::escapeTags(trim(rawurldecode($_GET['q']))) : '');
49
50                 if (DI::config()->get('system', 'block_public') && !Session::isAuthenticated()) {
51                         throw new HTTPException\ForbiddenException(DI::l10n()->t('Public access denied.'));
52                 }
53
54                 if (DI::config()->get('system', 'local_search') && !Session::isAuthenticated()) {
55                         $e = new HTTPException\ForbiddenException(DI::l10n()->t('Only logged in users are permitted to perform a search.'));
56                         $e->httpdesc = DI::l10n()->t('Public access denied.');
57                         throw $e;
58                 }
59
60                 if (DI::config()->get('system', 'permit_crawling') && !Session::isAuthenticated()) {
61                         // Default values:
62                         // 10 requests are "free", after the 11th only a call per minute is allowed
63
64                         $free_crawls = intval(DI::config()->get('system', 'free_crawls'));
65                         if ($free_crawls == 0)
66                                 $free_crawls = 10;
67
68                         $crawl_permit_period = intval(DI::config()->get('system', 'crawl_permit_period'));
69                         if ($crawl_permit_period == 0)
70                                 $crawl_permit_period = 10;
71
72                         $remote = $_SERVER['REMOTE_ADDR'];
73                         $result = DI::cache()->get('remote_search:' . $remote);
74                         if (!is_null($result)) {
75                                 $resultdata = json_decode($result);
76                                 if (($resultdata->time > (time() - $crawl_permit_period)) && ($resultdata->accesses > $free_crawls)) {
77                                         throw new HTTPException\TooManyRequestsException(DI::l10n()->t('Only one search per minute is permitted for not logged in users.'));
78                                 }
79                                 DI::cache()->set('remote_search:' . $remote, json_encode(['time' => time(), 'accesses' => $resultdata->accesses + 1]), Duration::HOUR);
80                         } else {
81                                 DI::cache()->set('remote_search:' . $remote, json_encode(['time' => time(), 'accesses' => 1]), Duration::HOUR);
82                         }
83                 }
84
85                 if (local_user()) {
86                         DI::page()['aside'] .= Widget\SavedSearches::getHTML(Search::getSearchPath($search), $search);
87                 }
88
89                 Nav::setSelected('search');
90
91                 $tag = false;
92                 if (!empty($_GET['tag'])) {
93                         $tag = true;
94                         $search = '#' . Strings::escapeTags(trim(rawurldecode($_GET['tag'])));
95                 }
96
97                 // contruct a wrapper for the search header
98                 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('content_wrapper.tpl'), [
99                         'name' => 'search-header',
100                         '$title' => DI::l10n()->t('Search'),
101                         '$title_size' => 3,
102                         '$content' => HTML::search($search, 'search-box', false)
103                 ]);
104
105                 if (!$search) {
106                         return $o;
107                 }
108
109                 if (strpos($search, '#') === 0) {
110                         $tag = true;
111                         $search = substr($search, 1);
112                 }
113
114                 self::tryRedirectToProfile($search);
115
116                 if (strpos($search, '@') === 0 || strpos($search, '!') === 0) {
117                         return self::performContactSearch($search);
118                 }
119
120                 self::tryRedirectToPost($search);
121
122                 if (!empty($_GET['search-option'])) {
123                         switch ($_GET['search-option']) {
124                                 case 'fulltext':
125                                         break;
126                                 case 'tags':
127                                         $tag = true;
128                                         break;
129                                 case 'contacts':
130                                         return self::performContactSearch($search, '@');
131                                 case 'forums':
132                                         return self::performContactSearch($search, '!');
133                         }
134                 }
135
136                 // Don't perform a fulltext or tag search on search results that look like an URL
137                 // Tags don't look like an URL and the fulltext search does only work with natural words
138                 if (parse_url($search, PHP_URL_SCHEME) && parse_url($search, PHP_URL_HOST)) {
139                         Logger::info('Skipping tag and fulltext search since the search looks like a URL.', ['q' => $search]);
140                         notice(DI::l10n()->t('No results.'));
141                         return $o;
142                 }
143
144                 $tag = $tag || DI::config()->get('system', 'only_tag_search');
145
146                 // Here is the way permissions work in the search module...
147                 // Only public posts can be shown
148                 // OR your own posts if you are a logged in member
149                 // No items will be shown if the member has a blocked profile wall.
150
151                 if (DI::mode()->isMobile()) {
152                         $itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_mobile_network',
153                                 DI::config()->get('system', 'itemspage_network_mobile'));
154                 } else {
155                         $itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_network',
156                                 DI::config()->get('system', 'itemspage_network'));
157                 }
158
159                 $last_uriid = isset($_GET['last_uriid']) ? intval($_GET['last_uriid']) : 0;
160
161                 $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), $itemsPerPage);
162
163                 if ($tag) {
164                         Logger::info('Start tag search.', ['q' => $search]);
165                         $uriids = Tag::getURIIdListByTag($search, local_user(), $pager->getStart(), $pager->getItemsPerPage(), $last_uriid);
166                         $count = Tag::countByTag($search, local_user());
167                 } else {
168                         Logger::info('Start fulltext search.', ['q' => $search]);
169                         $uriids = Post\Content::getURIIdListBySearch($search, local_user(), $pager->getStart(), $pager->getItemsPerPage(), $last_uriid);
170                         $count = Post\Content::countBySearch($search, local_user());
171                 }
172
173                 if (!empty($uriids)) {
174                         $params = ['order' => ['id' => true], 'group_by' => ['uri-id']];
175                         $items = Post::toArray(Post::selectForUser(local_user(), [], ['uri-id' => $uriids], $params));
176                 }
177
178                 if (empty($items)) {
179                         if (empty($last_uriid)) {
180                                 notice(DI::l10n()->t('No results.'));
181                         }
182                         return $o;
183                 }
184
185                 if (DI::pConfig()->get(local_user(), 'system', 'infinite_scroll')) {
186                         $tpl = Renderer::getMarkupTemplate('infinite_scroll_head.tpl');
187                         $o .= Renderer::replaceMacros($tpl, ['$reload_uri' => DI::args()->getQueryString()]);
188                 }
189
190                 if ($tag) {
191                         $title = DI::l10n()->t('Items tagged with: %s', $search);
192                 } else {
193                         $title = DI::l10n()->t('Results for: %s', $search);
194                 }
195
196                 $o .= Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'), [
197                         '$title' => $title
198                 ]);
199
200                 Logger::info('Start Conversation.', ['q' => $search]);
201
202                 $o .= conversation(DI::app(), $items, 'search', false, false, 'commented', local_user());
203
204                 if (DI::pConfig()->get(local_user(), 'system', 'infinite_scroll')) {
205                         $o .= HTML::scrollLoader();
206                 } else {
207                         $o .= $pager->renderMinimal($count);
208                 }
209
210
211                 return $o;
212         }
213
214         /**
215          * Tries to redirect to a local profile page based on the input.
216          *
217          * This method separates logged in and anonymous users. Logged in users can trigger contact probes to import
218          * non-existing contacts while anonymous users can only trigger a local lookup.
219          *
220          * Formats matched:
221          * - @user@domain
222          * - user@domain
223          * - Any fully-formed URL
224          *
225          * @param string  $search
226          * @throws HTTPException\InternalServerErrorException
227          * @throws \ImagickException
228          */
229         private static function tryRedirectToProfile(string $search)
230         {
231                 $isUrl = !empty(parse_url($search, PHP_URL_SCHEME));
232                 $isAddr = (bool)preg_match('/^@?([a-z0-9.-_]+@[a-z0-9.-_:]+)$/i', trim($search), $matches);
233
234                 if (!$isUrl && !$isAddr) {
235                         return;
236                 }
237
238                 if ($isAddr) {
239                         $search = $matches[1];
240                 }
241
242                 if (local_user()) {
243                         // User-specific contact URL/address search
244                         $contact_id = Contact::getIdForURL($search, local_user());
245                         if (!$contact_id) {
246                                 // User-specific contact URL/address search and probe
247                                 $contact_id = Contact::getIdForURL($search);
248                         }
249                 } else {
250                         // Cheaper local lookup for anonymous users, no probe
251                         if ($isAddr) {
252                                 $contact = Contact::selectFirst(['id'], ['addr' => $search, 'uid' => 0]);
253                         } else {
254                                 $contact = Contact::getByURL($search, null, ['id']) ?: ['id' => 0];
255                         }
256
257                         if (DBA::isResult($contact)) {
258                                 $contact_id = $contact['id'];
259                         }
260                 }
261
262                 if (!empty($contact_id)) {
263                         DI::baseUrl()->redirect('contact/' . $contact_id);
264                 }
265         }
266
267         /**
268          * Fetch/search a post by URL and redirects to its local representation if it was found.
269          *
270          * @param string  $search
271          * @throws HTTPException\InternalServerErrorException
272          */
273         private static function tryRedirectToPost(string $search)
274         {
275                 if (parse_url($search, PHP_URL_SCHEME) == '') {
276                         return;
277                 }
278
279                 if (local_user()) {
280                         // Post URL search
281                         $item_id = Item::fetchByLink($search, local_user());
282                         if (!$item_id) {
283                                 // If the user-specific search failed, we search and probe a public post
284                                 $item_id = Item::fetchByLink($search);
285                         }
286                 } else {
287                         // Cheaper local lookup for anonymous users, no probe
288                         $item_id = Item::searchByLink($search);
289                 }
290
291                 if (!empty($item_id)) {
292                         $item = Post::selectFirst(['guid'], ['id' => $item_id]);
293                         if (DBA::isResult($item)) {
294                                 DI::baseUrl()->redirect('display/' . $item['guid']);
295                         }
296                 }
297         }
298 }