1464fdad91a37cc41a25dddb0c4c39d26d0f85f4
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Mvc / Web / Request.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Mvc\Web;
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 /**
18 * Represents a web request.
19 */
20 class Request extends \TYPO3\CMS\Extbase\Mvc\Request
21 {
22 /**
23 * @var \TYPO3\CMS\Extbase\Security\Cryptography\HashService
24 */
25 protected $hashService;
26
27 /**
28 * @var string The requested representation format
29 */
30 protected $format = 'html';
31
32 /**
33 * @var string Contains the request method
34 */
35 protected $method = 'GET';
36
37 /**
38 * @var string
39 */
40 protected $requestUri;
41
42 /**
43 * @var string The base URI for this request - ie. the host and path leading to the index.php
44 */
45 protected $baseUri;
46
47 /**
48 * @var bool TRUE if the current request is cached, false otherwise.
49 */
50 protected $isCached = false;
51
52 /**
53 * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
54 */
55 protected $configurationManager;
56
57 /**
58 * @var \TYPO3\CMS\Extbase\Service\EnvironmentService
59 */
60 protected $environmentService;
61
62 /**
63 * @param \TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService
64 * @internal only to be used within Extbase, not part of TYPO3 Core API.
65 */
66 public function injectHashService(\TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService)
67 {
68 $this->hashService = $hashService;
69 }
70
71 /**
72 * @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager
73 * @internal only to be used within Extbase, not part of TYPO3 Core API.
74 */
75 public function injectConfigurationManager(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager)
76 {
77 $this->configurationManager = $configurationManager;
78 }
79
80 /**
81 * @param \TYPO3\CMS\Extbase\Service\EnvironmentService $environmentService
82 * @internal only to be used within Extbase, not part of TYPO3 Core API.
83 */
84 public function injectEnvironmentService(\TYPO3\CMS\Extbase\Service\EnvironmentService $environmentService)
85 {
86 $this->environmentService = $environmentService;
87 }
88
89 /**
90 * Sets the request method
91 *
92 * @param string $method Name of the request method
93 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\InvalidRequestMethodException if the request method is not supported
94 * @internal only to be used within Extbase, not part of TYPO3 Core API.
95 */
96 public function setMethod($method)
97 {
98 if ($method === '' || strtoupper($method) !== $method) {
99 throw new \TYPO3\CMS\Extbase\Mvc\Exception\InvalidRequestMethodException('The request method "' . $method . '" is not supported.', 1217778382);
100 }
101 $this->method = $method;
102 }
103
104 /**
105 * Returns the name of the request method
106 *
107 * @return string Name of the request method
108 */
109 public function getMethod()
110 {
111 return $this->method;
112 }
113
114 /**
115 * Sets the request URI
116 *
117 * @param string $requestUri URI of this web request
118 * @internal only to be used within Extbase, not part of TYPO3 Core API.
119 */
120 public function setRequestUri($requestUri)
121 {
122 $this->requestUri = $requestUri;
123 }
124
125 /**
126 * Returns the request URI
127 *
128 * @return string URI of this web request
129 */
130 public function getRequestUri()
131 {
132 return $this->requestUri;
133 }
134
135 /**
136 * Sets the base URI for this request.
137 *
138 * @param string $baseUri New base URI
139 * @internal only to be used within Extbase, not part of TYPO3 Core API.
140 */
141 public function setBaseUri($baseUri)
142 {
143 $this->baseUri = $baseUri;
144 }
145
146 /**
147 * Returns the base URI
148 *
149 * @return string Base URI of this web request
150 */
151 public function getBaseUri()
152 {
153 if ($this->environmentService->isEnvironmentInBackendMode()) {
154 return $this->baseUri . TYPO3_mainDir;
155 }
156 return $this->baseUri;
157 }
158
159 /**
160 * Set if the current request is cached.
161 *
162 * @param bool $isCached
163 * @internal only to be used within Extbase, not part of TYPO3 Core API.
164 */
165 public function setIsCached($isCached)
166 {
167 $this->isCached = (bool)$isCached;
168 }
169
170 /**
171 * Return whether the current request is a cached request or not.
172 *
173 * @return bool the caching status.
174 * @internal only to be used within Extbase, not part of TYPO3 Core API.
175 */
176 public function isCached()
177 {
178 return $this->isCached;
179 }
180
181 /**
182 * Get a freshly built request object pointing to the Referrer.
183 *
184 * @return ReferringRequest the referring request, or null if no referrer found
185 * @internal only to be used within Extbase, not part of TYPO3 Core API.
186 */
187 public function getReferringRequest()
188 {
189 if (isset($this->internalArguments['__referrer']['@request'])) {
190 $referrerArray = unserialize($this->hashService->validateAndStripHmac($this->internalArguments['__referrer']['@request']));
191 $arguments = [];
192 if (isset($this->internalArguments['__referrer']['arguments'])) {
193 // This case is kept for compatibility in 7.6 and 6.2, but will be removed in 8
194 $arguments = unserialize(base64_decode($this->hashService->validateAndStripHmac($this->internalArguments['__referrer']['arguments'])));
195 }
196 $referringRequest = new ReferringRequest();
197 $referringRequest->setArguments(array_replace_recursive($arguments, $referrerArray));
198 return $referringRequest;
199 }
200 return null;
201 }
202 }