‪TYPO3CMS  9.5
StaticRouteResolver.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
3 
5 
6 /*
7  * This file is part of the TYPO3 CMS project.
8  *
9  * It is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License, either version 2
11  * of the License, or any later version.
12  *
13  * For the full copyright and license information, please read the
14  * LICENSE.txt file that was distributed with this source code.
15  *
16  * The TYPO3 project - inspiring people to share!
17  */
18 
19 use Psr\Http\Message\ResponseInterface;
20 use Psr\Http\Message\ServerRequestInterface;
21 use Psr\Http\Server\MiddlewareInterface;
22 use Psr\Http\Server\RequestHandlerInterface;
32 
36 class ‪StaticRouteResolver implements MiddlewareInterface
37 {
45  public function ‪process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
46  {
47  if (($site = $request->getAttribute('site', null)) instanceof ‪Site &&
48  ($configuration = $site->getConfiguration()['routes'] ?? null)
49  ) {
50  $path = ltrim($request->getUri()->getPath(), '/');
51  $routeConfig = $this->‪getApplicableStaticRoute($configuration, $site, $path);
52  if (is_array($routeConfig)) {
53  try {
54  [$content, $contentType] = $this->‪resolveByType($request, $site, $routeConfig['type'], $routeConfig);
56  return new ‪Response('Invalid route', 404, ['Content-Type' => 'text/plain']);
57  }
58 
59  return new ‪HtmlResponse($content, 200, ['Content-Type' => $contentType]);
60  }
61  }
62  return $handler->handle($request);
63  }
64 
75  protected function ‪getApplicableStaticRoute(array $staticRouteConfiguration, ‪Site $site, string $uriPath): ?array
76  {
77  $routeNames = array_map(function (?string $route) use ($site) {
78  if ($route === null || $route === '') {
79  return null;
80  }
81  return ltrim(trim($site->‪getBase()->getPath(), '/') . '/' . ltrim($route, '/'), '/');
82  }, array_column($staticRouteConfiguration, 'route'));
83  // Remove empty routes which would throw an error (could happen within creating a false route in the GUI)
84  $routeNames = array_filter($routeNames);
85 
86  if (in_array($uriPath, $routeNames, true)) {
87  $key = array_search($uriPath, $routeNames, true);
88  // Only allow routes with a type "given"
89  if (isset($staticRouteConfiguration[$key]['type'])) {
90  return $staticRouteConfiguration[$key];
91  }
92  }
93  return null;
94  }
95 
100  protected function ‪getFromFile(‪File $file): array
101  {
102  $content = $file->‪getContents();
103  $contentType = $file->‪getMimeType();
104  return [$content, $contentType];
105  }
106 
111  protected function ‪getFromUri(string $uri): array
112  {
113  $requestFactory = GeneralUtility::makeInstance(RequestFactory::class);
114  $response = $requestFactory->request($uri);
115  $contentType = 'text/plain; charset=utf-8';
116  $content = '';
117  if ($response->getStatusCode() === 200) {
118  $content = $response->getBody()->getContents();
119  $contentType = $response->getHeader('Content-Type');
120  }
121 
122  return [$content, $contentType];
123  }
124 
132  protected function ‪getPageUri(ServerRequestInterface $request, ‪Site $site, array $urlParams): string
133  {
134  $parameters = [];
135  // Add additional parameters, if set via TypoLink
136  if (isset($urlParams['parameters'])) {
137  parse_str($urlParams['parameters'], $parameters);
138  }
139  $parameters['type'] = $urlParams['pagetype'] ?? 0;
140  $parameters['_language'] = $request->getAttribute('language', null);
141  $uri = $site->‪getRouter()->‪generateUri(
142  (int)$urlParams['pageuid'],
143  $parameters,
144  '',
146  );
147  return (string)$uri;
148  }
149 
158  protected function ‪resolveByType(ServerRequestInterface $request, ‪Site $site, string $type, array $routeConfig): array
159  {
160  switch ($type) {
161  case 'staticText':
162  $content = $routeConfig['content'];
163  $contentType = 'text/plain; charset=utf-8';
164  break;
165  case 'uri':
166  $linkService = GeneralUtility::makeInstance(LinkService::class);
167  $urlParams = $linkService->resolve($routeConfig['source']);
168  if ($urlParams['type'] === 'url' || $urlParams['type'] === 'page') {
169  $uri = $urlParams['url'] ?? $this->‪getPageUri($request, $site, $urlParams);
170  [$content, $contentType] = $this->‪getFromUri($uri);
171  } elseif ($urlParams['type'] === 'file') {
172  [$content, $contentType] = $this->‪getFromFile($urlParams['file']);
173  } else {
174  throw new \InvalidArgumentException('Can only handle URIs of type page, url or file.', 1537348076);
175  }
176 
177  break;
178  default:
179  throw new \InvalidArgumentException(
180  'Can only handle static file configurations with type uri or staticText.',
181  1537348083
182  );
183  }
184  return [$content, $contentType];
185  }
186 }
‪TYPO3\CMS\Core\Routing\RouterInterface
Definition: RouterInterface.php:27
‪TYPO3\CMS\Frontend\Middleware\StaticRouteResolver
Definition: StaticRouteResolver.php:37
‪TYPO3\CMS\Core\Resource\AbstractFile\getMimeType
‪string getMimeType()
Definition: AbstractFile.php:266
‪TYPO3\CMS\Core\Site\Entity\Site\getRouter
‪RouterInterface getRouter(Context $context=null)
Definition: Site.php:364
‪TYPO3\CMS\Frontend\Middleware\StaticRouteResolver\process
‪ResponseInterface process(ServerRequestInterface $request, RequestHandlerInterface $handler)
Definition: StaticRouteResolver.php:45
‪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:100
‪TYPO3\CMS\Core\Site\Entity\Site
Definition: Site.php:39
‪TYPO3\CMS\Frontend\Middleware\StaticRouteResolver\getApplicableStaticRoute
‪array null getApplicableStaticRoute(array $staticRouteConfiguration, Site $site, string $uriPath)
Definition: StaticRouteResolver.php:75
‪TYPO3\CMS\Core\Http\Response
Definition: Response.php:28
‪TYPO3\CMS\Frontend\Middleware\StaticRouteResolver\getPageUri
‪string getPageUri(ServerRequestInterface $request, Site $site, array $urlParams)
Definition: StaticRouteResolver.php:132
‪TYPO3\CMS\Frontend\Middleware
Definition: BackendUserAuthenticator.php:4
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:23
‪TYPO3\CMS\Core\Routing\InvalidRouteArgumentsException
Definition: InvalidRouteArgumentsException.php:23
‪TYPO3\CMS\Frontend\Middleware\StaticRouteResolver\getFromUri
‪array getFromUri(string $uri)
Definition: StaticRouteResolver.php:111
‪TYPO3\CMS\Core\Resource\File\getContents
‪string getContents()
Definition: File.php:130
‪TYPO3\CMS\Core\Http\RequestFactory
Definition: RequestFactory.php:27
‪TYPO3\CMS\Core\Site\Entity\Site\getBase
‪UriInterface getBase()
Definition: Site.php:186
‪TYPO3\CMS\Core\Routing\RouterInterface\ABSOLUTE_URL
‪const ABSOLUTE_URL
Definition: RouterInterface.php:31
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Http\HtmlResponse
Definition: HtmlResponse.php:25
‪TYPO3\CMS\Frontend\Middleware\StaticRouteResolver\resolveByType
‪array resolveByType(ServerRequestInterface $request, Site $site, string $type, array $routeConfig)
Definition: StaticRouteResolver.php:158