2 declare(strict_types = 1);
18 use Psr\Http\Message\UriInterface;
19 use Psr\Log\LoggerAwareInterface;
20 use Psr\Log\LoggerAwareTrait;
52 public function matchRedirect(
string $domain,
string $path,
string $query =
'')
55 $path = rawurldecode($path);
58 foreach ([$domain,
'*'] as $domainName) {
59 if (empty($allRedirects[$domainName])) {
63 $possibleRedirects = [];
65 if (!empty($allRedirects[$domainName][
'flat'][rtrim($path,
'/') .
'/'])) {
66 $possibleRedirects = $allRedirects[$domainName][
'flat'][rtrim($path,
'/') .
'/'];
70 $pathWithQuery = rtrim($path,
'/') .
'?' . ltrim($query,
'?');
71 if (!empty($allRedirects[$domainName][
'respect_query_parameters'][$pathWithQuery])) {
72 $possibleRedirects = $allRedirects[$domainName][
'respect_query_parameters'][$pathWithQuery];
74 $pathWithQueryAndSlash = rtrim($path,
'/') .
'/?' . ltrim($query,
'?');
75 if (!empty($allRedirects[$domainName][
'respect_query_parameters'][$pathWithQueryAndSlash])) {
76 $possibleRedirects = $allRedirects[$domainName][
'respect_query_parameters'][$pathWithQueryAndSlash];
81 if (!empty($allRedirects[$domainName][
'regexp'])) {
82 $allRegexps = array_keys($allRedirects[$domainName][
'regexp']);
83 foreach ($allRegexps as $regexp) {
84 $matchResult = @preg_match($regexp, $path);
86 $possibleRedirects += $allRedirects[$domainName][
'regexp'][$regexp];
87 } elseif ($matchResult ===
false) {
88 $this->logger->warning(
'Invalid regex in redirect', [
'regex' => $regexp]);
93 foreach ($possibleRedirects as $possibleRedirect) {
96 return $possibleRedirect;
111 return !$redirectRecord[
'disabled'] && $redirectRecord[
'starttime'] <=
$GLOBALS[
'SIM_ACCESS_TIME'] &&
112 (!$redirectRecord[
'endtime'] || $redirectRecord[
'endtime'] >=
$GLOBALS[
'SIM_ACCESS_TIME']);
123 return GeneralUtility::makeInstance(RedirectCacheService::class)->getRedirects();
136 $linkService = GeneralUtility::makeInstance(LinkService::class);
138 $linkDetails = $linkService->resolve($redirectTarget);
139 switch ($linkDetails[
'type']) {
145 $file = $linkDetails[
'file'];
146 if ($file instanceof
File) {
147 $linkDetails[
'url'] = $file->getPublicUrl();
152 $folder = $linkDetails[
'folder'];
153 if ($folder instanceof
Folder) {
154 $linkDetails[
'url'] = $folder->getPublicUrl();
176 $this->logger->debug(
'Found a redirect to process', $matchedRedirect);
177 $linkParameterParts = GeneralUtility::makeInstance(TypoLinkCodecService::class)->decode((
string)$matchedRedirect[
'target']);
178 $redirectTarget = $linkParameterParts[
'url'];
180 $this->logger->debug(
'Resolved link details for redirect', $linkDetails);
181 if (!empty($linkParameterParts[
'additionalParams']) && $matchedRedirect[
'keep_query_parameters']) {
182 $params = GeneralUtility::explodeUrl2Array($linkParameterParts[
'additionalParams']);
183 foreach ($params as $key => $value) {
184 $queryParams[$key] = $value;
188 if (!empty($linkDetails[
'url'])) {
189 if ($matchedRedirect[
'is_regexp'] ??
false) {
193 $url =
new Uri($linkDetails[
'url']);
194 if ($matchedRedirect[
'force_https']) {
195 $url = $url->withScheme(
'https');
197 if ($matchedRedirect[
'keep_query_parameters']) {
216 $newQueryParamString = $url->
getQuery();
217 if (!empty($newQueryParamString)) {
218 $newQueryParams = [];
219 parse_str($newQueryParamString, $newQueryParams);
220 $queryParams = array_replace_recursive($queryParams, $newQueryParams);
222 $query = http_build_query($queryParams,
'',
'&', PHP_QUERY_RFC3986);
240 if (!isset($linkDetails[
'type'],
$GLOBALS[
'TYPO3_CONF_VARS'][
'FE'][
'typolinkBuilder'][$linkDetails[
'type']])) {
245 $linkBuilder = GeneralUtility::makeInstance(
246 $GLOBALS[
'TYPO3_CONF_VARS'][
'FE'][
'typolinkBuilder'][$linkDetails[
'type']],
252 'parameter' => (string)$redirectRecord[
'target'],
253 'forceAbsoluteUrl' =>
true,
255 if ($redirectRecord[
'force_https']) {
256 $configuration[
'forceAbsoluteUrl.'][
'scheme'] =
'https';
258 if ($redirectRecord[
'keep_query_parameters']) {
261 [$url] = $linkBuilder->build($linkDetails,
'',
'', $configuration);
262 return new Uri($url);
265 $url = $controller->cObj->lastTypoLinkUrl;
267 return new Uri($url);
293 $GLOBALS[
'TYPO3_CONF_VARS'][
'FE'][
'pageUnavailable_handling'] =
false;
294 $controller = GeneralUtility::makeInstance(
295 TypoScriptFrontendController::class,
300 $controller->fe_user =
$GLOBALS[
'TSFE']->fe_user ??
null;
301 $controller->fetch_the_id();
302 $controller->calculateLinkVars($queryParams);
303 $controller->getConfigArray();
304 $controller->settingLanguage();
305 $controller->settingLocale();
306 $controller->newCObj();
311 $GLOBALS[
'TSFE']->sys_page = GeneralUtility::makeInstance(PageRepository::class);
324 $matchResult = @preg_match($matchedRedirect[
'source_path'], $uri->getPath(), $matches);
325 if ($matchResult > 0) {
326 foreach ($matches as $key => $val) {
327 $linkDetails[
'url'] = str_replace(
'$' . $key, $val, $linkDetails[
'url']);