2 namespace TYPO3\CMS\Rtehtmlarea\Controller
;
5 * This file is part of the TYPO3 CMS project.
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.
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
14 * The TYPO3 project - inspiring people to share!
17 use Psr\Http\Message\ResponseInterface
;
18 use Psr\Http\Message\ServerRequestInterface
;
19 use TYPO3\CMS\Backend\Utility\BackendUtility
;
20 use TYPO3\CMS\Core\Resource\ResourceFactory
;
21 use TYPO3\CMS\Core\Resource\Service\MagicImageService
;
22 use TYPO3\CMS\Core\Service\DependencyOrderingService
;
23 use TYPO3\CMS\Core\Utility\GeneralUtility
;
24 use TYPO3\CMS\Recordlist\Controller\AbstractLinkBrowserController
;
25 use TYPO3\CMS\Recordlist\LinkHandler\LinkHandlerInterface
;
28 * Script class to select images in RTE
30 class SelectImageController
extends AbstractLinkBrowserController
33 * These file extensions are allowed in the "plain" image selection mode.
37 const PLAIN_MODE_IMAGE_FILE_EXTENSIONS
= 'jpg,jpeg,gif,png';
40 * Active with TYPO3 Element Browser: Contains the name of the form field for which this window
41 * opens - thus allows us to make references back to the main window in which the form is.
42 * Example value: "data[pages][39][bodytext]|||tt_content|"
43 * or "data[tt_content][NEW3fba56fde763d][image]|||gif,jpg,jpeg,tif,bmp,pcx,tga,png,pdf,ai|"
46 * 0: form field name reference, eg. "data[tt_content][123][image]"
47 * 1: htmlArea RTE parameters: editorNo:contentTypo3Language
48 * 2: RTE config parameters: RTEtsConfigParams
49 * 3: allowed types. Eg. "tt_content" or "gif,jpg,jpeg,tif,bmp,pcx,tga,png,pdf,ai"
51 * $pArr = explode('|', $this->bparams);
52 * $formFieldName = $pArr[0];
53 * $allowedTablesOrFileTypes = $pArr[3];
64 protected $RTEProperties = [];
67 * Used with the Rich Text Editor.
68 * Example value: "tt_content:NEW3fba58c969f5c:bodytext:23:text:23:"
72 protected $RTEtsConfigParams;
80 * TYPO3 language code of the content language
84 protected $contentTypo3Language;
89 protected $buttonConfig = [];
92 * Initialize controller
94 protected function init()
97 $this->getLanguageService()->includeLLFile('EXT:rtehtmlarea/Resources/Private/Language/locallang_dialogs.xlf');
101 * @param ServerRequestInterface $request
103 protected function initVariables(ServerRequestInterface
$request)
105 parent
::initVariables($request);
107 $queryParameters = $request->getQueryParams();
108 $this->bparams
= isset($queryParameters['bparams']) ?
$queryParameters['bparams'] : '';
109 $this->currentLinkParts
['currentImage'] = !empty($queryParameters['fileUid']) ?
$queryParameters['fileUid'] : 0;
112 $pArr = explode('|', $this->bparams
);
113 $pRteArr = explode(':', $pArr[1]);
114 $this->editorNo
= $pRteArr[0];
115 $this->contentTypo3Language
= $pRteArr[1];
116 $this->RTEtsConfigParams
= $pArr[2];
117 if (!$this->editorNo
) {
118 $this->editorNo
= GeneralUtility
::_GP('editorNo');
119 $this->contentTypo3Language
= GeneralUtility
::_GP('contentTypo3Language');
120 $this->RTEtsConfigParams
= GeneralUtility
::_GP('RTEtsConfigParams');
122 $pArr[1] = implode(':', array($this->editorNo
, $this->contentTypo3Language
));
123 $pArr[2] = $this->RTEtsConfigParams
;
124 $pArr[3] = $this->displayedLinkHandlerId
=== 'plain'
125 ? self
::PLAIN_MODE_IMAGE_FILE_EXTENSIONS
127 $this->bparams
= implode('|', $pArr);
129 $RTEtsConfigParts = explode(':', $this->RTEtsConfigParams
);
130 $RTEsetup = $this->getBackendUser()->getTSConfig('RTE', BackendUtility
::getPagesTSconfig($RTEtsConfigParts[5]));
131 $this->RTEProperties
= $RTEsetup['properties'];
133 $thisConfig = BackendUtility
::RTEsetup($this->RTEProperties
, $RTEtsConfigParts[0], $RTEtsConfigParts[2], $RTEtsConfigParts[4]);
134 $this->buttonConfig
= isset($thisConfig['buttons.']['image.'])
135 ?
$thisConfig['buttons.']['image.']
140 * Initialize hook objects implementing the interface
142 * @throws \UnexpectedValueException
145 protected function initHookObjects()
148 isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['RteImageSelector']['hooks'])
149 && is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['RteImageSelector']['hooks'])
151 $hooks = GeneralUtility
::makeInstance(DependencyOrderingService
::class)->orderByDependencies(
152 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['RteImageSelector']['hooks']
154 foreach ($hooks as $key => $hook) {
155 $this->hookObjects
[] = GeneralUtility
::makeInstance($hook['handler']);
161 * Reads the configured image handlers from page TSconfig
164 * @throws \UnexpectedValueException
166 protected function getLinkHandlers()
168 $imageHandler = $this->buttonConfig
['options.']['imageHandler.'];
170 foreach ($this->hookObjects
as $hookObject) {
171 if (method_exists($hookObject, 'modifyImageHandlers')) {
172 $imageHandler = $hookObject->modifyImageHandlers($imageHandler, $this->currentLinkParts
);
176 if (empty($imageHandler)) {
177 throw new \
UnexpectedValueException('No image handlers are configured. Check page TSconfig RTE.default.buttons.image.options.imageHandler.', 1455499673);
180 return $imageHandler;
184 * Initialize $this->currentLinkParts and $this->currentLinkHandler
188 protected function initCurrentUrl()
190 if (empty($this->currentLinkParts
)) {
194 $orderedHandlers = GeneralUtility
::makeInstance(DependencyOrderingService
::class)->orderByDependencies($this->linkHandlers
, 'scanBefore', 'scanAfter');
196 // find responsible handler for current image
197 foreach ($orderedHandlers as $key => $configuration) {
198 /** @var LinkHandlerInterface $handler */
199 $handler = $configuration['handlerInstance'];
200 if ($handler->canHandleLink($this->currentLinkParts
)) {
201 $this->currentLinkHandler
= $handler;
202 $this->currentLinkHandlerId
= $key;
206 // reset the image reference if we have no handler for it
207 if (!$this->currentLinkHandler
) {
208 $this->currentLinkParts
= [];
213 * Render the currently set URL
217 protected function renderCurrentUrl()
219 return '<!-- Print current URL -->
220 <table border="0" cellpadding="0" cellspacing="0" id="typo3-curUrl">
222 <td>' . htmlspecialchars($this->getLanguageService()->getLL('currentImage')) . ': ' . htmlspecialchars($this->currentLinkHandler
->formatCurrentUrl()) . '</td>
228 * Get the allowed items or tabs
232 protected function getAllowedItems()
234 $allowedItems = array_keys($this->linkHandlers
);
236 foreach ($this->hookObjects
as $hookObject) {
237 if (method_exists($hookObject, 'modifyAllowedItems')) {
238 $allowedItems = $hookObject->modifyAllowedItems($allowedItems, $this->currentLinkParts
);
242 return $allowedItems;
246 * @param array $overrides
248 * @return array Array of parameters which have to be added to URLs
250 public function getUrlParameters(array $overrides = null
)
253 'act' => isset($overrides['act']) ?
$overrides['act'] : $this->displayedLinkHandlerId
,
254 'bparams' => $this->bparams
,
255 'editorNo' => $this->editorNo
262 public function getButtonConfiguration() {
263 return $this->buttonConfig
;
269 public function getRteProperties() {
270 return $this->RTEProperties
;
274 * Compile the final tags to be inserted into RTE
276 * @param ServerRequestInterface $request
277 * @param ResponseInterface $response
278 * @return ResponseInterface
280 public function buildImageMarkup(ServerRequestInterface
$request, ResponseInterface
$response)
282 $this->initVariables($request);
283 $uidList = GeneralUtility
::_GP('uidList');
284 // handle ajax request for
285 $uids = explode('|', $uidList);
287 foreach ($uids as $uid) {
288 $fileObject = ResourceFactory
::getInstance()->getFileObject((int)$uid);
289 // Get default values for alt and title attributes from file properties
290 $altText = $fileObject->getProperty('alternative');
291 $titleText = $fileObject->getProperty('title');
292 if ($this->displayedLinkHandlerId
=== 'magic') {
293 // Create the magic image service
294 $magicImageService = GeneralUtility
::makeInstance(MagicImageService
::class);
295 $magicImageService->setMagicImageMaximumDimensions($this->RTEProperties
['default.']);
296 // Create the magic image
297 $imageConfiguration = array(
298 'width' => GeneralUtility
::_GP('cWidth'),
299 'height' => GeneralUtility
::_GP('cHeight')
301 $fileObject = $magicImageService->createMagicImage($fileObject, $imageConfiguration);
302 $width = $fileObject->getProperty('width');
303 $height = $fileObject->getProperty('height');
305 $width = $fileObject->getProperty('width');
306 $height = $fileObject->getProperty('height');
307 if (!$width ||
!$height) {
308 $filePath = $fileObject->getForLocalProcessing(false
);
309 $imageInfo = @getimagesize
($filePath);
310 $width = $imageInfo[0];
311 $height = $imageInfo[1];
314 $imageUrl = $fileObject->getPublicUrl();
315 // If file is local, make the url absolute
316 if (strpos($imageUrl, 'http') !== 0) {
317 $imageUrl = GeneralUtility
::getIndpEnv('TYPO3_SITE_URL') . $imageUrl;
319 $tags[] = '<img src="' . htmlspecialchars($imageUrl) . '" width="' . htmlspecialchars($width) . '" height="' . htmlspecialchars($height) . '"'
320 . (isset($this->buttonConfig
['properties.']['class.']['default'])
321 ?
' class="' . trim($this->buttonConfig
['properties.']['class.']['default']) . '"'
323 . ' alt = "' . ($altText ?
htmlspecialchars($altText) : '') . '"'
324 . ($titleText ?
' title="' . htmlspecialchars($titleText) . '"' : '')
325 . ' data-htmlarea-file-uid="' . (int)$uid . '" />';
327 $finalHtmlCode = implode(' ', $tags);
329 $response->getBody()->write(json_encode(['images' => $finalHtmlCode]));
334 * Return the ID of current page
337 * @throws \RuntimeException
339 protected function getCurrentPageId()
341 throw new \
RuntimeException('Invalid method call. This function is not supported for image handlers', 14554996791);