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