]> git.mxchange.org Git - friendica.git/blobdiff - src/Module/Search/Index.php
Mode depending control for the behaviour with blocked contacts
[friendica.git] / src / Module / Search / Index.php
index 1c55ff4b4b0f483bb046e12194b1aa46e1c38100..d1cd5f40d3659f394471e960f851b3c599986849 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2023, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
 
 namespace Friendica\Module\Search;
 
+use Friendica\App;
+use Friendica\Content\Conversation;
 use Friendica\Content\Nav;
 use Friendica\Content\Pager;
 use Friendica\Content\Text\HTML;
 use Friendica\Content\Widget;
-use Friendica\Core\Cache\Duration;
+use Friendica\Core\Cache\Enum\Duration;
+use Friendica\Core\L10n;
 use Friendica\Core\Logger;
 use Friendica\Core\Renderer;
 use Friendica\Core\Search;
-use Friendica\Core\Session;
 use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Model\Contact;
 use Friendica\Model\Item;
-use Friendica\Model\ItemContent;
 use Friendica\Model\Post;
 use Friendica\Model\Tag;
 use Friendica\Module\BaseSearch;
+use Friendica\Module\Response;
 use Friendica\Network\HTTPException;
-use Friendica\Util\Strings;
+use Friendica\Util\Network;
+use Friendica\Util\Profiler;
+use Psr\Log\LoggerInterface;
 
 class Index extends BaseSearch
 {
-       public static function content(array $parameters = [])
+       /** @var string  */
+       private $remoteAddress;
+
+       public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, App\Request $request, array $server, array $parameters = [])
        {
-               $search = (!empty($_GET['q']) ? Strings::escapeTags(trim(rawurldecode($_GET['q']))) : '');
+               parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
+
+               $this->remoteAddress = $request->getRemoteAddress();
+       }
 
