[!!!][FEATURE] Introduce Backend Routing
[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 array
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 $router = GeneralUtility::makeInstance(Router::class);
53 $this->routes = $router->getRoutes();
54 }
55
56 /**
57 * Generates a URL or path for a specific route based on the given parameters.
58 * When the route is configured with "access=public" then the token generation is left out.
59 *
60 * If there is no route with the given name, the generator throws the RouteNotFoundException.
61 *
62 * @param string $name The name of the route
63 * @param array $parameters An array of parameters
64 * @param string $referenceType The type of reference to be generated (one of the constants)
65 * @return Uri The generated Uri
66 * @throws RouteNotFoundException If the named route doesn't exist
67 */
68 public function buildUriFromRoute($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH) {
69 $this->loadBackendRoutes();
70 if (!isset($this->routes[$name])) {
71 throw new RouteNotFoundException('Unable to generate a URL for the named route "' . $name . '" because this route was not found.');
72 }
73
74 $route = $this->routes[$name];
75
76 // If the route has the "public" option set, no token is generated.
77 if ($route->getOption('access') !== 'public') {
78 $parameters = array(
79 'token' => FormProtectionFactory::get()->generateToken('route', $name)
80 ) + $parameters;
81 }
82
83 // Add the Route path as &route=XYZ
84 $parameters = array(
85 'route' => rawurlencode($route->getPath())
86 ) + $parameters;
87
88 return $this->buildUri($parameters, $referenceType);
89 }
90
91 /**
92 * Generate a URI for a backend module, does not check if a module is available though
93 *
94 * @param string $moduleName The name of the module
95 * @param array $parameters An array of parameters
96 * @param string $referenceType The type of reference to be generated (one of the constants)
97 *
98 * @return Uri The generated Uri
99 */
100 public function buildUriFromModule($moduleName, $parameters = array(), $referenceType = self::ABSOLUTE_PATH) {
101 $parameters = array(
102 'M' => $moduleName,
103 'moduleToken' => FormProtectionFactory::get()->generateToken('moduleCall', $moduleName)
104 ) + $parameters;
105 return $this->buildUri($parameters, $referenceType);
106 }
107
108 /**
109 * Returns the Ajax URL for a given AjaxID including a CSRF token.
110 *
111 * This method is only called by the core and must not be used by extensions.
112 * Ajax URLs of all registered backend Ajax handlers are automatically published
113 * to JavaScript inline settings: TYPO3.settings.ajaxUrls['ajaxId']
114 *
115 * @param string $ajaxIdentifier the ajaxID (used as GET parameter)
116 * @param array $parameters An array of parameters
117 * @param string $referenceType The type of reference to be generated (one of the constants)
118 *
119 * @return Uri The generated Uri
120 */
121 public function buildUriFromAjaxId($ajaxIdentifier, $parameters = array(), $referenceType = self::ABSOLUTE_PATH) {
122 $parameters = array(
123 'ajaxID' => $ajaxIdentifier
124 ) + $parameters;
125 if (!empty($GLOBALS['TYPO3_CONF_VARS']['BE']['AJAX'][$ajaxIdentifier]['csrfTokenCheck'])) {
126 $parameters['ajaxToken'] = FormProtectionFactory::get()->generateToken('ajaxCall', $ajaxIdentifier);
127 }
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 $uri = 'index.php?' . ltrim(GeneralUtility::implodeArrayForUrl('', $parameters, '', TRUE, TRUE), '&');
141 if ($referenceType === self::ABSOLUTE_PATH) {
142 $uri = PathUtility::getAbsoluteWebPath(PATH_typo3 . $uri);
143 } else {
144 $uri = GeneralUtility::getIndpEnv('TYPO3_REQUEST_DIR') . $uri;
145 }
146 return GeneralUtility::makeInstance(Uri::class, $uri);
147 }
148 }