[FEATURE] Configurable Route Parameters
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Routing / UriBuilder.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 TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException;
18 use TYPO3\CMS\Core\FormProtection\FormProtectionFactory;
19 use TYPO3\CMS\Core\Http\Uri;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use TYPO3\CMS\Core\Utility\PathUtility;
22
23 /**
24 * Main UrlGenerator for creating URLs for the Backend. Generates a URL based on
25 * an identifier defined by Configuration/Backend/Routes.php of an extension,
26 * and adds some more parameters to the URL.
27 *
28 * Currently only available and useful when called from Router->generate() as the information
29 * about possible routes needs to be handed over.
30 */
31 class UriBuilder
32 {
33 /**
34 * Generates an absolute URL
35 */
36 const ABSOLUTE_URL = 'url';
37
38 /**
39 * Generates an absolute path
40 */
41 const ABSOLUTE_PATH = 'absolute';
42
43 /**
44 * @var Route[]
45 */
46 protected $routes;
47
48 /**
49 * Fetches the available routes from the Router to be used for generating routes
50 */
51 protected function loadBackendRoutes()
52 {
53 $router = GeneralUtility::makeInstance(Router::class);
54 $this->routes = $router->getRoutes();
55 }
56
57 /**
58 * Generates a URL or path for a specific route based on the given parameters.
59 * When the route is configured with "access=public" then the token generation is left out.
60 *
61 * If there is no route with the given name, the generator throws the RouteNotFoundException.
62 *
63 * @param string $name The name of the route
64 * @param array $parameters An array of parameters
65 * @param string $referenceType The type of reference to be generated (one of the constants)
66 * @return Uri The generated Uri
67 * @throws RouteNotFoundException If the named route doesn't exist
68 */
69 public function buildUriFromRoute($name, $parameters = [], $referenceType = self::ABSOLUTE_PATH)
70 {
71 $this->loadBackendRoutes();
72 if (!isset($this->routes[$name])) {
73 throw new RouteNotFoundException('Unable to generate a URL for the named route "' . $name . '" because this route was not found.', 1476050190);
74 }
75
76 $route = $this->routes[$name];
77 $parameters = array_merge(
78 $route->getOptions()['parameters'] ?? [],
79 $parameters
80 );
81
82 // The Route is an AJAX route, so the parameters are different in order
83 // for the AjaxRequestHandler to be triggered
84 if ($route->getOption('ajax')) {
85 // If the route has the "public" option set, no token is generated.
86 if ($route->getOption('access') !== 'public') {
87 $parameters = [
88 'ajaxToken' => FormProtectionFactory::get('backend')->generateToken('ajaxCall', $name)
89 ] + $parameters;
90 }
91
92 // Add the Route path as &ajaxID=XYZ
93 $parameters = [
94 'ajaxID' => $route->getPath()
95 ] + $parameters;
96 } else {
97 // If the route has the "public" option set, no token is generated.
98 if ($route->getOption('access') !== 'public') {
99 $parameters = [
100 'token' => FormProtectionFactory::get('backend')->generateToken('route', $name)
101 ] + $parameters;
102 }
103
104 // Add the Route path as &route=XYZ
105 $parameters = [
106 'route' => $route->getPath()
107 ] + $parameters;
108 }
109
110 return $this->buildUri($parameters, $referenceType);
111 }
112
113 /**
114 * Generate a URI for a backend module, does not check if a module is available though
115 *
116 * @param string $moduleName The name of the module
117 * @param array $parameters An array of parameters
118 * @param string $referenceType The type of reference to be generated (one of the constants)
119 *
120 * @return Uri The generated Uri
121 */
122 public function buildUriFromModule($moduleName, $parameters = [], $referenceType = self::ABSOLUTE_PATH)
123 {
124 $parameters = [
125 'M' => $moduleName,
126 'moduleToken' => FormProtectionFactory::get('backend')->generateToken('moduleCall', $moduleName)
127 ] + $parameters;
128 return $this->buildUri($parameters, $referenceType);
129 }
130
131 /**
132 * Internal method building a Uri object, merging the GET parameters array into a flat queryString
133 *
134 * @param array $parameters An array of GET parameters
135 * @param string $referenceType The type of reference to be generated (one of the constants)
136 *
137 * @return Uri
138 */
139 protected function buildUri($parameters, $referenceType)
140 {
141 $uri = 'index.php?' . ltrim(GeneralUtility::implodeArrayForUrl('', $parameters, '', false, true), '&');
142 if ($referenceType === self::ABSOLUTE_PATH) {
143 $uri = PathUtility::getAbsoluteWebPath(PATH_typo3 . $uri);
144 } else {
145 $uri = GeneralUtility::getIndpEnv('TYPO3_REQUEST_DIR') . $uri;
146 }
147 return GeneralUtility::makeInstance(Uri::class, $uri);
148 }
149 }