]> git.mxchange.org Git - friendica.git/blob - src/Module/Search/Index.php
ca5f7b609100612a2d23fadd27bbc270f9e74184
[friendica.git] / src / Module / Search / Index.php
1 <?php
2
3 namespace Friendica\Module\Search;
4
5 use Friendica\Content\Nav;
6 use Friendica\Content\Pager;
7 use Friendica\Content\Text\HTML;
8 use Friendica\Content\Widget;
9 use Friendica\Core\Cache\Duration;
10 use Friendica\Core\Config;
11 use Friendica\Core\Logger;
12 use Friendica\Core\Renderer;
13 use Friendica\Core\Session;
14 use Friendica\Database\DBA;
15 use Friendica\DI;
16 use Friendica\Model\Contact;
17 use Friendica\Model\Item;
18 use Friendica\Model\Term;
19 use Friendica\Module\BaseSearchModule;
20 use Friendica\Network\HTTPException;
21 use Friendica\Util\Strings;
22
23 class Index extends BaseSearchModule
24 {
25         public static function content(array $parameters = [])
26         {
27                 $search = (!empty($_GET['q']) ? Strings::escapeTags(trim(rawurldecode($_GET['q']))) : '');
28
29                 if (Config::get('system', 'block_public') && !Session::isAuthenticated()) {
30                         throw new HTTPException\ForbiddenException(DI::l10n()->t('Public access denied.'));
31                 }
32
33                 if (Config::get('system', 'local_search') && !Session::isAuthenticated()) {
34                         $e = new HTTPException\ForbiddenException(DI::l10n()->t('Only logged in users are permitted to perform a search.'));
35                         $e->httpdesc = DI::l10n()->t('Public access denied.');
36                         throw $e;
37                 }
38
39                 if (Config::get('system', 'permit_crawling') && !Session::isAuthenticated()) {
40                         // Default values:
41                         // 10 requests are "free", after the 11th only a call per minute is allowed
42
43                         $free_crawls = intval(Config::get('system', 'free_crawls'));
44                         if ($free_crawls == 0)
45                                 $free_crawls = 10;
46
47                         $crawl_permit_period = intval(Config::get('system', 'crawl_permit_period'));
48                         if ($crawl_permit_period == 0)
49                                 $crawl_permit_period = 10;
50
51                         $remote = $_SERVER['REMOTE_ADDR'];
52                         $result = DI::cache()->get('remote_search:' . $remote);
53                         if (!is_null($result)) {
54                                 $resultdata = json_decode($result);
55                                 if (($resultdata->time > (time() - $crawl_permit_period)) && ($resultdata->accesses > $free_crawls)) {
56                                         throw new HTTPException\TooManyRequestsException(DI::l10n()->t('Only one search per minute is permitted for not logged in users.'));
57                                 }
58                                 DI::cache()->set('remote_search:' . $remote, json_encode(['time' => time(), 'accesses' => $resultdata->accesses + 1]), Duration::HOUR);
59                         } else {
60                                 DI::cache()->set('remote_search:' . $remote, json_encode(['time' => time(), 'accesses' => 1]), Duration::HOUR);
61                         }
62                 }
63
64                 if (local_user()) {
65                         DI::page()['aside'] .= Widget\SavedSearches::getHTML('search?q=' . urlencode($search), $search);
66                 }
67
68                 Nav::setSelected('search');
69
70                 $tag = false;
71                 if (!empty($_GET['tag'])) {
72                         $tag = true;
73                         $search = '#' . Strings::escapeTags(trim(rawurldecode($_GET['tag'])));
74                 }
75
76                 // contruct a wrapper for the search header
77                 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('content_wrapper.tpl'), [
78                         'name' => 'search-header',
79                         '$title' => DI::l10n()->t('Search'),
80                         '$title_size' => 3,
81                         '$content' => HTML::search($search, 'search-box', false)
82                 ]);
83
84                 if (!$search) {
85                         return $o;
86                 }
87
88                 if (strpos($search, '#') === 0) {
89                         $tag = true;
90                         $search = substr($search, 1);
91                 }
92
93                 self::tryRedirectToProfile($search);
94
95                 if (strpos($search, '@') === 0 || strpos($search, '!') === 0) {
96                         return self::performContactSearch($search);
97                 }
98
99                 self::tryRedirectToPost($search);
100
101                 if (!empty($_GET['search-option'])) {
102                         switch ($_GET['search-option']) {
103                                 case 'fulltext':
104                                         break;
105                                 case 'tags':
106                                         $tag = true;
107                                         break;
108                                 case 'contacts':
109                                         return self::performContactSearch($search, '@');
110                                 case 'forums':
111                                         return self::performContactSearch($search, '!');
112                         }
113                 }
114
115                 $tag = $tag || Config::get('system', 'only_tag_search');
116
117                 // Here is the way permissions work in the search module...
118                 // Only public posts can be shown
119                 // OR your own posts if you are a logged in member
120                 // No items will be shown if the member has a blocked profile wall.
121
122                 $pager = new Pager(DI::args()->getQueryString());
123
124                 if ($tag) {
125                         Logger::info('Start tag search.', ['q' => $search]);
126
127                         $condition = [
128                                 "(`uid` = 0 OR (`uid` = ? AND NOT `global`))
129                                 AND `otype` = ? AND `type` = ? AND `term` = ?",
130                                 local_user(), Term::OBJECT_TYPE_POST, Term::HASHTAG, $search
131                         ];
132                         $params = [
133                                 'order' => ['received' => true],
134                                 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]
135                         ];
136                         $terms = DBA::select('term', ['oid'], $condition, $params);
137
138                         $itemids = [];
139                         while ($term = DBA::fetch($terms)) {
140                                 $itemids[] = $term['oid'];
141                         }
142
143                         DBA::close($terms);
144
145                         if (!empty($itemids)) {
146                                 $params = ['order' => ['id' => true]];
147                                 $items = Item::selectForUser(local_user(), [], ['id' => $itemids], $params);
148                                 $r = Item::inArray($items);
149                         } else {
150                                 $r = [];
151                         }
152                 } else {
153                         Logger::info('Start fulltext search.', ['q' => $search]);
154
155                         $condition = [
156                                 "(`uid` = 0 OR (`uid` = ? AND NOT `global`))
157                                 AND `body` LIKE CONCAT('%',?,'%')",
158                                 local_user(), $search
159                         ];
160                         $params = [
161                                 'order' => ['id' => true],
162                                 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]
163                         ];
164                         $items = Item::selectForUser(local_user(), [], $condition, $params);
165                         $r = Item::inArray($items);
166                 }
167
168                 if (!DBA::isResult($r)) {
169                         info(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, $pager, 'search', false, false, 'commented', local_user());
186
187                 $o .= $pager->renderMinimal(count($r));
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' => 'cid'], ['addr' => $search, 'uid' => 0]);
231                         } else {
232                                 $contact = Contact::getDetailsByURL($search, 0, ['cid' => 0]);
233                         }
234
235                         if (DBA::isResult($contact)) {
236                                 $contact_id = $contact['cid'];
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 }