[TASK] Move config.beLoginLinkIPList to compat7
[Packages/TYPO3.CMS.git] / typo3 / sysext / compatibility7 / Classes / Hooks / EnforceUrlSchemeHook.php
1 <?php
2 namespace TYPO3\CMS\Compatibility7\Hooks;
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
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Core\Utility\HttpUtility;
19 use TYPO3\CMS\Frontend\ContentObject\TypolinkModifyLinkConfigForPageLinksHookInterface;
20 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
21
22 /**
23 * Hooks for
24 * - TypoLink when linking to a page and sets forceAbsoluteUrl if the page has
25 * - TSFE to do a redirect if the url_scheme does not match
26 */
27 class EnforceUrlSchemeHook implements TypolinkModifyLinkConfigForPageLinksHookInterface
28 {
29 /**
30 * Hooks in typolink when linking to a page
31 *
32 * Checks if pages.url_scheme has a value and then enforces an absolute URL with the scheme set in there
33 *
34 * @param array $linkConfiguration the typolink configuration
35 * @param array $linkDetails
36 * @param array $pageRow the fetched page record
37 * @return array the modified link configuration
38 */
39 public function modifyPageLinkConfiguration(array $linkConfiguration, array $linkDetails, array $pageRow): array
40 {
41 // If pages.url_scheme is not set or not greater than zero, do not process
42 if (!isset($pageRow['url_scheme']) || !($pageRow['url_scheme'] > 0)) {
43 return $linkConfiguration;
44 }
45
46 // If an absolute URL with explicit scheme is already set, don't do anything
47 if (isset($linkConfiguration['forceAbsoluteUrl']) && $linkConfiguration['forceAbsoluteUrl']
48 && isset($conf['forceAbsoluteUrl.']['scheme']) && $conf['forceAbsoluteUrl.']['scheme']) {
49 return $linkConfiguration;
50 }
51
52 // Enable forceAbsoluteUrl so the link configuration actually enters the if() clause in TypoLink
53 $linkConfiguration['forceAbsoluteUrl'] = 1;
54
55 // Now explicitly override the scheme
56 if (!isset($linkConfiguration['forceAbsoluteUrl.'])) {
57 $linkConfiguration['forceAbsoluteUrl.'] = [];
58 }
59 $linkConfiguration['forceAbsoluteUrl.']['scheme'] = (int)$pageRow['url_scheme'] === HttpUtility::SCHEME_HTTP ? 'http' : 'https';
60 return $linkConfiguration;
61 }
62
63 /**
64 * Checks if pages.url_scheme is set, and then redirects to enforce HTTP / HTTPS if it does not match
65 *
66 * @param array $parameters not in use, as it does not contain useful information for this hook
67 * @param TypoScriptFrontendController $parentObject
68 */
69 public function redirectIfUrlSchemeDoesNotMatch($parameters, $parentObject)
70 {
71 if (isset($parentObject->page['url_scheme']) && $parentObject->page['url_scheme'] > 0) {
72 $newUrl = '';
73 $requestUrlScheme = parse_url(GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL'), PHP_URL_SCHEME);
74 if ((int)$parentObject->page['url_scheme'] === HttpUtility::SCHEME_HTTP && $requestUrlScheme === 'https') {
75 $newUrl = 'http://' . substr(GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL'), 8);
76 } elseif ((int)$parentObject->page['url_scheme'] === HttpUtility::SCHEME_HTTPS && $requestUrlScheme === 'http') {
77 $newUrl = 'https://' . substr(GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL'), 7);
78 }
79 if ($newUrl !== '') {
80 if ($_SERVER['REQUEST_METHOD'] === 'POST') {
81 $headerCode = HttpUtility::HTTP_STATUS_303;
82 } else {
83 $headerCode = HttpUtility::HTTP_STATUS_301;
84 }
85 HttpUtility::redirect($newUrl, $headerCode);
86 }
87 }
88 }
89 }