-               if (DI::config()->get('system', 'block_public') && !Session::isAuthenticated()) {
+       protected function content(array $request = []): string
+       {
+               $search = (!empty($_GET['q']) ? trim(rawurldecode($_GET['q'])) : '');
+
+               if (DI::config()->get('system', 'block_public') && !DI::userSession()->isAuthenticated()) {
                        throw new HTTPException\ForbiddenException(DI::l10n()->t('Public access denied.'));
                }
 
-               if (DI::config()->get('system', 'local_search') && !Session::isAuthenticated()) {
-                       $e = new HTTPException\ForbiddenException(DI::l10n()->t('Only logged in users are permitted to perform a search.'));
-                       $e->httpdesc = DI::l10n()->t('Public access denied.');
-                       throw $e;
+               if (DI::config()->get('system', 'local_search') && !DI::userSession()->isAuthenticated()) {
+                       throw new HTTPException\ForbiddenException(DI::l10n()->t('Only logged in users are permitted to perform a search.'));
                }
 
-               if (DI::config()->get('system', 'permit_crawling') && !Session::isAuthenticated()) {
+               if (DI::config()->get('system', 'permit_crawling') && !DI::userSession()->isAuthenticated()) {
                        // Default values:
                        // 10 requests are "free", after the 11th only a call per minute is allowed
 
@@ -69,7 +81,7 @@ class Index extends BaseSearch
                        if ($crawl_permit_period == 0)
                                $crawl_permit_period = 10;
 
-                       $remote = $_SERVER['REMOTE_ADDR'];
+                       $remote = $this->remoteAddress;
                        $result = DI::cache()->get('remote_search:' . $remote);
                        if (!is_null($result)) {
                                $resultdata = json_decode($result);
@@ -82,7 +94,7 @@ class Index extends BaseSearch
                        }
                }
 
-               if (local_user()) {
+               if (DI::userSession()->getLocalUserId()) {
                        DI::page()['aside'] .= Widget\SavedSearches::getHTML(Search::getSearchPath($search), $search);
                }
 
@@ -91,7 +103,7 @@ class Index extends BaseSearch
                $tag = false;
                if (!empty($_GET['tag'])) {
                        $tag = true;
-                       $search = '#' . Strings::escapeTags(trim(rawurldecode($_GET['tag'])));
+                       $search = '#' . trim(rawurldecode($_GET['tag']));
                }
 
                // contruct a wrapper for the search header
@@ -109,27 +121,27 @@ class Index extends BaseSearch
                if (strpos($search, '#') === 0) {
                        $tag = true;
                        $search = substr($search, 1);
-               }
-
-               self::tryRedirectToProfile($search);
-
-               if (strpos($search, '@') === 0 || strpos($search, '!') === 0) {
-                       return self::performContactSearch($search);
-               }
+               } else {
+                       if (strpos($search, '@') === 0 || strpos($search, '!') === 0) {
+                               return self::performContactSearch($search);
+                       }
 
-               self::tryRedirectToPost($search);
-
-               if (!empty($_GET['search-option'])) {
-                       switch ($_GET['search-option']) {
-                               case 'fulltext':
-                                       break;
-                               case 'tags':
-                                       $tag = true;
-                                       break;
-                               case 'contacts':
-                                       return self::performContactSearch($search, '@');
-                               case 'forums':
-                                       return self::performContactSearch($search, '!');
+                       self::tryRedirectToPost($search);
+
+                       self::tryRedirectToProfile($search);
+
+                       if (!empty($_GET['search-option'])) {
+                               switch ($_GET['search-option']) {
+                                       case 'fulltext':
+                                               break;
+                                       case 'tags':
+                                               $tag = true;
+                                               break;
+                                       case 'contacts':
+                                               return self::performContactSearch($search, '@');
+                                       case 'forums':
+                                               return self::performContactSearch($search, '!');
+                               }
                        }
                }
 
@@ -137,7 +149,7 @@ class Index extends BaseSearch
                // Tags don't look like an URL and the fulltext search does only work with natural words
                if (parse_url($search, PHP_URL_SCHEME) && parse_url($search, PHP_URL_HOST)) {
                        Logger::info('Skipping tag and fulltext search since the search looks like a URL.', ['q' => $search]);
-                       notice(DI::l10n()->t('No results.'));
+                       DI::sysmsg()->addNotice(DI::l10n()->t('No results.'));
                        return $o;
                }
 
@@ -149,10 +161,10 @@ class Index extends BaseSearch
                // No items will be shown if the member has a blocked profile wall.
 
                if (DI::mode()->isMobile()) {
-                       $itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_mobile_network',
+                       $itemsPerPage = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'itemspage_mobile_network',
                                DI::config()->get('system', 'itemspage_network_mobile'));
                } else {
-                       $itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_network',
+                       $itemsPerPage = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'itemspage_network',
                                DI::config()->get('system', 'itemspage_network'));
                }
 
@@ -162,27 +174,29 @@ class Index extends BaseSearch
 
                if ($tag) {
                        Logger::info('Start tag search.', ['q' => $search]);
-                       $uriids = Tag::getURIIdListByTag($search, local_user(), $pager->getStart(), $pager->getItemsPerPage(), $last_uriid);
-                       $count = Tag::countByTag($search, local_user());
+                       $uriids = Tag::getURIIdListByTag($search, DI::userSession()->getLocalUserId(), $pager->getStart(), $pager->getItemsPerPage(), $last_uriid);
+                       $count = Tag::countByTag($search, DI::userSession()->getLocalUserId());
                } else {
                        Logger::info('Start fulltext search.', ['q' => $search]);
-                       $uriids = ItemContent::getURIIdListBySearch($search, local_user(), $pager->getStart(), $pager->getItemsPerPage(), $last_uriid);
-                       $count = ItemContent::countBySearch($search, local_user());
+                       $uriids = Post\Content::getURIIdListBySearch($search, DI::userSession()->getLocalUserId(), $pager->getStart(), $pager->getItemsPerPage(), $last_uriid);
+                       $count = Post\Content::countBySearch($search, DI::userSession()->getLocalUserId());
                }
 
                if (!empty($uriids)) {
-                       $params = ['order' => ['id' => true], 'group_by' => ['uri-id']];
-                       $items = Post::toArray(Post::selectForUser(local_user(), [], ['uri-id' => $uriids], $params));
+                       $condition = ["(`uid` = ? OR (`uid` = ? AND NOT `global`))", 0, DI::userSession()->getLocalUserId()];
+                       $condition = DBA::mergeConditions($condition, ['uri-id' => $uriids]);
+                       $params = ['order' => ['id' => true]];
+                       $items = Post::toArray(Post::selectForUser(DI::userSession()->getLocalUserId(), Item::DISPLAY_FIELDLIST, $condition, $params));
                }
 
                if (empty($items)) {
                        if (empty($last_uriid)) {
-                               notice(DI::l10n()->t('No results.'));
+                               DI::sysmsg()->addNotice(DI::l10n()->t('No results.'));
                        }
                        return $o;
                }
 
-               if (DI::pConfig()->get(local_user(), 'system', 'infinite_scroll')) {
+               if (DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'infinite_scroll')) {
                        $tpl = Renderer::getMarkupTemplate('infinite_scroll_head.tpl');
                        $o .= Renderer::replaceMacros($tpl, ['$reload_uri' => DI::args()->getQueryString()]);
                }
@@ -199,9 +213,9 @@ class Index extends BaseSearch
 
                Logger::info('Start Conversation.', ['q' => $search]);
 
-               $o .= conversation(DI::app(), $items, 'search', false, false, 'commented', local_user());
+               $o .= DI::conversation()->create($items, Conversation::MODE_SEARCH, false, false, 'commented', DI::userSession()->getLocalUserId());
 
-               if (DI::pConfig()->get(local_user(), 'system', 'infinite_scroll')) {
+               if (DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'infinite_scroll')) {
                        $o .= HTML::scrollLoader();
                } else {
                        $o .= $pager->renderMinimal($count);
@@ -228,7 +242,8 @@ class Index extends BaseSearch
         */
        private static function tryRedirectToProfile(string $search)
        {
-               $isUrl = !empty(parse_url($search, PHP_URL_SCHEME));
+               $search = Network::convertToIdn($search);
+               $isUrl  = !empty(parse_url($search, PHP_URL_SCHEME));
                $isAddr = (bool)preg_match('/^@?([a-z0-9.-_]+@[a-z0-9.-_:]+)$/i', trim($search), $matches);
 
                if (!$isUrl && !$isAddr) {
@@ -239,9 +254,9 @@ class Index extends BaseSearch
                        $search = $matches[1];
                }
 
-               if (local_user()) {
+               if (DI::userSession()->getLocalUserId()) {
                        // User-specific contact URL/address search
-                       $contact_id = Contact::getIdForURL($search, local_user());
+                       $contact_id = Contact::getIdForURL($search, DI::userSession()->getLocalUserId());
                        if (!$contact_id) {
                                // User-specific contact URL/address search and probe
                                $contact_id = Contact::getIdForURL($search);
@@ -276,9 +291,11 @@ class Index extends BaseSearch
                        return;
                }
 
-               if (local_user()) {
+               $search = Network::convertToIdn($search);
+
+               if (DI::userSession()->getLocalUserId()) {
                        // Post URL search
-                       $item_id = Item::fetchByLink($search, local_user());
+                       $item_id = Item::fetchByLink($search, DI::userSession()->getLocalUserId());
                        if (!$item_id) {
                                // If the user-specific search failed, we search and probe a public post
                                $item_id = Item::fetchByLink($search);