From b56b56ccb6927eb2fd37202c771633c6ee1b5022 Mon Sep 17 00:00:00 2001 From: Philipp Holzer Date: Sun, 26 May 2019 14:33:09 +0200 Subject: [PATCH] friendica-6379 Monolog - logfile permission denied --- src/Factory/LoggerFactory.php | 37 +++++++++++++++++++----------- src/Module/Admin/Logs/Settings.php | 6 +++++ src/Module/Admin/Summary.php | 17 ++++++++++++++ 3 files changed, 47 insertions(+), 13 deletions(-) diff --git a/src/Factory/LoggerFactory.php b/src/Factory/LoggerFactory.php index 444a98cde..bdd85cf3a 100644 --- a/src/Factory/LoggerFactory.php +++ b/src/Factory/LoggerFactory.php @@ -26,6 +26,7 @@ class LoggerFactory { /** * A list of classes, which shouldn't get logged + * * @var array */ private static $ignoreClassList = [ @@ -37,8 +38,8 @@ class LoggerFactory /** * Creates a new PSR-3 compliant logger instances * - * @param string $channel The channel of the logger instance - * @param Configuration $config The config + * @param string $channel The channel of the logger instance + * @param Configuration $config The config * @param Profiler $profiler The profiler of the app * * @return LoggerInterface The PSR-3 compliant logger instance @@ -55,8 +56,8 @@ class LoggerFactory } $introspection = new Introspection(self::$ignoreClassList); - $level = $config->get('system', 'loglevel'); - $loglevel = self::mapLegacyConfigDebugLevel((string)$level); + $level = $config->get('system', 'loglevel'); + $loglevel = self::mapLegacyConfigDebugLevel((string)$level); switch ($config->get('system', 'logger_config', 'stream')) { case 'monolog': @@ -71,7 +72,10 @@ class LoggerFactory $stream = $config->get('system', 'logfile'); - static::addStreamHandler($logger, $stream, $loglevel); + // just add a stream in case it's either writable or not file + if (!is_file($stream) || is_writable($stream)) { + static::addStreamHandler($logger, $stream, $loglevel); + } break; case 'syslog': @@ -81,7 +85,12 @@ class LoggerFactory case 'stream': default: $stream = $config->get('system', 'logfile'); - $logger = new StreamLogger($channel, $stream, $introspection, $loglevel); + // just add a stream in case it's either writable or not file + if (!is_file($stream) || is_writable($stream)) { + $logger = new StreamLogger($channel, $stream, $introspection, $loglevel); + } else { + $logger = new VoidLogger(); + } break; } @@ -105,8 +114,8 @@ class LoggerFactory * * It should never get filled during normal usage of Friendica * - * @param string $channel The channel of the logger instance - * @param Configuration $config The config + * @param string $channel The channel of the logger instance + * @param Configuration $config The config * @param Profiler $profiler The profiler of the app * * @return LoggerInterface The PSR-3 compliant logger instance @@ -120,7 +129,8 @@ class LoggerFactory $stream = $config->get('system', 'dlogfile'); $developerIp = $config->get('system', 'dlogip'); - if (!isset($developerIp) || !$debugging) { + if ((!isset($developerIp) || !$debugging) && + (!is_file($stream) || is_writable($stream))) { $logger = new VoidLogger(); Logger::setDevLogger($logger); return $logger; @@ -149,7 +159,7 @@ class LoggerFactory break; case 'syslog': - $logger = new SyslogLogger($channel, $introspection, LogLevel::DEBUG); + $logger = new SyslogLogger($channel, $introspection, LogLevel::DEBUG); break; case 'stream': @@ -172,6 +182,7 @@ class LoggerFactory /** * Mapping a legacy level to the PSR-3 compliant levels + * * @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#5-psrlogloglevel * * @param string $level the level to be mapped @@ -208,9 +219,9 @@ class LoggerFactory /** * Adding a handler to a given logger instance * - * @param LoggerInterface $logger The logger instance - * @param mixed $stream The stream which handles the logger output - * @param string $level The level, for which this handler at least should handle logging + * @param LoggerInterface $logger The logger instance + * @param mixed $stream The stream which handles the logger output + * @param string $level The level, for which this handler at least should handle logging * * @return void * diff --git a/src/Module/Admin/Logs/Settings.php b/src/Module/Admin/Logs/Settings.php index d55618454..b9a5e3832 100644 --- a/src/Module/Admin/Logs/Settings.php +++ b/src/Module/Admin/Logs/Settings.php @@ -22,6 +22,12 @@ class Settings extends BaseAdminModule $debugging = !empty($_POST['debugging']); $loglevel = defaults($_POST, 'loglevel', LogLevel::ERROR); + if (is_file($logfile) && + !is_writeable($logfile)) { + notice(L10n::t('The logfile \'%s\' is not writable. No logging possible', $logfile)); + return; + } + Config::set('system', 'logfile', $logfile); Config::set('system', 'debugging', $debugging); Config::set('system', 'loglevel', $loglevel); diff --git a/src/Module/Admin/Summary.php b/src/Module/Admin/Summary.php index 98fd74fb3..6261ac75c 100644 --- a/src/Module/Admin/Summary.php +++ b/src/Module/Admin/Summary.php @@ -74,6 +74,23 @@ class Summary extends BaseAdminModule $well_known, $well_known, $a->getBaseURL() . '/help/Install'); } + // Check logfile permission + if (Config::get('system', 'debugging')) { + $stream = Config::get('system', 'logfile'); + + if (is_file($stream) && + !is_writeable($stream)) { + $warningtext[] = L10n::t('The logfile \'%s\' is not writable. No logging possible', $stream); + } + + $stream = Config::get('system', 'dlogfile'); + + if (is_file($stream) && + !is_writeable($stream)) { + $warningtext[] = L10n::t('The logfile \'%s\' is not writable. No logging possible', $stream); + } + } + // check legacy basepath settings $configLoader = new ConfigFileLoader($a->getBasePath(), $a->getMode()); $configCache = new Config\Cache\ConfigCache();