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