Move brotli and gzip compress option to application.ini

This commit is contained in:
the-djmaze 2023-02-23 08:39:40 +01:00
parent b5db67d23f
commit 202fb08d09
4 changed files with 24 additions and 27 deletions

View file

@ -4,16 +4,6 @@
//header('Strict-Transport-Security: max-age=31536000'); //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. * Uncomment to enable multiple domain installation.
*/ */

View file

@ -171,7 +171,10 @@ class Application extends \RainLoop\Config\AbstractConfig
'message_read_delay' => array(5, 'Mark message read after N seconds'), '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 '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( 'interface' => array(

View file

@ -43,6 +43,26 @@ abstract class Service
exit; 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'] ?? ''); $sQuery = \trim($_SERVER['QUERY_STRING'] ?? '');
$iPos = \strpos($sQuery, '&'); $iPos = \strpos($sQuery, '&');
if (0 < $iPos) { if (0 < $iPos) {

View file

@ -134,22 +134,6 @@ if (isset($_SERVER['HTTPS'])) {
header('Strict-Transport-Security: max-age=31536000'); 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 // cPanel https://github.com/the-djmaze/snappymail/issues/697
if (!empty($_ENV['CPANEL']) && !is_dir(APP_PLUGINS_PATH.'login-remote')) { if (!empty($_ENV['CPANEL']) && !is_dir(APP_PLUGINS_PATH.'login-remote')) {
require __DIR__ . '/cpanel.php'; require __DIR__ . '/cpanel.php';