54bc6da1bd94cbd2ebb4e65848afc0f6405a3a18
[Packages/TYPO3.CMS.git] / typo3 / sysext / rte_ckeditor / Classes / Controller / BrowseLinksController.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\RteCKEditor\Controller;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use Psr\Http\Message\ServerRequestInterface;
19 use TYPO3\CMS\Core\Configuration\Richtext;
20 use TYPO3\CMS\Core\LinkHandling\LinkService;
21 use TYPO3\CMS\Core\Page\PageRenderer;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23 use TYPO3\CMS\Lang\LanguageService;
24 use TYPO3\CMS\Recordlist\Controller\AbstractLinkBrowserController;
25
26 /**
27 * Extended controller for link browser
28 */
29 class BrowseLinksController extends AbstractLinkBrowserController
30 {
31 /**
32 * @var string
33 */
34 protected $editorId;
35
36 /**
37 * TYPO3 language code of the content language
38 *
39 * @var int
40 */
41 protected $contentsLanguage;
42
43 /**
44 * Language service object for localization to the content language
45 *
46 * @var LanguageService
47 */
48 protected $contentLanguageService;
49
50 /**
51 * @var array
52 */
53 protected $buttonConfig = [];
54
55 /**
56 * @var array
57 */
58 protected $thisConfig = [];
59
60 /**
61 * @var array
62 */
63 protected $classesAnchorDefault = [];
64
65 /**
66 * @var array
67 */
68 protected $classesAnchorDefaultTitle = [];
69
70 /**
71 * @var array
72 */
73 protected $classesAnchorClassTitle = [];
74
75 /**
76 * @var array
77 */
78 protected $classesAnchorDefaultTarget = [];
79
80 /**
81 * @var array
82 */
83 protected $classesAnchorJSOptions = [];
84
85 /**
86 * @var string
87 */
88 protected $defaultLinkTarget = '';
89
90 /**
91 * @var array
92 */
93 protected $additionalAttributes = [];
94
95 /**
96 * @var string
97 */
98 protected $siteUrl = '';
99
100 /**
101 * Initialize controller
102 */
103 protected function init()
104 {
105 parent::init();
106
107 $this->contentLanguageService = GeneralUtility::makeInstance(LanguageService::class);
108 }
109
110 /**
111 * @param ServerRequestInterface $request
112 */
113 protected function initVariables(ServerRequestInterface $request)
114 {
115 parent::initVariables($request);
116
117 $queryParameters = $request->getQueryParams();
118
119 $this->siteUrl = GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
120
121 $this->currentLinkParts = $queryParameters['curUrl'] ?? [];
122 $this->editorId = $queryParameters['editorId'];
123 $this->contentsLanguage = $queryParameters['contentsLanguage'];
124 $this->RTEtsConfigParams = $queryParameters['RTEtsConfigParams'] ?? null;
125
126 $this->contentLanguageService->init($this->contentsLanguage);
127
128 $tcaFieldConf = ['enableRichtext' => true];
129 if (!empty($queryParameters['P']['richtextConfigurationName'])) {
130 $tcaFieldConf['richtextConfiguration'] = $queryParameters['P']['richtextConfigurationName'];
131 }
132
133 /** @var Richtext $richtextConfigurationProvider */
134 $richtextConfigurationProvider = GeneralUtility::makeInstance(Richtext::class);
135 $this->thisConfig = $richtextConfigurationProvider->getConfiguration(
136 $this->parameters['table'],
137 $this->parameters['fieldName'],
138 (int)$this->parameters['pid'],
139 $this->parameters['recordType'],
140 $tcaFieldConf
141 );
142 $this->buttonConfig = $this->thisConfig['buttons']['link'] ?? [];
143 }
144
145 /**
146 * Initialize document template object
147 */
148 protected function initDocumentTemplate()
149 {
150 parent::initDocumentTemplate();
151
152 $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
153 $pageRenderer->loadRequireJsModule(
154 'TYPO3/CMS/RteCkeditor/RteLinkBrowser',
155 'function(RteLinkBrowser) {
156 RteLinkBrowser.initialize(' . GeneralUtility::quoteJSvalue($this->editorId) . ');
157 }'
158 );
159 }
160
161 /**
162 * Initialize $this->currentLink and $this->currentLinkHandler
163 */
164 protected function initCurrentUrl()
165 {
166 if (empty($this->currentLinkParts)) {
167 return;
168 }
169
170 if (!empty($this->currentLinkParts['url'])) {
171 $linkService = GeneralUtility::makeInstance(LinkService::class);
172 $data = $linkService->resolve($this->currentLinkParts['url']);
173 $this->currentLinkParts['type'] = $data['type'];
174 unset($data['type']);
175 $this->currentLinkParts['url'] = $data;
176 }
177
178 if (!empty($this->currentLinkParts['class'])) {
179 // Only keep last class value (others are automatically added again by required option)
180 // https://review.typo3.org/#/c/29643
181 $currentClasses = GeneralUtility::trimExplode(' ', $this->currentLinkParts['class'], true);
182 if (count($currentClasses) > 1) {
183 $this->currentLinkParts['class'] = end($currentClasses);
184 }
185 }
186 parent::initCurrentUrl();
187 }
188
189 /**
190 * Renders the link attributes for the selected link handler
191 *
192 * @return string
193 */
194 public function renderLinkAttributeFields()
195 {
196 // Processing the classes configuration
197 if (!empty($this->buttonConfig['properties']['class']['allowedClasses'])) {
198 $classesAnchorArray = is_array($this->buttonConfig['properties']['class']['allowedClasses']) ? $this->buttonConfig['properties']['class']['allowedClasses'] : GeneralUtility::trimExplode(',', $this->buttonConfig['properties']['class']['allowedClasses'], true);
199 // Collecting allowed classes and configured default values
200 $classesAnchor = [
201 'all' => []
202 ];
203
204 if (is_array($this->thisConfig['classesAnchor'])) {
205 foreach ($this->thisConfig['classesAnchor'] as $label => $conf) {
206 if (in_array($conf['class'], $classesAnchorArray, true)) {
207 $classesAnchor['all'][] = $conf['class'];
208 if ($conf['type'] === $this->displayedLinkHandlerId) {
209 $classesAnchor[$conf['type']][] = $conf['class'];
210 if ($this->buttonConfig[$conf['type']]['properties']['class']['default'] == $conf['class']) {
211 $this->classesAnchorDefault[$conf['type']] = $conf['class'];
212 if ($conf['titleText']) {
213 $this->classesAnchorDefaultTitle[$conf['type']] = $this->contentLanguageService->sL(trim($conf['titleText']));
214 }
215 if (isset($conf['target'])) {
216 $this->classesAnchorDefaultTarget[$conf['type']] = trim($conf['target']);
217 }
218 }
219 }
220 if ($conf['titleText']) {
221 $this->classesAnchorClassTitle[$conf['class']] = ($this->classesAnchorDefaultTitle[$conf['type']] = $this->contentLanguageService->sL(trim($conf['titleText'])));
222 }
223 }
224 }
225 }
226 if (isset($this->linkAttributeValues['class'])
227 && isset($classesAnchor[$this->displayedLinkHandlerId])
228 && !in_array($this->linkAttributeValues['class'], $classesAnchor[$this->displayedLinkHandlerId], true)
229 ) {
230 unset($this->linkAttributeValues['class']);
231 }
232 // Constructing the class selector options
233 foreach ($classesAnchorArray as $class) {
234 if (!in_array($class, $classesAnchor['all']) || in_array($class, $classesAnchor['all']) && is_array($classesAnchor[$this->displayedLinkHandlerId]) && in_array($class, $classesAnchor[$this->displayedLinkHandlerId])) {
235 $selected = '';
236 if ($this->linkAttributeValues['class'] === $class || !$this->linkAttributeValues['class'] && $this->classesAnchorDefault[$this->displayedLinkHandlerId] == $class) {
237 $selected = 'selected="selected"';
238 }
239 $classLabel = !empty($this->thisConfig['classes'][$class]['name'])
240 ? $this->getPageConfigLabel($this->thisConfig['classes'][$class]['name'], 0)
241 : $class;
242 $classStyle = !empty($this->thisConfig['classes'][$class]['value'])
243 ? $this->thisConfig['classes'][$class]['value']
244 : '';
245 $title = $this->classesAnchorClassTitle[$class] ?? $this->classesAnchorDefaultTitle[$class] ?? '';
246 $this->classesAnchorJSOptions[$this->displayedLinkHandlerId] .= '<option ' . $selected . ' value="' . htmlspecialchars($class) . '"'
247 . ($classStyle ? ' style="' . htmlspecialchars($classStyle) . '"' : '')
248 . 'data-link-title="' . htmlspecialchars($title) . '"'
249 . '>' . htmlspecialchars($classLabel)
250 . '</option>';
251 }
252 }
253 if ($this->classesAnchorJSOptions[$this->displayedLinkHandlerId] && !($this->buttonConfig['properties']['class']['required'] || $this->buttonConfig[$this->displayedLinkHandlerId]['properties']['class']['required'])) {
254 $selected = '';
255 if (!$this->linkAttributeValues['class'] && !$this->classesAnchorDefault[$this->displayedLinkHandlerId]) {
256 $selected = 'selected="selected"';
257 }
258 $this->classesAnchorJSOptions[$this->displayedLinkHandlerId] = '<option ' . $selected . ' value=""></option>' . $this->classesAnchorJSOptions[$this->displayedLinkHandlerId];
259 }
260 }
261 // Default target
262 $this->defaultLinkTarget = $this->classesAnchorDefault[$this->displayedLinkHandlerId] && $this->classesAnchorDefaultTarget[$this->displayedLinkHandlerId]
263 ? $this->classesAnchorDefaultTarget[$this->displayedLinkHandlerId]
264 : (isset($this->buttonConfig[$this->displayedLinkHandlerId]['properties']['target']['default'])
265 ? $this->buttonConfig[$this->displayedLinkHandlerId]['properties']['target']['default']
266 : (isset($this->buttonConfig['properties']['target']['default'])
267 ? $this->buttonConfig['properties']['target']['default']
268 : ''));
269
270 // todo: find new name for this option
271 // Initializing additional attributes
272 if ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['rte_ckeditor']['plugins']['TYPO3Link']['additionalAttributes']) {
273 $addAttributes = GeneralUtility::trimExplode(',', $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['rte_ckeditor']['plugins']['TYPO3Link']['additionalAttributes'], true);
274 foreach ($addAttributes as $attribute) {
275 $this->additionalAttributes[$attribute] = $this->linkAttributeValues[$attribute] ?? '';
276 }
277 }
278 return parent::renderLinkAttributeFields();
279 }
280
281 /**
282 * Localize a label obtained from Page TSConfig
283 *
284 * @param string $string The label to be localized
285 * @param bool $JScharCode If needs to be converted to an array of char numbers
286 * @return string Localized string
287 */
288 public function getPageConfigLabel($string, $JScharCode = true)
289 {
290 if (strpos($string, 'LLL:') !== 0) {
291 $label = $string;
292 } else {
293 $label = $this->getLanguageService()->sL(trim($string));
294 }
295 $label = str_replace('"', '\\"', str_replace('\\\'', '\'', $label));
296 return $JScharCode ? GeneralUtility::quoteJSvalue($label) : $label;
297 }
298
299 /**
300 * @return string
301 */
302 protected function renderCurrentUrl()
303 {
304 $removeLink = ' <a href="#" class="t3js-removeCurrentLink">' . htmlspecialchars($this->getLanguageService()->getLL('removeLink')) . '</a>';
305 return '
306 <div class="element-browser-panel element-browser-title">' .
307 htmlspecialchars($this->getLanguageService()->getLL('currentLink')) .
308 ': ' .
309 htmlspecialchars($this->currentLinkHandler->formatCurrentUrl()) .
310 '<span class="pull-right">' . $removeLink . '</span>' .
311 '</div>';
312 }
313
314 /**
315 * Get the allowed items or tabs
316 *
317 * @return string[]
318 */
319 protected function getAllowedItems()
320 {
321 $allowedItems = parent::getAllowedItems();
322
323 $blindLinkOptions = isset($this->thisConfig['blindLinkOptions'])
324 ? GeneralUtility::trimExplode(',', $this->thisConfig['blindLinkOptions'], true)
325 : [];
326 $allowedItems = array_diff($allowedItems, $blindLinkOptions);
327
328 if (is_array($this->buttonConfig['options']) && $this->buttonConfig['options']['removeItems']) {
329 $allowedItems = array_diff($allowedItems, GeneralUtility::trimExplode(',', $this->buttonConfig['options']['removeItems'], true));
330 }
331
332 return $allowedItems;
333 }
334
335 /**
336 * Get the allowed link attributes
337 *
338 * @return string[]
339 */
340 protected function getAllowedLinkAttributes()
341 {
342 $allowedLinkAttributes = parent::getAllowedLinkAttributes();
343
344 $blindLinkFields = isset($this->thisConfig['blindLinkFields'])
345 ? GeneralUtility::trimExplode(',', $this->thisConfig['blindLinkFields'], true)
346 : [];
347 $allowedLinkAttributes = array_diff($allowedLinkAttributes, $blindLinkFields);
348
349 return $allowedLinkAttributes;
350 }
351
352 /**
353 * Create an array of link attribute field rendering definitions
354 *
355 * @return string[]
356 */
357 protected function getLinkAttributeFieldDefinitions()
358 {
359 $fieldRenderingDefinitions = parent::getLinkAttributeFieldDefinitions();
360 $fieldRenderingDefinitions['title'] = $this->getTitleField();
361 $fieldRenderingDefinitions['class'] = $this->getClassField();
362 $fieldRenderingDefinitions['target'] = $this->getTargetField();
363 $fieldRenderingDefinitions['rel'] = $this->getRelField();
364 if (empty($this->buttonConfig['queryParametersSelector']['enabled'])) {
365 unset($fieldRenderingDefinitions['params']);
366 }
367 return $fieldRenderingDefinitions;
368 }
369
370 /**
371 * Add rel field
372 *
373 * @return string
374 */
375 protected function getRelField()
376 {
377 if (empty($this->buttonConfig['relAttribute']['enabled'])) {
378 return '';
379 }
380
381 $currentRel = '';
382 if ($this->displayedLinkHandler === $this->currentLinkHandler
383 && !empty($this->currentLinkParts)
384 && isset($this->linkAttributeValues['rel'])
385 && is_string($this->linkAttributeValues['rel'])
386 ) {
387 $currentRel = $this->linkAttributeValues['rel'];
388 }
389
390 return '
391 <form action="" name="lrelform" id="lrelform" class="t3js-dummyform form-horizontal">
392 <div class="form-group form-group-sm">
393 <label class="col-xs-4 control-label">' .
394 htmlspecialchars($this->getLanguageService()->getLL('linkRelationship')) .
395 '</label>
396 <div class="col-xs-8">
397 <input type="text" name="lrel" class="form-control" value="' . htmlspecialchars($currentRel) . '" />
398 </div>
399 </div>
400 </form>
401 ';
402 }
403
404 /**
405 * Add target selector
406 *
407 * @return string
408 */
409 protected function getTargetField()
410 {
411 $targetSelectorConfig = [];
412 if (is_array($this->buttonConfig['targetSelector'])) {
413 $targetSelectorConfig = $this->buttonConfig['targetSelector'];
414 }
415 $target = $this->linkAttributeValues['target'] ?: $this->defaultLinkTarget;
416 $lang = $this->getLanguageService();
417 $targetSelector = '';
418
419 if (!$targetSelectorConfig['disabled']) {
420 $targetSelector = '
421 <select name="ltarget_type" class="t3js-targetPreselect form-control">
422 <option value=""></option>
423 <option value="_top">' . htmlspecialchars($lang->getLL('top')) . '</option>
424 <option value="_blank">' . htmlspecialchars($lang->getLL('newWindow')) . '</option>
425 </select>
426 ';
427 }
428
429 return '
430 <form action="" name="ltargetform" id="ltargetform" class="t3js-dummyform form-horizontal">
431 <div class="form-group form-group-sm" ' . ($targetSelectorConfig['disabled'] ? ' style="display: none;"' : '') . '>
432 <label class="col-xs-4 control-label">' . htmlspecialchars($lang->getLL('target')) . '</label>
433 <div class="col-xs-4">
434 <input type="text" name="ltarget" class="t3js-linkTarget form-control"
435 value="' . htmlspecialchars($target) . '" />
436 </div>
437 <div class="col-xs-4">
438 ' . $targetSelector . '
439 </div>
440 </div>
441 </form>
442 ';
443 }
444
445 /**
446 * Add title selector
447 *
448 * @return string
449 */
450 protected function getTitleField()
451 {
452 if ($this->linkAttributeValues['title']) {
453 $title = $this->linkAttributeValues['title'];
454 } else {
455 $title = $this->classesAnchorDefaultTitle[$this->displayedLinkHandlerId] ?: '';
456 }
457 if (isset($this->buttonConfig[$this->displayedLinkHandlerId]['properties']['title']['readOnly'])) {
458 $readOnly = (bool)$this->buttonConfig[$this->displayedLinkHandlerId]['properties']['title']['readOnly'];
459 } else {
460 $readOnly = isset($this->buttonConfig['properties']['title']['readOnly'])
461 ? (bool)$this->buttonConfig['properties']['title']['readOnly']
462 : false;
463 }
464
465 if ($readOnly) {
466 $currentClass = $this->linkAttributeFields['class'];
467 if (!$currentClass) {
468 $currentClass = empty($this->classesAnchorDefault[$this->displayedLinkHandlerId]) ? '' : $this->classesAnchorDefault[$this->displayedLinkHandlerId];
469 }
470 $title = $currentClass
471 ? $this->classesAnchorClassTitle[$currentClass]
472 : ($this->classesAnchorDefaultTitle[$this->displayedLinkHandlerId] ?? '');
473 }
474 return '
475 <form action="" name="ltitleform" id="ltitleform" class="t3js-dummyform form-horizontal">
476 <div class="form-group form-group-sm">
477 <label class="col-xs-4 control-label">
478 ' . htmlspecialchars($this->getLanguageService()->getLL('title')) . '
479 </label>
480 <div class="col-xs-8">
481 <input ' . ($readOnly ? 'disabled' : '') . ' type="text" name="ltitle" class="form-control t3js-linkTitle"
482 value="' . htmlspecialchars($title) . '" />
483 </div>
484 </div>
485 </form>
486 ';
487 }
488
489 /**
490 * Return html code for the class selector
491 *
492 * @return string the html code to be added to the form
493 */
494 protected function getClassField()
495 {
496 $selectClass = '';
497 if ($this->classesAnchorJSOptions[$this->displayedLinkHandlerId]) {
498 $selectClass = '
499 <form action="" name="lclassform" id="lclassform" class="t3js-dummyform form-horizontal">
500 <div class="form-group form-group-sm">
501 <label class="col-xs-4 control-label">
502 ' . htmlspecialchars($this->getLanguageService()->getLL('class')) . '
503 </label>
504 <div class="col-xs-8">
505 <select name="lclass" class="t3js-class-selector form-control">
506 ' . $this->classesAnchorJSOptions[$this->displayedLinkHandlerId] . '
507 </select>
508 </div>
509 </div>
510 </form>
511 ';
512 }
513 return $selectClass;
514 }
515
516 /**
517 * Return the ID of current page
518 *
519 * @return int
520 */
521 protected function getCurrentPageId()
522 {
523 return (int)$this->parameters['pid'];
524 }
525
526 /**
527 * Retrieve the configuration
528 *
529 * This is only used by RTE currently.
530 *
531 * @return array
532 */
533 public function getConfiguration()
534 {
535 return $this->buttonConfig;
536 }
537
538 /**
539 * Get attributes for the body tag
540 *
541 * @return string[] Array of body-tag attributes
542 */
543 protected function getBodyTagAttributes()
544 {
545 $parameters = parent::getBodyTagAttributes();
546 $parameters['data-site-url'] = $this->siteUrl;
547 $parameters['data-default-link-target'] = $this->defaultLinkTarget;
548 return $parameters;
549 }
550
551 /**
552 * @param array $overrides
553 *
554 * @return array Array of parameters which have to be added to URLs
555 */
556 public function getUrlParameters(array $overrides = null)
557 {
558 return [
559 'act' => isset($overrides['act']) ? $overrides['act'] : $this->displayedLinkHandlerId,
560 'editorId' => $this->editorId,
561 'contentsLanguage' => $this->contentsLanguage,
562 'P' => $this->parameters
563 ];
564 }
565 }