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,123 @@
<?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\Modules\Crm\EntryPoints;
use Espo\Entities\User;
use Espo\Modules\Crm\Entities\Campaign;
use Espo\Modules\Crm\Entities\EmailQueueItem;
use Espo\Modules\Crm\Entities\MassEmail;
use Espo\Modules\Crm\Tools\Campaign\LogService;
use Espo\Core\Api\Request;
use Espo\Core\Api\Response;
use Espo\Core\EntryPoint\EntryPoint;
use Espo\Core\EntryPoint\Traits\NoAuth;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Exceptions\NotFound;
use Espo\Core\ORM\EntityManager;
class CampaignTrackOpened implements EntryPoint
{
use NoAuth;
public function __construct(
private EntityManager $entityManager,
private LogService $service,
private User $user,
) {}
/**
* @throws BadRequest
* @throws NotFound
*/
public function run(Request $request, Response $response): void
{
$id = $request->getQueryParam('id');
if (!$id) {
throw new BadRequest("No id.");
}
$queueItemId = $id;
/** @var ?EmailQueueItem $queueItem */
$queueItem = $this->entityManager->getEntityById(EmailQueueItem::ENTITY_TYPE, $queueItemId);
if (!$queueItem) {
throw new NotFound("Item not found.");
}
$targetType = $queueItem->getTargetType();
$targetId = $queueItem->getTargetId();
$target = $this->entityManager->getEntityById($targetType, $targetId);
if (!$target) {
return;
}
$massEmail = $queueItem->getMassEmail();
if (!$massEmail) {
return;
}
$campaign = $massEmail->getCampaign();
if (!$campaign) {
return;
}
if ($this->user->isSystem()) {
$this->service->logOpened($campaign->getId(), $queueItem);
}
header('Content-Type: image/png');
$img = imagecreatetruecolor(1, 1);
if (!$img) {
return;
}
imagesavealpha($img, true);
$color = imagecolorallocatealpha($img, 127, 127, 127, 127);
if ($color === false) {
return;
}
imagefill($img, 0, 0, $color);
imagepng($img);
imagecolordeallocate($img, $color);
imagedestroy($img);
}
}

View File

