[BUGFIX] Apply image cropping to click-enlarged images
[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\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 string
61 */
62 protected $crop;
63
64 /**
65 * @var int
66 */
67 protected $frame;
68
69 /**
70 * @var string
71 */
72 protected $bodyTag = '<body>';
73
74 /**
75 * @var string
76 */
77 protected $title = 'Image';
78
79 /**
80 * @var string
81 */
82 protected $content = <<<EOF
83 <!DOCTYPE html>
84 <html>
85 <head>
86 <title>###TITLE###</title>
87 <meta name="robots" content="noindex,follow" />
88 </head>
89 ###BODY###
90 ###IMAGE###
91 </body>
92 </html>
93 EOF;
94
95 /**
96 * @var string
97 */
98 protected $imageTag = '<img src="###publicUrl###" alt="###alt###" title="###title###" width="###width###" height="###height###" />';
99
100 /**
101 * Init function, setting the input vars in the global space.
102 *
103 * @return void
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');
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('|', array($fileUid, $parametersEncoded)));
123 if ($hmac !== $hmacParameter) {
124 throw new \InvalidArgumentException('hash does not match');
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 * @return void
146 */
147 public function main()
148 {
149 $processedImage = $this->processImage();
150 $imageTagMarkers = array(
151 '###publicUrl###' => htmlspecialchars($processedImage->getPublicUrl()),
152 '###alt###' => htmlspecialchars($this->file->getProperty('alternative') ?: $this->title),
153 '###title###' => htmlspecialchars($this->file->getProperty('title') ?: $this->title),
154 '###width###' => $processedImage->getProperty('width'),
155 '###height###' => $processedImage->getProperty('height')
156 );
157 $this->imageTag = str_replace(array_keys($imageTagMarkers), array_values($imageTagMarkers), $this->imageTag);
158 $markerArray = array(
159 '###TITLE###' => ($this->file->getProperty('title') ?: $this->title),
160 '###IMAGE###' => $this->imageTag,
161 '###BODY###' => $this->bodyTag
162 );
163
164 $this->content = str_replace(array_keys($markerArray), array_values($markerArray), $this->content);
165 }
166
167 /**
168 * Does the actual image processing
169 *
170 * @return \TYPO3\CMS\Core\Resource\ProcessedFile
171 */
172 protected function processImage()
173 {
174 if (strstr($this->width . $this->height, 'm')) {
175 $max = 'm';
176 } else {
177 $max = '';
178 }
179 $this->height = MathUtility::forceIntegerInRange($this->height, 0);
180 $this->width = MathUtility::forceIntegerInRange($this->width, 0) . $max;
181
182 $processingConfiguration = array(
183 'width' => $this->width,
184 'height' => $this->height,
185 'frame' => $this->frame,
186 'crop' => $this->crop,
187 );
188 return $this->file->process('Image.CropScaleMask', $processingConfiguration);
189 }
190
191 /**
192 * Fetches the content and builds a content file out of it
193 *
194 * @param ServerRequestInterface $request the current request object
195 * @param ResponseInterface $response the available response
196 * @return ResponseInterface the modified response
197 */
198 public function processRequest(ServerRequestInterface $request, ResponseInterface $response)
199 {
200 $this->request = $request;
201
202 try {
203 $this->initialize();
204 $this->main();
205 $response->getBody()->write($this->content);
206 return $response;
207 } catch (\InvalidArgumentException $e) {
208 // add a 410 "gone" if invalid parameters given
209 return $response->withStatus(410);
210 } catch (Exception $e) {
211 return $response->withStatus(404);
212 }
213 }
214 }