[BUGFIX] ResourceCompressor must deal with absolute web paths 21/50921/2
authorMarkus Klein <markus.klein@typo3.org>
Fri, 25 Nov 2016 19:29:55 +0000 (20:29 +0100)
committerSusanne Moog <susanne.moog@typo3.org>
Fri, 9 Dec 2016 18:09:25 +0000 (19:09 +0100)
ResourceCompressor lacks the ability to compress files which are
registered with their absolute web path (as FormEngine does).
Add a check to resolve such paths correctly.

Resolves: #78803
Releases: master
Change-Id: Ifdd60c65d9023aca57b5b20cf3f44e1bef121aa3
Reviewed-on: https://review.typo3.org/50921
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Susanne Moog <susanne.moog@typo3.org>
Tested-by: Susanne Moog <susanne.moog@typo3.org>
typo3/sysext/core/Classes/Resource/ResourceCompressor.php

index 91359ef..c2d7d7e 100644 (file)
@@ -415,13 +415,38 @@ class ResourceCompressor
      * Finds the relative path to a file, relative to the root path.
      *
      * @param string $filename the name of the file
-     * @return string the path to the file relative to the root path
+     * @return string the path to the file relative to the root path ($this->rootPath)
      */
     protected function getFilenameFromMainDir($filename)
     {
+        /*
+         * The various paths may have those values (e.g. if TYPO3 is installed in a subdir)
+         * - docRoot = /var/www/html/
+         * - PATH_site = /var/www/html/sites/site1/
+         * - $this->rootPath = /var/www/html/sites/site1/typo3
+         *
+         * The file names passed into this function may be either:
+         * - relative to $this->rootPath
+         * - relative to PATH_site
+         * - relative to docRoot
+         */
+        $docRoot = GeneralUtility::getIndpEnv('TYPO3_DOCUMENT_ROOT');
+        $fileNameWithoutSlash = ltrim($filename, '/');
+
+        // if the file is an absolute reference within the docRoot
+        $absolutePath = $docRoot . '/' . $fileNameWithoutSlash;
+        if (is_file($absolutePath)) {
+            if (strpos($absolutePath, $this->rootPath) === 0) {
+                // the path is within the current root path, simply strip rootPath off
+                return substr($absolutePath, strlen($this->rootPath));
+            }
+            // the path is not within the root path, strip off the site path, the remaining logic below
+            // takes care about adjusting the path correctly.
+            $filename = substr($absolutePath, strlen(PATH_site));
+        }
         // if the file exists in the root path, just return the $filename
-        if (is_file($this->rootPath . ltrim($filename, '/'))) {
-            return ltrim($filename, '/');
+        if (is_file($this->rootPath . $fileNameWithoutSlash)) {
+            return $fileNameWithoutSlash;
         }
         // if the file is from a special TYPO3 internal directory, add the missing typo3/ prefix
         if (is_file(realpath(PATH_site . TYPO3_mainDir . $filename))) {
@@ -433,7 +458,7 @@ class ResourceCompressor
         } elseif (strpos($filename, '../') === 0) {
             $file = GeneralUtility::resolveBackPath(PATH_typo3 . $filename);
         } else {
-            $file = PATH_site . ltrim($filename, '/');
+            $file = PATH_site . $fileNameWithoutSlash;
         }
 
         // check if the file exists, and if so, return the path relative to TYPO3_mainDir