92dfd6e5ead8d32202cef1304e98e0907aa47d02
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Routing / PageRouter.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Core\Routing;
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 Doctrine\DBAL\Connection;
20 use Psr\Http\Message\ServerRequestInterface;
21 use Psr\Http\Message\UriInterface;
22 use Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
23 use Symfony\Component\Routing\Exception\ResourceNotFoundException;
24 use Symfony\Component\Routing\Generator\UrlGenerator;
25 use Symfony\Component\Routing\RequestContext;
26 use TYPO3\CMS\Core\Context\Context;
27 use TYPO3\CMS\Core\Context\LanguageAspect;
28 use TYPO3\CMS\Core\Database\ConnectionPool;
29 use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
30 use TYPO3\CMS\Core\Database\Query\Restriction\FrontendWorkspaceRestriction;
31 use TYPO3\CMS\Core\Http\Uri;
32 use TYPO3\CMS\Core\Routing\Aspect\AspectFactory;
33 use TYPO3\CMS\Core\Routing\Aspect\MappableProcessor;
34 use TYPO3\CMS\Core\Routing\Aspect\StaticMappableAspectInterface;
35 use TYPO3\CMS\Core\Routing\Enhancer\DecoratingEnhancerInterface;
36 use TYPO3\CMS\Core\Routing\Enhancer\EnhancerFactory;
37 use TYPO3\CMS\Core\Routing\Enhancer\EnhancerInterface;
38 use TYPO3\CMS\Core\Routing\Enhancer\ResultingInterface;
39 use TYPO3\CMS\Core\Routing\Enhancer\RoutingEnhancerInterface;
40 use TYPO3\CMS\Core\Site\Entity\Site;
41 use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
42 use TYPO3\CMS\Core\Utility\GeneralUtility;
43 use TYPO3\CMS\Frontend\Page\CacheHashCalculator;
44 use TYPO3\CMS\Frontend\Page\PageRepository;
45
46 /**
47 * Page Router - responsible for a page based on a request, by looking up the slug of the page path.
48 * Is also used for generating URLs for pages.
49 *
50 * Resolving is done via the "Route Candidate" pattern.
51 *
52 * Example:
53 * - /about-us/team/management/
54 *
55 * will look for all pages that have
56 * - /about-us
57 * - /about-us/
58 * - /about-us/team
59 * - /about-us/team/
60 * - /about-us/team/management
61 * - /about-us/team/management/
62 *
63 * And create route candidates for that.
64 *
65 * Please note: PageRouter does not restrict the HTTP method or is bound to any domain constraints,
66 * as the SiteMatcher has done that already.
67 *
68 * The concept of the PageRouter is to *resolve*, and to *generate* URIs. On top, it is a facade to hide the
69 * dependency to symfony and to not expose its logic.
70 */
71 class PageRouter implements RouterInterface
72 {
73 /**
74 * @var Site
75 */
76 protected $site;
77
78 /**
79 * @var EnhancerFactory
80 */
81 protected $enhancerFactory;
82
83 /**
84 * @var AspectFactory
85 */
86 protected $aspectFactory;
87
88 /**
89 * @var CacheHashCalculator
90 */
91 protected $cacheHashCalculator;
92
93 /**
94 * A page router is always bound to a specific site.
95 * @param Site $site
96 */
97 public function __construct(Site $site)
98 {
99 $this->site = $site;
100 $this->enhancerFactory = GeneralUtility::makeInstance(EnhancerFactory::class);
101 $this->aspectFactory = GeneralUtility::makeInstance(AspectFactory::class);
102 $this->cacheHashCalculator = GeneralUtility::makeInstance(CacheHashCalculator::class);
103 }
104
105 /**
106 * Finds a RouteResult based on the given request.
107 *
108 * @param ServerRequestInterface $request
109 * @param RouteResultInterface|SiteRouteResult|null $previousResult
110 * @return SiteRouteResult
111 * @throws RouteNotFoundException
112 */
113 public function matchRequest(ServerRequestInterface $request, RouteResultInterface $previousResult = null): RouteResultInterface
114 {
115 $urlPath = $previousResult->getTail();
116 $slugCandidates = $this->getCandidateSlugsFromRoutePath($urlPath ?: '/');
117 $language = $previousResult->getLanguage();
118 $pageCandidates = $this->getPagesFromDatabaseForCandidates($slugCandidates, $language->getLanguageId());
119 // Stop if there are no candidates
120 if (empty($pageCandidates)) {
121 throw new RouteNotFoundException('No page candidates found for path "' . $urlPath . '"', 1538389999);
122 }
123
124 $decoratedParameters = [];
125 $fullCollection = new RouteCollection();
126 foreach ($pageCandidates ?? [] as $page) {
127 $pageIdForDefaultLanguage = (int)($page['l10n_parent'] ?: $page['uid']);
128 $pagePath = $page['slug'];
129 $pageCollection = new RouteCollection();
130 $defaultRouteForPage = new Route(
131 $pagePath,
132 [],
133 [],
134 ['utf8' => true, '_page' => $page]
135 );
136 $pageCollection->add('default', $defaultRouteForPage);
137 $enhancers = $this->getEnhancersForPage($pageIdForDefaultLanguage, $language);
138 foreach ($enhancers as $enhancer) {
139 if ($enhancer instanceof DecoratingEnhancerInterface) {
140 $enhancer->decorateForMatching($pageCollection, $decoratedParameters, $urlPath);
141 }
142 }
143 foreach ($enhancers as $enhancer) {
144 if ($enhancer instanceof RoutingEnhancerInterface) {
145 $enhancer->enhanceForMatching($pageCollection);
146 }
147 }
148
149 $pageCollection->addNamePrefix('page_' . $page['uid'] . '_');
150 $fullCollection->addCollection($pageCollection);
151 }
152
153 $matcher = new PageUriMatcher($fullCollection);
154 try {
155 $result = $matcher->match('/' . trim($urlPath, '/'));
156 /** @var Route $matchedRoute */
157 $matchedRoute = $fullCollection->get($result['_route']);
158 $matchedRoute->setOption('_decoratedParameters', $decoratedParameters);
159 return $this->buildPageArguments($matchedRoute, $result, $request->getQueryParams());
160 } catch (ResourceNotFoundException $e) {
161 // Do nothing
162 }
163 throw new RouteNotFoundException('No route found for path "' . $urlPath . '"', 1538389998);
164 }
165
166 /**
167 * API for generating a page where the $route parameter is typically an array (page record) or the page ID
168 *
169 * @param array|string $route
170 * @param array $parameters an array of query parameters which can be built into the URI path, also consider the special handling of "_language"
171 * @param string $fragment additional #my-fragment part
172 * @param string $type see the RouterInterface for possible types
173 * @return UriInterface
174 */
175 public function generateUri($route, array $parameters = [], string $fragment = '', string $type = ''): UriInterface
176 {
177 // Resolve language
178 $language = null;
179 $languageOption = $parameters['_language'] ?? null;
180 unset($parameters['_language']);
181 if ($languageOption instanceof SiteLanguage) {
182 $language = $languageOption;
183 } elseif ($languageOption !== null) {
184 $language = $this->site->getLanguageById((int)$languageOption);
185 }
186 if ($language === null) {
187 $language = $this->site->getDefaultLanguage();
188 }
189
190 $pageId = 0;
191 if (is_array($route)) {
192 $pageId = (int)$route['uid'];
193 } elseif (is_scalar($route)) {
194 $pageId = (int)$route;
195 }
196
197 $context = clone GeneralUtility::makeInstance(Context::class);
198 $context->setAspect('language', new LanguageAspect($language->getLanguageId()));
199 $pageRepository = GeneralUtility::makeInstance(PageRepository::class, $context);
200 $page = $pageRepository->getPage($pageId, true);
201 $pagePath = ltrim($page['slug'] ?? '', '/');
202 $originalParameters = $parameters;
203 $collection = new RouteCollection();
204 $defaultRouteForPage = new Route(
205 '/' . $pagePath,
206 [],
207 [],
208 ['utf8' => true, '_page' => $page]
209 );
210 $collection->add('default', $defaultRouteForPage);
211
212 // cHash is never considered because cHash is built by this very method.
213 unset($originalParameters['cHash']);
214 $enhancers = $this->getEnhancersForPage($pageId, $language);
215 foreach ($enhancers as $enhancer) {
216 if ($enhancer instanceof RoutingEnhancerInterface) {
217 $enhancer->enhanceForGeneration($collection, $originalParameters);
218 }
219 }
220 foreach ($enhancers as $enhancer) {
221 if ($enhancer instanceof DecoratingEnhancerInterface) {
222 $enhancer->decorateForGeneration($collection, $originalParameters);
223 }
224 }
225
226 $scheme = $language->getBase()->getScheme();
227 $mappableProcessor = new MappableProcessor();
228 $context = new RequestContext(
229 // page segment (slug & enhanced part) is supposed to start with '/'
230 rtrim($language->getBase()->getPath(), '/'),
231 'GET',
232 $language->getBase()->getHost(),
233 $scheme ?: 'http',
234 $scheme === 'http' ? $language->getBase()->getPort() ?? 80 : 80,
235 $scheme === 'https' ? $language->getBase()->getPort() ?? 443 : 443
236 );
237 $generator = new UrlGenerator($collection, $context);
238 $allRoutes = $collection->all();
239 $allRoutes = array_reverse($allRoutes, true);
240 $matchedRoute = null;
241 $pageRouteResult = null;
242 $uri = null;
243 // map our reference type to symfony's custom paths
244 $referenceType = $type === static::ABSOLUTE_PATH ? UrlGenerator::ABSOLUTE_PATH : UrlGenerator::ABSOLUTE_URL;
245 /**
246 * @var string $routeName
247 * @var Route $route
248 */
249 foreach ($allRoutes as $routeName => $route) {
250 try {
251 $parameters = $originalParameters;
252 if ($route->hasOption('deflatedParameters')) {
253 $parameters = $route->getOption('deflatedParameters');
254 }
255 $mappableProcessor->generate($route, $parameters);
256 // ABSOLUTE_URL is used as default fallback
257 $urlAsString = $generator->generate($routeName, $parameters, $referenceType);
258 $uri = new Uri($urlAsString);
259 /** @var Route $matchedRoute */
260 $matchedRoute = $collection->get($routeName);
261 parse_str($uri->getQuery() ?? '', $remainingQueryParameters);
262 $pageRouteResult = $this->buildPageArguments($route, $parameters, $remainingQueryParameters);
263 break;
264 } catch (MissingMandatoryParametersException $e) {
265 // no match
266 }
267 }
268
269 if (!$uri instanceof UriInterface) {
270 throw new InvalidRouteArgumentsException('Uri could not be built for page "' . $pageId . '"', 1538390230);
271 }
272
273 if ($pageRouteResult && $pageRouteResult->areDirty()) {
274 // for generating URLs this should(!) never happen
275 // if it does happen, generator logic has flaws
276 throw new InvalidRouteArgumentsException('Route arguments are dirty', 1537613247);
277 }
278
279 if ($matchedRoute && $pageRouteResult && !empty($pageRouteResult->getDynamicArguments())) {
280 $cacheHash = $this->generateCacheHash($pageId, $pageRouteResult);
281
282 if (!empty($cacheHash)) {
283 $queryArguments = $pageRouteResult->getQueryArguments();
284 $queryArguments['cHash'] = $cacheHash;
285 $uri = $uri->withQuery(http_build_query($queryArguments, '', '&', PHP_QUERY_RFC3986));
286 }
287 }
288 if ($fragment) {
289 $uri = $uri->withFragment($fragment);
290 }
291 return $uri;
292 }
293
294 /**
295 * Check for records in the database which matches one of the slug candidates.
296 *
297 * @param array $slugCandidates
298 * @param int $languageId
299 * @return array
300 */
301 protected function getPagesFromDatabaseForCandidates(array $slugCandidates, int $languageId): array
302 {
303 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
304 ->getQueryBuilderForTable('pages');
305 $queryBuilder
306 ->getRestrictions()
307 ->removeAll()
308 ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
309 ->add(GeneralUtility::makeInstance(FrontendWorkspaceRestriction::class));
310
311 $statement = $queryBuilder
312 ->select('uid', 'l10n_parent', 'pid', 'slug')
313 ->from('pages')
314 ->where(
315 $queryBuilder->expr()->eq(
316 'sys_language_uid',
317 $queryBuilder->createNamedParameter($languageId, \PDO::PARAM_INT)
318 ),
319 $queryBuilder->expr()->in(
320 'slug',
321 $queryBuilder->createNamedParameter(
322 $slugCandidates,
323 Connection::PARAM_STR_ARRAY
324 )
325 )
326 )
327 // Exact match will be first, that's important
328 ->orderBy('slug', 'desc')
329 ->execute();
330
331 $pages = [];
332 $siteMatcher = GeneralUtility::makeInstance(SiteMatcher::class);
333 while ($row = $statement->fetch()) {
334 $pageIdInDefaultLanguage = (int)($languageId > 0 ? $row['l10n_parent'] : $row['uid']);
335 if ($siteMatcher->matchByPageId($pageIdInDefaultLanguage)->getRootPageId() === $this->site->getRootPageId()) {
336 $pages[] = $row;
337 }
338 }
339 return $pages;
340 }
341
342 /**
343 * Fetch possible enhancers + aspects based on the current page configuration and the site configuration put
344 * into "routeEnhancers"
345 *
346 * @param int $pageId
347 * @param SiteLanguage $language
348 * @return EnhancerInterface[]
349 */
350 protected function getEnhancersForPage(int $pageId, SiteLanguage $language): array
351 {
352 $enhancers = [];
353 foreach ($this->site->getConfiguration()['routeEnhancers'] ?? [] as $enhancerConfiguration) {
354 // Check if there is a restriction to page Ids.
355 if (is_array($enhancerConfiguration['limitToPages'] ?? null) && !in_array($pageId, $enhancerConfiguration['limitToPages'])) {
356 continue;
357 }
358 $enhancerType = $enhancerConfiguration['type'] ?? '';
359 /** @var EnhancerInterface $enhancer */
360 $enhancer = $this->enhancerFactory->create($enhancerType, $enhancerConfiguration);
361 if (!empty($enhancerConfiguration['aspects'] ?? null)) {
362 $aspects = $this->aspectFactory->createAspects(
363 $enhancerConfiguration['aspects'],
364 $language
365 );
366 $enhancer->setAspects($aspects);
367 }
368 $enhancers[] = $enhancer;
369 }
370 return $enhancers;
371 }
372
373 /**
374 * Returns possible URL parts for a string like /home/about-us/offices/ or /home/about-us/offices.json
375 * to return.
376 *
377 * /home/about-us/offices/
378 * /home/about-us/offices.json
379 * /home/about-us/offices
380 * /home/about-us/
381 * /home/about-us
382 * /home/
383 * /home
384 *
385 * @param string $routePath
386 * @return array
387 */
388 protected function getCandidateSlugsFromRoutePath(string $routePath): array
389 {
390 $candidatePathParts = [];
391 $pathParts = GeneralUtility::trimExplode('/', $routePath, true);
392 if (empty($pathParts)) {
393 return ['/'];
394 }
395 // Check if the last part contains a ".", then split it
396 // @todo fix me based on enhancer configuration
397 $lastPart = array_pop($pathParts);
398 if (strpos($lastPart, '.') !== false) {
399 $pathParts = array_merge($pathParts, explode('.', $lastPart));
400 } else {
401 $pathParts[] = $lastPart;
402 }
403 while (!empty($pathParts)) {
404 $prefix = '/' . implode('/', $pathParts);
405 $candidatePathParts[] = $prefix . '/';
406 $candidatePathParts[] = $prefix;
407 array_pop($pathParts);
408 }
409 return $candidatePathParts;
410 }
411
412 /**
413 * @param int $pageId
414 * @param PageArguments $arguments
415 * @return string
416 */
417 protected function generateCacheHash(int $pageId, PageArguments $arguments): string
418 {
419 return $this->cacheHashCalculator->calculateCacheHash(
420 $this->getCacheHashParameters($pageId, $arguments)
421 );
422 }
423
424 /**
425 * @param int $pageId
426 * @param PageArguments $arguments
427 * @return array
428 */
429 protected function getCacheHashParameters(int $pageId, PageArguments $arguments): array
430 {
431 $hashParameters = $arguments->getDynamicArguments();
432 $hashParameters['id'] = $pageId;
433 $uri = http_build_query($hashParameters, '', '&', PHP_QUERY_RFC3986);
434 return $this->cacheHashCalculator->getRelevantParameters($uri);
435 }
436
437 /**
438 * Builds route arguments. The important part here is to distinguish between
439 * static and dynamic arguments. Per default all arguments are dynamic until
440 * aspects can be used to really consider them as static (= 1:1 mapping between
441 * route value and resulting arguments).
442 *
443 * Besides that, internal arguments (_route, _controller, _custom, ..) have
444 * to be separated since those values are not meant to be used for later
445 * processing. Not separating those values might result in invalid cHash.
446 *
447 * This method is used during resolving and generation of URLs.
448 *
449 * @param Route $route
450 * @param array $results
451 * @param array $remainingQueryParameters
452 * @return PageArguments
453 */
454 protected function buildPageArguments(Route $route, array $results, array $remainingQueryParameters = []): PageArguments
455 {
456 // only use parameters that actually have been processed
457 // (thus stripping internals like _route, _controller, ...)
458 $routeArguments = $this->filterProcessedParameters($route, $results);
459 // assert amount of "static" mappers is not too "dynamic"
460 $this->assertMaximumStaticMappableAmount($route, array_keys($routeArguments));
461 // delegate result handling to enhancer
462 $enhancer = $route->getEnhancer();
463 if ($enhancer instanceof ResultingInterface) {
464 // forward complete(!) results, not just filtered parameters
465 return $enhancer->buildResult($route, $results, $remainingQueryParameters);
466 }
467 $page = $route->getOption('_page');
468 $pageId = (int)($page['l10n_parent'] > 0 ? $page['l10n_parent'] : $page['uid']);
469 $type = $this->resolveType($route, $remainingQueryParameters);
470 return new PageArguments($pageId, $type, $routeArguments, [], $remainingQueryParameters);
471 }
472
473 /**
474 * Retrieves type from processed route and modifies remaining query parameters.
475 *
476 * @param Route $route
477 * @param array $remainingQueryParameters reference to remaining query parameters
478 * @return string
479 */
480 protected function resolveType(Route $route, array &$remainingQueryParameters): string
481 {
482 $decoratedParameters = $route->getOption('_decoratedParameters');
483 if (!isset($decoratedParameters['type'])) {
484 return '0';
485 }
486 $type = (string)$decoratedParameters['type'];
487 unset($decoratedParameters['type']);
488 $remainingQueryParameters = array_replace_recursive(
489 $remainingQueryParameters,
490 $decoratedParameters
491 );
492 return $type;
493 }
494
495 /**
496 * Asserts that possible amount of items in all static and countable mappers
497 * (such as StaticRangeMapper) is limited to 10000 in order to avoid
498 * brute-force scenarios and the risk of cache-flooding.
499 *
500 * @param Route $route
501 * @param array $variableNames
502 * @throws \OverflowException
503 */
504 protected function assertMaximumStaticMappableAmount(Route $route, array $variableNames = [])
505 {
506 $mappers = $route->filterAspects(
507 [StaticMappableAspectInterface::class, \Countable::class],
508 $variableNames
509 );
510 if (empty($mappers)) {
511 return;
512 }
513
514 $multipliers = array_map('count', $mappers);
515 $product = array_product($multipliers);
516 if ($product > 10000) {
517 throw new \OverflowException(
518 'Possible range of all mappers is larger than 10000 items',
519 1537696772
520 );
521 }
522 }
523
524 /**
525 * Determine parameters that have been processed.
526 *
527 * @param Route $route
528 * @param array $results
529 * @return array
530 */
531 protected function filterProcessedParameters(Route $route, $results): array
532 {
533 return array_intersect_key(
534 $results,
535 array_flip($route->compile()->getPathVariables())
536 );
537 }
538 }