[BUGFIX] Allow more tags when no RTE processing is available
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Html / RteHtmlParser.php
1 <?php
2 namespace TYPO3\CMS\Core\Html;
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\Log\LoggerAwareInterface;
18 use Psr\Log\LoggerAwareTrait;
19 use TYPO3\CMS\Backend\Utility\BackendUtility;
20 use TYPO3\CMS\Core\Compatibility\PublicMethodDeprecationTrait;
21 use TYPO3\CMS\Core\Compatibility\PublicPropertyDeprecationTrait;
22 use TYPO3\CMS\Core\LinkHandling\Exception\UnknownLinkHandlerException;
23 use TYPO3\CMS\Core\LinkHandling\LinkService;
24 use TYPO3\CMS\Core\Resource;
25 use TYPO3\CMS\Core\Type\File\ImageInfo;
26 use TYPO3\CMS\Core\Utility\GeneralUtility;
27 use TYPO3\CMS\Frontend\Service\TypoLinkCodecService;
28
29 /**
30 * Class for parsing HTML for the Rich Text Editor. (also called transformations)
31 *
32 * Concerning line breaks:
33 * Regardless if LF (Unix-style) or CRLF (Windows) was put in, the HtmlParser works with LFs and migrates all
34 * line breaks to LFs internally, however when all transformations are done, all LFs are transformed to CRLFs.
35 * This means: RteHtmlParser always returns CRLFs to be maximum compatible with all formats.
36 */
37 class RteHtmlParser extends HtmlParser implements LoggerAwareInterface
38 {
39 use LoggerAwareTrait;
40 use PublicPropertyDeprecationTrait;
41 use PublicMethodDeprecationTrait;
42
43 protected $deprecatedPublicProperties = [
44 'blockElementList' => 'Using $blockElementList of class RteHtmlParser from the outside is discouraged, as this property is only used for internal storage.',
45 'recPid' => 'Using $recPid of class RteHtmlParser from the outside is discouraged, as this property is only used for internal storage.',
46 'elRef' => 'Using $elRef of class RteHtmlParser from the outside is discouraged, as this property is only used for internal storage.',
47 'tsConfig' => 'Using $tsConfig of class RteHtmlParser from the outside is discouraged, as this property is only used for internal storage.',
48 'procOptions' => 'Using $procOptions of class RteHtmlParser from the outside is discouraged, as this property is only used for internal storage.',
49 'TS_transform_db_safecounter' => 'Using $TS_transform_db_safecounter of class RteHtmlParser from the outside is discouraged, as this property is only used for internal storage.',
50 'getKeepTags_cache' => 'Using $getKeepTags_cache of class RteHtmlParser from the outside is discouraged, as this property is only used for internal storage.',
51 'allowedClasses' => 'Using $allowedClasses of class RteHtmlParser from the outside is discouraged, as this property is only used for internal storage.',
52 ];
53
54 protected $deprecatedPublicMethods = [
55 'TS_images_db' => 'Using TS_images_db() of class RteHtmlParser from the outside is discouraged, as this method is only available for internal purposes.',
56 'TS_links_db' => 'Using TS_links_db() of class RteHtmlParser from the outside is discouraged, as this method is only available for internal purposes.',
57 'TS_transform_db' => 'Using TS_transform_db() of class RteHtmlParser from the outside is discouraged, as this method is only available for internal purposes.',
58 'TS_transform_rte' => 'Using TS_transform_rte() of class RteHtmlParser from the outside is discouraged, as this method is only available for internal purposes.',
59 'HTMLcleaner_db' => 'Using HTMLcleaner_db() of class RteHtmlParser from the outside is discouraged, as this method is only available for internal purposes.',
60 'getKeepTags' => 'Using getKeepTags() of class RteHtmlParser from the outside is discouraged, as this method is only available for internal purposes.',
61 'divideIntoLines' => 'Using divideIntoLines() of class RteHtmlParser from the outside is discouraged, as this method is only available for internal purposes.',
62 'setDivTags' => 'Using setDivTags() of class RteHtmlParser from the outside is discouraged, as this method is only available for internal purposes.',
63 'getWHFromAttribs' => 'Using getWHFromAttribs() of class RteHtmlParser from the outside is discouraged, as this method is only available for internal purposes.',
64 'urlInfoForLinkTags' => 'Using urlInfoForLinkTags() of class RteHtmlParser from the outside is discouraged, as this method is not in use anymore and will be removed.',
65 'TS_AtagToAbs' => 'Using TS_AtagToAbs() of class RteHtmlParser from the outside is discouraged, as this method is only available for internal purposes.',
66 ];
67
68 /**
69 * List of elements that are not wrapped into a "p" tag while doing the transformation.
70 * @var string
71 */
72 protected $blockElementList = 'DIV,TABLE,BLOCKQUOTE,PRE,UL,OL,H1,H2,H3,H4,H5,H6,ADDRESS,DL,DD,HEADER,SECTION,FOOTER,NAV,ARTICLE,ASIDE';
73
74 /**
75 * List of all tags that are allowed by default
76 * @var string
77 */
78 protected $defaultAllowedTagsList = 'b,i,u,a,img,br,div,center,pre,font,hr,sub,sup,p,strong,em,li,ul,ol,blockquote,strike,span,abbr,acronym,dfn';
79
80 /**
81 * Set this to the pid of the record manipulated by the class.
82 *
83 * @var int
84 */
85 protected $recPid = 0;
86
87 /**
88 * Element reference [table]:[field], eg. "tt_content:bodytext"
89 *
90 * @var string
91 */
92 protected $elRef = '';
93
94 /**
95 * Current Page TSConfig
96 *
97 * @var array
98 */
99 protected $tsConfig = [];
100
101 /**
102 * Set to the TSconfig options coming from Page TSconfig
103 *
104 * @var array
105 */
106 protected $procOptions = [];
107
108 /**
109 * Run-away brake for recursive calls.
110 *
111 * @var int
112 */
113 protected $TS_transform_db_safecounter = 100;
114
115 /**
116 * Data caching for processing function
117 *
118 * @var array
119 */
120 protected $getKeepTags_cache = [];
121
122 /**
123 * Storage of the allowed CSS class names in the RTE
124 *
125 * @var array
126 */
127 protected $allowedClasses = [];
128
129 /**
130 * A list of HTML attributes for <p> tags. Because <p> tags are wrapped currently in a special handling,
131 * they have a special place for configuration via 'proc.keepPDIVattribs'
132 *
133 * @var array
134 */
135 protected $allowedAttributesForParagraphTags = [
136 'class',
137 'align',
138 'id',
139 'title',
140 'dir',
141 'lang',
142 'xml:lang',
143 'itemscope',
144 'itemtype',
145 'itemprop'
146 ];
147
148 /**
149 * Any tags that are allowed outside of <p> sections - usually similar to the block elements
150 * plus some special tags like <hr> and <img> (if images are allowed).
151 * Completely overrideable via 'proc.allowTagsOutside'
152 *
153 * @var array
154 */
155 protected $allowedTagsOutsideOfParagraphs = [
156 'address',
157 'article',
158 'aside',
159 'blockquote',
160 'div',
161 'footer',
162 'header',
163 'hr',
164 'nav',
165 'section'
166 ];
167
168 /**
169 * Initialize, setting element reference and record PID
170 *
171 * @param string $elRef Element reference, eg "tt_content:bodytext
172 * @param int $recPid PID of the record (page id)
173 */
174 public function init($elRef = '', $recPid = 0)
175 {
176 $this->recPid = $recPid;
177 $this->elRef = $elRef;
178 }
179
180 /**********************************************
181 *
182 * Main function
183 *
184 **********************************************/
185 /**
186 * Transform value for RTE based on specConf in the direction specified by $direction (rte/db)
187 * This is the main function called from DataHandler and transfer data classes
188 *
189 * @param string $value Input value
190 * @param null $_ unused
191 * @param string $direction Direction of the transformation. Two keywords are allowed; "db" or "rte". If "db" it means the transformation will clean up content coming from the Rich Text Editor and goes into the database. The other direction, "rte", is of course when content is coming from database and must be transformed to fit the RTE.
192 * @param array $thisConfig Parsed TypoScript content configuring the RTE, probably coming from Page TSconfig.
193 * @return string Output value
194 */
195 public function RTE_transform($value, $_ = null, $direction = 'rte', $thisConfig = [])
196 {
197 $this->tsConfig = $thisConfig;
198 $this->procOptions = (array)$thisConfig['proc.'];
199 if (isset($this->procOptions['allowedClasses.'])) {
200 $this->allowedClasses = (array)$this->procOptions['allowedClasses.'];
201 } else {
202 $this->allowedClasses = GeneralUtility::trimExplode(',', $this->procOptions['allowedClasses'] ?? '', true);
203 }
204
205 // Dynamic configuration of blockElementList
206 if (!empty($this->procOptions['blockElementList'])) {
207 $this->blockElementList = $this->procOptions['blockElementList'];
208 }
209
210 // Define which attributes are allowed on <p> tags
211 if (isset($this->procOptions['allowAttributes.'])) {
212 $this->allowedAttributesForParagraphTags = $this->procOptions['allowAttributes.'];
213 } elseif (isset($this->procOptions['keepPDIVattribs'])) {
214 trigger_error('HTML parsing option "keepPDIVattribs" will not be evaluated anymore in TYPO3 v10.0. Use "allowedAttributes" instead.', E_USER_DEPRECATED);
215 $this->allowedAttributesForParagraphTags = GeneralUtility::trimExplode(',', strtolower($this->procOptions['keepPDIVattribs']), true);
216 }
217 // Override tags which are allowed outside of <p> tags
218 if (isset($this->procOptions['allowTagsOutside'])) {
219 if (!isset($this->procOptions['allowTagsOutside.'])) {
220 $this->allowedTagsOutsideOfParagraphs = GeneralUtility::trimExplode(',', strtolower($this->procOptions['allowTagsOutside']), true);
221 } else {
222 $this->allowedTagsOutsideOfParagraphs = (array)$this->procOptions['allowTagsOutside.'];
223 }
224 }
225
226 // Setting modes / transformations to be called
227 if ((string)$this->procOptions['overruleMode'] !== '') {
228 $modes = GeneralUtility::trimExplode(',', $this->procOptions['overruleMode']);
229 } else {
230 $modes = [$this->procOptions['mode']];
231 }
232 $modes = $this->resolveAppliedTransformationModes($direction, $modes);
233
234 $value = $this->streamlineLineBreaksForProcessing($value);
235
236 // If an entry HTML cleaner was configured, pass the content through the HTMLcleaner
237 $value = $this->runHtmlParserIfConfigured($value, 'entryHTMLparser_' . $direction);
238
239 // Traverse modes
240 foreach ($modes as $cmd) {
241 if ($direction === 'db') {
242 // Checking for user defined transformation:
243 if (!empty($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_parsehtml_proc.php']['transformation'][$cmd])) {
244 $_procObj = GeneralUtility::makeInstance($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_parsehtml_proc.php']['transformation'][$cmd]);
245 $_procObj->pObj = $this;
246 $_procObj->transformationKey = $cmd;
247 $value = $_procObj->transform_db($value, $this);
248 } else {
249 // ... else use defaults:
250 switch ($cmd) {
251 case 'detectbrokenlinks':
252 $value = $this->removeBrokenLinkMarkers($value);
253 break;
254 case 'ts_images':
255 $value = $this->TS_images_db($value);
256 break;
257 case 'ts_links':
258 $value = $this->TS_links_db($value);
259 break;
260 case 'css_transform':
261 // Transform empty paragraphs into spacing paragraphs
262 $value = str_replace('<p></p>', '<p>&nbsp;</p>', $value);
263 // Double any trailing spacing paragraph so that it does not get removed by divideIntoLines()
264 $value = preg_replace('/<p>&nbsp;<\/p>$/', '<p>&nbsp;</p>' . '<p>&nbsp;</p>', $value);
265 $value = $this->TS_transform_db($value);
266 break;
267 default:
268 // Do nothing
269 }
270 }
271 } elseif ($direction === 'rte') {
272 // Checking for user defined transformation:
273 if (!empty($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_parsehtml_proc.php']['transformation'][$cmd])) {
274 $_procObj = GeneralUtility::makeInstance($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_parsehtml_proc.php']['transformation'][$cmd]);
275 $_procObj->pObj = $this;
276 $value = $_procObj->transform_rte($value, $this);
277 } else {
278 // ... else use defaults:
279 switch ($cmd) {
280 case 'detectbrokenlinks':
281 $value = $this->markBrokenLinks($value);
282 break;
283 case 'ts_images':
284 $value = $this->TS_images_rte($value);
285 break;
286 case 'ts_links':
287 $value = $this->TS_links_rte($value, true);
288 break;
289 case 'css_transform':
290 $value = $this->TS_transform_rte($value);
291 break;
292 default:
293 // Do nothing
294 }
295 }
296 }
297 }
298
299 // If an exit HTML cleaner was configured, pass the content through the HTMLcleaner
300 $value = $this->runHtmlParserIfConfigured($value, 'exitHTMLparser_' . $direction);
301
302 // Final clean up of linebreaks
303 $value = $this->streamlineLineBreaksAfterProcessing($value);
304
305 return $value;
306 }
307
308 /**
309 * Ensures what transformation modes should be executed, and that they are only executed once.
310 *
311 * @param string $direction
312 * @param array $modes
313 * @return array the resolved transformation modes
314 */
315 protected function resolveAppliedTransformationModes(string $direction, array $modes)
316 {
317 $modeList = implode(',', $modes);
318
319 // Replace the shortcut "default" with all custom modes
320 $modeList = str_replace('default', 'detectbrokenlinks,css_transform,ts_images,ts_links', $modeList);
321
322 // Make list unique
323 $modes = array_unique(GeneralUtility::trimExplode(',', $modeList, true));
324 // Reverse order if direction is "rte"
325 if ($direction === 'rte') {
326 $modes = array_reverse($modes);
327 }
328
329 return $modes;
330 }
331
332 /**
333 * Runs the HTML parser if it is configured
334 * Getting additional HTML cleaner configuration. These are applied either before or after the main transformation
335 * is done and thus totally independent processing options you can set up.
336 *
337 * This is only possible via TSconfig (procOptions) currently.
338 *
339 * @param string $content
340 * @param string $configurationDirective used to look up in the procOptions if enabled, and then fetch the
341 * @return string the processed content
342 */
343 protected function runHtmlParserIfConfigured($content, $configurationDirective)
344 {
345 if (!empty($this->procOptions[$configurationDirective])) {
346 list($keepTags, $keepNonMatchedTags, $hscMode, $additionalConfiguration) = $this->HTMLparserConfig($this->procOptions[$configurationDirective . '.']);
347 $content = $this->HTMLcleaner($content, $keepTags, $keepNonMatchedTags, $hscMode, $additionalConfiguration);
348 }
349 return $content;
350 }
351
352 /************************************
353 *
354 * Specific RTE TRANSFORMATION functions
355 *
356 *************************************/
357 /**
358 * Transformation handler: 'ts_images' / direction: "db"
359 * Processing images inserted in the RTE.
360 * This is used when content goes from the RTE to the database.
361 * Images inserted in the RTE has an absolute URL applied to the src attribute. This URL is converted to a relative URL
362 * If it turns out that the URL is from another website than the current the image is read from that external URL and moved to the local server.
363 * Also "magic" images are processed here.
364 *
365 * @param string $value The content from RTE going to Database
366 * @return string Processed content
367 */
368 protected function TS_images_db($value)
369 {
370 // Split content by <img> tags and traverse the resulting array for processing:
371 $imgSplit = $this->splitTags('img', $value);
372 if (count($imgSplit) > 1) {
373 $siteUrl = GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
374 $sitePath = str_replace(GeneralUtility::getIndpEnv('TYPO3_REQUEST_HOST'), '', $siteUrl);
375 /** @var Resource\ResourceFactory $resourceFactory */
376 $resourceFactory = Resource\ResourceFactory::getInstance();
377 /** @var Resource\Service\MagicImageService $magicImageService */
378 $magicImageService = GeneralUtility::makeInstance(Resource\Service\MagicImageService::class);
379 $magicImageService->setMagicImageMaximumDimensions($this->tsConfig);
380 foreach ($imgSplit as $k => $v) {
381 // Image found, do processing:
382 if ($k % 2) {
383 // Get attributes
384 list($attribArray) = $this->get_tag_attributes($v, true);
385 // It's always an absolute URL coming from the RTE into the Database.
386 $absoluteUrl = trim($attribArray['src']);
387 // Make path absolute if it is relative and we have a site path which is not '/'
388 $pI = pathinfo($absoluteUrl);
389 if ($sitePath && !$pI['scheme'] && GeneralUtility::isFirstPartOfStr($absoluteUrl, $sitePath)) {
390 // If site is in a subpath (eg. /~user_jim/) this path needs to be removed because it will be added with $siteUrl
391 $absoluteUrl = substr($absoluteUrl, strlen($sitePath));
392 $absoluteUrl = $siteUrl . $absoluteUrl;
393 }
394 // Image dimensions set in the img tag, if any
395 $imgTagDimensions = $this->getWHFromAttribs($attribArray);
396 if ($imgTagDimensions[0]) {
397 $attribArray['width'] = $imgTagDimensions[0];
398 }
399 if ($imgTagDimensions[1]) {
400 $attribArray['height'] = $imgTagDimensions[1];
401 }
402 $originalImageFile = null;
403 if ($attribArray['data-htmlarea-file-uid']) {
404 // An original image file uid is available
405 try {
406 /** @var Resource\File $originalImageFile */
407 $originalImageFile = $resourceFactory->getFileObject((int)$attribArray['data-htmlarea-file-uid']);
408 } catch (Resource\Exception\FileDoesNotExistException $fileDoesNotExistException) {
409 // Log the fact the file could not be retrieved.
410 $message = sprintf('Could not find file with uid "%s"', $attribArray['data-htmlarea-file-uid']);
411 $this->logger->error($message);
412 }
413 }
414 if ($originalImageFile instanceof Resource\File) {
415 // Public url of local file is relative to the site url, absolute otherwise
416 if ($absoluteUrl == $originalImageFile->getPublicUrl() || $absoluteUrl == $siteUrl . $originalImageFile->getPublicUrl()) {
417 // This is a plain image, i.e. reference to the original image
418 if ($this->procOptions['plainImageMode']) {
419 // "plain image mode" is configured
420 // Find the dimensions of the original image
421 $imageInfo = [
422 $originalImageFile->getProperty('width'),
423 $originalImageFile->getProperty('height')
424 ];
425 if (!$imageInfo[0] || !$imageInfo[1]) {
426 $filePath = $originalImageFile->getForLocalProcessing(false);
427 $imageInfoObject = GeneralUtility::makeInstance(ImageInfo::class, $filePath);
428 $imageInfo = [
429 $imageInfoObject->getWidth(),
430 $imageInfoObject->getHeight()
431 ];
432 }
433 $attribArray = $this->applyPlainImageModeSettings($imageInfo, $attribArray);
434 }
435 } else {
436 // Magic image case: get a processed file with the requested configuration
437 $imageConfiguration = [
438 'width' => $imgTagDimensions[0],
439 'height' => $imgTagDimensions[1]
440 ];
441 $magicImage = $magicImageService->createMagicImage($originalImageFile, $imageConfiguration);
442 $attribArray['width'] = $magicImage->getProperty('width');
443 $attribArray['height'] = $magicImage->getProperty('height');
444 $attribArray['src'] = $magicImage->getPublicUrl();
445 }
446 } elseif (!GeneralUtility::isFirstPartOfStr($absoluteUrl, $siteUrl) && !$this->procOptions['dontFetchExtPictures'] && TYPO3_MODE === 'BE') {
447 // External image from another URL: in that case, fetch image, unless the feature is disabled or we are not in backend mode
448 // Fetch the external image
449 $externalFile = GeneralUtility::getUrl($absoluteUrl);
450 if ($externalFile) {
451 $pU = parse_url($absoluteUrl);
452 $pI = pathinfo($pU['path']);
453 $extension = strtolower($pI['extension']);
454 if ($extension === 'jpg' || $extension === 'jpeg' || $extension === 'gif' || $extension === 'png') {
455 $fileName = GeneralUtility::shortMD5($absoluteUrl) . '.' . $pI['extension'];
456 // We insert this image into the user default upload folder
457 list($table, $field) = explode(':', $this->elRef);
458 /** @var Resource\Folder $folder */
459 $folder = $GLOBALS['BE_USER']->getDefaultUploadFolder($this->recPid, $table, $field);
460 /** @var Resource\File $fileObject */
461 $fileObject = $folder->createFile($fileName)->setContents($externalFile);
462 $imageConfiguration = [
463 'width' => $attribArray['width'],
464 'height' => $attribArray['height']
465 ];
466 $magicImage = $magicImageService->createMagicImage($fileObject, $imageConfiguration);
467 $attribArray['width'] = $magicImage->getProperty('width');
468 $attribArray['height'] = $magicImage->getProperty('height');
469 $attribArray['data-htmlarea-file-uid'] = $fileObject->getUid();
470 $attribArray['src'] = $magicImage->getPublicUrl();
471 }
472 }
473 } elseif (GeneralUtility::isFirstPartOfStr($absoluteUrl, $siteUrl)) {
474 // Finally, check image as local file (siteURL equals the one of the image)
475 // Image has no data-htmlarea-file-uid attribute
476 // Relative path, rawurldecoded for special characters.
477 $path = rawurldecode(substr($absoluteUrl, strlen($siteUrl)));
478 // Absolute filepath, locked to relative path of this project
479 $filepath = GeneralUtility::getFileAbsFileName($path);
480 // Check file existence (in relative directory to this installation!)
481 if ($filepath && @is_file($filepath)) {
482 // Treat it as a plain image
483 if ($this->procOptions['plainImageMode']) {
484 // If "plain image mode" has been configured
485 // Find the original dimensions of the image
486 $imageInfoObject = GeneralUtility::makeInstance(ImageInfo::class, $filepath);
487 $imageInfo = [
488 $imageInfoObject->getWidth(),
489 $imageInfoObject->getHeight()
490 ];
491 $attribArray = $this->applyPlainImageModeSettings($imageInfo, $attribArray);
492 }
493 // Let's try to find a file uid for this image
494 try {
495 $fileOrFolderObject = $resourceFactory->retrieveFileOrFolderObject($path);
496 if ($fileOrFolderObject instanceof Resource\FileInterface) {
497 $fileIdentifier = $fileOrFolderObject->getIdentifier();
498 /** @var Resource\AbstractFile $fileObject */
499 $fileObject = $fileOrFolderObject->getStorage()->getFile($fileIdentifier);
500 // @todo if the retrieved file is a processed file, get the original file...
501 $attribArray['data-htmlarea-file-uid'] = $fileObject->getUid();
502 }
503 } catch (Resource\Exception\ResourceDoesNotExistException $resourceDoesNotExistException) {
504 // Nothing to be done if file/folder not found
505 }
506 }
507 }
508 // Remove width and height from style attribute
509 $attribArray['style'] = preg_replace('/(?:^|[^-])(\\s*(?:width|height)\\s*:[^;]*(?:$|;))/si', '', $attribArray['style']);
510 // Must have alt attribute
511 if (!isset($attribArray['alt'])) {
512 $attribArray['alt'] = '';
513 }
514 // Convert absolute to relative url
515 if (GeneralUtility::isFirstPartOfStr($attribArray['src'], $siteUrl)) {
516 $attribArray['src'] = substr($attribArray['src'], strlen($siteUrl));
517 }
518 $imgSplit[$k] = '<img ' . GeneralUtility::implodeAttributes($attribArray, true, true) . ' />';
519 }
520 }
521 }
522 return implode('', $imgSplit);
523 }
524
525 /**
526 * Transformation handler: 'ts_images' / direction: "rte"
527 * Processing images from database content going into the RTE.
528 * Processing includes converting the src attribute to an absolute URL.
529 *
530 * @param string $value Content input
531 * @return string Content output
532 */
533 public function TS_images_rte($value)
534 {
535 // Split content by <img> tags and traverse the resulting array for processing:
536 $imgSplit = $this->splitTags('img', $value);
537 if (count($imgSplit) > 1) {
538 $siteUrl = GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
539 $sitePath = str_replace(GeneralUtility::getIndpEnv('TYPO3_REQUEST_HOST'), '', $siteUrl);
540 foreach ($imgSplit as $k => $v) {
541 // Image found
542 if ($k % 2) {
543 // Get the attributes of the img tag
544 list($attribArray) = $this->get_tag_attributes($v, true);
545 $absoluteUrl = trim($attribArray['src']);
546 // Transform the src attribute into an absolute url, if it not already
547 if (stripos($absoluteUrl, 'http') !== 0) {
548 // If site is in a subpath (eg. /~user_jim/) this path needs to be removed because it will be added with $siteUrl
549 $attribArray['src'] = preg_replace('#^' . preg_quote($sitePath, '#') . '#', '', $attribArray['src']);
550 $attribArray['src'] = $siteUrl . $attribArray['src'];
551 }
552 // Must have alt attribute
553 if (!isset($attribArray['alt'])) {
554 $attribArray['alt'] = '';
555 }
556 $imgSplit[$k] = '<img ' . GeneralUtility::implodeAttributes($attribArray, true, true) . ' />';
557 }
558 }
559 }
560 // Return processed content:
561 return implode('', $imgSplit);
562 }
563
564 /**
565 * Transformation handler: 'ts_links' / direction: "db"
566 * Processing anchor tags, and resolves them correctly again via the LinkService syntax
567 *
568 * Splits content into <a> tag blocks and processes each tag, and allows hooks to actually render
569 * the result.
570 *
571 * @param string $value Content input
572 * @return string Content output
573 * @see TS_links_rte()
574 */
575 protected function TS_links_db($value)
576 {
577 $blockSplit = $this->splitIntoBlock('A', $value);
578 foreach ($blockSplit as $k => $v) {
579 if ($k % 2) {
580 list($tagAttributes) = $this->get_tag_attributes($this->getFirstTag($v), true);
581 $linkService = GeneralUtility::makeInstance(LinkService::class);
582 $linkInformation = $linkService->resolve($tagAttributes['href'] ?? '');
583
584 // Modify parameters, this hook should be deprecated
585 if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_parsehtml_proc.php']['modifyParams_LinksDb_PostProc'])) {
586 trigger_error('The hook "t3lib/class.t3lib_parsehtml_proc.php->modifyParams_LinksDb_PostProc" will be removed in TYPO3 v10.0, use LinkService syntax to modify links to be stored in the database.', E_USER_DEPRECATED);
587 $parameters = [
588 'currentBlock' => $v,
589 'linkInformation' => $linkInformation,
590 'url' => $linkInformation['href'],
591 'attributes' => $tagAttributes
592 ];
593 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_parsehtml_proc.php']['modifyParams_LinksDb_PostProc'] ?? [] as $className) {
594 $processor = GeneralUtility::makeInstance($className);
595 $blockSplit[$k] = $processor->modifyParamsLinksDb($parameters, $this);
596 }
597 } else {
598 // Otherwise store the link as <a> tag as default by TYPO3, with the new link service syntax
599 $tagAttributes['href'] = $linkService->asString($linkInformation);
600 $blockSplit[$k] = '<a ' . GeneralUtility::implodeAttributes($tagAttributes, true) . '>'
601 . $this->TS_links_db($this->removeFirstAndLastTag($blockSplit[$k])) . '</a>';
602 }
603 }
604 }
605 return implode('', $blockSplit);
606 }
607
608 /**
609 * Transformation handler: 'ts_links' / direction: "rte"
610 * Converting TYPO3-specific <link> tags to <a> tags
611 *
612 * This functionality is only used to convert legacy <link> tags to the new linking syntax using <a> tags, and will
613 * not be converted back to <link> tags anymore.
614 *
615 * @param string $value Content input
616 * @param bool $internallyCalledFromCore internal option for calls where the Core is still using this function, to supress method deprecations
617 * @return string Content output
618 * @deprecated will be removed in TYPO3 v10.0, only ->TS_AtagToAbs() should be called directly, <link> syntax is deprecated
619 */
620 public function TS_links_rte($value, $internallyCalledFromCore = null)
621 {
622 if ($internallyCalledFromCore === null) {
623 trigger_error('RteHtmlParser->TS_links_rte() will be removed in TYPO3 v10.0, use TS_AtagToAbs() directly and do not use <link> syntax anymore.', E_USER_DEPRECATED);
624 }
625 $hasLinkTags = false;
626 $value = $this->TS_AtagToAbs($value);
627 // Split content by the TYPO3 pseudo tag "<link>"
628 $blockSplit = $this->splitIntoBlock('link', $value, true);
629 foreach ($blockSplit as $k => $v) {
630 // Block
631 if ($k % 2) {
632 $hasLinkTags = true;
633 // Split away the first "<link " part
634 $typoLinkData = explode(' ', substr($this->getFirstTag($v), 0, -1), 2)[1];
635 $tagCode = GeneralUtility::makeInstance(TypoLinkCodecService::class)->decode($typoLinkData);
636
637 // Parsing the TypoLink data. This parsing is done like in \TYPO3\CMS\Frontend\ContentObject->typoLink()
638 $linkService = GeneralUtility::makeInstance(LinkService::class);
639 $linkInformation = $linkService->resolve($tagCode['url']);
640
641 try {
642 $href = $linkService->asString($linkInformation);
643 } catch (UnknownLinkHandlerException $e) {
644 $href = '';
645 }
646
647 // Modify parameters by a hook
648 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_parsehtml_proc.php']['modifyParams_LinksRte_PostProc'] ?? false)) {
649 trigger_error('The hook "t3lib/class.t3lib_parsehtml_proc.php->modifyParams_LinksRte_PostProc" will be removed in TYPO3 v10.0, use the link service to properly use .', E_USER_DEPRECATED);
650 // backwards-compatibility: show an error message if the page is not found
651 $error = '';
652 if ($linkInformation['type'] === LinkService::TYPE_PAGE) {
653 $pageRecord = BackendUtility::getRecord('pages', $linkInformation['pageuid']);
654 // Page does not exist
655 if (!is_array($pageRecord)) {
656 $error = 'Page with ID ' . $linkInformation['pageuid'] . ' not found';
657 }
658 }
659 $parameters = [
660 'currentBlock' => $v,
661 'url' => $href,
662 'tagCode' => $tagCode,
663 'external' => $linkInformation['type'] === LinkService::TYPE_URL,
664 'error' => $error
665 ];
666 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_parsehtml_proc.php']['modifyParams_LinksRte_PostProc'] as $className) {
667 $processor = GeneralUtility::makeInstance($className);
668 $blockSplit[$k] = $processor->modifyParamsLinksRte($parameters, $this);
669 }
670 } else {
671 $anchorAttributes = [
672 'href' => $href,
673 'target' => $tagCode['target'],
674 'class' => $tagCode['class'],
675 'title' => $tagCode['title']
676 ];
677
678 // Setting the <a> tag
679 $blockSplit[$k] = '<a ' . GeneralUtility::implodeAttributes($anchorAttributes, true) . '>'
680 . $this->TS_links_rte($this->removeFirstAndLastTag($blockSplit[$k]), $internallyCalledFromCore)
681 . '</a>';
682 }
683 }
684 }
685 if ($hasLinkTags) {
686 trigger_error('Content with <link> syntax was found, update your content to use the t3:// syntax, and migrate your content via the upgrade wizard in the install tool.', E_USER_DEPRECATED);
687 }
688 return implode('', $blockSplit);
689 }
690
691 /**
692 * Transformation handler: 'css_transform' / direction: "db"
693 * Cleaning (->db) for standard content elements (ts)
694 *
695 * @param string $value Content input
696 * @return string Content output
697 * @see TS_transform_rte()
698 */
699 protected function TS_transform_db($value)
700 {
701 // Safety... so forever loops are avoided (they should not occur, but an error would potentially do this...)
702 $this->TS_transform_db_safecounter--;
703 if ($this->TS_transform_db_safecounter < 0) {
704 return $value;
705 }
706 // Split the content from RTE by the occurrence of these blocks:
707 $blockSplit = $this->splitIntoBlock($this->blockElementList, $value);
708
709 // Avoid superfluous linebreaks by transform_db after ending headListTag
710 while (count($blockSplit) > 0 && trim(end($blockSplit)) === '') {
711 array_pop($blockSplit);
712 }
713
714 // Traverse the blocks
715 foreach ($blockSplit as $k => $v) {
716 if ($k % 2) {
717 // Inside block:
718 // Init:
719 $tag = $this->getFirstTag($v);
720 $tagName = strtolower($this->getFirstTagName($v));
721 // Process based on the tag:
722 switch ($tagName) {
723 case 'blockquote':
724 case 'dd':
725 case 'div':
726 case 'header':
727 case 'section':
728 case 'footer':
729 case 'nav':
730 case 'article':
731 case 'aside':
732 $blockSplit[$k] = $tag . $this->TS_transform_db($this->removeFirstAndLastTag($blockSplit[$k])) . '</' . $tagName . '>';
733 break;
734 case 'pre':
735 break;
736 default:
737 // usually <hx> tags and <table> tags where no other block elements are within the tags
738 // Eliminate true linebreaks inside block element tags
739 $blockSplit[$k] = preg_replace('/[' . LF . ']+/', ' ', $blockSplit[$k]);
740 }
741 } else {
742 // NON-block:
743 if (trim($blockSplit[$k]) !== '') {
744 $blockSplit[$k] = str_replace('<hr/>', '<hr />', $blockSplit[$k]);
745 // Remove linebreaks preceding hr tags
746 $blockSplit[$k] = preg_replace('/[' . LF . ']+<(hr)(\\s[^>\\/]*)?[[:space:]]*\\/?>/', '<$1$2/>', $blockSplit[$k]);
747 // Remove linebreaks following hr tags
748 $blockSplit[$k] = preg_replace('/<(hr)(\\s[^>\\/]*)?[[:space:]]*\\/?>[' . LF . ']+/', '<$1$2/>', $blockSplit[$k]);
749 // Replace other linebreaks with space
750 $blockSplit[$k] = preg_replace('/[' . LF . ']+/', ' ', $blockSplit[$k]);
751 $blockSplit[$k] = $this->divideIntoLines($blockSplit[$k]);
752 } else {
753 unset($blockSplit[$k]);
754 }
755 }
756 }
757 $this->TS_transform_db_safecounter++;
758 return implode(LF, $blockSplit);
759 }
760
761 /**
762 * Wraps a-tags that contain a style attribute with a span-tag
763 * This is not in use anymore, but was necessary before because <a> tags are transformed into <link> tags
764 * in the database, but <link> tags cannot handle style attributes. However, this is considered a
765 * bad approach as it leaves an ugly <span> tag in the database, if allowedTags=span with style attributes are
766 * allowed.
767 *
768 * @param string $value Content input
769 * @return string Content output
770 * @deprecated since TYPO3 v9.0, will be removed in TYPO3 v10.0, see comment above, adding attribuet "rteerror" is not necessary anymore.
771 */
772 public function transformStyledATags($value)
773 {
774 trigger_error('RteHtmlParser->transformStyledATags() will be removed in TYPO3 v10.0. TYPO3 can handle style attribute in anchor tags properly since TYPO3 v8 LTS.', E_USER_DEPRECATED);
775 $blockSplit = $this->splitIntoBlock('A', $value);
776 foreach ($blockSplit as $k => $v) {
777 // If an A-tag was found
778 if ($k % 2) {
779 list($attribArray) = $this->get_tag_attributes($this->getFirstTag($v), true);
780 // If "style" attribute is set and rteerror is not set!
781 if ($attribArray['style'] && !$attribArray['rteerror']) {
782 $attribArray_copy['style'] = $attribArray['style'];
783 unset($attribArray['style']);
784 $bTag = '<span ' . GeneralUtility::implodeAttributes($attribArray_copy, true) . '><a ' . GeneralUtility::implodeAttributes($attribArray, true) . '>';
785 $eTag = '</a></span>';
786 $blockSplit[$k] = $bTag . $this->removeFirstAndLastTag($blockSplit[$k]) . $eTag;
787 }
788 }
789 }
790 return implode('', $blockSplit);
791 }
792
793 /**
794 * Transformation handler: css_transform / direction: "rte"
795 * Set (->rte) for standard content elements (ts)
796 *
797 * @param string $value Content input
798 * @return string Content output
799 * @see TS_transform_db()
800 */
801 protected function TS_transform_rte($value)
802 {
803 // Split the content from database by the occurrence of the block elements
804 $blockSplit = $this->splitIntoBlock($this->blockElementList, $value);
805 // Traverse the blocks
806 foreach ($blockSplit as $k => $v) {
807 if ($k % 2) {
808 // Inside one of the blocks:
809 // Init:
810 $tag = $this->getFirstTag($v);
811 $tagName = strtolower($this->getFirstTagName($v));
812 // Based on tagname, we do transformations:
813 switch ($tagName) {
814 case 'blockquote':
815 case 'dd':
816 case 'div':
817 case 'header':
818 case 'section':
819 case 'footer':
820 case 'nav':
821 case 'article':
822 case 'aside':
823 $blockSplit[$k] = $tag . $this->TS_transform_rte($this->removeFirstAndLastTag($blockSplit[$k])) . '</' . $tagName . '>';
824 break;
825 }
826 $blockSplit[$k + 1] = preg_replace('/^[ ]*' . LF . '/', '', $blockSplit[$k + 1]);
827 } else {
828 // NON-block:
829 $nextFTN = $this->getFirstTagName($blockSplit[$k + 1] ?? '');
830 $onlyLineBreaks = (preg_match('/^[ ]*' . LF . '+[ ]*$/', $blockSplit[$k]) == 1);
831 // If the line is followed by a block or is the last line:
832 if (GeneralUtility::inList($this->blockElementList, $nextFTN) || !isset($blockSplit[$k + 1])) {
833 // If the line contains more than just linebreaks, reduce the number of trailing linebreaks by 1
834 if (!$onlyLineBreaks) {
835 $blockSplit[$k] = preg_replace('/(' . LF . '*)' . LF . '[ ]*$/', '$1', $blockSplit[$k]);
836 } else {
837 // If the line contains only linebreaks, remove the leading linebreak
838 $blockSplit[$k] = preg_replace('/^[ ]*' . LF . '/', '', $blockSplit[$k]);
839 }
840 }
841 // If $blockSplit[$k] is blank then unset the line, unless the line only contained linebreaks
842 if ((string)$blockSplit[$k] === '' && !$onlyLineBreaks) {
843 unset($blockSplit[$k]);
844 } else {
845 $blockSplit[$k] = $this->setDivTags($blockSplit[$k]);
846 }
847 }
848 }
849 return implode(LF, $blockSplit);
850 }
851
852 /***************************************************************
853 *
854 * Generic RTE transformation, analysis and helper functions
855 *
856 **************************************************************/
857
858 /**
859 * Function for cleaning content going into the database.
860 * Content is cleaned eg. by removing unallowed HTML and ds-HSC content
861 * It is basically calling HTMLcleaner from the parent class with some preset configuration specifically set up for cleaning content going from the RTE into the db
862 *
863 * @param string $content Content to clean up
864 * @return string Clean content
865 * @see getKeepTags()
866 */
867 protected function HTMLcleaner_db($content)
868 {
869 $keepTags = $this->getKeepTags('db');
870 // Default: remove unknown tags.
871 if (isset($this->procOptions['dontRemoveUnknownTags_db'])) {
872 trigger_error('HTMLParser option "dontRemoveUnknownTags_db" will not be evaluted anymore in TYPO3 v10.0. Remove its usages.', E_USER_DEPRECATED);
873 }
874 $keepUnknownTags = (bool)($this->procOptions['dontRemoveUnknownTags_db'] ?? false);
875 return $this->HTMLcleaner($content, $keepTags, $keepUnknownTags);
876 }
877
878 /**
879 * Creates an array of configuration for the HTMLcleaner function based on whether content
880 * go TO or FROM the Rich Text Editor ($direction)
881 *
882 * @param string $direction The direction of the content being processed by the output configuration; "db" (content going into the database FROM the rte) or "rte" (content going into the form)
883 * @return array Configuration array
884 * @see HTMLcleaner_db()
885 */
886 protected function getKeepTags($direction = 'rte')
887 {
888 if (!isset($this->getKeepTags_cache[$direction]) || !is_array($this->getKeepTags_cache[$direction])) {
889 // Setting up allowed tags:
890 // Default is to get allowed/denied tags from internal array of processing options:
891 // Construct default list of tags to keep:
892 if (isset($this->procOptions['allowTags.']) && is_array($this->procOptions['allowTags.'])) {
893 $keepTags = implode(',', $this->procOptions['allowTags.']);
894 } else {
895 $keepTags = $this->procOptions['allowTags'] ?? '';
896 }
897 $keepTags = array_flip(GeneralUtility::trimExplode(',', $this->defaultAllowedTagsList . ',' . strtolower($keepTags), true));
898 // For tags to deny, remove them from $keepTags array:
899 $denyTags = GeneralUtility::trimExplode(',', $this->procOptions['denyTags'] ?? '', true);
900 foreach ($denyTags as $dKe) {
901 unset($keepTags[$dKe]);
902 }
903 // Based on the direction of content, set further options:
904 switch ($direction) {
905 case 'rte':
906 // Transforming keepTags array so it can be understood by the HTMLcleaner function.
907 // This basically converts the format of the array from TypoScript (having dots) to plain multi-dimensional array.
908 list($keepTags) = $this->HTMLparserConfig($this->procOptions['HTMLparser_rte.'] ?? [], $keepTags);
909 break;
910 case 'db':
911 // Setting up span tags if they are allowed:
912 if (isset($keepTags['span'])) {
913 $keepTags['span'] = [
914 'allowedAttribs' => 'id,class,style,title,lang,xml:lang,dir,itemscope,itemtype,itemprop',
915 'fixAttrib' => [
916 'class' => [
917 'removeIfFalse' => 1
918 ]
919 ],
920 'rmTagIfNoAttrib' => 1
921 ];
922 if (!empty($this->allowedClasses)) {
923 $keepTags['span']['fixAttrib']['class']['list'] = $this->allowedClasses;
924 }
925 }
926 // Setting further options, getting them from the processing options
927 $TSc = $this->procOptions['HTMLparser_db.'] ?? [];
928 if (empty($TSc['globalNesting'])) {
929 $TSc['globalNesting'] = 'b,i,u,a,center,font,sub,sup,strong,em,strike,span';
930 }
931 if (empty($TSc['noAttrib'])) {
932 $TSc['noAttrib'] = 'b,i,u,br,center,hr,sub,sup,strong,em,li,ul,ol,blockquote,strike';
933 }
934 // Transforming the array from TypoScript to regular array:
935 list($keepTags) = $this->HTMLparserConfig($TSc, $keepTags);
936 break;
937 }
938 // Caching (internally, in object memory) the result
939 $this->getKeepTags_cache[$direction] = $keepTags;
940 }
941 // Return result:
942 return $this->getKeepTags_cache[$direction];
943 }
944
945 /**
946 * This resolves the $value into parts based on <p>-sections. These are returned as lines separated by LF.
947 * This point is to resolve the HTML-code returned from RTE into ordinary lines so it's 'human-readable'
948 * The function ->setDivTags does the opposite.
949 * This function processes content to go into the database.
950 *
951 * @param string $value Value to process.
952 * @param int $count Recursion brake. Decremented on each recursion down to zero. Default is 5 (which equals the allowed nesting levels of p tags).
953 * @param bool $returnArray If TRUE, an array with the lines is returned, otherwise a string of the processed input value.
954 * @return string|array Processed input value.
955 * @see setDivTags()
956 */
957 protected function divideIntoLines($value, $count = 5, $returnArray = false)
958 {
959 // Setting the third param will eliminate false end-tags. Maybe this is a good thing to do...?
960 $paragraphBlocks = $this->splitIntoBlock('p', $value, true);
961 // Returns plainly the content if there was no p sections in it
962 if (count($paragraphBlocks) <= 1 || $count <= 0) {
963 return $this->sanitizeLineBreaksForContentOnly($value);
964 }
965
966 // Traverse the splitted sections
967 foreach ($paragraphBlocks as $k => $v) {
968 if ($k % 2) {
969 // Inside a <p> section
970 $v = $this->removeFirstAndLastTag($v);
971 // Fetching 'sub-lines' - which will explode any further p nesting recursively
972 $subLines = $this->divideIntoLines($v, $count - 1, true);
973 // So, if there happened to be sub-nesting of p, this is written directly as the new content of THIS section. (This would be considered 'an error')
974 if (is_array($subLines)) {
975 $paragraphBlocks[$k] = implode(LF, $subLines);
976 } else {
977 //... but if NO subsection was found, we process it as a TRUE line without erroneous content:
978 $paragraphBlocks[$k] = $this->processContentWithinParagraph($subLines, $paragraphBlocks[$k]);
979 }
980 // If it turns out the line is just blank (containing a &nbsp; possibly) then just make it pure blank.
981 // But, prevent filtering of lines that are blank in sense above, but whose tags contain attributes.
982 // Those attributes should have been filtered before; if they are still there they must be considered as possible content.
983 if (trim(strip_tags($paragraphBlocks[$k])) === '&nbsp;' && !preg_match('/\\<(img)(\\s[^>]*)?\\/?>/si', $paragraphBlocks[$k]) && !preg_match('/\\<([^>]*)?( align| class| style| id| title| dir| lang| xml:lang)([^>]*)?>/si', trim($paragraphBlocks[$k]))) {
984 $paragraphBlocks[$k] = '';
985 }
986 } else {
987 // Outside a paragraph, if there is still something in there, just add a <p> tag
988 // Remove positions which are outside <p> tags and without content
989 $paragraphBlocks[$k] = trim(strip_tags($paragraphBlocks[$k], '<' . implode('><', $this->allowedTagsOutsideOfParagraphs) . '>'));
990 $paragraphBlocks[$k] = $this->sanitizeLineBreaksForContentOnly($paragraphBlocks[$k]);
991 if ((string)$paragraphBlocks[$k] === '') {
992 unset($paragraphBlocks[$k]);
993 } else {
994 // add <p> tags around the content
995 $paragraphBlocks[$k] = str_replace(strip_tags($paragraphBlocks[$k]), '<p>' . strip_tags($paragraphBlocks[$k]) . '</p>', $paragraphBlocks[$k]);
996 }
997 }
998 }
999 return $returnArray ? $paragraphBlocks : implode(LF, $paragraphBlocks);
1000 }
1001
1002 /**
1003 * Converts all lines into <p></p>-sections (unless the line has a p - tag already)
1004 * For processing of content going FROM database TO RTE.
1005 *
1006 * @param string $value Value to convert
1007 * @return string Processed value.
1008 * @see divideIntoLines()
1009 */
1010 protected function setDivTags($value)
1011 {
1012 // First, setting configuration for the HTMLcleaner function. This will process each line between the <div>/<p> section on their way to the RTE
1013 $keepTags = $this->getKeepTags('rte');
1014 // Divide the content into lines
1015 $parts = explode(LF, $value);
1016 foreach ($parts as $k => $v) {
1017 // Processing of line content:
1018 // If the line is blank, set it to &nbsp;
1019 if (trim($parts[$k]) === '') {
1020 $parts[$k] = '&nbsp;';
1021 } else {
1022 // Clean the line content, keeping unknown tags (as they can be removed in the entryHTMLparser)
1023 $parts[$k] = $this->HTMLcleaner($parts[$k], $keepTags, 'protect');
1024 // convert double-encoded &nbsp; into regular &nbsp; however this could also be reversed via the exitHTMLparser
1025 // This was previously an option to disable called "dontConvAmpInNBSP_rte"
1026 $parts[$k] = str_replace('&amp;nbsp;', '&nbsp;', $parts[$k]);
1027 }
1028 // Wrapping the line in <p> tags if not already wrapped and does not contain an hr tag
1029 if (!preg_match('/<(hr)(\\s[^>\\/]*)?[[:space:]]*\\/?>/i', $parts[$k])) {
1030 $testStr = strtolower(trim($parts[$k]));
1031 if (strpos($testStr, '<div') !== 0 || substr($testStr, -6) !== '</div>') {
1032 if (strpos($testStr, '<p') !== 0 || substr($testStr, -4) !== '</p>') {
1033 // Only set p-tags if there is not already div or p tags:
1034 $parts[$k] = '<p>' . $parts[$k] . '</p>';
1035 }
1036 }
1037 }
1038 }
1039 // Implode result:
1040 return implode(LF, $parts);
1041 }
1042
1043 /**
1044 * Used for transformation from RTE to DB
1045 *
1046 * Works on a single line within a <p> tag when storing into the database
1047 * This always adds <p> tags and validates the arguments,
1048 * additionally the content is cleaned up via the HTMLcleaner.
1049 *
1050 * @param string $content the content within the <p> tag
1051 * @param string $fullContentWithTag the whole <p> tag surrounded as well
1052 *
1053 * @return string the full <p> tag with cleaned content
1054 */
1055 protected function processContentWithinParagraph(string $content, string $fullContentWithTag)
1056 {
1057 // clean up the content
1058 $content = $this->HTMLcleaner_db($content);
1059 // Get the <p> tag, and validate the attributes
1060 $fTag = $this->getFirstTag($fullContentWithTag);
1061 // Check which attributes of the <p> tag to keep attributes
1062 if (!empty($this->allowedAttributesForParagraphTags)) {
1063 list($tagAttributes) = $this->get_tag_attributes($fTag);
1064 // Make sure the tag attributes only contain the ones that are defined to be allowed
1065 $tagAttributes = array_intersect_key($tagAttributes, array_flip($this->allowedAttributesForParagraphTags));
1066
1067 // Only allow classes that are whitelisted in $this->allowedClasses
1068 if (isset($tagAttributes['class']) && trim($tagAttributes['class']) !== '' && !empty($this->allowedClasses) && !in_array($tagAttributes['class'], $this->allowedClasses, true)) {
1069 $classes = GeneralUtility::trimExplode(' ', $tagAttributes['class'], true);
1070 $classes = array_intersect($classes, $this->allowedClasses);
1071 if (!empty($classes)) {
1072 $tagAttributes['class'] = implode(' ', $classes);
1073 } else {
1074 unset($tagAttributes['class']);
1075 }
1076 }
1077 } else {
1078 $tagAttributes = [];
1079 }
1080 // Remove any line break
1081 $content = str_replace(LF, '', $content);
1082 // Compile the surrounding <p> tag
1083 $content = '<' . rtrim('p ' . $this->compileTagAttribs($tagAttributes)) . '>' . $content . '</p>';
1084 return $content;
1085 }
1086
1087 /**
1088 * Wrap <hr> tags with LFs, and also remove double LFs, used when transforming from RTE to DB
1089 *
1090 * @param string $content
1091 * @return string the modified content
1092 */
1093 protected function sanitizeLineBreaksForContentOnly(string $content)
1094 {
1095 $content = preg_replace('/<(hr)(\\s[^>\\/]*)?[[:space:]]*\\/?>/i', LF . '<$1$2/>' . LF, $content);
1096 $content = str_replace(LF . LF, LF, $content);
1097 $content = preg_replace('/(^' . LF . ')|(' . LF . '$)/i', '', $content);
1098 return $content;
1099 }
1100
1101 /**
1102 * Finds width and height from attrib-array
1103 * If the width and height is found in the style-attribute, use that!
1104 *
1105 * @param array $attribArray Array of attributes from tag in which to search. More specifically the content of the key "style" is used to extract "width:xxx / height:xxx" information
1106 * @return array Integer w/h in key 0/1. Zero is returned if not found.
1107 */
1108 protected function getWHFromAttribs($attribArray)
1109 {
1110 $style = trim($attribArray['style']);
1111 $w = 0;
1112 $h = 0;
1113 if ($style) {
1114 $regex = '[[:space:]]*:[[:space:]]*([0-9]*)[[:space:]]*px';
1115 // Width
1116 $reg = [];
1117 preg_match('/width' . $regex . '/i', $style, $reg);
1118 $w = (int)$reg[1];
1119 // Height
1120 preg_match('/height' . $regex . '/i', $style, $reg);
1121 $h = (int)$reg[1];
1122 }
1123 if (!$w) {
1124 $w = $attribArray['width'];
1125 }
1126 if (!$h) {
1127 $h = $attribArray['height'];
1128 }
1129 return [(int)$w, (int)$h];
1130 }
1131
1132 /**
1133 * Parse <A>-tag href and return status of email,external,file or page
1134 * This functionality is not in use anymore
1135 *
1136 * @param string $url URL to analyze.
1137 * @return array Information in an array about the URL
1138 * @deprecated will be removed in TYPO3 v10.0. Not in use anymore.
1139 */
1140 protected function urlInfoForLinkTags($url)
1141 {
1142 $info = [];
1143 $url = trim($url);
1144 if (strpos(strtolower($url), 'mailto:') === 0) {
1145 $info['url'] = trim(substr($url, 7));
1146 $info['type'] = 'email';
1147 } elseif (strpos($url, '?file:') !== false) {
1148 $info['type'] = 'file';
1149 $info['url'] = rawurldecode(substr($url, strpos($url, '?file:') + 1));
1150 } else {
1151 $curURL = GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
1152 $urlLength = strlen($url);
1153 $a = 0;
1154 for (; $a < $urlLength; $a++) {
1155 if ($url[$a] != $curURL[$a]) {
1156 break;
1157 }
1158 }
1159 $info['relScriptPath'] = substr($curURL, $a);
1160 $info['relUrl'] = substr($url, $a);
1161 $info['url'] = $url;
1162 $info['type'] = 'ext';
1163 $siteUrl_parts = parse_url($url);
1164 $curUrl_parts = parse_url($curURL);
1165 // Hosts should match
1166 if ($siteUrl_parts['host'] == $curUrl_parts['host'] && (!$info['relScriptPath'] || defined('TYPO3_mainDir') && strpos($info['relScriptPath'], TYPO3_mainDir) === 0)) {
1167 // If the script path seems to match or is empty (FE-EDIT)
1168 // New processing order 100502
1169 $uP = parse_url($info['relUrl']);
1170 if ($info['relUrl'] === '#' . $siteUrl_parts['fragment']) {
1171 $info['url'] = $info['relUrl'];
1172 $info['type'] = 'anchor';
1173 } elseif (!trim($uP['path']) || $uP['path'] === 'index.php') {
1174 // URL is a page (id parameter)
1175 $pp = preg_split('/^id=/', $uP['query']);
1176 $pp[1] = preg_replace('/&id=[^&]*/', '', $pp[1]);
1177 $parameters = explode('&', $pp[1]);
1178 $id = array_shift($parameters);
1179 if ($id) {
1180 $info['pageid'] = $id;
1181 $info['cElement'] = $uP['fragment'];
1182 $info['url'] = $id . ($info['cElement'] ? '#' . $info['cElement'] : '');
1183 $info['type'] = 'page';
1184 $info['query'] = $parameters[0] ? '&' . implode('&', $parameters) : '';
1185 }
1186 } else {
1187 $info['url'] = $info['relUrl'];
1188 $info['type'] = 'file';
1189 }
1190 } else {
1191 unset($info['relScriptPath']);
1192 unset($info['relUrl']);
1193 }
1194 }
1195 return $info;
1196 }
1197
1198 /**
1199 * Converting <A>-tags to absolute URLs (+ setting rtekeep attribute)
1200 *
1201 * @param string $value Content input
1202 * @return string Content output
1203 */
1204 protected function TS_AtagToAbs($value)
1205 {
1206 if (func_num_args() > 1) {
1207 trigger_error('Second argument of RteHtmlParser->TS_AtagToAbs() is not in use and will be removed in TYPO3 v10.0, however the argument in the callers code can be removed without side-effects.', E_USER_DEPRECATED);
1208 }
1209 $blockSplit = $this->splitIntoBlock('A', $value);
1210 foreach ($blockSplit as $k => $v) {
1211 // Block
1212 if ($k % 2) {
1213 list($attribArray) = $this->get_tag_attributes($this->getFirstTag($v), true);
1214 // Checking if there is a scheme, and if not, prepend the current url.
1215 // ONLY do this if href has content - the <a> tag COULD be an anchor and if so, it should be preserved...
1216 if ($attribArray['href'] !== '') {
1217 $uP = parse_url(strtolower($attribArray['href']));
1218 if (!$uP['scheme']) {
1219 $attribArray['href'] = GeneralUtility::getIndpEnv('TYPO3_SITE_URL') . $attribArray['href'];
1220 }
1221 }
1222 $bTag = '<a ' . GeneralUtility::implodeAttributes($attribArray, true) . '>';
1223 $eTag = '</a>';
1224 $blockSplit[$k] = $bTag . $this->TS_AtagToAbs($this->removeFirstAndLastTag($blockSplit[$k])) . $eTag;
1225 }
1226 }
1227 return implode('', $blockSplit);
1228 }
1229
1230 /**
1231 * Apply plain image settings to the dimensions of the image
1232 *
1233 * @param array $imageInfo: info array of the image
1234 * @param array $attribArray: array of attributes of an image tag
1235 *
1236 * @return array a modified attributes array
1237 */
1238 protected function applyPlainImageModeSettings($imageInfo, $attribArray)
1239 {
1240 if ($this->procOptions['plainImageMode']) {
1241 // Perform corrections to aspect ratio based on configuration
1242 switch ((string)$this->procOptions['plainImageMode']) {
1243 case 'lockDimensions':
1244 $attribArray['width'] = $imageInfo[0];
1245 $attribArray['height'] = $imageInfo[1];
1246 break;
1247 case 'lockRatioWhenSmaller':
1248 if ($attribArray['width'] > $imageInfo[0]) {
1249 $attribArray['width'] = $imageInfo[0];
1250 }
1251 if ($imageInfo[0] > 0) {
1252 $attribArray['height'] = round($attribArray['width'] * ($imageInfo[1] / $imageInfo[0]));
1253 }
1254 break;
1255 case 'lockRatio':
1256 if ($imageInfo[0] > 0) {
1257 $attribArray['height'] = round($attribArray['width'] * ($imageInfo[1] / $imageInfo[0]));
1258 }
1259 break;
1260 }
1261 }
1262 return $attribArray;
1263 }
1264
1265 /**
1266 * Called before any processing / transformation is made
1267 * Removing any CRs (char 13) and only deal with LFs (char 10) internally.
1268 * CR has a very disturbing effect, so just remove all CR and rely on LF
1269 *
1270 * Historical note: Previously it was possible to disable this functionality via disableUnifyLineBreaks.
1271 *
1272 * @param string $content the content to process
1273 * @return string the modified content
1274 */
1275 protected function streamlineLineBreaksForProcessing(string $content)
1276 {
1277 return str_replace(CR, '', $content);
1278 }
1279
1280 /**
1281 * Called after any processing / transformation was made
1282 * just before the content is returned by the RTE parser all line breaks
1283 * get unified to be "CRLF"s again.
1284 *
1285 * Historical note: Previously it was possible to disable this functionality via disableUnifyLineBreaks.
1286 *
1287 * @param string $content the content to process
1288 * @return string the modified content
1289 */
1290 protected function streamlineLineBreaksAfterProcessing(string $content)
1291 {
1292 // Make sure no \r\n sequences has entered in the meantime
1293 $content = $this->streamlineLineBreaksForProcessing($content);
1294 // ... and then change all \n into \r\n
1295 return str_replace(LF, CRLF, $content);
1296 }
1297
1298 /**
1299 * Content Transformation from DB to RTE
1300 * Checks all <a> tags which reference a t3://page and checks if the page is available
1301 * If not, some offensive styling is added.
1302 *
1303 * @param string $content
1304 * @return string the modified content
1305 */
1306 protected function markBrokenLinks(string $content): string
1307 {
1308 $blocks = $this->splitIntoBlock('A', $content);
1309 $linkService = GeneralUtility::makeInstance(LinkService::class);
1310 foreach ($blocks as $position => $value) {
1311 if ($position % 2 === 0) {
1312 continue;
1313 }
1314 list($attributes) = $this->get_tag_attributes($this->getFirstTag($value), true);
1315 if (empty($attributes['href'])) {
1316 continue;
1317 }
1318 $hrefInformation = $linkService->resolve($attributes['href']);
1319 if ($hrefInformation['type'] === LinkService::TYPE_PAGE && $hrefInformation['pageuid'] !== 'current') {
1320 $pageRecord = BackendUtility::getRecord('pages', $hrefInformation['pageuid']);
1321 if (!is_array($pageRecord)) {
1322 // Page does not exist
1323 $attributes['data-rte-error'] = 'Page with ID ' . $hrefInformation['pageuid'] . ' not found';
1324 }
1325 }
1326 // Always rewrite the block to allow the nested calling even if a page is found
1327 $blocks[$position] =
1328 '<a ' . GeneralUtility::implodeAttributes($attributes, true, true) . '>'
1329 . $this->markBrokenLinks($this->removeFirstAndLastTag($blocks[$position]))
1330 . '</a>';
1331 }
1332 return implode('', $blocks);
1333 }
1334
1335 /**
1336 * Content Transformation from RTE to DB
1337 * Removes link information error attributes from <a> tags that are added to broken links
1338 *
1339 * @param string $content the content to process
1340 * @return string the modified content
1341 */
1342 protected function removeBrokenLinkMarkers(string $content): string
1343 {
1344 $blocks = $this->splitIntoBlock('A', $content);
1345 foreach ($blocks as $position => $value) {
1346 if ($position % 2 === 0) {
1347 continue;
1348 }
1349 list($attributes) = $this->get_tag_attributes($this->getFirstTag($value), true);
1350 if (empty($attributes['href'])) {
1351 continue;
1352 }
1353 // Always remove the styling again (regardless of the page was found or not)
1354 // so the database does not contain ugly stuff
1355 unset($attributes['data-rte-error']);
1356 if (isset($attributes['style'])) {
1357 $attributes['style'] = trim(str_replace('background-color: yellow; border:2px red solid; color: black;', '', $attributes['style']));
1358 if (empty($attributes['style'])) {
1359 unset($attributes['style']);
1360 }
1361 }
1362 $blocks[$position] =
1363 '<a ' . GeneralUtility::implodeAttributes($attributes, true, true) . '>'
1364 . $this->removeBrokenLinkMarkers($this->removeFirstAndLastTag($blocks[$position]))
1365 . '</a>';
1366 }
1367 return implode('', $blocks);
1368 }
1369 }