improved handling on errors on redirect page + preventing PAGE->context errors
authorPetr Skoda <skodak@moodle.org>
Tue, 28 Sep 2010 22:34:33 +0000 (22:34 +0000)
committerPetr Skoda <skodak@moodle.org>
Tue, 28 Sep 2010 22:34:33 +0000 (22:34 +0000)
lib/weblib.php

index 6c3e7a0..63fb88e 100644 (file)
@@ -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)) {