5f7bfb571a379686bccbe1c82ef4731843a00a87
[Packages/TYPO3.CMS.git] / typo3 / sysext / rtehtmlarea / Classes / SelectImage.php
1 <?php
2 namespace TYPO3\CMS\Rtehtmlarea;
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\GeneralUtility;
18 use TYPO3\CMS\Core\Resource;
19
20 /**
21 * Script Class
22 *
23 * @author Kasper Skårhøj <kasper@typo3.com>
24 */
25 class SelectImage extends \TYPO3\CMS\Recordlist\Browser\ElementBrowser {
26
27 /**
28 * These file extensions are allowed in the "plain" image selection mode.
29 *
30 * @const
31 */
32 const PLAIN_MODE_IMAGE_FILE_EXTENSIONS = 'jpg,jpeg,gif,png';
33
34 /**
35 * @var string
36 */
37 public $extKey = 'rtehtmlarea';
38
39 /**
40 * @var string
41 */
42 public $content;
43
44 public $allowedItems;
45
46 public $allowedFileTypes = array();
47
48 protected $defaultClass;
49
50 /**
51 * Relevant for RTE mode "plain": the maximum width an image must have to be selectable.
52 *
53 * @var int
54 */
55 protected $plainMaxWidth;
56
57 /**
58 * Relevant for RTE mode "plain": the maximum height an image must have to be selectable.
59 *
60 * @var int
61 */
62 protected $plainMaxHeight;
63
64 protected $imgPath;
65
66 public $editorNo;
67
68 public $sys_language_content;
69
70 public $thisConfig;
71
72 public $buttonConfig;
73
74 protected $imgObj;
75
76 /**
77 * Initialisation
78 *
79 * @return void
80 */
81 public function init() {
82 $this->initVariables();
83 $this->initConfiguration();
84 $this->initHookObjects('ext/rtehtmlarea/mod4/class.tx_rtehtmlarea_select_image.php');
85
86 $this->allowedItems = $this->getAllowedItems('magic,plain,image');
87 $this->insertImage();
88
89 $this->initDocumentTemplate();
90 }
91
92 /**
93 * Initialize class variables
94 *
95 * @return void
96 */
97 public function initVariables() {
98 parent::initVariables();
99 // Get "act"
100 $this->act = GeneralUtility::_GP('act');
101 if (!$this->act) {
102 $this->act = FALSE;
103 }
104 $this->addModifyTab = GeneralUtility::_GP('addModifyTab');
105 // Process bparams
106 $pArr = explode('|', $this->bparams);
107 $pRteArr = explode(':', $pArr[1]);
108 $this->editorNo = $pRteArr[0];
109 $this->sys_language_content = $pRteArr[1];
110 $this->RTEtsConfigParams = $pArr[2];
111 if (!$this->editorNo) {
112 $this->editorNo = GeneralUtility::_GP('editorNo');
113 $this->sys_language_content = GeneralUtility::_GP('sys_language_content');
114 $this->RTEtsConfigParams = GeneralUtility::_GP('RTEtsConfigParams');
115 }
116 $pArr[1] = implode(':', array($this->editorNo, $this->sys_language_content));
117 $pArr[2] = $this->RTEtsConfigParams;
118 if ($this->act == 'dragdrop' || $this->act == 'plain') {
119 $this->allowedFileTypes = explode(',', self::PLAIN_MODE_IMAGE_FILE_EXTENSIONS);
120 }
121 $pArr[3] = implode(',', $this->allowedFileTypes);
122 $this->bparams = implode('|', $pArr);
123 }
124
125 /**
126 * Initialize document template object
127 *
128 * @return void
129 */
130 protected function initDocumentTemplate() {
131 parent::initDocumentTemplate();
132
133 $this->doc->bodyTagId = 'typo3-browse-links-php';
134 $this->doc->bodyTagAdditions = $this->getBodyTagAdditions();
135 $this->doc->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/LegacyTree', 'function(Tree) {
136 Tree.ajaxID = "SC_alt_file_navframe::expandCollapse";
137 }');
138 $this->doc->getPageRenderer()->addCssFile($this->doc->backPath . \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extRelPath('t3skin') . 'rtehtmlarea/htmlarea.css');
139 $this->doc->getContextMenuCode();
140 }
141
142 /**
143 * Provide the additional parameters to be included in the template body tag
144 *
145 * @return string the body tag additions
146 */
147 public function getBodyTagAdditions() {
148 return 'onload="initEventListeners();"';
149 }
150
151 /**
152 * Insert the image in the editing area
153 *
154 * @return void
155 */
156 protected function insertImage() {
157 $table = htmlspecialchars(GeneralUtility::_GP('table'));
158 $uid = (int)GeneralUtility::_GP('uid');
159 if (GeneralUtility::_GP('insertImage') && $uid) {
160 /** @var $fileObject Resource\File */
161 $fileObject = Resource\ResourceFactory::getInstance()->getFileObject($uid);
162 // Get default values for alt and title attributes from file properties
163 $altText = $fileObject->getProperty('alternative');
164 $titleText = $fileObject->getProperty('title');
165 switch ($this->act) {
166 case 'magic':
167 $this->insertMagicImage($fileObject, $altText, $titleText, 'data-htmlarea-file-uid="' . $uid . '"');
168 die;
169 break;
170 case 'plain':
171 $this->insertPlainImage($fileObject, $altText, $titleText, 'data-htmlarea-file-uid="' . $uid . '"');
172 die;
173 break;
174 default:
175 // Call hook
176 foreach ($this->hookObjects as $hookObject) {
177 if (method_exists($hookObject, 'insertElement')) {
178 $hookObject->insertElement($this->act);
179 }
180 }
181 }
182 }
183 }
184
185 /**
186 * Insert a magic image
187 *
188 * @param Resource\File $fileObject: the image file
189 * @param string $altText: text for the alt attribute of the image
190 * @param string $titleText: text for the title attribute of the image
191 * @param string $additionalParams: text representing more HTML attributes to be added on the img tag
192 * @return void
193 */
194 public function insertMagicImage(Resource\File $fileObject, $altText = '', $titleText = '', $additionalParams = '') {
195 // Create the magic image service
196 /** @var $magicImageService Resource\Service\MagicImageService */
197 $magicImageService = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\Service\MagicImageService::class);
198 $magicImageService->setMagicImageMaximumDimensions($this->thisConfig);
199 // Create the magic image
200 $imageConfiguration = array(
201 'width' => GeneralUtility::_GP('cWidth'),
202 'height' => GeneralUtility::_GP('cHeight')
203 );
204 $magicImage = $magicImageService->createMagicImage($fileObject, $imageConfiguration);
205 $imageUrl = $magicImage->getPublicUrl();
206 // If file is local, make the url absolute
207 if (substr($imageUrl, 0, 4) !== 'http') {
208 $imageUrl = $this->siteURL . $imageUrl;
209 }
210 // Insert the magic image
211 $this->imageInsertJS($imageUrl, $magicImage->getProperty('width'), $magicImage->getProperty('height'), $altText, $titleText, $additionalParams);
212 }
213
214 /**
215 * Insert a plain image
216 *
217 * @param \TYPO3\CMS\Core\Resource\File $fileObject: the image file
218 * @param string $altText: text for the alt attribute of the image
219 * @param string $titleText: text for the title attribute of the image
220 * @param string $additionalParams: text representing more HTML attributes to be added on the img tag
221 * @return void
222 */
223 public function insertPlainImage(Resource\File $fileObject, $altText = '', $titleText = '', $additionalParams = '') {
224 $width = $fileObject->getProperty('width');
225 $height = $fileObject->getProperty('height');
226 if (!$width || !$height) {
227 $filePath = $fileObject->getForLocalProcessing(FALSE);
228 $imageInfo = @getimagesize($filePath);
229 $width = $imageInfo[0];
230 $height = $imageInfo[1];
231 }
232 $imageUrl = $fileObject->getPublicUrl();
233 // If file is local, make the url absolute
234 if (substr($imageUrl, 0, 4) !== 'http') {
235 $imageUrl = $this->siteURL . $imageUrl;
236 }
237 $this->imageInsertJS($imageUrl, $width, $height, $altText, $titleText, $additionalParams);
238 }
239
240 /**
241 * Echo the HTML page and JS that will insert the image
242 *
243 * @param string $url: the url of the image
244 * @param int $width: the width of the image
245 * @param int $height: the height of the image
246 * @param string $altText: text for the alt attribute of the image
247 * @param string $titleText: text for the title attribute of the image
248 * @param string $additionalParams: text representing more html attributes to be added on the img tag
249 * @return void
250 */
251 protected function imageInsertJS($url, $width, $height, $altText = '', $titleText = '', $additionalParams = '') {
252 echo '
253 <!DOCTYPE html>
254 <html>
255 <head>
256 <title>Untitled</title>
257 <script type="text/javascript">
258 /*<![CDATA[*/
259 var plugin = window.parent.RTEarea["' . $this->editorNo . '"].editor.getPlugin("TYPO3Image");
260 function insertImage(file,width,height,alt,title,additionalParams) {
261 plugin.insertImage(\'<img src="\'+file+\'" width="\'+parseInt(width)+\'" height="\'+parseInt(height)+\'"\'' . ($this->defaultClass ? '+\' class="' . $this->defaultClass . '"\'' : '') . '+(alt?\' alt="\'+alt+\'"\':\'\')+(title?\' title="\'+title+\'"\':\'\')+(additionalParams?\' \'+additionalParams:\'\')+\' />\');
262 }
263 /*]]>*/
264 </script>
265 </head>
266 <body>
267 <script type="text/javascript">
268 /*<![CDATA[*/
269 insertImage(' . GeneralUtility::quoteJSvalue($url, 1) . ',' . $width . ',' . $height . ',' . GeneralUtility::quoteJSvalue($altText, 1) . ',' . GeneralUtility::quoteJSvalue($titleText, 1) . ',' . GeneralUtility::quoteJSvalue($additionalParams, 1) . ');
270 /*]]>*/
271 </script>
272 </body>
273 </html>';
274 }
275
276 /**
277 * Generate JS code to be used on the image insert/modify dialogue
278 *
279 * @param string $act: the action to be performed
280 * @param string $editorNo: the number of the RTE instance on the page
281 * @param string $sys_language_content: the language of the content element
282 * @return string the generated JS code
283 */
284 public function getJSCode($act, $editorNo, $sys_language_content) {
285 $removedProperties = array();
286 if (is_array($this->buttonConfig['properties.'])) {
287 if ($this->buttonConfig['properties.']['removeItems']) {
288 $removedProperties = GeneralUtility::trimExplode(',', $this->buttonConfig['properties.']['removeItems'], TRUE);
289 }
290 }
291 if ($this->buttonConfig['properties.']['class.']['allowedClasses']) {
292 $classesImageArray = GeneralUtility::trimExplode(',', $this->buttonConfig['properties.']['class.']['allowedClasses'], TRUE);
293 $classesImageJSOptions = '<option value=""></option>';
294 foreach ($classesImageArray as $class) {
295 $classesImageJSOptions .= '<option value="' . $class . '">' . $class . '</option>';
296 }
297 }
298 $lockPlainWidth = 'false';
299 $lockPlainHeight = 'false';
300 if (is_array($this->thisConfig['proc.']) && $this->thisConfig['proc.']['plainImageMode']) {
301 $plainImageMode = $this->thisConfig['proc.']['plainImageMode'];
302 $lockPlainWidth = $plainImageMode == 'lockDimensions' ? 'true' : 'false';
303 $lockPlainHeight = $lockPlainWidth || $plainImageMode == 'lockRatio' || $plainImageMode == 'lockRatioWhenSmaller' ? 'true' : 'false';
304 }
305 $JScode = '
306 var plugin = window.parent.RTEarea["' . $editorNo . '"].editor.getPlugin("TYPO3Image");
307
308 function insertElement(table, uid, type, fileName, filePath, fileExt, fileIcon, action, close) {
309 return jumpToUrl(' . GeneralUtility::quoteJSvalue($this->getThisScript() . 'editorNo=') . ' + \'' . $editorNo . '\' + \'&insertImage=\' + filePath + \'&table=\' + table + \'&uid=\' + uid + \'&type=\' + type + \'bparams=\' + \'' . $this->bparams . '\');
310 }
311 function initEventListeners() {
312 require(["TYPO3/CMS/Rtehtmlarea/HTMLArea/UserAgent/UserAgent", "TYPO3/CMS/Rtehtmlarea/HTMLArea/Event/Event"], function (UserAgent, Event) {
313 if (UserAgent.isWebKit) {
314 Event.one(window.document.body, "dragend.TYPO3Image", function (event) { plugin.onDrop(event); });
315 }
316 });
317 }
318 function jumpToUrl(URL,anchor) {
319 var add_act = URL.indexOf("act=")==-1 ? "&act=' . $act . '" : "";
320 var add_editorNo = URL.indexOf("editorNo=")==-1 ? "&editorNo=' . $editorNo . '" : "";
321 var add_sys_language_content = URL.indexOf("sys_language_content=")==-1 ? "&sys_language_content=' . $sys_language_content . '" : "";
322 var RTEtsConfigParams = "&RTEtsConfigParams=' . rawurlencode($this->RTEtsConfigParams) . '";
323
324 var cur_width = selectedImageRef ? "&cWidth="+selectedImageRef.style.width : "";
325 var cur_height = selectedImageRef ? "&cHeight="+selectedImageRef.style.height : "";
326 var addModifyTab = plugin.image ? "&addModifyTab=1" : "";
327
328 var theLocation = URL+add_act+add_editorNo+add_sys_language_content+RTEtsConfigParams+addModifyTab+cur_width+cur_height+(typeof(anchor)=="string"?anchor:"");
329 window.location.href = theLocation;
330 return false;
331 }
332 function insertImage(file,width,height) {
333 plugin.insertImage(\'<img src="\'+file+\'"' . ($this->defaultClass ? ' class="' . $this->defaultClass . '"' : '') . ' width="\'+parseInt(width)+\'" height="\'+parseInt(height)+\'" />\');
334 }
335 function launchView(url) {
336 var thePreviewWindow="";
337 thePreviewWindow = window.open("' . GeneralUtility::getIndpEnv('TYPO3_SITE_URL') . TYPO3_mainDir . 'show_item.php?table="+url,"ShowItem","height=300,width=410,status=0,menubar=0,resizable=0,location=0,directories=0,scrollbars=1,toolbar=0");
338 if (thePreviewWindow && thePreviewWindow.focus) {
339 thePreviewWindow.focus();
340 }
341 }
342 function getCurrentImageRef() {
343 if (plugin.image) {
344 return plugin.image;
345 } else {
346 return null;
347 }
348 }
349 function printCurrentImageOptions() {
350 var classesImage = ' . ($this->buttonConfig['properties.']['class.']['allowedClasses'] || $this->thisConfig['classesImage'] ? 'true' : 'false') . ';
351 if (classesImage) var styleSelector=\'<select id="iClass" name="iClass" style="width:140px;">' . $classesImageJSOptions . '</select>\';
352 var floatSelector=\'<select id="iFloat" name="iFloat"><option value="">' . $GLOBALS['LANG']->getLL('notSet') . '</option><option value="none">' . $GLOBALS['LANG']->getLL('nonFloating') . '</option><option value="left">' . $GLOBALS['LANG']->getLL('left') . '</option><option value="right">' . $GLOBALS['LANG']->getLL('right') . '</option></select>\';
353 if (plugin.getButton("Language")) {
354 var languageSelector = \'<select id="iLang" name="iLang">\';
355 var options = plugin.getButton("Language").getOptions();
356 for (var i = 0, n = options.length; i < n; i++) {
357 languageSelector +=\'<option value="\' + options[i].value + \'">\' + options[i].innerHTML + \'</option>\';
358 }
359 languageSelector += \'</select>\';
360 }
361 var sz="";
362 sz+=\'<form action="" name="imageData"><table class="htmlarea-window-table">\';
363 ' . (in_array('class', $removedProperties) ? '' : '
364 if(classesImage) {
365 sz+=\'<tr><td><label for="iClass">' . $GLOBALS['LANG']->getLL('class') . ': </label></td><td>\'+styleSelector+\'</td></tr>\';
366 }') . (in_array('width', $removedProperties) ? '' : '
367 if (!(selectedImageRef && selectedImageRef.src.indexOf("RTEmagic") == -1 && ' . $lockPlainWidth . ')) {
368 sz+=\'<tr><td><label for="iWidth">' . $GLOBALS['LANG']->getLL('width') . ': </label></td><td><input type="text" id="iWidth" name="iWidth" value=""' . $GLOBALS['TBE_TEMPLATE']->formWidth(4) . ' /></td></tr>\';
369 }') . (in_array('height', $removedProperties) ? '' : '
370 if (!(selectedImageRef && selectedImageRef.src.indexOf("RTEmagic") == -1 && ' . $lockPlainHeight . ')) {
371 sz+=\'<tr><td><label for="iHeight">' . $GLOBALS['LANG']->getLL('height') . ': </label></td><td><input type="text" id="iHeight" name="iHeight" value=""' . $GLOBALS['TBE_TEMPLATE']->formWidth(4) . ' /></td></tr>\';
372 }') . (in_array('border', $removedProperties) ? '' : '
373 sz+=\'<tr><td><label for="iBorder">' . $GLOBALS['LANG']->getLL('border') . ': </label></td><td><input type="checkbox" id="iBorder" name="iBorder" value="1" /></td></tr>\';') . (in_array('float', $removedProperties) ? '' : '
374 sz+=\'<tr><td><label for="iFloat">' . $GLOBALS['LANG']->getLL('float') . ': </label></td><td>\'+floatSelector+\'</td></tr>\';') . (in_array('paddingTop', $removedProperties) ? '' : '
375 sz+=\'<tr><td><label for="iPaddingTop">' . $GLOBALS['LANG']->getLL('padding_top') . ': </label></td><td><input type="text" id="iPaddingTop" name="iPaddingTop" value=""' . $GLOBALS['TBE_TEMPLATE']->formWidth(4) . '></td></tr>\';') . (in_array('paddingRight', $removedProperties) ? '' : '
376 sz+=\'<tr><td><label for="iPaddingRight">' . $GLOBALS['LANG']->getLL('padding_right') . ': </label></td><td><input type="text" id="iPaddingRight" name="iPaddingRight" value=""' . $GLOBALS['TBE_TEMPLATE']->formWidth(4) . ' /></td></tr>\';') . (in_array('paddingBottom', $removedProperties) ? '' : '
377 sz+=\'<tr><td><label for="iPaddingBottom">' . $GLOBALS['LANG']->getLL('padding_bottom') . ': </label></td><td><input type="text" id="iPaddingBottom" name="iPaddingBottom" value=""' . $GLOBALS['TBE_TEMPLATE']->formWidth(4) . ' /></td></tr>\';') . (in_array('paddingLeft', $removedProperties) ? '' : '
378 sz+=\'<tr><td><label for="iPaddingLeft">' . $GLOBALS['LANG']->getLL('padding_left') . ': </label></td><td><input type="text" id="iPaddingLeft" name="iPaddingLeft" value=""' . $GLOBALS['TBE_TEMPLATE']->formWidth(4) . ' /></td></tr>\';') . (in_array('title', $removedProperties) ? '' : '
379 sz+=\'<tr><td><label for="iTitle">' . $GLOBALS['LANG']->getLL('title') . ': </label></td><td><input type="text" id="iTitle" name="iTitle"' . $GLOBALS['TBE_TEMPLATE']->formWidth(20) . ' /></td></tr>\';') . (in_array('alt', $removedProperties) ? '' : '
380 sz+=\'<tr><td><label for="iAlt">' . $GLOBALS['LANG']->getLL('alt') . ': </label></td><td><input type="text" id="iAlt" name="iAlt"' . $GLOBALS['TBE_TEMPLATE']->formWidth(20) . ' /></td></tr>\';') . (in_array('lang', $removedProperties) ? '' : '
381 if (plugin.getButton("Language")) {
382 sz+=\'<tr><td><label for="iLang">\' + plugin.editor.getPlugin("Language").localize(\'Language-Tooltip\') + \': </label></td><td>\' + languageSelector + \'</td></tr>\';
383 }') . (in_array('clickenlarge', $removedProperties) || in_array('data-htmlarea-clickenlarge', $removedProperties) ? '' : '
384 sz+=\'<tr><td><label for="iClickEnlarge">' . $GLOBALS['LANG']->sL('LLL:EXT:cms/locallang_ttc.xlf:image_zoom', TRUE) . ' </label></td><td><input type="checkbox" name="iClickEnlarge" id="iClickEnlarge" value="0" /></td></tr>\';') . '
385 sz+=\'<tr><td></td><td><input class="btn btn-default" type="submit" value="' . $GLOBALS['LANG']->getLL('update') . '" onClick="return setImageProperties();"></td></tr>\';
386 sz+=\'</table></form>\';
387 return sz;
388 }
389 function setImageProperties() {
390 var classesImage = ' . ($this->buttonConfig['properties.']['class.']['allowedClasses'] || $this->thisConfig['classesImage'] ? 'true' : 'false') . ';
391 if (selectedImageRef) {
392 if (document.imageData.iWidth) {
393 if (document.imageData.iWidth.value && parseInt(document.imageData.iWidth.value)) {
394 selectedImageRef.style.width = "";
395 selectedImageRef.width = parseInt(document.imageData.iWidth.value);
396 }
397 }
398 if (document.imageData.iHeight) {
399 if (document.imageData.iHeight.value && parseInt(document.imageData.iHeight.value)) {
400 selectedImageRef.style.height = "";
401 selectedImageRef.height = parseInt(document.imageData.iHeight.value);
402 }
403 }
404 if (document.imageData.iPaddingTop) {
405 if (document.imageData.iPaddingTop.value != "" && !isNaN(parseInt(document.imageData.iPaddingTop.value))) {
406 selectedImageRef.style.paddingTop = parseInt(document.imageData.iPaddingTop.value) + "px";
407 } else {
408 selectedImageRef.style.paddingTop = "";
409 }
410 }
411 if (document.imageData.iPaddingRight) {
412 if (document.imageData.iPaddingRight.value != "" && !isNaN(parseInt(document.imageData.iPaddingRight.value))) {
413 selectedImageRef.style.paddingRight = parseInt(document.imageData.iPaddingRight.value) + "px";
414 } else {
415 selectedImageRef.style.paddingRight = "";
416 }
417 }
418 if (document.imageData.iPaddingBottom) {
419 if (document.imageData.iPaddingBottom.value != "" && !isNaN(parseInt(document.imageData.iPaddingBottom.value))) {
420 selectedImageRef.style.paddingBottom = parseInt(document.imageData.iPaddingBottom.value) + "px";
421 } else {
422 selectedImageRef.style.paddingBottom = "";
423 }
424 }
425 if (document.imageData.iPaddingLeft) {
426 if (document.imageData.iPaddingLeft.value != "" && !isNaN(parseInt(document.imageData.iPaddingLeft.value))) {
427 selectedImageRef.style.paddingLeft = parseInt(document.imageData.iPaddingLeft.value) + "px";
428 } else {
429 selectedImageRef.style.paddingLeft = "";
430 }
431 }
432 if (document.imageData.iTitle) {
433 selectedImageRef.title=document.imageData.iTitle.value;
434 }
435 if (document.imageData.iAlt) {
436 selectedImageRef.alt=document.imageData.iAlt.value;
437 }
438 if (document.imageData.iBorder) {
439 selectedImageRef.style.borderStyle = "";
440 selectedImageRef.style.borderWidth = "";
441 selectedImageRef.style.border = ""; // this statement ignored by Mozilla 1.3.1
442 selectedImageRef.style.borderTopStyle = "";
443 selectedImageRef.style.borderRightStyle = "";
444 selectedImageRef.style.borderBottomStyle = "";
445 selectedImageRef.style.borderLeftStyle = "";
446 selectedImageRef.style.borderTopWidth = "";
447 selectedImageRef.style.borderRightWidth = "";
448 selectedImageRef.style.borderBottomWidth = "";
449 selectedImageRef.style.borderLeftWidth = "";
450 if(document.imageData.iBorder.checked) {
451 selectedImageRef.style.borderStyle = "solid";
452 selectedImageRef.style.borderWidth = "thin";
453 }
454 selectedImageRef.removeAttribute("border");
455 }
456 if (document.imageData.iFloat) {
457 var iFloat = document.imageData.iFloat.options[document.imageData.iFloat.selectedIndex].value;
458 if (document.all) {
459 selectedImageRef.style.styleFloat = iFloat ? iFloat : "";
460 } else {
461 selectedImageRef.style.cssFloat = iFloat ? iFloat : "";
462 }
463 }
464 if (classesImage && document.imageData.iClass) {
465 var iClass;
466 if (document.imageData.iClass.options.length > 0) {
467 iClass = document.imageData.iClass.options[document.imageData.iClass.selectedIndex].value;
468 }
469 if (iClass || (selectedImageRef.attributes["class"] && selectedImageRef.attributes["class"].value)) {
470 selectedImageRef.className = iClass;
471 } else {
472 selectedImageRef.className = "";
473 }
474 }
475 if (document.imageData.iLang) {
476 var iLang = document.imageData.iLang.options[document.imageData.iLang.selectedIndex].value;
477 var languageObject = plugin.editor.getPlugin("Language");
478 if (iLang || languageObject.getLanguageAttribute(selectedImageRef)) {
479 languageObject.setLanguageAttributes(selectedImageRef, iLang);
480 } else {
481 languageObject.setLanguageAttributes(selectedImageRef, "none");
482 }
483 }
484 if (document.imageData.iClickEnlarge) {
485 if (document.imageData.iClickEnlarge.checked) {
486 selectedImageRef.setAttribute("data-htmlarea-clickenlarge","1");
487 } else {
488 selectedImageRef.removeAttribute("data-htmlarea-clickenlarge");
489 selectedImageRef.removeAttribute("clickenlarge");
490 }
491 }
492 plugin.close();
493 }
494 return false;
495 }
496 function insertImagePropertiesInForm() {
497 var classesImage = ' . ($this->buttonConfig['properties.']['class.']['allowedClasses'] || $this->thisConfig['classesImage'] ? 'true' : 'false') . ';
498 if (selectedImageRef) {
499 var styleWidth, styleHeight, padding;
500 if (document.imageData.iWidth) {
501 styleWidth = selectedImageRef.style.width ? selectedImageRef.style.width : selectedImageRef.width;
502 styleWidth = parseInt(styleWidth);
503 if (!(isNaN(styleWidth) || styleWidth == 0)) {
504 document.imageData.iWidth.value = styleWidth;
505 }
506 }
507 if (document.imageData.iHeight) {
508 styleHeight = selectedImageRef.style.height ? selectedImageRef.style.height : selectedImageRef.height;
509 styleHeight = parseInt(styleHeight);
510 if (!(isNaN(styleHeight) || styleHeight == 0)) {
511 document.imageData.iHeight.value = styleHeight;
512 }
513 }
514 if (document.imageData.iPaddingTop) {
515 var padding = selectedImageRef.style.paddingTop ? selectedImageRef.style.paddingTop : selectedImageRef.vspace;
516 var padding = parseInt(padding);
517 if (isNaN(padding) || padding <= 0) { padding = ""; }
518 document.imageData.iPaddingTop.value = padding;
519 }
520 if (document.imageData.iPaddingRight) {
521 padding = selectedImageRef.style.paddingRight ? selectedImageRef.style.paddingRight : selectedImageRef.hspace;
522 var padding = parseInt(padding);
523 if (isNaN(padding) || padding <= 0) { padding = ""; }
524 document.imageData.iPaddingRight.value = padding;
525 }
526 if (document.imageData.iPaddingBottom) {
527 var padding = selectedImageRef.style.paddingBottom ? selectedImageRef.style.paddingBottom : selectedImageRef.vspace;
528 var padding = parseInt(padding);
529 if (isNaN(padding) || padding <= 0) { padding = ""; }
530 document.imageData.iPaddingBottom.value = padding;
531 }
532 if (document.imageData.iPaddingLeft) {
533 var padding = selectedImageRef.style.paddingLeft ? selectedImageRef.style.paddingLeft : selectedImageRef.hspace;
534 var padding = parseInt(padding);
535 if (isNaN(padding) || padding <= 0) { padding = ""; }
536 document.imageData.iPaddingLeft.value = padding;
537 }
538 if (document.imageData.iTitle) {
539 document.imageData.iTitle.value = selectedImageRef.title;
540 }
541 if (document.imageData.iAlt) {
542 document.imageData.iAlt.value = selectedImageRef.alt;
543 }
544 if (document.imageData.iBorder) {
545 if((selectedImageRef.style.borderStyle && selectedImageRef.style.borderStyle != "none" && selectedImageRef.style.borderStyle != "none none none none") || selectedImageRef.border) {
546 document.imageData.iBorder.checked = 1;
547 }
548 }
549 if (document.imageData.iFloat) {
550 var fObj=document.imageData.iFloat;
551 var value = (selectedImageRef.style.cssFloat ? selectedImageRef.style.cssFloat : selectedImageRef.style.styleFloat);
552 var l=fObj.length;
553 for (var a=0;a<l;a++) {
554 if (fObj.options[a].value == value) {
555 fObj.selectedIndex = a;
556 }
557 }
558 }
559 if (classesImage && document.imageData.iClass) {
560 var fObj=document.imageData.iClass;
561 var value=selectedImageRef.className;
562 var l=fObj.length;
563 for (var a=0;a < l; a++) {
564 if (fObj.options[a].value == value) {
565 fObj.selectedIndex = a;
566 }
567 }
568 }
569 if (document.imageData.iLang) {
570 var fObj=document.imageData.iLang;
571 var value=plugin.editor.getPlugin("Language").getLanguageAttribute(selectedImageRef);
572 for (var i = 0, n = fObj.length; i < n; i++) {
573 if (fObj.options[i].value == value) {
574 fObj.selectedIndex = i;
575 if (i) {
576 fObj.options[0].text = plugin.editor.getPlugin("Language").localize("Remove language mark");
577 }
578 }
579 }
580 }
581 if (document.imageData.iClickEnlarge) {
582 if (selectedImageRef.getAttribute("data-htmlarea-clickenlarge") == "1" || selectedImageRef.getAttribute("clickenlarge") == "1") {
583 document.imageData.iClickEnlarge.checked = 1;
584 } else {
585 document.imageData.iClickEnlarge.checked = 0;
586 }
587 }
588 return false;
589 }
590 }
591
592 var selectedImageRef = getCurrentImageRef();';
593 // Setting this to a reference to the image object.
594 return $JScode;
595 }
596
597 /**
598 * Session data for this class can be set from outside with this method.
599 * Call after init()
600 *
601 * @param array $data Session data array
602 * @return array Session data and boolean which indicates that data needs to be stored in session because it's changed
603 */
604 public function processSessionData($data) {
605 $store = FALSE;
606 if ($this->act != 'image') {
607 if (isset($this->act)) {
608 $data['act'] = $this->act;
609 $store = TRUE;
610 } else {
611 $this->act = $data['act'];
612 }
613 }
614 if (isset($this->expandFolder)) {
615 $data['expandFolder'] = $this->expandFolder;
616 $store = TRUE;
617 } else {
618 $this->expandFolder = $data['expandFolder'];
619 }
620 return array($data, $store);
621 }
622
623 /**
624 * Rich Text Editor (RTE) image selector
625 *
626 * @param bool $wiz Not used here, kept for method signature compatibility with parent class
627 * @return string Modified content variable.
628 * @return string
629 */
630 public function main_rte($wiz = FALSE) {
631 // Starting content:
632 $this->content = $this->doc->startPage($GLOBALS['LANG']->getLL('Insert Image', TRUE));
633
634 $this->content .= $this->doc->getTabMenuRaw($this->buildMenuArray($wiz, $this->allowedItems));
635 switch ($this->act) {
636 case 'image':
637 $JScode = '
638 document.write(printCurrentImageOptions());
639 insertImagePropertiesInForm();';
640 $this->content .= '<br />' . $this->doc->wrapScriptTags($JScode);
641 break;
642 case 'plain':
643
644 case 'magic':
645 // Create folder tree:
646 $foldertree = GeneralUtility::makeInstance(\TYPO3\CMS\Rtehtmlarea\FolderTree::class);
647 $foldertree->thisScript = $this->thisScript;
648 $tree = $foldertree->getBrowsableTree();
649 // Get currently selected folder
650 if (!$this->curUrlInfo['value'] || $this->curUrlInfo['act'] != $this->act) {
651 $cmpPath = '';
652 } else {
653 $cmpPath = $this->curUrlInfo['value'];
654 if (!isset($this->expandFolder)) {
655 $this->expandFolder = $cmpPath;
656 }
657 }
658 // Get the selected folder
659 $selectedFolder = FALSE;
660 if ($this->expandFolder) {
661 $fileOrFolderObject = NULL;
662 try {
663 $fileOrFolderObject = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->retrieveFileOrFolderObject($this->expandFolder);
664 } catch (\Exception $e) {
665 // No path is selected
666 }
667 if ($fileOrFolderObject instanceof \TYPO3\CMS\Core\Resource\Folder) {
668 // it's a folder
669 $selectedFolder = $fileOrFolderObject;
670 } elseif ($fileOrFolderObject instanceof \TYPO3\CMS\Core\Resource\FileInterface) {
671 // it's a file
672 try {
673 $selectedFolder = $fileOrFolderObject->getParentFolder();
674 } catch (\Exception $e) {
675 // Accessing the parent folder failed for some reason. e.g. permissions
676 }
677 }
678 }
679 // If no folder is selected, get the user's default upload folder
680 if (!$selectedFolder) {
681 try {
682 $selectedFolder = $GLOBALS['BE_USER']->getDefaultUploadFolder();
683 } catch (\Exception $e) {
684 // The configured default user folder does not exist
685 }
686 }
687 // Build the file upload and folder creation form
688 $uploadForm = '';
689 $createFolder = '';
690 if ($selectedFolder) {
691 $uploadForm = $this->uploadForm($selectedFolder);
692 $createFolder = $this->createFolder($selectedFolder);
693 }
694 // Insert the upload form on top, if so configured
695 if ($GLOBALS['BE_USER']->getTSConfigVal('options.uploadFieldsInTopOfEB')) {
696 $this->content .= $uploadForm;
697 }
698 // Render the filelist if there is a folder selected
699 $files = '';
700 if ($selectedFolder) {
701 $files = $this->TBE_expandFolder($selectedFolder, $this->act === 'plain' ? self::PLAIN_MODE_IMAGE_FILE_EXTENSIONS : $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], $GLOBALS['BE_USER']->getTSConfigVal('options.noThumbsInRTEimageSelect'));
702 }
703 // Setup filelist indexed elements:
704 $this->doc->JScode .= $this->doc->wrapScriptTags('
705 require(["TYPO3/CMS/Backend/BrowseLinks"], function(BrowseLinks) {
706 BrowseLinks.addElements(' . json_encode($this->elements) . ');
707 });');
708 // Wrap tree
709 $this->content .= '
710
711 <!--
712 Wrapper table for folder tree / file/folder list:
713 -->
714 <table border="0" cellpadding="0" cellspacing="0" id="typo3-linkFiles">
715 <tr>
716 <td class="c-wCell" valign="top">' . $this->barheader(($GLOBALS['LANG']->getLL('folderTree') . ':')) . $tree . '</td>
717 <td class="c-wCell" valign="top">' . $files . '</td>
718 </tr>
719 </table>
720 ';
721 // Add help message
722 $helpMessage = $this->getHelpMessage($this->act);
723 if ($helpMessage) {
724 $this->content .= $this->getMsgBox($helpMessage);
725 }
726 // Adding create folder + upload form if applicable
727 if (!$GLOBALS['BE_USER']->getTSConfigVal('options.uploadFieldsInTopOfEB')) {
728 $this->content .= $uploadForm;
729 }
730 $this->content .= $createFolder;
731 $this->content .= '<br />';
732 break;
733 case 'dragdrop':
734 $foldertree = GeneralUtility::makeInstance(\TYPO3\CMS\Recordlist\Tree\View\ElementBrowserFolderTreeView::class);
735 $foldertree->thisScript = $this->thisScript;
736 $foldertree->ext_noTempRecyclerDirs = TRUE;
737 $tree = $foldertree->getBrowsableTree();
738 // Get currently selected folder
739 if (!$this->curUrlInfo['value'] || $this->curUrlInfo['act'] != $this->act) {
740 $cmpPath = '';
741 } else {
742 $cmpPath = $this->curUrlInfo['value'];
743 if (!isset($this->expandFolder)) {
744 $this->expandFolder = $cmpPath;
745 }
746 }
747 $selectedFolder = FALSE;
748 if ($this->expandFolder) {
749 try {
750 $selectedFolder = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->getFolderObjectFromCombinedIdentifier($this->expandFolder);
751 } catch (\Exception $e) {
752 }
753 }
754 // Render the filelist if there is a folder selected
755 $files = '';
756 if ($selectedFolder) {
757 $files = $this->TBE_dragNDrop($selectedFolder, implode(',', $this->allowedFileTypes));
758 }
759 // Wrap tree
760 $this->content .= '<table border="0" cellpadding="0" cellspacing="0">
761 <tr>
762 <td style="vertical-align: top;">' . $this->barheader(($GLOBALS['LANG']->getLL('folderTree') . ':')) . $tree . '</td>
763 <td>&nbsp;</td>
764 <td style="vertical-align: top;">' . $files . '</td>
765 </tr>
766 </table>';
767 break;
768 default:
769 // Call hook
770 foreach ($this->hookObjects as $hookObject) {
771 $this->content .= $hookObject->getTab($this->act);
772 }
773 }
774 $this->content .= $this->doc->endPage();
775
776 // unset the default jumpToUrl() function
777 unset($this->doc->JScodeArray['jumpToUrl']);
778
779 $this->doc->JScodeArray['rtehtmlarea'] = $this->getJSCode($this->act, $this->editorNo, $this->sys_language_content);
780 $this->content = $this->doc->insertStylesAndJS($this->content);
781 return $this->content;
782 }
783
784 /**
785 * Returns an array definition of the top menu
786 *
787 * @param $wiz
788 * @param $allowedItems
789 * @return array
790 */
791 protected function buildMenuArray($wiz, $allowedItems) {
792 $menuDef = array();
793 if (in_array('image', $this->allowedItems) && ($this->act === 'image' || $this->addModifyTab)) {
794 $menuDef['image']['isActive'] = FALSE;
795 $menuDef['image']['label'] = $GLOBALS['LANG']->getLL('currentImage', TRUE);
796 $menuDef['image']['url'] = '#';
797 $menuDef['image']['addParams'] = 'onClick="jumpToUrl(' . GeneralUtility::quoteJSvalue($this->getThisScript() . 'act=image&bparams=' . $this->bparams) . ');return false;"';
798 }
799 if (in_array('magic', $this->allowedItems)) {
800 $menuDef['magic']['isActive'] = FALSE;
801 $menuDef['magic']['label'] = $GLOBALS['LANG']->getLL('magicImage', TRUE);
802 $menuDef['magic']['url'] = '#';
803 $menuDef['magic']['addParams'] = 'onClick="jumpToUrl(' . GeneralUtility::quoteJSvalue($this->getThisScript() . 'act=magic&bparams=' . $this->bparams) . ');return false;"';
804 }
805 if (in_array('plain', $this->allowedItems)) {
806 $menuDef['plain']['isActive'] = FALSE;
807 $menuDef['plain']['label'] = $GLOBALS['LANG']->getLL('plainImage', TRUE);
808 $menuDef['plain']['url'] = '#';
809 $menuDef['plain']['addParams'] = 'onClick="jumpToUrl(' . GeneralUtility::quoteJSvalue($this->getThisScript() . 'act=plain&bparams=' . $this->bparams) . ');return false;"';
810 }
811 if (in_array('dragdrop', $this->allowedItems)) {
812 $menuDef['dragdrop']['isActive'] = FALSE;
813 $menuDef['dragdrop']['label'] = $GLOBALS['LANG']->getLL('dragDropImage', TRUE);
814 $menuDef['dragdrop']['url'] = '#';
815 $menuDef['dragdrop']['addParams'] = 'onClick="jumpToUrl(' . GeneralUtility::quoteJSvalue($this->getThisScript() . 'act=dragdrop&bparams=' . $this->bparams) . ');return false;"';
816 }
817 // Call hook for extra options
818 foreach ($this->hookObjects as $hookObject) {
819 $menuDef = $hookObject->modifyMenuDefinition($menuDef);
820 }
821 // Order the menu items as specified in Page TSconfig
822 $menuDef = $this->orderMenuDefinition($menuDef);
823 // Set active menu item
824 reset($menuDef);
825 if ($this->act === FALSE || !in_array($this->act, $this->allowedItems)) {
826 $this->act = key($menuDef);
827 }
828 $menuDef[$this->act]['isActive'] = TRUE;
829 return $menuDef;
830 }
831
832 /**
833 * Initializes the configuration variables
834 *
835 * @return void
836 */
837 public function initConfiguration() {
838 $this->thisConfig = $this->getRTEConfig();
839 $this->buttonConfig = $this->getButtonConfig();
840 $this->imgPath = $this->getImgPath();
841 $this->defaultClass = $this->getDefaultClass();
842 $this->setMaximumPlainImageDimensions();
843 }
844
845 /**
846 * Get the path of the image to be inserted or modified
847 *
848 * @return string path to the image
849 */
850 protected function getImgPath() {
851 $RTEtsConfigParts = explode(':', $this->RTEtsConfigParams);
852 return $RTEtsConfigParts[6];
853 }
854
855 /**
856 * Get the configuration of the image button
857 *
858 * @return array the configuration array of the image button
859 */
860 protected function getButtonConfig() {
861 return is_array($this->thisConfig['buttons.']) && is_array($this->thisConfig['buttons.']['image.']) ? $this->thisConfig['buttons.']['image.'] : array();
862 }
863
864 /**
865 * Get the allowed items or tabs
866 *
867 * @param string $items: initial list of possible items
868 * @return array the allowed items
869 */
870 public function getAllowedItems($items) {
871 $allowedItems = explode(',', $items);
872 $clientInfo = GeneralUtility::clientInfo();
873 if ($clientInfo['BROWSER'] !== 'opera') {
874 $allowedItems[] = 'dragdrop';
875 }
876 // Call hook for extra options
877 foreach ($this->hookObjects as $hookObject) {
878 $allowedItems = $hookObject->addAllowedItems($allowedItems);
879 }
880 // Remove tab "image" if there is no current image
881 if ($this->act !== 'image' && !$this->addModifyTab) {
882 $allowedItems = array_diff($allowedItems, array('image'));
883 }
884 // Remove options according to RTE configuration
885 if (is_array($this->buttonConfig['options.']) && $this->buttonConfig['options.']['removeItems']) {
886 $allowedItems = array_diff($allowedItems, GeneralUtility::trimExplode(',', $this->buttonConfig['options.']['removeItems'], TRUE));
887 }
888 return $allowedItems;
889 }
890
891 /**
892 * Order the definition of menu items according to configured order
893 *
894 * @param array $menuDefinition: definition of menu items
895 * @return array ordered menu definition
896 */
897 public function orderMenuDefinition($menuDefinition) {
898 $orderedMenuDefinition = array();
899 if (is_array($this->buttonConfig['options.']) && $this->buttonConfig['options.']['orderItems']) {
900 $orderItems = GeneralUtility::trimExplode(',', $this->buttonConfig['options.']['orderItems'], TRUE);
901 $orderItems = array_intersect($orderItems, $this->allowedItems);
902 foreach ($orderItems as $item) {
903 $orderedMenuDefinition[$item] = $menuDefinition[$item];
904 }
905 } else {
906 $orderedMenuDefinition = $menuDefinition;
907 }
908 return $orderedMenuDefinition;
909 }
910
911 /**
912 * Get the default image class
913 *
914 * @return string the default class, if any
915 */
916 protected function getDefaultClass() {
917 $defaultClass = '';
918 if (is_array($this->buttonConfig['properties.'])) {
919 if (is_array($this->buttonConfig['properties.']['class.']) && trim($this->buttonConfig['properties.']['class.']['default'])) {
920 $defaultClass = trim($this->buttonConfig['properties.']['class.']['default']);
921 }
922 }
923 return $defaultClass;
924 }
925
926 /**
927 * Set variables for maximum plain image dimensions
928 *
929 * @return void
930 */
931 protected function setMaximumPlainImageDimensions() {
932 if (is_array($this->buttonConfig['options.']) && is_array($this->buttonConfig['options.']['plain.'])) {
933 if ($this->buttonConfig['options.']['plain.']['maxWidth']) {
934 $this->plainMaxWidth = $this->buttonConfig['options.']['plain.']['maxWidth'];
935 }
936 if ($this->buttonConfig['options.']['plain.']['maxHeight']) {
937 $this->plainMaxHeight = $this->buttonConfig['options.']['plain.']['maxHeight'];
938 }
939 }
940 if (!$this->plainMaxWidth) {
941 $this->plainMaxWidth = 640;
942 }
943 if (!$this->plainMaxHeight) {
944 $this->plainMaxHeight = 680;
945 }
946 }
947
948 /**
949 * Get the help message to be displayed on a given tab
950 *
951 * @param string $act: the identifier of the tab
952 * @return string the text of the message
953 */
954 public function getHelpMessage($act) {
955 switch ($act) {
956 case 'plain':
957 return sprintf($GLOBALS['LANG']->getLL('plainImage_msg'), $this->plainMaxWidth, $this->plainMaxHeight);
958 break;
959 case 'magic':
960 return sprintf($GLOBALS['LANG']->getLL('magicImage_msg'));
961 break;
962 default:
963 return '';
964 }
965 }
966
967 /**
968 * Checks if the given file is selectable in the file list.
969 *
970 * In "plain" RTE mode only image files with a maximum width and height are selectable.
971 *
972 * @param \TYPO3\CMS\Core\Resource\FileInterface $file
973 * @param array $imgInfo Image dimensions from \TYPO3\CMS\Core\Imaging\GraphicalFunctions::getImageDimensions()
974 * @return bool TRUE if file is selectable.
975 */
976 protected function fileIsSelectableInFileList(\TYPO3\CMS\Core\Resource\FileInterface $file, array $imgInfo) {
977 return (
978 $this->act !== 'plain'
979 || (
980 GeneralUtility::inList(self::PLAIN_MODE_IMAGE_FILE_EXTENSIONS, strtolower($file->getExtension()))
981 && $imgInfo[0] <= $this->plainMaxWidth
982 && $imgInfo[1] <= $this->plainMaxHeight
983 )
984 );
985 }
986
987 }