2c1228ce55d53627158519f2b126ecb2269a8870
[Packages/TYPO3.CMS.git] / typo3 / sysext / rtehtmlarea / Classes / Controller / BrowseLinksController.php
1 <?php
2 namespace TYPO3\CMS\Rtehtmlarea\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 Psr\Http\Message\ServerRequestInterface;
18 use TYPO3\CMS\Backend\Utility\BackendUtility;
19 use TYPO3\CMS\Core\Page\PageRenderer;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use TYPO3\CMS\Lang\LanguageService;
22 use TYPO3\CMS\Recordlist\Controller\AbstractLinkBrowserController;
23
24 /**
25 * Extended controller for link browser
26 */
27 class BrowseLinksController extends AbstractLinkBrowserController
28 {
29 /**
30 * Active with TYPO3 Element Browser: Contains the name of the form field for which this window
31 * opens - thus allows us to make references back to the main window in which the form is.
32 * Example value: "data[pages][39][bodytext]|||tt_content|"
33 * or "data[tt_content][NEW3fba56fde763d][image]|||gif,jpg,jpeg,tif,bmp,pcx,tga,png,pdf,ai|"
34 *
35 * Values:
36 * 0: form field name reference, eg. "data[tt_content][123][image]"
37 * 1: htmlArea RTE parameters: editorNo:contentTypo3Language
38 * 2: RTE config parameters: RTEtsConfigParams
39 * 3: allowed types. Eg. "tt_content" or "gif,jpg,jpeg,tif,bmp,pcx,tga,png,pdf,ai"
40 *
41 * $pArr = explode('|', $this->bparams);
42 * $formFieldName = $pArr[0];
43 * $allowedTablesOrFileTypes = $pArr[3];
44 *
45 * @var string
46 */
47 protected $bparams;
48
49 /**
50 * @var int
51 */
52 protected $editorNo;
53
54 /**
55 * TYPO3 language code of the content language
56 *
57 * @var int
58 */
59 protected $contentTypo3Language;
60
61 /**
62 * Language service object for localization to the content language
63 *
64 * @var LanguageService
65 */
66 protected $contentLanguageService;
67
68 /**
69 * @var array
70 */
71 protected $buttonConfig = [];
72
73 /**
74 * @var array
75 */
76 protected $thisConfig = [];
77
78 /**
79 * RTE configuration
80 *
81 * @var array
82 */
83 protected $RTEProperties = [];
84
85 /**
86 * Used with the Rich Text Editor.
87 * Example value: "tt_content:NEW3fba58c969f5c:bodytext:23:text:23:"
88 *
89 * @var string
90 */
91 protected $RTEtsConfigParams;
92
93 /**
94 * @var array
95 */
96 protected $classesAnchorDefault = array();
97
98 /**
99 * @var array
100 */
101 protected $classesAnchorDefaultTitle = array();
102
103 /**
104 * @var array
105 */
106 protected $classesAnchorClassTitle = array();
107
108 /**
109 * @var array
110 */
111 protected $classesAnchorDefaultTarget = array();
112
113 /**
114 * @var array
115 */
116 protected $classesAnchorJSOptions = array();
117
118 /**
119 * @var string
120 */
121 protected $defaultLinkTarget = '';
122
123 /**
124 * @var array
125 */
126 protected $additionalAttributes = array();
127
128 /**
129 * @var string
130 */
131 protected $siteUrl = '';
132
133 /**
134 * Initialize controller
135 */
136 protected function init()
137 {
138 parent::init();
139
140 $lang = $this->getLanguageService();
141 $lang->includeLLFile('EXT:rtehtmlarea/Resources/Private/Language/locallang_browselinkscontroller.xlf');
142 $lang->includeLLFile('EXT:rtehtmlarea/Resources/Private/Language/locallang_dialogs.xlf');
143
144 $this->contentLanguageService = GeneralUtility::makeInstance(LanguageService::class);
145 }
146
147 /**
148 * @param ServerRequestInterface $request
149 */
150 protected function initVariables(ServerRequestInterface $request)
151 {
152 parent::initVariables($request);
153
154 $queryParameters = $request->getQueryParams();
155 $this->bparams = isset($queryParameters['bparams']) ? $queryParameters['bparams'] : '';
156
157 $this->siteUrl = GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
158
159 $currentLinkParts = isset($queryParameters['curUrl']) ? $queryParameters['curUrl'] : [];
160 if (isset($currentLinkParts['all'])) {
161 $currentLinkParts = GeneralUtility::get_tag_attributes($queryParameters['curUrl']['all']);
162 $currentLinkParts['url'] = htmlspecialchars_decode($currentLinkParts['href']);
163 unset($currentLinkParts['href']);
164 }
165 $this->currentLinkParts = $currentLinkParts;
166
167 // Process bparams
168 $pArr = explode('|', $this->bparams);
169 $pRteArr = explode(':', $pArr[1]);
170 $this->editorNo = $pRteArr[0];
171 $this->contentTypo3Language = $pRteArr[1];
172 $this->RTEtsConfigParams = $pArr[2];
173 if (!$this->editorNo) {
174 $this->editorNo = GeneralUtility::_GP('editorNo');
175 $this->contentTypo3Language = GeneralUtility::_GP('contentTypo3Language');
176 $this->RTEtsConfigParams = GeneralUtility::_GP('RTEtsConfigParams');
177 }
178 $pArr[1] = implode(':', array($this->editorNo, $this->contentTypo3Language));
179 $pArr[2] = $this->RTEtsConfigParams;
180 $this->bparams = implode('|', $pArr);
181
182 $this->contentLanguageService->init($this->contentTypo3Language);
183
184 $RTEtsConfigParts = explode(':', $this->RTEtsConfigParams);
185 $RTEsetup = $this->getBackendUser()->getTSConfig('RTE', BackendUtility::getPagesTSconfig($RTEtsConfigParts[5]));
186 $this->RTEProperties = $RTEsetup['properties'];
187
188 $this->thisConfig = BackendUtility::RTEsetup($this->RTEProperties, $RTEtsConfigParts[0], $RTEtsConfigParts[2], $RTEtsConfigParts[4]);
189 $this->buttonConfig = isset($this->thisConfig['buttons.']['link.'])
190 ? $this->thisConfig['buttons.']['link.']
191 : [];
192 }
193
194 /**
195 * Initialize document template object
196 *
197 * @return void
198 */
199 protected function initDocumentTemplate()
200 {
201 parent::initDocumentTemplate();
202
203 $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
204 $pageRenderer->loadRequireJsModule('TYPO3/CMS/Rtehtmlarea/RteLinkBrowser');
205 }
206
207 /**
208 * Initialize $this->currentLink and $this->currentLinkHandler
209 *
210 * @return void
211 */
212 protected function initCurrentUrl()
213 {
214 if (empty($this->currentLinkParts)) {
215 return;
216 }
217
218 if (!empty($this->currentLinkParts['class'])) {
219 // remove required classes
220 $currentClasses = GeneralUtility::trimExplode(' ', $this->currentLinkParts['class'], true);
221 if (count($currentClasses) > 1) {
222 $this->currentLinkParts['class'] = end($currentClasses);
223 }
224 }
225
226 if (empty($this->currentLinkParts['data-htmlarea-external']) && strpos($this->currentLinkParts['url'], 'mailto:') === false) {
227 // strip siteUrl prefix except for external and mail links
228 $paramsPosition = strpos($this->currentLinkParts['url'], '?');
229 if ($paramsPosition !== false) {
230 $this->currentLinkParts['url'] = substr($this->currentLinkParts['url'], $paramsPosition + 1);
231 }
232 // special treatment for page links, remove the id= part
233 $idPosition = strpos($this->currentLinkParts['url'], 'id=');
234 if ($idPosition !== false) {
235 $this->currentLinkParts['url'] = substr($this->currentLinkParts['url'], $idPosition + 3);
236 }
237
238 // in RTE the additional params are encoded directly at the end of the href part
239 // we need to split this again into dedicated fields
240 $additionalParamsPosition = strpos($this->currentLinkParts['url'], '?');
241 if ($additionalParamsPosition === false) {
242 $additionalParamsPosition = strpos($this->currentLinkParts['url'], '&');
243 }
244 if ($additionalParamsPosition !== false) {
245 $this->currentLinkParts['params'] = substr($this->currentLinkParts['url'], $additionalParamsPosition);
246 $this->currentLinkParts['url'] = substr($this->currentLinkParts['url'], 0, $additionalParamsPosition);
247 // in case the first sign was an ? override it with &
248 $this->currentLinkParts['params'][0] = '&';
249 }
250 }
251
252 parent::initCurrentUrl();
253 }
254
255 /**
256 * Renders the link attributes for the selected link handler
257 *
258 * @return string
259 */
260 public function renderLinkAttributeFields()
261 {
262 // Processing the classes configuration
263 if (!empty($this->buttonConfig['properties.']['class.']['allowedClasses'])) {
264 $classesAnchorArray = GeneralUtility::trimExplode(',', $this->buttonConfig['properties.']['class.']['allowedClasses'], true);
265 // Collecting allowed classes and configured default values
266 $classesAnchor = [
267 'all' => []
268 ];
269 $titleReadOnly = $this->buttonConfig['properties.']['title.']['readOnly']
270 || $this->buttonConfig[$this->displayedLinkHandlerId . '.']['properties.']['title.']['readOnly'];
271 if (is_array($this->RTEProperties['classesAnchor.'])) {
272 foreach ($this->RTEProperties['classesAnchor.'] as $label => $conf) {
273 if (in_array($conf['class'], $classesAnchorArray)) {
274 $classesAnchor['all'][] = $conf['class'];
275 if ($conf['type'] === $this->displayedLinkHandlerId) {
276 $classesAnchor[$conf['type']][] = $conf['class'];
277 if ($this->buttonConfig[$conf['type'] . '.']['properties.']['class.']['default'] == $conf['class']) {
278 $this->classesAnchorDefault[$conf['type']] = $conf['class'];
279 if ($conf['titleText']) {
280 $this->classesAnchorDefaultTitle[$conf['type']] = $this->contentLanguageService->sL(trim($conf['titleText']));
281 }
282 if (isset($conf['target'])) {
283 $this->classesAnchorDefaultTarget[$conf['type']] = trim($conf['target']);
284 }
285 }
286 }
287 if ($titleReadOnly && $conf['titleText']) {
288 $this->classesAnchorClassTitle[$conf['class']] = ($this->classesAnchorDefaultTitle[$conf['type']] = $this->contentLanguageService->sL(trim($conf['titleText'])));
289 }
290 }
291 }
292 }
293 if (isset($this->linkAttributeValues['class'])
294 && isset($classesAnchor[$this->displayedLinkHandlerId])
295 && !in_array($this->linkAttributeValues['class'], $classesAnchor[$this->displayedLinkHandlerId], true)
296 ) {
297 unset($this->linkAttributeValues['class']);
298 }
299 // Constructing the class selector options
300 foreach ($classesAnchorArray as $class) {
301 if (!in_array($class, $classesAnchor['all']) || in_array($class, $classesAnchor['all']) && is_array($classesAnchor[$this->displayedLinkHandlerId]) && in_array($class, $classesAnchor[$this->displayedLinkHandlerId])) {
302 $selected = '';
303 if ($this->linkAttributeValues['class'] === $class || !$this->linkAttributeValues['class'] && $this->classesAnchorDefault[$this->displayedLinkHandlerId] == $class) {
304 $selected = 'selected="selected"';
305 }
306 $classLabel = !empty($this->RTEProperties['classes.'][$class . '.']['name'])
307 ? $this->getPageConfigLabel($this->RTEProperties['classes.'][$class . '.']['name'], 0)
308 : $class;
309 $classStyle = !empty($this->RTEProperties['classes.'][$class . '.']['value'])
310 ? $this->RTEProperties['classes.'][$class . '.']['value']
311 : '';
312 $this->classesAnchorJSOptions[$this->displayedLinkHandlerId] .= '<option ' . $selected . ' value="' . $class . '"' . ($classStyle ? ' style="' . $classStyle . '"' : '') . '>' . $classLabel . '</option>';
313 }
314 }
315 if ($this->classesAnchorJSOptions[$this->displayedLinkHandlerId] && !($this->buttonConfig['properties.']['class.']['required'] || $this->buttonConfig[$this->displayedLinkHandlerId . '.']['properties.']['class.']['required'])) {
316 $selected = '';
317 if (!$this->linkAttributeValues['class'] && !$this->classesAnchorDefault[$this->displayedLinkHandlerId]) {
318 $selected = 'selected="selected"';
319 }
320 $this->classesAnchorJSOptions[$this->displayedLinkHandlerId] = '<option ' . $selected . ' value=""></option>' . $this->classesAnchorJSOptions[$this->displayedLinkHandlerId];
321 }
322 }
323 // Default target
324 $this->defaultLinkTarget = $this->classesAnchorDefault[$this->displayedLinkHandlerId] && $this->classesAnchorDefaultTarget[$this->displayedLinkHandlerId]
325 ? $this->classesAnchorDefaultTarget[$this->displayedLinkHandlerId]
326 : (isset($this->buttonConfig[$this->displayedLinkHandlerId . '.']['properties.']['target.']['default'])
327 ? $this->buttonConfig[$this->displayedLinkHandlerId . '.']['properties.']['target.']['default']
328 : (isset($this->buttonConfig['properties.']['target.']['default'])
329 ? $this->buttonConfig['properties.']['target.']['default']
330 : ''));
331 // Initializing additional attributes
332 if ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['rtehtmlarea']['plugins']['TYPO3Link']['additionalAttributes']) {
333 $addAttributes = GeneralUtility::trimExplode(',', $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['rtehtmlarea']['plugins']['TYPO3Link']['additionalAttributes'], true);
334 foreach ($addAttributes as $attribute) {
335 $this->additionalAttributes[$attribute] = isset($this->linkAttributeValues[$attribute]) ? $this->linkAttributeValues[$attribute] : '';
336 }
337 }
338 return parent::renderLinkAttributeFields();
339 }
340
341 /**
342 * Localize a label obtained from Page TSConfig
343 *
344 * @param string $string The label to be localized
345 * @param bool $JScharCode If needs to be converted to an array of char numbers
346 * @return string Localized string
347 */
348 public function getPageConfigLabel($string, $JScharCode = true)
349 {
350 if (substr($string, 0, 4) !== 'LLL:') {
351 $label = $string;
352 } else {
353 $label = $this->getLanguageService()->sL(trim($string));
354 }
355 $label = str_replace('"', '\\"', str_replace('\\\'', '\'', $label));
356 return $JScharCode ? GeneralUtility::quoteJSvalue($label) : $label;
357 }
358
359 /**
360 * @return string
361 */
362 protected function renderCurrentUrl()
363 {
364 $removeLink = ' <a href="#" class="btn btn-default t3js-removeCurrentLink">' . $this->getLanguageService()->getLL('removeLink', true) . '</a>';
365 return '
366 <div class="link-browser-section link-browser-current-link">
367 <strong>' .
368 $this->getLanguageService()->getLL('currentLink', true) .
369 ': ' .
370 htmlspecialchars($this->currentLinkHandler->formatCurrentUrl()) .
371 '</strong>' .
372 '<span class="pull-right">' . $removeLink . '</span>' .
373 '</div>';
374 }
375
376 /**
377 * Get the allowed items or tabs
378 *
379 * @return string[]
380 */
381 protected function getAllowedItems()
382 {
383 $allowedItems = parent::getAllowedItems();
384
385 $blindLinkOptions = isset($this->thisConfig['blindLinkOptions'])
386 ? GeneralUtility::trimExplode(',', $this->thisConfig['blindLinkOptions'], true)
387 : [];
388 $allowedItems = array_diff($allowedItems, $blindLinkOptions);
389
390 if (is_array($this->buttonConfig['options.']) && $this->buttonConfig['options.']['removeItems']) {
391 $allowedItems = array_diff($allowedItems, GeneralUtility::trimExplode(',', $this->buttonConfig['options.']['removeItems'], true));
392 }
393
394 return $allowedItems;
395 }
396
397 /**
398 * Get the allowed link attributes
399 *
400 * @return string[]
401 */
402 protected function getAllowedLinkAttributes()
403 {
404 $allowedLinkAttributes = parent::getAllowedLinkAttributes();
405
406 $blindLinkFields = isset($this->thisConfig['blindLinkFields'])
407 ? GeneralUtility::trimExplode(',', $this->thisConfig['blindLinkFields'], true)
408 : [];
409 $allowedLinkAttributes = array_diff($allowedLinkAttributes, $blindLinkFields);
410
411 return $allowedLinkAttributes;
412 }
413
414 /**
415 * Create an array of link attribute field rendering definitions
416 *
417 * @return string[]
418 */
419 protected function getLinkAttributeFieldDefinitions()
420 {
421 $fieldRenderingDefinitions = parent::getLinkAttributeFieldDefinitions();
422 $fieldRenderingDefinitions['title'] = $this->getTitleField();
423 $fieldRenderingDefinitions['class'] = $this->getClassField();
424 $fieldRenderingDefinitions['target'] = $this->getTargetField();
425 $fieldRenderingDefinitions['rel'] = $this->getRelField();
426 if (empty($this->buttonConfig['queryParametersSelector.']['enabled'])) {
427 unset($fieldRenderingDefinitions['params']);
428 }
429 return $fieldRenderingDefinitions;
430 }
431
432 /**
433 * Add rel field
434 *
435 * @return string
436 */
437 protected function getRelField()
438 {
439 if (empty($this->buttonConfig['relAttribute.']['enabled'])) {
440 return '';
441 }
442 // @todo add rel to attributes
443 $currentRel = $this->displayedLinkHandler === $this->currentLinkHandler && !empty($this->currentLinkParts)
444 ? $this->linkAttributeValues['rel']
445 : '';
446 // @todo define label "linkRelationship" below in xlf
447 return '
448 <form action="" name="lrelform" id="lrelform" class="t3js-dummyform form-horizontal">
449 <div class="form-group form-group-sm">
450 <label class="col-xs-4 control-label">' .
451 $this->getLanguageService()->getLL('linkRelationship', true) .
452 '</label>
453 <div class="col-xs-8">
454 <input type="text" name="lrel" class="form-control" value="' . $currentRel . '" />
455 </div>
456 </div>
457 </form>
458 ';
459 }
460
461 /**
462 * Add target selector
463 *
464 * @return string
465 */
466 protected function getTargetField()
467 {
468 $targetSelectorConfig = array();
469 if (is_array($this->buttonConfig['targetSelector.'])) {
470 $targetSelectorConfig = $this->buttonConfig['targetSelector.'];
471 }
472 $target = $this->linkAttributeValues['target'] ?: $this->defaultLinkTarget;
473 $lang = $this->getLanguageService();
474 $targetSelector = '';
475
476 if (!$targetSelectorConfig['disabled']) {
477 $targetSelector = '
478 <select name="ltarget_type" class="t3js-targetPreselect form-control">
479 <option value=""></option>
480 <option value="_top">' . $lang->getLL('top', true) . '</option>
481 <option value="_blank">' . $lang->getLL('newWindow', true) . '</option>
482 </select>
483 ';
484 }
485
486 return '
487 <form action="" name="ltargetform" id="ltargetform" class="t3js-dummyform form-horizontal">
488 <div class="form-group form-group-sm" ' . ($targetSelectorConfig['disabled'] ? ' style="display: none;"' : '') . '>
489 <label class="col-xs-4 control-label">' . $lang->getLL('target', true) . '</label>
490 <div class="col-xs-3">
491 <input type="text" name="ltarget" class="t3js-linkTarget form-control"
492 value="' . htmlspecialchars($target) . '" />
493 </div>
494 <div class="col-xs-3">
495 ' . $targetSelector . '
496 </div>
497 </div>
498 </form>
499 ';
500 }
501
502 /**
503 * Add title selector
504 *
505 * @return string
506 */
507 protected function getTitleField()
508 {
509 if ($this->linkAttributeValues['title']) {
510 $title = $this->linkAttributeValues['title'];
511 } else {
512 $title = !$this->classesAnchorDefault[$this->displayedLinkHandlerId] ? '' : $this->classesAnchorDefaultTitle[$this->displayedLinkHandlerId];
513 }
514 if (isset($this->buttonConfig[$this->displayedLinkHandlerId . '.']['properties.']['title.']['readOnly'])) {
515 $readOnly = (bool)$this->buttonConfig[$this->displayedLinkHandlerId . '.']['properties.']['title.']['readOnly'];
516 } else {
517 $readOnly = isset($this->buttonConfig['properties.']['title.']['readOnly'])
518 ? (bool)$this->buttonConfig['properties.']['title.']['readOnly']
519 : false;
520 }
521
522 if ($readOnly) {
523 $currentClass = $this->linkAttributeFields['class'];
524 if (!$currentClass) {
525 $currentClass = empty($this->classesAnchorDefault[$this->displayedLinkHandlerId]) ? '' : $this->classesAnchorDefault[$this->displayedLinkHandlerId];
526 }
527 $title = $currentClass
528 ? $this->classesAnchorClassTitle[$currentClass]
529 : $this->classesAnchorDefaultTitle[$this->displayedLinkHandlerId];
530 }
531 return '
532 <form action="" name="ltitleform" id="ltitleform" class="t3js-dummyform form-horizontal">
533 <div class="form-group form-group-sm">
534 <label class="col-xs-4 control-label">
535 ' . $this->getLanguageService()->getLL('anchor_title', true) . '
536 </label>
537 <div class="col-xs-8">
538 <span style="display: ' . ($readOnly ? 'none' : 'inline') . ';">
539 <input type="text" name="ltitle" class="form-control"
540 value="' . htmlspecialchars($title) . '" />
541 </span>
542 <span id="rtehtmlarea-browse-links-title-readonly"
543 style="display: ' . ($readOnly ? 'inline' : 'none') . ';">
544 ' . htmlspecialchars($title) . '</span>
545 </div>
546 </div>
547 </form>
548 ';
549 }
550
551 /**
552 * Return html code for the class selector
553 *
554 * @return string the html code to be added to the form
555 */
556 protected function getClassField()
557 {
558 $selectClass = '';
559 if ($this->classesAnchorJSOptions[$this->displayedLinkHandlerId]) {
560 $selectClass = '
561 <form action="" name="lclassform" id="lclassform" class="t3js-dummyform">
562 <table border="0" cellpadding="2" cellspacing="1" id="typo3-linkClass">
563 <tr>
564 <td style="width: 96px;">' . $this->getLanguageService()->getLL('anchor_class', true) . '</td>
565 <td><select name="lclass" class="t3js-class-selector form-control">
566 ' . $this->classesAnchorJSOptions[$this->displayedLinkHandlerId] . '
567 </select></td>
568 </tr>
569 </table>
570 </form>
571 ';
572 }
573 return $selectClass;
574 }
575
576 /**
577 * Return the ID of current page
578 *
579 * @return int
580 */
581 protected function getCurrentPageId()
582 {
583 return explode(':', $this->RTEtsConfigParams)[5];
584 }
585
586 /**
587 * Retrieve the configuration
588 *
589 * This is only used by RTE currently.
590 *
591 * @return array
592 */
593 public function getConfiguration()
594 {
595 return $this->buttonConfig;
596 }
597
598 /**
599 * Get attributes for the body tag
600 *
601 * @return string[] Array of body-tag attributes
602 */
603 protected function getBodyTagAttributes()
604 {
605 $parameters = parent::getBodyTagAttributes();
606 $parameters['data-site-url'] = $this->siteUrl;
607 return $parameters;
608 }
609
610 /**
611 * @param array $overrides
612 *
613 * @return array Array of parameters which have to be added to URLs
614 */
615 public function getUrlParameters(array $overrides = null)
616 {
617 return [
618 'act' => isset($overrides['act']) ? $overrides['act'] : $this->displayedLinkHandlerId,
619 'bparams' => $this->bparams,
620 'editorNo' => $this->editorNo,
621 'contentTypo3Language' => $this->contentTypo3Language
622 ];
623 }
624 }