Initial upload to Gitea

This commit is contained in:
antonn committed 2026-11-30 16:32:37 +00:00
1 parent 78193254ad
commit 8478998cae
1485 files changed
+461025 -42348

No files matched your search

@@ -0,0 +1,166 @@
<?php
/*
* SPDX-FileCopyrightText: Copyright 2020 - 2026 grommunio GmbH
* SPDX-FileCopyrightText: Copyright 2016 Kopano and its licensors
* SPDX-FileCopyrightText: Copyright 2005 - 2016 Zarafa B.V. and its licensors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
abstract class Logger {
/**
* @var string
*/
protected $user = '';
/**
* @var array
*/
protected $specialLogUsers = [];
/**
* Only used as a cache value for IsUserInSpecialLogUsers.
*
* @var array
*/
private $isUserInSpecialLogUsers = [];
/**
* Only used as a cache value for IsAuthUserInSpecialLogUsers function.
*
* @var bool
*/
private $isAuthUserInSpecialLogUsers = false;
/**
* Returns the current user.
*
* @return string
*/
public function GetUser() {
return $this->user;
}
/**
* Sets the current user.
*
* @param string $value name of the currently logged-in user
*/
public function SetUser($value) {
$this->user = $value;
}
/**
* Indicates if special log users are known.
*
* @return bool True if we do have to log some specific user. False otherwise.
*/
public function HasSpecialLogUsers() {
return !empty($this->specialLogUsers);
}
/**
* Indicates if the user is in the special log users.
*
* @param string $user
*
* @return bool
*/
public function IsUserInSpecialLogUsers($user) {
if (isset($this->isUserInSpecialLogUsers[$user])) {
return true;
}
if ($this->HasSpecialLogUsers()) {
$specialLogUsers = $this->GetSpecialLogUsers();
if (array_search($user, $specialLogUsers, true) !== false) {
$this->isUserInSpecialLogUsers[$user] = true;
return true;
}
}
return false;
}
/**
* Returns the current special log users array.
*
* @return array
*/
public function GetSpecialLogUsers() {
return $this->specialLogUsers;
}
/**
* Sets the current special log users array.
*/
public function SetSpecialLogUsers(array $value) {
$this->isUserInSpecialLogUsers = []; // reset cache
$this->specialLogUsers = $value;
}
/**
* Check that the current login user is in the special log user array.
* This call is equivalent to `$this->IsUserInSpecialLogUsers($this->GetUser())` at the exception that this
* call uses cache so there won't be more than one check to the specialLogUser for the login user.
*
* @return bool true if user exist in special log user array else false
*/
public function IsAuthUserInSpecialLogUsers() {
if ($this->isAuthUserInSpecialLogUsers) {
return true;
}
if ($this->IsUserInSpecialLogUsers($this->GetUser())) {
$this->isAuthUserInSpecialLogUsers = true;
return true;
}
return false;
}
/**
* Logs a message with a given log level.
*
* @param int $logLevel The log level which will be configured in config file
* @param string $message The log message which we want to log in user specific log file
* @param mixed $detailMessage (optional) The detailed log message. it can be Error/Exception array.
* @param mixed $request (optional) The request data sent by the user
*/
public function Log($logLevel, $message, $detailMessage = false, $request = false) {
if ($logLevel <= LOG_USER_LEVEL) {
if ($this->IsAuthUserInSpecialLogUsers()) {
$this->Write($logLevel, $message, $detailMessage, $request);
}
}
}
/**
* Returns the string representation of the given $loglevel.
*
* @param int $loglevel one of the LOGLEVELs
*
* @return string
*/
protected function GetLogLevelString($loglevel) {
return match ($loglevel) {
LOGLEVEL_OFF => "",
LOGLEVEL_FATAL => "[FATAL]",
LOGLEVEL_ERROR => "[ERROR]",
LOGLEVEL_WARN => "[WARN]",
LOGLEVEL_INFO => "[INFO]",
LOGLEVEL_DEBUG => "[DEBUG]",
default => "",
};
}
/**
* Writes a log message to the general log.
*
* @param int $logLevel The log level which will be configured in config file
* @param string $message The log message which we want to log in user specific log file
* @param mixed $detailMessage (optional) The detailed log message. it can be Error/Exception array.
* @param mixed $request (optional) The request data sent by the user
*/
abstract protected function Write($logLevel, $message, $detailMessage, $request);
}
@@ -0,0 +1,95 @@
<?php
/*
* SPDX-FileCopyrightText: Copyright 2020 - 2026 grommunio GmbH
* SPDX-FileCopyrightText: Copyright 2016 Kopano and its licensors
* SPDX-FileCopyrightText: Copyright 2005 - 2016 Zarafa B.V. and its licensors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
class FileLog extends Logger {
/**
* @var string
*/
private $logToUserFile = "";
/**
* Writes a log message to the general log.
*
* @param int $logLevel one of the defined LOGLEVELS
* @param string $message The log message which we want to log in user specific log file
* @param mixed $detailMessage (optional) The detailed log message. it can be Error/Exception array.
* @param mixed $request (optional) The request data sent by the user
*/
protected function Write($logLevel, $message, $detailMessage = false, $request = false) {
$dir = LOG_FILE_DIR;
if (!str_ends_with(LOG_FILE_DIR, "/")) {
$dir .= "/";
}
// If users directory not created then create it first.
if (!is_dir($dir)) {
if (empty($dir)) {
error_log("Log directory has not configured. provide valid directory path.");
return;
}
if (mkdir($dir, 0777, true) === false) {
error_log("Problem in creating log folder " . $dir);
return;
}
}
$data = $this->BuildLogString($logLevel, $message, $detailMessage, $request) . PHP_EOL;
file_put_contents($dir . $this->getLogToUserFile(), $data, FILE_APPEND);
}
/**
* Get the log user file.
*
* @return string
*/
private function getLogToUserFile() {
if (strlen($this->logToUserFile) == 0) {
$this->setLogToUserFile($this->getUser() . ".log");
}
return $this->logToUserFile;
}
/**
* Set user log-file relative to log directory.
*
* @param string $value
*/
private function setLogToUserFile($value) {
$this->logToUserFile = $value;
}
/**
* Returns the string to be logged.
*
* @param int $loglevel one of the defined LOGLEVELS
* @param string $message The log message which we want to log in user specific log file
* @param mixed $detailMessage (optional) The detailed log message. it can be Error/Exception array.
* @param mixed $request (optional) The request data sent by the user
*
* @return string
*/
public function BuildLogString($loglevel, $message, $detailMessage = false, $request = false) {
$dateTime = date("d-M-Y H:i:s");
$log = "[" . $dateTime . "] ";
$log .= $this->GetLogLevelString($loglevel);
$log .= ' ' . $message;
if ($detailMessage) {
$log .= ' :' . var_export($detailMessage, true) . "\r\n";
}
if ($request) {
$log .= ' Request:' . var_export($request, true) . "\r\n";
}
return $log;
}
}