diff --git a/Classes/Controller/AbstractController.php b/Classes/Controller/AbstractController.php index c830a8a32..0011ec3ea 100644 --- a/Classes/Controller/AbstractController.php +++ b/Classes/Controller/AbstractController.php @@ -195,7 +195,7 @@ protected function configureProxyUrl(string &$url): void ->setCreateAbsoluteUri(!empty($this->extConf['general']['forceAbsoluteUrl'])) ->setArguments( [ - 'eID' => 'tx_dlf_pageview_proxy', + 'middleware' => 'dlf/page-view-proxy', 'url' => $url, 'uHash' => GeneralUtility::hmac($url, 'PageViewProxy') ] diff --git a/Classes/Eid/PageViewProxy.php b/Classes/Middleware/PageViewProxy.php similarity index 90% rename from Classes/Eid/PageViewProxy.php rename to Classes/Middleware/PageViewProxy.php index 99d3fc37c..b14eba36e 100644 --- a/Classes/Eid/PageViewProxy.php +++ b/Classes/Middleware/PageViewProxy.php @@ -10,12 +10,14 @@ * LICENSE.txt file that was distributed with this source code. */ -namespace Kitodo\Dlf\Eid; +namespace Kitodo\Dlf\Middleware; use Kitodo\Dlf\Common\Helper; use Kitodo\Dlf\Common\StdOutStream; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; +use Psr\Http\Server\MiddlewareInterface; +use Psr\Http\Server\RequestHandlerInterface; use TYPO3\CMS\Core\Configuration\ExtensionConfiguration; use TYPO3\CMS\Core\Http\JsonResponse; use TYPO3\CMS\Core\Http\RequestFactory; @@ -23,7 +25,7 @@ use TYPO3\CMS\Core\Utility\GeneralUtility; /** - * eID image proxy for plugin 'Page View' of the 'dlf' extension + * Image proxy Middleware for plugin 'Page View' of the 'dlf' extension * * Supported query parameters: * - `url` (mandatory): The URL to be proxied @@ -34,7 +36,7 @@ * * @access public */ -class PageViewProxy +class PageViewProxy implements MiddlewareInterface { /** * @access protected @@ -61,15 +63,51 @@ public function __construct() $this->extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('dlf', 'general'); } + /** + * The process method of the middleware. + * + * @access public + * + * @param ServerRequestInterface $request + * @param RequestHandlerInterface $handler + * + * @return ResponseInterface + */ + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface + { + $response = $handler->handle($request); + // Get input parameters and decrypt core name. + $parameters = $request->getQueryParams(); + if (!isset($parameters['middleware']) || ($parameters['middleware'] != 'dlf/page-view-proxy')) { + return $response; + } + + switch ($request->getMethod()) { + case 'OPTIONS': + return $this->handleOptions($request); + + case 'GET': + return $this->handleGet($request); + + case 'HEAD': + return $this->handleHead($request); + + default: + // 405 Method Not Allowed + return GeneralUtility::makeInstance(Response::class) + ->withStatus(405); + } + } + /** * Return a response that is derived from $response and contains CORS * headers to be sent to the client. - * + * * @access protected * * @param ResponseInterface $response * @param ServerRequestInterface $request The incoming request. - * + * * @return ResponseInterface */ protected function withCorsResponseHeaders( @@ -223,32 +261,4 @@ protected function handleGet(ServerRequestInterface $request): ResponseInterface return $this->withCorsResponseHeaders($clientResponse, $request); } - - /** - * The main method of the eID script - * - * @access public - * - * @param ServerRequestInterface $request - * - * @return ResponseInterface - */ - public function main(ServerRequestInterface $request): ResponseInterface - { - switch ($request->getMethod()) { - case 'OPTIONS': - return $this->handleOptions($request); - - case 'GET': - return $this->handleGet($request); - - case 'HEAD': - return $this->handleHead($request); - - default: - // 405 Method Not Allowed - return GeneralUtility::makeInstance(Response::class) - ->withStatus(405); - } - } } diff --git a/Configuration/RequestMiddlewares.php b/Configuration/RequestMiddlewares.php index f328daa0f..dc29972c1 100644 --- a/Configuration/RequestMiddlewares.php +++ b/Configuration/RequestMiddlewares.php @@ -11,6 +11,11 @@ */ return [ + 'backend' => [ + 'dlf/page-view-proxy' => [ + 'target' => \Kitodo\Dlf\Middleware\PageViewProxy::class, + ], + ], 'frontend' => [ 'dlf/search-in-document' => [ 'target' => \Kitodo\Dlf\Middleware\SearchInDocument::class, diff --git a/Tests/Functional/Api/PageViewProxyDisabledTest.php b/Tests/Functional/Api/PageViewProxyDisabledTest.php index d31e7411c..913c63583 100644 --- a/Tests/Functional/Api/PageViewProxyDisabledTest.php +++ b/Tests/Functional/Api/PageViewProxyDisabledTest.php @@ -21,7 +21,7 @@ class PageViewProxyDisabledTest extends FunctionalTestCase protected function queryProxy(array $query, string $method = 'GET') { - $query['eID'] = 'tx_dlf_pageview_proxy'; + $query['middleware'] = 'dlf/page-view-proxy'; return $this->httpClient->request($method, '', [ 'query' => $query, diff --git a/Tests/Functional/Api/PageViewProxyTest.php b/Tests/Functional/Api/PageViewProxyTest.php index 2a0879f51..f6b7e1e56 100644 --- a/Tests/Functional/Api/PageViewProxyTest.php +++ b/Tests/Functional/Api/PageViewProxyTest.php @@ -30,7 +30,7 @@ protected function getDlfConfiguration() protected function queryProxy(array $query, string $method = 'GET') { - $query['eID'] = 'tx_dlf_pageview_proxy'; + $query['middleware'] = 'dlf/page-view-proxy'; return $this->httpClient->request($method, '', [ 'query' => $query, diff --git a/ext_localconf.php b/ext_localconf.php index 09914da11..c582ad8b0 100644 --- a/ext_localconf.php +++ b/ext_localconf.php @@ -116,10 +116,6 @@ 'description' => 'LLL:EXT:dlf/Resources/Private/Language/locallang_tasks.xlf:suggestBuildTask.description', 'additionalFields' => \Kitodo\Dlf\Task\SuggestBuildAdditionalFieldProvider::class, ]; -// Register AJAX eID handlers. -if ($GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['dlf']['general']['enableInternalProxy'] ?? false) { - $GLOBALS['TYPO3_CONF_VARS']['FE']['eID_include']['tx_dlf_pageview_proxy'] = \Kitodo\Dlf\Eid\PageViewProxy::class . '::main'; -} // Use Caching Framework for Solr queries $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['tx_dlf_solr'] ??= [];