]> git.mxchange.org Git - friendica.git/blob - src/Module/Search/Index.php
debaec7301516ad3353d95c903d485005716913e
[friendica.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\Tag;
39 use Friendica\Module\BaseSearch;
40 use Friendica\Network\HTTPException;
41 use Friendica\Util\Strings;
42
43 class Index extends BaseSearch
44 {
45         public static function content(array $parameters = [])
46         {
47                 $search = (!empty($_GET['q']) ? Strings::escapeTags(trim(rawurldecode($_GET['q']))) : '');
48
49                 if (DI::config()->get('system', 'block_public') && !Session::isAuthenticated()) {
50                         throw new HTTPException\ForbiddenException(DI::l10n()->t('Public access denied.'));
51                 }
52
53                 if (DI::config()->get('system', 'local_search') && !Session::isAuthenticated()) {
54                         $e = new HTTPException\ForbiddenException(DI::l10n()->t('Only logged in users are permitted to perform a search.'));
55                         $e->httpdesc = DI::l10n()->t('Public access denied.');
56                         throw $e;
57                 }
58
59                 if (DI::config()->get('system', 'permit_crawling') && !Session::isAuthenticated()) {
60                         // Default values:
61                         // 10 requests are "free", after the 11th only a call per minute is allowed
62
63                         $free_crawls = intval(DI::config()->get('system', 'free_crawls'));
64                         if ($free_crawls == 0)
65                                 $free_crawls = 10;
66
67                         $crawl_permit_period = intval(DI::config()->get('system', 'crawl_permit_period'));
68                         if ($crawl_permit_period == 0)
69                                 $crawl_permit_period = 10;
70
71                         $remote = $_SERVER['REMOTE_ADDR'];
72                         $result = DI::cache()->get('remote_search:' . $remote);
73                         if (!is_null($result)) {
74                                 $resultdata = json_decode($result);
75                                 if (($resultdata->time > (time() - $crawl_permit_period)) && ($resultdata->accesses > $free_crawls)) {
76                                         throw new HTTPException\TooManyRequestsException(DI::l10n()->t('Only one search per minute is permitted for not logged in users.'));
77                                 }
78                                 DI::cache()->set('remote_search:' . $remote, json_encode(['time' => time(), 'accesses' => $resultdata->accesses + 1]), Duration::HOUR);
79                         } else {
80                                 DI::cache()->set('remote_search:' . $remote, json_encode(['time' => time(), 'accesses' => 1]), Duration::HOUR);
81                         }
82                 }
83
84                 if (local_user()) {
85                         DI::page()['aside'] .= Widget\SavedSearches::getHTML(Search::getSearchPath($search), $search);
86                 }
87
88                 Nav::setSelected('search');
89
90                 $tag = false;
91                 if (!empty($_GET['tag'])) {
92                         $tag = true;
93                         $search = '#' . Strings::escapeTags(trim(rawurldecode($_GET['tag'])));
94                 }
95
96                 // contruct a wrapper for the search header
97                 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('content_wrapper.tpl'), [
98                         'name' => 'search-header',
99                         '$title' => DI::l10n()->t('Search'),
100                         '$title_size' => 3,
101                         '$content' => HTML::search($search, 'search-box', false)
102                 ]);
103
104                 if (!$search) {
105                         return $o;
106                 }
107
108                 if (strpos($search, '#') === 0) {
109                         $tag = true;
110                         $search = substr($search, 1);
111                 }
112
113                 self::tryRedirectToProfile($search);
114
115                 if (strpos($search, '@') === 0 || strpos($search, '!') === 0) {
116                         return self::performContactSearch($search);
117                 }
118
119                 self::tryRedirectToPost($search);
120
121                 if (!empty($_GET['search-option'])) {
122                         switch ($_GET['search-option']) {
123                                 case 'fulltext':
124                                         break;
125                                 case 'tags':
126                                         $tag = true;
127                                         break;
128                                 case 'contacts':
129                                         return self::performContactSearch($search, '@');
130                                 case 'forums':
131                                         return self::performContactSearch($search, '!');
132                         }
133                 }
134
135                 $tag = $tag || DI::config()->get('system', 'only_tag_search');
136
137                 // Here is the way permissions work in the search module...
138                 // Only public posts can be shown
139                 // OR your own posts if you are a logged in member
140                 // No items will be shown if the member has a blocked profile wall.
141
142                 if (DI::mode()->isMobile()) {
143                         $itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_mobile_network',
144                                 DI::config()->get('system', 'itemspage_network_mobile'));
145                 } else {
146                         $itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_network',
147                                 DI::config()->get('system', 'itemspage_network'));
148                 }
149
150                 $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), $itemsPerPage);
151
152                 if ($tag) {
153                         Logger::info('Start tag search.', ['q' => $search]);
154                         $uriids = Tag::getURIIdListByTag($search, local_user(), $pager->getStart(), $pager->getItemsPerPage());
155                         $count = Tag::countByTag($search, local_user());
156                 } else {
157                         Logger::info('Start fulltext search.', ['q' => $search]);
158                         $uriids = ItemContent::getURIIdListBySearch($search, local_user(), $pager->getStart(), $pager->getItemsPerPage());
159                         $count = ItemContent::countBySearch($search, local_user());
160                 }
161
162                 if (!empty($uriids)) {
163                         $params = ['order' => ['id' => true], 'group_by' => ['uri-id']];
164                         $items = Item::selectForUser(local_user(), [], ['uri-id' => $uriids], $params);
165                         $r = Item::inArray($items);
166                 }
167
168                 if (!DBA::isResult($r)) {
169                         notice(DI::l10n()->t('No results.'));
170                         return $o;
171                 }
172
173                 if ($tag) {
174                         $title = DI::l10n()->t('Items tagged with: %s', $search);
175                 } else {
176                         $title = DI::l10n()->t('Results for: %s', $search);
177                 }
178
179                 $o .= Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'), [
180                         '$title' => $title
181                 ]);
182
183                 Logger::info('Start Conversation.', ['q' => $search]);
184
185                 $o .= conversation(DI::app(), $r, 'search', false, false, 'commented', local_user());
186
187                 $o .= $pager->renderMinimal($count);
188
189                 return $o;
190         }
191
192         /**
193          * Tries to redirect to a local profile page based on the input.
194          *
195          * This method separates logged in and anonymous users. Logged in users can trigger contact probes to import
196          * non-existing contacts while anonymous users can only trigger a local lookup.
197          *
198          * Formats matched:
199          * - @user@domain
200          * - user@domain
201          * - Any fully-formed URL
202          *
203          * @param string  $search
204          * @throws HTTPException\InternalServerErrorException
205          * @throws \ImagickException
206          */
207         private static function tryRedirectToProfile(string $search)
208         {
209                 $isUrl = !empty(parse_url($search, PHP_URL_SCHEME));
210                 $isAddr = (bool)preg_match('/^@?([a-z0-9.-_]+@[a-z0-9.-_:]+)$/i', trim($search), $matches);
211
212                 if (!$isUrl && !$isAddr) {
213                         return;
214                 }
215
216                 if ($isAddr) {
217                         $search = $matches[1];
218                 }
219
220                 if (local_user()) {
221                         // User-specific contact URL/address search
222                         $contact_id = Contact::getIdForURL($search, local_user());
223                         if (!$contact_id) {
224                                 // User-specific contact URL/address search and probe
225                                 $contact_id = Contact::getIdForURL($search);
226                         }
227                 } else {
228                         // Cheaper local lookup for anonymous users, no probe
229                         if ($isAddr) {
230                                 $contact = Contact::selectFirst(['id'], ['addr' => $search, 'uid' => 0]);
231                         } else {
232                                 $contact = Contact::getByURL($search, null, ['id']) ?: ['id' => 0];
233                         }
234
235                         if (DBA::isResult($contact)) {
236                                 $contact_id = $contact['id'];
237                         }
238                 }
239
240                 if (!empty($contact_id)) {
241                         DI::baseUrl()->redirect('contact/' . $contact_id);
242                 }
243         }
244
245         /**
246          * Fetch/search a post by URL and redirects to its local representation if it was found.
247          *
248          * @param string  $search
249          * @throws HTTPException\InternalServerErrorException
250          */
251         private static function tryRedirectToPost(string $search)
252         {
253                 if (parse_url($search, PHP_URL_SCHEME) == '') {
254                         return;
255                 }
256
257                 if (local_user()) {
258                         // Post URL search
259                         $item_id = Item::fetchByLink($search, local_user());
260                         if (!$item_id) {
261                                 // If the user-specific search failed, we search and probe a public post
262                                 $item_id = Item::fetchByLink($search);
263                         }
264                 } else {
265                         // Cheaper local lookup for anonymous users, no probe
266                         $item_id = Item::searchByLink($search);
267                 }
268
269                 if (!empty($item_id)) {
270                         $item = Item::selectFirst(['guid'], ['id' => $item_id]);
271                         if (DBA::isResult($item)) {
272                                 DI::baseUrl()->redirect('display/' . $item['guid']);
273                         }
274                 }
275         }
276 }