Initial commit

This commit is contained in:
root
2026-01-19 17:44:46 +01:00
commit 823af8b11d
8721 changed files with 1130846 additions and 0 deletions

View File

@@ -0,0 +1,142 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Log\Handler;
use Espo\Core\Api\Request;
use Espo\Core\ApplicationState;
use Espo\Core\ORM\EntityManagerProxy;
use Espo\Entities\AppLogRecord;
use Monolog\Handler\HandlerInterface;
use Monolog\Level;
use Monolog\LogRecord;
use Throwable;
class DatabaseHandler implements HandlerInterface
{
public function __construct(
private Level $level,
private EntityManagerProxy $entityManager,
private ApplicationState $applicationState
) {}
public function isHandling(LogRecord $record): bool
{
if (!$this->applicationState->hasUser()) {
return false;
}
if ($record->context['isSql'] ?? false) {
return false;
}
return $record->level->value >= $this->level->value;
}
public function handle(LogRecord $record): bool
{
if (!$this->isHandling($record)) {
return false;
}
try {
$logRecord = $this->entityManager->getRDBRepositoryByClass(AppLogRecord::class)->getNew();
$level = ucfirst($record->level->toPsrLogLevel());
$message = $this->interpolate($record, $record->message);
$logRecord
->setLevel($level)
->setMessage($message);
$this->setException($record, $logRecord);
$this->setRequest($record, $logRecord);
$this->entityManager->saveEntity($logRecord);
} catch (Throwable) {
// Nowhere to log.
}
return false;
}
private function interpolate(LogRecord $record, string $line): string
{
$replace = [];
foreach ($record->context as $key => $val) {
if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) {
$replace['{' . $key . '}'] = $val;
}
}
return strtr($line, $replace);
}
/**
* @param LogRecord[] $records
*/
public function handleBatch(array $records): void
{
foreach ($records as $record) {
$this->handle($record);
}
}
public function close(): void
{}
private function setException(LogRecord $record, AppLogRecord $logRecord): void
{
$exception = $record->context['exception'] ?? null;
if (!$exception instanceof Throwable) {
return;
}
$logRecord
->setExceptionClass(get_class($exception))
->setFile($exception->getFile())
->setLine($exception->getLine())
->setCode($exception->getCode());
}
private function setRequest(LogRecord $record, AppLogRecord $logRecord): void
{
$request = $record->context['request'] ?? null;
if (!$request instanceof Request) {
return;
}
$logRecord
->setRequestMethod($request->getMethod())
->setRequestResourcePath($request->getResourcePath());
}
}

View File

@@ -0,0 +1,103 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Log\Handler;
use Espo\Core\Utils\Config;
use Espo\Core\Utils\File\Manager as FileManager;
use Monolog\Handler\StreamHandler as MonologStreamHandler;
use Monolog\Level;
use Monolog\LogRecord;
use RuntimeException;
use Throwable;
class EspoFileHandler extends MonologStreamHandler
{
protected FileManager $fileManager;
protected int $maxErrorMessageLength = 10000;
public function __construct(
Config $config,
string $filename,
Level $level = Level::Debug,
bool $bubble = true
) {
parent::__construct($filename, $level, $bubble);
$defaultPermissions = $config->get('defaultPermissions');
$this->fileManager = new FileManager($defaultPermissions);
}
protected function write(LogRecord $record): void
{
if (!$this->url) {
throw new RuntimeException("Missing a logger file path. Check logger params in config.");
}
try {
if (!is_writable($this->url)) {
$checkFileResult = $this->fileManager->checkCreateFile($this->url);
if (!$checkFileResult) {
return;
}
}
$this->fileManager->appendContents(
$this->url,
$this->pruneMessage($record)
);
} catch (Throwable $e) {
$msg = "Could not write file `$this->url`.";
if ($e->getMessage()) {
$msg .= " Error message: " . $e->getMessage();
}
throw new RuntimeException($msg);
}
}
private function pruneMessage(LogRecord $record): string
{
if (strlen($record->message) <= $this->maxErrorMessageLength) {
return $record->formatted;
}
$message = substr($record->message, 0, $this->maxErrorMessageLength) . '...';
$record = $record->with(message: $message);
return $this->getFormatter()->format($record);
}
}

View File

@@ -0,0 +1,113 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Log\Handler;
use Espo\Core\Utils\Config;
use Monolog\Level;
class EspoRotatingFileHandler extends EspoFileHandler
{
protected string $dateFormat = 'Y-m-d';
protected string $filenameFormat = '{filename}-{date}';
protected string $filename;
protected int $maxFiles;
public function __construct(
Config $config,
string $filename,
int $maxFiles = 0,
Level $level = Level::Debug,
bool $bubble = true
) {
$this->filename = $filename;
$this->maxFiles = $maxFiles;
parent::__construct($config, $this->getTimedFilename(), $level, $bubble);
$this->rotate();
}
protected function rotate(): void
{
if (0 === $this->maxFiles) {
return; // unlimited number of files for 0
}
$filePattern = $this->getFilePattern();
$dirPath = $this->fileManager->getDirName($this->filename);
/** @var string[] $logFiles */
$logFiles = $this->fileManager->getFileList($dirPath, false, $filePattern, true);
if (!empty($logFiles) && count($logFiles) > $this->maxFiles) {
usort($logFiles, function ($a, $b) {
return strcmp($b, $a);
});
$logFilesToBeRemoved = array_slice($logFiles, $this->maxFiles);
$this->fileManager->removeFile($logFilesToBeRemoved, $dirPath);
}
}
protected function getTimedFilename(): string
{
$fileInfo = pathinfo($this->filename);
$timedFilename = str_replace(
['{filename}', '{date}'],
[$fileInfo['filename'], date($this->dateFormat)],
($fileInfo['dirname'] ?? '') . '/' . $this->filenameFormat
);
if (!empty($fileInfo['extension'])) {
$timedFilename .= '.' . $fileInfo['extension'];
}
return $timedFilename;
}
protected function getFilePattern(): string
{
$fileInfo = pathinfo($this->filename);
$glob = str_replace(
['{filename}', '{date}'],
[$fileInfo['filename'], '.*'],
$this->filenameFormat
);
if (!empty($fileInfo['extension'])) {
$glob .= '\.'.$fileInfo['extension'];
}
return '^' . $glob . '$';
}
}