openrat-cms

OpenRat Content Management System
git clone http://git.code.weiherhei.de/openrat-cms.git
Log | Files | Refs | README

commit 938a500f63532accbabfa9fcc6cd624df1c25f2e
parent d3b49a85d0c68f777562c00adff3350fc99c45d1
Author: Jan Dankert <develop@jandankert.de>
Date:   Thu, 19 Nov 2020 19:51:26 +0100

Fix: Using a stream for log output (like php://stdout)

Diffstat:
Mmodules/cms/Dispatcher.class.php | 3++-
Mmodules/logger/Logger.class.php | 14+++++++++-----
Mmodules/util/FileUtils.class.php | 21++++++++++++++++++++-
3 files changed, 31 insertions(+), 7 deletions(-)

diff --git a/modules/cms/Dispatcher.class.php b/modules/cms/Dispatcher.class.php @@ -26,6 +26,7 @@ use util\ClassUtils; use util\exception\DatabaseException; use util\exception\ObjectNotFoundException; use util\exception\ValidationException; +use util\FileUtils; use util\Http; use logger\Logger; use LogicException; @@ -196,7 +197,7 @@ class Dispatcher $logFile = $logConfig->get('file',''); // Wenn Logfile relativ angegeben wurde, dann muss dies relativ zum Root der Anwendung sein. - if ( $logFile && $logFile[0] != '/' ) + if ( $logFile && FileUtils::isRelativePath($logFile) ) $logFile = __DIR__ . '/../../' . $logFile; Logger::$messageFormat = $logConfig->get('format',['time','level','host','text']); diff --git a/modules/logger/Logger.class.php b/modules/logger/Logger.class.php @@ -183,14 +183,18 @@ class Logger if ( Logger::$filename ) { - if (!is_writable( Logger::$filename )) { + // Is the file writable? + // Exception: Streams (like php://stdout) are never 'writable' :/ + if ( is_writable( Logger::$filename ) || strpos(Logger::$filename,'://')!==FALSE ) { + // Writing to the logfile + // It's not a good idea to user error_log here because it failed on symlinked files. + $result = file_put_contents( Logger::$filename,$text . "\n", FILE_APPEND ); + if ( $result === FALSE ) + error_log('logfile ' . Logger::$filename . ' is not available for writing'); + } else { error_log('logfile ' . Logger::$filename . ' is not writable'); error_log($text . "\n"); - } else { - // Writing to the logfile - // It's not a good idea to user error_log here because it failed on symlinked files. - file_put_contents( Logger::$filename,$text . "\n", FILE_APPEND ); } } diff --git a/modules/util/FileUtils.class.php b/modules/util/FileUtils.class.php @@ -110,9 +110,18 @@ class FileUtils } + /** + * Is the given path an absolute path? + * + * @param $path + * @return bool + */ public static function isAbsolutePath($path) { - return @$path[0] == '/'; + return $path && + @$path[0] == '/' || // beginning with '/' (absolute file path) + substr($path,0,4)=='php:' || // beginning with 'php:' (PHP-Streams) + strpos($path,'://') !== FALSE; // containing '://' (URLs) } @@ -121,6 +130,7 @@ class FileUtils $pathElements = array_map(function ($path) { return trim($path, '/'); }, $pathElements); + return array_reduce($pathElements, function ($path, $item) { return $path . ($item ? '/' . $item : ''); }, ''); @@ -131,4 +141,13 @@ class FileUtils { return '.' . self::toAbsolutePath($pathElements); } + + + /** + * @param $path + * @return bool + */ + public static function isRelativePath($path ) { + return ! self::isAbsolutePath($path); + } }