TYPO3  7.6
backend/Classes/Routing/UriBuilder.php
Go to the documentation of this file.
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 
22 
32 {
36  const ABSOLUTE_URL = 'url';
37 
41  const ABSOLUTE_PATH = 'absolute';
42 
46  protected $routes;
47 
51  protected function loadBackendRoutes()
52  {
53  $router = GeneralUtility::makeInstance(Router::class);
54  $this->routes = $router->getRoutes();
55  }
56 
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 
107  return $this->buildUri($parameters, $referenceType);
108  }
109 
119  public function buildUriFromModule($moduleName, $parameters = array(), $referenceType = self::ABSOLUTE_PATH)
120  {
121  $parameters = array(
122  'M' => $moduleName,
123  'moduleToken' => FormProtectionFactory::get()->generateToken('moduleCall', $moduleName)
124  ) + $parameters;
125  return $this->buildUri($parameters, $referenceType);
126  }
127 
141  public function buildUriFromAjaxId($ajaxIdentifier, $parameters = array(), $referenceType = self::ABSOLUTE_PATH)
142  {
143  $parameters = array(
144  'ajaxID' => $ajaxIdentifier
145  ) + $parameters;
146  if (!empty($GLOBALS['TYPO3_CONF_VARS']['BE']['AJAX'][$ajaxIdentifier]['csrfTokenCheck'])) {
147  $parameters['ajaxToken'] = FormProtectionFactory::get()->generateToken('ajaxCall', $ajaxIdentifier);
148  }
149  return $this->buildUri($parameters, $referenceType);
150  }
151 
160  protected function buildUri($parameters, $referenceType)
161  {
162  $uri = 'index.php?' . ltrim(GeneralUtility::implodeArrayForUrl('', $parameters, '', true, true), '&');
163  if ($referenceType === self::ABSOLUTE_PATH) {
164  $uri = PathUtility::getAbsoluteWebPath(PATH_typo3 . $uri);
165  } else {
166  $uri = GeneralUtility::getIndpEnv('TYPO3_REQUEST_DIR') . $uri;
167  }
168  return GeneralUtility::makeInstance(Uri::class, $uri);
169  }
170 }