From c9271c80a7108ba186d56ee887f062d8d3cd12e3 Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Wed, 23 Mar 2022 10:49:19 +0200 Subject: [PATCH] Create method `cleanOutputBuffers()` and use it --- system/src/Grav/Common/Grav.php | 27 +++++++++++++++++++++------ system/src/Grav/Common/Utils.php | 15 ++++++--------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/system/src/Grav/Common/Grav.php b/system/src/Grav/Common/Grav.php index 8c5f48612..68a0bf3cf 100644 --- a/system/src/Grav/Common/Grav.php +++ b/system/src/Grav/Common/Grav.php @@ -341,6 +341,25 @@ class Grav extends Container } } + /** + * Clean any output buffers. Useful when exiting from the application. + * + * Please use $grav->close() and $grav->redirect() instead of calling this one! + * + * @return void + */ + 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) { + ob_end_clean(); + } + } + /** * Terminates Grav request with a response. * @@ -351,11 +370,7 @@ class Grav extends Container */ public function close(ResponseInterface $response): void { - $gzip_enabled = (int) Grav::instance()['config']->get('system.cache.gzip'); - // Make sure nothing extra gets written to the response. - while (ob_get_level() > 2 + $gzip_enabled) { - ob_end_clean(); - } + $this->cleanOutputBuffers(); // Close the session. if (isset($this['session'])) { @@ -401,7 +416,7 @@ class Grav extends Container /** * @param ResponseInterface $response * @return never-return - * @deprecated 1.7 Do not use + * @deprecated 1.7 Use $grav->close() instead. */ public function exit(ResponseInterface $response): void { diff --git a/system/src/Grav/Common/Utils.php b/system/src/Grav/Common/Utils.php index 5d0169c8c..55e7f1dec 100644 --- a/system/src/Grav/Common/Utils.php +++ b/system/src/Grav/Common/Utils.php @@ -668,20 +668,17 @@ abstract class Utils */ public static function download($file, $force_download = true, $sec = 0, $bytes = 1024, array $options = []) { + $grav = Grav::instance(); + if (file_exists($file)) { // fire download event - Grav::instance()->fireEvent('onBeforeDownload', new Event(['file' => $file, 'options' => &$options])); + $grav->fireEvent('onBeforeDownload', new Event(['file' => $file, 'options' => &$options])); $file_parts = static::pathinfo($file); $mimetype = $options['mime'] ?? static::getMimeByExtension($file_parts['extension']); $size = filesize($file); // File size - $gzip_enabled = (int) Grav::instance()['config']->get('system.cache.gzip'); - - // clean all buffers - while (ob_get_level() > 2 + $gzip_enabled) { - ob_end_clean(); - } + $grav->cleanOutputBuffers(); // required for IE, otherwise Content-Disposition may be ignored if (ini_get('zlib.output_compression')) { @@ -716,8 +713,8 @@ abstract class Utils $new_length = $size; header('Content-Length: ' . $size); - if (Grav::instance()['config']->get('system.cache.enabled')) { - $expires = $options['expires'] ?? Grav::instance()['config']->get('system.pages.expires'); + if ($grav['config']->get('system.cache.enabled')) { + $expires = $options['expires'] ?? $grav['config']->get('system.pages.expires'); if ($expires > 0) { $expires_date = gmdate('D, d M Y H:i:s T', time() + $expires); header('Cache-Control: max-age=' . $expires);