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