From c284d71f91fbdd7e368708fafc81f9b54cd2e1aa Mon Sep 17 00:00:00 2001 From: jaswrks Date: Wed, 19 Apr 2017 22:48:31 -0800 Subject: [PATCH] Check invalid UTF-8. See: https://github.com/websharks/comet-cache/issues/871 --- src/includes/classes/Core.php | 73 ++++++++++++++++++++++------------- 1 file changed, 47 insertions(+), 26 deletions(-) diff --git a/src/includes/classes/Core.php b/src/includes/classes/Core.php index 31e83b4..a707a2e 100644 --- a/src/includes/classes/Core.php +++ b/src/includes/classes/Core.php @@ -416,33 +416,34 @@ public function compress($input) if (($benchmark = !empty($this->options['benchmark']))) { $time = microtime(true); } - $html = &$input; // Let's call this HTML now. - - if (!empty($this->options['amp_exclusions_enable']) && $this->isDocAmpd($html)) { - $this->options['compress_combine_head_body_css'] = false; - $this->options['compress_combine_head_js'] = false; - $this->options['compress_combine_footer_js'] = false; - $this->options['compress_combine_remote_css_js'] = false; - } - $html = $this->tokenizeGlobalExclusions($html); - $html = $this->maybeCompressCombineHeadBodyCss($html); - $html = $this->maybeCompressCombineHeadJs($html); - $html = $this->maybeCompressCombineFooterJs($html); - $html = $this->maybeCompressInlineJsCode($html); - $html = $this->maybeCompressInlineJsonCode($html); - $html = $this->restoreGlobalExclusions($html); - $html = $this->maybeCompressHtmlCode($html); - + $html = &$input; // Raw HTML. + $is_valid_utf8 = $this->isValidUtf8($html); + + if ($is_valid_utf8) { // Must have valid UTF-8. + if (!empty($this->options['amp_exclusions_enable']) && $this->isDocAmpd($html)) { + $this->options['compress_combine_head_body_css'] = false; + $this->options['compress_combine_head_js'] = false; + $this->options['compress_combine_footer_js'] = false; + $this->options['compress_combine_remote_css_js'] = false; + } // This auto-enables AMP compatibility. + + $html = $this->tokenizeGlobalExclusions($html); + $html = $this->maybeCompressCombineHeadBodyCss($html); + $html = $this->maybeCompressCombineHeadJs($html); + $html = $this->maybeCompressCombineFooterJs($html); + $html = $this->maybeCompressInlineJsCode($html); + $html = $this->maybeCompressInlineJsonCode($html); + $html = $this->restoreGlobalExclusions($html); + $html = $this->maybeCompressHtmlCode($html); + } if (!isset($this->options['cleanup_cache_dirs']) || $this->options['cleanup_cache_dirs']) { - if (mt_rand(1, 20) === 1) { - $this->cleanupCacheDirs(); - } + mt_rand(1, 20) === 1 ? $this->cleanupCacheDirs() : null; } if ($benchmark && !empty($time)) { $time = number_format(microtime(true) - $time, 5, '.', ''); if ($this->benchmark->times) { - $html .= "\n"; + $html .= "\n"; // Spacer. } foreach ($this->benchmark->times as $_benchmark_time) { $html .= "\n".''; } // unset($_benchmark_time); // Housekeeping. - $html .= "\n\n".''; + if (!$is_valid_utf8) { + $html .= "\n\n".''; + } else { + $html .= "\n\n".''; + } } return $html; // HTML markup. } @@ -493,6 +501,19 @@ public function __get($property) /********************************************************************************************************/ + /* + * Validation-Related Methods + */ + + protected function isValidUtf8($html) + { + preg_match('/./u', $html); + $last_error = preg_last_error(); + return !in_array($last_error, [PREG_BAD_UTF8_ERROR, PREG_BAD_UTF8_OFFSET_ERROR], true); + } + + /********************************************************************************************************/ + /* * Exclusion-Related Methods */