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