| @@ -25,18 +25,18 @@ | ||
| 25 | 25 | class RemoveBlurScript implements ManipulationInterface | 
| 26 | 26 |  { | 
| 27 | 27 | |
| 28 | - /** | |
| 29 | - * @param string $html The original HTML | |
| 30 | - * @param array $configuration Configuration | |
| 31 | - * | |
| 32 | - * @return string the manipulated HTML | |
| 33 | - */ | |
| 34 | - public function manipulate($html, array $configuration = []) | |
| 35 | -    { | |
| 36 | -        if (strlen($html) < 100000) { | |
| 37 | - $pattern = '/<script (type="text\/javascript"|language="javascript")>.+?Konqueror.+function blurLink.+theObject.blur.+?<\/script>/is'; | |
| 38 | - $html = preg_replace($pattern, '', $html); // in head | |
| 39 | - } | |
| 40 | -        return str_replace(' onfocus="blurLink(this);"', '', $html); // in body | |
| 41 | - } | |
| 28 | + /** | |
| 29 | + * @param string $html The original HTML | |
| 30 | + * @param array $configuration Configuration | |
| 31 | + * | |
| 32 | + * @return string the manipulated HTML | |
| 33 | + */ | |
| 34 | + public function manipulate($html, array $configuration = []) | |
| 35 | +	{ | |
| 36 | +		if (strlen($html) < 100000) { | |
| 37 | + $pattern = '/<script (type="text\/javascript"|language="javascript")>.+?Konqueror.+function blurLink.+theObject.blur.+?<\/script>/is'; | |
| 38 | + $html = preg_replace($pattern, '', $html); // in head | |
| 39 | + } | |
| 40 | +		return str_replace(' onfocus="blurLink(this);"', '', $html); // in body | |
| 41 | + } | |
| 42 | 42 | } | 
| @@ -13,11 +13,11 @@ | ||
| 13 | 13 | interface ManipulationInterface | 
| 14 | 14 |  { | 
| 15 | 15 | |
| 16 | - /** | |
| 17 | - * @param string $html The original HTML | |
| 18 | - * @param array $configuration Configuration | |
| 19 | - * | |
| 20 | - * @return string the manipulated HTML | |
| 21 | - */ | |
| 22 | - public function manipulate($html, array $configuration = []); | |
| 16 | + /** | |
| 17 | + * @param string $html The original HTML | |
| 18 | + * @param array $configuration Configuration | |
| 19 | + * | |
| 20 | + * @return string the manipulated HTML | |
| 21 | + */ | |
| 22 | + public function manipulate($html, array $configuration = []); | |
| 23 | 23 | } | 
| @@ -13,65 +13,65 @@ | ||
| 13 | 13 | class RemoveComments implements ManipulationInterface | 
| 14 | 14 |  { | 
| 15 | 15 | |
| 16 | - /** | |
| 17 | - * Patterns for white-listing comments inside content | |
| 18 | - * | |
| 19 | - * @var array | |
| 20 | - */ | |
| 21 | - protected $whiteListCommentsPatterns = []; | |
| 16 | + /** | |
| 17 | + * Patterns for white-listing comments inside content | |
| 18 | + * | |
| 19 | + * @var array | |
| 20 | + */ | |
| 21 | + protected $whiteListCommentsPatterns = []; | |
| 22 | 22 | |
| 23 | - /** | |
| 24 | - * @param string $html The original HTML | |
| 25 | - * @param array $configuration Configuration | |
| 26 | - * | |
| 27 | - * @return string the manipulated HTML | |
| 28 | - */ | |
| 29 | - public function manipulate($html, array $configuration = []) | |
| 30 | -    { | |
| 31 | -        if (isset($configuration['keep.'])) { | |
| 32 | - $this->whiteListCommentsPatterns = $configuration['keep.']; | |
| 33 | - } | |
| 23 | + /** | |
| 24 | + * @param string $html The original HTML | |
| 25 | + * @param array $configuration Configuration | |
| 26 | + * | |
| 27 | + * @return string the manipulated HTML | |
| 28 | + */ | |
| 29 | + public function manipulate($html, array $configuration = []) | |
| 30 | +	{ | |
| 31 | +		if (isset($configuration['keep.'])) { | |
| 32 | + $this->whiteListCommentsPatterns = $configuration['keep.']; | |
| 33 | + } | |
| 34 | 34 | |
| 35 | - // match all styles, scripts and comments | |
| 36 | - $matches = []; | |
| 37 | - preg_match_all( | |
| 38 | - '/(?s)((<!--.*?-->)|(<[ \n\r]*style[^>]*>.*?<[ \n\r]*\/style[^>]*>)|(<[ \n\r]*script[^>]*>.*?<[ \n\r]*\/script[^>]*>))/im', | |
| 39 | - $html, | |
| 40 | - $matches | |
| 41 | - ); | |
| 42 | -        foreach ($matches[0] as $tag) { | |
| 43 | -            if ($this->keepComment($tag) === false) { | |
| 44 | - $html = str_replace($tag, '', $html); | |
| 45 | - } | |
| 46 | - } | |
| 47 | - return $html; | |
| 48 | - } | |
| 35 | + // match all styles, scripts and comments | |
| 36 | + $matches = []; | |
| 37 | + preg_match_all( | |
| 38 | + '/(?s)((<!--.*?-->)|(<[ \n\r]*style[^>]*>.*?<[ \n\r]*\/style[^>]*>)|(<[ \n\r]*script[^>]*>.*?<[ \n\r]*\/script[^>]*>))/im', | |
| 39 | + $html, | |
| 40 | + $matches | |
| 41 | + ); | |
| 42 | +		foreach ($matches[0] as $tag) { | |
| 43 | +			if ($this->keepComment($tag) === false) { | |
| 44 | + $html = str_replace($tag, '', $html); | |
| 45 | + } | |
| 46 | + } | |
| 47 | + return $html; | |
| 48 | + } | |
| 49 | 49 | |
| 50 | - /** | |
| 51 | - * Check if a comment is defined to be kept in a pattern whiteListOfComments | |
| 52 | - * | |
| 53 | - * @param string $commentHtml | |
| 54 | - * | |
| 55 | - * @return boolean | |
| 56 | - */ | |
| 57 | - protected function keepComment($commentHtml) | |
| 58 | -    { | |
| 59 | - // if not even a comment, skip this | |
| 60 | -        if (!preg_match('/^\<\!\-\-(.*?)\-\-\>$/usi', $commentHtml)) { | |
| 61 | - return true; | |
| 62 | - } | |
| 50 | + /** | |
| 51 | + * Check if a comment is defined to be kept in a pattern whiteListOfComments | |
| 52 | + * | |
| 53 | + * @param string $commentHtml | |
| 54 | + * | |
| 55 | + * @return boolean | |
| 56 | + */ | |
| 57 | + protected function keepComment($commentHtml) | |
| 58 | +	{ | |
| 59 | + // if not even a comment, skip this | |
| 60 | +		if (!preg_match('/^\<\!\-\-(.*?)\-\-\>$/usi', $commentHtml)) { | |
| 61 | + return true; | |
| 62 | + } | |
| 63 | 63 | |
| 64 | - // if not defined in white list | |
| 65 | -        if (!empty($this->whiteListCommentsPatterns)) { | |
| 66 | -            $commentHtml = str_replace("<!--", "", $commentHtml); | |
| 67 | -            $commentHtml = str_replace("-->", "", $commentHtml); | |
| 68 | - $commentHtml = trim($commentHtml); | |
| 69 | -            foreach ($this->whiteListCommentsPatterns as $pattern) { | |
| 70 | -                if (!empty($pattern) && preg_match($pattern, $commentHtml)) { | |
| 71 | - return true; | |
| 72 | - } | |
| 73 | - } | |
| 74 | - } | |
| 75 | - return false; | |
| 76 | - } | |
| 64 | + // if not defined in white list | |
| 65 | +		if (!empty($this->whiteListCommentsPatterns)) { | |
| 66 | +			$commentHtml = str_replace("<!--", "", $commentHtml); | |
| 67 | +			$commentHtml = str_replace("-->", "", $commentHtml); | |
| 68 | + $commentHtml = trim($commentHtml); | |
| 69 | +			foreach ($this->whiteListCommentsPatterns as $pattern) { | |
| 70 | +				if (!empty($pattern) && preg_match($pattern, $commentHtml)) { | |
| 71 | + return true; | |
| 72 | + } | |
| 73 | + } | |
| 74 | + } | |
| 75 | + return false; | |
| 76 | + } | |
| 77 | 77 | } | 
| @@ -13,15 +13,15 @@ | ||
| 13 | 13 | class RemoveGenerator implements ManipulationInterface | 
| 14 | 14 |  { | 
| 15 | 15 | |
| 16 | - /** | |
| 17 | - * @param string $html The original HTML | |
| 18 | - * @param array $configuration Configuration | |
| 19 | - * | |
| 20 | - * @return string the manipulated HTML | |
| 21 | - */ | |
| 22 | - public function manipulate($html, array $configuration = []) | |
| 23 | -    { | |
| 24 | - $regex = '<meta name=["\']?generator["\']? [^>]+>'; | |
| 25 | -        return preg_replace('/' . $regex . '/is', '', $html); | |
| 26 | - } | |
| 16 | + /** | |
| 17 | + * @param string $html The original HTML | |
| 18 | + * @param array $configuration Configuration | |
| 19 | + * | |
| 20 | + * @return string the manipulated HTML | |
| 21 | + */ | |
| 22 | + public function manipulate($html, array $configuration = []) | |
| 23 | +	{ | |
| 24 | + $regex = '<meta name=["\']?generator["\']? [^>]+>'; | |
| 25 | +		return preg_replace('/' . $regex . '/is', '', $html); | |
| 26 | + } | |
| 27 | 27 | } | 
| @@ -22,6 +22,6 @@ | ||
| 22 | 22 | public function manipulate($html, array $configuration = []) | 
| 23 | 23 |      { | 
| 24 | 24 | $regex = '<meta name=["\']?generator["\']? [^>]+>'; | 
| 25 | -        return preg_replace('/' . $regex . '/is', '', $html); | |
| 25 | +        return preg_replace('/'.$regex.'/is', '', $html); | |
| 26 | 26 | } | 
| 27 | 27 | } | 
| @@ -76,7 +76,7 @@ discard block | ||
| 76 | 76 | |
| 77 | 77 |          if (!empty($config)) { | 
| 78 | 78 |              if ($config['formatHtml'] && is_numeric($config['formatHtml'])) { | 
| 79 | - $this->formatType = (int)$config['formatHtml']; | |
| 79 | + $this->formatType = (int) $config['formatHtml']; | |
| 80 | 80 | } | 
| 81 | 81 | |
| 82 | 82 |              if ($config['formatHtml.']['tabSize'] && is_numeric($config['formatHtml.']['tabSize'])) { | 
| @@ -84,14 +84,14 @@ discard block | ||
| 84 | 84 | } | 
| 85 | 85 | |
| 86 | 86 |              if (isset($config['formatHtml.']['debugComment'])) { | 
| 87 | - $this->debugComment = (bool)$config['formatHtml.']['debugComment']; | |
| 87 | + $this->debugComment = (bool) $config['formatHtml.']['debugComment']; | |
| 88 | 88 | } | 
| 89 | 89 | |
| 90 | 90 |              if (isset($config['headerComment'])) { | 
| 91 | 91 | $this->headerComment = $config['headerComment']; | 
| 92 | 92 | } | 
| 93 | 93 | |
| 94 | -            if (isset($config['dropEmptySpaceChar']) && (bool)$config['dropEmptySpaceChar']) { | |
| 94 | +            if (isset($config['dropEmptySpaceChar']) && (bool) $config['dropEmptySpaceChar']) { | |
| 95 | 95 | $this->emptySpaceChar = ''; | 
| 96 | 96 | } | 
| 97 | 97 | } | 
| @@ -108,7 +108,7 @@ discard block | ||
| 108 | 108 | public function clean($html, $config = []) | 
| 109 | 109 |      { | 
| 110 | 110 |          if (!empty($config)) { | 
| 111 | -            if ((bool)$config['enabled'] === false) { | |
| 111 | +            if ((bool) $config['enabled'] === false) { | |
| 112 | 112 | return $html; | 
| 113 | 113 | } | 
| 114 | 114 | |
| @@ -117,15 +117,15 @@ discard block | ||
| 117 | 117 | |
| 118 | 118 | $manipulations = []; | 
| 119 | 119 | |
| 120 | -        if (isset($config['removeGenerator']) && (bool)$config['removeGenerator']) { | |
| 120 | +        if (isset($config['removeGenerator']) && (bool) $config['removeGenerator']) { | |
| 121 | 121 | $manipulations['removeGenerator'] = GeneralUtility::makeInstance(RemoveGenerator::class); | 
| 122 | 122 | } | 
| 123 | 123 | |
| 124 | -        if (isset($config['removeComments']) && (bool)$config['removeComments']) { | |
| 124 | +        if (isset($config['removeComments']) && (bool) $config['removeComments']) { | |
| 125 | 125 | $manipulations['removeComments'] = GeneralUtility::makeInstance(RemoveComments::class); | 
| 126 | 126 | } | 
| 127 | 127 | |
| 128 | -        if (isset($config['removeBlurScript']) && (bool)$config['removeBlurScript']) { | |
| 128 | +        if (isset($config['removeBlurScript']) && (bool) $config['removeBlurScript']) { | |
| 129 | 129 | $manipulations['removeBlurScript'] = GeneralUtility::makeInstance(RemoveBlurScript::class); | 
| 130 | 130 | } | 
| 131 | 131 | |
| @@ -135,7 +135,7 @@ discard block | ||
| 135 | 135 | |
| 136 | 136 |          foreach ($manipulations as $key => $manipulation) { | 
| 137 | 137 | /** @var ManipulationInterface $manipulation */ | 
| 138 | - $configuration = isset($config[$key . '.']) && is_array($config[$key . '.']) ? $config[$key . '.'] : []; | |
| 138 | + $configuration = isset($config[$key.'.']) && is_array($config[$key.'.']) ? $config[$key.'.'] : []; | |
| 139 | 139 | $html = $manipulation->manipulate($html, $configuration); | 
| 140 | 140 | } | 
| 141 | 141 | |
| @@ -181,7 +181,7 @@ discard block | ||
| 181 | 181 | $functionalBoxElements = 'dd|dt|frameset|li|tbody|td|tfoot|th|thead|tr|colgroup'; | 
| 182 | 182 | $usableBoxElements = 'applet|button|del|iframe|ins|map|object|script'; | 
| 183 | 183 | $imagineBoxElements = 'html|body|head|meta|title|link|script|base|!--'; | 
| 184 | - $allBoxLikeElements = '(?>' . $trueBoxElements . '|' . $functionalBoxElements . '|' . $usableBoxElements . '|' . $imagineBoxElements . ')'; | |
| 184 | + $allBoxLikeElements = '(?>'.$trueBoxElements.'|'.$functionalBoxElements.'|'.$usableBoxElements.'|'.$imagineBoxElements.')'; | |
| 185 | 185 | $esteticBoxLikeElements = '(?>html|head|body|meta name|title|div|table|h1|h2|h3|h4|h5|h6|p|form|pre|center|!--)'; | 
| 186 | 186 | $structureBoxLikeElements = '(?>html|head|body|div|!--)'; | 
| 187 | 187 | |
| @@ -190,7 +190,7 @@ discard block | ||
| 190 | 190 | '/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/', | 
| 191 | 191 | $html, | 
| 192 | 192 | -1, | 
| 193 | - PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY | |
| 193 | + PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY | |
| 194 | 194 | ); | 
| 195 | 195 | // remove empty lines | 
| 196 | 196 | $htmlArray = ['']; | 
| @@ -217,52 +217,52 @@ discard block | ||
| 217 | 217 | } elseif ($this->formatType == 2 && ( // minimalistic line break | 
| 218 | 218 | # this element has a line break before itself | 
| 219 | 219 | preg_match( | 
| 220 | - '/<' . $structureBoxLikeElements . '(.*)>/Usi', | |
| 220 | + '/<'.$structureBoxLikeElements.'(.*)>/Usi', | |
| 221 | 221 | $htmlArray[$x] | 
| 222 | 222 | ) || preg_match( | 
| 223 | - '/<' . $structureBoxLikeElements . '(.*) \/>/Usi', | |
| 223 | + '/<'.$structureBoxLikeElements.'(.*) \/>/Usi', | |
| 224 | 224 | $htmlArray[$x] | 
| 225 | 225 | ) || # one element before is a element that has a line break after | 
| 226 | 226 | preg_match( | 
| 227 | - '/<\/' . $structureBoxLikeElements . '(.*)>/Usi', | |
| 227 | + '/<\/'.$structureBoxLikeElements.'(.*)>/Usi', | |
| 228 | 228 | $htmlArray[$x - 1] | 
| 229 | 229 | ) || substr( | 
| 230 | 230 | $htmlArray[$x - 1], | 
| 231 | 231 | 0, | 
| 232 | 232 | 4 | 
| 233 | -                    ) == '<!--' || preg_match('/<' . $structureBoxLikeElements . '(.*) \/>/Usi', $htmlArray[$x - 1])) | |
| 233 | +                    ) == '<!--' || preg_match('/<'.$structureBoxLikeElements.'(.*) \/>/Usi', $htmlArray[$x - 1])) | |
| 234 | 234 |              ) { | 
| 235 | 235 | $newline = true; | 
| 236 | 236 | } elseif ($this->formatType == 3 && ( // aestetic line break | 
| 237 | 237 | # this element has a line break before itself | 
| 238 | 238 | preg_match( | 
| 239 | - '/<' . $esteticBoxLikeElements . '(.*)>/Usi', | |
| 239 | + '/<'.$esteticBoxLikeElements.'(.*)>/Usi', | |
| 240 | 240 | $htmlArray[$x] | 
| 241 | 241 | ) || preg_match( | 
| 242 | - '/<' . $esteticBoxLikeElements . '(.*) \/>/Usi', | |
| 242 | + '/<'.$esteticBoxLikeElements.'(.*) \/>/Usi', | |
| 243 | 243 | $htmlArray[$x] | 
| 244 | 244 | ) || # one element before is a element that has a line break after | 
| 245 | -                    preg_match('/<\/' . $esteticBoxLikeElements . '(.*)>/Usi', $htmlArray[$x - 1]) || substr( | |
| 245 | +                    preg_match('/<\/'.$esteticBoxLikeElements.'(.*)>/Usi', $htmlArray[$x - 1]) || substr( | |
| 246 | 246 | $htmlArray[$x - 1], | 
| 247 | 247 | 0, | 
| 248 | 248 | 4 | 
| 249 | -                    ) == '<!--' || preg_match('/<' . $esteticBoxLikeElements . '(.*) \/>/Usi', $htmlArray[$x - 1])) | |
| 249 | +                    ) == '<!--' || preg_match('/<'.$esteticBoxLikeElements.'(.*) \/>/Usi', $htmlArray[$x - 1])) | |
| 250 | 250 |              ) { | 
| 251 | 251 | $newline = true; | 
| 252 | 252 | } elseif ($this->formatType >= 4 && ( // logical line break | 
| 253 | 253 | # this element has a line break before itself | 
| 254 | 254 | preg_match( | 
| 255 | - '/<' . $allBoxLikeElements . '(.*)>/Usi', | |
| 255 | + '/<'.$allBoxLikeElements.'(.*)>/Usi', | |
| 256 | 256 | $htmlArray[$x] | 
| 257 | 257 | ) || preg_match( | 
| 258 | - '/<' . $allBoxLikeElements . '(.*) \/>/Usi', | |
| 258 | + '/<'.$allBoxLikeElements.'(.*) \/>/Usi', | |
| 259 | 259 | $htmlArray[$x] | 
| 260 | 260 | ) || # one element before is a element that has a line break after | 
| 261 | -                    preg_match('/<\/' . $allBoxLikeElements . '(.*)>/Usi', $htmlArray[$x - 1]) || substr( | |
| 261 | +                    preg_match('/<\/'.$allBoxLikeElements.'(.*)>/Usi', $htmlArray[$x - 1]) || substr( | |
| 262 | 262 | $htmlArray[$x - 1], | 
| 263 | 263 | 0, | 
| 264 | 264 | 4 | 
| 265 | -                    ) == '<!--' || preg_match('/<' . $allBoxLikeElements . '(.*) \/>/Usi', $htmlArray[$x - 1])) | |
| 265 | +                    ) == '<!--' || preg_match('/<'.$allBoxLikeElements.'(.*) \/>/Usi', $htmlArray[$x - 1])) | |
| 266 | 266 |              ) { | 
| 267 | 267 | $newline = true; | 
| 268 | 268 | } | 
| @@ -339,7 +339,7 @@ discard block | ||
| 339 | 339 | |
| 340 | 340 | // include debug comment at the end | 
| 341 | 341 |          if ($tabs != 0 && $this->debugComment === true) { | 
| 342 | - $html .= '<!--' . $tabs . " open elements found-->\r\n"; | |
| 342 | + $html .= '<!--'.$tabs." open elements found-->\r\n"; | |
| 343 | 343 | } | 
| 344 | 344 | |
| 345 | 345 | return $html; | 
| @@ -470,7 +470,7 @@ discard block | ||
| 470 | 470 | 'textarea', | 
| 471 | 471 | 'pre' | 
| 472 | 472 | ]; // eventuell auch: span, script, style | 
| 473 | -        $peaces = preg_split('#(<(' . implode('|', $splitArray) . ').*>.*</\2>)#Uis', $html, -1, PREG_SPLIT_DELIM_CAPTURE); | |
| 473 | +        $peaces = preg_split('#(<('.implode('|', $splitArray).').*>.*</\2>)#Uis', $html, -1, PREG_SPLIT_DELIM_CAPTURE); | |
| 474 | 474 | $html = ""; | 
| 475 | 475 |          for ($i = 0; $i < count($peaces); $i++) { | 
| 476 | 476 |              if (($i + 1) % 3 == 0) { | 
| @@ -524,8 +524,8 @@ discard block | ||
| 524 | 524 | public function includeHeaderComment(&$html) | 
| 525 | 525 |      { | 
| 526 | 526 |          if (!empty($this->headerComment)) { | 
| 527 | -            $html = preg_replace_callback('/<meta http-equiv(.*)>/Usi', function ($matches) { | |
| 528 | - return trim($matches[0] . $this->newline . $this->tab . $this->tab . '<!-- ' . $this->headerComment . '-->'); | |
| 527 | +            $html = preg_replace_callback('/<meta http-equiv(.*)>/Usi', function($matches) { | |
| 528 | + return trim($matches[0].$this->newline.$this->tab.$this->tab.'<!-- '.$this->headerComment.'-->'); | |
| 529 | 529 | }, $html, 1); | 
| 530 | 530 | } | 
| 531 | 531 | } | 
| @@ -17,508 +17,508 @@ | ||
| 17 | 17 | class CleanHtmlService implements SingletonInterface | 
| 18 | 18 |  { | 
| 19 | 19 | |
| 20 | - /** | |
| 21 | - * Enable Debug comment in footer | |
| 22 | - * | |
| 23 | - * @var boolean | |
| 24 | - */ | |
| 25 | - protected $debugComment = false; | |
| 26 | - | |
| 27 | - /** | |
| 28 | - * Format Type | |
| 29 | - * | |
| 30 | - * @var integer | |
| 31 | - */ | |
| 32 | - protected $formatType = 0; | |
| 33 | - | |
| 34 | - /** | |
| 35 | - * Tab character | |
| 36 | - * | |
| 37 | - * @var string | |
| 38 | - */ | |
| 39 | - protected $tab = "\t"; | |
| 40 | - | |
| 41 | - /** | |
| 42 | - * Newline character | |
| 43 | - * | |
| 44 | - * @var string | |
| 45 | - */ | |
| 46 | - protected $newline = "\n"; | |
| 47 | - | |
| 48 | - /** | |
| 49 | - * Configured extra header comment | |
| 50 | - * | |
| 51 | - * @var string | |
| 52 | - */ | |
| 53 | - protected $headerComment = ''; | |
| 54 | - | |
| 55 | - /** | |
| 56 | - * Empty space char | |
| 57 | - * @var string | |
| 58 | - */ | |
| 59 | - protected $emptySpaceChar = ' '; | |
| 60 | - | |
| 61 | - /** | |
| 62 | - * Set variables based on given config | |
| 63 | - * | |
| 64 | - * @param array $config | |
| 65 | - * | |
| 66 | - * @return void | |
| 67 | - */ | |
| 68 | - public function setVariables(array $config) | |
| 69 | -    { | |
| 70 | - // Set newline based on OS | |
| 71 | -        if (Environment::isWindows()) { | |
| 72 | - $this->newline = "\r\n"; | |
| 73 | -        } else { | |
| 74 | - $this->newline = "\n"; | |
| 75 | - } | |
| 76 | - | |
| 77 | -        if (!empty($config)) { | |
| 78 | -            if ($config['formatHtml'] && is_numeric($config['formatHtml'])) { | |
| 79 | - $this->formatType = (int)$config['formatHtml']; | |
| 80 | - } | |
| 81 | - | |
| 82 | -            if ($config['formatHtml.']['tabSize'] && is_numeric($config['formatHtml.']['tabSize'])) { | |
| 83 | -                $this->tab = str_pad('', $config['formatHtml.']['tabSize'], ' '); | |
| 84 | - } | |
| 85 | - | |
| 86 | -            if (isset($config['formatHtml.']['debugComment'])) { | |
| 87 | - $this->debugComment = (bool)$config['formatHtml.']['debugComment']; | |
| 88 | - } | |
| 89 | - | |
| 90 | -            if (isset($config['headerComment'])) { | |
| 91 | - $this->headerComment = $config['headerComment']; | |
| 92 | - } | |
| 93 | - | |
| 94 | -            if (isset($config['dropEmptySpaceChar']) && (bool)$config['dropEmptySpaceChar']) { | |
| 95 | - $this->emptySpaceChar = ''; | |
| 96 | - } | |
| 97 | - } | |
| 98 | - } | |
| 99 | - | |
| 100 | - /** | |
| 101 | - * Clean given HTML with formatter | |
| 102 | - * | |
| 103 | - * @param string $html | |
| 104 | - * @param array $config | |
| 105 | - * | |
| 106 | - * @return string | |
| 107 | - */ | |
| 108 | - public function clean($html, $config = []) | |
| 109 | -    { | |
| 110 | -        if (!empty($config)) { | |
| 111 | -            if ((bool)$config['enabled'] === false) { | |
| 112 | - return $html; | |
| 113 | - } | |
| 114 | - | |
| 115 | - $this->setVariables($config); | |
| 116 | - } | |
| 117 | - | |
| 118 | - $manipulations = []; | |
| 119 | - | |
| 120 | -        if (isset($config['removeGenerator']) && (bool)$config['removeGenerator']) { | |
| 121 | - $manipulations['removeGenerator'] = GeneralUtility::makeInstance(RemoveGenerator::class); | |
| 122 | - } | |
| 123 | - | |
| 124 | -        if (isset($config['removeComments']) && (bool)$config['removeComments']) { | |
| 125 | - $manipulations['removeComments'] = GeneralUtility::makeInstance(RemoveComments::class); | |
| 126 | - } | |
| 127 | - | |
| 128 | -        if (isset($config['removeBlurScript']) && (bool)$config['removeBlurScript']) { | |
| 129 | - $manipulations['removeBlurScript'] = GeneralUtility::makeInstance(RemoveBlurScript::class); | |
| 130 | - } | |
| 131 | - | |
| 132 | -        if (!empty($this->headerComment)) { | |
| 133 | - $this->includeHeaderComment($html); | |
| 134 | - } | |
| 135 | - | |
| 136 | -        foreach ($manipulations as $key => $manipulation) { | |
| 137 | - /** @var ManipulationInterface $manipulation */ | |
| 138 | - $configuration = isset($config[$key . '.']) && is_array($config[$key . '.']) ? $config[$key . '.'] : []; | |
| 139 | - $html = $manipulation->manipulate($html, $configuration); | |
| 140 | - } | |
| 141 | - | |
| 142 | -        if ($this->formatType > 0) { | |
| 143 | - $html = $this->formatHtml($html); | |
| 144 | - } | |
| 145 | - | |
| 146 | - return $html; | |
| 147 | - } | |
| 148 | - | |
| 149 | - /** | |
| 150 | - * Formats the (X)HTML code: | |
| 151 | - * - taps according to the hirarchy of the tags | |
| 152 | - * - removes empty spaces between tags | |
| 153 | - * - removes linebreaks within tags (spares where necessary: pre, textarea, comments, ..) | |
| 154 | - * choose from five options: | |
| 155 | - * 0 => off | |
| 156 | - * 1 => no line break at all (code in one line) | |
| 157 | - * 2 => minimalistic line breaks (structure defining box-elements) | |
| 158 | - * 3 => aesthetic line breaks (important box-elements) | |
| 159 | - * 4 => logic line breaks (all box-elements) | |
| 160 | - * 5 => max line breaks (all elements) | |
| 161 | - * | |
| 162 | - * @param string $html | |
| 163 | - * | |
| 164 | - * @return string | |
| 165 | - */ | |
| 166 | - protected function formatHtml($html) | |
| 167 | -    { | |
| 168 | - // Save original formated comments, pre, textarea, styles and java-scripts & replace them with markers | |
| 169 | - preg_match_all( | |
| 170 | - '/(?s)((<!--.*?-->)|(<[ \n\r]*pre[^>]*>.*?<[ \n\r]*\/pre[^>]*>)|(<[ \n\r]*textarea[^>]*>.*?<[ \n\r]*\/textarea[^>]*>)|(<[ \n\r]*style[^>]*>.*?<[ \n\r]*\/style[^>]*>)|(<[ \n\r]*script[^>]*>.*?<[ \n\r]*\/script[^>]*>))/im', | |
| 171 | - $html, | |
| 172 | - $matches | |
| 173 | - ); | |
| 174 | - $noFormat = $matches[0]; // do not format these block elements | |
| 175 | -        for ($i = 0; $i < count($noFormat); $i++) { | |
| 176 | - $html = str_replace($noFormat[$i], "\n<!-- ELEMENT $i -->", $html); | |
| 177 | - } | |
| 178 | - | |
| 179 | - // define box elements for formatting | |
| 180 | - $trueBoxElements = 'address|blockquote|center|dir|div|dl|fieldset|form|h1|h2|h3|h4|h5|h6|hr|isindex|menu|noframes|noscript|ol|p|pre|table|ul|article|aside|details|figcaption|figure|footer|header|hgroup|menu|nav|section'; | |
| 181 | - $functionalBoxElements = 'dd|dt|frameset|li|tbody|td|tfoot|th|thead|tr|colgroup'; | |
| 182 | - $usableBoxElements = 'applet|button|del|iframe|ins|map|object|script'; | |
| 183 | - $imagineBoxElements = 'html|body|head|meta|title|link|script|base|!--'; | |
| 184 | - $allBoxLikeElements = '(?>' . $trueBoxElements . '|' . $functionalBoxElements . '|' . $usableBoxElements . '|' . $imagineBoxElements . ')'; | |
| 185 | - $esteticBoxLikeElements = '(?>html|head|body|meta name|title|div|table|h1|h2|h3|h4|h5|h6|p|form|pre|center|!--)'; | |
| 186 | - $structureBoxLikeElements = '(?>html|head|body|div|!--)'; | |
| 187 | - | |
| 188 | - // split html into it's elements | |
| 189 | - $htmlArrayTemp = preg_split( | |
| 190 | - '/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/', | |
| 191 | - $html, | |
| 192 | - -1, | |
| 193 | - PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY | |
| 194 | - ); | |
| 195 | - // remove empty lines | |
| 196 | - $htmlArray = ['']; | |
| 197 | - $z = 1; | |
| 198 | -        for ($x = 0; $x < count($htmlArrayTemp); $x++) { | |
| 199 | - $t = trim($htmlArrayTemp[$x]); | |
| 200 | -            if ($t !== '') { | |
| 201 | - $htmlArray[$z] = $htmlArrayTemp[$x]; | |
| 202 | - $z++; | |
| 203 | -            } else { | |
| 204 | - $htmlArray[$z] = $this->emptySpaceChar; | |
| 205 | - $z++; | |
| 206 | - } | |
| 207 | - } | |
| 208 | - | |
| 209 | - // rebuild html | |
| 210 | - $html = ''; | |
| 211 | - $tabs = 0; | |
| 212 | -        for ($x = 0; $x < count($htmlArray); $x++) { | |
| 213 | - // check if the element should stand in a new line | |
| 214 | - $newline = false; | |
| 215 | -            if (substr($htmlArray[$x - 1], 0, 5) == '<?xml') { | |
| 216 | - $newline = true; | |
| 217 | - } elseif ($this->formatType == 2 && ( // minimalistic line break | |
| 218 | - # this element has a line break before itself | |
| 219 | - preg_match( | |
| 220 | - '/<' . $structureBoxLikeElements . '(.*)>/Usi', | |
| 221 | - $htmlArray[$x] | |
| 222 | - ) || preg_match( | |
| 223 | - '/<' . $structureBoxLikeElements . '(.*) \/>/Usi', | |
| 224 | - $htmlArray[$x] | |
| 225 | - ) || # one element before is a element that has a line break after | |
| 226 | - preg_match( | |
| 227 | - '/<\/' . $structureBoxLikeElements . '(.*)>/Usi', | |
| 228 | - $htmlArray[$x - 1] | |
| 229 | - ) || substr( | |
| 230 | - $htmlArray[$x - 1], | |
| 231 | - 0, | |
| 232 | - 4 | |
| 233 | -                    ) == '<!--' || preg_match('/<' . $structureBoxLikeElements . '(.*) \/>/Usi', $htmlArray[$x - 1])) | |
| 234 | -            ) { | |
| 235 | - $newline = true; | |
| 236 | - } elseif ($this->formatType == 3 && ( // aestetic line break | |
| 237 | - # this element has a line break before itself | |
| 238 | - preg_match( | |
| 239 | - '/<' . $esteticBoxLikeElements . '(.*)>/Usi', | |
| 240 | - $htmlArray[$x] | |
| 241 | - ) || preg_match( | |
| 242 | - '/<' . $esteticBoxLikeElements . '(.*) \/>/Usi', | |
| 243 | - $htmlArray[$x] | |
| 244 | - ) || # one element before is a element that has a line break after | |
| 245 | -                    preg_match('/<\/' . $esteticBoxLikeElements . '(.*)>/Usi', $htmlArray[$x - 1]) || substr( | |
| 246 | - $htmlArray[$x - 1], | |
| 247 | - 0, | |
| 248 | - 4 | |
| 249 | -                    ) == '<!--' || preg_match('/<' . $esteticBoxLikeElements . '(.*) \/>/Usi', $htmlArray[$x - 1])) | |
| 250 | -            ) { | |
| 251 | - $newline = true; | |
| 252 | - } elseif ($this->formatType >= 4 && ( // logical line break | |
| 253 | - # this element has a line break before itself | |
| 254 | - preg_match( | |
| 255 | - '/<' . $allBoxLikeElements . '(.*)>/Usi', | |
| 256 | - $htmlArray[$x] | |
| 257 | - ) || preg_match( | |
| 258 | - '/<' . $allBoxLikeElements . '(.*) \/>/Usi', | |
| 259 | - $htmlArray[$x] | |
| 260 | - ) || # one element before is a element that has a line break after | |
| 261 | -                    preg_match('/<\/' . $allBoxLikeElements . '(.*)>/Usi', $htmlArray[$x - 1]) || substr( | |
| 262 | - $htmlArray[$x - 1], | |
| 263 | - 0, | |
| 264 | - 4 | |
| 265 | -                    ) == '<!--' || preg_match('/<' . $allBoxLikeElements . '(.*) \/>/Usi', $htmlArray[$x - 1])) | |
| 266 | -            ) { | |
| 267 | - $newline = true; | |
| 268 | - } | |
| 269 | - | |
| 270 | - // count down a tab | |
| 271 | -            if (substr($htmlArray[$x], 0, 2) == '</') { | |
| 272 | - $tabs--; | |
| 273 | - } | |
| 274 | - | |
| 275 | - // add tabs and line breaks in front of the current tag | |
| 276 | -            if ($newline) { | |
| 277 | - $html .= $this->newline; | |
| 278 | -                for ($y = 0; $y < $tabs; $y++) { | |
| 279 | - $html .= $this->tab; | |
| 280 | - } | |
| 281 | - } | |
| 282 | - | |
| 283 | - // remove white spaces and line breaks and add current tag to the html-string | |
| 284 | - if (substr($htmlArray[$x - 1], 0, 4) == '<pre' // remove white space after line ending in PRE / TEXTAREA / comment | |
| 285 | - || substr($htmlArray[$x - 1], 0, 9) == '<textarea' || substr($htmlArray[$x - 1], 0, 4) == '<!--' | |
| 286 | -            ) { | |
| 287 | - $html .= $this->rTrimLines($htmlArray[$x]); | |
| 288 | - } elseif (substr($htmlArray[$x], 0, 9) == '<![CDATA[' // remove multiple white space in CDATA / XML | |
| 289 | - || substr($htmlArray[$x], 0, 5) == '<?xml' | |
| 290 | -            ) { | |
| 291 | - $html .= $this->killWhiteSpace($htmlArray[$x]); | |
| 292 | -            } else { // remove all line breaks | |
| 293 | - $html .= $this->killLineBreaks($htmlArray[$x]); | |
| 294 | - } | |
| 295 | - | |
| 296 | - // count up a tab | |
| 297 | -            if (substr($htmlArray[$x], 0, 1) == '<' && substr($htmlArray[$x], 1, 1) != '/') { | |
| 298 | - if ( | |
| 299 | - substr($htmlArray[$x], 1, 1) !== ' ' | |
| 300 | - && substr($htmlArray[$x], 1, 3) !== 'img' | |
| 301 | - && substr($htmlArray[$x], 1, 6) !== 'source' | |
| 302 | - && substr($htmlArray[$x], 1, 2) !== 'br' | |
| 303 | - && substr($htmlArray[$x], 1, 2) !== 'hr' | |
| 304 | - && substr($htmlArray[$x], 1, 5) !== 'input' | |
| 305 | - && substr($htmlArray[$x], 1, 4) !== 'link' | |
| 306 | - && substr($htmlArray[$x], 1, 4) !== 'meta' | |
| 307 | - && substr($htmlArray[$x], 1, 4) !== 'col ' | |
| 308 | - && substr($htmlArray[$x], 1, 5) !== 'frame' | |
| 309 | - && substr($htmlArray[$x], 1, 7) !== 'isindex' | |
| 310 | - && substr($htmlArray[$x], 1, 5) !== 'param' | |
| 311 | - && substr($htmlArray[$x], 1, 4) !== 'area' | |
| 312 | - && substr($htmlArray[$x], 1, 4) !== 'base' | |
| 313 | - && substr($htmlArray[$x], 0, 2) !== '<!' | |
| 314 | - && substr($htmlArray[$x], 0, 5) !== '<?xml' | |
| 315 | -                ) { | |
| 316 | - $tabs++; | |
| 317 | - } | |
| 318 | - } | |
| 319 | - } | |
| 320 | - | |
| 321 | - // Remove empty lines | |
| 322 | -        if ($this->formatType > 1) { | |
| 323 | - $this->removeEmptyLines($html); | |
| 324 | - } | |
| 325 | - | |
| 326 | - // Restore saved comments, styles and java-scripts | |
| 327 | -        for ($i = 0; $i < count($noFormat); $i++) { | |
| 328 | - $noFormat[$i] = $this->rTrimLines($noFormat[$i]); // remove white space after line ending | |
| 329 | -            $html = str_replace("<!-- ELEMENT $i -->", $noFormat[$i], $html); | |
| 330 | - } | |
| 331 | - | |
| 332 | - // include debug comment at the end | |
| 333 | -        if ($tabs != 0 && $this->debugComment === true) { | |
| 334 | - $html .= '<!--' . $tabs . " open elements found-->\r\n"; | |
| 335 | - } | |
| 336 | - | |
| 337 | - return $html; | |
| 338 | - } | |
| 339 | - | |
| 340 | - /** | |
| 341 | - * Remove ALL line breaks and multiple white space | |
| 342 | - * | |
| 343 | - * @param string $html | |
| 344 | - * | |
| 345 | - * @return string | |
| 346 | - */ | |
| 347 | - protected function killLineBreaks($html) | |
| 348 | -    { | |
| 349 | - $html = $this->convNlOs($html); | |
| 350 | - $html = str_replace($this->newline, "", $html); | |
| 351 | -        $html = preg_replace('/\s\s+/u', ' ', $html); | |
| 352 | - return $html; | |
| 353 | - } | |
| 354 | - | |
| 355 | - /** | |
| 356 | - * Remove multiple white space, keeps line breaks | |
| 357 | - * | |
| 358 | - * @param string $html | |
| 359 | - * | |
| 360 | - * @return string | |
| 361 | - */ | |
| 362 | - protected function killWhiteSpace($html) | |
| 363 | -    { | |
| 364 | - $html = $this->convNlOs($html); | |
| 365 | - $temp = explode($this->newline, $html); | |
| 366 | -        for ($i = 0; $i < count($temp); $i++) { | |
| 367 | -            if (!trim($temp[$i])) { | |
| 368 | - unset($temp[$i]); | |
| 369 | -            } else { | |
| 370 | - $temp[$i] = trim($temp[$i]); | |
| 371 | -                $temp[$i] = preg_replace('/\s\s+/', ' ', $temp[$i]); | |
| 372 | - } | |
| 373 | - } | |
| 374 | - $html = implode($this->newline, $temp); | |
| 375 | - return $html; | |
| 376 | - } | |
| 377 | - | |
| 378 | - /** | |
| 379 | - * Remove white space at the end of lines, keeps other white space and line breaks | |
| 380 | - * | |
| 381 | - * @param string $html | |
| 382 | - * | |
| 383 | - * @return string | |
| 384 | - */ | |
| 385 | - protected function rTrimLines($html) | |
| 386 | -    { | |
| 387 | - $html = $this->convNlOs($html); | |
| 388 | - $temp = explode($this->newline, $html); | |
| 389 | -        for ($i = 0; $i < count($temp); $i++) { | |
| 390 | - $temp[$i] = rtrim($temp[$i]); | |
| 391 | - } | |
| 392 | - $html = implode($this->newline, $temp); | |
| 393 | - return $html; | |
| 394 | - } | |
| 395 | - | |
| 396 | - /** | |
| 397 | - * Convert newlines according to the current OS | |
| 398 | - * | |
| 399 | - * @param string $html | |
| 400 | - * | |
| 401 | - * @return string | |
| 402 | - */ | |
| 403 | - protected function convNlOs($html) | |
| 404 | -    { | |
| 405 | -        $html = preg_replace("(\r\n|\n|\r)", $this->newline, $html); | |
| 406 | - return $html; | |
| 407 | - } | |
| 408 | - | |
| 409 | - /** | |
| 410 | - * Remove tabs and empty spaces before and after lines, transforms linebreaks system conform | |
| 411 | - * | |
| 412 | - * @param string $html Html-Code | |
| 413 | - * | |
| 414 | - * @return void | |
| 415 | - */ | |
| 416 | - protected function trimLines(&$html) | |
| 417 | -    { | |
| 418 | -        $html = str_replace("\t", "", $html); | |
| 419 | - // convert newlines according to the current OS | |
| 420 | -        if (Environment::isWindows()) { | |
| 421 | -            $html = str_replace("\n", "\r\n", $html); | |
| 422 | -        } else { | |
| 423 | -            $html = str_replace("\r\n", "\n", $html); | |
| 424 | - } | |
| 425 | - $temp = explode($this->newline, $html); | |
| 426 | -        $temp = array_map('trim', $temp); | |
| 427 | - $html = implode($this->newline, $temp); | |
| 428 | - unset($temp); | |
| 429 | - } | |
| 430 | - | |
| 431 | - /** | |
| 432 | - * Remove empty lines | |
| 433 | - * | |
| 434 | - * @param string $html | |
| 435 | - * | |
| 436 | - * @return void | |
| 437 | - */ | |
| 438 | - protected function removeEmptyLines(&$html) | |
| 439 | -    { | |
| 440 | - $temp = explode($this->newline, $html); | |
| 441 | - $result = []; | |
| 442 | -        for ($i = 0; $i < count($temp); ++$i) { | |
| 443 | -            if ("" == trim($temp[$i])) { | |
| 444 | - continue; | |
| 445 | - } | |
| 446 | - $result[] = $temp[$i]; | |
| 447 | - } | |
| 448 | - $html = implode($this->newline, $result); | |
| 449 | - } | |
| 450 | - | |
| 451 | - /** | |
| 452 | - * Remove new lines where unnecessary | |
| 453 | - * spares line breaks within: pre, textarea, ... | |
| 454 | - * | |
| 455 | - * @param string $html | |
| 456 | - * | |
| 457 | - * @return void | |
| 458 | - */ | |
| 459 | - protected function removeNewLines(&$html) | |
| 460 | -    { | |
| 461 | - $splitArray = [ | |
| 462 | - 'textarea', | |
| 463 | - 'pre' | |
| 464 | - ]; // eventuell auch: span, script, style | |
| 465 | -        $peaces = preg_split('#(<(' . implode('|', $splitArray) . ').*>.*</\2>)#Uis', $html, -1, PREG_SPLIT_DELIM_CAPTURE); | |
| 466 | - $html = ""; | |
| 467 | -        for ($i = 0; $i < count($peaces); $i++) { | |
| 468 | -            if (($i + 1) % 3 == 0) { | |
| 469 | - continue; | |
| 470 | - } | |
| 471 | - $html .= (($i - 1) % 3 != 0) ? $this->killLineBreaks($peaces[$i]) : $peaces[$i]; | |
| 472 | - } | |
| 473 | - } | |
| 474 | - | |
| 475 | - /** | |
| 476 | - * Remove obsolete link schema | |
| 477 | - * | |
| 478 | - * @param string $html | |
| 479 | - * | |
| 480 | - * @return void | |
| 481 | - */ | |
| 482 | - protected function removeLinkSchema(&$html) | |
| 483 | -    { | |
| 484 | -        $html = preg_replace("/<link rel=\"?schema.dc\"?.+?>/is", "", $html); | |
| 485 | - } | |
| 486 | - | |
| 487 | - /** | |
| 488 | - * Remove empty alt tags | |
| 489 | - * | |
| 490 | - * @param string $html | |
| 491 | - * | |
| 492 | - * @return void | |
| 493 | - */ | |
| 494 | - protected function removeEmptyAltAtr(&$html) | |
| 495 | -    { | |
| 496 | -        $html = str_replace("alt=\"\"", "", $html); | |
| 497 | - } | |
| 498 | - | |
| 499 | - /** | |
| 500 | - * Remove broken links in <a> tags | |
| 501 | - * | |
| 502 | - * @param string $html | |
| 503 | - * | |
| 504 | - * @return void | |
| 505 | - */ | |
| 506 | - protected function removeRealUrlBrokenRootLink(&$html) | |
| 507 | -    { | |
| 508 | -        $html = str_replace('href=".html"', 'href=""', $html); | |
| 509 | - } | |
| 510 | - | |
| 511 | - /** | |
| 512 | - * Include configured header comment in HTML content block | |
| 513 | - * | |
| 514 | - * @param $html | |
| 515 | - */ | |
| 516 | - public function includeHeaderComment(&$html) | |
| 517 | -    { | |
| 518 | -        if (!empty($this->headerComment)) { | |
| 519 | -            $html = preg_replace_callback('/<meta http-equiv(.*)>/Usi', function ($matches) { | |
| 520 | - return trim($matches[0] . $this->newline . $this->tab . $this->tab . '<!-- ' . $this->headerComment . '-->'); | |
| 521 | - }, $html, 1); | |
| 522 | - } | |
| 523 | - } | |
| 20 | + /** | |
| 21 | + * Enable Debug comment in footer | |
| 22 | + * | |
| 23 | + * @var boolean | |
| 24 | + */ | |
| 25 | + protected $debugComment = false; | |
| 26 | + | |
| 27 | + /** | |
| 28 | + * Format Type | |
| 29 | + * | |
| 30 | + * @var integer | |
| 31 | + */ | |
| 32 | + protected $formatType = 0; | |
| 33 | + | |
| 34 | + /** | |
| 35 | + * Tab character | |
| 36 | + * | |
| 37 | + * @var string | |
| 38 | + */ | |
| 39 | + protected $tab = "\t"; | |
| 40 | + | |
| 41 | + /** | |
| 42 | + * Newline character | |
| 43 | + * | |
| 44 | + * @var string | |
| 45 | + */ | |
| 46 | + protected $newline = "\n"; | |
| 47 | + | |
| 48 | + /** | |
| 49 | + * Configured extra header comment | |
| 50 | + * | |
| 51 | + * @var string | |
| 52 | + */ | |
| 53 | + protected $headerComment = ''; | |
| 54 | + | |
| 55 | + /** | |
| 56 | + * Empty space char | |
| 57 | + * @var string | |
| 58 | + */ | |
| 59 | + protected $emptySpaceChar = ' '; | |
| 60 | + | |
| 61 | + /** | |
| 62 | + * Set variables based on given config | |
| 63 | + * | |
| 64 | + * @param array $config | |
| 65 | + * | |
| 66 | + * @return void | |
| 67 | + */ | |
| 68 | + public function setVariables(array $config) | |
| 69 | +	{ | |
| 70 | + // Set newline based on OS | |
| 71 | +		if (Environment::isWindows()) { | |
| 72 | + $this->newline = "\r\n"; | |
| 73 | +		} else { | |
| 74 | + $this->newline = "\n"; | |
| 75 | + } | |
| 76 | + | |
| 77 | +		if (!empty($config)) { | |
| 78 | +			if ($config['formatHtml'] && is_numeric($config['formatHtml'])) { | |
| 79 | + $this->formatType = (int)$config['formatHtml']; | |
| 80 | + } | |
| 81 | + | |
| 82 | +			if ($config['formatHtml.']['tabSize'] && is_numeric($config['formatHtml.']['tabSize'])) { | |
| 83 | +				$this->tab = str_pad('', $config['formatHtml.']['tabSize'], ' '); | |
| 84 | + } | |
| 85 | + | |
| 86 | +			if (isset($config['formatHtml.']['debugComment'])) { | |
| 87 | + $this->debugComment = (bool)$config['formatHtml.']['debugComment']; | |
| 88 | + } | |
| 89 | + | |
| 90 | +			if (isset($config['headerComment'])) { | |
| 91 | + $this->headerComment = $config['headerComment']; | |
| 92 | + } | |
| 93 | + | |
| 94 | +			if (isset($config['dropEmptySpaceChar']) && (bool)$config['dropEmptySpaceChar']) { | |
| 95 | + $this->emptySpaceChar = ''; | |
| 96 | + } | |
| 97 | + } | |
| 98 | + } | |
| 99 | + | |
| 100 | + /** | |
| 101 | + * Clean given HTML with formatter | |
| 102 | + * | |
| 103 | + * @param string $html | |
| 104 | + * @param array $config | |
| 105 | + * | |
| 106 | + * @return string | |
| 107 | + */ | |
| 108 | + public function clean($html, $config = []) | |
| 109 | +	{ | |
| 110 | +		if (!empty($config)) { | |
| 111 | +			if ((bool)$config['enabled'] === false) { | |
| 112 | + return $html; | |
| 113 | + } | |
| 114 | + | |
| 115 | + $this->setVariables($config); | |
| 116 | + } | |
| 117 | + | |
| 118 | + $manipulations = []; | |
| 119 | + | |
| 120 | +		if (isset($config['removeGenerator']) && (bool)$config['removeGenerator']) { | |
| 121 | + $manipulations['removeGenerator'] = GeneralUtility::makeInstance(RemoveGenerator::class); | |
| 122 | + } | |
| 123 | + | |
| 124 | +		if (isset($config['removeComments']) && (bool)$config['removeComments']) { | |
| 125 | + $manipulations['removeComments'] = GeneralUtility::makeInstance(RemoveComments::class); | |
| 126 | + } | |
| 127 | + | |
| 128 | +		if (isset($config['removeBlurScript']) && (bool)$config['removeBlurScript']) { | |
| 129 | + $manipulations['removeBlurScript'] = GeneralUtility::makeInstance(RemoveBlurScript::class); | |
| 130 | + } | |
| 131 | + | |
| 132 | +		if (!empty($this->headerComment)) { | |
| 133 | + $this->includeHeaderComment($html); | |
| 134 | + } | |
| 135 | + | |
| 136 | +		foreach ($manipulations as $key => $manipulation) { | |
| 137 | + /** @var ManipulationInterface $manipulation */ | |
| 138 | + $configuration = isset($config[$key . '.']) && is_array($config[$key . '.']) ? $config[$key . '.'] : []; | |
| 139 | + $html = $manipulation->manipulate($html, $configuration); | |
| 140 | + } | |
| 141 | + | |
| 142 | +		if ($this->formatType > 0) { | |
| 143 | + $html = $this->formatHtml($html); | |
| 144 | + } | |
| 145 | + | |
| 146 | + return $html; | |
| 147 | + } | |
| 148 | + | |
| 149 | + /** | |
| 150 | + * Formats the (X)HTML code: | |
| 151 | + * - taps according to the hirarchy of the tags | |
| 152 | + * - removes empty spaces between tags | |
| 153 | + * - removes linebreaks within tags (spares where necessary: pre, textarea, comments, ..) | |
| 154 | + * choose from five options: | |
| 155 | + * 0 => off | |
| 156 | + * 1 => no line break at all (code in one line) | |
| 157 | + * 2 => minimalistic line breaks (structure defining box-elements) | |
| 158 | + * 3 => aesthetic line breaks (important box-elements) | |
| 159 | + * 4 => logic line breaks (all box-elements) | |
| 160 | + * 5 => max line breaks (all elements) | |
| 161 | + * | |
| 162 | + * @param string $html | |
| 163 | + * | |
| 164 | + * @return string | |
| 165 | + */ | |
| 166 | + protected function formatHtml($html) | |
| 167 | +	{ | |
| 168 | + // Save original formated comments, pre, textarea, styles and java-scripts & replace them with markers | |
| 169 | + preg_match_all( | |
| 170 | + '/(?s)((<!--.*?-->)|(<[ \n\r]*pre[^>]*>.*?<[ \n\r]*\/pre[^>]*>)|(<[ \n\r]*textarea[^>]*>.*?<[ \n\r]*\/textarea[^>]*>)|(<[ \n\r]*style[^>]*>.*?<[ \n\r]*\/style[^>]*>)|(<[ \n\r]*script[^>]*>.*?<[ \n\r]*\/script[^>]*>))/im', | |
| 171 | + $html, | |
| 172 | + $matches | |
| 173 | + ); | |
| 174 | + $noFormat = $matches[0]; // do not format these block elements | |
| 175 | +		for ($i = 0; $i < count($noFormat); $i++) { | |
| 176 | + $html = str_replace($noFormat[$i], "\n<!-- ELEMENT $i -->", $html); | |
| 177 | + } | |
| 178 | + | |
| 179 | + // define box elements for formatting | |
| 180 | + $trueBoxElements = 'address|blockquote|center|dir|div|dl|fieldset|form|h1|h2|h3|h4|h5|h6|hr|isindex|menu|noframes|noscript|ol|p|pre|table|ul|article|aside|details|figcaption|figure|footer|header|hgroup|menu|nav|section'; | |
| 181 | + $functionalBoxElements = 'dd|dt|frameset|li|tbody|td|tfoot|th|thead|tr|colgroup'; | |
| 182 | + $usableBoxElements = 'applet|button|del|iframe|ins|map|object|script'; | |
| 183 | + $imagineBoxElements = 'html|body|head|meta|title|link|script|base|!--'; | |
| 184 | + $allBoxLikeElements = '(?>' . $trueBoxElements . '|' . $functionalBoxElements . '|' . $usableBoxElements . '|' . $imagineBoxElements . ')'; | |
| 185 | + $esteticBoxLikeElements = '(?>html|head|body|meta name|title|div|table|h1|h2|h3|h4|h5|h6|p|form|pre|center|!--)'; | |
| 186 | + $structureBoxLikeElements = '(?>html|head|body|div|!--)'; | |
| 187 | + | |
| 188 | + // split html into it's elements | |
| 189 | + $htmlArrayTemp = preg_split( | |
| 190 | + '/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/', | |
| 191 | + $html, | |
| 192 | + -1, | |
| 193 | + PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY | |
| 194 | + ); | |
| 195 | + // remove empty lines | |
| 196 | + $htmlArray = ['']; | |
| 197 | + $z = 1; | |
| 198 | +		for ($x = 0; $x < count($htmlArrayTemp); $x++) { | |
| 199 | + $t = trim($htmlArrayTemp[$x]); | |
| 200 | +			if ($t !== '') { | |
| 201 | + $htmlArray[$z] = $htmlArrayTemp[$x]; | |
| 202 | + $z++; | |
| 203 | +			} else { | |
| 204 | + $htmlArray[$z] = $this->emptySpaceChar; | |
| 205 | + $z++; | |
| 206 | + } | |
| 207 | + } | |
| 208 | + | |
| 209 | + // rebuild html | |
| 210 | + $html = ''; | |
| 211 | + $tabs = 0; | |
| 212 | +		for ($x = 0; $x < count($htmlArray); $x++) { | |
| 213 | + // check if the element should stand in a new line | |
| 214 | + $newline = false; | |
| 215 | +			if (substr($htmlArray[$x - 1], 0, 5) == '<?xml') { | |
| 216 | + $newline = true; | |
| 217 | + } elseif ($this->formatType == 2 && ( // minimalistic line break | |
| 218 | + # this element has a line break before itself | |
| 219 | + preg_match( | |
| 220 | + '/<' . $structureBoxLikeElements . '(.*)>/Usi', | |
| 221 | + $htmlArray[$x] | |
| 222 | + ) || preg_match( | |
| 223 | + '/<' . $structureBoxLikeElements . '(.*) \/>/Usi', | |
| 224 | + $htmlArray[$x] | |
| 225 | + ) || # one element before is a element that has a line break after | |
| 226 | + preg_match( | |
| 227 | + '/<\/' . $structureBoxLikeElements . '(.*)>/Usi', | |
| 228 | + $htmlArray[$x - 1] | |
| 229 | + ) || substr( | |
| 230 | + $htmlArray[$x - 1], | |
| 231 | + 0, | |
| 232 | + 4 | |
| 233 | +					) == '<!--' || preg_match('/<' . $structureBoxLikeElements . '(.*) \/>/Usi', $htmlArray[$x - 1])) | |
| 234 | +			) { | |
| 235 | + $newline = true; | |
| 236 | + } elseif ($this->formatType == 3 && ( // aestetic line break | |
| 237 | + # this element has a line break before itself | |
| 238 | + preg_match( | |
| 239 | + '/<' . $esteticBoxLikeElements . '(.*)>/Usi', | |
| 240 | + $htmlArray[$x] | |
| 241 | + ) || preg_match( | |
| 242 | + '/<' . $esteticBoxLikeElements . '(.*) \/>/Usi', | |
| 243 | + $htmlArray[$x] | |
| 244 | + ) || # one element before is a element that has a line break after | |
| 245 | +					preg_match('/<\/' . $esteticBoxLikeElements . '(.*)>/Usi', $htmlArray[$x - 1]) || substr( | |
| 246 | + $htmlArray[$x - 1], | |
| 247 | + 0, | |
| 248 | + 4 | |
| 249 | +					) == '<!--' || preg_match('/<' . $esteticBoxLikeElements . '(.*) \/>/Usi', $htmlArray[$x - 1])) | |
| 250 | +			) { | |
| 251 | + $newline = true; | |
| 252 | + } elseif ($this->formatType >= 4 && ( // logical line break | |
| 253 | + # this element has a line break before itself | |
| 254 | + preg_match( | |
| 255 | + '/<' . $allBoxLikeElements . '(.*)>/Usi', | |
| 256 | + $htmlArray[$x] | |
| 257 | + ) || preg_match( | |
| 258 | + '/<' . $allBoxLikeElements . '(.*) \/>/Usi', | |
| 259 | + $htmlArray[$x] | |
| 260 | + ) || # one element before is a element that has a line break after | |
| 261 | +					preg_match('/<\/' . $allBoxLikeElements . '(.*)>/Usi', $htmlArray[$x - 1]) || substr( | |
| 262 | + $htmlArray[$x - 1], | |
| 263 | + 0, | |
| 264 | + 4 | |
| 265 | +					) == '<!--' || preg_match('/<' . $allBoxLikeElements . '(.*) \/>/Usi', $htmlArray[$x - 1])) | |
| 266 | +			) { | |
| 267 | + $newline = true; | |
| 268 | + } | |
| 269 | + | |
| 270 | + // count down a tab | |
| 271 | +			if (substr($htmlArray[$x], 0, 2) == '</') { | |
| 272 | + $tabs--; | |
| 273 | + } | |
| 274 | + | |
| 275 | + // add tabs and line breaks in front of the current tag | |
| 276 | +			if ($newline) { | |
| 277 | + $html .= $this->newline; | |
| 278 | +				for ($y = 0; $y < $tabs; $y++) { | |
| 279 | + $html .= $this->tab; | |
| 280 | + } | |
| 281 | + } | |
| 282 | + | |
| 283 | + // remove white spaces and line breaks and add current tag to the html-string | |
| 284 | + if (substr($htmlArray[$x - 1], 0, 4) == '<pre' // remove white space after line ending in PRE / TEXTAREA / comment | |
| 285 | + || substr($htmlArray[$x - 1], 0, 9) == '<textarea' || substr($htmlArray[$x - 1], 0, 4) == '<!--' | |
| 286 | +			) { | |
| 287 | + $html .= $this->rTrimLines($htmlArray[$x]); | |
| 288 | + } elseif (substr($htmlArray[$x], 0, 9) == '<![CDATA[' // remove multiple white space in CDATA / XML | |
| 289 | + || substr($htmlArray[$x], 0, 5) == '<?xml' | |
| 290 | +			) { | |
| 291 | + $html .= $this->killWhiteSpace($htmlArray[$x]); | |
| 292 | +			} else { // remove all line breaks | |
| 293 | + $html .= $this->killLineBreaks($htmlArray[$x]); | |
| 294 | + } | |
| 295 | + | |
| 296 | + // count up a tab | |
| 297 | +			if (substr($htmlArray[$x], 0, 1) == '<' && substr($htmlArray[$x], 1, 1) != '/') { | |
| 298 | + if ( | |
| 299 | + substr($htmlArray[$x], 1, 1) !== ' ' | |
| 300 | + && substr($htmlArray[$x], 1, 3) !== 'img' | |
| 301 | + && substr($htmlArray[$x], 1, 6) !== 'source' | |
| 302 | + && substr($htmlArray[$x], 1, 2) !== 'br' | |
| 303 | + && substr($htmlArray[$x], 1, 2) !== 'hr' | |
| 304 | + && substr($htmlArray[$x], 1, 5) !== 'input' | |
| 305 | + && substr($htmlArray[$x], 1, 4) !== 'link' | |
| 306 | + && substr($htmlArray[$x], 1, 4) !== 'meta' | |
| 307 | + && substr($htmlArray[$x], 1, 4) !== 'col ' | |
| 308 | + && substr($htmlArray[$x], 1, 5) !== 'frame' | |
| 309 | + && substr($htmlArray[$x], 1, 7) !== 'isindex' | |
| 310 | + && substr($htmlArray[$x], 1, 5) !== 'param' | |
| 311 | + && substr($htmlArray[$x], 1, 4) !== 'area' | |
| 312 | + && substr($htmlArray[$x], 1, 4) !== 'base' | |
| 313 | + && substr($htmlArray[$x], 0, 2) !== '<!' | |
| 314 | + && substr($htmlArray[$x], 0, 5) !== '<?xml' | |
| 315 | +				) { | |
| 316 | + $tabs++; | |
| 317 | + } | |
| 318 | + } | |
| 319 | + } | |
| 320 | + | |
| 321 | + // Remove empty lines | |
| 322 | +		if ($this->formatType > 1) { | |
| 323 | + $this->removeEmptyLines($html); | |
| 324 | + } | |
| 325 | + | |
| 326 | + // Restore saved comments, styles and java-scripts | |
| 327 | +		for ($i = 0; $i < count($noFormat); $i++) { | |
| 328 | + $noFormat[$i] = $this->rTrimLines($noFormat[$i]); // remove white space after line ending | |
| 329 | +			$html = str_replace("<!-- ELEMENT $i -->", $noFormat[$i], $html); | |
| 330 | + } | |
| 331 | + | |
| 332 | + // include debug comment at the end | |
| 333 | +		if ($tabs != 0 && $this->debugComment === true) { | |
| 334 | + $html .= '<!--' . $tabs . " open elements found-->\r\n"; | |
| 335 | + } | |
| 336 | + | |
| 337 | + return $html; | |
| 338 | + } | |
| 339 | + | |
| 340 | + /** | |
| 341 | + * Remove ALL line breaks and multiple white space | |
| 342 | + * | |
| 343 | + * @param string $html | |
| 344 | + * | |
| 345 | + * @return string | |
| 346 | + */ | |
| 347 | + protected function killLineBreaks($html) | |
| 348 | +	{ | |
| 349 | + $html = $this->convNlOs($html); | |
| 350 | + $html = str_replace($this->newline, "", $html); | |
| 351 | +		$html = preg_replace('/\s\s+/u', ' ', $html); | |
| 352 | + return $html; | |
| 353 | + } | |
| 354 | + | |
| 355 | + /** | |
| 356 | + * Remove multiple white space, keeps line breaks | |
| 357 | + * | |
| 358 | + * @param string $html | |
| 359 | + * | |
| 360 | + * @return string | |
| 361 | + */ | |
| 362 | + protected function killWhiteSpace($html) | |
| 363 | +	{ | |
| 364 | + $html = $this->convNlOs($html); | |
| 365 | + $temp = explode($this->newline, $html); | |
| 366 | +		for ($i = 0; $i < count($temp); $i++) { | |
| 367 | +			if (!trim($temp[$i])) { | |
| 368 | + unset($temp[$i]); | |
| 369 | +			} else { | |
| 370 | + $temp[$i] = trim($temp[$i]); | |
| 371 | +				$temp[$i] = preg_replace('/\s\s+/', ' ', $temp[$i]); | |
| 372 | + } | |
| 373 | + } | |
| 374 | + $html = implode($this->newline, $temp); | |
| 375 | + return $html; | |
| 376 | + } | |
| 377 | + | |
| 378 | + /** | |
| 379 | + * Remove white space at the end of lines, keeps other white space and line breaks | |
| 380 | + * | |
| 381 | + * @param string $html | |
| 382 | + * | |
| 383 | + * @return string | |
| 384 | + */ | |
| 385 | + protected function rTrimLines($html) | |
| 386 | +	{ | |
| 387 | + $html = $this->convNlOs($html); | |
| 388 | + $temp = explode($this->newline, $html); | |
| 389 | +		for ($i = 0; $i < count($temp); $i++) { | |
| 390 | + $temp[$i] = rtrim($temp[$i]); | |
| 391 | + } | |
| 392 | + $html = implode($this->newline, $temp); | |
| 393 | + return $html; | |
| 394 | + } | |
| 395 | + | |
| 396 | + /** | |
| 397 | + * Convert newlines according to the current OS | |
| 398 | + * | |
| 399 | + * @param string $html | |
| 400 | + * | |
| 401 | + * @return string | |
| 402 | + */ | |
| 403 | + protected function convNlOs($html) | |
| 404 | +	{ | |
| 405 | +		$html = preg_replace("(\r\n|\n|\r)", $this->newline, $html); | |
| 406 | + return $html; | |
| 407 | + } | |
| 408 | + | |
| 409 | + /** | |
| 410 | + * Remove tabs and empty spaces before and after lines, transforms linebreaks system conform | |
| 411 | + * | |
| 412 | + * @param string $html Html-Code | |
| 413 | + * | |
| 414 | + * @return void | |
| 415 | + */ | |
| 416 | + protected function trimLines(&$html) | |
| 417 | +	{ | |
| 418 | +		$html = str_replace("\t", "", $html); | |
| 419 | + // convert newlines according to the current OS | |
| 420 | +		if (Environment::isWindows()) { | |
| 421 | +			$html = str_replace("\n", "\r\n", $html); | |
| 422 | +		} else { | |
| 423 | +			$html = str_replace("\r\n", "\n", $html); | |
| 424 | + } | |
| 425 | + $temp = explode($this->newline, $html); | |
| 426 | +		$temp = array_map('trim', $temp); | |
| 427 | + $html = implode($this->newline, $temp); | |
| 428 | + unset($temp); | |
| 429 | + } | |
| 430 | + | |
| 431 | + /** | |
| 432 | + * Remove empty lines | |
| 433 | + * | |
| 434 | + * @param string $html | |
| 435 | + * | |
| 436 | + * @return void | |
| 437 | + */ | |
| 438 | + protected function removeEmptyLines(&$html) | |
| 439 | +	{ | |
| 440 | + $temp = explode($this->newline, $html); | |
| 441 | + $result = []; | |
| 442 | +		for ($i = 0; $i < count($temp); ++$i) { | |
| 443 | +			if ("" == trim($temp[$i])) { | |
| 444 | + continue; | |
| 445 | + } | |
| 446 | + $result[] = $temp[$i]; | |
| 447 | + } | |
| 448 | + $html = implode($this->newline, $result); | |
| 449 | + } | |
| 450 | + | |
| 451 | + /** | |
| 452 | + * Remove new lines where unnecessary | |
| 453 | + * spares line breaks within: pre, textarea, ... | |
| 454 | + * | |
| 455 | + * @param string $html | |
| 456 | + * | |
| 457 | + * @return void | |
| 458 | + */ | |
| 459 | + protected function removeNewLines(&$html) | |
| 460 | +	{ | |
| 461 | + $splitArray = [ | |
| 462 | + 'textarea', | |
| 463 | + 'pre' | |
| 464 | + ]; // eventuell auch: span, script, style | |
| 465 | +		$peaces = preg_split('#(<(' . implode('|', $splitArray) . ').*>.*</\2>)#Uis', $html, -1, PREG_SPLIT_DELIM_CAPTURE); | |
| 466 | + $html = ""; | |
| 467 | +		for ($i = 0; $i < count($peaces); $i++) { | |
| 468 | +			if (($i + 1) % 3 == 0) { | |
| 469 | + continue; | |
| 470 | + } | |
| 471 | + $html .= (($i - 1) % 3 != 0) ? $this->killLineBreaks($peaces[$i]) : $peaces[$i]; | |
| 472 | + } | |
| 473 | + } | |
| 474 | + | |
| 475 | + /** | |
| 476 | + * Remove obsolete link schema | |
| 477 | + * | |
| 478 | + * @param string $html | |
| 479 | + * | |
| 480 | + * @return void | |
| 481 | + */ | |
| 482 | + protected function removeLinkSchema(&$html) | |
| 483 | +	{ | |
| 484 | +		$html = preg_replace("/<link rel=\"?schema.dc\"?.+?>/is", "", $html); | |
| 485 | + } | |
| 486 | + | |
| 487 | + /** | |
| 488 | + * Remove empty alt tags | |
| 489 | + * | |
| 490 | + * @param string $html | |
| 491 | + * | |
| 492 | + * @return void | |
| 493 | + */ | |
| 494 | + protected function removeEmptyAltAtr(&$html) | |
| 495 | +	{ | |
| 496 | +		$html = str_replace("alt=\"\"", "", $html); | |
| 497 | + } | |
| 498 | + | |
| 499 | + /** | |
| 500 | + * Remove broken links in <a> tags | |
| 501 | + * | |
| 502 | + * @param string $html | |
| 503 | + * | |
| 504 | + * @return void | |
| 505 | + */ | |
| 506 | + protected function removeRealUrlBrokenRootLink(&$html) | |
| 507 | +	{ | |
| 508 | +		$html = str_replace('href=".html"', 'href=""', $html); | |
| 509 | + } | |
| 510 | + | |
| 511 | + /** | |
| 512 | + * Include configured header comment in HTML content block | |
| 513 | + * | |
| 514 | + * @param $html | |
| 515 | + */ | |
| 516 | + public function includeHeaderComment(&$html) | |
| 517 | +	{ | |
| 518 | +		if (!empty($this->headerComment)) { | |
| 519 | +			$html = preg_replace_callback('/<meta http-equiv(.*)>/Usi', function ($matches) { | |
| 520 | + return trim($matches[0] . $this->newline . $this->tab . $this->tab . '<!-- ' . $this->headerComment . '-->'); | |
| 521 | + }, $html, 1); | |
| 522 | + } | |
| 523 | + } | |
| 524 | 524 | } | 
| @@ -17,43 +17,43 @@ | ||
| 17 | 17 | */ | 
| 18 | 18 | class CleanHtmlMiddleware implements MiddlewareInterface | 
| 19 | 19 |  { | 
| 20 | - /** | |
| 21 | - * @var CleanHtmlService | |
| 22 | - */ | |
| 23 | - protected $cleanHtmlService = null; | |
| 24 | - | |
| 25 | - public function __construct() | |
| 26 | -    { | |
| 27 | - $this->cleanHtmlService = GeneralUtility::makeInstance(CleanHtmlService::class); | |
| 28 | - } | |
| 29 | - | |
| 30 | - /** | |
| 31 | - * Clean the HTML output | |
| 32 | - * | |
| 33 | - * @param ServerRequestInterface $request | |
| 34 | - * @param RequestHandlerInterface $handler | |
| 35 | - * @return ResponseInterface | |
| 36 | - */ | |
| 37 | - public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | |
| 38 | -    { | |
| 39 | - $response = $handler->handle($request); | |
| 40 | - | |
| 41 | - if ( | |
| 42 | - !($response instanceof NullResponse) | |
| 43 | - && $GLOBALS['TSFE'] instanceof TypoScriptFrontendController | |
| 44 | -            && $GLOBALS['TSFE']->isOutputting()) { | |
| 45 | - | |
| 46 | - $processedHtml = $this->cleanHtmlService->clean( | |
| 47 | - $response->getBody()->__toString(), | |
| 48 | - $GLOBALS['TSFE']->config['config']['sourceopt.'] | |
| 49 | - ); | |
| 50 | - | |
| 51 | - // Replace old body with $processedHtml | |
| 52 | -            $responseBody = new Stream('php://temp', 'rw'); | |
| 53 | - $responseBody->write($processedHtml); | |
| 54 | - $response = $response->withBody($responseBody); | |
| 55 | - } | |
| 56 | - | |
| 57 | - return $response; | |
| 58 | - } | |
| 20 | + /** | |
| 21 | + * @var CleanHtmlService | |
| 22 | + */ | |
| 23 | + protected $cleanHtmlService = null; | |
| 24 | + | |
| 25 | + public function __construct() | |
| 26 | +	{ | |
| 27 | + $this->cleanHtmlService = GeneralUtility::makeInstance(CleanHtmlService::class); | |
| 28 | + } | |
| 29 | + | |
| 30 | + /** | |
| 31 | + * Clean the HTML output | |
| 32 | + * | |
| 33 | + * @param ServerRequestInterface $request | |
| 34 | + * @param RequestHandlerInterface $handler | |
| 35 | + * @return ResponseInterface | |
| 36 | + */ | |
| 37 | + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | |
| 38 | +	{ | |
| 39 | + $response = $handler->handle($request); | |
| 40 | + | |
| 41 | + if ( | |
| 42 | + !($response instanceof NullResponse) | |
| 43 | + && $GLOBALS['TSFE'] instanceof TypoScriptFrontendController | |
| 44 | +			&& $GLOBALS['TSFE']->isOutputting()) { | |
| 45 | + | |
| 46 | + $processedHtml = $this->cleanHtmlService->clean( | |
| 47 | + $response->getBody()->__toString(), | |
| 48 | + $GLOBALS['TSFE']->config['config']['sourceopt.'] | |
| 49 | + ); | |
| 50 | + | |
| 51 | + // Replace old body with $processedHtml | |
| 52 | +			$responseBody = new Stream('php://temp', 'rw'); | |
| 53 | + $responseBody->write($processedHtml); | |
| 54 | + $response = $response->withBody($responseBody); | |
| 55 | + } | |
| 56 | + | |
| 57 | + return $response; | |
| 58 | + } | |
| 59 | 59 | } |