PageRenderTime 30ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/t3lib/class.t3lib_parsehtml_proc.php

https://github.com/andreaswolf/typo3-tceforms
PHP | 1644 lines | 1024 code | 156 blank | 464 comment | 261 complexity | df93bdac61f57996645fb3a3d9178acb MD5 | raw file
Possible License(s): Apache-2.0, BSD-2-Clause, LGPL-3.0

Large files files are truncated, but you can click here to view the full file

  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. *
  30. * $Id$
  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/FUNCTION INDEX of SCRIPT]
  39. *
  40. *
  41. *
  42. * 103: class t3lib_parsehtml_proc extends t3lib_parsehtml
  43. * 138: function init($elRef='',$recPid=0)
  44. * 150: function setRelPath($path)
  45. * 174: function evalWriteFile($pArr,$currentRecord)
  46. *
  47. * SECTION: Main function
  48. * 232: function RTE_transform($value,$specConf,$direction='rte',$thisConfig=array())
  49. *
  50. * SECTION: Specific RTE TRANSFORMATION functions
  51. * 398: function TS_images_db($value)
  52. * 550: function TS_images_rte($value)
  53. * 589: function TS_reglinks($value,$direction)
  54. * 626: function TS_links_db($value)
  55. * 675: function TS_links_rte($value)
  56. * 760: function TS_preserve_db($value)
  57. * 784: function TS_preserve_rte($value)
  58. * 805: function TS_transform_db($value,$css=FALSE)
  59. * 922: function transformStyledATags($value)
  60. * 948: function TS_transform_rte($value,$css=0)
  61. * 1019: function TS_strip_db($value)
  62. *
  63. * SECTION: Generic RTE transformation, analysis and helper functions
  64. * 1050: function getURL($url)
  65. * 1064: function HTMLcleaner_db($content,$tagList='')
  66. * 1091: function getKeepTags($direction='rte',$tagList='')
  67. * 1200: function divideIntoLines($value,$count=5,$returnArray=FALSE)
  68. * 1304: function setDivTags($value,$dT='p')
  69. * 1349: function internalizeFontTags($value)
  70. * 1385: function siteUrl()
  71. * 1395: function rteImageStorageDir()
  72. * 1407: function removeTables($value,$breakChar='<br />')
  73. * 1439: function defaultTStagMapping($code,$direction='rte')
  74. * 1462: function getWHFromAttribs($attribArray)
  75. * 1489: function urlInfoForLinkTags($url)
  76. * 1548: function TS_AtagToAbs($value,$dontSetRTEKEEP=FALSE)
  77. *
  78. * TOTAL FUNCTIONS: 28
  79. * (This index is automatically created/updated by the extension "extdeveval")
  80. *
  81. */
  82. /**
  83. * Class for parsing HTML for the Rich Text Editor. (also called transformations)
  84. *
  85. * @author Kasper Skårhøj <kasperYYYY@typo3.com>
  86. * @package TYPO3
  87. * @subpackage t3lib
  88. */
  89. class t3lib_parsehtml_proc extends t3lib_parsehtml {
  90. // Static:
  91. var $blockElementList = 'PRE,UL,OL,H1,H2,H3,H4,H5,H6,ADDRESS,DL,DD'; // List of tags for these elements
  92. // Internal, static:
  93. var $recPid = 0; // Set this to the pid of the record manipulated by the class.
  94. var $elRef = ''; // Element reference [table]:[field], eg. "tt_content:bodytext"
  95. var $relPath = ''; // Relative path
  96. var $relBackPath = ''; // Relative back-path
  97. public $tsConfig = array(); // Current Page TSConfig
  98. var $procOptions = ''; // Set to the TSconfig options coming from Page TSconfig
  99. // Internal, dynamic
  100. var $TS_transform_db_safecounter = 100; // Run-away brake for recursive calls.
  101. var $rte_p = ''; // Parameters from TCA types configuration related to the RTE
  102. var $getKeepTags_cache = array(); // Data caching for processing function
  103. var $allowedClasses = array(); // Storage of the allowed CSS class names in the RTE
  104. var $preserveTags = ''; // Set to tags to preserve from Page TSconfig configuration
  105. /**
  106. * Initialize, setting element reference and record PID
  107. *
  108. * @param string Element reference, eg "tt_content:bodytext"
  109. * @param integer PID of the record (page id)
  110. * @return void
  111. */
  112. function init($elRef = '', $recPid = 0) {
  113. $this->recPid = $recPid;
  114. $this->elRef = $elRef;
  115. }
  116. /**
  117. * Setting the ->relPath and ->relBackPath to proper values so absolute references to links and images can be converted to relative dittos.
  118. * This is used when editing files with the RTE
  119. *
  120. * @param string The relative path from PATH_site to the place where the file being edited is. Eg. "fileadmin/static".
  121. * @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 "../../"
  122. */
  123. function setRelPath($path) {
  124. $path = trim($path);
  125. $path = preg_replace('/^\//', '', $path);
  126. $path = preg_replace('/\/$/', '', $path);
  127. if ($path) {
  128. $this->relPath = $path;
  129. $this->relBackPath = '';
  130. $partsC = count(explode('/', $this->relPath));
  131. for ($a = 0; $a < $partsC; $a++) {
  132. $this->relBackPath .= '../';
  133. }
  134. $this->relPath .= '/';
  135. }
  136. }
  137. /**
  138. * Evaluate the environment for editing a staticFileEdit file.
  139. * Called for almost all fields being saved in the database. Is called without an instance of the object: t3lib_parsehtml_proc::evalWriteFile()
  140. *
  141. * @param array Parameters for the current field as found in types-config
  142. * @param array Current record we are editing.
  143. * @return mixed On success an array with various information is returned, otherwise a string with an error message
  144. * @see t3lib_TCEmain, t3lib_transferData
  145. */
  146. function evalWriteFile($pArr, $currentRecord) {
  147. // Write file configuration:
  148. if (is_array($pArr)) {
  149. if ($GLOBALS['TYPO3_CONF_VARS']['BE']['staticFileEditPath']
  150. && substr($GLOBALS['TYPO3_CONF_VARS']['BE']['staticFileEditPath'], -1) == '/'
  151. && @is_dir(PATH_site . $GLOBALS['TYPO3_CONF_VARS']['BE']['staticFileEditPath'])) {
  152. $SW_p = $pArr['parameters'];
  153. $SW_editFileField = trim($SW_p[0]);
  154. $SW_editFile = $currentRecord[$SW_editFileField];
  155. if ($SW_editFileField && $SW_editFile && t3lib_div::validPathStr($SW_editFile)) {
  156. $SW_relpath = $GLOBALS['TYPO3_CONF_VARS']['BE']['staticFileEditPath'] . $SW_editFile;
  157. $SW_editFile = PATH_site . $SW_relpath;
  158. if (@is_file($SW_editFile)) {
  159. return array(
  160. 'editFile' => $SW_editFile,
  161. 'relEditFile' => $SW_relpath,
  162. 'contentField' => trim($SW_p[1]),
  163. 'markerField' => trim($SW_p[2]),
  164. 'loadFromFileField' => trim($SW_p[3]),
  165. 'statusField' => trim($SW_p[4])
  166. );
  167. } else {
  168. return "ERROR: Editfile '" . $SW_relpath . "' did not exist";
  169. }
  170. } else {
  171. return "ERROR: Edit file name could not be found or was bad.";
  172. }
  173. } else {
  174. return "ERROR: staticFileEditPath was not set, not set correctly or did not exist!";
  175. }
  176. }
  177. }
  178. /**********************************************
  179. *
  180. * Main function
  181. *
  182. **********************************************/
  183. /**
  184. * Transform value for RTE based on specConf in the direction specified by $direction (rte/db)
  185. * This is the main function called from tcemain and transfer data classes
  186. *
  187. * @param string Input value
  188. * @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!
  189. * @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.
  190. * @param array Parsed TypoScript content configuring the RTE, probably coming from Page TSconfig.
  191. * @return string Output value
  192. * @see t3lib_TCEmain::fillInFieldArray(), t3lib_transferData::renderRecord_typesProc()
  193. */
  194. function RTE_transform($value, $specConf, $direction = 'rte', $thisConfig = array()) {
  195. // Init:
  196. $this->tsConfig = $thisConfig;
  197. $this->procOptions = $thisConfig['proc.'];
  198. $this->preserveTags = strtoupper(implode(',', t3lib_div::trimExplode(',', $this->procOptions['preserveTags'])));
  199. // dynamic configuration of blockElementList
  200. if ($this->procOptions['blockElementList']) {
  201. $this->blockElementList = $this->procOptions['blockElementList'];
  202. }
  203. // Get parameters for rte_transformation:
  204. $p = $this->rte_p = t3lib_BEfunc::getSpecConfParametersFromArray($specConf['rte_transform']['parameters']);
  205. // Setting modes:
  206. if (strcmp($this->procOptions['overruleMode'], '')) {
  207. $modes = array_unique(t3lib_div::trimExplode(',', $this->procOptions['overruleMode']));
  208. } else {
  209. $modes = array_unique(t3lib_div::trimExplode('-', $p['mode']));
  210. }
  211. $revmodes = array_flip($modes);
  212. // Find special modes and extract them:
  213. if (isset($revmodes['ts'])) {
  214. $modes[$revmodes['ts']] = 'ts_transform,ts_preserve,ts_images,ts_links';
  215. }
  216. // Find special modes and extract them:
  217. if (isset($revmodes['ts_css'])) {
  218. $modes[$revmodes['ts_css']] = 'css_transform,ts_images,ts_links';
  219. }
  220. // Make list unique
  221. $modes = array_unique(t3lib_div::trimExplode(',', implode(',', $modes), 1));
  222. // Reverse order if direction is "rte"
  223. if ($direction == 'rte') {
  224. $modes = array_reverse($modes);
  225. }
  226. // 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:
  227. $entry_HTMLparser = $this->procOptions['entryHTMLparser_' . $direction] ? $this->HTMLparserConfig($this->procOptions['entryHTMLparser_' . $direction . '.']) : '';
  228. $exit_HTMLparser = $this->procOptions['exitHTMLparser_' . $direction] ? $this->HTMLparserConfig($this->procOptions['exitHTMLparser_' . $direction . '.']) : '';
  229. // Line breaks of content is unified into char-10 only (removing char 13)
  230. if (!$this->procOptions['disableUnifyLineBreaks']) {
  231. $value = str_replace(CRLF, LF, $value);
  232. }
  233. // In an entry-cleaner was configured, pass value through the HTMLcleaner with that:
  234. if (is_array($entry_HTMLparser)) {
  235. $value = $this->HTMLcleaner($value, $entry_HTMLparser[0], $entry_HTMLparser[1], $entry_HTMLparser[2], $entry_HTMLparser[3]);
  236. }
  237. // Traverse modes:
  238. foreach ($modes as $cmd) {
  239. // ->DB
  240. if ($direction == 'db') {
  241. // Checking for user defined transformation:
  242. if ($_classRef = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_parsehtml_proc.php']['transformation'][$cmd]) {
  243. $_procObj = t3lib_div::getUserObj($_classRef);
  244. $_procObj->pObj = $this;
  245. $_procObj->transformationKey = $cmd;
  246. $value = $_procObj->transform_db($value, $this);
  247. } else { // ... else use defaults:
  248. switch ($cmd) {
  249. case 'ts_images':
  250. $value = $this->TS_images_db($value);
  251. break;
  252. case 'ts_reglinks':
  253. $value = $this->TS_reglinks($value, 'db');
  254. break;
  255. case 'ts_links':
  256. $value = $this->TS_links_db($value);
  257. break;
  258. case 'ts_preserve':
  259. $value = $this->TS_preserve_db($value);
  260. break;
  261. case 'ts_transform':
  262. case 'css_transform':
  263. $value = str_replace(CR, '', $value); // Has a very disturbing effect, so just remove all '13' - depend on '10'
  264. $this->allowedClasses = t3lib_div::trimExplode(',', $this->procOptions['allowedClasses'], 1);
  265. $value = $this->TS_transform_db($value, $cmd == 'css_transform');
  266. break;
  267. case 'ts_strip':
  268. $value = $this->TS_strip_db($value);
  269. break;
  270. default:
  271. break;
  272. }
  273. }
  274. }
  275. // ->RTE
  276. if ($direction == 'rte') {
  277. // Checking for user defined transformation:
  278. if ($_classRef = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_parsehtml_proc.php']['transformation'][$cmd]) {
  279. $_procObj = t3lib_div::getUserObj($_classRef);
  280. $_procObj->pObj = $this;
  281. $value = $_procObj->transform_rte($value, $this);
  282. } else { // ... else use defaults:
  283. switch ($cmd) {
  284. case 'ts_images':
  285. $value = $this->TS_images_rte($value);
  286. break;
  287. case 'ts_reglinks':
  288. $value = $this->TS_reglinks($value, 'rte');
  289. break;
  290. case 'ts_links':
  291. $value = $this->TS_links_rte($value);
  292. break;
  293. case 'ts_preserve':
  294. $value = $this->TS_preserve_rte($value);
  295. break;
  296. case 'ts_transform':
  297. case 'css_transform':
  298. $value = str_replace(CR, '', $value); // Has a very disturbing effect, so just remove all '13' - depend on '10'
  299. $value = $this->TS_transform_rte($value, $cmd == 'css_transform');
  300. break;
  301. default:
  302. break;
  303. }
  304. }
  305. }
  306. }
  307. // In an exit-cleaner was configured, pass value through the HTMLcleaner with that:
  308. if (is_array($exit_HTMLparser)) {
  309. $value = $this->HTMLcleaner($value, $exit_HTMLparser[0], $exit_HTMLparser[1], $exit_HTMLparser[2], $exit_HTMLparser[3]);
  310. }
  311. // Final clean up of linebreaks:
  312. if (!$this->procOptions['disableUnifyLineBreaks']) {
  313. $value = str_replace(CRLF, LF, $value); // Make sure no \r\n sequences has entered in the meantime...
  314. $value = str_replace(LF, CRLF, $value); // ... and then change all \n into \r\n
  315. }
  316. // Return value:
  317. return $value;
  318. }
  319. /************************************
  320. *
  321. * Specific RTE TRANSFORMATION functions
  322. *
  323. *************************************/
  324. /**
  325. * Transformation handler: 'ts_images' / direction: "db"
  326. * Processing images inserted in the RTE.
  327. * This is used when content goes from the RTE to the database.
  328. * Images inserted in the RTE has an absolute URL applied to the src attribute. This URL is converted to a relative URL
  329. * 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.
  330. * Also "magic" images are processed here.
  331. *
  332. * @param string The content from RTE going to Database
  333. * @return string Processed content
  334. */
  335. function TS_images_db($value) {
  336. // Split content by <img> tags and traverse the resulting array for processing:
  337. $imgSplit = $this->splitTags('img', $value);
  338. foreach ($imgSplit as $k => $v) {
  339. if ($k % 2) { // image found, do processing:
  340. // Init
  341. $attribArray = $this->get_tag_attributes_classic($v, 1);
  342. $siteUrl = $this->siteUrl();
  343. $sitePath = str_replace(t3lib_div::getIndpEnv('TYPO3_REQUEST_HOST'), '', $siteUrl);
  344. $absRef = trim($attribArray['src']); // It's always a absolute URL coming from the RTE into the Database.
  345. // make path absolute if it is relative and we have a site path wich is not '/'
  346. $pI = pathinfo($absRef);
  347. if ($sitePath AND !$pI['scheme'] && t3lib_div::isFirstPartOfStr($absRef, $sitePath)) {
  348. // if site is in a subpath (eg. /~user_jim/) this path needs to be removed because it will be added with $siteUrl
  349. $absRef = substr($absRef, strlen($sitePath));
  350. $absRef = $siteUrl . $absRef;
  351. }
  352. // External image from another URL? In that case, fetch image (unless disabled feature).
  353. if (!t3lib_div::isFirstPartOfStr($absRef, $siteUrl) && !$this->procOptions['dontFetchExtPictures']) {
  354. $externalFile = $this->getUrl($absRef); // Get it
  355. if ($externalFile) {
  356. $pU = parse_url($absRef);
  357. $pI = pathinfo($pU['path']);
  358. if (t3lib_div::inList('gif,png,jpeg,jpg', strtolower($pI['extension']))) {
  359. $filename = t3lib_div::shortMD5($absRef) . '.' . $pI['extension'];
  360. $origFilePath = PATH_site . $this->rteImageStorageDir() . 'RTEmagicP_' . $filename;
  361. $C_origFilePath = PATH_site . $this->rteImageStorageDir() . 'RTEmagicC_' . $filename . '.' . $pI['extension'];
  362. if (!@is_file($origFilePath)) {
  363. t3lib_div::writeFile($origFilePath, $externalFile);
  364. t3lib_div::writeFile($C_origFilePath, $externalFile);
  365. }
  366. $absRef = $siteUrl . $this->rteImageStorageDir() . 'RTEmagicC_' . $filename . '.' . $pI['extension'];
  367. $attribArray['src'] = $absRef;
  368. $params = t3lib_div::implodeAttributes($attribArray, 1);
  369. $imgSplit[$k] = '<img ' . $params . ' />';
  370. }
  371. }
  372. }
  373. // Check image as local file (siteURL equals the one of the image)
  374. if (t3lib_div::isFirstPartOfStr($absRef, $siteUrl)) {
  375. $path = rawurldecode(substr($absRef, strlen($siteUrl))); // Rel-path, rawurldecoded for special characters.
  376. $filepath = t3lib_div::getFileAbsFileName($path); // Abs filepath, locked to relative path of this project.
  377. // Check file existence (in relative dir to this installation!)
  378. if ($filepath && @is_file($filepath)) {
  379. // If "magic image":
  380. $pathPre = $this->rteImageStorageDir() . 'RTEmagicC_';
  381. if (t3lib_div::isFirstPartOfStr($path, $pathPre)) {
  382. // Find original file:
  383. $pI = pathinfo(substr($path, strlen($pathPre)));
  384. $filename = substr($pI['basename'], 0, -strlen('.' . $pI['extension']));
  385. $origFilePath = PATH_site . $this->rteImageStorageDir() . 'RTEmagicP_' . $filename;
  386. if (@is_file($origFilePath)) {
  387. $imgObj = t3lib_div::makeInstance('t3lib_stdGraphic');
  388. $imgObj->init();
  389. $imgObj->mayScaleUp = 0;
  390. $imgObj->tempPath = PATH_site . $imgObj->tempPath;
  391. $curInfo = $imgObj->getImageDimensions($filepath); // Image dimensions of the current image
  392. $curWH = $this->getWHFromAttribs($attribArray); // Image dimensions as set in the image tag
  393. // Compare dimensions:
  394. if ($curWH[0] != $curInfo[0] || $curWH[1] != $curInfo[1]) {
  395. $origImgInfo = $imgObj->getImageDimensions($origFilePath); // Image dimensions of the current image
  396. $cW = $curWH[0];
  397. $cH = $curWH[1];
  398. $cH = 1000; // Make the image based on the width solely...
  399. $imgI = $imgObj->imageMagickConvert($origFilePath, $pI['extension'], $cW . 'm', $cH . 'm');
  400. if ($imgI[3]) {
  401. $fI = pathinfo($imgI[3]);
  402. @copy($imgI[3], $filepath); // Override the child file
  403. // Removing width and heigth form style attribute
  404. $attribArray['style'] = preg_replace('/((?:^|)\s*(?:width|height)\s*:[^;]*(?:$|;))/si', '', $attribArray['style']);
  405. $attribArray['width'] = $imgI[0];
  406. $attribArray['height'] = $imgI[1];
  407. $params = t3lib_div::implodeAttributes($attribArray, 1);
  408. $imgSplit[$k] = '<img ' . $params . ' />';
  409. }
  410. }
  411. }
  412. } elseif ($this->procOptions['plainImageMode']) { // If "plain image" has been configured:
  413. // Image dimensions as set in the image tag, if any
  414. $curWH = $this->getWHFromAttribs($attribArray);
  415. if ($curWH[0]) {
  416. $attribArray['width'] = $curWH[0];
  417. }
  418. if ($curWH[1]) {
  419. $attribArray['height'] = $curWH[1];
  420. }
  421. // Removing width and heigth form style attribute
  422. $attribArray['style'] = preg_replace('/((?:^|)\s*(?:width|height)\s*:[^;]*(?:$|;))/si', '', $attribArray['style']);
  423. // Finding dimensions of image file:
  424. $fI = @getimagesize($filepath);
  425. // Perform corrections to aspect ratio based on configuration:
  426. switch ((string) $this->procOptions['plainImageMode']) {
  427. case 'lockDimensions':
  428. $attribArray['width'] = $fI[0];
  429. $attribArray['height'] = $fI[1];
  430. break;
  431. case 'lockRatioWhenSmaller': // If the ratio has to be smaller, then first set the width...:
  432. if ($attribArray['width'] > $fI[0]) {
  433. $attribArray['width'] = $fI[0];
  434. }
  435. case 'lockRatio':
  436. if ($fI[0] > 0) {
  437. $attribArray['height'] = round($attribArray['width'] * ($fI[1] / $fI[0]));
  438. }
  439. break;
  440. }
  441. // Compile the image tag again:
  442. $params = t3lib_div::implodeAttributes($attribArray, 1);
  443. $imgSplit[$k] = '<img ' . $params . ' />';
  444. }
  445. } else { // Remove image if it was not found in a proper position on the server!
  446. // Commented out; removing the image tag might not be that logical...
  447. // $imgSplit[$k]='';
  448. }
  449. }
  450. // Convert abs to rel url
  451. if ($imgSplit[$k]) {
  452. $attribArray = $this->get_tag_attributes_classic($imgSplit[$k], 1);
  453. $absRef = trim($attribArray['src']);
  454. if (t3lib_div::isFirstPartOfStr($absRef, $siteUrl)) {
  455. $attribArray['src'] = $this->relBackPath . substr($absRef, strlen($siteUrl));
  456. if (!isset($attribArray['alt'])) {
  457. $attribArray['alt'] = '';
  458. } // Must have alt-attribute for XHTML compliance.
  459. $imgSplit[$k] = '<img ' . t3lib_div::implodeAttributes($attribArray, 1, 1) . ' />';
  460. }
  461. }
  462. }
  463. }
  464. return implode('', $imgSplit);
  465. }
  466. /**
  467. * Transformation handler: 'ts_images' / direction: "rte"
  468. * Processing images from database content going into the RTE.
  469. * Processing includes converting the src attribute to an absolute URL.
  470. *
  471. * @param string Content input
  472. * @return string Content output
  473. */
  474. function TS_images_rte($value) {
  475. $siteUrl = $this->siteUrl();
  476. $sitePath = str_replace(t3lib_div::getIndpEnv('TYPO3_REQUEST_HOST'), '', $siteUrl);
  477. // Split content by <img> tags and traverse the resulting array for processing:
  478. $imgSplit = $this->splitTags('img', $value);
  479. foreach ($imgSplit as $k => $v) {
  480. if ($k % 2) { // image found:
  481. // Init
  482. $attribArray = $this->get_tag_attributes_classic($v, 1);
  483. $absRef = trim($attribArray['src']);
  484. // Unless the src attribute is already pointing to an external URL:
  485. if (strtolower(substr($absRef, 0, 4)) != 'http') {
  486. $attribArray['src'] = substr($attribArray['src'], strlen($this->relBackPath));
  487. // if site is in a subpath (eg. /~user_jim/) this path needs to be removed because it will be added with $siteUrl
  488. $attribArray['src'] = preg_replace('#^' . preg_quote($sitePath, '#') . '#', '', $attribArray['src']);
  489. $attribArray['src'] = $siteUrl . $attribArray['src'];
  490. if (!isset($attribArray['alt'])) {
  491. $attribArray['alt'] = '';
  492. }
  493. $params = t3lib_div::implodeAttributes($attribArray);
  494. $imgSplit[$k] = '<img ' . $params . ' />';
  495. }
  496. }
  497. }
  498. // return processed content:
  499. return implode('', $imgSplit);
  500. }
  501. /**
  502. * Transformation handler: 'ts_reglinks' / direction: "db"+"rte" depending on $direction variable.
  503. * Converting <A>-tags to/from abs/rel
  504. *
  505. * @param string Content input
  506. * @param string Direction of conversion; "rte" (from database to RTE) or "db" (from RTE to database)
  507. * @return string Content output
  508. */
  509. function TS_reglinks($value, $direction) {
  510. $retVal = '';
  511. switch ($direction) {
  512. case 'rte':
  513. $retVal = $this->TS_AtagToAbs($value, 1);
  514. break;
  515. case 'db':
  516. $siteURL = $this->siteUrl();
  517. $blockSplit = $this->splitIntoBlock('A', $value);
  518. foreach ($blockSplit as $k => $v) {
  519. if ($k % 2) { // block:
  520. $attribArray = $this->get_tag_attributes_classic($this->getFirstTag($v), 1);
  521. // If the url is local, remove url-prefix
  522. if ($siteURL && substr($attribArray['href'], 0, strlen($siteURL)) == $siteURL) {
  523. $attribArray['href'] = $this->relBackPath . substr($attribArray['href'], strlen($siteURL));
  524. }
  525. $bTag = '<a ' . t3lib_div::implodeAttributes($attribArray, 1) . '>';
  526. $eTag = '</a>';
  527. $blockSplit[$k] = $bTag . $this->TS_reglinks($this->removeFirstAndLastTag($blockSplit[$k]), $direction) . $eTag;
  528. }
  529. }
  530. $retVal = implode('', $blockSplit);
  531. break;
  532. }
  533. return $retVal;
  534. }
  535. /**
  536. * Transformation handler: 'ts_links' / direction: "db"
  537. * Converting <A>-tags to <link tags>
  538. *
  539. * @param string Content input
  540. * @return string Content output
  541. * @see TS_links_rte()
  542. */
  543. function TS_links_db($value) {
  544. // Split content into <a> tag blocks and process:
  545. $blockSplit = $this->splitIntoBlock('A', $value);
  546. foreach ($blockSplit as $k => $v) {
  547. if ($k % 2) { // If an A-tag was found:
  548. $attribArray = $this->get_tag_attributes_classic($this->getFirstTag($v), 1);
  549. $info = $this->urlInfoForLinkTags($attribArray['href']);
  550. // Check options:
  551. $attribArray_copy = $attribArray;
  552. unset($attribArray_copy['href']);
  553. unset($attribArray_copy['target']);
  554. unset($attribArray_copy['class']);
  555. unset($attribArray_copy['title']);
  556. unset($attribArray_copy['external']);
  557. if ($attribArray_copy['rteerror']) { // Unset "rteerror" and "style" attributes if "rteerror" is set!
  558. unset($attribArray_copy['style']);
  559. unset($attribArray_copy['rteerror']);
  560. }
  561. if (!count($attribArray_copy)) { // Only if href, target and class are the only attributes, we can alter the link!
  562. // Quoting class and title attributes if they contain spaces
  563. $attribArray['class'] = preg_match('/ /', $attribArray['class']) ? '"' . $attribArray['class'] . '"' : $attribArray['class'];
  564. $attribArray['title'] = preg_match('/ /', $attribArray['title']) ? '"' . $attribArray['title'] . '"' : $attribArray['title'];
  565. // Creating the TYPO3 pseudo-tag "<LINK>" for the link (includes href/url, target and class attributes):
  566. // If external attribute is set, keep the href unchanged
  567. $href = $attribArray['external'] ? $attribArray['href'] : $info['url'] . ($info['query'] ? ',0,' . $info['query'] : '');
  568. $bTag = '<link ' . $href . ($attribArray['target'] ? ' ' . $attribArray['target'] : (($attribArray['class'] || $attribArray['title']) ? ' -' : '')) . ($attribArray['class'] ? ' ' . $attribArray['class'] : ($attribArray['title'] ? ' -' : '')) . ($attribArray['title'] ? ' ' . $attribArray['title'] : '') . '>';
  569. $eTag = '</link>';
  570. $blockSplit[$k] = $bTag . $this->TS_links_db($this->removeFirstAndLastTag($blockSplit[$k])) . $eTag;
  571. } else { // ... otherwise store the link as a-tag.
  572. // Unsetting 'rtekeep' attribute if that had been set.
  573. unset($attribArray['rtekeep']);
  574. if (!$attribArray['external']) {
  575. // If the url is local, remove url-prefix
  576. $siteURL = $this->siteUrl();
  577. if ($siteURL && substr($attribArray['href'], 0, strlen($siteURL)) == $siteURL) {
  578. $attribArray['href'] = $this->relBackPath . substr($attribArray['href'], strlen($siteURL));
  579. }
  580. }
  581. unset($attribArray['external']);
  582. $bTag = '<a ' . t3lib_div::implodeAttributes($attribArray, 1) . '>';
  583. $eTag = '</a>';
  584. $blockSplit[$k] = $bTag . $this->TS_links_db($this->removeFirstAndLastTag($blockSplit[$k])) . $eTag;
  585. }
  586. }
  587. }
  588. return implode('', $blockSplit);
  589. }
  590. /**
  591. * Transformation handler: 'ts_links' / direction: "rte"
  592. * Converting <link tags> to <A>-tags
  593. *
  594. * @param string Content input
  595. * @return string Content output
  596. * @see TS_links_rte()
  597. */
  598. function TS_links_rte($value) {
  599. $value = $this->TS_AtagToAbs($value);
  600. // Split content by the TYPO3 pseudo tag "<link>":
  601. $blockSplit = $this->splitIntoBlock('link', $value, 1);
  602. $siteUrl = $this->siteUrl();
  603. foreach ($blockSplit as $k => $v) {
  604. $error = '';
  605. if ($k % 2) { // block:
  606. $tagCode = t3lib_div::unQuoteFilenames(trim(substr($this->getFirstTag($v), 0, -1)), true);
  607. $link_param = $tagCode[1];
  608. $href = '';
  609. // Parsing the typolink data. This parsing is roughly done like in tslib_content->typolink()
  610. if (strstr($link_param, '@')) { // mailadr
  611. $href = 'mailto:' . preg_replace('/^mailto:/i', '', $link_param);
  612. } elseif (substr($link_param, 0, 1) == '#') { // check if anchor
  613. $href = $siteUrl . $link_param;
  614. } else {
  615. $fileChar = intval(strpos($link_param, '/'));
  616. $urlChar = intval(strpos($link_param, '.'));
  617. $external = FALSE;
  618. // Parse URL:
  619. $pU = parse_url($link_param);
  620. // Detects if a file is found in site-root OR is a simulateStaticDocument.
  621. list($rootFileDat) = explode('?', $link_param);
  622. $rFD_fI = pathinfo($rootFileDat);
  623. if (trim($rootFileDat) && !strstr($link_param, '/') && (@is_file(PATH_site . $rootFileDat) || t3lib_div::inList('php,html,htm', strtolower($rFD_fI['extension'])))) {
  624. $href = $siteUrl . $link_param;
  625. } elseif ($pU['scheme'] || ($urlChar && (!$fileChar || $urlChar < $fileChar))) {
  626. // url (external): if has scheme or if a '.' comes before a '/'.
  627. $href = $link_param;
  628. if (!$pU['scheme']) {
  629. $href = 'http://' . $href;
  630. }
  631. $external = TRUE;
  632. } elseif ($fileChar) { // file (internal)
  633. $href = $siteUrl . $link_param;
  634. } else { // integer or alias (alias is without slashes or periods or commas, that is 'nospace,alphanum_x,lower,unique' according to tables.php!!)
  635. // Splitting the parameter by ',' and if the array counts more than 1 element it's a id/type/parameters triplet
  636. $pairParts = t3lib_div::trimExplode(',', $link_param, TRUE);
  637. $idPart = $pairParts[0];
  638. $link_params_parts = explode('#', $idPart);
  639. $idPart = trim($link_params_parts[0]);
  640. $sectionMark = trim($link_params_parts[1]);
  641. if (!strcmp($idPart, '')) {
  642. $idPart = $this->recPid;
  643. } // If no id or alias is given, set it to class record pid
  644. // Checking if the id-parameter is an alias.
  645. if (!t3lib_div::testInt($idPart)) {
  646. list($idPartR) = t3lib_BEfunc::getRecordsByField('pages', 'alias', $idPart);
  647. $idPart = intval($idPartR['uid']);
  648. }
  649. $page = t3lib_BEfunc::getRecord('pages', $idPart);
  650. if (is_array($page)) { // Page must exist...
  651. $href = $siteUrl . '?id=' . $idPart . ($pairParts[2] ? $pairParts[2] : '') . ($sectionMark ? '#' . $sectionMark : '');
  652. // linkHandler - allowing links to start with registerd linkHandler e.g.. "record:"
  653. } elseif (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_content.php']['typolinkLinkHandler'][array_shift(explode(':', $link_param))])) {
  654. $href = $link_param;
  655. } else {
  656. #$href = '';
  657. $href = $siteUrl . '?id=' . $link_param;
  658. $error = 'No page found: ' . $idPart;
  659. }
  660. }
  661. }
  662. // Setting the A-tag:
  663. $bTag = '<a href="' . htmlspecialchars($href) . '"' .
  664. ($tagCode[2] && $tagCode[2] != '-' ? ' target="' . htmlspecialchars($tagCode[2]) . '"' : '') .
  665. ($tagCode[3] && $tagCode[3] != '-' ? ' class="' . htmlspecialchars($tagCode[3]) . '"' : '') .
  666. ($tagCode[4] ? ' title="' . htmlspecialchars($tagCode[4]) . '"' : '') .
  667. ($external ? ' external="1"' : '') .
  668. ($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...
  669. '>';
  670. $eTag = '</a>';
  671. $blockSplit[$k] = $bTag . $this->TS_links_rte($this->removeFirstAndLastTag($blockSplit[$k])) . $eTag;
  672. }
  673. }
  674. // Return content:
  675. return implode('', $blockSplit);
  676. }
  677. /**
  678. * Preserve special tags
  679. *
  680. * @param string Content input
  681. * @return string Content output
  682. */
  683. function TS_preserve_db($value) {
  684. if (!$this->preserveTags) {
  685. return $value;
  686. }
  687. // Splitting into blocks for processing (span-tags are used for special tags)
  688. $blockSplit = $this->splitIntoBlock('span', $value);
  689. foreach ($blockSplit as $k => $v) {
  690. if ($k % 2) { // block:
  691. $attribArray = $this->get_tag_attributes_classic($this->getFirstTag($v));
  692. if ($attribArray['specialtag']) {
  693. $theTag = rawurldecode($attribArray['specialtag']);
  694. $theTagName = $this->getFirstTagName($theTag);
  695. $blockSplit[$k] = $theTag . $this->removeFirstAndLastTag($blockSplit[$k]) . '</' . $theTagName . '>';
  696. }
  697. }
  698. }
  699. return implode('', $blockSplit);
  700. }
  701. /**
  702. * Preserve special tags
  703. *
  704. * @param string Content input
  705. * @return string Content output
  706. */
  707. function TS_preserve_rte($value) {
  708. if (!$this->preserveTags) {
  709. return $value;
  710. }
  711. $blockSplit = $this->splitIntoBlock($this->preserveTags, $value);
  712. foreach ($blockSplit as $k => $v) {
  713. if ($k % 2) { // block:
  714. $blockSplit[$k] = '<span specialtag="' . rawurlencode($this->getFirstTag($v)) . '">' . $this->removeFirstAndLastTag($blockSplit[$k]) . '</span>';
  715. }
  716. }
  717. return implode('', $blockSplit);
  718. }
  719. /**
  720. * Transformation handler: 'ts_transform' + 'css_transform' / direction: "db"
  721. * Cleaning (->db) for standard content elements (ts)
  722. *
  723. * @param string Content input
  724. * @param boolean If true, the transformation was "css_transform", otherwise "ts_transform"
  725. * @return string Content output
  726. * @see TS_transform_rte()
  727. */
  728. function TS_transform_db($value, $css = FALSE) {
  729. // safety... so forever loops are avoided (they should not occur, but an error would potentially do this...)
  730. $this->TS_transform_db_safecounter--;
  731. if ($this->TS_transform_db_safecounter < 0) {
  732. return $value;
  733. }
  734. // Split the content from RTE by the occurence of these blocks:
  735. $blockSplit = $this->splitIntoBlock('TABLE,BLOCKQUOTE,' . ($this->procOptions['preserveDIVSections'] ? 'DIV,' : '') . $this->blockElementList, $value);
  736. $cc = 0;
  737. $aC = count($blockSplit);
  738. // Avoid superfluous linebreaks by transform_db after ending headListTag
  739. while ($aC && !strcmp(trim($blockSplit[$aC - 1]), '')) {
  740. unset($blockSplit[$aC - 1]);
  741. $aC = count($blockSplit);
  742. }
  743. // Traverse the blocks
  744. foreach ($blockSplit as $k => $v) {
  745. $cc++;
  746. $lastBR = $cc == $aC ? '' : LF;
  747. if ($k % 2) { // Inside block:
  748. // Init:
  749. $tag = $this->getFirstTag($v);
  750. $tagName = strtolower($this->getFirstTagName($v));
  751. // Process based on the tag:
  752. switch ($tagName) {
  753. case 'blockquote': // Keep blockquotes, but clean the inside recursively in the same manner as the main code
  754. case 'dd' : // Do the same on dd elements
  755. case 'div': // Do the same on div sections, if they were splitted
  756. $blockSplit[$k] = $tag . $this->TS_transform_db($this->removeFirstAndLastTag($blockSplit[$k]), $css) . '</' . $tagName . '>' . $lastBR;
  757. break;
  758. case 'ol':
  759. case 'ul': // Transform lists into <typolist>-tags:
  760. if (!$css) {
  761. if (!isset($this->procOptions['typolist']) || $this->procOptions['typolist']) {
  762. $parts = $this->getAllParts($this->splitIntoBlock('LI', $this->removeFirstAndLastTag($blockSplit[$k])), 1, 0);
  763. foreach ($parts as $k2 => $value) {
  764. $parts[$k2] = preg_replace('/[' . preg_quote(LF . CR) . ']+/', '', $parts[$k2]); // remove all linesbreaks!
  765. $parts[$k2] = $this->defaultTStagMapping($parts[$k2], 'db');
  766. $parts[$k2] = $this->cleanFontTags($parts[$k2], 0, 0, 0);
  767. $parts[$k2] = $this->HTMLcleaner_db($parts[$k2], strtolower($this->procOptions['allowTagsInTypolists'] ? $this->procOptions['allowTagsInTypolists'] : 'br,font,b,i,u,a,img,span,strong,em'));
  768. }
  769. if ($tagName == 'ol') {
  770. $params = ' type="1"';
  771. } else {
  772. $params = '';
  773. }
  774. $blockSplit[$k] = '<typolist' . $params . '>' . LF . implode(LF, $parts) . LF . '</typolist>' . $lastBR;
  775. }
  776. } else {
  777. $blockSplit[$k] = preg_replace('/[' . preg_quote(LF . CR) . ']+/', ' ', $this->transformStyledATags($blockSplit[$k])) . $lastBR;
  778. }
  779. break;
  780. case 'table': // Tables are NOT allowed in any form (unless preserveTables is set or CSS is the mode)
  781. if (!$this->procOptions['preserveTables'] && !$css) {
  782. $blockSplit[$k] = $this->TS_transform_db($this->removeTables($blockSplit[$k]));
  783. } else {
  784. $blockSplit[$k] = preg_replace('/[' . preg_quote(LF . CR) . ']+/', ' ', $this->transformStyledATags($blockSplit[$k])) . $lastBR;
  785. }
  786. break;
  787. case 'h1':
  788. case 'h2':
  789. case 'h3':
  790. case 'h4':
  791. case 'h5':
  792. case 'h6':
  793. if (!$css) {
  794. $attribArray = $this->get_tag_attributes_classic($tag);
  795. // Processing inner content here:
  796. $innerContent = $this->HTMLcleaner_db($this->removeFirstAndLastTag($blockSplit[$k]));
  797. if (!isset($this->procOptions['typohead']) || $this->procOptions['typohead']) {
  798. $type = intval(substr($tagName, 1));
  799. $blockSplit[$k] = '<typohead' .
  800. ($type != 6 ? ' type="' . $type . '"' : '') .
  801. ($attribArray['align'] ? ' align="' . $attribArray['align'] . '"' : '') .
  802. ($attribArray['class'] ? ' class="' . $attribArray['class'] . '"' : '') .
  803. '>' .
  804. $innerContent .
  805. '</typohead>' .
  806. $lastBR;
  807. } else {
  808. $blockSplit[$k] = '<' . $tagName .
  809. ($attribArray['align'] ? ' align="' . htmlspecialchars($attribArray['align']) . '"' : '') .
  810. ($attribArray['class'] ? ' class="' . htmlspecialchars($attribArray['class']) . '"' : '') .
  811. '>' .
  812. $innerContent .
  813. '</' . $tagName . '>' .
  814. $lastBR;
  815. }
  816. } else {
  817. // Eliminate true linebreaks inside Hx tags
  818. $blockSplit[$k] = preg_replace('/[' . preg_quote(LF . CR) . ']+/', ' ', $this->transformStyledATags($blockSplit[$k])) . $lastBR;
  819. }
  820. break;
  821. default:
  822. // Eliminate true linebreaks inside other headlist tags
  823. $blockSplit[$k] = preg_replace('/[' . preg_quote(LF . CR) . ']+/', ' ', $this->transformStyledATags($blockSplit[$k])) . $lastBR;
  824. break;
  825. }
  826. } else { // NON-block:
  827. if (strcmp(trim($blockSplit[$k]), '')) {
  828. // Remove linebreaks following hr tags
  829. $blockSplit[$k] = preg_replace('/<(hr)(\s[^>\/]*)?[[:space:]]*\/?>[' . preg_quote(LF . CR) . ']+/', '<$1$2 />', $blockSplit[$k]);
  830. // Replace other linebreaks with space
  831. $blockSplit[$k] = preg_replace('/[' . preg_quote(LF . CR) . ']+/', ' ', $blockSplit[$k]);
  832. $blockSplit[$k] = $this->divideIntoLines($blockSplit[$k]) . $lastBR;
  833. $blockSplit[$k] = $this->transformStyledATags($blockSplit[$k]);
  834. } else {
  835. unset($blockSplit[$k]);
  836. }
  837. }
  838. }
  839. $this->TS_transform_db_safecounter++;
  840. return implode('', $blockSplit);
  841. }
  842. /**
  843. * Wraps a-tags that contain a style attribute with a span-tag
  844. *
  845. * @param string Content input
  846. * @return string Content output
  847. */
  848. function transformStyledATags($value) {
  849. $blockSplit = $this->splitIntoBlock('A', $value);
  850. foreach ($blockSplit as $k => $v) {
  851. if ($k % 2) { // If an A-tag was found:
  852. $attribArray = $this->get_tag_attributes_classic($this->getFirstTag($v), 1);
  853. if ($attribArray['style']) { // If "style" attribute is set!
  854. $attribArray_copy['style'] = $attribArray['style'];
  855. unset($attribArray['style']);
  856. $bTag = '<span ' . t3lib_div::implodeAttributes($attribArray_copy, 1) . '><a ' . t3lib_div::implodeAttributes($attribArray, 1) . '>';
  857. $eTag = '</a></span>';
  858. $blockSplit[$k] = $bTag . $this->removeFirstAndLastTag($blockSplit[$k]) . $eTag;
  859. }
  860. }
  861. }
  862. return implode('', $blockSplit);
  863. }
  864. /**
  865. * Transformation handler: 'ts_transform' + 'css_transform' / direction: "rte"
  866. * Set (->rte) for standard content elements (ts)
  867. *
  868. * @param string Content input
  869. * @param boolean If true, the transformation was "css_transform", otherwise "ts_transform"
  870. * @return string Content output
  871. * @see TS_transform_db()
  872. */
  873. function TS_transform_rte($value, $css = 0) {
  874. // Split the content from Database by the occurence of these blocks:
  875. $blockSplit = $this->splitIntoBlock('TABLE,BLOCKQUOTE,TYPOLIST,TYPOHEAD,' . ($this->procOptions['preserveDIVSections'] ? 'DIV,' : '') . $this->blockElementList, $value);
  876. // Traverse the blocks
  877. foreach ($blockSplit as $k => $v) {
  878. if ($k % 2) { // Inside one of the blocks:
  879. // Init:
  880. $tag = $this->getFirstTag($v);
  881. $tagName = strtolower($this->getFirstTagName($v));
  882. $attribArray = $this->get_tag_attributes_classic($tag);
  883. // Based on tagname, we do transformations:
  884. switch ($tagName) {
  885. case 'blockquote': // Keep blockquotes
  886. case 'dd': // Keep definitions
  887. case 'div': // Keep div sections, if they were splitted
  888. $blockSplit[$k] = $tag .
  889. $this->TS_transform_rte($this->removeFirstAndLastTag($blockSplit[$k]), $css) .
  890. '</' . $tagName . '>';
  891. break;
  892. case 'typolist': // Transform typolist blocks into OL/UL lists. Type 1 is expected to be numerical block
  893. if (!isset($this->procOptions['typolist']) || $this->procOptions['typolist']) {
  894. $tListContent = $this->removeFirstAndLastTag($blockSplit[$k]);
  895. $tListContent = preg_replace('/^[ ]*' . LF . '/', '', $tListContent);
  896. $tListContent = preg_replace('/' . LF . '[ ]*$/', '', $tListContent);
  897. $lines = explode(LF, $tListContent);
  898. $typ = $attribArray['type'] == 1 ? 'ol' : 'ul';
  899. $blockSplit[$k] = '<' . $typ . '>' . LF .
  900. '<li>' . implode('</li>' . LF . '<li>', $lines) . '</li>' .
  901. '</' . $typ . '>';
  902. }
  903. break;
  904. case 'typohead': // Transform typohead into Hx tags.
  905. if (!isset($this->procOptions['typohead']) || $this->procOptions['typohead']) {
  906. $tC = $this->removeFirstAndLastTag($blockSplit[$k]);
  907. $typ = t3lib_div::intInRange($attribArray['type'], 0, 6);
  908. if (!$typ) {
  909. $typ = 6;
  910. }
  911. $align = $attribArray['align'] ? ' align="' . $attribArray['align'] . '"' : '';
  912. $class = $attribArray['class'] ? ' class="' . $attribArray['class'] . '"' : '';
  913. $blockSplit[$k] = '<h' . $typ . $align . $class . '>' .
  914. $tC .
  915. '</h' . $typ . '>';
  916. }
  917. break;
  918. }
  919. $blockSplit[$k + 1] = preg_replace('/^[ ]*' . LF . '/', '', $blockSplit[$k + 1]); // Removing linebreak if typohead
  920. } else { // NON-block:
  921. $nextFTN = $this->getFirstTagName($blockSplit[$k + 1]);
  922. $singleLineBreak = $blockSplit[$k] == LF;
  923. if (t3lib_div::inList('TABLE,BLOCKQUOTE,TYPOLIST,TYPOHEAD,' . ($this->procOptions['preserveDIVSections'] ? 'DIV,' : '') . $this->blockElementList, $nextFTN)) { // Removing linebreak if typolist/typohead
  924. $blockSplit[$k] = preg_replace('/' . LF . '[ ]*$/', '', $blockSplit[$k]);
  925. }
  926. // If $blockSplit[$k] is blank then unset the line. UNLESS the line happend to be a single line break.
  927. if (!strcmp($blockSplit[$k], '') && !$singleLineBreak) {
  928. unset($blockSplit[$k]);
  929. } else {
  930. $blockSplit[$k] = $this->setDivTags($blockSplit[$k], ($this->procOptions['useDIVasParagraphTagForRTE'] ? 'div' : 'p'));
  931. }
  932. }
  933. }
  934. return implode(LF, $blockSplit);
  935. }
  936. /**
  937. * Transformation handler: 'ts_strip' / direction: "db"
  938. * Removing all non-allowed tags
  939. *
  940. * @param string Content input
  941. * @return string Content output
  942. */
  943. function TS_strip_db($value) {
  944. $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')) . '>');
  945. return $value;
  946. }
  947. /***************************************************************
  948. *
  949. * Generic RTE transformation, analysis and helper functions
  950. *
  951. **************************************************************/
  952. /**
  953. * Reads the file or url $url and returns the content
  954. *
  955. * @param string Filepath/URL to read
  956. * @return string The content from the resource given as input.
  957. * @see t3lib_div::getURL()
  958. */
  959. function getURL($url) {
  960. return t3lib_div::getURL($url);
  961. }
  962. /**
  963. * Function for cleaning content going into the database.
  964. * Content is cleaned eg. by removing unallowed HTML and ds-HSC content
  965. * 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
  966. *
  967. * @param string Content to clean up
  968. * @param string Comma list of tags to specifically allow. Default comes from getKeepTags and is ""
  969. * @return string Clean content
  970. * @see getKeepTags()
  971. */
  972. function HTMLcleaner_db($content, $tagList = '') {
  973. if (!$tagList) {
  974. $keepTags = $this->getKeepTags('db');
  975. } else {
  976. $keepTags = $this->getKeepTags('db', $tagList);
  977. }
  978. $kUknown = $this->procOptions['dontRemoveUnknownTags_db'] ? 1 : 0; // Default: remove unknown tags.
  979. $hSC = $this->procOptions['dontUndoHSC_db'] ? 0 : -1; // Default: re-convert literals to characters (that is &lt; to <)
  980. // Create additional configuration in order to honor the setting RTE.default.proc.HTMLparser_db.xhtml_cleaning=1
  981. $addConfig = array();
  982. 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'])) {
  983. $addConfig['xhtml'] = 1;
  984. }
  985. return $this->HTMLcleaner($content, $keepTags, $kUknown, $hSC, $addConfig);
  986. }
  987. /**
  988. * Creates an array of configuration for the HTMLcleaner function based on whether content go TO or FROM the Rich Text Editor ($direction)
  989. * 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)
  990. *
  991. * @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)
  992. * @param string Comma list of tags to keep (overriding default which is to keep all + take notice of internal configuration)
  993. * @return array Configuration array
  994. * @see HTMLcleaner_db()
  995. */
  996. function getKeepTags($direction = 'rte', $tagList = '') {
  997. if (!is_array($this->getKeepTags_cache[$direction]) || $tagList) {
  998. // Setting up allowed tags:
  999. if (strcmp($tagList, '')) { // If the $tagList input var is set, this will take precedence
  1000. $keepTags = array_flip(t3lib_div::trimExplode(',', $tagList, 1));
  1001. } else { // Default is to get allowed/denied tags from internal array of processing options:
  1002. // Construct default list of tags to keep:
  1003. $typoScript_list = 'b,i,u,a,img,br,div,center,pre,font,hr,sub,sup,p,strong,em,li,ul,ol,blockquote,strike,span';
  1004. $keepTags = array_flip(t3lib_div::trimExplode(',', $typoScript_list . ',' . strtolower($this->procOptions['allowTags']), 1));
  1005. // For tags to deny, remove them from $keepTags array:
  1006. $denyTags = t3lib_div::trimExplode(',', $this->procOptions['denyTags'], 1);
  1007. foreach ($denyTags as $dKe) {
  1008. unset($keepTags[$dKe]);
  1009. }
  1010. }
  1011. // Based on the direction of content, set further options:
  1012. switch ($direction) {
  1013. // GOING from database to Rich Text Editor:
  1014. case 'rte':
  1015. if (!isset($this->procOptions['transformBoldAndItalicTags']) || $this->procOptions['transformBoldAndItalicTags']) {
  1016. // Transform bold/italics tags to strong/em
  1017. if (isset($keepTags['b'])) {
  1018. $keepTags['b'] = array('remap' => 'STRONG');
  1019. }
  1020. if (isset($keepTags['i'])) {
  1021. $keepTags['i'] = array('remap' => 'EM');
  1022. }
  1023. }
  1024. // 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.
  1025. list($keepTags) = $this->HTMLparserConfig($this->procOptions['HTMLparser_rte.'], $keepTags);
  1026. break;
  1027. // GOING from RTE to database:
  1028. case 'db':
  1029. if (!isset($this->procOptions['transformBoldAndItalicTags']) || $this->procOptions['transformBoldAndItalicTags']) {
  1030. // Transform strong/em back to bold/italics:
  1031. if (isset($keepTags['strong'])) {
  1032. $keepTags['strong'] = array('remap' => 'b');
  1033. }
  1034. if (isset($keepTags['em'])) {
  1035. $keepTags['em'] = array('remap' => 'i');
  1036. }
  1037. }
  1038. // Setting up span tags if they are allowed:
  1039. if (isset($keepTags['span'])) {
  1040. $classes = array_merge(array(''), $this->allowedClasses);
  1041. $keepTags['span'] = array(
  1042. 'allowedAttribs' => 'id,class,style,title,lang,xml:lang,dir',
  1043. 'fixAttrib' => Array(
  1044. 'class' => Array(
  1045. 'list' => $classes,
  1046. 'removeIfFalse' => 1
  1047. ),
  1048. ),
  1049. 'rmTagIfNoAttrib' => 1
  1050. );
  1051. if (!$this->procOptions['allowedClasses']) {
  1052. unset($keepTags['span']['fixAttrib']['class']['list']);
  1053. }
  1054. }
  1055. // Setting up font tags if they are allowed:
  1056. if (isset($keepTags['font'])) {
  1057. $colors = array_merge(array(''), t3lib_div::trimExplode(',', $this->procOptions['allowedFontColors'], 1));
  1058. $keepTags['font'] = array(
  1059. 'allowedAttribs' => 'face,color,size',
  1060. 'fixAttrib' => Array(
  1061. 'face' => Array(
  1062. 'removeIfFalse' => 1
  1063. ),
  1064. 'color' => Array(
  1065. 'removeIfFalse' => 1,
  1066. 'list' => $colors
  1067. ),
  1068. 'size' => Array(
  1069. 'removeIfFalse' => 1,
  1070. )
  1071. ),
  1072. 'rmTagIfNoAttrib' => 1
  1073. );
  1074. if (!$this->procOptions['allowedFontColors']) {
  1075. unset($keepTags['font']['fixAttrib']['color']['list']);
  1076. }
  1077. }
  1078. // Setting further options, getting them from the processiong options:
  1079. $TSc = $this->procOptions['HTMLparser_db.'];
  1080. if (!$TSc['globalNesting']) {
  1081. $TSc['globalNesting'] = 'b,i,u,a,center,font,sub,sup,strong,em,strike,span';
  1082. }
  1083. if (!$TSc['noAttrib']) {
  1084. $TSc['noAttrib'] = 'b,i,u,br,center,hr,sub,sup,strong,em,li,ul,ol,blockquote,strike';
  1085. }
  1086. // Transforming the array from TypoScript to regular array:
  1087. list($keepTags) = $this->HTMLparserConfig($TSc, $keepTags);
  1088. break;
  1089. }
  1090. // Caching (internally, in object memory) the result unless tagList is set:
  1091. if (!$tagList) {
  1092. $this->getKeep

Large files files are truncated, but you can click here to view the full file