@@ -0,0 +1,212 @@
<?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\Modules\Crm\EntryPoints;
use Espo\Core\Utils\Client\ActionRenderer;
use Espo\Entities\EmailAddress;
use Espo\Modules\Crm\Entities\Campaign;
use Espo\Modules\Crm\Tools\Campaign\LogService;
use Espo\Repositories\EmailAddress as EmailAddressRepository;
use Espo\Modules\Crm\Entities\CampaignTrackingUrl;
use Espo\Modules\Crm\Entities\EmailQueueItem;
use Espo\Core\Api\Request;
use Espo\Core\Api\Response;
use Espo\Core\EntryPoint\EntryPoint;
use Espo\Core\EntryPoint\Traits\NoAuth;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Exceptions\NotFoundSilent;
use Espo\Core\HookManager;
use Espo\Core\ORM\EntityManager;
use Espo\Core\Utils\Hasher;
use Espo\Core\Utils\Metadata;
class CampaignUrl implements EntryPoint
{
use NoAuth;
public function __construct(
private EntityManager $entityManager,
private LogService $service,
private Hasher $hasher,
private HookManager $hookManager,
private Metadata $metadata,
private ActionRenderer $actionRenderer
) {}
/**
* @throws BadRequest
* @throws NotFoundSilent
*/
public function run(Request $request, Response $response): void
{
$queueItemId = $request->getQueryParam('queueItemId') ?? null;
$trackingUrlId = $request->getQueryParam('id') ?? null;
$emailAddress = $request->getQueryParam('emailAddress') ?? null;
$hash = $request->getQueryParam('hash') ?? null;
$uid = $request->getQueryParam('uid') ?? null;
if (!$trackingUrlId) {
throw new BadRequest("No tracking URL ID.");
}
/** @var ?CampaignTrackingUrl $trackingUrl */
$trackingUrl = $this->entityManager->getEntityById(CampaignTrackingUrl::ENTITY_TYPE, $trackingUrlId);
if (!$trackingUrl) {
throw new NotFoundSilent("Tracking URL '$trackingUrlId' not found.");
}
if ($emailAddress && $hash) {
$this->processWithHash($trackingUrl, $emailAddress, $hash);
} else if ($uid && $hash) {
$this->processWithUniqueId($trackingUrl, $uid, $hash);
} else {
if (!$queueItemId) {
throw new BadRequest("No item ID.");
}
/** @var ?EmailQueueItem $queueItem */
$queueItem = $this->entityManager->getEntityById(EmailQueueItem::ENTITY_TYPE, $queueItemId);
if (!$queueItem) {
throw new NotFoundSilent("Item not found.");
}
$this->processWithQueueItem($trackingUrl, $queueItem);
}
if ($trackingUrl->getAction() === CampaignTrackingUrl::ACTION_SHOW_MESSAGE) {
$this->displayMessage($response, $trackingUrl->getMessage());
return;
}
$url = $trackingUrl->getUrl();
if ($url) {
ob_clean();
header('Location: ' . $url);
die;
}
}
private function processWithQueueItem(CampaignTrackingUrl $trackingUrl, EmailQueueItem $queueItem): void
{
$campaign = null;
$targetType = $queueItem->getTargetType();
$targetId = $queueItem->getTargetId();
$target = $this->entityManager->getEntityById($targetType, $targetId);
$campaignId = $trackingUrl->getCampaignId();
if ($campaignId) {
$campaign = $this->entityManager->getEntityById(Campaign::ENTITY_TYPE, $campaignId);
}
if ($target) {
$this->hookManager->process(CampaignTrackingUrl::ENTITY_TYPE, 'afterClick', $trackingUrl, [], [
'targetId' => $targetId,
'targetType' => $targetType,
]);
}
if ($campaign && $target) {
$this->service->logClicked($campaign->getId(), $queueItem, $trackingUrl);
}
}
/**
* @throws NotFoundSilent
*/
private function processWithHash(CampaignTrackingUrl $trackingUrl, string $emailAddress, string $hash): void
{
$hashActual = $this->hasher->hash($emailAddress);
if ($hashActual !== $hash) {
throw new NotFoundSilent();
}
$eaRepository = $this->getEmailAddressRepository();
$ea = $eaRepository->getByAddress($emailAddress);
if (!$ea) {
throw new NotFoundSilent();
}
$entityList = $eaRepository->getEntityListByAddressId($ea->getId());
foreach ($entityList as $target) {
$this->hookManager->process(CampaignTrackingUrl::ENTITY_TYPE, 'afterClick', $trackingUrl, [], [
'targetId' => $target->getId(),
'targetType' => $target->getEntityType(),
]);
}
}
/**
* @throws NotFoundSilent
*/
private function processWithUniqueId(CampaignTrackingUrl $trackingUrl, string $uid, string $hash): void
{
$hashActual = $this->hasher->hash($uid);
if ($hashActual !== $hash) {
throw new NotFoundSilent();
}
$this->hookManager->process(CampaignTrackingUrl::ENTITY_TYPE, 'afterClick', $trackingUrl, [], [
'uid' => $uid,
]);
}
private function displayMessage(Response $response, ?string $message): void
{
$data = [
'message' => $message ?? '',
'view' => $this->metadata->get(['clientDefs', 'Campaign', 'trackingUrlMessageView']),
'template' => $this->metadata->get(['clientDefs', 'Campaign', 'trackingUrlMessageTemplate']),
];
$params = ActionRenderer\Params::create('crm:controllers/tracking-url', 'displayMessage', $data);
$this->actionRenderer->write($response, $params);
}
private function getEmailAddressRepository(): EmailAddressRepository
{
/** @var EmailAddressRepository */
return $this->entityManager->getRepository(EmailAddress::ENTITY_TYPE);
}
}

View File

