[BUGFIX] Use parse_url to determine absolute urls in ImageService 77/41777/5
authorFrans Saris <franssaris@gmail.com>
Tue, 21 Jul 2015 11:50:22 +0000 (13:50 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Tue, 21 Jul 2015 16:58:05 +0000 (18:58 +0200)
Since it is possible to have protocol relative URIs and parse_url
only identifies a hostname once a scheme or scheme relative prefix is
applied, we can check for a hostname here.

Resolves: #68423
Releases: master, 6.2
Change-Id: I3eb51d9b5175657454657bbd7f75cfafa6bd9760
Reviewed-on: http://review.typo3.org/41777
Reviewed-by: Nicole Cordes <typo3@cordes.co>
Tested-by: Nicole Cordes <typo3@cordes.co>
Reviewed-by: Susanne Moog <typo3@susannemoog.de>
Tested-by: Susanne Moog <typo3@susannemoog.de>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/extbase/Classes/Service/ImageService.php
typo3/sysext/extbase/Tests/Unit/Service/ImageScriptServiceTest.php [new file with mode: 0644]

index b9c42bd..30db6ae 100644 (file)
@@ -69,7 +69,8 @@ class ImageService implements \TYPO3\CMS\Core\SingletonInterface {
                $imageUrl = $image->getPublicUrl();
 
                // no prefix in case of an already fully qualified URL (having a schema)
-               if (strpos($imageUrl, '://')) {
+               // We need to fix the dection for PHP 5.4.6 and below as the host detection is broken
+               if (parse_url($imageUrl, PHP_URL_HOST) !== NULL || strpos($imageUrl, '//') === 0) {
                        $uriPrefix = '';
                } elseif ($this->environmentService->isEnvironmentInFrontendMode()) {
                        $uriPrefix = $GLOBALS['TSFE']->absRefPrefix;
diff --git a/typo3/sysext/extbase/Tests/Unit/Service/ImageScriptServiceTest.php b/typo3/sysext/extbase/Tests/Unit/Service/ImageScriptServiceTest.php
new file mode 100644 (file)
index 0000000..4dabd59
--- /dev/null
@@ -0,0 +1,83 @@
+<?php
+namespace TYPO3\CMS\Extbase\Tests\Unit\Service;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use TYPO3\CMS\Extbase\Service\EnvironmentService;
+use TYPO3\CMS\Extbase\Service\ImageService;
+
+/**
+ * Test case
+ */
+class ImageServiceTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
+
+       /**
+        * @var ImageService
+        */
+       protected $subject;
+
+       /**
+        * @var EnvironmentService|\PHPUnit_Framework_MockObject_MockObject
+        */
+       protected $environmentService;
+
+       /**
+        * Initialize ImageService and environment service mock
+        */
+       protected function setUp() {
+               $this->subject = new ImageService();
+               $this->environmentService = $this->getMock('TYPO3\\CMS\\Extbase\\Service\\EnvironmentService');
+               $this->inject($this->subject, 'environmentService', $this->environmentService);
+       }
+
+       /**
+        * @test
+        */
+       public function fileIsUnwrappedFromReferenceForProcessing() {
+               $reference = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Resource\\FileReference', array(), array(), '', FALSE);
+               $file = $this->getMock('TYPO3\\CMS\\Core\\Resource\\File', array(), array(), '', FALSE);
+               $file->expects($this->once())->method('process')->willReturn($this->getMock('TYPO3\\CMS\\Core\\Resource\\ProcessedFile', array(), array(), '', FALSE));
+               $reference->expects($this->once())->method('getOriginalFile')->willReturn($file);
+               $reference->_set('file', $file);
+
+               $this->subject->applyProcessingInstructions($reference, array());
+       }
+
+       /**
+        * @return array
+        */
+       public function prefixIsCorrectlyAppliedToGetImageUriDataProvider() {
+               return array(
+                       'with scheme' => array('http://foo.bar/img.jpg', 'http://foo.bar/img.jpg'),
+                       'scheme relative' => array('//foo.bar/img.jpg', '//foo.bar/img.jpg'),
+                       'without scheme' => array('foo.bar/img.jpg', '/prefix/foo.bar/img.jpg'),
+               );
+       }
+
+       /**
+        * @test
+        * @dataProvider prefixIsCorrectlyAppliedToGetImageUriDataProvider
+        */
+       public function prefixIsCorrectlyAppliedToGetImageUri($imageUri, $expected) {
+               $this->environmentService->expects($this->any())->method('isEnvironmentInFrontendMode')->willReturn(TRUE);
+               $GLOBALS['TSFE'] = new \stdClass();
+               $GLOBALS['TSFE']->absRefPrefix = '/prefix/';
+
+               $file = $this->getMock('TYPO3\\CMS\\Core\\Resource\\File', array(), array(), '', FALSE);
+               $file->expects($this->once())->method('getPublicUrl')->willReturn($imageUri);
+
+               $this->assertSame($expected, $this->subject->getImageUri($file));
+       }
+
+}