[BUGFIX] Fix absolute link generation for files 36/21036/12
authorMarkus Klein <klein.t3@reelworx.at>
Sat, 25 May 2013 10:47:53 +0000 (12:47 +0200)
committerFrans Saris <franssaris@gmail.com>
Thu, 20 Nov 2014 21:01:39 +0000 (22:01 +0100)
The method ContentRendererObject::forceAbsoluteUrl() fails
to generate absolute URLs for internal file resources even
if the flag "forceAbsoluteUrl" is activated.

Fix this by modifying the regexp that defines what is
accepted as an "already absolute link".

Resolves: #48529
Releases: master, 6.2
Change-Id: Iaf78d946f0f5cd35d175fe39159de72d18a7fa52
Reviewed-on: http://review.typo3.org/21036
Reviewed-by: Tymoteusz Motylewski <t.motylewski@gmail.com>
Tested-by: Tymoteusz Motylewski <t.motylewski@gmail.com>
Reviewed-by: Frans Saris <franssaris@gmail.com>
Tested-by: Frans Saris <franssaris@gmail.com>
typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
typo3/sysext/frontend/Tests/Unit/ContentObject/ContentObjectRendererTest.php

index a77ebb5..b42d66e 100644 (file)
@@ -6375,7 +6375,7 @@ class ContentObjectRenderer {
         */
        protected function forceAbsoluteUrl($url, array $configuration) {
                if (!empty($url) && isset($configuration['forceAbsoluteUrl']) && $configuration['forceAbsoluteUrl']) {
-                       if (preg_match('#^(?:([a-z]+)(://))?([^/]*)(.*)$#', $url, $matches)) {
+                       if (preg_match('#^(?:([a-z]+)(://)([^/]*)/?)?(.*)$#', $url, $matches)) {
                                $urlParts = array(
                                        'scheme' => $matches[1],
                                        'delimiter' => '://',
@@ -6387,6 +6387,7 @@ class ContentObjectRenderer {
                                if (empty($urlParts['host'])) {
                                        $urlParts['scheme'] = 'http';
                                        $urlParts['host'] = $this->getEnvironmentVariable('HTTP_HOST');
+                                       $urlParts['path'] = '/' . ltrim($urlParts['path'], '/');
                                        $isUrlModified = TRUE;
                                }
                                // Override scheme:
index 3cd7b8c..a3e659d 100644 (file)
@@ -2736,9 +2736,23 @@ class ContentObjectRendererTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                                        )
                                )
                        ),
-                       'Scheme can be forced with relative path' => array(
+                       'Relative path old-style' => array(
+                               'http://localhost/fileadmin/dummy.txt',
+                               '/fileadmin/dummy.txt',
+                               array(
+                                       'forceAbsoluteUrl' => '1',
+                               )
+                       ),
+                       'Relative path' => array(
+                               'http://localhost/fileadmin/dummy.txt',
+                               'fileadmin/dummy.txt',
+                               array(
+                                       'forceAbsoluteUrl' => '1',
+                               )
+                       ),
+                       'Scheme can be forced with pseudo-relative path' => array(
                                'typo3://localhost/fileadmin/dummy.txt',
-                               '/fileadmin/dummy.txt', // this leading slash is weird, but we need it to really get an absolute link
+                               '/fileadmin/dummy.txt',
                                array(
                                        'forceAbsoluteUrl' => '1',
                                        'forceAbsoluteUrl.' => array(
@@ -2746,6 +2760,23 @@ class ContentObjectRendererTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                                        )
                                )
                        ),
+                       'Hostname only is not treated as valid absolute URL' => array(
+                               'http://localhost/example.org',
+                               'example.org',
+                               array(
+                                       'forceAbsoluteUrl' => '1'
+                               )
+                       ),
+                       'Scheme and host is added to local file path' => array(
+                               'typo3://localhost/fileadmin/my.pdf',
+                               'fileadmin/my.pdf',
+                               array(
+                                       'forceAbsoluteUrl' => '1',
+                                       'forceAbsoluteUrl.' => array(
+                                               'scheme' => 'typo3'
+                                       )
+                               )
+                       )
                );
        }