From 1c1ffd3b9695db17000262a6497243dbb43cfe40 Mon Sep 17 00:00:00 2001 From: Damien Regad Date: Sun, 5 May 2019 18:34:16 +0200 Subject: [PATCH] log_event() create log file if it does not exist (#1509) log_event() create log file if it does not exist This is a regression from issue #19642. When MantisBT tries to write an event to a log file that does not already exist, a warning is printed and the file is not created even if PHP process has write access to the directory. The event is added to the PHP system log. Instead of relying on is_writable(), the code now calls error_log() with errors suppressed, then checks the result of the function call to determine if the operation was successful or not. Fixes #25734 --- core/logging_api.php | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/core/logging_api.php b/core/logging_api.php index 73c0d5a577..945327420f 100644 --- a/core/logging_api.php +++ b/core/logging_api.php @@ -107,12 +107,16 @@ function log_event( $p_level, $p_msg ) { break; case 'file': if( isset( $t_modifiers ) ) { - # Detect if the log file is writable; if not, issue a one-time warning static $s_log_writable = null; - if( $s_log_writable === null ) { - $s_log_writable = is_writable( $t_modifiers ); + + if( $s_log_writable ) { + error_log( $t_php_event . PHP_EOL, 3, $t_modifiers ); + } elseif( $s_log_writable === null ) { + # Try to log the event, suppress errors in case the file is not writable + $s_log_writable = @error_log( $t_php_event . PHP_EOL, 3, $t_modifiers ); + if( !$s_log_writable ) { - # Display warning message and write it in PHP system log as well. + # Display a one-time warning message and write it to PHP system log as well. # Note: to ensure the error is shown regardless of $g_display_error settings, # we manually set the message and log it with error_log_delayed(), which will # cause it to be displayed at page bottom. @@ -122,9 +126,6 @@ function log_event( $p_level, $p_msg ) { error_log( 'MantisBT - ' . htmlspecialchars_decode( $t_message ) ); } } - if( $s_log_writable ) { - error_log( $t_php_event . PHP_EOL, 3, $t_modifiers ); - } } break; case 'page': @@ -368,4 +369,4 @@ function log_get_caller( $p_level = null ) { $t_caller = $t_caller_plugin . $t_caller_file . ':' . $t_caller_line . ' ' . $t_caller_class . $t_caller_function; return $t_caller; -} \ No newline at end of file +}