From c9c23c6c4f77d0628126e2e839f477794cb8a59f Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Wed, 23 Mar 2022 15:47:45 +0200 Subject: [PATCH] Better fix for `system.cache.gzip: true` --- CHANGELOG.md | 2 +- .../src/Grav/Common/Errors/SystemFacade.php | 21 +++++++++++++++++++ system/src/Grav/Common/Grav.php | 8 +++---- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a3590c4e..165516638 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +12,7 @@ - `text`, `url`, `hidden`, `commalist`: 2048 - `text` (multiline), `textarea`: 65536 3. [](#bugfix) - * Fixed issue with `system.cache.gzip: true` resulted in admin "Fetch Failed" for PHP 8.0+ + * Fixed issue with `system.cache.gzip: true` resulted in admin "Fetch Failed" for PHP 8.0.17 and PHP 8.1.4 [PHP bug #8218](https://github.com/php/php-src/issues/8218). * Fix for multi-lang issues with Security Report # v1.7.31 diff --git a/system/src/Grav/Common/Errors/SystemFacade.php b/system/src/Grav/Common/Errors/SystemFacade.php index edf2b5338..e13b0f6e7 100644 --- a/system/src/Grav/Common/Errors/SystemFacade.php +++ b/system/src/Grav/Common/Errors/SystemFacade.php @@ -43,4 +43,25 @@ class SystemFacade extends \Whoops\Util\SystemFacade $handler(); } } + + + /** + * @param int $httpCode + * + * @return int + */ + public function setHttpResponseCode($httpCode) + { + if (!headers_sent()) { + // Ensure that no 'location' header is present as otherwise this + // will override the HTTP code being set here, and mask the + // expected error page. + header_remove('location'); + + // Work around PHP bug #8218 (8.0.17 & 8.1.4). + header_remove('Content-Encoding'); + } + + return http_response_code($httpCode); + } } diff --git a/system/src/Grav/Common/Grav.php b/system/src/Grav/Common/Grav.php index 68a0bf3cf..7e3dcaff8 100644 --- a/system/src/Grav/Common/Grav.php +++ b/system/src/Grav/Common/Grav.php @@ -350,14 +350,12 @@ class Grav extends Container */ public function cleanOutputBuffers(): void { - /** @var Config $config */ - $config = $this['config']; - $gzip_enabled = (int) $config->get('system.cache.gzip'); - // Make sure nothing extra gets written to the response. - while (ob_get_level() > 2 + $gzip_enabled) { + while (ob_get_level()) { ob_end_clean(); } + // Work around PHP bug #8218 (8.0.17 & 8.1.4). + header_remove('Content-Encoding'); } /**