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