9c8869f5f9b68e4a85ab7ae10b7ee0ddb2d4112f
[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['data-htmlarea-external'])) {
219 // strip siteUrl prefix except for external and mail links
220 if (strpos($this->currentLinkParts['url'], 'mailto:') === false) {
221 $paramsPosition = strpos($this->currentLinkParts['url'], '?');
222 if ($paramsPosition !== false) {
223 $this->currentLinkParts['url'] = substr($this->currentLinkParts['url'], $paramsPosition + 1);
224 }
225 }
226 // special treatment for page links, remove the id= part
227 $idPosition = strpos($this->currentLinkParts['url'], 'id=');
228 if ($idPosition !== false) {
229 $this->currentLinkParts['url'] = substr($this->currentLinkParts['url'], $idPosition + 3);
230 }
231
232 // in RTE the additional params are encoded directly at the end of the href part
233 // we need to split this again into dedicated fields
234 $additionalParamsPosition = strpos($this->currentLinkParts['url'], '?');
235 if ($additionalParamsPosition === false) {
236 $additionalParamsPosition = strpos($this->currentLinkParts['url'], '&');
237 }
238 if ($additionalParamsPosition !== false) {
239 $this->currentLinkParts['params'] = substr($this->currentLinkParts['url'], $additionalParamsPosition);
240 $this->currentLinkParts['url'] = substr($this->currentLinkParts['url'], 0, $additionalParamsPosition);
241 // in case the first sign was an ? override it with &
242 $this->currentLinkParts['params'][0] = '&';
243 }
244 }
245
246 parent::initCurrentUrl();
247 }
248
249 /**
250 * Renders the link attributes for the selected link handler
251 *
252 * @return string
253 */
254 public function renderLinkAttributeFields()
255 {
256 // Processing the classes configuration
257 if (!empty($this->buttonConfig['properties.']['class.']['allowedClasses'])) {
258 $classesAnchorArray = GeneralUtility::trimExplode(',', $this->buttonConfig['properties.']['class.']['allowedClasses'], true);
259 // Collecting allowed classes and configured default values
260 $classesAnchor = [
261 'all' => []
262 ];
263 $titleReadOnly = $this->buttonConfig['properties.']['title.']['readOnly']
264 || $this->buttonConfig[$this->displayedLinkHandlerId . '.']['properties.']['title.']['readOnly'];
265 if (is_array($this->RTEProperties['classesAnchor.'])) {
266 foreach ($this->RTEProperties['classesAnchor.'] as $label => $conf) {
267 if (in_array($conf['class'], $classesAnchorArray)) {
268 $classesAnchor['all'][] = $conf['class'];
269 if ($conf['type'] === $this->displayedLinkHandlerId) {
270 $classesAnchor[$conf['type']][] = $conf['class'];
271 if ($this->buttonConfig[$conf['type'] . '.']['properties.']['class.']['default'] == $conf['class']) {
272 $this->classesAnchorDefault[$conf['type']] = $conf['class'];
273 if ($conf['titleText']) {
274 $this->classesAnchorDefaultTitle[$conf['type']] = $this->contentLanguageService->sL(trim($conf['titleText']));
275 }
276 if (isset($conf['target'])) {
277 $this->classesAnchorDefaultTarget[$conf['type']] = trim($conf['target']);
278 }
279 }
280 }
281 if ($titleReadOnly && $conf['titleText']) {
282 $this->classesAnchorClassTitle[$conf['class']] = ($this->classesAnchorDefaultTitle[$conf['type']] = $this->contentLanguageService->sL(trim($conf['titleText'])));
283 }
284 }
285 }
286 }
287 // Constructing the class selector options
288 foreach ($classesAnchorArray as $class) {
289 if (!in_array($class, $classesAnchor['all']) || in_array($class, $classesAnchor['all']) && is_array($classesAnchor[$this->displayedLinkHandlerId]) && in_array($class, $classesAnchor[$this->displayedLinkHandlerId])) {
290 $selected = '';
291 if ($this->linkAttributeValues['class'] === $class || !$this->linkAttributeValues['class'] && $this->classesAnchorDefault[$this->displayedLinkHandlerId] == $class) {
292 $selected = 'selected="selected"';
293 }
294 $classLabel = !empty($this->RTEProperties['classes.'][$class . '.']['name'])
295 ? $this->getPageConfigLabel($this->RTEProperties['classes.'][$class . '.']['name'], 0)
296 : $class;
297 $classStyle = !empty($this->RTEProperties['classes.'][$class . '.']['value'])
298 ? $this->RTEProperties['classes.'][$class . '.']['value']
299 : '';
300 $this->classesAnchorJSOptions[$this->displayedLinkHandlerId] .= '<option ' . $selected . ' value="' . $class . '"' . ($classStyle ? ' style="' . $classStyle . '"' : '') . '>' . $classLabel . '</option>';
301 }
302 }
303 if ($this->classesAnchorJSOptions[$this->displayedLinkHandlerId] && !($this->buttonConfig['properties.']['class.']['required'] || $this->buttonConfig[$this->displayedLinkHandlerId . '.']['properties.']['class.']['required'])) {
304 $selected = '';
305 if (!$this->linkAttributeValues['class'] && !$this->classesAnchorDefault[$this->displayedLinkHandlerId]) {
306 $selected = 'selected="selected"';
307 }
308 $this->classesAnchorJSOptions[$this->displayedLinkHandlerId] = '<option ' . $selected . ' value=""></option>' . $this->classesAnchorJSOptions[$this->displayedLinkHandlerId];
309 }
310 }
311 // Default target
312 $this->defaultLinkTarget = $this->classesAnchorDefault[$this->displayedLinkHandlerId] && $this->classesAnchorDefaultTarget[$this->displayedLinkHandlerId]
313 ? $this->classesAnchorDefaultTarget[$this->displayedLinkHandlerId]
314 : (isset($this->buttonConfig[$this->displayedLinkHandlerId . '.']['properties.']['target.']['default'])
315 ? $this->buttonConfig[$this->displayedLinkHandlerId . '.']['properties.']['target.']['default']
316 : (isset($this->buttonConfig['properties.']['target.']['default'])
317 ? $this->buttonConfig['properties.']['target.']['default']
318 : ''));
319 // Initializing additional attributes
320 if ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['rtehtmlarea']['plugins']['TYPO3Link']['additionalAttributes']) {
321 $addAttributes = GeneralUtility::trimExplode(',', $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['rtehtmlarea']['plugins']['TYPO3Link']['additionalAttributes'], true);
322 foreach ($addAttributes as $attribute) {
323 $this->additionalAttributes[$attribute] = isset($this->linkAttributeValues[$attribute]) ? $this->linkAttributeValues[$attribute] : '';
324 }
325 }
326 return parent::renderLinkAttributeFields();
327 }
328
329 /**
330 * Localize a label obtained from Page TSConfig
331 *
332 * @param string $string The label to be localized
333 * @param bool $JScharCode If needs to be converted to an array of char numbers
334 * @return string Localized string
335 */
336 public function getPageConfigLabel($string, $JScharCode = true)
337 {
338 if (substr($string, 0, 4) !== 'LLL:') {
339 $label = $string;
340 } else {
341 $label = $this->getLanguageService()->sL(trim($string));
342 }
343 $label = str_replace('"', '\\"', str_replace('\\\'', '\'', $label));
344 return $JScharCode ? GeneralUtility::quoteJSvalue($label) : $label;
345 }
346
347 /**
348 * @return string
349 */
350 protected function renderCurrentUrl()
351 {
352 $removeLink = '<a href="#" class="btn btn-default t3js-removeCurrentLink">' . $this->getLanguageService()->getLL('removeLink', true) . '</a>';
353 return '
354 <table border="0" cellpadding="0" cellspacing="0" id="typo3-curUrl">
355 <tr>
356 <td>' . $this->getLanguageService()->getLL('currentLink', true) . ': ' . htmlspecialchars($this->currentLinkHandler->formatCurrentUrl()) . $removeLink . '</td>
357 </tr>
358 </table>';
359 }
360
361 /**
362 * Get the allowed items or tabs
363 *
364 * @return string[]
365 */
366 protected function getAllowedItems()
367 {
368 $allowedItems = parent::getAllowedItems();
369
370 $blindLinkOptions = isset($this->thisConfig['blindLinkOptions'])
371 ? GeneralUtility::trimExplode(',', $this->thisConfig['blindLinkOptions'], true)
372 : [];
373 $allowedItems = array_diff($allowedItems, $blindLinkOptions);
374
375 if (is_array($this->buttonConfig['options.']) && $this->buttonConfig['options.']['removeItems']) {
376 $allowedItems = array_diff($allowedItems, GeneralUtility::trimExplode(',', $this->buttonConfig['options.']['removeItems'], true));
377 }
378
379 return $allowedItems;
380 }
381
382 /**
383 * Get the allowed link attributes
384 *
385 * @return string[]
386 */
387 protected function getAllowedLinkAttributes()
388 {
389 $allowedLinkAttributes = parent::getAllowedLinkAttributes();
390
391 $blindLinkFields = isset($this->thisConfig['blindLinkFields'])
392 ? GeneralUtility::trimExplode(',', $this->thisConfig['blindLinkFields'], true)
393 : [];
394 $allowedLinkAttributes = array_diff($allowedLinkAttributes, $blindLinkFields);
395
396 return $allowedLinkAttributes;
397 }
398
399 /**
400 * Create an array of link attribute field rendering definitions
401 *
402 * @return string[]
403 */
404 protected function getLinkAttributeFieldDefinitions()
405 {
406 $fieldRenderingDefinitions = parent::getLinkAttributeFieldDefinitions();
407 $fieldRenderingDefinitions['title'] = $this->getTitleField();
408 $fieldRenderingDefinitions['class'] = $this->getClassField();
409 $fieldRenderingDefinitions['target'] = $this->getTargetField();
410 $fieldRenderingDefinitions['rel'] = $this->getRelField();
411 if (empty($this->buttonConfig['queryParametersSelector.']['enabled'])) {
412 unset($fieldRenderingDefinitions['params']);
413 }
414 return $fieldRenderingDefinitions;
415 }
416
417 /**
418 * Add rel field
419 *
420 * @return string
421 */
422 protected function getRelField()
423 {
424 if (empty($this->buttonConfig['relAttribute.']['enabled'])) {
425 return '';
426 }
427 // @todo add rel to attributes
428 $currentRel = $this->displayedLinkHandler === $this->currentLinkHandler && !empty($this->currentLinkParts)
429 ? $this->linkAttributeValues['rel']
430 : '';
431 // @todo define label "linkRelationship" below in xlf
432 return '
433 <form action="" name="lrelform" id="lrelform" class="t3js-dummyform">
434 <table border="0" cellpadding="2" cellspacing="1" id="typo3-linkRel">
435 <tr>
436 <td><label>' . $this->getLanguageService()->getLL('linkRelationship', true) . ':</label></td>
437 <td colspan="3"><input type="text" name="lrel" value="' . $currentRel . '" /></td>
438 </tr>
439 </table>
440 </form>
441 ';
442 }
443
444 /**
445 * Add target selector
446 *
447 * @return string
448 */
449 protected function getTargetField()
450 {
451 $targetSelectorConfig = array();
452 if (is_array($this->buttonConfig['targetSelector.'])) {
453 $targetSelectorConfig = $this->buttonConfig['targetSelector.'];
454 }
455 $target = $this->linkAttributeValues['target'] ?: $this->defaultLinkTarget;
456 $lang = $this->getLanguageService();
457 $targetSelector = '';
458
459 if (!$targetSelectorConfig['disabled']) {
460 $targetSelector = '
461 <select name="ltarget_type" class="t3js-targetPreselect">
462 <option value=""></option>
463 <option value="_top">' . $lang->getLL('top', true) . '</option>
464 <option value="_blank">' . $lang->getLL('newWindow', true) . '</option>
465 </select>
466 ';
467 }
468
469 return '
470 <form action="" name="ltargetform" id="ltargetform" class="t3js-dummyform">
471 <table border="0" cellpadding="2" cellspacing="1" id="typo3-linkTarget">
472 <tr' . ($targetSelectorConfig['disabled'] ? ' style="display: none;"' : '') . '>
473 <td style="width: 96px;">' . $lang->getLL('target', true) . ':</td>
474 <td>
475 <input type="text" name="ltarget" class="t3js-linkTarget" value="' . htmlspecialchars($target) . '" />
476 ' . $targetSelector . '
477 </td>
478 </tr>
479 </table>
480 </form>
481 ';
482 }
483
484 /**
485 * Add title selector
486 *
487 * @return string
488 */
489 protected function getTitleField()
490 {
491 if ($this->linkAttributeValues['title']) {
492 $title = $this->linkAttributeValues['title'];
493 } else {
494 $title = !$this->classesAnchorDefault[$this->displayedLinkHandlerId] ? '' : $this->classesAnchorDefaultTitle[$this->displayedLinkHandlerId];
495 }
496 if (isset($this->buttonConfig[$this->displayedLinkHandlerId . '.']['properties.']['title.']['readOnly'])) {
497 $readOnly = (bool)$this->buttonConfig[$this->displayedLinkHandlerId . '.']['properties.']['title.']['readOnly'];
498 } else {
499 $readOnly = isset($this->buttonConfig['properties.']['title.']['readOnly'])
500 ? (bool)$this->buttonConfig['properties.']['title.']['readOnly']
501 : false;
502 }
503
504 if ($readOnly) {
505 $currentClass = $this->linkAttributeFields['class'];
506 if (!$currentClass) {
507 $currentClass = empty($this->classesAnchorDefault[$this->displayedLinkHandlerId]) ? '' : $this->classesAnchorDefault[$this->displayedLinkHandlerId];
508 }
509 $title = $currentClass
510 ? $this->classesAnchorClassTitle[$currentClass]
511 : $this->classesAnchorDefaultTitle[$this->displayedLinkHandlerId];
512 }
513 return '
514 <form action="" name="ltitleform" id="ltitleform" class="t3js-dummyform">
515 <table border="0" cellpadding="2" cellspacing="1" id="typo3-linkTitle">
516 <tr>
517 <td style="width: 96px;"><label for="rtehtmlarea-browse-links-anchor_title" id="rtehtmlarea-browse-links-title-label">' . $this->getLanguageService()->getLL('anchor_title', true) . '</label></td>
518 <td>
519 <span id="rtehtmlarea-browse-links-title-input" style="display: ' . ($readOnly ? 'none' : 'inline') . ';">
520 <input type="text" id="rtehtmlarea-browse-links-anchor_title" name="ltitle" value="' . htmlspecialchars($title) . '" />
521 </span>
522 <span id="rtehtmlarea-browse-links-title-readonly" style="display: ' . ($readOnly ? 'inline' : 'none') . ';">' . htmlspecialchars($title) . '</span>
523 </td>
524 </tr>
525 </table>
526 </form>
527 ';
528 }
529
530 /**
531 * Return html code for the class selector
532 *
533 * @return string the html code to be added to the form
534 */
535 protected function getClassField()
536 {
537 $selectClass = '';
538 if ($this->classesAnchorJSOptions[$this->displayedLinkHandlerId]) {
539 $selectClass = '
540 <form action="" name="lclassform" id="lclassform" class="t3js-dummyform">
541 <table border="0" cellpadding="2" cellspacing="1" id="typo3-linkClass">
542 <tr>
543 <td style="width: 96px;">' . $this->getLanguageService()->getLL('anchor_class', true) . '</td>
544 <td><select name="lclass" class="t3js-class-selector">
545 ' . $this->classesAnchorJSOptions[$this->displayedLinkHandlerId] . '
546 </select></td>
547 </tr>
548 </table>
549 </form>
550 ';
551 }
552 return $selectClass;
553 }
554
555 /**
556 * Return the ID of current page
557 *
558 * @return int
559 */
560 protected function getCurrentPageId()
561 {
562 return explode(':', $this->RTEtsConfigParams)[5];
563 }
564
565 /**
566 * Retrieve the configuration
567 *
568 * This is only used by RTE currently.
569 *
570 * @return array
571 */
572 public function getConfiguration()
573 {
574 return $this->buttonConfig;
575 }
576
577 /**
578 * Get attributes for the body tag
579 *
580 * @return string[] Array of body-tag attributes
581 */
582 protected function getBodyTagAttributes()
583 {
584 $parameters = parent::getBodyTagAttributes();
585 $parameters['data-site-url'] = $this->siteUrl;
586 return $parameters;
587 }
588
589 /**
590 * @param array $overrides
591 *
592 * @return array Array of parameters which have to be added to URLs
593 */
594 public function getUrlParameters(array $overrides = null)
595 {
596 return [
597 'act' => isset($overrides['act']) ? $overrides['act'] : $this->displayedLinkHandlerId,
598 'bparams' => $this->bparams,
599 'editorNo' => $this->editorNo,
600 'contentTypo3Language' => $this->contentTypo3Language
601 ];
602 }
603 }