diff --git a/admin/dbperformance.php b/admin/dbperformance.php index f5f597b6225ec..fb32a6557d263 100644 --- a/admin/dbperformance.php +++ b/admin/dbperformance.php @@ -1,12 +1,13 @@ libdir . '/filelib.php'); @@ -36,9 +39,6 @@ require_login(); require_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM)); -// disable moodle specific debug messages -disable_debugging(); - // get file requested $relativepath = get_file_argument(); diff --git a/draftfile.php b/draftfile.php index f9c7d30909042..de82ca43ab943 100644 --- a/draftfile.php +++ b/draftfile.php @@ -24,6 +24,9 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +// disable moodle specific debug messages and any errors in output +define('NO_DEBUG_DISPLAY', true); + require_once('config.php'); require_once('lib/filelib.php'); @@ -32,9 +35,6 @@ print_error('noguest'); } -// disable moodle specific debug messages -disable_debugging(); - $relativepath = get_file_argument(); // relative path must start with '/' diff --git a/file.php b/file.php index c7f1fb8b28ea2..ddbdfd24226b2 100644 --- a/file.php +++ b/file.php @@ -31,6 +31,9 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +// disable moodle specific debug messages and any errors in output +define('NO_DEBUG_DISPLAY', true); + require_once('config.php'); require_once('lib/filelib.php'); @@ -40,9 +43,6 @@ $lifetime = $CFG->filelifetime; } -// disable moodle specific debug messages -disable_debugging(); - $relativepath = get_file_argument(); $forcedownload = optional_param('forcedownload', 0, PARAM_BOOL); diff --git a/filter/algebra/pix.php b/filter/algebra/pix.php index f65c2ecab1fde..5debd28829fbe 100644 --- a/filter/algebra/pix.php +++ b/filter/algebra/pix.php @@ -1,19 +1,18 @@ -libdir.'/filelib.php'); require_once($CFG->dirroot.'/filter/tex/lib.php'); @@ -61,4 +60,4 @@ echo "Please turn on debug mode in site configuration to see more info here."; } } -?> + diff --git a/filter/tex/pix.php b/filter/tex/pix.php index efb6eb2b89349..bbfe1c8fbc161 100644 --- a/filter/tex/pix.php +++ b/filter/tex/pix.php @@ -1,8 +1,10 @@ -libdir.'/filelib.php'); require_once($CFG->dirroot.'/filter/tex/lib.php'); require_once($CFG->dirroot.'/filter/tex/latex.php'); @@ -75,4 +74,3 @@ echo "Please turn on debug mode in site configuration to see more info here."; } } -?> diff --git a/lib/setup.php b/lib/setup.php index 23a4175f162f0..18eb6c28e276f 100644 --- a/lib/setup.php +++ b/lib/setup.php @@ -161,6 +161,14 @@ */ global $SCRIPT; +// Scripts may request no debug and error messages in output +// please note it must be defined before including the config.php script +// and in some cases you also need to set custom default exception handler +if (!defined('NO_DEBUG_DISPLAY')) { + define('NO_DEBUG_DISPLAY', false); +} + +/// wwwroot is mandatory if (!isset($CFG->wwwroot)) { trigger_error('Fatal: $CFG->wwwroot is not configured! Exiting.'); die; @@ -354,27 +362,26 @@ unset($originaldatabasedebug); error_reporting($CFG->debug); -/// find out if PHP cofigured to display warnings + // find out if PHP cofigured to display warnings, + // this is a security problem because some moodle scripts may + // disclose sensitive information if (ini_get_bool('display_errors')) { define('WARN_DISPLAY_ERRORS_ENABLED', true); } -/// If we want to display Moodle errors, then try and set PHP errors to match + // If we want to display Moodle errors, then try and set PHP errors to match if (!isset($CFG->debugdisplay)) { - //keep it as is during installation + // keep it "as is" during installation + } else if (NO_DEBUG_DISPLAY) { + // some parts of Moodle cannot display errors and debug at all. + @ini_set('display_errors', '0'); + @ini_set('log_errors', '1'); } else if (empty($CFG->debugdisplay)) { @ini_set('display_errors', '0'); @ini_set('log_errors', '1'); } else { + // This is very problematic in XHTML strict mode! @ini_set('display_errors', '1'); } -// Even when users want to see errors in the output, -// some parts of Moodle cannot display them at all. -// (Once we are XHTML strict compliant, debugdisplay -// _must_ go away). - if (defined('MOODLE_SANE_OUTPUT')) { - @ini_set('display_errors', '0'); - @ini_set('log_errors', '1'); - } /// detect unsupported upgrade jump as soon as possible - do not change anything, do not use system functions if (!empty($CFG->version) and $CFG->version < 2007101509) { @@ -685,6 +692,6 @@ function stripslashes_deep($value) { } -/// note: we can not block non utf-8 installatrions here, because empty mysql database +/// note: we can not block non utf-8 installations here, because empty mysql database /// might be converted to utf-8 in admin/index.php during installation diff --git a/lib/setuplib.php b/lib/setuplib.php index a76ea1ae8ee13..2dcbdc3bca92d 100644 --- a/lib/setuplib.php +++ b/lib/setuplib.php @@ -187,7 +187,7 @@ function default_exception_handler($ex) { $info = get_exception_info($ex); if (debugging('', DEBUG_MINIMAL)) { - $logerrmsg = "Default exception handler: ".$info->message.' Debug: '.$info->debuginfo."\n".format_backtrace($info->backtrace); + $logerrmsg = "Default exception handler: ".$info->message.' Debug: '.$info->debuginfo."\n".format_backtrace($info->backtrace, true); error_log($logerrmsg, 0); } @@ -198,7 +198,7 @@ function default_exception_handler($ex) { echo $OUTPUT->fatal_error($info->message, $info->moreinfourl, $info->link, $info->backtrace, $info->debuginfo); } catch (Exception $out_ex) { // default exception handler MUST not throw any exceptions!! - // the problem here is we do not know if page already started or not, we only know that somebody messed up in outputlib or theme + // the problem here is we do not know if page already started or not, we only know that somebody messed up in outputlib or theme // so we just print at least something instead of "Exception thrown without a stack frame in Unknown on line 0":-( echo bootstrap_renderer::early_error_content($info->message, $info->moreinfourl, $info->link, $info->backtrace, $info->debuginfo); $outinfo = get_exception_info($out_ex); diff --git a/lib/weblib.php b/lib/weblib.php index 24927cebf8e21..3046547a8ea9c 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -3140,8 +3140,8 @@ function get_docs_url($path) { * trigger_error() or error_log(). Using echo or print will break XHTML * JS and HTTP headers. * + * It is also possible to define NO_DEBUG_DISPLAY which redirects the message to error_log. * - * @global object * @uses DEBUG_NORMAL * @param string $message a message to print * @param int $level the level at which this debugging statement should show @@ -3164,16 +3164,25 @@ function debugging($message = '', $level = DEBUG_NORMAL, $backtrace = null) { $backtrace = debug_backtrace(); } $from = format_backtrace($backtrace, CLI_SCRIPT); - if ($CFG->debugdisplay || isset($UNITTEST->running)) { - // When the unit tests are running, any call to trigger_error - // is intercepted by the test framework and reported as an exception. - // Therefore, we cannot use trigger_error during unit tests. - // At the same time I do not think we should just discard those messages, - // so displaying them on-screen seems like the only option. (MDL-20398) + if (!empty($UNITTEST->running)) { + // When the unit tests are running, any call to trigger_error + // is intercepted by the test framework and reported as an exception. + // Therefore, we cannot use trigger_error during unit tests. + // At the same time I do not think we should just discard those messages, + // so displaying them on-screen seems like the only option. (MDL-20398) + echo '
' . $message . $from . '
'; + + } else if (NO_DEBUG_DISPLAY) { + // script does not want any errors or debugging in output, + // we send the info to error log instead + error_log('Debugging: ' . $message . $from); + + } else if ($CFG->debugdisplay) { if (!defined('DEBUGGING_PRINTED')) { define('DEBUGGING_PRINTED', 1); // indicates we have printed something } echo '
' . $message . $from . '
'; + } else { trigger_error($message . $from, E_USER_NOTICE); } @@ -3181,17 +3190,6 @@ function debugging($message = '', $level = DEBUG_NORMAL, $backtrace = null) { return true; } -/** - * Disable debug messages from debugging(), while keeping PHP error reporting level as is. - * - * @global object - */ -function disable_debugging() { - global $CFG; - $CFG->debug = $CFG->debug | 0x80000000; // switch the sign bit in integer number ;-) -} - - /** * Returns string to add a frame attribute, if required * diff --git a/mod/glossary/exportfile.php b/mod/glossary/exportfile.php index 5589bbf48b064..9df8ddd60633b 100644 --- a/mod/glossary/exportfile.php +++ b/mod/glossary/exportfile.php @@ -1,11 +1,11 @@ \ No newline at end of file diff --git a/pluginfile.php b/pluginfile.php index c98058754d41e..d47b5c47309aa 100644 --- a/pluginfile.php +++ b/pluginfile.php @@ -24,12 +24,12 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +// disable moodle specific debug messages and any errors in output +define('NO_DEBUG_DISPLAY', true); + require_once('config.php'); require_once('lib/filelib.php'); -// disable moodle specific debug messages -disable_debugging(); - $relativepath = get_file_argument(); $forcedownload = optional_param('forcedownload', 0, PARAM_BOOL); diff --git a/question/file.php b/question/file.php index b2d3681548001..b6ea0470473eb 100644 --- a/question/file.php +++ b/question/file.php @@ -1,4 +1,4 @@ -wwwroot); //this is not displayed on IIS?? } -?> + diff --git a/rss/file.php b/rss/file.php index 35cac5fe74085..e13d664e13ef8 100644 --- a/rss/file.php +++ b/rss/file.php @@ -1,4 +1,4 @@ -libdir.'/filelib.php'); @@ -24,9 +26,6 @@ // this is a big one big hack - NO_MOODLE_COOKIES is not compatible with capabilities MDL-7243 // it should be replaced once we get to codes in urls - // disable moodle specific debug messages - disable_debugging(); - $relativepath = get_file_argument(); @@ -114,4 +113,4 @@ function rss_not_found() { global $lifetime, $filename; send_file(rss_geterrorxmlfile(), $filename, $lifetime, false, true); } -?> + diff --git a/user/grouppix.php b/user/grouppix.php index ebe032f42db17..f74669d191ac7 100644 --- a/user/grouppix.php +++ b/user/grouppix.php @@ -1,16 +1,15 @@ -libdir.'/filelib.php'); - // disable moodle specific debug messages - disable_debugging(); - $relativepath = get_file_argument(); $args = explode('/', trim($relativepath, '/')); @@ -30,4 +29,3 @@ header('HTTP/1.0 404 not found'); print_error('filenotfound', 'error'); //this is not displayed on IIS?? } -?> diff --git a/user/pix.php b/user/pix.php index bcea1e4702614..b333fd9753684 100644 --- a/user/pix.php +++ b/user/pix.php @@ -1,8 +1,11 @@ -libdir.'/filelib.php'); @@ -12,9 +15,6 @@ redirect($OUTPUT->old_icon_url('u/f1')); } - // disable moodle specific debug messages - disable_debugging(); - $relativepath = get_file_argument(); $args = explode('/', trim($relativepath, '/')); @@ -33,4 +33,3 @@ // picture was deleted - use default instead redirect($OUTPUT->old_icon_url('u/f1')); -?> diff --git a/userfile.php b/userfile.php index 04784ae4dd268..3f8f6571165c3 100644 --- a/userfile.php +++ b/userfile.php @@ -24,11 +24,12 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +// disable moodle specific debug messages and any errors in output +define('NO_DEBUG_DISPLAY', true); + require_once('config.php'); require_once('lib/filelib.php'); -// disable moodle specific debug messages -disable_debugging(); $relativepath = get_file_argument(); $forcedownload = optional_param('forcedownload', 0, PARAM_BOOL);