‪TYPO3CMS  10.4
StaticRouteResolver.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Http\Message\ResponseInterface;
21 use Psr\Http\Message\ServerRequestInterface;
22 use Psr\Http\Server\MiddlewareInterface;
23 use Psr\Http\Server\RequestHandlerInterface;
32 
36 class ‪StaticRouteResolver implements MiddlewareInterface
37 {
41  protected ‪$requestFactory;
42 
46  protected ‪$linkService;
47 
48  public function ‪__construct(
51  ) {
52  $this->requestFactory = ‪$requestFactory;
53  $this->linkService = ‪$linkService;
54  }
55 
63  public function ‪process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
64  {
65  if (($site = $request->getAttribute('site', null)) instanceof ‪Site &&
66  ($configuration = $site->getConfiguration()['routes'] ?? null)
67  ) {
68  $path = ltrim($request->getUri()->getPath(), '/');
69  $routeConfig = $this->‪getApplicableStaticRoute($configuration, $site, $path);
70  if (is_array($routeConfig)) {
71  try {
72  [$content, $contentType] = $this->‪resolveByType($request, $site, $routeConfig['type'], $routeConfig);
73  } catch (InvalidRouteArgumentsException $e) {
74  return new Response('Invalid route', 404, ['Content-Type' => 'text/plain']);
75  }
76 
77  return new HtmlResponse($content, 200, ['Content-Type' => $contentType]);
78  }
79  }
80  return $handler->handle($request);
81  }
82 
93  protected function ‪getApplicableStaticRoute(array $staticRouteConfiguration, Site $site, string $uriPath): ?array
94  {
95  $routeNames = array_map(function (?string $route) use ($site) {
96  if ($route === null || $route === '') {
97  return null;
98  }
99  return ltrim(trim($site->getBase()->getPath(), '/') . '/' . ltrim($route, '/'), '/');
100  }, array_column($staticRouteConfiguration, 'route'));
101  // Remove empty routes which would throw an error (could happen within creating a false route in the GUI)
102  $routeNames = array_filter($routeNames);
103 
104  if (in_array($uriPath, $routeNames, true)) {
105  $key = array_search($uriPath, $routeNames, true);
106  // Only allow routes with a type "given"
107  if (isset($staticRouteConfiguration[$key]['type'])) {
108  return $staticRouteConfiguration[$key];
109  }
110  }
111  return null;
112  }
113 
118  protected function ‪getFromFile(File $file): array
119  {
120  $content = $file->getContents();
121  $contentType = $file->getMimeType();
122  return [$content, $contentType];
123  }
124 
129  protected function ‪getFromUri(string $uri): array
130  {
131  $response = $this->requestFactory->request($uri);
132  $contentType = 'text/plain; charset=utf-8';
133  $content = '';
134  if ($response->getStatusCode() === 200) {
135  $content = $response->getBody()->getContents();
136  $contentType = $response->getHeader('Content-Type');
137  }
138 
139  return [$content, $contentType];
140  }
141 
149  protected function ‪getPageUri(ServerRequestInterface $request, Site $site, array $urlParams): string
150  {
151  $parameters = [];
152  // Add additional parameters, if set via TypoLink
153  if (isset($urlParams['parameters'])) {
154  parse_str($urlParams['parameters'], $parameters);
155  }
156  $parameters['type'] = $urlParams['pagetype'] ?? 0;
157  $parameters['_language'] = $request->getAttribute('language', null);
158  $uri = $site->getRouter()->generateUri(
159  (int)$urlParams['pageuid'],
160  $parameters,
161  '',
163  );
164  return (string)$uri;
165  }
166 
175  protected function ‪resolveByType(ServerRequestInterface $request, Site $site, string $type, array $routeConfig): array
176  {
177  switch ($type) {
178  case 'staticText':
179  $content = $routeConfig['content'];
180  $contentType = 'text/plain; charset=utf-8';
181  break;
182  case 'uri':
183  $urlParams = $this->linkService->resolve($routeConfig['source']);
184  if ($urlParams['type'] === 'url' || $urlParams['type'] === 'page') {
185  $uri = $urlParams['url'] ?? $this->‪getPageUri($request, $site, $urlParams);
186  [$content, $contentType] = $this->‪getFromUri($uri);
187  } elseif ($urlParams['type'] === 'file') {
188  [$content, $contentType] = $this->‪getFromFile($urlParams['file']);
189  } else {
190  throw new \InvalidArgumentException('Can only handle URIs of type page, url or file.', 1537348076);
191  }
192 
193  break;
194  default:
195  throw new \InvalidArgumentException(
196  'Can only handle static file configurations with type uri or staticText.',
197  1537348083
198  );
199  }
200  return [$content, $contentType];
201  }
202 }
‪TYPO3\CMS\Core\Routing\RouterInterface
Definition: RouterInterface.php:28
‪TYPO3\CMS\Frontend\Middleware\StaticRouteResolver
Definition: StaticRouteResolver.php:37
‪TYPO3\CMS\Core\Resource\AbstractFile\getMimeType
‪string getMimeType()
Definition: AbstractFile.php:268
‪TYPO3\CMS\Frontend\Middleware\StaticRouteResolver\$requestFactory
‪RequestFactory $requestFactory
Definition: StaticRouteResolver.php:40
‪TYPO3\CMS\Core\Site\Entity\Site\getRouter
‪RouterInterface getRouter(Context $context=null)
Definition: Site.php:365
‪TYPO3\CMS\Frontend\Middleware\StaticRouteResolver\process
‪ResponseInterface process(ServerRequestInterface $request, RequestHandlerInterface $handler)
Definition: StaticRouteResolver.php:61
‪TYPO3\CMS\Core\Routing\RouterInterface\generateUri
‪UriInterface generateUri($route, array $parameters=[], string $fragment='', string $type=self::ABSOLUTE_URL)
‪TYPO3\CMS\Frontend\Middleware\StaticRouteResolver\getFromFile
‪array getFromFile(File $file)
Definition: StaticRouteResolver.php:116
‪TYPO3\CMS\Core\Site\Entity\Site
Definition: Site.php:40
‪TYPO3\CMS\Frontend\Middleware\StaticRouteResolver\getApplicableStaticRoute
‪array null getApplicableStaticRoute(array $staticRouteConfiguration, Site $site, string $uriPath)
Definition: StaticRouteResolver.php:91
‪TYPO3\CMS\Frontend\Middleware\StaticRouteResolver\__construct
‪__construct(RequestFactory $requestFactory, LinkService $linkService)
Definition: StaticRouteResolver.php:46
‪TYPO3\CMS\Core\Http\Response
Definition: Response.php:30
‪TYPO3\CMS\Frontend\Middleware\StaticRouteResolver\getPageUri
‪string getPageUri(ServerRequestInterface $request, Site $site, array $urlParams)
Definition: StaticRouteResolver.php:147
‪TYPO3\CMS\Frontend\Middleware
Definition: BackendUserAuthenticator.php:18
‪TYPO3\CMS\Frontend\Middleware\StaticRouteResolver\$linkService
‪LinkService $linkService
Definition: StaticRouteResolver.php:44
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:24
‪TYPO3\CMS\Core\Routing\InvalidRouteArgumentsException
Definition: InvalidRouteArgumentsException.php:26
‪TYPO3\CMS\Frontend\Middleware\StaticRouteResolver\getFromUri
‪array getFromUri(string $uri)
Definition: StaticRouteResolver.php:127
‪TYPO3\CMS\Core\Resource\File\getContents
‪string getContents()
Definition: File.php:128
‪TYPO3\CMS\Core\Http\RequestFactory
Definition: RequestFactory.php:31
‪TYPO3\CMS\Core\Site\Entity\Site\getBase
‪UriInterface getBase()
Definition: Site.php:187
‪TYPO3\CMS\Core\Routing\RouterInterface\ABSOLUTE_URL
‪const ABSOLUTE_URL
Definition: RouterInterface.php:32
‪TYPO3\CMS\Core\Http\HtmlResponse
Definition: HtmlResponse.php:26
‪TYPO3\CMS\Frontend\Middleware\StaticRouteResolver\resolveByType
‪array resolveByType(ServerRequestInterface $request, Site $site, string $type, array $routeConfig)
Definition: StaticRouteResolver.php:173