From 48e4e495e2f5a85204e4f81bffd2164f095876f7 Mon Sep 17 00:00:00 2001
From: Simon Schaufelberger <simonschaufi+typo3@gmail.com>
Date: Tue, 28 Sep 2021 02:01:05 +0200
Subject: [PATCH] [BUGFIX] Make file paths absolute in GIFBUILDER
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

In CLI context the full path to the temporary generated file by the
GIFBUILDER can't be resolved. Because of this inconsistency, absolute
file paths are used from now on.

Resolves: #95379
Releases: master, 10.4
Change-Id: I6d2a489eee84843fc001ac405f69a15403598388
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/71335
Tested-by: core-ci <typo3@b13.com>
Tested-by: Stefan Bürk <stefan@buerk.tech>
Tested-by: Benni Mack <benni@typo3.org>
Reviewed-by: Stefan Bürk <stefan@buerk.tech>
Reviewed-by: Benni Mack <benni@typo3.org>
---
 .../ContentObject/ContentObjectRenderer.php   |  9 ++--
 .../frontend/Classes/Imaging/GifBuilder.php   |  4 +-
 .../Functional/Imaging/GifBuilderTest.php     | 42 ++++++++++++++++++-
 3 files changed, 49 insertions(+), 6 deletions(-)

diff --git a/typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php b/typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
index 66598ef387a5..f2da8343544f 100644
--- a/typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
+++ b/typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
@@ -4025,13 +4025,16 @@ class ContentObjectRenderer implements LoggerAwareInterface
         $imageResource = null;
         if ($file === 'GIFBUILDER') {
             $gifCreator = GeneralUtility::makeInstance(GifBuilder::class);
-            $theImage = '';
             if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib']) {
                 $gifCreator->start($fileArray, $this->data);
                 $theImage = $gifCreator->gifBuild();
+                if (!empty($theImage)) {
+                    // Use absolute path to retrieve image dimension to mitigate path inconsistency in cli context,
+                    // but use the relative path for further processing.
+                    $imageResource = $gifCreator->getImageDimensions(Environment::getPublicPath() . '/' . $theImage);
+                    $imageResource['origFile'] = $theImage;
+                }
             }
-            $imageResource = $gifCreator->getImageDimensions($theImage);
-            $imageResource['origFile'] = $theImage;
         } else {
             if ($file instanceof File) {
                 $fileObject = $file;
diff --git a/typo3/sysext/frontend/Classes/Imaging/GifBuilder.php b/typo3/sysext/frontend/Classes/Imaging/GifBuilder.php
index badd559d9e82..78ac8522e5ee 100644
--- a/typo3/sysext/frontend/Classes/Imaging/GifBuilder.php
+++ b/typo3/sysext/frontend/Classes/Imaging/GifBuilder.php
@@ -346,12 +346,12 @@ class GifBuilder extends GraphicalFunctions
             // Relative to Environment::getPublicPath()
             $gifFileName = $this->fileName('assets/images/');
             // File exists
-            if (!file_exists($gifFileName)) {
+            if (!file_exists(Environment::getPublicPath() . '/' . $gifFileName)) {
                 // Create temporary directory if not done:
                 GeneralUtility::mkdir_deep(Environment::getPublicPath() . '/typo3temp/assets/images/');
                 // Create file:
                 $this->make();
-                $this->output($gifFileName);
+                $this->output(Environment::getPublicPath() . '/' . $gifFileName);
                 $this->destroy();
             }
             return $gifFileName;
diff --git a/typo3/sysext/frontend/Tests/Functional/Imaging/GifBuilderTest.php b/typo3/sysext/frontend/Tests/Functional/Imaging/GifBuilderTest.php
index df2e467f1ce0..2a5bdf344256 100644
--- a/typo3/sysext/frontend/Tests/Functional/Imaging/GifBuilderTest.php
+++ b/typo3/sysext/frontend/Tests/Functional/Imaging/GifBuilderTest.php
@@ -30,6 +30,44 @@ use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
  */
 class GifBuilderTest extends FunctionalTestCase
 {
+    /**
+     * Sets up Environment to simulate Composer mode and a cli request
+     */
+    protected function simulateCliRequestInComposerMode(): void
+    {
+        Environment::initialize(
+            Environment::getContext(),
+            true,
+            true,
+            Environment::getProjectPath(),
+            Environment::getPublicPath() . '/public',
+            Environment::getVarPath(),
+            Environment::getConfigPath(),
+            Environment::getCurrentScript(),
+            Environment::isWindows() ? 'WINDOWS' : 'UNIX'
+        );
+    }
+
+    /**
+     * @test
+     */
+    public function buildImageInCommandLineInterfaceAndComposerContext(): void
+    {
+        $this->simulateCliRequestInComposerMode();
+
+        $fileArray = [
+            'XY' => '10,10',
+            'format' => 'jpg',
+        ];
+
+        $gifBuilder = new GifBuilder();
+        $gifBuilder->start($fileArray, []);
+        $gifFileName = $gifBuilder->gifBuild();
+
+        self::assertFileDoesNotExist(Environment::getProjectPath() . '/' . $gifFileName);
+        self::assertFileExists(Environment::getPublicPath() . '/' . $gifFileName);
+    }
+
     /**
      * Check hashes of Images overlayed with other images are idempotent
      *
@@ -59,8 +97,10 @@ class GifBuilderTest extends FunctionalTestCase
             'quality' => 88,
             '10' => 'IMAGE',
             '10.' => [
-                'file.width' => 300,
                 'file' => $file,
+                'file.' => [
+                    'width' => 300,
+                ],
             ],
             '30' => 'IMAGE',
             '30.' => [
-- 
GitLab