From 9d00dcc33fea126bef7adc7eba38644d7294605f Mon Sep 17 00:00:00 2001
From: Stefan Galinski <stefan@sgalinski.de>
Date: Fri, 13 Aug 2021 16:02:33 +0200
Subject: [PATCH] [BUGFIX] Remove totally stupid frontend link resolver code

---
 Classes/Middleware/FrontendLinkResolver.php | 79 ---------------------
 Classes/Service/RoutingService.php          | 30 +++-----
 Configuration/RequestMiddlewares.php        |  6 --
 3 files changed, 11 insertions(+), 104 deletions(-)
 delete mode 100644 Classes/Middleware/FrontendLinkResolver.php

diff --git a/Classes/Middleware/FrontendLinkResolver.php b/Classes/Middleware/FrontendLinkResolver.php
deleted file mode 100644
index 05dcfb9..0000000
--- a/Classes/Middleware/FrontendLinkResolver.php
+++ /dev/null
@@ -1,79 +0,0 @@
-<?php
-
-namespace SGalinski\SgRoutes\Middleware;
-
-/**
- *
- * Copyright notice
- *
- * (c) sgalinski Internet Services (https://www.sgalinski.de)
- *
- * All rights reserved
- *
- * This script is part of the TYPO3 project. The TYPO3 project is
- * free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * The GNU General Public License can be found at
- * http://www.gnu.org/copyleft/gpl.html.
- *
- * This script is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * This copyright notice MUST APPEAR in all copies of the script!
- */
-
-use Psr\Http\Message\ResponseInterface;
-use Psr\Http\Message\ServerRequestInterface;
-use Psr\Http\Server\MiddlewareInterface;
-use Psr\Http\Server\RequestHandlerInterface;
-use SGalinski\SgRoutes\Service\DemoModeService;
-use SGalinski\SgRoutes\Service\LicenceCheckService;
-use TYPO3\CMS\Core\Http\Response;
-use TYPO3\CMS\Core\Utility\GeneralUtility;
-use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
-
-/**
- * Resolver for generating frontend links
- *
- * @package SGalinski\SgRoutes\Middleware
- * @author Kevin Ditscheid <kevin.ditscheid@sgalinski.de>
- */
-class FrontendLinkResolver implements MiddlewareInterface {
-
-	/**
-	 * Process an incoming server request.
-	 *
-	 * Processes an incoming server request in order to produce a response.
-	 * If unable to produce the response itself, it may delegate to the provided
-	 * request handler to do so.
-	 */
-	public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface {
-		if (!LicenceCheckService::hasValidLicense() && !DemoModeService::isInDemoMode()) {
-			return $handler->handle($request);
-		}
-
-		$queryParams = $request->getQueryParams();
-		if (!isset($queryParams['tx_sgroutes_frontendlink'])) {
-			return $handler->handle($request);
-		}
-
-		$contentObjectRenderer = GeneralUtility::makeInstance(ContentObjectRenderer::class);
-		$response = new Response();
-		$response->getBody()
-			->write(
-				$contentObjectRenderer->typoLink(
-					'',
-					[
-						'parameter' => $queryParams['tx_sgroutes_frontendlink']['destination_url'],
-						'returnLast' => 'url'
-					]
-				)
-			);
-		return $response;
-	}
-}
diff --git a/Classes/Service/RoutingService.php b/Classes/Service/RoutingService.php
index 306b7fb..c55129d 100644
--- a/Classes/Service/RoutingService.php
+++ b/Classes/Service/RoutingService.php
@@ -32,6 +32,7 @@ use TYPO3\CMS\Core\Controller\ErrorPageController;
 use TYPO3\CMS\Core\Database\ConnectionPool;
 use TYPO3\CMS\Core\Error\Http\PageNotFoundException;
 use TYPO3\CMS\Core\Http\Request;
+use TYPO3\CMS\Core\Site\SiteFinder;
 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Core\Utility\HttpUtility;
@@ -39,6 +40,7 @@ use TYPO3\CMS\Core\Utility\VersionNumberUtility;
 use TYPO3\CMS\Extbase\Object\ObjectManager;
 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
 use TYPO3\CMS\Frontend\Controller\ErrorController;
+use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
 
 /**
  * Class SGalinski\SgRoutes\Service\RoutingService
@@ -74,7 +76,7 @@ class RoutingService {
 			foreach ($routes as $route) {
 				$route['description'] = \trim($route['description']);
 				if ($route['description']) {
-					$htaccessEntries .= \preg_replace('/(^)/m', '#$1', $route['description']) . LF;
+					$htaccessEntries .= \preg_replace('/(^)/m', '# $1', $route['description']) . LF;
 				}
 				$directive = 'Redirect';
 				if ($route['use_regular_expression']) {
@@ -88,29 +90,19 @@ class RoutingService {
 					\is_numeric($route['destination_url']) ||
 					strpos($route['destination_url'], 't3://') === 0
 				) {
-					$report = [
-						'error' => 0
-					];
-					$route['destination_url'] = GeneralUtility::getUrl(
-						GeneralUtility::getIndpEnv('TYPO3_REQUEST_HOST') . '/?' . HttpUtility::buildQueryString(
-							[
-								'tx_sgroutes_frontendlink' => [
-									'destination_url' => $route['destination_url']
-								]
-							]
-						),
-						0,
-						NULL,
-						$report
+					$contentObjectRenderer = GeneralUtility::makeInstance(ContentObjectRenderer::class);
+					$route['destination_url'] = $contentObjectRenderer->typoLink(
+						'',
+						[
+							'parameter' => $route['destination_url'],
+							'returnLast' => 'url'
+						]
 					);
-					if ($report['error'] !== 0) {
-						$route['destination_url'] = '';
-					}
 				}
 				if ($route['destination_url'] !== '') {
 					// only add the htaccess rule if there is a destination, because otherwise it will be 404 anyway
 					$htaccessEntries .= $directive . ' ' . $route['redirect_code'] . ' '
-						. $route['source_url'] . ' ' . $route['destination_url'] . LF;
+						. $route['source_url'] . ' ' . $route['destination_url'] . LF . LF;
 				}
 			}
 		}
diff --git a/Configuration/RequestMiddlewares.php b/Configuration/RequestMiddlewares.php
index 9457a3c..9abaa1a 100644
--- a/Configuration/RequestMiddlewares.php
+++ b/Configuration/RequestMiddlewares.php
@@ -31,12 +31,6 @@ return [
 			'before' => [
 				'typo3/cms-frontend/page-resolver'
 			]
-		],
-		'sgalinski/sg-routes/frontendlink-resolver' => [
-			'target' => \SGalinski\SgRoutes\Middleware\FrontendLinkResolver::class,
-			'after' => [
-				'typo3/cms-frontend/tsfe'
-			]
 		]
 	]
 ];
-- 
GitLab