-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathEnvelopeAssembler.php
More file actions
185 lines (164 loc) · 6.22 KB
/
EnvelopeAssembler.php
File metadata and controls
185 lines (164 loc) · 6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Libresign\Service\File;
use DateTimeInterface;
use OCA\Libresign\Db\File;
use OCA\Libresign\Db\FileMapper;
use OCA\Libresign\Db\SignRequestMapper;
use OCA\Libresign\Service\FileElementService;
use OCA\Libresign\Service\IdentifyMethodService;
use OCP\Files\IRootFolder;
use OCP\IURLGenerator;
use Psr\Log\LoggerInterface;
class EnvelopeAssembler {
public function __construct(
private SignRequestMapper $signRequestMapper,
private IdentifyMethodService $identifyMethodService,
private FileMapper $fileMapper,
private IRootFolder $root,
private IURLGenerator $urlGenerator,
private SignersLoader $signersLoader,
private ?CertificateChainService $certificateChainService,
private \OCA\Libresign\Handler\SignEngine\Pkcs12Handler $pkcs12Handler,
private LoggerInterface $logger,
private FileElementService $fileElementService,
) {
}
public function buildEnvelopeChildData(File $childFile, \OCA\Libresign\Service\File\FileResponseOptions $options): \stdClass {
$fileData = new \stdClass();
$fileData->id = $childFile->getId();
$fileData->uuid = $childFile->getUuid();
$fileData->name = $childFile->getName();
$fileData->status = $childFile->getStatus();
$fileData->statusText = $this->fileMapper->getTextOfStatus($childFile->getStatus());
$fileData->nodeId = $childFile->getNodeId();
$fileData->file = $this->urlGenerator->linkToRoute('libresign.page.getPdf', ['uuid' => $childFile->getUuid()]);
$childMetadata = $childFile->getMetadata() ?? [];
$fileData->totalPages = (int)($childMetadata['p'] ?? 0);
$fileData->pdfVersion = (string)($childMetadata['pdfVersion'] ?? '');
$childMetadata = ValidationMetadataNormalizer::normalize($childMetadata, $childFile->getName(), $fileData->totalPages);
$fileData->metadata = $childMetadata;
$nodeId = $childFile->getSignedNodeId() ?: $childFile->getNodeId();
$fileNode = $this->root->getUserFolder($childFile->getUserId())->getFirstNodeById($nodeId);
if ($fileNode instanceof \OCP\Files\File) {
if (method_exists($fileNode, 'getSize')) {
$fileData->size = $fileNode->getSize();
}
if (method_exists($fileNode, 'getMimeType')) {
$fileData->mime = $fileNode->getMimeType();
}
}
$fileData->size ??= 0;
$fileData->signers = [];
$fileData->visibleElements = [];
$signRequests = $this->signRequestMapper->getByFileId($childFile->getId());
if (empty($signRequests)) {
return $fileData;
}
$signRequestIds = array_column(array_map(fn ($sr) => ['id' => $sr->getId()], $signRequests), 'id');
$identifyMethodsBatch = $this->identifyMethodService
->setIsRequest(false)
->getIdentifyMethodsFromSignRequestIds($signRequestIds);
foreach ($signRequests as $signRequest) {
$identifyMethods = $identifyMethodsBatch[$signRequest->getId()] ?? [];
$identifyMethodsArray = [];
$signerUid = null;
foreach ($identifyMethods as $methods) {
foreach ($methods as $identifyMethod) {
$entity = $identifyMethod->getEntity();
$identifyMethodsArray[] = [
'method' => $entity->getIdentifierKey(),
'value' => $entity->getIdentifierValue(),
'mandatory' => $entity->getMandatory(),
];
$signerUid ??= $entity->getUniqueIdentifier();
}
}
$email = '';
foreach ($identifyMethods[IdentifyMethodService::IDENTIFY_EMAIL] ?? [] as $identifyMethod) {
$entity = $identifyMethod->getEntity();
if ($entity->getIdentifierKey() === IdentifyMethodService::IDENTIFY_EMAIL) {
$email = $entity->getIdentifierValue();
break;
}
}
$signed = null;
if ($signRequest->getSigned()) {
$signed = $signRequest->getSigned()->format(DateTimeInterface::ATOM);
}
$displayName = $signRequest->getDisplayName();
if ($displayName === '' && $email !== '') {
$displayName = $email;
}
$signer = new \stdClass();
$signer->signRequestId = $signRequest->getId();
$signer->displayName = $displayName;
$signer->email = $email;
$signer->uid = $signerUid;
$signer->signed = $signed;
$signer->status = $signRequest->getStatus();
$signer->statusText = $this->signRequestMapper->getTextOfSignerStatus($signRequest->getStatus());
$signer->identifyMethods = $identifyMethodsArray;
$signer->metadata = $signRequest->getMetadata();
$fileData->signers[] = $signer;
}
if ($options->isShowVisibleElements()) {
$childMetadata = $childFile->getMetadata();
foreach ($this->signRequestMapper->getVisibleElementsFromSigners($signRequests) as $row) {
if (empty($row)) {
continue;
}
$fileData->visibleElements = array_merge(
$this->fileElementService->formatVisibleElements($row, $childMetadata),
$fileData->visibleElements
);
}
}
if ($options->isValidateFile() && $childFile->getSignedNodeId()) {
try {
$fileNode = $this->root->getUserFolder($childFile->getUserId())->getFirstNodeById($childFile->getSignedNodeId());
if ($fileNode instanceof \OCP\Files\File) {
if ($this->certificateChainService !== null) {
$certData = $this->certificateChainService->getCertificateChain($fileNode, $childFile, $options);
} else {
$resource = $fileNode->fopen('rb');
if (!is_resource($resource)) {
throw new \RuntimeException('unable to open signed file stream');
}
$sha256 = $this->getSha256FromResource($resource);
rewind($resource);
if ($sha256 === $childFile->getSignedHash()) {
$this->pkcs12Handler->setIsLibreSignFile();
}
$certData = $this->pkcs12Handler->getCertificateChain($resource);
fclose($resource);
}
if (!empty($certData)) {
$this->signersLoader->loadSignersFromCertData($fileData, $certData, $options->getHost());
}
}
} catch (\Exception $e) {
$this->logger->warning('Failed to load envelope child certificate chain: ' . $e->getMessage());
}
}
return $fileData;
}
private function getSha256FromResource($resource): string {
if (!is_resource($resource)) {
return '';
}
$hashContext = hash_init('sha256');
while (!feof($resource)) {
$buffer = fread($resource, 8192);
if ($buffer === false) {
break;
}
hash_update($hashContext, $buffer);
}
return hash_final($hashContext);
}
}