[CLEANUP] Remove deprecated filemount functions in BE_USER
[Packages/TYPO3.CMS.git] / typo3 / sysext / rtehtmlarea / Classes / Controller / ImageRenderingController.php
1 <?php
2 namespace TYPO3\CMS\Rtehtmlarea\Controller;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2014 Stanislas Rolland <typo3(arobas)sjbr.ca>
8 * Fabien Udriot <fabien.udriot@typo3.org>
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 * A copy is found in the text file GPL.txt and important notices to the license
20 * from the author is found in LICENSE.txt distributed with these scripts.
21 *
22 *
23 * This script is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * This copyright notice MUST APPEAR in all copies of the script!
29 ***************************************************************/
30
31 use TYPO3\CMS\Core\Utility\GeneralUtility;
32 use TYPO3\CMS\Core\Resource;
33
34 /**
35 * Render the image attributes and reconstruct magic images, if necessary (and possible)
36 */
37 class ImageRenderingController extends \TYPO3\CMS\Frontend\Plugin\AbstractPlugin {
38
39 /**
40 * Same as class name
41 *
42 * @var string
43 */
44 public $prefixId = 'ImageRenderingController';
45
46 /**
47 * Path to this script relative to the extension dir
48 *
49 * @var string
50 */
51 public $scriptRelPath = 'Classes/Controller/ImageRenderingController.php';
52
53 /**
54 * The extension key
55 *
56 * @var string
57 */
58 public $extKey = 'rtehtmlarea';
59
60 /**
61 * Configuration
62 *
63 * @var array
64 */
65 public $conf = array();
66
67 /**
68 * cObj object
69 *
70 * @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
71 */
72 public $cObj;
73
74 /**
75 * Returns a processed image to be displayed on the Frontend.
76 *
77 * @param string $content Content input (not used).
78 * @param array $conf TypoScript configuration
79 * @return string HTML output
80 */
81 public function renderImageAttributes($content = '', $conf) {
82
83 $imageAttributes = $this->getImageAttributes();
84
85 // It is pretty rare to be in presence of an external image as the default behaviour
86 // of the RTE is to download the external image and create a local image.
87 // However, it may happen if the RTE has the flag "disable"
88 if (!$this->isExternalImage()) {
89 $fileUid = (int)$imageAttributes['data-htmlarea-file-uid'];
90 if ($fileUid) {
91 try {
92 $file = Resource\ResourceFactory::getInstance()->getFileObject($fileUid);
93 if ($imageAttributes['src'] !== $file->getPublicUrl()) {
94 // Source file is a processed image
95 $imageConfiguration = array(
96 'width' => (int)$imageAttributes['width'],
97 'height' => (int)$imageAttributes['height']
98 );
99 $processedFile = $this->getMagicImageService()->createMagicImage($file, $imageConfiguration);
100 $additionalAttributes = array(
101 'src' => $processedFile->getPublicUrl(),
102 'title' => $imageAttributes['title'] ?: $file->getProperty('title'),
103 'alt' => $imageAttributes['alt'] ?: $file->getProperty('alternative'),
104 'width' => $processedFile->getProperty('width'),
105 'height' => $processedFile->getProperty('height'),
106 );
107 $imageAttributes = array_merge($imageAttributes, $additionalAttributes);
108 }
109 } catch (Resource\Exception\FileDoesNotExistException $fileDoesNotExistException) {
110 // Log the fact the file could not be retrieved.
111 $message = sprintf('I could not find file with uid "%s"', $fileUid);
112 $this->getLogger()->error($message);
113 }
114 }
115 }
116 return '<img ' . GeneralUtility::implodeAttributes($imageAttributes, TRUE, TRUE) . ' />';
117 }
118
119 /**
120 * Returns a sanitizes array of attributes out of $this->cObj
121 *
122 * @return array
123 */
124 protected function getImageAttributes() {
125 return $this->cObj->parameters;
126 }
127
128 /**
129 * Instantiates and prepares the Magic Image service.
130 *
131 * @return \TYPO3\CMS\Core\Resource\Service\MagicImageService
132 */
133 protected function getMagicImageService() {
134
135 /** @var $magicImageService Resource\Service\MagicImageService */
136 $magicImageService = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\Service\\MagicImageService');
137
138 // Get RTE configuration
139 $pageTSConfig = $this->getFrontendObject()->getPagesTSconfig();
140 if (is_array($pageTSConfig) && is_array($pageTSConfig['RTE.'])) {
141 $magicImageService->setMagicImageMaximumDimensions($pageTSConfig['RTE.']);
142 }
143
144 return $magicImageService;
145 }
146
147 /**
148 * Tells whether the image URL is found to be "external".
149 *
150 * @return bool
151 */
152 protected function isExternalImage() {
153 $srcAbsoluteUrl = $this->cObj->parameters['src'];
154 return strtolower(substr($srcAbsoluteUrl, 0, 4)) === 'http' || substr($srcAbsoluteUrl, 0, 2) === '//';
155 }
156
157 /**
158 * @return \TYPO3\CMS\Core\Log\Logger
159 */
160 protected function getLogger() {
161
162 /** @var $logManager \TYPO3\CMS\Core\Log\LogManager */
163 $logManager = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\LogManager');
164
165 return $logManager->getLogger(get_class($this));
166 }
167
168 /**
169 * Returns an instance of the Frontend object.
170 *
171 * @return \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
172 */
173 protected function getFrontendObject() {
174 return $GLOBALS['TSFE'];
175 }
176 }