[TASK] Have eIDs with PSR-7 without ControllerInterface
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / Controller / ShowImageController.php
1 <?php
2 namespace TYPO3\CMS\Frontend\Controller;
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 use Psr\Http\Message\ServerRequestInterface;
18 use Psr\Http\Message\ResponseInterface;
19 use TYPO3\CMS\Core\Exception;
20 use TYPO3\CMS\Core\Resource\ResourceFactory;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\CMS\Core\Utility\MathUtility;
23
24 /**
25 * eID-Script "tx_cms_showpic"
26 *
27 * Shows a picture from FAL in enlarged format in a separate window.
28 * Picture file and settings is supplied by GET-parameters:
29 *
30 * - file = fileUid or Combined Identifier
31 * - encoded in an parameter Array (with weird format - see ContentObjectRenderer about ll. 1500)
32 * - width, height = usual width an height, m/c supported
33 * - frame
34 * - bodyTag
35 * - title
36 */
37 class ShowImageController {
38
39 /**
40 * @var \Psr\Http\Message\ServerRequestInterface
41 */
42 protected $request;
43
44 /**
45 * @var \TYPO3\CMS\Core\Resource\File
46 */
47 protected $file;
48
49 /**
50 * @var int
51 */
52 protected $width;
53
54 /**
55 * @var int
56 */
57 protected $height;
58
59 /**
60 * @var int
61 */
62 protected $frame;
63
64 /**
65 * @var string
66 */
67 protected $bodyTag = '<body>';
68
69 /**
70 * @var string
71 */
72 protected $title = 'Image';
73
74 /**
75 * @var string
76 */
77 protected $content = <<<EOF
78 <!DOCTYPE html>
79 <html>
80 <head>
81 <title>###TITLE###</title>
82 <meta name="robots" content="noindex,follow" />
83 </head>
84 ###BODY###
85 ###IMAGE###
86 </body>
87 </html>
88 EOF;
89
90 /**
91 * @var string
92 */
93 protected $imageTag = '<img src="###publicUrl###" alt="###alt###" title="###title###" width="###width###" height="###height###" />';
94
95 /**
96 * Init function, setting the input vars in the global space.
97 *
98 * @return void
99 * @throws \InvalidArgumentException
100 * @throws \TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException
101 */
102 public function initialize() {
103 $fileUid = isset($this->request->getQueryParams()['file']) ? $this->request->getQueryParams()['file'] : NULL;
104 $parametersArray = isset($this->request->getQueryParams()['parameters']) ? $this->request->getQueryParams()['parameters'] : NULL;
105
106 // If no file-param or parameters are given, we must exit
107 if (!$fileUid || !isset($parametersArray) || !is_array($parametersArray)) {
108 throw new \InvalidArgumentException('No valid fileUid given');
109 }
110
111 // rebuild the parameter array and check if the HMAC is correct
112 $parametersEncoded = implode('', $parametersArray);
113
114 /* For backwards compatibility the HMAC is transported within the md5 param */
115 $hmacParameter = isset($this->request->getQueryParams()['md5']) ? $this->request->getQueryParams()['md5'] : NULL;
116 $hmac = GeneralUtility::hmac(implode('|', array($fileUid, $parametersEncoded)));
117 if ($hmac !== $hmacParameter) {
118 throw new \InvalidArgumentException('hash does not match');
119 }
120
121 // decode the parameters Array
122 $parameters = unserialize(base64_decode($parametersEncoded));
123 foreach ($parameters as $parameterName => $parameterValue) {
124 $this->{$parameterName} = $parameterValue;
125 }
126
127 if (MathUtility::canBeInterpretedAsInteger($fileUid)) {
128 $this->file = ResourceFactory::getInstance()->getFileObject((int)$fileUid);
129 } else {
130 $this->file = ResourceFactory::getInstance()->retrieveFileOrFolderObject($fileUid);
131 }
132 $this->frame = isset($this->request->getQueryParams()['frame']) ? $this->request->getQueryParams()['frame'] : NULL;
133 }
134
135 /**
136 * Main function which creates the image if needed and outputs the HTML code for the page displaying the image.
137 * Accumulates the content in $this->content
138 *
139 * @return void
140 */
141 public function main() {
142 $processedImage = $this->processImage();
143 $imageTagMarkers = array(
144 '###publicUrl###' => htmlspecialchars($processedImage->getPublicUrl()),
145 '###alt###' => htmlspecialchars($this->file->getProperty('alternative') ?: $this->title),
146 '###title###' => htmlspecialchars($this->file->getProperty('title') ?: $this->title),
147 '###width###' => $processedImage->getProperty('width'),
148 '###height###' => $processedImage->getProperty('height')
149 );
150 $this->imageTag = str_replace(array_keys($imageTagMarkers), array_values($imageTagMarkers), $this->imageTag);
151 $markerArray = array(
152 '###TITLE###' => ($this->file->getProperty('title') ?: $this->title),
153 '###IMAGE###' => $this->imageTag,
154 '###BODY###' => $this->bodyTag
155 );
156
157 $this->content = str_replace(array_keys($markerArray), array_values($markerArray), $this->content);
158 }
159
160 /**
161 * Does the actual image processing
162 *
163 * @return \TYPO3\CMS\Core\Resource\ProcessedFile
164 */
165 protected function processImage() {
166 if (strstr($this->width . $this->height, 'm')) {
167 $max = 'm';
168 } else {
169 $max = '';
170 }
171 $this->height = MathUtility::forceIntegerInRange($this->height, 0);
172 $this->width = MathUtility::forceIntegerInRange($this->width, 0) . $max;
173
174 $processingConfiguration = array(
175 'width' => $this->width,
176 'height' => $this->height,
177 'frame' => $this->frame,
178
179 );
180 return $this->file->process('Image.CropScaleMask', $processingConfiguration);
181 }
182
183 /**
184 * Fetches the content and builds a content file out of it
185 *
186 * @param ServerRequestInterface $request the current request object
187 * @param ResponseInterface $response the available response
188 * @return ResponseInterface the modified response
189 */
190 public function processRequest(ServerRequestInterface $request, ResponseInterface $response) {
191 $this->request = $request;
192
193 try {
194 $this->initialize();
195 $this->main();
196 $response->getBody()->write($this->content);
197 return $response;
198 } catch (\InvalidArgumentException $e) {
199 // add a 410 "gone" if invalid parameters given
200 return $response->withStatus(410);
201 } catch (Exception $e) {
202 return $response->withStatus(404);
203 }
204 }
205
206 }