[TASK] Use &route=/ajax/ instead of &ajaxId for BE AJAX calls
[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 // If the route has the "public" option set, no token is generated.
83 if ($route->getOption('access') !== 'public') {
84 $parameters = [
85 'token' => FormProtectionFactory::get('backend')->generateToken('route', $name)
86 ] + $parameters;
87 }
88
89 // Add the Route path as &route=XYZ
90 $parameters = [
91 'route' => $route->getPath()
92 ] + $parameters;
93
94 return $this->buildUri($parameters, $referenceType);
95 }
96
97 /**
98 * Generate a URI for a backend module, does not check if a module is available though
99 *
100 * @param string $moduleName The name of the module
101 * @param array $parameters An array of parameters
102 * @param string $referenceType The type of reference to be generated (one of the constants)
103 *
104 * @return Uri The generated Uri
105 */
106 public function buildUriFromModule($moduleName, $parameters = [], $referenceType = self::ABSOLUTE_PATH)
107 {
108 $parameters = [
109 'M' => $moduleName,
110 'moduleToken' => FormProtectionFactory::get('backend')->generateToken('moduleCall', $moduleName)
111 ] + $parameters;
112 return $this->buildUri($parameters, $referenceType);
113 }
114
115 /**
116 * Internal method building a Uri object, merging the GET parameters array into a flat queryString
117 *
118 * @param array $parameters An array of GET parameters
119 * @param string $referenceType The type of reference to be generated (one of the constants)
120 *
121 * @return Uri
122 */
123 protected function buildUri($parameters, $referenceType)
124 {
125 $uri = 'index.php?' . ltrim(GeneralUtility::implodeArrayForUrl('', $parameters, '', false, true), '&');
126 if ($referenceType === self::ABSOLUTE_PATH) {
127 $uri = PathUtility::getAbsoluteWebPath(PATH_typo3 . $uri);
128 } else {
129 $uri = GeneralUtility::getIndpEnv('TYPO3_REQUEST_DIR') . $uri;
130 }
131 return GeneralUtility::makeInstance(Uri::class, $uri);
132 }
133 }