[BUGFIX] Absolute URIs get prefixed with absRefPrefix in typolink 18/47118/2
authorOliver Hader <oliver@typo3.org>
Wed, 20 Jan 2016 18:18:24 +0000 (19:18 +0100)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Sat, 5 Mar 2016 19:51:38 +0000 (20:51 +0100)
An absolute URI (e.g. "/folder/someFile.png") gets prefixed with
absRefPrefix (e.g. "/") which results in "//folder/someFile.png",
which is a schema-less URI notation and thus "folder" is
interpreted as hostname.

Resolves: #72846
Releases: master, 7.6
Change-Id: Ic49c0a24a17f754c6c89186e6041b9857eb967b1
Reviewed-on: https://review.typo3.org/47118
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
typo3/sysext/frontend/Tests/Unit/ContentObject/ContentObjectRendererTest.php

index 3efe51e..84eacc5 100644 (file)
@@ -6521,7 +6521,8 @@ class ContentObjectRenderer
                     if ($linktxt === '') {
                         $linktxt = $this->parseFunc(rawurldecode($linkParameter), array('makelinks' => 0), '< lib.parseFunc');
                     }
-                    $this->lastTypoLinkUrl = $this->processUrl(UrlProcessorInterface::CONTEXT_FILE, $GLOBALS['TSFE']->absRefPrefix . $linkParameter, $conf);
+                    $fileUri = (!StringUtility::beginsWith($linkParameter, '/') ? $GLOBALS['TSFE']->absRefPrefix : '') . $linkParameter;
+                    $this->lastTypoLinkUrl = $this->processUrl(UrlProcessorInterface::CONTEXT_FILE, $fileUri, $conf);
                     $this->lastTypoLinkUrl = $this->forceAbsoluteUrl($this->lastTypoLinkUrl, $conf);
                     $target = isset($conf['fileTarget']) ? $conf['fileTarget'] : $tsfe->fileTarget;
                     if ($conf['fileTarget.']) {
index ebb304d..38d4546 100644 (file)
@@ -4628,6 +4628,155 @@ class ContentObjectRendererTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
     }
 
     /**
+     * @return array
+     */
+    public function typolinkReturnsCorrectLinksForFilesWithAbsRefPrefixDataProvider()
+    {
+        return array(
+            'Link to file' => array(
+                'My file',
+                array(
+                    'parameter' => 'fileadmin/foo.bar',
+                ),
+                '/',
+                '<a href="/fileadmin/foo.bar">My file</a>',
+            ),
+            'Link to file with longer absRefPrefix' => array(
+                'My file',
+                array(
+                    'parameter' => 'fileadmin/foo.bar',
+                ),
+                '/sub/',
+                '<a href="/sub/fileadmin/foo.bar">My file</a>',
+            ),
+            'Link to absolute file' => array(
+                'My file',
+                array(
+                    'parameter' => '/images/foo.bar',
+                ),
+                '/',
+                '<a href="/images/foo.bar">My file</a>',
+            ),
+            'Link to absolute file with longer absRefPrefix' => array(
+                'My file',
+                array(
+                    'parameter' => '/images/foo.bar',
+                ),
+                '/sub/',
+                '<a href="/images/foo.bar">My file</a>',
+            ),
+            'Link to absolute file with identical longer absRefPrefix' => array(
+                'My file',
+                array(
+                    'parameter' => '/sub/fileadmin/foo.bar',
+                ),
+                '/sub/',
+                '<a href="/sub/fileadmin/foo.bar">My file</a>',
+            ),
+            'Link to file with empty absRefPrefix' => array(
+                'My file',
+                array(
+                    'parameter' => 'fileadmin/foo.bar',
+                ),
+                '',
+                '<a href="fileadmin/foo.bar">My file</a>',
+            ),
+            'Link to absolute file with empty absRefPrefix' => array(
+                'My file',
+                array(
+                    'parameter' => '/fileadmin/foo.bar',
+                ),
+                '',
+                '<a href="/fileadmin/foo.bar">My file</a>',
+            ),
+            'Link to file with attributes with absRefPrefix' => array(
+                'My file',
+                array(
+                    'parameter' => 'fileadmin/foo.bar',
+                    'ATagParams' => 'class="file-class"',
+                    'fileTarget' => '_blank',
+                    'title' => 'Title of the file',
+                ),
+                '/',
+                '<a href="/fileadmin/foo.bar" title="Title of the file" target="_blank" class="file-class">My file</a>',
+            ),
+            'Link to file with attributes with longer absRefPrefix' => array(
+                'My file',
+                array(
+                    'parameter' => 'fileadmin/foo.bar',
+                    'ATagParams' => 'class="file-class"',
+                    'fileTarget' => '_blank',
+                    'title' => 'Title of the file',
+                ),
+                '/sub/',
+                '<a href="/sub/fileadmin/foo.bar" title="Title of the file" target="_blank" class="file-class">My file</a>',
+            ),
+            'Link to absolute file with attributes with absRefPrefix' => array(
+                'My file',
+                array(
+                    'parameter' => '/images/foo.bar',
+                    'ATagParams' => 'class="file-class"',
+                    'fileTarget' => '_blank',
+                    'title' => 'Title of the file',
+                ),
+                '/',
+                '<a href="/images/foo.bar" title="Title of the file" target="_blank" class="file-class">My file</a>',
+            ),
+            'Link to absolute file with attributes with longer absRefPrefix' => array(
+                'My file',
+                array(
+                    'parameter' => '/images/foo.bar',
+                    'ATagParams' => 'class="file-class"',
+                    'fileTarget' => '_blank',
+                    'title' => 'Title of the file',
+                ),
+                '/sub/',
+                '<a href="/images/foo.bar" title="Title of the file" target="_blank" class="file-class">My file</a>',
+            ),
+            'Link to absolute file with attributes with identical longer absRefPrefix' => array(
+                'My file',
+                array(
+                    'parameter' => '/sub/fileadmin/foo.bar',
+                    'ATagParams' => 'class="file-class"',
+                    'fileTarget' => '_blank',
+                    'title' => 'Title of the file',
+                ),
+                '/sub/',
+                '<a href="/sub/fileadmin/foo.bar" title="Title of the file" target="_blank" class="file-class">My file</a>',
+            ),
+        );
+    }
+
+    /**
+     * @test
+     * @param string $linkText
+     * @param array $configuration
+     * @param string $absRefPrefix
+     * @param string $expectedResult
+     * @dataProvider typolinkReturnsCorrectLinksForFilesWithAbsRefPrefixDataProvider
+     */
+    public function typolinkReturnsCorrectLinksForFilesWithAbsRefPrefix($linkText, $configuration, $absRefPrefix, $expectedResult)
+    {
+        $templateServiceObjectMock = $this->getMock(TemplateService::class, array('dummy'));
+        $templateServiceObjectMock->setup = array(
+            'lib.' => array(
+                'parseFunc.' => $this->getLibParseFunc(),
+            ),
+        );
+        $typoScriptFrontendControllerMockObject = $this->getMock(TypoScriptFrontendController::class, array(), array(), '', false);
+        $typoScriptFrontendControllerMockObject->config = array(
+            'config' => array(),
+            'mainScript' => 'index.php',
+        );
+        $typoScriptFrontendControllerMockObject->tmpl = $templateServiceObjectMock;
+        $GLOBALS['TSFE'] = $typoScriptFrontendControllerMockObject;
+        $GLOBALS['TSFE']->absRefPrefix = $absRefPrefix;
+        $this->subject->_set('typoScriptFrontendController', $typoScriptFrontendControllerMockObject);
+
+        $this->assertEquals($expectedResult, $this->subject->typoLink($linkText, $configuration));
+    }
+
+    /**
      * @test
      */
     public function stdWrap_splitObjReturnsCount()