@@ -0,0 +1,238 @@
<?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\Modules\Crm\EntryPoints;
use Espo\Core\Api\Request;
use Espo\Core\Api\Response;
use Espo\Core\EntryPoint\EntryPoint;
use Espo\Core\EntryPoint\Traits\NoAuth;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Exceptions\Error;
use Espo\Core\Exceptions\NotFound;
use Espo\Core\HookManager;
use Espo\Core\Name\Field;
use Espo\Core\ORM\EntityManager;
use Espo\Core\ORM\Repository\Option\SaveOption;
use Espo\Core\Utils\Client\ActionRenderer;
use Espo\Core\Utils\Language;
use Espo\Core\Utils\Metadata;
use Espo\Entities\Note;
use Espo\Entities\UniqueId;
use Espo\Entities\User;
use Espo\Modules\Crm\Entities\Meeting;
use Espo\ORM\Entity;
class EventConfirmation implements EntryPoint
{
use NoAuth;
private const NOTE_TYPE = 'EventConfirmation';
private const ACTION_ACCEPT = 'accept';
private const ACTION_DECLINE = 'decline';
private const ACTION_TENTATIVE = 'tentative';
public function __construct(
private EntityManager $entityManager,
private HookManager $hookManager,
private ActionRenderer $actionRenderer,
private Metadata $metadata,
private Language $language
) {}
/**
* @throws BadRequest
* @throws Error
*/
public function run(Request $request, Response $response): void
{
$uid = $request->getQueryParam('uid') ?? null;
$action = $request->getQueryParam('action') ?? null;
if (!$uid) {
throw new BadRequest("No uid.");
}
if (!in_array($action, [self::ACTION_ACCEPT, self::ACTION_DECLINE, self::ACTION_TENTATIVE])) {
throw new BadRequest("Bad action.");
}
/** @var ?UniqueId $uniqueId */
$uniqueId = $this->entityManager
->getRDBRepositoryByClass(UniqueId::class)
->where(['name' => $uid])
->findOne();
if (!$uniqueId) {
$this->actionRenderer->write($response, ActionRenderer\Params::create('controllers/base', 'error404'));
return;
}
$data = $uniqueId->getData();
$eventType = $data->eventType ?? null;
$eventId = $data->eventId ?? null;
$inviteeType = $data->inviteeType ?? null;
$inviteeId = $data->inviteeId ?? null;
$link = $data->link ?? null;
$sentDateStart = $data->dateStart ?? null;
$toProcess =
$eventType &&
$eventId &&
$inviteeType &&
$inviteeId &&
$link;
if (!$toProcess) {
throw new Error("Bad data.");
}
if ($sentDateStart !== null && !is_string($sentDateStart)) {
throw new Error("No date in data.");
}
$event = $this->entityManager->getEntityById($eventType, $eventId);
$invitee = $this->entityManager->getEntityById($inviteeType, $inviteeId);
if (!$event || !$invitee) {
$this->actionRenderer->write($response, ActionRenderer\Params::create('controllers/base', 'error404'));
return;
}
$isRelated = $this->entityManager
->getRDBRepository($eventType)
->getRelation($event, $link)
->isRelated($invitee);
$eventStatus = $event->get('status');
if (!$isRelated) {
$eventStatus = Meeting::STATUS_NOT_HELD;
}
if (in_array($eventStatus, [Meeting::STATUS_HELD, Meeting::STATUS_NOT_HELD])) {
$actionData = [
'eventName' => $event->get(Field::NAME),
'translatedEntityType' => $this->language->translateLabel($eventType, 'scopeNames'),
'translatedStatus' => $this->language->translateOption($eventStatus, 'status', $eventType),
'style' => $this->metadata->get(['entityDefs', $eventType, 'fields', 'status', 'style', $eventStatus]),
];
$this->actionRenderer->write(
$response,
ActionRenderer\Params
::create('crm:controllers/event-confirmation', 'confirmEvent', $actionData)
);
return;
}
$status = match($action) {
self::ACTION_ACCEPT => Meeting::ATTENDEE_STATUS_ACCEPTED,
self::ACTION_DECLINE => Meeting::ATTENDEE_STATUS_DECLINED,
self::ACTION_TENTATIVE => Meeting::ATTENDEE_STATUS_TENTATIVE,
default => Meeting::ATTENDEE_STATUS_NONE,
};
$actionData = [
'eventName' => $event->get(Field::NAME),
'eventType' => $event->getEntityType(),
'eventId' => $event->getId(),
'dateStart' => $event->get('dateStart'),
'action' => $action,
'status' => $status,
'link' => $link,
'inviteeType' => $invitee->getEntityType(),
'inviteeId' => $invitee->getId(),
];
$currentStatus = $this->entityManager
->getRDBRepository($eventType)
->getRelation($event, $link)
->getColumn($invitee, 'status');
if ($currentStatus !== $status) {
$this->entityManager
->getRDBRepository($eventType)
->getRelation($event, $link)
->updateColumns($invitee, ['status' => $status]);
if ($this->metadata->get(['scopes', $eventType, 'stream'])) {
$this->createNote($event, $invitee, $status);
}
$this->hookManager->process(
$event->getEntityType(),
'afterConfirmation',
$event,
[],
$actionData
);
}
$actionData['translatedEntityType'] = $this->language->translateLabel($eventType, 'scopeNames');
$actionData['translatedStatus'] = $this->language->translateOption($status, 'acceptanceStatus', $eventType);
$actionData['style'] = $this->metadata
->get(['entityDefs', $eventType, 'fields', 'acceptanceStatus', 'style', $status]);
$actionData['sentDateStart'] = $sentDateStart;
$actionData['statusTranslation'] = $this->language->get([Meeting::ENTITY_TYPE, 'options', 'acceptanceStatus']);
$this->actionRenderer->write(
$response,
ActionRenderer\Params
::create('crm:controllers/event-confirmation', 'confirmEvent', $actionData)
);
}
private function createNote(Entity $entity, Entity $invitee, string $status): void
{
$options = $invitee->getEntityType() === User::ENTITY_TYPE ?
[SaveOption::CREATED_BY_ID => $invitee->getId()] :
[];
$style = $this->metadata
->get(['entityDefs', $entity->getEntityType(), 'fields', 'acceptanceStatus', 'style', $status]);
$this->entityManager->createEntity(Note::ENTITY_TYPE, [
'type' => self::NOTE_TYPE,
'parentId' => $entity->getId(),
'parentType' => $entity->getEntityType(),
'relatedId' => $invitee->getId(),
'relatedType' => $invitee->getEntityType(),
'data' => [
'status' => $status,
'style' => $style,
],
], $options);
}
}

