[BUGFIX] Always use backend form protection in backend URI builder
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Routing / Router.php
1 <?php
2 namespace TYPO3\CMS\Backend\Routing;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use Psr\Http\Message\ServerRequestInterface;
18 use TYPO3\CMS\Backend\Routing\Exception\ResourceNotFoundException;
19
20 /**
21 * Implementation of a class for adding routes, collecting throughout the Bootstrap
22 * to register all sorts of Backend Routes, and to fetch the main Collection in order
23 * to resolve a route (see ->match() and ->matchRequest()).
24 *
25 * Ideally, the Router is solely instantiated and accessed via the Bootstrap, the RequestHandler and the UriBuilder.
26 *
27 * See \TYPO3\CMS\Backend\RequestHandler for more details on route matching() and Bootstrap->initializeBackendRouting().
28 *
29 * The architecture is inspired by the Symfony Routing Component.
30 */
31 class Router implements \TYPO3\CMS\Core\SingletonInterface
32 {
33 /**
34 * All routes used in the Backend
35 *
36 * @var Route[]
37 */
38 protected $routes = array();
39
40 /**
41 * Adds a new route with the identifiers
42 *
43 * @param string $routeIdentifier
44 * @param Route $route
45 */
46 public function addRoute($routeIdentifier, $route)
47 {
48 $this->routes[$routeIdentifier] = $route;
49 }
50
51 /**
52 * Fetch all registered routes, only use in UriBuilder
53 *
54 * @return Route[]
55 */
56 public function getRoutes()
57 {
58 return $this->routes;
59 }
60
61 /**
62 * Tries to match a URL path with a set of routes.
63 *
64 * @param string $pathInfo The path info to be parsed
65 * @return Route the first Route object found
66 * @throws ResourceNotFoundException If the resource could not be found
67 */
68 public function match($pathInfo)
69 {
70 foreach ($this->routes as $routeIdentifier => $route) {
71 // This check is done in a simple way as there are no parameters yet (get parameters only)
72 if ($route->getPath() === $pathInfo) {
73 // Store the name of the Route in the _identifier option so the token can be checked against that
74 $route->setOption('_identifier', $routeIdentifier);
75 return $route;
76 }
77 }
78 throw new ResourceNotFoundException('The requested resource "' . $pathInfo . '" was not found.', 1425389240);
79 }
80
81 /**
82 * Tries to match a URI against the registered routes
83 *
84 * @param ServerRequestInterface $request
85 * @return Route the first Route object found
86 */
87 public function matchRequest(ServerRequestInterface $request)
88 {
89 return $this->match($request->getAttribute('routePath'));
90 }
91 }