TYPO3  7.6
ShowImageController.php
Go to the documentation of this file.
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 
23 
38 {
42  protected $request;
43 
47  protected $file;
48 
52  protected $width;
53 
57  protected $height;
58 
62  protected $frame;
63 
67  protected $bodyTag = '<body>';
68 
72  protected $title = 'Image';
73 
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 
93  protected $imageTag = '<img src="###publicUrl###" alt="###alt###" title="###title###" width="###width###" height="###height###" />';
94 
102  public function initialize()
103  {
104  $fileUid = isset($this->request->getQueryParams()['file']) ? $this->request->getQueryParams()['file'] : null;
105  $parametersArray = isset($this->request->getQueryParams()['parameters']) ? $this->request->getQueryParams()['parameters'] : null;
106 
107  // If no file-param or parameters are given, we must exit
108  if (!$fileUid || !isset($parametersArray) || !is_array($parametersArray)) {
109  throw new \InvalidArgumentException('No valid fileUid given');
110  }
111 
112  // rebuild the parameter array and check if the HMAC is correct
113  $parametersEncoded = implode('', $parametersArray);
114 
115  /* For backwards compatibility the HMAC is transported within the md5 param */
116  $hmacParameter = isset($this->request->getQueryParams()['md5']) ? $this->request->getQueryParams()['md5'] : null;
117  $hmac = GeneralUtility::hmac(implode('|', array($fileUid, $parametersEncoded)));
118  if ($hmac !== $hmacParameter) {
119  throw new \InvalidArgumentException('hash does not match');
120  }
121 
122  // decode the parameters Array
123  $parameters = unserialize(base64_decode($parametersEncoded));
124  foreach ($parameters as $parameterName => $parameterValue) {
125  $this->{$parameterName} = $parameterValue;
126  }
127 
129  $this->file = ResourceFactory::getInstance()->getFileObject((int)$fileUid);
130  } else {
131  $this->file = ResourceFactory::getInstance()->retrieveFileOrFolderObject($fileUid);
132  }
133  $this->frame = isset($this->request->getQueryParams()['frame']) ? $this->request->getQueryParams()['frame'] : null;
134  }
135 
142  public function main()
143  {
144  $processedImage = $this->processImage();
145  $imageTagMarkers = array(
146  '###publicUrl###' => htmlspecialchars($processedImage->getPublicUrl()),
147  '###alt###' => htmlspecialchars($this->file->getProperty('alternative') ?: $this->title),
148  '###title###' => htmlspecialchars($this->file->getProperty('title') ?: $this->title),
149  '###width###' => $processedImage->getProperty('width'),
150  '###height###' => $processedImage->getProperty('height')
151  );
152  $this->imageTag = str_replace(array_keys($imageTagMarkers), array_values($imageTagMarkers), $this->imageTag);
153  $markerArray = array(
154  '###TITLE###' => ($this->file->getProperty('title') ?: $this->title),
155  '###IMAGE###' => $this->imageTag,
156  '###BODY###' => $this->bodyTag
157  );
158 
159  $this->content = str_replace(array_keys($markerArray), array_values($markerArray), $this->content);
160  }
161 
167  protected function processImage()
168  {
169  if (strstr($this->width . $this->height, 'm')) {
170  $max = 'm';
171  } else {
172  $max = '';
173  }
174  $this->height = MathUtility::forceIntegerInRange($this->height, 0);
175  $this->width = MathUtility::forceIntegerInRange($this->width, 0) . $max;
176 
177  $processingConfiguration = array(
178  'width' => $this->width,
179  'height' => $this->height,
180  'frame' => $this->frame,
181 
182  );
183  return $this->file->process('Image.CropScaleMask', $processingConfiguration);
184  }
185 
194  {
195  $this->request = $request;
196 
197  try {
198  $this->initialize();
199  $this->main();
200  $response->getBody()->write($this->content);
201  return $response;
202  } catch (\InvalidArgumentException $e) {
203  // add a 410 "gone" if invalid parameters given
204  return $response->withStatus(410);
205  } catch (Exception $e) {
206  return $response->withStatus(404);
207  }
208  }
209 }