View File

@@ -0,0 +1,108 @@
<?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\Modules\Crm\EntryPoints;
use Espo\Core\Utils\Client\ActionRenderer;
use Espo\Modules\Crm\Tools\MassEmail\UnsubscribeService;
use Espo\Core\Api\Request;
use Espo\Core\Api\Response;
use Espo\Core\EntryPoint\EntryPoint;
use Espo\Core\EntryPoint\Traits\NoAuth;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Exceptions\NotFound;
use Espo\Core\Utils\Metadata;
/**
* @noinspection PhpUnused
*/
class Unsubscribe implements EntryPoint
{
use NoAuth;
public function __construct(
private Metadata $metadata,
private ActionRenderer $actionRenderer,
private UnsubscribeService $unsubscribeService
) {}
/**
* @throws BadRequest
* @throws NotFound
*/
public function run(Request $request, Response $response): void
{
$id = $request->getQueryParam('id') ?? null;
$emailAddress = $request->getQueryParam('emailAddress') ?? null;
$hash = $request->getQueryParam('hash') ?? null;
if ($emailAddress && $hash) {
$this->processWithHash($response, $emailAddress, $hash);
return;
}
if (!$id) {
throw new BadRequest("No id.");
}
$this->display($response, [
'queueItemId' => $id,
'isSubscribed' => $this->unsubscribeService->isSubscribed($id),
]);
}
/**
* @param array<string, mixed> $actionData
*/
private function display(Response $response, array $actionData): void
{
$data = [
'actionData' => $actionData,
'view' => $this->metadata->get(['clientDefs', 'Campaign', 'unsubscribeView']),
'template' => $this->metadata->get(['clientDefs', 'Campaign', 'unsubscribeTemplate']),
];
$params = ActionRenderer\Params::create('crm:controllers/unsubscribe', 'unsubscribe', $data);
$this->actionRenderer->write($response, $params);
}
/**
* @throws NotFound
*/
private function processWithHash(Response $response, string $emailAddress, string $hash): void
{
$this->display($response, [
'emailAddress' => $emailAddress,
'hash' => $hash,
'isSubscribed' => $this->unsubscribeService->isSubscribedWithHash($emailAddress, $hash),
]);
}
}