‪TYPO3CMS  ‪main
LockedBackendGuard.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\ResponseInterface;
21 use Psr\Http\Message\ServerRequestInterface;
22 use Psr\Http\Server\MiddlewareInterface;
23 use Psr\Http\Server\RequestHandlerInterface;
31 
40 class ‪LockedBackendGuard implements MiddlewareInterface
41 {
45  public function ‪process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
46  {
47  try {
48  $redirectToUri = $this->‪checkLockedBackend();
49  if (!empty($redirectToUri)) {
50  return new ‪RedirectResponse($redirectToUri, 302);
51  }
52  } catch (‪BackendLockedException $e) {
53  // Looks like an AJAX request that can handle JSON, (usually from the timeout functionality)
54  // So, let's form a request that fits
55  if (str_contains($request->getHeaderLine('Accept'), 'application/json')) {
56  $session = [
57  'timed_out' => false,
58  'will_time_out' => false,
59  'locked' => true,
60  'message' => $e->getMessage(),
61  ];
62  return new ‪JsonResponse(['login' => $session]);
63  }
64  throw $e;
65  }
67  $request->getAttribute('normalizedParams')->getRemoteAddress(),
68  trim((string)‪$GLOBALS['TYPO3_CONF_VARS']['BE']['IPmaskList'])
69  );
70 
71  return $handler->handle($request);
72  }
73 
79  protected function ‪checkLockedBackend(): ?string
80  {
81  if (‪$GLOBALS['TYPO3_CONF_VARS']['BE']['adminOnly'] < 0) {
82  throw new ‪BackendLockedException(
84  'Backend and Install Tool are locked for maintenance. [BE][adminOnly] is set to "' . (int)‪$GLOBALS['TYPO3_CONF_VARS']['BE']['adminOnly'] . '".',
85  'TYPO3 Backend locked',
86  1517949794
87  );
88  }
89  if (@is_file(‪Environment::getLegacyConfigPath() . '/LOCK_BACKEND')) {
90  $fileContent = file_get_contents(‪Environment::getLegacyConfigPath() . '/LOCK_BACKEND');
91  if ($fileContent) {
92  return $fileContent;
93  }
94  throw new ‪BackendLockedException(
96  'Backend access by browser is locked for maintenance. Remove lock by removing the file "typo3conf/LOCK_BACKEND" or use CLI-scripts.',
97  'TYPO3 Backend locked',
98  1517949793
99  );
100  }
101 
102  return null;
103  }
104 
110  protected function ‪validateVisitorsIpAgainstIpMaskList(string $ipAddress, string $ipMaskList = '')
111  {
112  if ($ipMaskList !== '' && !‪GeneralUtility::cmpIP($ipAddress, $ipMaskList)) {
115  'The IP address of your client does not match the list of allowed IP addresses.',
116  'TYPO3 Backend access denied',
117  1517949792
118  );
119  }
120  }
121 }
‪TYPO3\CMS\Backend\Middleware
Definition: AdditionalResponseHeaders.php:18
‪TYPO3\CMS\Backend\Exception\BackendAccessDeniedException
Definition: BackendAccessDeniedException.php:22
‪TYPO3\CMS\Core\Utility\GeneralUtility\cmpIP
‪static bool cmpIP(string $baseIP, string $list)
Definition: GeneralUtility.php:113
‪TYPO3\CMS\Core\Core\Environment\getLegacyConfigPath
‪static getLegacyConfigPath()
Definition: Environment.php:268
‪TYPO3\CMS\Backend\Middleware\LockedBackendGuard
Definition: LockedBackendGuard.php:41
‪TYPO3\CMS\Backend\Middleware\LockedBackendGuard\validateVisitorsIpAgainstIpMaskList
‪validateVisitorsIpAgainstIpMaskList(string $ipAddress, string $ipMaskList='')
Definition: LockedBackendGuard.php:110
‪TYPO3\CMS\Backend\Exception\BackendLockedException
Definition: BackendLockedException.php:22
‪TYPO3\CMS\Core\Utility\HttpUtility\HTTP_STATUS_403
‪const HTTP_STATUS_403
Definition: HttpUtility.php:56
‪TYPO3\CMS\Core\Http\RedirectResponse
Definition: RedirectResponse.php:30
‪TYPO3\CMS\Backend\Middleware\LockedBackendGuard\process
‪process(ServerRequestInterface $request, RequestHandlerInterface $handler)
Definition: LockedBackendGuard.php:45
‪TYPO3\CMS\Core\Http\JsonResponse
Definition: JsonResponse.php:28
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Core\Utility\HttpUtility
Definition: HttpUtility.php:24
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Backend\Middleware\LockedBackendGuard\checkLockedBackend
‪checkLockedBackend()
Definition: LockedBackendGuard.php:79