TYPO3 CMS  TYPO3_7-6
UriBuilder.php
Go to the documentation of this file.
1 <?php
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 = [], $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 = [
84  'ajaxToken' => FormProtectionFactory::get('backend')->generateToken('ajaxCall', $name)
85  ] + $parameters;
86  }
87 
88  // Add the Route path as &ajaxID=XYZ
89  $parameters = [
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 = [
96  'token' => FormProtectionFactory::get('backend')->generateToken('route', $name)
97  ] + $parameters;
98  }
99 
100  // Add the Route path as &route=XYZ
101  $parameters = [
102  'route' => $route->getPath()
103  ] + $parameters;
104  }
105 
106  return $this->buildUri($parameters, $referenceType);
107  }
108 
118  public function buildUriFromModule($moduleName, $parameters = [], $referenceType = self::ABSOLUTE_PATH)
119  {
120  $parameters = [
121  'M' => $moduleName,
122  'moduleToken' => FormProtectionFactory::get('backend')->generateToken('moduleCall', $moduleName)
123  ] + $parameters;
124  return $this->buildUri($parameters, $referenceType);
125  }
126 
140  public function buildUriFromAjaxId($ajaxIdentifier, $parameters = [], $referenceType = self::ABSOLUTE_PATH)
141  {
142  $parameters = [
143  'ajaxID' => $ajaxIdentifier
144  ] + $parameters;
145  if (!empty($GLOBALS['TYPO3_CONF_VARS']['BE']['AJAX'][$ajaxIdentifier]['csrfTokenCheck'])) {
146  $parameters['ajaxToken'] = FormProtectionFactory::get('backend')->generateToken('ajaxCall', $ajaxIdentifier);
147  }
148  return $this->buildUri($parameters, $referenceType);
149  }
150 
159  protected function buildUri($parameters, $referenceType)
160  {
161  $uri = 'index.php?' . ltrim(GeneralUtility::implodeArrayForUrl('', $parameters, '', false, 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 }
buildUriFromModule($moduleName, $parameters=[], $referenceType=self::ABSOLUTE_PATH)
Definition: UriBuilder.php:118
buildUriFromRoute($name, $parameters=[], $referenceType=self::ABSOLUTE_PATH)
Definition: UriBuilder.php:69
buildUriFromAjaxId($ajaxIdentifier, $parameters=[], $referenceType=self::ABSOLUTE_PATH)
Definition: UriBuilder.php:140
static getAbsoluteWebPath($targetPath)
Definition: PathUtility.php:40
buildUri($parameters, $referenceType)
Definition: UriBuilder.php:159
static implodeArrayForUrl($name, array $theArray, $str='', $skipBlank=false, $rawurlencodeParamName=false)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']