[BUGFIX] Show warning message on regex error in Object Browser 00/44500/8
authorHelmut Hummel <helmut.hummel@typo3.org>
Tue, 3 Nov 2015 10:58:33 +0000 (11:58 +0100)
committerStefan Neufeind <typo3.neufeind@speedpartner.de>
Tue, 3 Nov 2015 21:31:36 +0000 (22:31 +0100)
We test the pattern with a shutdown operator, before we're going to use it
in the regular expression search.

With doing so, we can output an error message when the regex is wrong.

Resolves: #71271
Releases: master
Change-Id: I0244c9727b838b788bfdc8ad457f90778cc79be7
Reviewed-on: https://review.typo3.org/44500
Reviewed-by: Daniel Goerz <ervaude@gmail.com>
Tested-by: Daniel Goerz <ervaude@gmail.com>
Reviewed-by: Stefan Neufeind <typo3.neufeind@speedpartner.de>
Tested-by: Stefan Neufeind <typo3.neufeind@speedpartner.de>
typo3/sysext/core/Classes/TypoScript/ExtendedTemplateService.php
typo3/sysext/tstemplate/Classes/Controller/TypoScriptTemplateObjectBrowserModuleFunctionController.php
typo3/sysext/tstemplate/Resources/Private/Language/locallang_objbrowser.xlf

index 9ea5bc8..8770bc3 100644 (file)
@@ -16,6 +16,7 @@ namespace TYPO3\CMS\Core\TypoScript;
 
 use TYPO3\CMS\Backend\Template\DocumentTemplate;
 use TYPO3\CMS\Backend\Utility\BackendUtility;
+use TYPO3\CMS\Core\Exception;
 use TYPO3\CMS\Core\Imaging\Icon;
 use TYPO3\CMS\Core\Imaging\IconFactory;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
@@ -585,6 +586,7 @@ class ExtendedTemplateService extends TemplateService
      * @param string $searchString
      * @param array $keyArray
      * @return array
+     * @throws Exception
      */
     public function ext_getSearchKeys($arr, $depth_in, $searchString, $keyArray)
     {
@@ -598,20 +600,28 @@ class ExtendedTemplateService extends TemplateService
         if ($depth_in) {
             $depth_in = $depth_in . '.';
         }
+        $searchPattern = '';
+        if ($this->regexMode) {
+            $searchPattern = '/' . addcslashes($searchString, '/') . '/';
+            $matchResult = @preg_match($searchPattern, '');
+            if ($matchResult === false) {
+                throw new Exception(sprintf('Error evaluating regular expression "%s".', $searchPattern), 1446559458);
+            }
+        }
         foreach ($keyArr as $key => $value) {
             $depth = $depth_in . $key;
             $deeper = is_array($arr[$key . '.']);
             if ($this->regexMode) {
                 // The value has matched
-                if (preg_match('/' . $searchString . '/', $arr[$key])) {
+                if (preg_match($searchPattern, $arr[$key])) {
                     $this->tsbrowser_searchKeys[$depth] += 2;
                 }
                 // The key has matched
-                if (preg_match('/' . $searchString . '/', $key)) {
+                if (preg_match($searchPattern, $key)) {
                     $this->tsbrowser_searchKeys[$depth] += 4;
                 }
                 // Just open this subtree if the parent key has matched the search
-                if (preg_match('/' . $searchString . '/', $depth_in)) {
+                if (preg_match($searchPattern, $depth_in)) {
                     $this->tsbrowser_searchKeys[$depth] = 1;
                 }
             } else {
index f193bae..aeefd51 100644 (file)
@@ -17,6 +17,7 @@ namespace TYPO3\CMS\Tstemplate\Controller;
 use TYPO3\CMS\Backend\Module\AbstractFunctionModule;
 use TYPO3\CMS\Backend\Utility\BackendUtility;
 use TYPO3\CMS\Core\DataHandling\DataHandler;
+use TYPO3\CMS\Core\Exception;
 use TYPO3\CMS\Core\Imaging\Icon;
 use TYPO3\CMS\Core\Imaging\IconFactory;
 use TYPO3\CMS\Core\Messaging\FlashMessage;
@@ -408,7 +409,20 @@ class TypoScriptTemplateObjectBrowserModuleFunctionController extends AbstractFu
             $templateService->tsbrowser_depthKeys = $this->pObj->MOD_SETTINGS['tsbrowser_depthKeys_' . $bType];
             if (GeneralUtility::_POST('search') && GeneralUtility::_POST('search_field')) {
                 // If any POST-vars are send, update the condition array
-                $templateService->tsbrowser_depthKeys = $templateService->ext_getSearchKeys($theSetup, '', GeneralUtility::_POST('search_field'), array());
+                $searchString = GeneralUtility::_POST('search_field');
+                try {
+                    $templateService->tsbrowser_depthKeys =
+                        $templateService->ext_getSearchKeys(
+                            $theSetup,
+                            '',
+                            $searchString,
+                            array()
+                        );
+                } catch (Exception $e) {
+                    $this->addFlashMessage(
+                        GeneralUtility::makeInstance(FlashMessage::class, sprintf($lang->getLL('error.' . $e->getCode()), $searchString), '', FlashMessage::ERROR)
+                    );
+                }
             }
             $theOutput .= '
                                <div class="tsob-menu">
index 7c38a10..f33240a 100644 (file)
                        <trans-unit id="severity.3">
                                <source>Error</source>
                        </trans-unit>
+                       <trans-unit id="error.1446559458">
+                               <source>Error evaluating regular expression "%s".</source>
+                       </trans-unit>
                </body>
        </file>
 </xliff>