[TASK] Removes extra empty lines
[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 = array(), $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.');
74 }
75
76 $route = $this->routes[$name];
77
78 // The Route is an AJAX route, so the parameters are different in order
79 // for the AjaxRequestHandler to be triggered
80 if ($route->getOption('ajax')) {
81 // If the route has the "public" option set, no token is generated.
82 if ($route->getOption('access') !== 'public') {
83 $parameters = array(
84 'ajaxToken' => FormProtectionFactory::get()->generateToken('ajaxCall', $name)
85 ) + $parameters;
86 }
87
88 // Add the Route path as &ajaxID=XYZ
89 $parameters = array(
90 'ajaxID' => $route->getPath()
91 ) + $parameters;
92 } else {
93 // If the route has the "public" option set, no token is generated.
94 if ($route->getOption('access') !== 'public') {
95 $parameters = array(
96 'token' => FormProtectionFactory::get()->generateToken('route', $name)
97 ) + $parameters;
98 }
99
100 // Add the Route path as &route=XYZ
101 $parameters = array(
102 'route' => $route->getPath()
103 ) + $parameters;
104 }
105
106 return $this->buildUri($parameters, $referenceType);
107 }
108
109 /**
110 * Generate a URI for a backend module, does not check if a module is available though
111 *
112 * @param string $moduleName The name of the module
113 * @param array $parameters An array of parameters
114 * @param string $referenceType The type of reference to be generated (one of the constants)
115 *
116 * @return Uri The generated Uri
117 */
118 public function buildUriFromModule($moduleName, $parameters = array(), $referenceType = self::ABSOLUTE_PATH)
119 {
120 $parameters = array(
121 'M' => $moduleName,
122 'moduleToken' => FormProtectionFactory::get()->generateToken('moduleCall', $moduleName)
123 ) + $parameters;
124 return $this->buildUri($parameters, $referenceType);
125 }
126
127 /**
128 * Returns the Ajax URL for a given AjaxID including a CSRF token.
129 *
130 * This method is only called by the core and must not be used by extensions.
131 * Ajax URLs of all registered backend Ajax handlers are automatically published
132 * to JavaScript inline settings: TYPO3.settings.ajaxUrls['ajaxId']
133 *
134 * @param string $ajaxIdentifier the ajaxID (used as GET parameter)
135 * @param array $parameters An array of parameters
136 * @param string $referenceType The type of reference to be generated (one of the constants)
137 *
138 * @return Uri The generated Uri
139 */
140 public function buildUriFromAjaxId($ajaxIdentifier, $parameters = array(), $referenceType = self::ABSOLUTE_PATH)
141 {
142 $parameters = array(
143 'ajaxID' => $ajaxIdentifier
144 ) + $parameters;
145 if (!empty($GLOBALS['TYPO3_CONF_VARS']['BE']['AJAX'][$ajaxIdentifier]['csrfTokenCheck'])) {
146 $parameters['ajaxToken'] = FormProtectionFactory::get()->generateToken('ajaxCall', $ajaxIdentifier);
147 }
148 return $this->buildUri($parameters, $referenceType);
149 }
150
151 /**
152 * Internal method building a Uri object, merging the GET parameters array into a flat queryString
153 *
154 * @param array $parameters An array of GET parameters
155 * @param string $referenceType The type of reference to be generated (one of the constants)
156 *
157 * @return Uri
158 */
159 protected function buildUri($parameters, $referenceType)
160 {
161 $uri = 'index.php?' . ltrim(GeneralUtility::implodeArrayForUrl('', $parameters, '', true, true), '&');
162 if ($referenceType === self::ABSOLUTE_PATH) {
163 $uri = PathUtility::getAbsoluteWebPath(PATH_typo3 . $uri);
164 } else {
165 $uri = GeneralUtility::getIndpEnv('TYPO3_REQUEST_DIR') . $uri;
166 }
167 return GeneralUtility::makeInstance(Uri::class, $uri);
168 }
169 }