[BUGFIX] Allow comma in config.linkVars values 78/53478/4
authorSascha Egerer <sascha@sascha-egerer.de>
Fri, 14 Jul 2017 15:56:48 +0000 (17:56 +0200)
committerGeorg Ringer <georg.ringer@gmail.com>
Sat, 15 Jul 2017 08:13:20 +0000 (10:13 +0200)
The config.linkVars TypoScript setting is splitted by comma.
This will fail if the optional value, wrapped by braces,
does contain a comma.
The string must only be splitted by commas that are not inside
of braces.

Resolves: #81810
Releases: master,8.7
Change-Id: If11615b0f4ed9ca55aea4ac3baf79284b35da398
Reviewed-on: https://review.typo3.org/53478
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Joerg Boesche <typo3@joergboesche.de>
Reviewed-by: Wolfgang Klinger <wolfgang@wazum.com>
Reviewed-by: Frank Naegler <frank.naegler@typo3.org>
Tested-by: Frank Naegler <frank.naegler@typo3.org>
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
typo3/sysext/frontend/Classes/Controller/TypoScriptFrontendController.php
typo3/sysext/frontend/Tests/Unit/Controller/TypoScriptFrontendControllerTest.php

index 64871db..92e6d7a 100644 (file)
@@ -2783,7 +2783,12 @@ class TypoScriptFrontendController
     public function calculateLinkVars()
     {
         $this->linkVars = '';
     public function calculateLinkVars()
     {
         $this->linkVars = '';
-        $linkVars = GeneralUtility::trimExplode(',', (string)$this->config['config']['linkVars']);
+        if (empty($this->config['config']['linkVars'])) {
+            return;
+        }
+
+        $linkVars = $this->splitLinkVarsString((string)$this->config['config']['linkVars']);
+
         if (empty($linkVars)) {
             return;
         }
         if (empty($linkVars)) {
             return;
         }
@@ -2816,6 +2821,28 @@ class TypoScriptFrontendController
     }
 
     /**
     }
 
     /**
+     * Split the link vars string by "," but not if the "," is inside of braces
+     *
+     * @param $string
+     *
+     * @return array
+     */
+    protected function splitLinkVarsString(string $string): array
+    {
+        $tempCommaReplacementString = '###KASPER###';
+
+        // replace every "," wrapped in "()" by a "unique" string
+        $string = preg_replace_callback('/\((?>[^()]|(?R))*\)/', function ($result) use ($tempCommaReplacementString) {
+            return str_replace(',', $tempCommaReplacementString, $result[0]);
+        }, $string);
+
+        $string = GeneralUtility::trimExplode(',', $string);
+
+        // replace all "unique" strings back to ","
+        return str_replace($tempCommaReplacementString, ',', $string);
+    }
+
+    /**
      * Redirect to target page if the current page is an overlaid mountpoint.
      *
      * If the current page is of type mountpoint and should be overlaid with the contents of the mountpoint page
      * Redirect to target page if the current page is an overlaid mountpoint.
      *
      * If the current page is of type mountpoint and should be overlaid with the contents of the mountpoint page
index 9c5b356..3c006eb 100644 (file)
@@ -287,4 +287,123 @@ class TypoScriptFrontendControllerTest extends \TYPO3\TestingFramework\Core\Unit
         $this->subject->preparePageContentGeneration();
         $this->assertEquals($this->subject->sWordRegEx, $expectedRegex);
     }
         $this->subject->preparePageContentGeneration();
         $this->assertEquals($this->subject->sWordRegEx, $expectedRegex);
     }
+
+    /**
+     * @test
+     * @dataProvider splitLinkVarsDataProvider
+     *
+     * @param $string
+     * @param $expected
+     */
+    public function splitLinkVarsStringSplitsStringByComma($string, $expected)
+    {
+        $this->assertEquals($expected, $this->subject->_callRef('splitLinkVarsString', $string));
+    }
+
+    /**
+     * @return array
+     */
+    public function splitLinkVarsDataProvider()
+    {
+        return [
+            [
+                'L',
+                ['L']
+            ],
+            [
+                'L,a',
+                [
+                    'L',
+                    'a'
+                ]
+            ],
+            [
+                'L, a',
+                [
+                    'L',
+                    'a'
+                ]
+            ],
+            [
+                'L , a',
+                [
+                    'L',
+                    'a'
+                ]
+            ],
+            [
+                ' L, a ',
+                [
+                    'L',
+                    'a'
+                ]
+            ],
+            [
+                'L(1)',
+                [
+                    'L(1)'
+                ]
+            ],
+            [
+                'L(1),a',
+                [
+                    'L(1)',
+                    'a'
+                ]
+            ],
+            [
+                'L(1) ,  a',
+                [
+                    'L(1)',
+                    'a'
+                ]
+            ],
+            [
+                'a,L(1)',
+                [
+                    'a',
+                    'L(1)'
+                ]
+            ],
+            [
+                'L(1),a(2-3)',
+                [
+                    'L(1)',
+                    'a(2-3)'
+                ]
+            ],
+            [
+                'L(1),a((2-3))',
+                [
+                    'L(1)',
+                    'a((2-3))'
+                ]
+            ],
+            [
+                'L(1),a(a{2,4})',
+                [
+                    'L(1)',
+                    'a(a{2,4})'
+                ]
+            ],
+            [
+                'L(1),a(/a{2,4}\,()/)',
+                [
+                    'L(1)',
+                    'a(/a{2,4}\,()/)'
+                ]
+            ],
+            [
+                'L,a , b(c) , dd(/g{1,2}/), eee(, ()f) , 2',
+                [
+                    'L',
+                    'a',
+                    'b(c)',
+                    'dd(/g{1,2}/)',
+                    'eee(, ()f)',
+                    '2'
+                ]
+            ]
+        ];
+    }
 }
 }