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