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