From a5e4cf6a747ebd49162bce207403b4e3b7128e79 Mon Sep 17 00:00:00 2001 From: Roman Date: Sat, 4 May 2024 17:06:39 +0200 Subject: [PATCH] Captcha bugfix + Logger stracktrace improvements --- Core/API/Request.class.php | 2 +- Core/API/Traits/Captcha.trait.php | 4 ++-- Core/API/UserAPI.class.php | 7 +++---- Core/Driver/Logger/Logger.class.php | 28 +++++++++++++++++++--------- index.php | 2 +- 5 files changed, 26 insertions(+), 17 deletions(-) diff --git a/Core/API/Request.class.php b/Core/API/Request.class.php index ffb1434..47f8268 100644 --- a/Core/API/Request.class.php +++ b/Core/API/Request.class.php @@ -321,7 +321,7 @@ abstract class Request { } catch (\Throwable $err) { http_response_code(500); $this->createError($err->getMessage()); - $this->logger->error($err->getMessage()); + $this->logger->severe($err); } $sql->setLastError(""); diff --git a/Core/API/Traits/Captcha.trait.php b/Core/API/Traits/Captcha.trait.php index 23eb4c3..2dd1638 100644 --- a/Core/API/Traits/Captcha.trait.php +++ b/Core/API/Traits/Captcha.trait.php @@ -8,8 +8,8 @@ use Core\Objects\Context; trait Captcha { - function addCaptchaParameters(array &$parameters): void { - $settings = $this->context->getSettings(); + function addCaptchaParameters(Context $context, array &$parameters): void { + $settings = $context->getSettings(); if ($settings->isCaptchaEnabled()) { $parameters["captcha"] = new StringType("captcha"); } diff --git a/Core/API/UserAPI.class.php b/Core/API/UserAPI.class.php index 94253b8..949457b 100644 --- a/Core/API/UserAPI.class.php +++ b/Core/API/UserAPI.class.php @@ -756,8 +756,7 @@ namespace Core\API\User { "confirmPassword" => new StringType("confirmPassword"), ); - $this->addCaptchaParameters($parameters); - + $this->addCaptchaParameters($context, $parameters); parent::__construct($context, $externalCall, $parameters); $this->csrfTokenRequired = false; } @@ -1033,7 +1032,7 @@ namespace Core\API\User { 'email' => new Parameter('email', Parameter::TYPE_EMAIL), ]; - $this->addCaptchaParameters($parameters); + $this->addCaptchaParameters($context, $parameters); parent::__construct($context, $externalCall, $parameters); } @@ -1121,7 +1120,7 @@ namespace Core\API\User { 'email' => new Parameter('email', Parameter::TYPE_EMAIL), ); - $this->addCaptchaParameters($parameters); + $this->addCaptchaParameters($context, $parameters); parent::__construct($context, $externalCall, $parameters); } diff --git a/Core/Driver/Logger/Logger.class.php b/Core/Driver/Logger/Logger.class.php index fe34024..ce4035a 100644 --- a/Core/Driver/Logger/Logger.class.php +++ b/Core/Driver/Logger/Logger.class.php @@ -40,8 +40,11 @@ class Logger { $this->lastLevel = null; } - protected function getStackTrace(int $pop = 2): string { - $debugTrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); + protected function getStackTrace(int $pop = 2, ?array $debugTrace = null): string { + if ($debugTrace === null) { + $debugTrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); + } + if ($pop > 0) { array_splice($debugTrace, 0, $pop); } @@ -54,10 +57,17 @@ class Logger { }, $debugTrace)); } - public function log(string $message, string $severity, bool $appendStackTrace = true): void { + public function log(string|\Throwable $logEntry, string $severity, bool $appendStackTrace = true): void { + + $debugTrace = null; + $message = $logEntry; + if ($message instanceof \Throwable) { + $message = $logEntry->getMessage(); + $debugTrace = $logEntry->getTrace(); + } if ($appendStackTrace) { - $message .= "\n" . $this->getStackTrace(); + $message .= "\n" . $this->getStackTrace(2, $debugTrace); } $this->lastMessage = $message; @@ -87,27 +97,27 @@ class Logger { @file_put_contents($logPath, $message); } - public function error(string $message): string { + public function error(string|\Throwable $message): string { $this->log($message, "error"); return $message; } - public function severe(string $message): string { + public function severe(string|\Throwable $message): string { $this->log($message, "severe"); return $message; } - public function warning(string $message): string { + public function warning(string|\Throwable $message): string { $this->log($message, "warning", false); return $message; } - public function info(string $message): string { + public function info(string|\Throwable $message): string { $this->log($message, "info", false); return $message; } - public function debug(string $message, bool $appendStackTrace = false): string { + public function debug(string|\Throwable $message, bool $appendStackTrace = false): string { $this->log($message, "debug", $appendStackTrace); return $message; } diff --git a/index.php b/index.php index 9f1260f..d26daed 100644 --- a/index.php +++ b/index.php @@ -90,7 +90,7 @@ if ($installation) { } } catch (\Throwable $e) { http_response_code(500); - $logger->error($e->getMessage()); + $logger->severe($e); $response = $router->returnStatusCode(500); } }