[BUGFIX] Respect GET parameters when generating canonicalized URLs
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / Utility / CanonicalizationUtility.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Frontend\Utility;
5
6 /*
7 * This file is part of the TYPO3 CMS project.
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19 use Psr\Http\Message\ServerRequestInterface;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use TYPO3\CMS\Core\Utility\HttpUtility;
22 use TYPO3\CMS\Frontend\Page\CacheHashCalculator;
23
24 class CanonicalizationUtility
25 {
26 /**
27 * Get all params that are not needed to determine a canonicalized URL
28 *
29 * The format of the additionalCanonicalizedUrlParameters is:
30 * $parameters = [
31 * 'foo',
32 * 'bar',
33 * 'foo[bar]'
34 * ]
35 *
36 * @param int $pageId Id of the page you want to get the excluded params
37 * @param array $additionalCanonicalizedUrlParameters Which GET-params should stay besides the params used for cHash calculation
38 *
39 * @return array
40 */
41 public static function getParamsToExcludeForCanonicalizedUrl(int $pageId, array $additionalCanonicalizedUrlParameters = []): array
42 {
43 $cacheHashCalculator = GeneralUtility::makeInstance(CacheHashCalculator::class);
44
45 $GET = ($GLOBALS['TYPO3_REQUEST'] instanceof ServerRequestInterface) ? $GLOBALS['TYPO3_REQUEST']->getQueryParams() : [];
46 $GET['id'] = $pageId;
47
48 $queryString = HttpUtility::buildQueryString($GET, '&');
49 $cHashArray = $cacheHashCalculator->getRelevantParameters($queryString);
50
51 // By exploding the earlier imploded array, we get the flat array with URL params
52 $urlParameters = GeneralUtility::explodeUrl2Array($queryString);
53
54 $paramsToExclude = array_keys(
55 array_diff(
56 $urlParameters,
57 $cHashArray
58 )
59 );
60
61 return array_diff($paramsToExclude, $additionalCanonicalizedUrlParameters);
62 }
63 }