[BUGFIX] Fix absolute link generation for files 11/34411/3
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:20:02 +0000 (22:20 +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/34411
Reviewed-by: Markus Klein <klein.t3@reelworx.at>
Tested-by: Markus Klein <klein.t3@reelworx.at>
typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
typo3/sysext/frontend/Tests/Unit/ContentObject/ContentObjectRendererTest.php

index dc20c83..0724f05 100644 (file)
@@ -6211,7 +6211,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' => '://',
@@ -6223,6 +6223,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 ee8f998..62d7cb3 100644 (file)
@@ -2650,9 +2650,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(
@@ -2660,6 +2674,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'
+                                       )
+                               )
+                       )
                );
        }
 }