5bff145ea3d5e7185a8a1c3af06a4eb7332f00a5
[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 TYPO3\CMS\Backend\Utility\BackendUtility;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Core\Utility\StringUtility;
20 use TYPO3\CMS\Core\Resource;
21 use TYPO3\CMS\Frontend\Service\TypoLinkCodecService;
22
23 /**
24 * Class for parsing HTML for the Rich Text Editor. (also called transformations)
25 *
26 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
27 */
28 class RteHtmlParser extends \TYPO3\CMS\Core\Html\HtmlParser {
29
30 /**
31 * @var string
32 */
33 public $blockElementList = 'PRE,UL,OL,H1,H2,H3,H4,H5,H6,ADDRESS,DL,DD,HEADER,SECTION,FOOTER,NAV,ARTICLE,ASIDE';
34
35 /**
36 * Set this to the pid of the record manipulated by the class.
37 *
38 * @var int
39 */
40 public $recPid = 0;
41
42 /**
43 * Element reference [table]:[field], eg. "tt_content:bodytext"
44 *
45 * @var string
46 */
47 public $elRef = '';
48
49 /**
50 * Relative path
51 *
52 * @var string
53 */
54 public $relPath = '';
55
56 /**
57 * Relative back-path
58 *
59 * @var string
60 */
61 public $relBackPath = '';
62
63 /**
64 * Current Page TSConfig
65 *
66 * @var array
67 */
68 public $tsConfig = array();
69
70 /**
71 * Set to the TSconfig options coming from Page TSconfig
72 *
73 * @var array
74 */
75 public $procOptions = array();
76
77 /**
78 * Run-away brake for recursive calls.
79 *
80 * @var int
81 */
82 public $TS_transform_db_safecounter = 100;
83
84 /**
85 * Parameters from TCA types configuration related to the RTE
86 *
87 * @var string
88 */
89 public $rte_p = '';
90
91 /**
92 * Data caching for processing function
93 *
94 * @var array
95 */
96 public $getKeepTags_cache = array();
97
98 /**
99 * Storage of the allowed CSS class names in the RTE
100 *
101 * @var array
102 */
103 public $allowedClasses = array();
104
105 /**
106 * Set to tags to preserve from Page TSconfig configuration
107 *
108 * @var string
109 */
110 public $preserveTags = '';
111
112 /**
113 * Initialize, setting element reference and record PID
114 *
115 * @param string $elRef Element reference, eg "tt_content:bodytext
116 * @param int $recPid PID of the record (page id)
117 * @return void
118 */
119 public function init($elRef = '', $recPid = 0) {
120 $this->recPid = $recPid;
121 $this->elRef = $elRef;
122 }
123
124 /**
125 * Setting the ->relPath and ->relBackPath to proper values so absolute references to links and images can be converted to relative dittos.
126 * This is used when editing files with the RTE
127 *
128 * @param string $path The relative path from PATH_site to the place where the file being edited is. Eg. "fileadmin/static".
129 * @return void There is no output, it is set in internal variables. With the above example of "fileadmin/static" as input this will yield ->relPath to be "fileadmin/static/" and ->relBackPath to be "../../
130 * @TODO: Check if relPath and relBackPath are used for anything useful after removal of "static file edit" with #63818
131 */
132 public function setRelPath($path) {
133 $path = trim($path);
134 $path = preg_replace('/^\\//', '', $path);
135 $path = preg_replace('/\\/$/', '', $path);
136 if ($path) {
137 $this->relPath = $path;
138 $this->relBackPath = '';
139 $partsC = count(explode('/', $this->relPath));
140 for ($a = 0; $a < $partsC; $a++) {
141 $this->relBackPath .= '../';
142 }
143 $this->relPath .= '/';
144 }
145 }
146
147 /**
148 * Evaluate the environment for editing a staticFileEdit file.
149 * Called for almost all fields being saved in the database. Is called without
150 * an instance of \TYPO3\CMS\Core\Html\RteHtmlParser::evalWriteFile()
151 *
152 * @param array $pArr Parameters for the current field as found in types-config
153 * @param array $currentRecord Current record we are editing.
154 * @return mixed On success an array with various information is returned, otherwise a string with an error message
155 * @deprecated since TYPO3 CMS 7, will be removed in TYPO3 CMS 8
156 */
157 static public function evalWriteFile($pArr, $currentRecord) {
158 GeneralUtility::logDeprecatedFunction();
159 }
160
161 /**********************************************
162 *
163 * Main function
164 *
165 **********************************************/
166 /**
167 * Transform value for RTE based on specConf in the direction specified by $direction (rte/db)
168 * This is the main function called from tcemain and transfer data classes
169 *
170 * @param string Input value
171 * @param array Special configuration for a field; This is coming from the types-configuration of the field in the TCA. In the types-configuration you can setup features for the field rendering and in particular the RTE takes al its major configuration options from there!
172 * @param string 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.
173 * @param array Parsed TypoScript content configuring the RTE, probably coming from Page TSconfig.
174 * @return string Output value
175 */
176 public function RTE_transform($value, $specConf, $direction = 'rte', $thisConfig = array()) {
177 // Init:
178 $this->tsConfig = $thisConfig;
179 $this->procOptions = (array)$thisConfig['proc.'];
180 $this->preserveTags = strtoupper(implode(',', GeneralUtility::trimExplode(',', $this->procOptions['preserveTags'])));
181 // dynamic configuration of blockElementList
182 if ($this->procOptions['blockElementList']) {
183 $this->blockElementList = $this->procOptions['blockElementList'];
184 }
185 // Get parameters for rte_transformation:
186 $p = ($this->rte_p = BackendUtility::getSpecConfParametersFromArray($specConf['rte_transform']['parameters']));
187 // Setting modes:
188 if ((string)$this->procOptions['overruleMode'] !== '') {
189 $modes = array_unique(GeneralUtility::trimExplode(',', $this->procOptions['overruleMode']));
190 } else {
191 $modes = array_unique(GeneralUtility::trimExplode('-', $p['mode']));
192 }
193 $revmodes = array_flip($modes);
194 // Find special modes and extract them:
195 if (isset($revmodes['ts'])) {
196 $modes[$revmodes['ts']] = 'ts_transform,ts_preserve,ts_images,ts_links';
197 }
198 // Find special modes and extract them:
199 if (isset($revmodes['ts_css'])) {
200 $modes[$revmodes['ts_css']] = 'css_transform,ts_images,ts_links';
201 }
202 // Make list unique
203 $modes = array_unique(GeneralUtility::trimExplode(',', implode(',', $modes), TRUE));
204 // Reverse order if direction is "rte"
205 if ($direction == 'rte') {
206 $modes = array_reverse($modes);
207 }
208 // Getting additional HTML cleaner configuration. These are applied either before or after the main transformation is done and is thus totally independent processing options you can set up:
209 $entry_HTMLparser = $this->procOptions['entryHTMLparser_' . $direction] ? $this->HTMLparserConfig($this->procOptions['entryHTMLparser_' . $direction . '.']) : '';
210 $exit_HTMLparser = $this->procOptions['exitHTMLparser_' . $direction] ? $this->HTMLparserConfig($this->procOptions['exitHTMLparser_' . $direction . '.']) : '';
211 // Line breaks of content is unified into char-10 only (removing char 13)
212 if (!$this->procOptions['disableUnifyLineBreaks']) {
213 $value = str_replace(CRLF, LF, $value);
214 }
215 // In an entry-cleaner was configured, pass value through the HTMLcleaner with that:
216 if (is_array($entry_HTMLparser)) {
217 $value = $this->HTMLcleaner($value, $entry_HTMLparser[0], $entry_HTMLparser[1], $entry_HTMLparser[2], $entry_HTMLparser[3]);
218 }
219 // Traverse modes:
220 foreach ($modes as $cmd) {
221 // ->DB
222 if ($direction == 'db') {
223 // Checking for user defined transformation:
224 if ($_classRef = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_parsehtml_proc.php']['transformation'][$cmd]) {
225 $_procObj = GeneralUtility::getUserObj($_classRef);
226 $_procObj->pObj = $this;
227 $_procObj->transformationKey = $cmd;
228 $value = $_procObj->transform_db($value, $this);
229 } else {
230 // ... else use defaults:
231 switch ($cmd) {
232 case 'ts_images':
233 $value = $this->TS_images_db($value);
234 break;
235 case 'ts_reglinks':
236 $value = $this->TS_reglinks($value, 'db');
237 break;
238 case 'ts_links':
239 $value = $this->TS_links_db($value);
240 break;
241 case 'ts_preserve':
242 $value = $this->TS_preserve_db($value);
243 break;
244 case 'ts_transform':
245
246 case 'css_transform':
247 $this->allowedClasses = GeneralUtility::trimExplode(',', $this->procOptions['allowedClasses'], TRUE);
248 // CR has a very disturbing effect, so just remove all CR and rely on LF
249 $value = str_replace(CR, '', $value);
250 // Transform empty paragraphs into spacing paragraphs
251 $value = str_replace('<p></p>', '<p>&nbsp;</p>', $value);
252 // Double any trailing spacing paragraph so that it does not get removed by divideIntoLines()
253 $value = preg_replace('/<p>&nbsp;<\/p>$/', '<p>&nbsp;</p>' . '<p>&nbsp;</p>', $value);
254 $value = $this->TS_transform_db($value, $cmd == 'css_transform');
255 break;
256 case 'ts_strip':
257 $value = $this->TS_strip_db($value);
258 break;
259 default:
260 // Do nothing
261 }
262 }
263 }
264 // ->RTE
265 if ($direction == 'rte') {
266 // Checking for user defined transformation:
267 if ($_classRef = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_parsehtml_proc.php']['transformation'][$cmd]) {
268 $_procObj = GeneralUtility::getUserObj($_classRef);
269 $_procObj->pObj = $this;
270 $value = $_procObj->transform_rte($value, $this);
271 } else {
272 // ... else use defaults:
273 switch ($cmd) {
274 case 'ts_images':
275 $value = $this->TS_images_rte($value);
276 break;
277 case 'ts_reglinks':
278 $value = $this->TS_reglinks($value, 'rte');
279 break;
280 case 'ts_links':
281 $value = $this->TS_links_rte($value);
282 break;
283 case 'ts_preserve':
284 $value = $this->TS_preserve_rte($value);
285 break;
286 case 'ts_transform':
287
288 case 'css_transform':
289 // Has a very disturbing effect, so just remove all '13' - depend on '10'
290 $value = str_replace(CR, '', $value);
291 $value = $this->TS_transform_rte($value, $cmd == 'css_transform');
292 break;
293 default:
294 // Do nothing
295 }
296 }
297 }
298 }
299 // In an exit-cleaner was configured, pass value through the HTMLcleaner with that:
300 if (is_array($exit_HTMLparser)) {
301 $value = $this->HTMLcleaner($value, $exit_HTMLparser[0], $exit_HTMLparser[1], $exit_HTMLparser[2], $exit_HTMLparser[3]);
302 }
303 // Final clean up of linebreaks:
304 if (!$this->procOptions['disableUnifyLineBreaks']) {
305 // Make sure no \r\n sequences has entered in the meantime...
306 $value = str_replace(CRLF, LF, $value);
307 // ... and then change all \n into \r\n
308 $value = str_replace(LF, CRLF, $value);
309 }
310 // Return value:
311 return $value;
312 }
313
314 /************************************
315 *
316 * Specific RTE TRANSFORMATION functions
317 *
318 *************************************/
319 /**
320 * Transformation handler: 'ts_images' / direction: "db"
321 * Processing images inserted in the RTE.
322 * This is used when content goes from the RTE to the database.
323 * Images inserted in the RTE has an absolute URL applied to the src attribute. This URL is converted to a relative URL
324 * 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.
325 * Also "magic" images are processed here.
326 *
327 * @param string $value The content from RTE going to Database
328 * @return string Processed content
329 */
330 public function TS_images_db($value) {
331 // Split content by <img> tags and traverse the resulting array for processing:
332 $imgSplit = $this->splitTags('img', $value);
333 if (count($imgSplit) > 1) {
334 $siteUrl = $this->siteUrl();
335 $sitePath = str_replace(GeneralUtility::getIndpEnv('TYPO3_REQUEST_HOST'), '', $siteUrl);
336 /** @var $resourceFactory Resource\ResourceFactory */
337 $resourceFactory = Resource\ResourceFactory::getInstance();
338 /** @var $magicImageService Resource\Service\MagicImageService */
339 $magicImageService = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\Service\MagicImageService::class);
340 $magicImageService->setMagicImageMaximumDimensions($this->tsConfig);
341 foreach ($imgSplit as $k => $v) {
342 // Image found, do processing:
343 if ($k % 2) {
344 // Get attributes
345 $attribArray = $this->get_tag_attributes_classic($v, 1);
346 // It's always an absolute URL coming from the RTE into the Database.
347 $absoluteUrl = trim($attribArray['src']);
348 // Make path absolute if it is relative and we have a site path which is not '/'
349 $pI = pathinfo($absoluteUrl);
350 if ($sitePath && !$pI['scheme'] && GeneralUtility::isFirstPartOfStr($absoluteUrl, $sitePath)) {
351 // If site is in a subpath (eg. /~user_jim/) this path needs to be removed because it will be added with $siteUrl
352 $absoluteUrl = substr($absoluteUrl, strlen($sitePath));
353 $absoluteUrl = $siteUrl . $absoluteUrl;
354 }
355 // Image dimensions set in the img tag, if any
356 $imgTagDimensions = $this->getWHFromAttribs($attribArray);
357 if ($imgTagDimensions[0]) {
358 $attribArray['width'] = $imgTagDimensions[0];
359 }
360 if ($imgTagDimensions[1]) {
361 $attribArray['height'] = $imgTagDimensions[1];
362 }
363 $originalImageFile = NULL;
364 if ($attribArray['data-htmlarea-file-uid']) {
365 // An original image file uid is available
366 try {
367 /** @var $originalImageFile Resource\File */
368 $originalImageFile = $resourceFactory->getFileObject(intval($attribArray['data-htmlarea-file-uid']));
369 } catch (Resource\Exception\FileDoesNotExistException $fileDoesNotExistException) {
370 // Log the fact the file could not be retrieved.
371 $message = sprintf('Could not find file with uid "%s"', $attribArray['data-htmlarea-file-uid']);
372 $this->getLogger()->error($message);
373 }
374 }
375 if ($originalImageFile instanceof Resource\File) {
376 // Public url of local file is relative to the site url, absolute otherwise
377 if ($absoluteUrl == $originalImageFile->getPublicUrl() || $absoluteUrl == $siteUrl . $originalImageFile->getPublicUrl()) {
378 // This is a plain image, i.e. reference to the original image
379 if ($this->procOptions['plainImageMode']) {
380 // "plain image mode" is configured
381 // Find the dimensions of the original image
382 $imageInfo = array(
383 $originalImageFile->getProperty('width'),
384 $originalImageFile->getProperty('height')
385 );
386 if (!$imageInfo[0] || !$imageInfo[1]) {
387 $filePath = $originalImageFile->getForLocalProcessing(FALSE);
388 $imageInfo = @getimagesize($filePath);
389 }
390 $attribArray = $this->applyPlainImageModeSettings($imageInfo, $attribArray);
391 }
392 } else {
393 // Magic image case: get a processed file with the requested configuration
394 $imageConfiguration = array(
395 'width' => $imgTagDimensions[0],
396 'height' => $imgTagDimensions[1]
397 );
398 $magicImage = $magicImageService->createMagicImage($originalImageFile, $imageConfiguration);
399 $attribArray['width'] = $magicImage->getProperty('width');
400 $attribArray['height'] = $magicImage->getProperty('height');
401 $attribArray['src'] = $magicImage->getPublicUrl();
402 }
403 } elseif (!GeneralUtility::isFirstPartOfStr($absoluteUrl, $siteUrl) && !$this->procOptions['dontFetchExtPictures'] && TYPO3_MODE === 'BE') {
404 // External image from another URL: in that case, fetch image, unless the feature is disabled or we are not in backend mode
405 // Fetch the external image
406 $externalFile = $this->getUrl($absoluteUrl);
407 if ($externalFile) {
408 $pU = parse_url($absoluteUrl);
409 $pI = pathinfo($pU['path']);
410 if (GeneralUtility::inList('gif,png,jpeg,jpg', strtolower($pI['extension']))) {
411 $fileName = GeneralUtility::shortMD5($absoluteUrl) . '.' . $pI['extension'];
412 // We insert this image into the user default upload folder
413 $folder = $GLOBALS['BE_USER']->getDefaultUploadFolder();
414 $fileObject = $folder->createFile($fileName)->setContents($externalFile);
415 $imageConfiguration = array(
416 'width' => $attribArray['width'],
417 'height' => $attribArray['height']
418 );
419 $magicImage = $magicImageService->createMagicImage($fileObject, $imageConfiguration);
420 $attribArray['width'] = $magicImage->getProperty('width');
421 $attribArray['height'] = $magicImage->getProperty('height');
422 $attribArray['data-htmlarea-file-uid'] = $fileObject->getUid();
423 $attribArray['src'] = $magicImage->getPublicUrl();
424 }
425 }
426 } elseif (GeneralUtility::isFirstPartOfStr($absoluteUrl, $siteUrl)) {
427 // Finally, check image as local file (siteURL equals the one of the image)
428 // Image has no data-htmlarea-file-uid attribute
429 // Relative path, rawurldecoded for special characters.
430 $path = rawurldecode(substr($absoluteUrl, strlen($siteUrl)));
431 // Absolute filepath, locked to relative path of this project
432 $filepath = GeneralUtility::getFileAbsFileName($path);
433 // Check file existence (in relative directory to this installation!)
434 if ($filepath && @is_file($filepath)) {
435 // Treat it as a plain image
436 if ($this->procOptions['plainImageMode']) {
437 // If "plain image mode" has been configured
438 // Find the original dimensions of the image
439 $imageInfo = @getimagesize($filepath);
440 $attribArray = $this->applyPlainImageModeSettings($imageInfo, $attribArray);
441 }
442 // Let's try to find a file uid for this image
443 try {
444 $fileOrFolderObject = $resourceFactory->retrieveFileOrFolderObject($path);
445 if ($fileOrFolderObject instanceof Resource\FileInterface) {
446 $fileIdentifier = $fileOrFolderObject->getIdentifier();
447 $fileObject = $fileOrFolderObject->getStorage()->getFile($fileIdentifier);
448 // @todo if the retrieved file is a processed file, get the original file...
449 $attribArray['data-htmlarea-file-uid'] = $fileObject->getUid();
450 }
451 } catch (Resource\Exception\ResourceDoesNotExistException $resourceDoesNotExistException) {
452 // Nothing to be done if file/folder not found
453 }
454 }
455 }
456 // Remove width and heigth from style attribute
457 $attribArray['style'] = preg_replace('/((?:^|)\\s*(?:width|height)\\s*:[^;]*(?:$|;))/si', '', $attribArray['style']);
458 // Must have alt attribute
459 if (!isset($attribArray['alt'])) {
460 $attribArray['alt'] = '';
461 }
462 // Convert absolute to relative url
463 if (GeneralUtility::isFirstPartOfStr($attribArray['src'], $siteUrl)) {
464 $attribArray['src'] = $this->relBackPath . substr($attribArray['src'], strlen($siteUrl));
465 }
466 $imgSplit[$k] = '<img ' . GeneralUtility::implodeAttributes($attribArray, 1, 1) . ' />';
467 }
468 }
469 }
470 return implode('', $imgSplit);
471 }
472
473 /**
474 * Transformation handler: 'ts_images' / direction: "rte"
475 * Processing images from database content going into the RTE.
476 * Processing includes converting the src attribute to an absolute URL.
477 *
478 * @param string $value Content input
479 * @return string Content output
480 */
481 public function TS_images_rte($value) {
482 // Split content by <img> tags and traverse the resulting array for processing:
483 $imgSplit = $this->splitTags('img', $value);
484 if (count($imgSplit) > 1) {
485 $siteUrl = $this->siteUrl();
486 $sitePath = str_replace(GeneralUtility::getIndpEnv('TYPO3_REQUEST_HOST'), '', $siteUrl);
487 foreach ($imgSplit as $k => $v) {
488 // Image found
489 if ($k % 2) {
490 // Get the attributes of the img tag
491 $attribArray = $this->get_tag_attributes_classic($v, 1);
492 $absoluteUrl = trim($attribArray['src']);
493 // Transform the src attribute into an absolute url, if it not already
494 if (strtolower(substr($absoluteUrl, 0, 4)) !== 'http') {
495 $attribArray['src'] = substr($attribArray['src'], strlen($this->relBackPath));
496 // If site is in a subpath (eg. /~user_jim/) this path needs to be removed because it will be added with $siteUrl
497 $attribArray['src'] = preg_replace('#^' . preg_quote($sitePath, '#') . '#', '', $attribArray['src']);
498 $attribArray['src'] = $siteUrl . $attribArray['src'];
499 }
500 // Must have alt attribute
501 if (!isset($attribArray['alt'])) {
502 $attribArray['alt'] = '';
503 }
504 $imgSplit[$k] = '<img ' . GeneralUtility::implodeAttributes($attribArray, 1, 1) . ' />';
505 }
506 }
507 }
508 // Return processed content:
509 return implode('', $imgSplit);
510 }
511
512 /**
513 * Transformation handler: 'ts_reglinks' / direction: "db"+"rte" depending on $direction variable.
514 * Converting <A>-tags to/from abs/rel
515 *
516 * @param string $value Content input
517 * @param string $direction Direction of conversion; "rte" (from database to RTE) or "db" (from RTE to database)
518 * @return string Content output
519 */
520 public function TS_reglinks($value, $direction) {
521 $retVal = '';
522 switch ($direction) {
523 case 'rte':
524 $retVal = $this->TS_AtagToAbs($value, 1);
525 break;
526 case 'db':
527 $siteURL = $this->siteUrl();
528 $blockSplit = $this->splitIntoBlock('A', $value);
529 foreach ($blockSplit as $k => $v) {
530 // Block
531 if ($k % 2) {
532 $attribArray = $this->get_tag_attributes_classic($this->getFirstTag($v), 1);
533 // If the url is local, remove url-prefix
534 if ($siteURL && substr($attribArray['href'], 0, strlen($siteURL)) == $siteURL) {
535 $attribArray['href'] = $this->relBackPath . substr($attribArray['href'], strlen($siteURL));
536 }
537 $bTag = '<a ' . GeneralUtility::implodeAttributes($attribArray, 1) . '>';
538 $eTag = '</a>';
539 $blockSplit[$k] = $bTag . $this->TS_reglinks($this->removeFirstAndLastTag($blockSplit[$k]), $direction) . $eTag;
540 }
541 }
542 $retVal = implode('', $blockSplit);
543 break;
544 }
545 return $retVal;
546 }
547
548 /**
549 * Transformation handler: 'ts_links' / direction: "db"
550 * Converting <A>-tags to <link tags>
551 *
552 * @param string $value Content input
553 * @return string Content output
554 * @see TS_links_rte()
555 */
556 public function TS_links_db($value) {
557 $conf = array();
558 // Split content into <a> tag blocks and process:
559 $blockSplit = $this->splitIntoBlock('A', $value);
560 foreach ($blockSplit as $k => $v) {
561 // If an A-tag was found:
562 if ($k % 2) {
563 $attribArray = $this->get_tag_attributes_classic($this->getFirstTag($v), 1);
564 $info = $this->urlInfoForLinkTags($attribArray['href']);
565 // Check options:
566 $attribArray_copy = $attribArray;
567 unset($attribArray_copy['href']);
568 unset($attribArray_copy['target']);
569 unset($attribArray_copy['class']);
570 unset($attribArray_copy['title']);
571 unset($attribArray_copy['data-htmlarea-external']);
572 // Unset "rteerror" and "style" attributes if "rteerror" is set!
573 if ($attribArray_copy['rteerror']) {
574 unset($attribArray_copy['style']);
575 unset($attribArray_copy['rteerror']);
576 }
577 // Remove additional parameters
578 if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_parsehtml_proc.php']['removeParams_PostProc']) && is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_parsehtml_proc.php']['removeParams_PostProc'])) {
579 $parameters = array(
580 'conf' => &$conf,
581 'aTagParams' => &$attribArray_copy
582 );
583 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_parsehtml_proc.php']['removeParams_PostProc'] as $objRef) {
584 $processor = GeneralUtility::getUserObj($objRef);
585 $attribArray_copy = $processor->removeParams($parameters, $this);
586 }
587 }
588 // Only if href, target, class and tile are the only attributes, we can alter the link!
589 if (!count($attribArray_copy)) {
590 // Quoting class and title attributes if they contain spaces
591 $attribArray['class'] = preg_match('/ /', $attribArray['class']) ? '"' . $attribArray['class'] . '"' : $attribArray['class'];
592 $attribArray['title'] = preg_match('/ /', $attribArray['title']) ? '"' . $attribArray['title'] . '"' : $attribArray['title'];
593 // Creating the TYPO3 pseudo-tag "<LINK>" for the link (includes href/url, target and class attributes):
594 // If data-htmlarea-external attribute is set, keep the href unchanged
595 if ($attribArray['data-htmlarea-external']) {
596 $href = $attribArray['href'];
597 } else {
598 $href = $info['url'] . ($info['query'] ? ',0,' . $info['query'] : '');
599 }
600 $bTag = '<link ' . $href . ($attribArray['target'] ? ' ' . $attribArray['target'] : ($attribArray['class'] || $attribArray['title'] ? ' -' : '')) . ($attribArray['class'] ? ' ' . $attribArray['class'] : ($attribArray['title'] ? ' -' : '')) . ($attribArray['title'] ? ' ' . $attribArray['title'] : '') . '>';
601 $eTag = '</link>';
602 // Modify parameters
603 if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_parsehtml_proc.php']['modifyParams_LinksDb_PostProc']) && is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_parsehtml_proc.php']['modifyParams_LinksDb_PostProc'])) {
604 $parameters = array(
605 'conf' => &$conf,
606 'currentBlock' => $v,
607 'url' => $href,
608 'attributes' => $attribArray
609 );
610 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_parsehtml_proc.php']['modifyParams_LinksDb_PostProc'] as $objRef) {
611 $processor = GeneralUtility::getUserObj($objRef);
612 $blockSplit[$k] = $processor->modifyParamsLinksDb($parameters, $this);
613 }
614 } else {
615 $blockSplit[$k] = $bTag . $this->TS_links_db($this->removeFirstAndLastTag($blockSplit[$k])) . $eTag;
616 }
617 } else {
618 // ... otherwise store the link as a-tag.
619 // Unsetting 'rtekeep' attribute if that had been set.
620 unset($attribArray['rtekeep']);
621 if (!$attribArray['data-htmlarea-external']) {
622 $siteURL = $this->siteUrl();
623 // If the url is local, remove url-prefix
624 if ($siteURL && substr($attribArray['href'], 0, strlen($siteURL)) == $siteURL) {
625 $attribArray['href'] = $this->relBackPath . substr($attribArray['href'], strlen($siteURL));
626 }
627 // Check for FAL link-handler keyword
628 list($linkHandlerKeyword, $linkHandlerValue) = explode(':', $attribArray['href'], 2);
629 if ($linkHandlerKeyword === '?file') {
630 try {
631 $fileOrFolderObject = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->retrieveFileOrFolderObject(rawurldecode($linkHandlerValue));
632 if ($fileOrFolderObject instanceof \TYPO3\CMS\Core\Resource\FileInterface || $fileOrFolderObject instanceof \TYPO3\CMS\Core\Resource\Folder) {
633 $attribArray['href'] = $fileOrFolderObject->getPublicUrl();
634 }
635 } catch (\TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException $resourceDoesNotExistException) {
636 // The indentifier inserted in the RTE is already gone...
637 }
638 }
639 }
640 unset($attribArray['data-htmlarea-external']);
641 $bTag = '<a ' . GeneralUtility::implodeAttributes($attribArray, 1) . '>';
642 $eTag = '</a>';
643 $blockSplit[$k] = $bTag . $this->TS_links_db($this->removeFirstAndLastTag($blockSplit[$k])) . $eTag;
644 }
645 }
646 }
647 return implode('', $blockSplit);
648 }
649
650 /**
651 * Transformation handler: 'ts_links' / direction: "rte"
652 * Converting <link tags> to <A>-tags
653 *
654 * @param string $value Content input
655 * @return string Content output
656 * @see TS_links_rte()
657 */
658 public function TS_links_rte($value) {
659 $conf = array();
660 $value = $this->TS_AtagToAbs($value);
661 // Split content by the TYPO3 pseudo tag "<link>":
662 $blockSplit = $this->splitIntoBlock('link', $value, 1);
663 $siteUrl = $this->siteUrl();
664 foreach ($blockSplit as $k => $v) {
665 $error = '';
666 $external = FALSE;
667 // Block
668 if ($k % 2) {
669 // split away the first "<link" part
670 $typolink = explode(' ', substr($this->getFirstTag($v), 0, -1), 2)[1];
671 $tagCode = GeneralUtility::makeInstance(TypoLinkCodecService::class)->decode($typolink);
672
673 $link_param = $tagCode['url'];
674 // Parsing the typolink data. This parsing is roughly done like in \TYPO3\CMS\Frontend\ContentObject->typolink()
675 // Parse URL:
676 $pU = parse_url($link_param);
677 if (strstr($link_param, '@') && (!$pU['scheme'] || $pU['scheme'] == 'mailto')) {
678 // mailadr
679 $href = 'mailto:' . preg_replace('/^mailto:/i', '', $link_param);
680 } elseif ($link_param[0] === '#') {
681 // check if anchor
682 $href = $siteUrl . $link_param;
683 } else {
684 // Check for FAL link-handler keyword:
685 list($linkHandlerKeyword, $linkHandlerValue) = explode(':', trim($link_param), 2);
686 if ($linkHandlerKeyword === 'file' && !StringUtility::beginsWith($link_param, 'file://')) {
687 $href = $siteUrl . '?' . $linkHandlerKeyword . ':' . rawurlencode($linkHandlerValue);
688 } else {
689 $fileChar = (int)strpos($link_param, '/');
690 $urlChar = (int)strpos($link_param, '.');
691 // Detects if a file is found in site-root.
692 list($rootFileDat) = explode('?', $link_param);
693 $rFD_fI = pathinfo($rootFileDat);
694 if (trim($rootFileDat) && !strstr($link_param, '/') && (@is_file((PATH_site . $rootFileDat)) || GeneralUtility::inList('php,html,htm', strtolower($rFD_fI['extension'])))) {
695 $href = $siteUrl . $link_param;
696 } elseif (
697 (
698 $pU['scheme']
699 && !isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_content.php']['typolinkLinkHandler'][$pU['scheme']])
700 )
701 || $urlChar && (!$fileChar || $urlChar < $fileChar)
702 ) {
703 // url (external): if has scheme or if a '.' comes before a '/'.
704 $href = $link_param;
705 if (!$pU['scheme']) {
706 $href = 'http://' . $href;
707 }
708 $external = TRUE;
709 } elseif ($fileChar) {
710 // It is an internal file or folder
711 // Try to transform the href into a FAL reference
712 try {
713 $fileOrFolderObject = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->retrieveFileOrFolderObject($link_param);
714 } catch (\TYPO3\CMS\Core\Resource\Exception $exception) {
715 // Nothing to be done if file/folder not found or path invalid
716 $fileOrFolderObject = NULL;
717 }
718 if ($fileOrFolderObject instanceof \TYPO3\CMS\Core\Resource\Folder) {
719 // It's a folder
720 $folderIdentifier = $fileOrFolderObject->getIdentifier();
721 $href = $siteUrl . '?file:' . rawurlencode($folderIdentifier);
722 } elseif ($fileOrFolderObject instanceof \TYPO3\CMS\Core\Resource\FileInterface) {
723 // It's a file
724 $fileIdentifier = $fileOrFolderObject->getIdentifier();
725 $fileObject = $fileOrFolderObject->getStorage()->getFile($fileIdentifier);
726 $href = $siteUrl . '?file:' . $fileObject->getUid();
727 } else {
728 $href = $siteUrl . $link_param;
729 }
730 } else {
731 // integer or alias (alias is without slashes or periods or commas, that is 'nospace,alphanum_x,lower,unique' according to tables.php!!)
732 // Splitting the parameter by ',' and if the array counts more than 1 element it's a id/type/parameters triplet
733 $pairParts = GeneralUtility::trimExplode(',', $link_param, TRUE);
734 $idPart = $pairParts[0];
735 $link_params_parts = explode('#', $idPart);
736 $idPart = trim($link_params_parts[0]);
737 $sectionMark = trim($link_params_parts[1]);
738 if ((string)$idPart === '') {
739 $idPart = $this->recPid;
740 }
741 // If no id or alias is given, set it to class record pid
742 // Checking if the id-parameter is an alias.
743 if (!\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($idPart)) {
744 list($idPartR) = BackendUtility::getRecordsByField('pages', 'alias', $idPart);
745 $idPart = (int)$idPartR['uid'];
746 }
747 $page = BackendUtility::getRecord('pages', $idPart);
748 if (is_array($page)) {
749 // Page must exist...
750 $href = $siteUrl . '?id=' . $idPart . ($pairParts[2] ? $pairParts[2] : '') . ($sectionMark ? '#' . $sectionMark : '');
751 } elseif (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_content.php']['typolinkLinkHandler'][array_shift(explode(':', $link_param))])) {
752 $href = $link_param;
753 } else {
754 $href = $siteUrl . '?id=' . $link_param;
755 $error = 'No page found: ' . $idPart;
756 }
757 }
758 }
759 }
760 // Setting the A-tag:
761 $bTag = '<a href="' . htmlspecialchars($href) . '"'
762 . ($tagCode['target'] ? ' target="' . htmlspecialchars($tagCode['target']) . '"' : '')
763 . ($tagCode['class'] ? ' class="' . htmlspecialchars($tagCode['class']) . '"' : '')
764 . ($tagCode['title'] ? ' title="' . htmlspecialchars($tagCode['title']) . '"' : '')
765 . ($external ? ' data-htmlarea-external="1"' : '')
766 . ($error ? ' rteerror="' . htmlspecialchars($error) . '" style="background-color: yellow; border:2px red solid; color: black;"' : '') . '>';
767 $eTag = '</a>';
768 // Modify parameters
769 if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_parsehtml_proc.php']['modifyParams_LinksRte_PostProc']) && is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_parsehtml_proc.php']['modifyParams_LinksRte_PostProc'])) {
770 $parameters = array(
771 'conf' => &$conf,
772 'currentBlock' => $v,
773 'url' => $href,
774 'tagCode' => $tagCode,
775 'external' => $external,
776 'error' => $error
777 );
778 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_parsehtml_proc.php']['modifyParams_LinksRte_PostProc'] as $objRef) {
779 $processor = GeneralUtility::getUserObj($objRef);
780 $blockSplit[$k] = $processor->modifyParamsLinksRte($parameters, $this);
781 }
782 } else {
783 $blockSplit[$k] = $bTag . $this->TS_links_rte($this->removeFirstAndLastTag($blockSplit[$k])) . $eTag;
784 }
785 }
786 }
787 // Return content:
788 return implode('', $blockSplit);
789 }
790
791 /**
792 * Preserve special tags
793 *
794 * @param string $value Content input
795 * @return string Content output
796 */
797 public function TS_preserve_db($value) {
798 if (!$this->preserveTags) {
799 return $value;
800 }
801 // Splitting into blocks for processing (span-tags are used for special tags)
802 $blockSplit = $this->splitIntoBlock('span', $value);
803 foreach ($blockSplit as $k => $v) {
804 // Block
805 if ($k % 2) {
806 $attribArray = $this->get_tag_attributes_classic($this->getFirstTag($v));
807 if ($attribArray['specialtag']) {
808 $theTag = rawurldecode($attribArray['specialtag']);
809 $theTagName = $this->getFirstTagName($theTag);
810 $blockSplit[$k] = $theTag . $this->removeFirstAndLastTag($blockSplit[$k]) . '</' . $theTagName . '>';
811 }
812 }
813 }
814 return implode('', $blockSplit);
815 }
816
817 /**
818 * Preserve special tags
819 *
820 * @param string $value Content input
821 * @return string Content output
822 */
823 public function TS_preserve_rte($value) {
824 if (!$this->preserveTags) {
825 return $value;
826 }
827 $blockSplit = $this->splitIntoBlock($this->preserveTags, $value);
828 foreach ($blockSplit as $k => $v) {
829 // Block
830 if ($k % 2) {
831 $blockSplit[$k] = '<span specialtag="' . rawurlencode($this->getFirstTag($v)) . '">' . $this->removeFirstAndLastTag($blockSplit[$k]) . '</span>';
832 }
833 }
834 return implode('', $blockSplit);
835 }
836
837 /**
838 * Transformation handler: 'ts_transform' + 'css_transform' / direction: "db"
839 * Cleaning (->db) for standard content elements (ts)
840 *
841 * @param string $value Content input
842 * @param bool $css If TRUE, the transformation was "css_transform", otherwise "ts_transform
843 * @return string Content output
844 * @see TS_transform_rte()
845 */
846 public function TS_transform_db($value, $css = FALSE) {
847 // Safety... so forever loops are avoided (they should not occur, but an error would potentially do this...)
848 $this->TS_transform_db_safecounter--;
849 if ($this->TS_transform_db_safecounter < 0) {
850 return $value;
851 }
852 // Split the content from RTE by the occurrence of these blocks:
853 $blockSplit = $this->splitIntoBlock('TABLE,BLOCKQUOTE,' . ($this->procOptions['preserveDIVSections'] ? 'DIV,' : '') . $this->blockElementList, $value);
854 $cc = 0;
855 $aC = count($blockSplit);
856 // Avoid superfluous linebreaks by transform_db after ending headListTag
857 while ($aC && trim($blockSplit[($aC - 1)]) === '') {
858 unset($blockSplit[$aC - 1]);
859 $aC = count($blockSplit);
860 }
861 // Traverse the blocks
862 foreach ($blockSplit as $k => $v) {
863 $cc++;
864 $lastBR = $cc == $aC ? '' : LF;
865 if ($k % 2) {
866 // Inside block:
867 // Init:
868 $tag = $this->getFirstTag($v);
869 $tagName = strtolower($this->getFirstTagName($v));
870 // Process based on the tag:
871 switch ($tagName) {
872 case 'blockquote':
873
874 case 'dd':
875
876 case 'div':
877
878 case 'header':
879
880 case 'section':
881
882 case 'footer':
883
884 case 'nav':
885
886 case 'article':
887
888 case 'aside':
889 $blockSplit[$k] = $tag . $this->TS_transform_db($this->removeFirstAndLastTag($blockSplit[$k]), $css) . '</' . $tagName . '>' . $lastBR;
890 break;
891 case 'ol':
892
893 case 'ul':
894 // Transform lists into <typolist>-tags:
895 if (!$css) {
896 if (!isset($this->procOptions['typolist']) || $this->procOptions['typolist']) {
897 $parts = $this->getAllParts($this->splitIntoBlock('LI', $this->removeFirstAndLastTag($blockSplit[$k])), 1, 0);
898 foreach ($parts as $k2 => $value) {
899 $parts[$k2] = preg_replace('/[' . LF . CR . ']+/', '', $parts[$k2]);
900 // remove all linesbreaks!
901 $parts[$k2] = $this->defaultTStagMapping($parts[$k2], 'db');
902 $parts[$k2] = $this->cleanFontTags($parts[$k2], 0, 0, 0);
903 $parts[$k2] = $this->HTMLcleaner_db($parts[$k2], strtolower($this->procOptions['allowTagsInTypolists'] ? $this->procOptions['allowTagsInTypolists'] : 'br,font,b,i,u,a,img,span,strong,em'));
904 }
905 if ($tagName == 'ol') {
906 $params = ' type="1"';
907 } else {
908 $params = '';
909 }
910 $blockSplit[$k] = '<typolist' . $params . '>' . LF . implode(LF, $parts) . LF . '</typolist>' . $lastBR;
911 }
912 } else {
913 $blockSplit[$k] = preg_replace(('/[' . LF . CR . ']+/'), ' ', $this->transformStyledATags($blockSplit[$k])) . $lastBR;
914 }
915 break;
916 case 'table':
917 // Tables are NOT allowed in any form (unless preserveTables is set or CSS is the mode)
918 if (!$this->procOptions['preserveTables'] && !$css) {
919 $blockSplit[$k] = $this->TS_transform_db($this->removeTables($blockSplit[$k]));
920 } else {
921 $blockSplit[$k] = preg_replace(('/[' . LF . CR . ']+/'), ' ', $this->transformStyledATags($blockSplit[$k])) . $lastBR;
922 }
923 break;
924 case 'h1':
925
926 case 'h2':
927
928 case 'h3':
929
930 case 'h4':
931
932 case 'h5':
933
934 case 'h6':
935 if (!$css) {
936 $attribArray = $this->get_tag_attributes_classic($tag);
937 // Processing inner content here:
938 $innerContent = $this->HTMLcleaner_db($this->removeFirstAndLastTag($blockSplit[$k]));
939 if (!isset($this->procOptions['typohead']) || $this->procOptions['typohead']) {
940 $type = (int)substr($tagName, 1);
941 $blockSplit[$k] = '<typohead' . ($type != 6 ? ' type="' . $type . '"' : '') . ($attribArray['align'] ? ' align="' . $attribArray['align'] . '"' : '') . ($attribArray['class'] ? ' class="' . $attribArray['class'] . '"' : '') . '>' . $innerContent . '</typohead>' . $lastBR;
942 } else {
943 $blockSplit[$k] = '<' . $tagName . ($attribArray['align'] ? ' align="' . htmlspecialchars($attribArray['align']) . '"' : '') . ($attribArray['class'] ? ' class="' . htmlspecialchars($attribArray['class']) . '"' : '') . '>' . $innerContent . '</' . $tagName . '>' . $lastBR;
944 }
945 } else {
946 // Eliminate true linebreaks inside Hx tags
947 $blockSplit[$k] = preg_replace(('/[' . LF . CR . ']+/'), ' ', $this->transformStyledATags($blockSplit[$k])) . $lastBR;
948 }
949 break;
950 default:
951 // Eliminate true linebreaks inside other headlist tags
952 $blockSplit[$k] = preg_replace(('/[' . LF . CR . ']+/'), ' ', $this->transformStyledATags($blockSplit[$k])) . $lastBR;
953 }
954 } else {
955 // NON-block:
956 if (trim($blockSplit[$k]) !== '') {
957 $blockSplit[$k] = preg_replace('/<hr\\/>/', '<hr />', $blockSplit[$k]);
958 // Remove linebreaks preceding hr tags
959 $blockSplit[$k] = preg_replace('/[' . LF . CR . ']+<(hr)(\\s[^>\\/]*)?[[:space:]]*\\/?>/', '<$1$2/>', $blockSplit[$k]);
960 // Remove linebreaks following hr tags
961 $blockSplit[$k] = preg_replace('/<(hr)(\\s[^>\\/]*)?[[:space:]]*\\/?>[' . LF . CR . ']+/', '<$1$2/>', $blockSplit[$k]);
962 // Replace other linebreaks with space
963 $blockSplit[$k] = preg_replace('/[' . LF . CR . ']+/', ' ', $blockSplit[$k]);
964 $blockSplit[$k] = $this->divideIntoLines($blockSplit[$k]) . $lastBR;
965 $blockSplit[$k] = $this->transformStyledATags($blockSplit[$k]);
966 } else {
967 unset($blockSplit[$k]);
968 }
969 }
970 }
971 $this->TS_transform_db_safecounter++;
972 return implode('', $blockSplit);
973 }
974
975 /**
976 * Wraps a-tags that contain a style attribute with a span-tag
977 *
978 * @param string $value Content input
979 * @return string Content output
980 */
981 public function transformStyledATags($value) {
982 $blockSplit = $this->splitIntoBlock('A', $value);
983 foreach ($blockSplit as $k => $v) {
984 // If an A-tag was found
985 if ($k % 2) {
986 $attribArray = $this->get_tag_attributes_classic($this->getFirstTag($v), 1);
987 // If "style" attribute is set and rteerror is not set!
988 if ($attribArray['style'] && !$attribArray['rteerror']) {
989 $attribArray_copy['style'] = $attribArray['style'];
990 unset($attribArray['style']);
991 $bTag = '<span ' . GeneralUtility::implodeAttributes($attribArray_copy, 1) . '><a ' . GeneralUtility::implodeAttributes($attribArray, 1) . '>';
992 $eTag = '</a></span>';
993 $blockSplit[$k] = $bTag . $this->removeFirstAndLastTag($blockSplit[$k]) . $eTag;
994 }
995 }
996 }
997 return implode('', $blockSplit);
998 }
999
1000 /**
1001 * Transformation handler: 'ts_transform' + 'css_transform' / direction: "rte"
1002 * Set (->rte) for standard content elements (ts)
1003 *
1004 * @param string Content input
1005 * @param bool If TRUE, the transformation was "css_transform", otherwise "ts_transform
1006 * @return string Content output
1007 * @see TS_transform_db()
1008 */
1009 public function TS_transform_rte($value, $css = 0) {
1010 // Split the content from database by the occurrence of the block elements
1011 $blockElementList = 'TABLE,BLOCKQUOTE,TYPOLIST,TYPOHEAD,' . ($this->procOptions['preserveDIVSections'] ? 'DIV,' : '') . $this->blockElementList;
1012 $blockSplit = $this->splitIntoBlock($blockElementList, $value);
1013 // Traverse the blocks
1014 foreach ($blockSplit as $k => $v) {
1015 if ($k % 2) {
1016 // Inside one of the blocks:
1017 // Init:
1018 $tag = $this->getFirstTag($v);
1019 $tagName = strtolower($this->getFirstTagName($v));
1020 $attribArray = $this->get_tag_attributes_classic($tag);
1021 // Based on tagname, we do transformations:
1022 switch ($tagName) {
1023 case 'blockquote':
1024
1025 case 'dd':
1026
1027 case 'div':
1028
1029 case 'header':
1030
1031 case 'section':
1032
1033 case 'footer':
1034
1035 case 'nav':
1036
1037 case 'article':
1038
1039 case 'aside':
1040 $blockSplit[$k] = $tag . $this->TS_transform_rte($this->removeFirstAndLastTag($blockSplit[$k]), $css) . '</' . $tagName . '>';
1041 break;
1042 case 'typolist':
1043 // Transform typolist blocks into OL/UL lists. Type 1 is expected to be numerical block
1044 if (!isset($this->procOptions['typolist']) || $this->procOptions['typolist']) {
1045 $tListContent = $this->removeFirstAndLastTag($blockSplit[$k]);
1046 $tListContent = preg_replace('/^[ ]*' . LF . '/', '', $tListContent);
1047 $tListContent = preg_replace('/' . LF . '[ ]*$/', '', $tListContent);
1048 $lines = explode(LF, $tListContent);
1049 $typ = $attribArray['type'] == 1 ? 'ol' : 'ul';
1050 $blockSplit[$k] = '<' . $typ . '>' . LF . '<li>' . implode(('</li>' . LF . '<li>'), $lines) . '</li>' . '</' . $typ . '>';
1051 }
1052 break;
1053 case 'typohead':
1054 // Transform typohead into Hx tags.
1055 if (!isset($this->procOptions['typohead']) || $this->procOptions['typohead']) {
1056 $tC = $this->removeFirstAndLastTag($blockSplit[$k]);
1057 $typ = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($attribArray['type'], 0, 6);
1058 if (!$typ) {
1059 $typ = 6;
1060 }
1061 $align = $attribArray['align'] ? ' align="' . $attribArray['align'] . '"' : '';
1062 $class = $attribArray['class'] ? ' class="' . $attribArray['class'] . '"' : '';
1063 $blockSplit[$k] = '<h' . $typ . $align . $class . '>' . $tC . '</h' . $typ . '>';
1064 }
1065 break;
1066 }
1067 $blockSplit[$k + 1] = preg_replace('/^[ ]*' . LF . '/', '', $blockSplit[$k + 1]);
1068 } else {
1069 // NON-block:
1070 $nextFTN = $this->getFirstTagName($blockSplit[$k + 1]);
1071 $onlyLineBreaks = (preg_match('/^[ ]*' . LF . '+[ ]*$/', $blockSplit[$k]) == 1);
1072 // If the line is followed by a block or is the last line:
1073 if (GeneralUtility::inList($blockElementList, $nextFTN) || !isset($blockSplit[$k + 1])) {
1074 // If the line contains more than just linebreaks, reduce the number of trailing linebreaks by 1
1075 if (!$onlyLineBreaks) {
1076 $blockSplit[$k] = preg_replace('/(' . LF . '*)' . LF . '[ ]*$/', '$1', $blockSplit[$k]);
1077 } else {
1078 // If the line contains only linebreaks, remove the leading linebreak
1079 $blockSplit[$k] = preg_replace('/^[ ]*' . LF . '/', '', $blockSplit[$k]);
1080 }
1081 }
1082 // If $blockSplit[$k] is blank then unset the line, unless the line only contained linebreaks
1083 if ((string)$blockSplit[$k] === '' && !$onlyLineBreaks) {
1084 unset($blockSplit[$k]);
1085 } else {
1086 $blockSplit[$k] = $this->setDivTags($blockSplit[$k], $this->procOptions['useDIVasParagraphTagForRTE'] ? 'div' : 'p');
1087 }
1088 }
1089 }
1090 return implode(LF, $blockSplit);
1091 }
1092
1093 /**
1094 * Transformation handler: 'ts_strip' / direction: "db"
1095 * Removing all non-allowed tags
1096 *
1097 * @param string $value Content input
1098 * @return string Content output
1099 */
1100 public function TS_strip_db($value) {
1101 $value = strip_tags($value, '<' . implode('><', explode(',', 'b,i,u,a,img,br,div,center,pre,font,hr,sub,sup,p,strong,em,li,ul,ol,blockquote')) . '>');
1102 return $value;
1103 }
1104
1105 /***************************************************************
1106 *
1107 * Generic RTE transformation, analysis and helper functions
1108 *
1109 **************************************************************/
1110 /**
1111 * Reads the file or url $url and returns the content
1112 *
1113 * @param string $url Filepath/URL to read
1114 * @return string The content from the resource given as input.
1115 * @see \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl()
1116 */
1117 public function getUrl($url) {
1118 return GeneralUtility::getUrl($url);
1119 }
1120
1121 /**
1122 * Function for cleaning content going into the database.
1123 * Content is cleaned eg. by removing unallowed HTML and ds-HSC content
1124 * 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
1125 *
1126 * @param string $content Content to clean up
1127 * @param string $tagList Comma list of tags to specifically allow. Default comes from getKeepTags and is
1128 * @return string Clean content
1129 * @see getKeepTags()
1130 */
1131 public function HTMLcleaner_db($content, $tagList = '') {
1132 if (!$tagList) {
1133 $keepTags = $this->getKeepTags('db');
1134 } else {
1135 $keepTags = $this->getKeepTags('db', $tagList);
1136 }
1137 // Default: remove unknown tags.
1138 $kUknown = $this->procOptions['dontRemoveUnknownTags_db'] ? 1 : 0;
1139 // Default: re-convert literals to characters (that is &lt; to <)
1140 $hSC = $this->procOptions['dontUndoHSC_db'] ? 0 : -1;
1141 // Create additional configuration in order to honor the setting RTE.default.proc.HTMLparser_db.xhtml_cleaning=1
1142 $addConfig = array();
1143 if (is_array($this->procOptions['HTMLparser_db.']) && $this->procOptions['HTMLparser_db.']['xhtml_cleaning'] || is_array($this->procOptions['entryHTMLparser_db.']) && $this->procOptions['entryHTMLparser_db.']['xhtml_cleaning'] || is_array($this->procOptions['exitHTMLparser_db.']) && $this->procOptions['exitHTMLparser_db.']['xhtml_cleaning']) {
1144 $addConfig['xhtml'] = 1;
1145 }
1146 return $this->HTMLcleaner($content, $keepTags, $kUknown, $hSC, $addConfig);
1147 }
1148
1149 /**
1150 * Creates an array of configuration for the HTMLcleaner function based on whether content go TO or FROM the Rich Text Editor ($direction)
1151 * Unless "tagList" is given, the function will cache the configuration for next time processing goes on. (In this class that is the case only if we are processing a bulletlist)
1152 *
1153 * @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)
1154 * @param string $tagList Comma list of tags to keep (overriding default which is to keep all + take notice of internal configuration)
1155 * @return array Configuration array
1156 * @see HTMLcleaner_db()
1157 */
1158 public function getKeepTags($direction = 'rte', $tagList = '') {
1159 if (!is_array($this->getKeepTags_cache[$direction]) || $tagList) {
1160 // Setting up allowed tags:
1161 // If the $tagList input var is set, this will take precedence
1162 if ((string)$tagList !== '') {
1163 $keepTags = array_flip(GeneralUtility::trimExplode(',', $tagList, TRUE));
1164 } else {
1165 // Default is to get allowed/denied tags from internal array of processing options:
1166 // Construct default list of tags to keep:
1167 $typoScript_list = 'b,i,u,a,img,br,div,center,pre,font,hr,sub,sup,p,strong,em,li,ul,ol,blockquote,strike,span';
1168 $keepTags = array_flip(GeneralUtility::trimExplode(',', $typoScript_list . ',' . strtolower($this->procOptions['allowTags']), TRUE));
1169 // For tags to deny, remove them from $keepTags array:
1170 $denyTags = GeneralUtility::trimExplode(',', $this->procOptions['denyTags'], TRUE);
1171 foreach ($denyTags as $dKe) {
1172 unset($keepTags[$dKe]);
1173 }
1174 }
1175 // Based on the direction of content, set further options:
1176 switch ($direction) {
1177 case 'rte':
1178 if (!isset($this->procOptions['transformBoldAndItalicTags']) || $this->procOptions['transformBoldAndItalicTags']) {
1179 // Transform bold/italics tags to strong/em
1180 if (isset($keepTags['b'])) {
1181 $keepTags['b'] = array('remap' => 'STRONG');
1182 }
1183 if (isset($keepTags['i'])) {
1184 $keepTags['i'] = array('remap' => 'EM');
1185 }
1186 }
1187 // Transforming keepTags array so it can be understood by the HTMLcleaner function. This basically converts the format of the array from TypoScript (having .'s) to plain multi-dimensional array.
1188 list($keepTags) = $this->HTMLparserConfig($this->procOptions['HTMLparser_rte.'], $keepTags);
1189 break;
1190 case 'db':
1191 if (!isset($this->procOptions['transformBoldAndItalicTags']) || $this->procOptions['transformBoldAndItalicTags']) {
1192 // Transform strong/em back to bold/italics:
1193 if (isset($keepTags['strong'])) {
1194 $keepTags['strong'] = array('remap' => 'b');
1195 }
1196 if (isset($keepTags['em'])) {
1197 $keepTags['em'] = array('remap' => 'i');
1198 }
1199 }
1200 // Setting up span tags if they are allowed:
1201 if (isset($keepTags['span'])) {
1202 $classes = array_merge(array(''), $this->allowedClasses);
1203 $keepTags['span'] = array(
1204 'allowedAttribs' => 'id,class,style,title,lang,xml:lang,dir,itemscope,itemtype,itemprop',
1205 'fixAttrib' => array(
1206 'class' => array(
1207 'list' => $classes,
1208 'removeIfFalse' => 1
1209 )
1210 ),
1211 'rmTagIfNoAttrib' => 1
1212 );
1213 if (!$this->procOptions['allowedClasses']) {
1214 unset($keepTags['span']['fixAttrib']['class']['list']);
1215 }
1216 }
1217 // Setting up font tags if they are allowed:
1218 if (isset($keepTags['font'])) {
1219 $colors = array_merge(array(''), GeneralUtility::trimExplode(',', $this->procOptions['allowedFontColors'], TRUE));
1220 $keepTags['font'] = array(
1221 'allowedAttribs' => 'face,color,size',
1222 'fixAttrib' => array(
1223 'face' => array(
1224 'removeIfFalse' => 1
1225 ),
1226 'color' => array(
1227 'removeIfFalse' => 1,
1228 'list' => $colors
1229 ),
1230 'size' => array(
1231 'removeIfFalse' => 1
1232 )
1233 ),
1234 'rmTagIfNoAttrib' => 1
1235 );
1236 if (!$this->procOptions['allowedFontColors']) {
1237 unset($keepTags['font']['fixAttrib']['color']['list']);
1238 }
1239 }
1240 // Setting further options, getting them from the processiong options:
1241 $TSc = $this->procOptions['HTMLparser_db.'];
1242 if (!$TSc['globalNesting']) {
1243 $TSc['globalNesting'] = 'b,i,u,a,center,font,sub,sup,strong,em,strike,span';
1244 }
1245 if (!$TSc['noAttrib']) {
1246 $TSc['noAttrib'] = 'b,i,u,br,center,hr,sub,sup,strong,em,li,ul,ol,blockquote,strike';
1247 }
1248 // Transforming the array from TypoScript to regular array:
1249 list($keepTags) = $this->HTMLparserConfig($TSc, $keepTags);
1250 break;
1251 }
1252 // Caching (internally, in object memory) the result unless tagList is set:
1253 if (!$tagList) {
1254 $this->getKeepTags_cache[$direction] = $keepTags;
1255 } else {
1256 return $keepTags;
1257 }
1258 }
1259 // Return result:
1260 return $this->getKeepTags_cache[$direction];
1261 }
1262
1263 /**
1264 * This resolves the $value into parts based on <div></div>-sections and <p>-sections and <br />-tags. These are returned as lines separated by LF.
1265 * This point is to resolve the HTML-code returned from RTE into ordinary lines so it's 'human-readable'
1266 * The function ->setDivTags does the opposite.
1267 * This function processes content to go into the database.
1268 *
1269 * @param string $value Value to process.
1270 * @param int $count Recursion brake. Decremented on each recursion down to zero. Default is 5 (which equals the allowed nesting levels of p/div tags).
1271 * @param bool $returnArray If TRUE, an array with the lines is returned, otherwise a string of the processed input value.
1272 * @return string Processed input value.
1273 * @see setDivTags()
1274 */
1275 public function divideIntoLines($value, $count = 5, $returnArray = FALSE) {
1276 // Internalize font tags (move them from OUTSIDE p/div to inside it that is the case):
1277 if ($this->procOptions['internalizeFontTags']) {
1278 $value = $this->internalizeFontTags($value);
1279 }
1280 // Setting configuration for processing:
1281 $allowTagsOutside = GeneralUtility::trimExplode(',', strtolower($this->procOptions['allowTagsOutside'] ? 'hr,' . $this->procOptions['allowTagsOutside'] : 'hr,img'), TRUE);
1282 $remapParagraphTag = strtoupper($this->procOptions['remapParagraphTag']);
1283 $divSplit = $this->splitIntoBlock('div,p', $value, 1);
1284 // Setting the third param to 1 will eliminate false end-tags. Maybe this is a good thing to do...?
1285 if ($this->procOptions['keepPDIVattribs']) {
1286 $keepAttribListArr = GeneralUtility::trimExplode(',', strtolower($this->procOptions['keepPDIVattribs']), TRUE);
1287 } else {
1288 $keepAttribListArr = array();
1289 }
1290 // Returns plainly the value if there was no div/p sections in it
1291 if (count($divSplit) <= 1 || $count <= 0) {
1292 // Wrap hr tags with LF's
1293 $newValue = preg_replace('/<(hr)(\\s[^>\\/]*)?[[:space:]]*\\/?>/i', LF . '<$1$2/>' . LF, $value);
1294 $newValue = preg_replace('/' . LF . LF . '/i', LF, $newValue);
1295 $newValue = preg_replace('/(^' . LF . ')|(' . LF . '$)/i', '', $newValue);
1296 return $newValue;
1297 }
1298 // Traverse the splitted sections:
1299 foreach ($divSplit as $k => $v) {
1300 if ($k % 2) {
1301 // Inside
1302 $v = $this->removeFirstAndLastTag($v);
1303 // Fetching 'sub-lines' - which will explode any further p/div nesting...
1304 $subLines = $this->divideIntoLines($v, $count - 1, 1);
1305 // So, if there happend to be sub-nesting of p/div, this is written directly as the new content of THIS section. (This would be considered 'an error')
1306 if (is_array($subLines)) {
1307
1308 } else {
1309 //... but if NO subsection was found, we process it as a TRUE line without erronous content:
1310 $subLines = array($subLines);
1311 // process break-tags, if configured for. Simply, the breaktags will here be treated like if each was a line of content...
1312 if (!$this->procOptions['dontConvBRtoParagraph']) {
1313 $subLines = preg_split('/<br[[:space:]]*[\\/]?>/i', $v);
1314 }
1315 // Traverse sublines (there is typically one, except if <br/> has been converted to lines as well!)
1316 foreach ($subLines as $sk => $value) {
1317 // Clear up the subline for DB.
1318 $subLines[$sk] = $this->HTMLcleaner_db($subLines[$sk]);
1319 // Get first tag, attributes etc:
1320 $fTag = $this->getFirstTag($divSplit[$k]);
1321 $tagName = strtolower($this->getFirstTagName($divSplit[$k]));
1322 $attribs = $this->get_tag_attributes($fTag);
1323 // Keep attributes (lowercase)
1324 $newAttribs = array();
1325 if (!empty($keepAttribListArr)) {
1326 foreach ($keepAttribListArr as $keepA) {
1327 if (isset($attribs[0][$keepA])) {
1328 $newAttribs[$keepA] = $attribs[0][$keepA];
1329 }
1330 }
1331 }
1332 // ALIGN attribute:
1333 if (!$this->procOptions['skipAlign'] && trim($attribs[0]['align']) !== '' && strtolower($attribs[0]['align']) != 'left') {
1334 // Set to value, but not 'left'
1335 $newAttribs['align'] = strtolower($attribs[0]['align']);
1336 }
1337 // CLASS attribute:
1338 // Set to whatever value
1339 if (!$this->procOptions['skipClass'] && trim($attribs[0]['class']) !== '') {
1340 if (empty($this->allowedClasses) || in_array($attribs[0]['class'], $this->allowedClasses)) {
1341 $newAttribs['class'] = $attribs[0]['class'];
1342 } else {
1343 $classes = GeneralUtility::trimExplode(' ', $attribs[0]['class'], TRUE);
1344 $newClasses = array();
1345 foreach ($classes as $class) {
1346 if (in_array($class, $this->allowedClasses)) {
1347 $newClasses[] = $class;
1348 }
1349 }
1350 if (!empty($newClasses)) {
1351 $newAttribs['class'] = implode(' ', $newClasses);
1352 }
1353 }
1354 }
1355 // Remove any line break char (10 or 13)
1356 $subLines[$sk] = preg_replace('/' . LF . '|' . CR . '/', '', $subLines[$sk]);
1357 // If there are any attributes or if we are supposed to remap the tag, then do so:
1358 if (!empty($newAttribs) && $remapParagraphTag !== '1') {
1359 if ($remapParagraphTag === 'P') {
1360 $tagName = 'p';
1361 }
1362 if ($remapParagraphTag === 'DIV') {
1363 $tagName = 'div';
1364 }
1365 $subLines[$sk] = '<' . trim(($tagName . ' ' . $this->compileTagAttribs($newAttribs))) . '>' . $subLines[$sk] . '</' . $tagName . '>';
1366 }
1367 }
1368 }
1369 // Add the processed line(s)
1370 $divSplit[$k] = implode(LF, $subLines);
1371 // If it turns out the line is just blank (containing a &nbsp; possibly) then just make it pure blank.
1372 // But, prevent filtering of lines that are blank in sense above, but whose tags contain attributes.
1373 // Those attributes should have been filtered before; if they are still there they must be considered as possible content.
1374 if (trim(strip_tags($divSplit[$k])) == '&nbsp;' && !preg_match('/\\<(img)(\\s[^>]*)?\\/?>/si', $divSplit[$k]) && !preg_match('/\\<([^>]*)?( align| class| style| id| title| dir| lang| xml:lang)([^>]*)?>/si', trim($divSplit[$k]))) {
1375 $divSplit[$k] = '';
1376 }
1377 } else {
1378 // outside div:
1379 // Remove positions which are outside div/p tags and without content
1380 $divSplit[$k] = trim(strip_tags($divSplit[$k], '<' . implode('><', $allowTagsOutside) . '>'));
1381 // Wrap hr tags with LF's
1382 $divSplit[$k] = preg_replace('/<(hr)(\\s[^>\\/]*)?[[:space:]]*\\/?>/i', LF . '<$1$2/>' . LF, $divSplit[$k]);
1383 $divSplit[$k] = preg_replace('/' . LF . LF . '/i', LF, $divSplit[$k]);
1384 $divSplit[$k] = preg_replace('/(^' . LF . ')|(' . LF . '$)/i', '', $divSplit[$k]);
1385 if ((string)$divSplit[$k] === '') {
1386 unset($divSplit[$k]);
1387 }
1388 }
1389 }
1390 // Return value:
1391 return $returnArray ? $divSplit : implode(LF, $divSplit);
1392 }
1393
1394 /**
1395 * Converts all lines into <div></div>/<p></p>-sections (unless the line is a div-section already)
1396 * For processing of content going FROM database TO RTE.
1397 *
1398 * @param string $value Value to convert
1399 * @param string $dT Tag to wrap with. Either "p" or "div" should it be. Lowercase preferably.
1400 * @return string Processed value.
1401 * @see divideIntoLines()
1402 */
1403 public function setDivTags($value, $dT = 'p') {
1404 // First, setting configuration for the HTMLcleaner function. This will process each line between the <div>/<p> section on their way to the RTE
1405 $keepTags = $this->getKeepTags('rte');
1406 // Default: remove unknown tags.
1407 $kUknown = $this->procOptions['dontProtectUnknownTags_rte'] ? 0 : 'protect';
1408 // Default: re-convert literals to characters (that is &lt; to <)
1409 $hSC = $this->procOptions['dontHSC_rte'] ? 0 : 1;
1410 $convNBSP = !$this->procOptions['dontConvAmpInNBSP_rte'] ? 1 : 0;
1411 // Divide the content into lines, based on LF:
1412 $parts = explode(LF, $value);
1413 foreach ($parts as $k => $v) {
1414 // Processing of line content:
1415 // If the line is blank, set it to &nbsp;
1416 if (trim($parts[$k]) === '') {
1417 $parts[$k] = '&nbsp;';
1418 } else {
1419 // Clean the line content:
1420 $parts[$k] = $this->HTMLcleaner($parts[$k], $keepTags, $kUknown, $hSC);
1421 if ($convNBSP) {
1422 $parts[$k] = str_replace('&amp;nbsp;', '&nbsp;', $parts[$k]);
1423 }
1424 }
1425 // Wrapping the line in <$dT> if not already wrapped and does not contain an hr tag
1426 if (!preg_match('/<(hr)(\\s[^>\\/]*)?[[:space:]]*\\/?>/i', $parts[$k])) {
1427 $testStr = strtolower(trim($parts[$k]));
1428 if (substr($testStr, 0, 4) != '<div' || substr($testStr, -6) != '</div>') {
1429 if (substr($testStr, 0, 2) != '<p' || substr($testStr, -4) != '</p>') {
1430 // Only set p-tags if there is not already div or p tags:
1431 $parts[$k] = '<' . $dT . '>' . $parts[$k] . '</' . $dT . '>';
1432 }
1433 }
1434 }
1435 }
1436 // Implode result:
1437 return implode(LF, $parts);
1438 }
1439
1440 /**
1441 * This splits the $value in font-tag chunks.
1442 * If there are any <P>/<DIV> sections inside of them, the font-tag is wrapped AROUND the content INSIDE of the P/DIV sections and the outer font-tag is removed.
1443 * This functions seems to be a good choice for pre-processing content if it has been pasted into the RTE from eg. star-office.
1444 * In that case the font-tags are normally on the OUTSIDE of the sections.
1445 * This function is used by eg. divideIntoLines() if the procesing option 'internalizeFontTags' is set.
1446 *
1447 * @param string Input content
1448 * @return string Output content
1449 * @see divideIntoLines()
1450 */
1451 public function internalizeFontTags($value) {
1452 // Splitting into font tag blocks:
1453 $fontSplit = $this->splitIntoBlock('font', $value);
1454 foreach ($fontSplit as $k => $v) {
1455 // Inside
1456 if ($k % 2) {
1457 // Fint font-tag
1458 $fTag = $this->getFirstTag($v);
1459 $divSplit_sub = $this->splitIntoBlock('div,p', $this->removeFirstAndLastTag($v), 1);
1460 // If there were div/p sections inside the font-tag, do something about it...
1461 if (count($divSplit_sub) > 1) {
1462 // Traverse those sections:
1463 foreach ($divSplit_sub as $k2 => $v2) {
1464 // Inside
1465 if ($k2 % 2) {
1466 // Fint font-tag
1467 $div_p = $this->getFirstTag($v2);
1468 // Fint font-tag
1469 $div_p_tagname = $this->getFirstTagName($v2);
1470 // ... and remove it from original.
1471 $v2 = $this->removeFirstAndLastTag($v2);
1472 $divSplit_sub[$k2] = $div_p . $fTag . $v2 . '</font>' . '</' . $div_p_tagname . '>';
1473 } elseif (trim(strip_tags($v2))) {
1474 $divSplit_sub[$k2] = $fTag . $v2 . '</font>';
1475 }
1476 }
1477 $fontSplit[$k] = implode('', $divSplit_sub);
1478 }
1479 }
1480 }
1481 return implode('', $fontSplit);
1482 }
1483
1484 /**
1485 * Returns SiteURL based on thisScript.
1486 *
1487 * @return string Value of GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
1488 * @see \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv()
1489 */
1490 public function siteUrl() {
1491 return GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
1492 }
1493
1494 /**
1495 * Remove all tables from incoming code
1496 * The function is trying to to this is some more or less respectfull way. The approach is to resolve each table cells content and implode it all by <br /> chars. Thus at least the content is preserved in some way.
1497 *
1498 * @param string $value Input value
1499 * @param string $breakChar Break character to use for linebreaks.
1500 * @return string Output value
1501 */
1502 public function removeTables($value, $breakChar = '<br />') {
1503 // Splitting value into table blocks:
1504 $tableSplit = $this->splitIntoBlock('table', $value);
1505 // Traverse blocks of tables:
1506 foreach ($tableSplit as $k => $v) {
1507 if ($k % 2) {
1508 $tableSplit[$k] = '';
1509 $rowSplit = $this->splitIntoBlock('tr', $v);
1510 foreach ($rowSplit as $k2 => $v2) {
1511 if ($k2 % 2) {
1512 $cellSplit = $this->getAllParts($this->splitIntoBlock('td', $v2), 1, 0);
1513 foreach ($cellSplit as $k3 => $v3) {
1514 $tableSplit[$k] .= $v3 . $breakChar;
1515 }
1516 }
1517 }
1518 }
1519 }
1520 // Implode it all again:
1521 return implode($breakChar, $tableSplit);
1522 }
1523
1524 /**
1525 * Default tag mapping for TS
1526 *
1527 * @param string $code Input code to process
1528 * @param string $direction Direction To databsae (db) or from database to RTE (rte)
1529 * @return string Processed value
1530 */
1531 public function defaultTStagMapping($code, $direction = 'rte') {
1532 if ($direction == 'db') {
1533 $code = $this->mapTags($code, array(
1534 // Map tags
1535 'strong' => 'b',
1536 'em' => 'i'
1537 ));
1538 }
1539 if ($direction == 'rte') {
1540 $code = $this->mapTags($code, array(
1541 // Map tags
1542 'b' => 'strong',
1543 'i' => 'em'
1544 ));
1545 }
1546 return $code;
1547 }
1548
1549 /**
1550 * Finds width and height from attrib-array
1551 * If the width and height is found in the style-attribute, use that!
1552 *
1553 * @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
1554 * @return array Integer w/h in key 0/1. Zero is returned if not found.
1555 */
1556 public function getWHFromAttribs($attribArray) {
1557 $style = trim($attribArray['style']);
1558 if ($style) {
1559 $regex = '[[:space:]]*:[[:space:]]*([0-9]*)[[:space:]]*px';
1560 // Width
1561 $reg = array();
1562 preg_match('/width' . $regex . '/i', $style, $reg);
1563 $w = (int)$reg[1];
1564 // Height
1565 preg_match('/height' . $regex . '/i', $style, $reg);
1566 $h = (int)$reg[1];
1567 }
1568 if (!$w) {
1569 $w = $attribArray['width'];
1570 }
1571 if (!$h) {
1572 $h = $attribArray['height'];
1573 }
1574 return array((int)$w, (int)$h);
1575 }
1576
1577 /**
1578 * Parse <A>-tag href and return status of email,external,file or page
1579 *
1580 * @param string $url URL to analyse.
1581 * @return array Information in an array about the URL
1582 */
1583 public function urlInfoForLinkTags($url) {
1584 $info = array();
1585 $url = trim($url);
1586 if (substr(strtolower($url), 0, 7) == 'mailto:') {
1587 $info['url'] = trim(substr($url, 7));
1588 $info['type'] = 'email';
1589 } elseif (strpos($url, '?file:') !== FALSE) {
1590 $info['type'] = 'file';
1591 $info['url'] = rawurldecode(substr($url, strpos($url, '?file:') + 1));
1592 } else {
1593 $curURL = $this->siteUrl();
1594 $urlLength = strlen($url);
1595 for ($a = 0; $a < $urlLength; $a++) {
1596 if ($url[$a] != $curURL[$a]) {
1597 break;
1598 }
1599 }
1600 $info['relScriptPath'] = substr($curURL, $a);
1601 $info['relUrl'] = substr($url, $a);
1602 $info['url'] = $url;
1603 $info['type'] = 'ext';
1604 $siteUrl_parts = parse_url($url);
1605 $curUrl_parts = parse_url($curURL);
1606 // Hosts should match
1607 if ($siteUrl_parts['host'] == $curUrl_parts['host'] && (!$info['relScriptPath'] || defined('TYPO3_mainDir') && substr($info['relScriptPath'], 0, strlen(TYPO3_mainDir)) == TYPO3_mainDir)) {
1608 // If the script path seems to match or is empty (FE-EDIT)
1609 // New processing order 100502
1610 $uP = parse_url($info['relUrl']);
1611 if ($info['relUrl'] === '#' . $siteUrl_parts['fragment']) {
1612 $info['url'] = $info['relUrl'];
1613 $info['type'] = 'anchor';
1614 } elseif (!trim($uP['path']) || $uP['path'] === 'index.php') {
1615 // URL is a page (id parameter)
1616 $pp = preg_split('/^id=/', $uP['query']);
1617 $pp[1] = preg_replace('/&id=[^&]*/', '', $pp[1]);
1618 $parameters = explode('&', $pp[1]);
1619 $id = array_shift($parameters);
1620 if ($id) {
1621 $info['pageid'] = $id;
1622 $info['cElement'] = $uP['fragment'];
1623 $info['url'] = $id . ($info['cElement'] ? '#' . $info['cElement'] : '');
1624 $info['type'] = 'page';
1625 $info['query'] = $parameters[0] ? '&' . implode('&', $parameters) : '';
1626 }
1627 } else {
1628 $info['url'] = $info['relUrl'];
1629 $info['type'] = 'file';
1630 }
1631 } else {
1632 unset($info['relScriptPath']);
1633 unset($info['relUrl']);
1634 }
1635 }
1636 return $info;
1637 }
1638
1639 /**
1640 * Converting <A>-tags to absolute URLs (+ setting rtekeep attribute)
1641 *
1642 * @param string $value Content input
1643 * @param bool $dontSetRTEKEEP If TRUE, then the "rtekeep" attribute will not be set.
1644 * @return string Content output
1645 */
1646 public function TS_AtagToAbs($value, $dontSetRTEKEEP = FALSE) {
1647 $blockSplit = $this->splitIntoBlock('A', $value);
1648 foreach ($blockSplit as $k => $v) {
1649 // Block
1650 if ($k % 2) {
1651 $attribArray = $this->get_tag_attributes_classic($this->getFirstTag($v), 1);
1652 // Checking if there is a scheme, and if not, prepend the current url.
1653 // ONLY do this if href has content - the <a> tag COULD be an anchor and if so, it should be preserved...
1654 if ($attribArray['href'] !== '') {
1655 $uP = parse_url(strtolower($attribArray['href']));
1656 if (!$uP['scheme']) {
1657 $attribArray['href'] = $this->siteUrl() . substr($attribArray['href'], strlen($this->relBackPath));
1658 } elseif ($uP['scheme'] != 'mailto') {
1659 $attribArray['data-htmlarea-external'] = 1;
1660 }
1661 } else {
1662 $attribArray['rtekeep'] = 1;
1663 }
1664 if (!$dontSetRTEKEEP) {
1665 $attribArray['rtekeep'] = 1;
1666 }
1667 $bTag = '<a ' . GeneralUtility::implodeAttributes($attribArray, 1) . '>';
1668 $eTag = '</a>';
1669 $blockSplit[$k] = $bTag . $this->TS_AtagToAbs($this->removeFirstAndLastTag($blockSplit[$k])) . $eTag;
1670 }
1671 }
1672 return implode('', $blockSplit);
1673 }
1674
1675 /**
1676 * Apply plain image settings to the dimensions of the image
1677 *
1678 * @param array $imageInfo: info array of the image
1679 * @param array $attribArray: array of attributes of an image tag
1680 *
1681 * @return array a modified attributes array
1682 */
1683 protected function applyPlainImageModeSettings($imageInfo, $attribArray) {
1684 if ($this->procOptions['plainImageMode']) {
1685 // Perform corrections to aspect ratio based on configuration
1686 switch ((string)$this->procOptions['plainImageMode']) {
1687 case 'lockDimensions':
1688 $attribArray['width'] = $imageInfo[0];
1689 $attribArray['height'] = $imageInfo[1];
1690 break;
1691 case 'lockRatioWhenSmaller':
1692 if ($attribArray['width'] > $imageInfo[0]) {
1693 $attribArray['width'] = $imageInfo[0];
1694 }
1695 case 'lockRatio':
1696 if ($imageInfo[0] > 0) {
1697 $attribArray['height'] = round($attribArray['width'] * ($imageInfo[1] / $imageInfo[0]));
1698 }
1699 break;
1700 }
1701 }
1702 return $attribArray;
1703 }
1704
1705 /**
1706 * @return \TYPO3\CMS\Core\Log\Logger
1707 */
1708 protected function getLogger() {
1709 /** @var $logManager \TYPO3\CMS\Core\Log\LogManager */
1710 $logManager = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Log\LogManager::class);
1711
1712 return $logManager->getLogger(get_class($this));
1713 }
1714
1715 }