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