web-base/Core/API/LogsAPI.class.php

129 lines
4.1 KiB
PHP
Raw Normal View History

2022-06-01 11:20:24 +02:00
<?php
2022-11-18 18:06:46 +01:00
namespace Core\API {
2022-06-01 11:20:24 +02:00
2022-11-18 18:06:46 +01:00
use Core\Objects\Context;
2022-06-01 11:20:24 +02:00
abstract class LogsAPI extends Request {
2022-06-20 19:52:31 +02:00
public function __construct(Context $context, bool $externalCall = false, array $params = array()) {
parent::__construct($context, $externalCall, $params);
2022-06-01 11:20:24 +02:00
}
}
}
2022-11-18 18:06:46 +01:00
namespace Core\API\Logs {
2022-06-01 11:20:24 +02:00
2022-11-18 18:06:46 +01:00
use Core\API\LogsAPI;
use Core\API\Parameter\Parameter;
use Core\API\Parameter\StringType;
use Core\Driver\Logger\Logger;
use Core\Driver\SQL\Column\Column;
use Core\Driver\SQL\Condition\Compare;
use Core\Driver\SQL\Condition\CondIn;
2023-01-16 21:47:23 +01:00
use Core\Driver\SQL\Query\Insert;
2022-11-18 18:06:46 +01:00
use Core\Objects\Context;
2023-01-16 21:47:23 +01:00
use Core\Objects\DatabaseEntity\Group;
2022-11-18 18:06:46 +01:00
use Core\Objects\DatabaseEntity\SystemLog;
2022-06-01 11:20:24 +02:00
class Get extends LogsAPI {
2022-06-20 19:52:31 +02:00
public function __construct(Context $context, bool $externalCall = false) {
parent::__construct($context, $externalCall, [
2022-06-01 11:20:24 +02:00
"since" => new Parameter("since", Parameter::TYPE_DATE_TIME, true),
"severity" => new StringType("severity", 32, true, "debug")
]);
2022-06-20 19:52:31 +02:00
$this->csrfTokenRequired = false;
2022-06-01 11:20:24 +02:00
}
protected function _execute(): bool {
$since = $this->getParam("since");
2022-06-20 19:52:31 +02:00
$sql = $this->context->getSQL();
2022-06-01 11:20:24 +02:00
$severity = strtolower(trim($this->getParam("severity")));
$shownLogLevels = Logger::LOG_LEVELS;
$logLevel = array_search($severity, Logger::LOG_LEVELS, true);
if ($logLevel === false) {
return $this->createError("Invalid severity. Allowed values: " . implode(",", Logger::LOG_LEVELS));
} else if ($logLevel > 0) {
$shownLogLevels = array_slice(Logger::LOG_LEVELS, $logLevel);
}
2022-11-20 17:13:53 +01:00
$query = SystemLog::createBuilder($sql, false)
2022-06-01 11:20:24 +02:00
->orderBy("timestamp")
->descending();
if ($since !== null) {
$query->where(new Compare("timestamp", $since, ">="));
}
if ($logLevel > 0) {
$query->where(new CondIn(new Column("severity"), $shownLogLevels));
}
2022-11-20 17:13:53 +01:00
$logEntries = SystemLog::findBy($query);
2022-06-20 19:52:31 +02:00
$this->success = $logEntries !== false;
2022-06-01 11:20:24 +02:00
$this->lastError = $sql->getLastError();
if ($this->success) {
2022-06-20 19:52:31 +02:00
$this->result["logs"] = [];
foreach ($logEntries as $logEntry) {
$this->result["logs"][] = $logEntry->jsonSerialize();
}
2022-06-01 11:20:24 +02:00
} else {
// we couldn't fetch logs from database, return a message and proceed to log files
$this->result["logs"] = [
[
"id" => "fetch-fail",
"module" => "LogsAPI",
"message" => "Failed retrieving logs from database: " . $this->lastError,
"severity" => "error",
"timestamp" => (new \DateTime())->format(Parameter::DATE_TIME_FORMAT)
]
];
}
// get all log entries from filesystem (if database failed)
2022-12-04 13:21:40 +01:00
$logPath = realpath(implode(DIRECTORY_SEPARATOR, [WEBROOT, "Site", "Logs"]));
2022-06-01 11:20:24 +02:00
if ($logPath) {
$index = 1;
foreach (scandir($logPath) as $fileName) {
$logFile = $logPath . DIRECTORY_SEPARATOR . $fileName;
// {module}_{severity}_{date}_{time}_{ms}.log
if (preg_match("/^(\w+)_(\w+)_((\d+-\d+-\d+_){2}\d+)\.log$/", $fileName, $matches) && is_file($logFile)) {
$content = @file_get_contents($logFile);
$date = \DateTime::createFromFormat(Logger::LOG_FILE_DATE_FORMAT, $matches[3]);
if ($content && $date) {
// filter log date
if ($since !== null && datetimeDiff($date, $since) < 0) {
continue;
}
// filter log level
if (!in_array(trim(strtolower($matches[2])), $shownLogLevels)) {
continue;
}
$this->result["logs"][] = [
"id" => "file-" . ($index++),
"module" => $matches[1],
"severity" => $matches[2],
"message" => $content,
"timestamp" => $date->format(Parameter::DATE_TIME_FORMAT)
];
}
}
}
}
return true;
}
2023-01-16 21:47:23 +01:00
public static function getDefaultACL(Insert $insert): void {
$insert->addRow(self::getEndpoint(), [Group::ADMIN], "Allows users to fetch system logs");
}
2022-06-01 11:20:24 +02:00
}
}