Captcha bugfix + Logger stracktrace improvements

This commit is contained in:
Roman 2024-05-04 17:06:39 +02:00
parent 2a1db919e8
commit a5e4cf6a74
5 changed files with 26 additions and 17 deletions

@ -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("");

@ -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");
}

@ -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);
}

@ -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;
}

@ -90,7 +90,7 @@ if ($installation) {
}
} catch (\Throwable $e) {
http_response_code(500);
$logger->error($e->getMessage());
$logger->severe($e);
$response = $router->returnStatusCode(500);
}
}