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