‪TYPO3CMS  ‪main
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 
110  public function ‪resolve(‪Router $router): void
111  {
112  $route = $router->‪getRoute($this->name);
113  if ($route === null) {
114  throw new ‪RouteNotFoundException(
115  sprintf('Route "%s" was not found', $this->name),
116  1627907587
117  );
118  }
119  if ($route->getOption('ajax')) {
121  sprintf('AJAX route "%s" cannot be redirected', $this->name),
122  1627407451
123  );
124  }
125  // Links to modules are always allowed, no problem here
126  // Check for the AJAX option should be handled before
127  if ($route->getOption('module')) {
128  return;
129  }
130  if ($route->getMethods() !== [] && !in_array('GET', $route->getMethods(), true)) {
132  sprintf('"%s" cannot be redirected as it does not allow GET methods', $this->name),
133  1627407452
134  );
135  }
136  $settings = $route->getOption('redirect');
137  if (($settings['enable'] ?? false) !== true) {
138  throw new ‪RouteNotFoundException(
139  sprintf('Route "%s" cannot be redirected', $this->name),
140  1627407511
141  );
142  }
143  // Only use allowed arguments, if set, otherwise no parameters are allowed
144  if (!empty($settings['parameters'])) {
145  $this->parameters = ArrayUtility::intersectRecursive($this->parameters, (array)$settings['parameters']);
146  } else {
147  $this->parameters = [];
148  }
149  }
150 }
‪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:142
‪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\getRoute
‪SymfonyRoute Route null getRoute(string $routeName)
Definition: Router.php:92
‪TYPO3\CMS\Backend\Routing\Route
Definition: Route.php:24
‪TYPO3\CMS\Backend\Routing\RouteRedirect\resolve
‪resolve(Router $router)
Definition: RouteRedirect.php:110
‪TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException
Definition: RouteNotFoundException.php:21
‪TYPO3\CMS\Backend\Routing\RouteRedirect
Definition: RouteRedirect.php:30
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:26
‪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:40
‪TYPO3\CMS\Backend\Routing