diff --git a/_include.php b/_include.php index 65a689ba4..de1cc3ee0 100644 --- a/_include.php +++ b/_include.php @@ -4,16 +4,6 @@ //header('Strict-Transport-Security: max-age=31536000'); -/** - * Uncomment to use gzip compressed output - */ -//define('USE_GZIP', 1); - -/** - * Uncomment to use brotli compressed output - */ -//define('USE_BROTLI', 1); - /** * Uncomment to enable multiple domain installation. */ diff --git a/snappymail/v/0.0.0/app/libraries/RainLoop/Config/Application.php b/snappymail/v/0.0.0/app/libraries/RainLoop/Config/Application.php index c06967ef9..eee57dbb3 100644 --- a/snappymail/v/0.0.0/app/libraries/RainLoop/Config/Application.php +++ b/snappymail/v/0.0.0/app/libraries/RainLoop/Config/Application.php @@ -171,7 +171,10 @@ class Application extends \RainLoop\Config\AbstractConfig 'message_read_delay' => array(5, 'Mark message read after N seconds'), 'attachment_size_limit' => array(\min($upload_max_filesize, 25), 'File size limit (MB) for file upload on compose screen -0 for unlimited.') +0 for unlimited.'), + + 'compress_output' => array(false, 'brotli or gzip compress the output. +Warning: only enable when server does not do this, else double compression errors occur') ), 'interface' => array( diff --git a/snappymail/v/0.0.0/app/libraries/RainLoop/Service.php b/snappymail/v/0.0.0/app/libraries/RainLoop/Service.php index bd460d6ae..6a56a5c54 100644 --- a/snappymail/v/0.0.0/app/libraries/RainLoop/Service.php +++ b/snappymail/v/0.0.0/app/libraries/RainLoop/Service.php @@ -43,6 +43,26 @@ abstract class Service exit; } + // See https://github.com/kjdev/php-ext-brotli + if (!empty($_SERVER['HTTP_ACCEPT_ENCODING']) + && $oConfig->Get('webmail', 'compress_output', false) + && !\ini_get('zlib.output_compression') + && !\ini_get('brotli.output_compression') + ) { + if (\is_callable('brotli_compress_add') && false !== \stripos($_SERVER['HTTP_ACCEPT_ENCODING'], 'br')) { + \ob_start(function(string $buffer, int $phase){ + static $resource; + if ($phase & PHP_OUTPUT_HANDLER_START) { + \header('Content-Encoding: br'); + $resource = \brotli_compress_init(/*int $quality = 11, int $mode = BROTLI_GENERIC*/); + } + return \brotli_compress_add($resource, $buffer, ($phase & PHP_OUTPUT_HANDLER_FINAL) ? BROTLI_FINISH : BROTLI_PROCESS); + }); + } else { + \ob_start('ob_gzhandler'); + } + } + $sQuery = \trim($_SERVER['QUERY_STRING'] ?? ''); $iPos = \strpos($sQuery, '&'); if (0 < $iPos) { diff --git a/snappymail/v/0.0.0/include.php b/snappymail/v/0.0.0/include.php index 589d47d31..a0eeec9cc 100644 --- a/snappymail/v/0.0.0/include.php +++ b/snappymail/v/0.0.0/include.php @@ -134,22 +134,6 @@ if (isset($_SERVER['HTTPS'])) { header('Strict-Transport-Security: max-age=31536000'); } -// See https://github.com/kjdev/php-ext-brotli -if (!ini_get('zlib.output_compression') && !ini_get('brotli.output_compression')) { - if (defined('USE_BROTLI') && is_callable('brotli_compress_add') && false !== stripos($_SERVER['HTTP_ACCEPT_ENCODING'], 'br')) { - ob_start(function(string $buffer, int $phase){ - static $resource; - if ($phase & PHP_OUTPUT_HANDLER_START) { - header('Content-Encoding: br'); - $resource = brotli_compress_init(/*int $quality = 11, int $mode = BROTLI_GENERIC*/); - } - return brotli_compress_add($resource, $buffer, ($phase & PHP_OUTPUT_HANDLER_FINAL) ? BROTLI_FINISH : BROTLI_PROCESS); - }); - } else if (defined('USE_GZIP')) { - ob_start('ob_gzhandler'); - } -} - // cPanel https://github.com/the-djmaze/snappymail/issues/697 if (!empty($_ENV['CPANEL']) && !is_dir(APP_PLUGINS_PATH.'login-remote')) { require __DIR__ . '/cpanel.php';