TYPO3 CMS  TYPO3_8-7
RedirectFinisher.php
Go to the documentation of this file.
1 <?php
2 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 
23 
30 {
31 
35  protected $defaultOptions = [
36  'pageUid' => 1,
37  'additionalParameters' => '',
38  'delay' => 0,
39  'statusCode' => 303,
40  ];
41 
45  protected $request;
46 
50  protected $response;
51 
55  protected $uriBuilder;
56 
61  protected function executeInternal()
62  {
63  $formRuntime = $this->finisherContext->getFormRuntime();
64  $this->request = $formRuntime->getRequest();
65  $this->response = $formRuntime->getResponse();
66  $this->uriBuilder = $this->objectManager->get(UriBuilder::class);
67  $this->uriBuilder->setRequest($this->request);
68 
69  $pageUid = (int)str_replace('pages_', '', $this->parseOption('pageUid'));
70  $additionalParameters = $this->parseOption('additionalParameters');
71  $additionalParameters = '&' . ltrim($additionalParameters, '&');
72  $delay = (int)$this->parseOption('delay');
73  $statusCode = (int)$this->parseOption('statusCode');
74 
75  $this->finisherContext->cancel();
76  $this->redirect($pageUid, $additionalParameters, $delay, $statusCode);
77  }
78 
94  protected function redirect(int $pageUid = 1, string $additionalParameters = '', int $delay = 0, int $statusCode = 303)
95  {
96  if (!$this->request instanceof Request) {
97  throw new UnsupportedRequestTypeException('redirect() only supports web requests.', 1471776457);
98  }
99 
100  $typolinkConfiguration = [
101  'parameter' => $pageUid,
102  'additionalParams' => $additionalParameters,
103  ];
104  $redirectUri = $this->getTypoScriptFrontendController()->cObj->typoLink_URL($typolinkConfiguration);
105  $this->redirectToUri($redirectUri, $delay, $statusCode);
106  }
107 
119  protected function redirectToUri(string $uri, int $delay = 0, int $statusCode = 303)
120  {
121  if (!$this->request instanceof Request) {
122  throw new UnsupportedRequestTypeException('redirect() only supports web requests.', 1471776458);
123  }
124 
125  $uri = $this->addBaseUriIfNecessary($uri);
126  $escapedUri = htmlentities($uri, ENT_QUOTES, 'utf-8');
127 
128  $this->response->setContent('<html><head><meta http-equiv="refresh" content="' . (int)$delay . ';url=' . $escapedUri . '"/></head></html>');
129  if ($this->response instanceof \TYPO3\CMS\Extbase\Mvc\Web\Response) {
130  $this->response->setStatus($statusCode);
131  $this->response->setHeader('Location', (string)$uri);
132  }
133  throw new StopActionException('redirectToUri', 1477070964);
134  }
135 
142  protected function addBaseUriIfNecessary(string $uri): string
143  {
144  return GeneralUtility::locationHeaderUrl((string)$uri);
145  }
146 }
redirect(int $pageUid=1, string $additionalParameters='', int $delay=0, int $statusCode=303)
redirectToUri(string $uri, int $delay=0, int $statusCode=303)