From afa7cfa833bf817c61639f34d2e69143823ceacd Mon Sep 17 00:00:00 2001 From: Petr Skoda Date: Tue, 28 Sep 2010 22:34:33 +0000 Subject: [PATCH] improved handling on errors on redirect page + preventing PAGE->context errors --- lib/weblib.php | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/lib/weblib.php b/lib/weblib.php index 6c3e7a0c1dc..63fb88e5614 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -2518,6 +2518,9 @@ function redirect($url, $message='', $delay=-1) { throw new moodle_exception('redirecterrordetected', 'error'); } + // prevent debug errors - make sure context is properly initialised + $PAGE->set_context(null); + if ($url instanceof moodle_url) { $url = $url->out(false); } @@ -2526,12 +2529,42 @@ function redirect($url, $message='', $delay=-1) { $url = $SESSION->sid_process_url($url); } - if (function_exists('error_get_last')) { - $lasterror = error_get_last(); - //NOTE: problem here is that this contains error even if error hidden with @do(); - } - $debugdisableredirect = defined('DEBUGGING_PRINTED') || - (!empty($CFG->debugdisplay) && !empty($lasterror) && ($lasterror['type'] & DEBUG_DEVELOPER)); + $debugdisableredirect = false; + do { + if (defined('DEBUGGING_PRINTED')) { + // some debugging already printed, no need to look more + $debugdisableredirect = true; + break; + } + + if (empty($CFG->debugdisplay) or empty($CFG->debug)) { + // no errors should be displayed + break; + } + + if (!function_exists('error_get_last') or !$lasterror = error_get_last()) { + break; + } + + if (!($lasterror['type'] & $CFG->debug)) { + //last error not interesting + break; + } + + // watch out here, @hidden() errors are returned from error_get_last() too + if (headers_sent()) { + //we already started printing something - that means errors likely printed + $debugdisableredirect = true; + break; + } + + if (ob_get_level() and ob_get_contents()) { + // there is something waiting to be printed, hopefully it is the errors, + // but it might be some error hidden by @ too - such as the timezone mess from setup.php + $debugdisableredirect = true; + break; + } + } while (false); if (!empty($message)) { if ($delay === -1 || !is_numeric($delay)) {