‪TYPO3CMS  11.5
RouteRedirect.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\ServerRequestInterface;
25 
30 {
34  private string ‪$name;
35 
40  private array ‪$parameters;
41 
42  public static function ‪create(string ‪$name, $params): self
43  {
44  if (is_string($params)) {
45  parse_str($params, $parsedParameters);
46  $params = $parsedParameters;
47  } elseif (!is_array($params)) {
48  throw new \LogicException('Params must be array or string', 1627907107);
49  }
50  return new self(‪$name, $params);
51  }
52 
53  public static function ‪createFromRoute(‪Route $route, array ‪$parameters): self
54  {
55  return new self($route->‪getOption('_identifier'), ‪$parameters);
56  }
57 
58  public static function ‪createFromRequest(ServerRequestInterface $request): ?self
59  {
60  ‪$name = $request->getQueryParams()['redirect'] ?? null;
61  if (empty(‪$name)) {
62  return null;
63  }
64  return ‪self::create(‪$name, $request->getQueryParams()['redirectParams'] ?? []);
65  }
66 
67  private function ‪__construct(string ‪$name, array $params)
68  {
69  $this->name = ‪$name;
70  $this->parameters = $this->‪sanitizeParameters($params);
71  }
72 
73  private function ‪sanitizeParameters(array $redirectParameters): array
74  {
75  unset($redirectParameters['token']);
76  unset($redirectParameters['route']);
77  unset($redirectParameters['redirect']);
78  unset($redirectParameters['redirectParams']);
79  return $redirectParameters;
80  }
81 
82  public function ‪getName(): string
83  {
84  return ‪$this->name;
85  }
86 
87  public function ‪getParameters(): array
88  {
89  return ‪$this->parameters;
90  }
91 
92  public function ‪getFormattedParameters(): string
93  {
94  $redirectParameters = http_build_query($this->parameters, '', '&', PHP_QUERY_RFC3986);
95  return ltrim($redirectParameters, '&?');
96  }
97 
98  public function ‪hasParameters(): bool
99  {
100  return !empty($this->parameters);
101  }
102 
111  public function ‪resolve(‪Router $router): void
112  {
113  $route = $router->‪getRouteCollection()->get($this->name);
114  if ($route === null) {
115  throw new ‪RouteNotFoundException(
116  sprintf('Route "%s" was not found', $this->name),
117  1627907587
118  );
119  }
120  if ($route->getOption('ajax')) {
122  sprintf('AJAX route "%s" cannot be redirected', $this->name),
123  1627407451
124  );
125  }
126  // Links to modules are always allowed, no problem here
127  // Check for the AJAX option should be handled before
128  if ($route->getOption('module')) {
129  return;
130  }
131  if ($route->getMethods() !== [] && !in_array('GET', $route->getMethods(), true)) {
133  sprintf('"%s" cannot be redirected as it does not allow GET methods', $this->name),
134  1627407452
135  );
136  }
137  $settings = $route->getOption('redirect');
138  if (($settings['enable'] ?? false) !== true) {
139  throw new ‪RouteNotFoundException(
140  sprintf('Route "%s" cannot be redirected', $this->name),
141  1627407511
142  );
143  }
144  // Only use allowed arguments, if set, otherwise no parameters are allowed
145  if (!empty($settings['parameters'])) {
146  $this->parameters = ‪ArrayUtility::intersectRecursive($this->parameters, (array)$settings['parameters']);
147  } else {
148  $this->parameters = [];
149  }
150  }
151 }
‪TYPO3\CMS\Backend\Routing\RouteRedirect\$parameters
‪array $parameters
Definition: RouteRedirect.php:40
‪TYPO3\CMS\Backend\Routing\RouteRedirect\getName
‪getName()
Definition: RouteRedirect.php:82
‪TYPO3\CMS\Backend\Routing\RouteRedirect\create
‪static create(string $name, $params)
Definition: RouteRedirect.php:42
‪TYPO3\CMS\Backend\Routing\RouteRedirect\sanitizeParameters
‪sanitizeParameters(array $redirectParameters)
Definition: RouteRedirect.php:73
‪TYPO3\CMS\Backend\Routing\Route\getOption
‪mixed getOption($name)
Definition: Route.php:143
‪TYPO3\CMS\Backend\Routing\RouteRedirect\__construct
‪__construct(string $name, array $params)
Definition: RouteRedirect.php:67
‪TYPO3\CMS\Backend\Routing\RouteRedirect\hasParameters
‪hasParameters()
Definition: RouteRedirect.php:98
‪TYPO3\CMS\Backend\Routing\RouteRedirect\$name
‪string $name
Definition: RouteRedirect.php:34
‪TYPO3\CMS\Backend\Routing\RouteRedirect\getFormattedParameters
‪getFormattedParameters()
Definition: RouteRedirect.php:92
‪TYPO3\CMS\Backend\Routing\RouteRedirect\getParameters
‪getParameters()
Definition: RouteRedirect.php:87
‪TYPO3\CMS\Backend\Routing\Exception\MethodNotAllowedException
Definition: MethodNotAllowedException.php:23
‪TYPO3\CMS\Backend\Routing\Router\getRouteCollection
‪SymfonyRouteCollection getRouteCollection()
Definition: Router.php:76
‪TYPO3\CMS\Backend\Routing\Route
Definition: Route.php:24
‪TYPO3\CMS\Backend\Routing\RouteRedirect\resolve
‪resolve(Router $router)
Definition: RouteRedirect.php:111
‪TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException
Definition: RouteNotFoundException.php:21
‪TYPO3\CMS\Backend\Routing\RouteRedirect
Definition: RouteRedirect.php:30
‪TYPO3\CMS\Core\Utility\ArrayUtility\intersectRecursive
‪static array intersectRecursive(array $source, array $mask=[])
Definition: ArrayUtility.php:569
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:24
‪TYPO3\CMS\Backend\Routing\RouteRedirect\createFromRoute
‪static createFromRoute(Route $route, array $parameters)
Definition: RouteRedirect.php:53
‪TYPO3\CMS\Backend\Routing\Exception\RouteTypeNotAllowedException
Definition: RouteTypeNotAllowedException.php:25
‪TYPO3\CMS\Backend\Routing\RouteRedirect\createFromRequest
‪static createFromRequest(ServerRequestInterface $request)
Definition: RouteRedirect.php:58
‪TYPO3\CMS\Backend\Routing\Router
Definition: Router.php:39
‪TYPO3\CMS\Backend\Routing