7bcb266d23068fa3272472f6c0fdc915dc323cf9
[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 \TYPO3\CMS\Core\Utility\HttpUtility;
18 use \TYPO3\CMS\Core\Utility\GeneralUtility;
19 use \TYPO3\CMS\Core\Utility\MathUtility;
20
21 /**
22 * Script Class, generating the page output.
23 * Instantiated in the bottom of this script.
24 *
25 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
26 */
27 class ShowImageController {
28
29 // Parameters loaded into these internal variables:
30 /**
31 * @var \TYPO3\CMS\Core\Resource\File
32 */
33 protected $file;
34
35 /**
36 * @var int
37 */
38 protected $width;
39
40 /**
41 * @var int
42 */
43 protected $height;
44
45 /**
46 * @var string
47 */
48 protected $sample;
49
50 /**
51 * @var string
52 */
53 protected $effects;
54
55 /**
56 * @var int
57 */
58 protected $frame;
59
60 /**
61 * @var string
62 */
63 protected $hmac;
64
65 /**
66 * @var string
67 */
68 protected $bodyTag = '<body>';
69
70 /**
71 * @var string
72 */
73 protected $wrap = '|';
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 protected $imageTag = '<img src="###publicUrl###" alt="###alt###" title="###title###" />';
97
98 /**
99 * Init function, setting the input vars in the global space.
100 *
101 * @return void
102 */
103 public function init() {
104 // Loading internal vars with the GET/POST parameters from outside:
105 $fileUid = GeneralUtility::_GP('file');
106 $this->frame = GeneralUtility::_GP('frame');
107 /* For backwards compatibility the HMAC is transported within the md5 param */
108 $this->hmac = GeneralUtility::_GP('md5');
109
110 $parametersArray = GeneralUtility::_GP('parameters');
111
112 // If no file-param or parameters are given, we must exit
113 if (!$fileUid || !isset($parametersArray) || !is_array($parametersArray)) {
114 HttpUtility::setResponseCodeAndExit(HttpUtility::HTTP_STATUS_410);
115 }
116
117 // rebuild the parameter array and check if the HMAC is correct
118 $parametersEncoded = implode('', $parametersArray);
119 $hmac = GeneralUtility::hmac(implode('|', array($fileUid, $parametersEncoded)));
120 if ($hmac !== $this->hmac) {
121 HttpUtility::setResponseCodeAndExit(HttpUtility::HTTP_STATUS_410);
122
123 }
124
125 // decode the parameters Array
126 $parameters = unserialize(base64_decode($parametersEncoded));
127 foreach ($parameters as $parameterName => $parameterValue) {
128 $this->{$parameterName} = $parameterValue;
129 }
130
131 try {
132 if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($fileUid)) {
133 $this->file = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->getFileObject((int)$fileUid);
134 } else {
135 $this->file = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->retrieveFileOrFolderObject($fileUid);
136 }
137 } catch (\TYPO3\CMS\Core\Exception $e) {
138 HttpUtility::setResponseCodeAndExit(HttpUtility::HTTP_STATUS_404);
139 }
140 }
141
142 /**
143 * Main function which creates the image if needed and outputs the HTML code for the page displaying the image.
144 * Accumulates the content in $this->content
145 *
146 * @return void
147 */
148 public function main() {
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 );
155 $this->imageTag = str_replace(array_keys($imageTagMarkers), array_values($imageTagMarkers), $this->imageTag);
156 if ($this->wrap !== '|') {
157 $wrapParts = explode('|', $this->wrap, 2);
158 $this->imageTag = $wrapParts[0] . $this->imageTag . $wrapParts[1];
159 }
160 $markerArray = array(
161 '###TITLE###' => ($this->file->getProperty('title') ?: $this->title),
162 '###IMAGE###' => $this->imageTag,
163 '###BODY###' => $this->bodyTag
164 );
165
166 $this->content = str_replace(array_keys($markerArray), array_values($markerArray), $this->content);
167
168 }
169
170 /**
171 * Does the actual image processing
172 * @return \TYPO3\CMS\Core\Resource\ProcessedFile
173 */
174 protected function processImage() {
175 if (strstr($this->width . $this->height, 'm')) {
176 $max = 'm';
177 } else {
178 $max = '';
179 }
180 $this->height = MathUtility::forceIntegerInRange($this->height, 0);
181 $this->width = MathUtility::forceIntegerInRange($this->width, 0) . $max;
182
183 $processingConfiguration = array(
184 'width' => $this->width,
185 'height' => $this->height,
186 'frame' => $this->frame,
187
188 );
189 return $this->file->process('Image.CropScaleMask', $processingConfiguration);
190 }
191 /**
192 * Outputs the content from $this->content
193 *
194 * @return void
195 */
196 public function printContent() {
197 echo $this->content;
198 HttpUtility::setResponseCodeAndExit(HttpUtility::HTTP_STATUS_200);
199 }
200
201 /**
202 *
203 * @return void
204 */
205 public function execute() {
206 $this->init();
207 $this->main();
208 $this->printContent();
209 }
210 }