[TASK] Update copyright year to 2013
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Html / RteHtmlParser.php
index b6dbd90..025924b 100644 (file)
@@ -4,7 +4,7 @@ namespace TYPO3\CMS\Core\Html;
 /***************************************************************
  *  Copyright notice
  *
- *  (c) 1999-2011 Kasper Skårhøj (kasperYYYY@typo3.com)
+ *  (c) 1999-2013 Kasper Skårhøj (kasperYYYY@typo3.com)
  *  All rights reserved
  *
  *  This script is part of the TYPO3 project. The TYPO3 project is
@@ -38,8 +38,6 @@ namespace TYPO3\CMS\Core\Html;
  * Class for parsing HTML for the Rich Text Editor. (also called transformations)
  *
  * @author Kasper Skårhøj <kasperYYYY@typo3.com>
- * @package TYPO3
- * @subpackage t3lib
  */
 class RteHtmlParser extends \TYPO3\CMS\Core\Html\HtmlParser {
 
@@ -279,9 +277,13 @@ class RteHtmlParser extends \TYPO3\CMS\Core\Html\HtmlParser {
                                        case 'ts_transform':
 
                                        case 'css_transform':
-                                               // Has a very disturbing effect, so just remove all '13' - depend on '10'
-                                               $value = str_replace(CR, '', $value);
                                                $this->allowedClasses = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $this->procOptions['allowedClasses'], 1);
+                                               // CR has a very disturbing effect, so just remove all CR and rely on LF
+                                               $value = str_replace(CR, '', $value);
+                                               // Transform empty paragraphs into spacing paragraphs
+                                               $value = str_replace('<p></p>', '<p>&nbsp;</p>', $value);
+                                               // Double any trailing spacing paragraph so that it does not get removed by divideIntoLines()
+                                               $value = preg_replace('/<p>&nbsp;<\/p>$/', '<p>&nbsp;</p>' . '<p>&nbsp;</p>', $value);
                                                $value = $this->TS_transform_db($value, $cmd == 'css_transform');
                                                break;
                                        case 'ts_strip':
@@ -423,11 +425,12 @@ class RteHtmlParser extends \TYPO3\CMS\Core\Html\HtmlParser {
                                        // Check file existence (in relative dir to this installation!)
                                        if ($filepath && @is_file($filepath)) {
                                                // If "magic image":
-                                               $folder = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->getFolderObjectFromCombinedIdentifier($this->rteImageStorageDir());
-                                               if ($folder instanceof \TYPO3\CMS\Core\Resource\Folder) {
-                                                       $storageConfiguration = $folder->getStorage()->getConfiguration();
-                                                       $rteImageStorageDir = rtrim($storageConfiguration['basePath'], '/') . '/' . $folder->getName() . '/';
-                                                       $pathPre = $rteImageStorageDir . 'RTEmagicC_';
+                                               $magicFolder = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->getFolderObjectFromCombinedIdentifier(
+                                                       $this->rteImageStorageDir()
+                                               );
+                                               if ($magicFolder instanceof \TYPO3\CMS\Core\Resource\Folder) {
+                                                       $magicFolderPath = $magicFolder->getPublicUrl();
+                                                       $pathPre = $magicFolderPath . 'RTEmagicC_';
                                                        if (\TYPO3\CMS\Core\Utility\GeneralUtility::isFirstPartOfStr($path, $pathPre)) {
                                                                // Find original file
                                                                if ($attribArray['data-htmlarea-file-uid']) {
@@ -436,9 +439,9 @@ class RteHtmlParser extends \TYPO3\CMS\Core\Html\HtmlParser {
                                                                        // Backward compatibility mode
                                                                        $pI = pathinfo(substr($path, strlen($pathPre)));
                                                                        $filename = substr($pI['basename'], 0, -strlen(('.' . $pI['extension'])));
-                                                                       $origFilePath = PATH_site . $rteImageStorageDir . 'RTEmagicP_' . $filename;
+                                                                       $origFilePath = PATH_site . $magicFolderPath . 'RTEmagicP_' . $filename;
                                                                        if (@is_file($origFilePath)) {
-                                                                               $originalFileObject = $folder->addFile($origFilePath, $filename, 'changeName');
+                                                                               $originalFileObject = $magicFolder->addFile($origFilePath, $filename, 'changeName');
                                                                                $attribArray['data-htmlarea-file-uid'] = $originalFileObject->getUid();
                                                                        }
                                                                }
@@ -549,10 +552,38 @@ class RteHtmlParser extends \TYPO3\CMS\Core\Html\HtmlParser {
                                $absRef = trim($attribArray['src']);
                                // Unless the src attribute is already pointing to an external URL:
                                if (strtolower(substr($absRef, 0, 4)) != 'http') {
-                                       $attribArray['src'] = substr($attribArray['src'], strlen($this->relBackPath));
-                                       // If site is in a subpath (eg. /~user_jim/) this path needs to be removed because it will be added with $siteUrl
-                                       $attribArray['src'] = preg_replace('#^' . preg_quote($sitePath, '#') . '#', '', $attribArray['src']);
-                                       $attribArray['src'] = $siteUrl . $attribArray['src'];
+                                       $isMagicImage = FALSE;
+                                       $fileFactory = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance();
+                                       $magicFolder = $fileFactory->getFolderObjectFromCombinedIdentifier(
+                                               $this->rteImageStorageDir()
+                                       );
+                                       if ($magicFolder instanceof \TYPO3\CMS\Core\Resource\Folder) {
+                                               $magicFolderPath = $magicFolder->getPublicUrl();
+                                               $pathPre = $magicFolderPath . 'RTEmagicC_';
+                                               if (\TYPO3\CMS\Core\Utility\GeneralUtility::isFirstPartOfStr($attribArray['src'], $pathPre)) {
+                                                       $isMagicImage = TRUE;
+                                               }
+                                       }
+                                       if ($attribArray['data-htmlarea-file-uid'] && !$isMagicImage) {
+                                               $fileObject = $fileFactory->getFileObject($attribArray['data-htmlarea-file-uid']);
+                                               $filePath = $fileObject->getForLocalProcessing(FALSE);
+                                               $attribArray['src'] = $siteUrl . substr($filePath, strlen(PATH_site));
+                                       } else {
+                                               $attribArray['src'] = substr($attribArray['src'], strlen($this->relBackPath));
+                                               // if site is in a subpath (eg. /~user_jim/) this path needs to be removed because it will be added with $siteUrl
+                                               $attribArray['src'] = preg_replace('#^' . preg_quote($sitePath, '#') . '#', '', $attribArray['src']);
+                                               // If the image is not magic and does not have a file uid, try to add the uid
+                                               if (!$attribArray['data-htmlarea-file-uid'] && !$isMagicImage) {
+                                                       $fileOrFolderObject = $fileFactory->retrieveFileOrFolderObject($attribArray['src']);
+                                                       if ($fileOrFolderObject instanceof \TYPO3\CMS\Core\Resource\FileInterface) {
+                                                               $fileIdentifier = $fileOrFolderObject->getIdentifier();
+                                                               $fileObject = $fileOrFolderObject->getStorage()->getFile($fileIdentifier);
+                                                               $attribArray['data-htmlarea-file-uid'] = $fileObject->getUid();
+                                                               $attribArray['data-htmlarea-file-table'] = 'sys_file';
+                                                       }
+                                               }
+                                               $attribArray['src'] = $siteUrl . $attribArray['src'];
+                                       }
                                        if (!isset($attribArray['alt'])) {
                                                $attribArray['alt'] = '';
                                        }
@@ -713,7 +744,9 @@ class RteHtmlParser extends \TYPO3\CMS\Core\Html\HtmlParser {
                                $link_param = $tagCode[1];
                                $href = '';
                                // Parsing the typolink data. This parsing is roughly done like in tslib_content->typolink()
-                               if (strstr($link_param, '@')) {
+                               // Parse URL:
+                               $pU = parse_url($link_param);
+                               if (strstr($link_param, '@') && (!$pU['scheme'] || $pU['scheme'] == 'mailto')) {
                                        // mailadr
                                        $href = 'mailto:' . preg_replace('/^mailto:/i', '', $link_param);
                                } elseif (substr($link_param, 0, 1) == '#') {
@@ -727,8 +760,6 @@ class RteHtmlParser extends \TYPO3\CMS\Core\Html\HtmlParser {
                                        } else {
                                                $fileChar = intval(strpos($link_param, '/'));
                                                $urlChar = intval(strpos($link_param, '.'));
-                                               // Parse URL:
-                                               $pU = parse_url($link_param);
                                                // Detects if a file is found in site-root.
                                                list($rootFileDat) = explode('?', $link_param);
                                                $rFD_fI = pathinfo($rootFileDat);
@@ -742,8 +773,21 @@ class RteHtmlParser extends \TYPO3\CMS\Core\Html\HtmlParser {
                                                        }
                                                        $external = TRUE;
                                                } elseif ($fileChar) {
-                                                       // file (internal)
-                                                       $href = $siteUrl . $link_param;
+                                                       // It is an internal file or folder
+                                                       // Try to transform the href into a FAL reference
+                                                       $fileOrFolderObject = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->retrieveFileOrFolderObject($link_param);
+                                                       if ($fileOrFolderObject instanceof \TYPO3\CMS\Core\Resource\Folder) {
+                                                               // It's a folder
+                                                               $folderIdentifier = $fileOrFolderObject->getIdentifier();
+                                                               $href = $siteUrl . '?file:' . rawurlencode($folderIdentifier);
+                                                       } elseif ($fileOrFolderObject instanceof \TYPO3\CMS\Core\Resource\FileInterface) {
+                                                               // It's a file
+                                                               $fileIdentifier = $fileOrFolderObject->getIdentifier();
+                                                               $fileObject = $fileOrFolderObject->getStorage()->getFile($fileIdentifier);
+                                                               $href = $siteUrl . '?file:' . $fileObject->getUid();
+                                                       } else {
+                                                               $href = $siteUrl . $link_param;
+                                                       }
                                                } else {
                                                        // integer or alias (alias is without slashes or periods or commas, that is 'nospace,alphanum_x,lower,unique' according to tables.php!!)
                                                        // Splitting the parameter by ',' and if the array counts more than 1 element it's a id/type/parameters triplet
@@ -1025,8 +1069,9 @@ class RteHtmlParser extends \TYPO3\CMS\Core\Html\HtmlParser {
         * @todo Define visibility
         */
        public function TS_transform_rte($value, $css = 0) {
-               // Split the content from Database by the occurence of these blocks:
-               $blockSplit = $this->splitIntoBlock('TABLE,BLOCKQUOTE,TYPOLIST,TYPOHEAD,' . ($this->procOptions['preserveDIVSections'] ? 'DIV,' : '') . $this->blockElementList, $value);
+               // Split the content from database by the occurence of the block elements
+               $blockElementList = 'TABLE,BLOCKQUOTE,TYPOLIST,TYPOHEAD,' . ($this->procOptions['preserveDIVSections'] ? 'DIV,' : '') . $this->blockElementList;
+               $blockSplit = $this->splitIntoBlock($blockElementList, $value);
                // Traverse the blocks
                foreach ($blockSplit as $k => $v) {
                        if ($k % 2) {
@@ -1085,13 +1130,19 @@ class RteHtmlParser extends \TYPO3\CMS\Core\Html\HtmlParser {
                        } else {
                                // NON-block:
                                $nextFTN = $this->getFirstTagName($blockSplit[$k + 1]);
-                               $singleLineBreak = $blockSplit[$k] == LF;
-                               if (\TYPO3\CMS\Core\Utility\GeneralUtility::inList('TABLE,BLOCKQUOTE,TYPOLIST,TYPOHEAD,' . ($this->procOptions['preserveDIVSections'] ? 'DIV,' : '') . $this->blockElementList, $nextFTN)) {
-                                       // Removing linebreak if typolist/typohead
-                                       $blockSplit[$k] = preg_replace('/' . LF . '[ ]*$/', '', $blockSplit[$k]);
+                               $onlyLineBreaks = (preg_match('/^[ ]*' . LF . '+[ ]*$/', $blockSplit[$k]) == 1);
+                               // If the line is followed by a block or is the last line:
+                               if (\TYPO3\CMS\Core\Utility\GeneralUtility::inList($blockElementList, $nextFTN) || !isset($blockSplit[$k + 1])) {
+                                       // If the line contains more than just linebreaks, reduce the number of trailing linebreaks by 1
+                                       if (!$onlyLineBreaks) {
+                                               $blockSplit[$k] = preg_replace('/(' . LF . '*)' . LF . '[ ]*$/', '$1', $blockSplit[$k]);
+                                       } else {
+                                               // If the line contains only linebreaks, remove the leading linebreak
+                                               $blockSplit[$k] = preg_replace('/^[ ]*' . LF . '/', '', $blockSplit[$k]);
+                                       }
                                }
-                               // If $blockSplit[$k] is blank then unset the line. UNLESS the line happend to be a single line break.
-                               if (!strcmp($blockSplit[$k], '') && !$singleLineBreak) {
+                               // If $blockSplit[$k] is blank then unset the line, unless the line only contained linebreaks
+                               if (!strcmp($blockSplit[$k], '') && !$onlyLineBreaks) {
                                        unset($blockSplit[$k]);
                                } else {
                                        $blockSplit[$k] = $this->setDivTags($blockSplit[$k], $this->procOptions['useDIVasParagraphTagForRTE'] ? 'div' : 'p');
@@ -1124,7 +1175,7 @@ class RteHtmlParser extends \TYPO3\CMS\Core\Html\HtmlParser {
         *
         * @param string $url Filepath/URL to read
         * @return string The content from the resource given as input.
-        * @see t3lib_div::getUrl()
+        * @see \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl()
         * @todo Define visibility
         */
        public function getUrl($url) {
@@ -1502,8 +1553,8 @@ class RteHtmlParser extends \TYPO3\CMS\Core\Html\HtmlParser {
        /**
         * Returns SiteURL based on thisScript.
         *
-        * @return string Value of t3lib_div::getIndpEnv('TYPO3_SITE_URL');
-        * @see t3lib_div::getIndpEnv()
+        * @return string Value of \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
+        * @see \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv()
         * @todo Define visibility
         */
        public function siteUrl() {