From 952830b5290de9176336bd8d938389a8fcd4e15b Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Thu, 25 Dec 2025 21:17:22 -0700 Subject: [PATCH] fix gzip encoding issue with newer PHP 8 versions Signed-off-by: Andy Miller --- system/src/Grav/Common/Grav.php | 32 ++++++++++--------- .../Common/Processors/InitializeProcessor.php | 8 +++-- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/system/src/Grav/Common/Grav.php b/system/src/Grav/Common/Grav.php index 3ad09ba2f..d73b803f3 100644 --- a/system/src/Grav/Common/Grav.php +++ b/system/src/Grav/Common/Grav.php @@ -608,23 +608,25 @@ class Grav extends Container // Unfortunately without FastCGI there is no way to force close the connection. // We need to ask browser to close the connection for us. - if ($config->get('system.cache.gzip')) { - // Flush gzhandler buffer if gzip setting was enabled to get the size of the compressed output. - ob_end_flush(); - } elseif ($config->get('system.cache.allow_webserver_gzip')) { - // Let web server to do the hard work. - header('Content-Encoding: identity'); - } elseif (function_exists('apache_setenv')) { - // Without gzip we have no other choice than to prevent server from compressing the output. - // This action turns off mod_deflate which would prevent us from closing the connection. - @apache_setenv('no-gzip', '1'); - } else { - // Fall back to unknown content encoding, it prevents most servers from deflating the content. - header('Content-Encoding: none'); + // With zlib.output_compression enabled, PHP handles compression automatically. + // We cannot set Content-Length when compression is active (size unknown until compressed). + if (!$config->get('system.cache.gzip') && !ini_get('zlib.output_compression')) { + if ($config->get('system.cache.allow_webserver_gzip')) { + // Let web server to do the hard work. + header('Content-Encoding: identity'); + } elseif (function_exists('apache_setenv')) { + // Without gzip we have no other choice than to prevent server from compressing the output. + // This action turns off mod_deflate which would prevent us from closing the connection. + @apache_setenv('no-gzip', '1'); + } else { + // Fall back to unknown content encoding, it prevents most servers from deflating the content. + header('Content-Encoding: none'); + } + + // Get length and close the connection (only when not using compression). + header('Content-Length: ' . ob_get_length()); } - // Get length and close the connection. - header('Content-Length: ' . ob_get_length()); header('Connection: close'); ob_end_flush(); diff --git a/system/src/Grav/Common/Processors/InitializeProcessor.php b/system/src/Grav/Common/Processors/InitializeProcessor.php index af8d59711..70620f300 100644 --- a/system/src/Grav/Common/Processors/InitializeProcessor.php +++ b/system/src/Grav/Common/Processors/InitializeProcessor.php @@ -345,9 +345,11 @@ class InitializeProcessor extends ProcessorBase // Use output buffering to prevent headers from being sent too early. ob_start(); - if ($config->get('system.cache.gzip') && !@ob_start('ob_gzhandler')) { - // Enable zip/deflate with a fallback in case of if browser does not support compressing. - ob_start(); + if ($config->get('system.cache.gzip')) { + // Try to use zlib.output_compression instead of ob_gzhandler (more robust) + if (!ini_get('zlib.output_compression') && !headers_sent()) { + ini_set('zlib.output_compression', '4096'); + } } $this->stopTimer('_init_ob');