From 4cbc81c7ea3c97ddb656215b05075a6ffd5fecff Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Fri, 17 May 2024 00:00:34 +0800 Subject: [PATCH] MDL-80275 core: Move disable_output_buffering to setup.php This method is the only reason stopping lib/setuplib.php from being included after the autoloader and it is single-purposed. --- lib/deprecatedlib.php | 8 ++++++++ lib/setup.php | 35 +++++++++++++++++++++++++++++++++-- lib/setuplib.php | 37 ------------------------------------- 3 files changed, 41 insertions(+), 39 deletions(-) diff --git a/lib/deprecatedlib.php b/lib/deprecatedlib.php index 666cf535409..8448f265117 100644 --- a/lib/deprecatedlib.php +++ b/lib/deprecatedlib.php @@ -3216,3 +3216,11 @@ function question_fix_top_names() { function search_generate_text_SQL() { \core\deprecation::emit_deprecation_if_present(__FUNCTION__); } + +/** + * @deprecated Since Moodle 4.5 + */ +#[\core\attribute\deprecated('This method should not be used', since: '4.5', mdl: 'MDL-80275', final: true)] +function disable_output_buffering(): void { + \core\deprecation::emit_deprecation_if_present(__FUNCTION__); +} diff --git a/lib/setup.php b/lib/setup.php index d6fc92b71fa..81c7658e9fe 100644 --- a/lib/setup.php +++ b/lib/setup.php @@ -551,9 +551,40 @@ global $SCRIPT; // The httpswwwroot has been deprecated, we keep it as an alias for backwards compatibility with plugins only. $CFG->httpswwwroot = $CFG->wwwroot; +// We have to call this always before starting session because it discards headers! if (NO_OUTPUT_BUFFERING) { - // we have to call this always before starting session because it discards headers! - disable_output_buffering(); + // Try to disable all output buffering and purge all headers. + $olddebug = error_reporting(0); + + // Disable compression, it would prevent closing of buffers. + if ($outputcompression = ini_get('zlib.output_compression')) { + switch(strtolower($outputcompression)) { + case 'on': + case '1': + ini_set('zlib.output_compression', 'Off'); + break; + } + } + + // Try to flush everything all the time. + ob_implicit_flush(true); + + // Close all buffers if possible and discard any existing output. + // This can actually work around some whitespace problems in config.php. + while (ob_get_level()) { + if (!ob_end_clean()) { + // Prevent infinite loop when buffer can not be closed. + break; + } + } + + // Disable any other output handlers. + ini_set('output_handler', ''); + + error_reporting($olddebug); + + // Disable buffering in nginx. + header('X-Accel-Buffering: no'); } // Point pear include path to moodles lib/pear so that includes and requires will search there for files before anywhere else diff --git a/lib/setuplib.php b/lib/setuplib.php index 5d7669d98ac..62b72477c9a 100644 --- a/lib/setuplib.php +++ b/lib/setuplib.php @@ -1464,43 +1464,6 @@ function get_real_size($size = 0) { return (int) $size; } -/** - * Try to disable all output buffering and purge - * all headers. - * - * @access private to be called only from lib/setup.php ! - * @return void - */ -function disable_output_buffering() { - $olddebug = error_reporting(0); - - // disable compression, it would prevent closing of buffers - if (ini_get_bool('zlib.output_compression')) { - ini_set('zlib.output_compression', 'Off'); - } - - // try to flush everything all the time - ob_implicit_flush(true); - - // close all buffers if possible and discard any existing output - // this can actually work around some whitespace problems in config.php - while(ob_get_level()) { - if (!ob_end_clean()) { - // prevent infinite loop when buffer can not be closed - break; - } - } - - // disable any other output handlers - ini_set('output_handler', ''); - - error_reporting($olddebug); - - // Disable buffering in nginx. - header('X-Accel-Buffering: no'); - -} - /** * Check whether a major upgrade is needed. *