‪TYPO3CMS  ‪main
BackendEntryPointResolver.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Http\Message\ServerRequestInterface;
21 use Psr\Http\Message\UriInterface;
24 
29 {
30  protected string ‪$entryPoint = '/typo3';
31 
35  public function ‪getPathFromRequest(ServerRequestInterface $request): string
36  {
37  ‪$entryPoint = $this->‪getEntryPoint($request);
38  if (str_contains(‪$entryPoint, '//')) {
39  $entryPointParts = parse_url(‪$entryPoint);
40  /* Remove trailing slash unless, the string is '/' itself */
41  ‪$entryPoint = rtrim('/' . trim($entryPointParts['path'] ?? '', '/'), '/');
42  }
43  return ‪$entryPoint . '/';
44  }
45 
49  public function ‪getUriFromRequest(ServerRequestInterface $request, string $additionalPathPart = ''): UriInterface
50  {
51  if (str_starts_with(ltrim($additionalPathPart, '/'), 'install.php')) {
52  // install entrypoint not configurable yet, therefore it's essential to return the static `typo3/install.php`
53  // uri - otherwise wrong redirect would happen and leading to nested backend for admin area displayed.
54  if ($request->getAttribute('normalizedParams') instanceof ‪NormalizedParams) {
55  $normalizedParams = $request->getAttribute('normalizedParams');
56  } else {
57  $normalizedParams = ‪NormalizedParams::createFromRequest($request);
58  }
59  return new ‪Uri($normalizedParams->getSiteUrl() . 'typo3/install.php');
60  }
62  if (str_starts_with(‪$entryPoint, 'https://') || str_starts_with(‪$entryPoint, 'http://')) {
63  // fqdn, early return as all required information are available.
64  return new ‪Uri(‪$entryPoint . '/' . ltrim($additionalPathPart, '/'));
65  }
66  if ($request->getAttribute('normalizedParams') instanceof ‪NormalizedParams) {
67  $normalizedParams = $request->getAttribute('normalizedParams');
68  } else {
69  $normalizedParams = ‪NormalizedParams::createFromRequest($request);
70  }
71  if (str_starts_with(‪$entryPoint, '//')) {
72  // Browser supports uri starting with `//` and uses the current request scheme for the link. Do avoid issue
73  // for example checking the url at some point we prefix it with the current request protocol.
74  return new ‪Uri(($normalizedParams->isHttps() ? 'https:' : 'http:') . ‪$entryPoint . '/' . ltrim($additionalPathPart, '/'));
75  }
76  return new ‪Uri($normalizedParams->getSiteUrl() . ‪$entryPoint . '/' . ltrim($additionalPathPart, '/'));
77  }
78 
79  public function ‪isBackendRoute(ServerRequestInterface $request): bool
80  {
81  return $this->‪getBackendRoutePath($request) !== null;
82  }
83 
84  public function ‪getBackendRoutePath(ServerRequestInterface $request): ?string
85  {
86  $uri = $request->getUri();
87  $path = $uri->getPath();
88  ‪$entryPoint = $this->‪getEntryPoint($request);
89 
90  if (str_contains(‪$entryPoint, '//')) {
91  $entryPointParts = parse_url(‪$entryPoint);
92  if ($uri->getHost() !== $entryPointParts['host']) {
93  return null;
94  }
95  /* Remove trailing slash unless, the string is '/' itself */
96  ‪$entryPoint = rtrim('/' . trim($entryPointParts['path'] ?? '', '/'), '/');
97  }
98 
99  if ($path === ‪$entryPoint) {
100  return '';
101  }
102  if (str_starts_with($path, ‪$entryPoint . '/')) {
103  return substr($path, strlen(‪$entryPoint));
104  }
105  return null;
106  }
107 
111  protected function ‪getEntryPoint(ServerRequestInterface $request): string
112  {
114  if (str_contains(‪$entryPoint, '//')) {
115  return ‪$entryPoint;
116  }
117  if ($request->getAttribute('normalizedParams') instanceof ‪NormalizedParams) {
118  $normalizedParams = $request->getAttribute('normalizedParams');
119  } else {
120  $normalizedParams = ‪NormalizedParams::createFromRequest($request);
121  }
122  return $normalizedParams->getSitePath() . ‪$entryPoint;
123  }
124 
125  protected function ‪getEntryPointConfiguration(): string
126  {
127  ‪$entryPoint = ‪$GLOBALS['TYPO3_CONF_VARS']['BE']['entryPoint'] ?? ‪$this->entryPoint;
128  if (str_starts_with(‪$entryPoint, 'https://')
129  || str_starts_with(‪$entryPoint, 'http://')
130  || str_starts_with(‪$entryPoint, '//')
131  ) {
132  $uri = new ‪Uri(rtrim(‪$entryPoint, '/'));
133  $uri = $uri->withPath($this->‪removeMultipleSlashes($uri->getPath()));
134  return (string)$uri;
135  }
136  return $this->‪removeMultipleSlashes(trim(‪$entryPoint, '/'));
137  }
138 
139  private function ‪removeMultipleSlashes(string $value): string
140  {
141  return preg_replace('/(\/+)/', '/', $value);
142  }
143 }
‪TYPO3\CMS\Core\Routing\BackendEntryPointResolver\getUriFromRequest
‪getUriFromRequest(ServerRequestInterface $request, string $additionalPathPart='')
Definition: BackendEntryPointResolver.php:49
‪TYPO3\CMS\Core\Routing\BackendEntryPointResolver\removeMultipleSlashes
‪removeMultipleSlashes(string $value)
Definition: BackendEntryPointResolver.php:139
‪TYPO3\CMS\Core\Routing\BackendEntryPointResolver\isBackendRoute
‪isBackendRoute(ServerRequestInterface $request)
Definition: BackendEntryPointResolver.php:79
‪TYPO3\CMS\Core\Routing
‪TYPO3\CMS\Core\Routing\BackendEntryPointResolver\getBackendRoutePath
‪getBackendRoutePath(ServerRequestInterface $request)
Definition: BackendEntryPointResolver.php:84
‪TYPO3\CMS\Core\Http\Uri
Definition: Uri.php:30
‪TYPO3\CMS\Core\Routing\BackendEntryPointResolver\$entryPoint
‪string $entryPoint
Definition: BackendEntryPointResolver.php:30
‪TYPO3\CMS\Core\Routing\BackendEntryPointResolver\getEntryPointConfiguration
‪getEntryPointConfiguration()
Definition: BackendEntryPointResolver.php:125
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Http\NormalizedParams\createFromRequest
‪static static createFromRequest(ServerRequestInterface $request, array $systemConfiguration=null)
Definition: NormalizedParams.php:840
‪TYPO3\CMS\Core\Routing\BackendEntryPointResolver
Definition: BackendEntryPointResolver.php:29
‪TYPO3\CMS\Core\Http\NormalizedParams
Definition: NormalizedParams.php:38
‪TYPO3\CMS\Core\Routing\BackendEntryPointResolver\getEntryPoint
‪getEntryPoint(ServerRequestInterface $request)
Definition: BackendEntryPointResolver.php:111
‪TYPO3\CMS\Core\Routing\BackendEntryPointResolver\getPathFromRequest
‪getPathFromRequest(ServerRequestInterface $request)
Definition: BackendEntryPointResolver.php:35