From f223827157360127be1b18ec6e8c5a99e601b8f0 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Mon, 17 Aug 2020 20:04:21 +0200 Subject: [PATCH] LSsession::includeFile(): improve logging --- src/includes/class/class.LSsession.php | 32 +++++++++++++------------- src/includes/functions.php | 25 ++++++++++++++++++++ 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/src/includes/class/class.LSsession.php b/src/includes/class/class.LSsession.php index 278e3161..713387c1 100644 --- a/src/includes/class/class.LSsession.php +++ b/src/includes/class/class.LSsession.php @@ -151,33 +151,33 @@ class LSsession { $path = ($external?'':LS_ROOT_DIR."/").$file; $local_path = ($external?'':LS_ROOT_DIR."/").LS_LOCAL_DIR.$file; $path = (file_exists($local_path)?$local_path:$path); - if ($path[0] != '/') { + if (!isAbsolutePath($path)) { $found = stream_resolve_include_path($path); if ($found === false) { - if (!$warn) - return false; - $log_msg = "includeFile($file, external=$external) : file $path not found in include path."; - if (class_exists('LSlog')) - self :: log_warning($log_msg); - else - error_log($log_msg); + self :: log( + ($warn?'WARNING':'TRACE'), + "includeFile($file, external=$external) : file $path not found in include path." + ); return false; } else { + self :: log_trace("includeFile($file, external=$external): file path found using include path => '$found'"); $path = $found; } } else if (!file_exists($path)) { - if (!$warn) - return false; - $log_msg = "includeFile($file, external=$external) : file not found ($local_path / $path)"; - if (class_exists('LSlog')) - self :: log_warning($log_msg); - else - error_log($log_msg); + self :: log( + ($warn?'WARNING':'TRACE'), + "includeFile($file, external=$external): file not found ($local_path / $path)" + ); return false; } - return include_once($path); + if (!include_once($path)) { + // Always log as warning in this case + self :: log_warning("includeFile($file, external=$external): include_once($path) not returned TRUE"); + return false; + } + return true; } /** diff --git a/src/includes/functions.php b/src/includes/functions.php index a5dfc4e0..a7d8e302 100644 --- a/src/includes/functions.php +++ b/src/includes/functions.php @@ -669,6 +669,31 @@ function LSdebugDefined() { return "unknown : ".(string)$callable; } +/** + * Check if a path is absolute + * + * @param[in] $path string The path + * + * @retval boolean True if path is absolute, False otherwise + */ +function isAbsolutePath($path) { + return strStartWith($path, '/') || strStartWith($path, './') || strStartWith($path, '../'); +} + +/** + * Check if a string start with another specified string + * + * @param[in] $string string The string to search in + * @param[in] $start_string string The starting string to check + * + * @retval boolean True if string start by specified one, False otherwise + */ +function strStartWith($string, $start_string) { + if (strlen($start_string) > strlen($string)) + return false; + return substr($string, 0, strlen($start_string)) === $start_string; +} + /** * Dump file content *