9eb8136be0126109700633760d71f8df29c3651c
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / Page / ExternalPageUrlHandler.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Frontend\Page;
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
18 use Psr\Http\Message\ResponseInterface;
19 use TYPO3\CMS\Core\Http\RedirectResponse;
20 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
21
22 /**
23 * Handles the redirection for external URL pages.
24 */
25 class ExternalPageUrlHandler implements \TYPO3\CMS\Frontend\Http\UrlHandlerInterface
26 {
27 /**
28 * @var string
29 */
30 protected $externalUrl = '';
31
32 /**
33 * Checks if external URLs are enabled and if the current page points to an external URL.
34 *
35 * @return bool
36 */
37 public function canHandleCurrentUrl(): bool
38 {
39 $tsfe = $this->getTypoScriptFrontendController();
40
41 if (!empty($tsfe->config['config']['disablePageExternalUrl'])) {
42 return false;
43 }
44
45 $this->externalUrl = $tsfe->sys_page->getExtURL($tsfe->page);
46 if (empty($this->externalUrl)) {
47 return false;
48 }
49 return true;
50 }
51
52 /**
53 * Redirects the user to the detected external URL.
54 */
55 public function handle(): ResponseInterface
56 {
57 return new RedirectResponse($this->externalUrl, 303);
58 }
59
60 /**
61 * @return TypoScriptFrontendController
62 */
63 protected function getTypoScriptFrontendController(): TypoScriptFrontendController
64 {
65 return $GLOBALS['TSFE'];
66 }
67 }