LSsession::includeFile(): improve logging

This commit is contained in:
Benjamin Renard 2020-08-17 20:04:21 +02:00
parent ee74eed4b8
commit f223827157
2 changed files with 41 additions and 16 deletions

View file

@ -151,33 +151,33 @@ class LSsession {
$path = ($external?'':LS_ROOT_DIR."/").$file; $path = ($external?'':LS_ROOT_DIR."/").$file;
$local_path = ($external?'':LS_ROOT_DIR."/").LS_LOCAL_DIR.$file; $local_path = ($external?'':LS_ROOT_DIR."/").LS_LOCAL_DIR.$file;
$path = (file_exists($local_path)?$local_path:$path); $path = (file_exists($local_path)?$local_path:$path);
if ($path[0] != '/') { if (!isAbsolutePath($path)) {
$found = stream_resolve_include_path($path); $found = stream_resolve_include_path($path);
if ($found === false) { if ($found === false) {
if (!$warn) self :: log(
return false; ($warn?'WARNING':'TRACE'),
$log_msg = "includeFile($file, external=$external) : file $path not found in include path."; "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);
return false; return false;
} }
else { else {
self :: log_trace("includeFile($file, external=$external): file path found using include path => '$found'");
$path = $found; $path = $found;
} }
} }
else if (!file_exists($path)) { else if (!file_exists($path)) {
if (!$warn) self :: log(
return false; ($warn?'WARNING':'TRACE'),
$log_msg = "includeFile($file, external=$external) : file not found ($local_path / $path)"; "includeFile($file, external=$external): file not found ($local_path / $path)"
if (class_exists('LSlog')) );
self :: log_warning($log_msg);
else
error_log($log_msg);
return false; 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;
} }
/** /**

View file

@ -669,6 +669,31 @@ function LSdebugDefined() {
return "unknown : ".(string)$callable; 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 * Dump file content
* *