Skip to content

EspoCRM – Open Source CRM Application

PDF template image check

Yurii91a9edbcedb9
diff --git a/application/Espo/Classes/RecordHooks/Template/BeforeSaveValidation.php b/application/Espo/Classes/RecordHooks/Template/BeforeSaveValidation.php
new file mode 100644
index 0000000000..b73e386917
--- /dev/null
+++ b/application/Espo/Classes/RecordHooks/Template/BeforeSaveValidation.php
@@ -0,0 +1,50 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 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\Classes\RecordHooks\Template;
+
+use Espo\Core\Record\Hook\SaveHook;
+use Espo\Entities\Template;
+use Espo\ORM\Entity;
+use Espo\Tools\Pdf\Util\Validation;
+
+/**
+ * @implements SaveHook<Template>
+ */
+class BeforeSaveValidation implements SaveHook
+{
+ public function __construct(
+ private Validation $validation,
+ ) {}
+
+ public function process(Entity $entity): void
+ {
+ $this->validation->process($entity);
+ }
+}
diff --git a/application/Espo/Entities/Template.php b/application/Espo/Entities/Template.php
index 45eca49da9..2e937e3e36 100644
--- a/application/Espo/Entities/Template.php
+++ b/application/Espo/Entities/Template.php
@@ -34,12 +34,19 @@ use UnexpectedValueException;
class Template extends Entity
{
- public const ENTITY_TYPE = 'Template';
+ public const string ENTITY_TYPE = 'Template';
- public const STATUS_ACTIVE = 'Active';
+ public const string STATUS_ACTIVE = 'Active';
public const string FIELD_ENTITY_TYPE = 'entityType';
+ /** @since 10.1.0 */
+ public const string FIELD_BODY = 'body';
+ /** @since 10.1.0 */
+ public const string FIELD_HEADER = 'header';
+ /** @since 10.1.0 */
+ public const string FIELD_FOOTER = 'footer';
+
public function getTargetEntityType(): string
{
$entityType = $this->get(self::FIELD_ENTITY_TYPE);
@@ -60,4 +67,28 @@ class Template extends Entity
{
return $this->get('filename');
}
+
+ /**
+ * since 10.1.0
+ */
+ public function getBody(): ?string
+ {
+ return $this->get(self::FIELD_BODY);
+ }
+
+ /**
+ * since 10.1.0
+ */
+ public function getHeader(): ?string
+ {
+ return $this->get(self::FIELD_HEADER);
+ }
+
+ /**
+ * since 10.1.0
+ */
+ public function getFooter(): ?string
+ {
+ return $this->get(self::FIELD_FOOTER);
+ }
}
diff --git a/application/Espo/Resources/metadata/recordDefs/Template.json b/application/Espo/Resources/metadata/recordDefs/Template.json
index 0cfd9e5e05..2d5a5a59c4 100644
--- a/application/Espo/Resources/metadata/recordDefs/Template.json
+++ b/application/Espo/Resources/metadata/recordDefs/Template.json
@@ -6,5 +6,11 @@
"update": {
"allowed": true
}
- }
+ },
+ "beforeCreateHookClassNameList": [
+ "Espo\\Classes\\RecordHooks\\Template\\BeforeSaveValidation"
+ ],
+ "beforeUpdateHookClassNameList": [
+ "Espo\\Classes\\RecordHooks\\Template\\BeforeSaveValidation"
+ ]
}
diff --git a/application/Espo/Tools/Pdf/Util/Validation.php b/application/Espo/Tools/Pdf/Util/Validation.php
new file mode 100644
index 0000000000..ee7f8a00bb
--- /dev/null
+++ b/application/Espo/Tools/Pdf/Util/Validation.php
@@ -0,0 +1,130 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 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\Tools\Pdf\Util;
+
+use Espo\Core\Acl;
+use Espo\Core\Exceptions\BadRequest;
+use Espo\Core\Exceptions\Forbidden;
+use Espo\Entities\Attachment;
+use Espo\Entities\Template;
+use Espo\ORM\EntityManager;
+use RuntimeException;
+
+/**
+ * @internal
+ */
+class Validation
+{
+ public function __construct(
+ private Acl $acl,
+ private EntityManager $entityManager,
+ ) {}
+
+ /**
+ * @throws BadRequest
+ * @throws Forbidden
+ */
+ public function process(Template $template): void
+ {
+ $fields = [
+ Template::FIELD_BODY,
+ Template::FIELD_HEADER,
+ Template::FIELD_FOOTER,
+ ];
+
+ foreach ($fields as $field) {
+ if (!$template->isAttributeChanged($field)) {
+ continue;
+ }
+
+ $content = $template->get($field);
+
+ if ($content === null) {
+ continue;
+ }
+
+ $previous = $template->getFetched($field);
+
+ if (!is_string($content)) {
+ throw new RuntimeException();
+ }
+
+ if ($previous !== null && !is_string($previous)) {
+ throw new RuntimeException();
+ }
+
+ $this->assertContent($content, $previous);
+ }
+ }
+
+ /**
+ * @throws BadRequest
+ * @throws Forbidden
+ */
+ private function assertContent(string $content, ?string $previous): void
+ {
+ $previousIds = $this->parseIds($previous ?? '');
+ $ids = $this->parseIds($content);
+
+ foreach (array_diff($ids, $previousIds) as $id) {
+ $attachment = $this->entityManager->getRDBRepositoryByClass(Attachment::class)->getById($id);
+
+ if (!$attachment) {
+ throw new BadRequest("Inline attachment '$id' not found. Remove the inline image.");
+ }
+
+ if (!$this->acl->checkEntityRead($attachment)) {
+ throw new Forbidden("No access to inline attachment '$id'.");
+ }
+ }
+ }
+
+ /**
+ * @return string[]
+ */
+ private function parseIds(string $content): array
+ {
+ $ids = [];
+
+ preg_match_all(RegExp::INLINE_ATTACHMENT, $content, $matches, PREG_SET_ORDER);
+
+ foreach ($matches as $match) {
+ $id = $match[1];
+
+ if (!$id) {
+ continue;
+ }
+
+ $ids[] = $id;
+ }
+
+ return $ids;
+ }
+}
diff --git a/tests/unit/Espo/Tools/Pdf/Util/ValidationTest.php b/tests/unit/Espo/Tools/Pdf/Util/ValidationTest.php
new file mode 100644
index 0000000000..c032fffdec
--- /dev/null
+++ b/tests/unit/Espo/Tools/Pdf/Util/ValidationTest.php
@@ -0,0 +1,100 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 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 tests\unit\Espo\Tools\Pdf\Util;
+
+use Espo\Core\Acl;
+use Espo\Entities\Attachment;
+use Espo\Entities\Template;
+use Espo\ORM\EntityManager;
+use Espo\ORM\Repository\RDBRepository;
+use Espo\Tools\Pdf\Util\Validation;
+use PHPUnit\Framework\TestCase;
+
+class ValidationTest extends TestCase
+{
+ /**
+ * @noinspection PhpUnhandledExceptionInspection
+ */
+ public function testValidation(): void
+ {
+ $template = $this->createMock(Template::class);
+
+ $template->expects(self::any())
+ ->method('isAttributeChanged')
+ ->willReturnMap([
+ [Template::FIELD_BODY, true],
+ [Template::FIELD_HEADER, false],
+ [Template::FIELD_FOOTER, false],
+ ]);
+
+ $acl = $this->createMock(Acl::class);
+
+ $entityManager = $this->createMock(EntityManager::class);
+
+ $repo = $this->createMock(RDBRepository::class);
+
+ $entityManager->expects(self::any())
+ ->method('getRDBRepositoryByClass')
+ ->willReturnMap([
+ [Attachment::class, $repo],
+ ]);
+
+ $template->expects(self::any())
+ ->method('get')
+ ->willReturnMap([
+ [Template::FIELD_BODY, "<img src=\"?entryPoint=attachment&id=test-id\">"],
+ ]);
+
+ $template->expects(self::any())
+ ->method('getFetched')
+ ->willReturnMap([
+ [Template::FIELD_BODY, null],
+ ]);
+
+ $attachment = $this->createMock(Attachment::class);
+
+ $repo->expects(self::once())
+ ->method('getById')
+ ->with('test-id')
+ ->willReturn($attachment);
+
+ $acl->expects(self::once())
+ ->method('checkEntityRead')
+ ->with($attachment)
+ ->willReturn(true);
+
+ $validation = new Validation(
+ acl: $acl,
+ entityManager: $entityManager,
+ );
+
+ $validation->process($template);
+ }
+}