[BUG] Too much recursion kills apache 09/18409/2
authorBastian Bringenberg <mail@bastian-bringenberg.de>
Wed, 20 Feb 2013 22:21:40 +0000 (23:21 +0100)
committerBastian Bringenberg <mail@bastian-bringenberg.de>
Wed, 20 Feb 2013 22:25:26 +0000 (23:25 +0100)
Replacing RegEx by explode function

Change-Id: Ic15289f84b25a4753af44fc87686b694eea47ed8
Fixes: #45687
Reviewed-on: https://review.typo3.org/18409
Reviewed-by: Bastian Bringenberg
Tested-by: Bastian Bringenberg
Classes/Helper/RenderHelper.php

index f127762..0592582 100644 (file)
                 */
                private function _renderContentList($text) {
                        if(strpos($text, '{TOC}') === FALSE) return $text;
-                       $stageList = $this->_getContentListStage($text, 1);
-                       $tocView = $this->createViewHelper('TableOfContents');
-                       $tocView->assign('stageList', $stageList);
-                       return str_replace('{TOC}', $tocView->render(), $text);
+                       $return = $this->_getCurrentLevel($text, 1);
+                       return str_replace('{TOC}', $return, $text);
                }
 
                /**
-                * HelperMethod for Rendering ContentList
+                * Helper Method for _renderContentList doing the magic ( recursivly )
                 *
-                * @param string $text
+                * @param string $string
                 * @param int $stage
-                * @return array
+                * @return string
                 */
-               private function _getContentListStage($text, $stage){
-                       $returnArray = array();
-                       if($stage == $this->maxStage) return $returnArray;
-                       preg_match_all('/<h'.$stage.'>(.*?)<\/h'.$stage.'>.*?((?:(?!<h'.$stage.'>).)*)/s', $text, $tmp);
-                       for($i = 0; $i < count($tmp[1]); $i++){
-                               $returnArray[$tmp[1][$i]] = $this->_getContentListStage($tmp[2][$i], $stage+1);
+               private function _getCurrentLevel($string, $stage){
+                       $array = explode('<h'.$stage.'>', $string);
+                       $return = '';
+                       foreach($array as $i => $arrayField) {
+                               if($i == 0) continue;
+                               $internal_array = explode('</h'.$stage.'>', $arrayField);
+                               $return .= '<li>'.$internal_array[0];
+                               if($stage != 6) $return .= $this->_getCurrentLevel($internal_array[1], $stage+1);
+                               $return .='</li>';
                        }
-                       return $returnArray;
+                       if($return != '') $return = '<ul>'.$return.'</ul>';
+                       return $return;
                }
 
                /**