Skip to content

Commit b7149a7

Browse files
committed
feat: custom signature template
Signed-off-by: Vitor Mattos <vitor@php.rio>
1 parent 3771455 commit b7149a7

File tree

7 files changed

+61
-17
lines changed

7 files changed

+61
-17
lines changed

lib/Handler/FooterHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ private function getTemplate(): string {
158158
if ($footerTemplate) {
159159
return $footerTemplate;
160160
}
161-
return (string) file_get_contents(__DIR__ . '/Templates/footer.teig');
161+
return (string)file_get_contents(__DIR__ . '/Templates/footer.teig');
162162
}
163163

164164
private function getQrCodeImageBase64(string $text): string {

lib/Handler/ISignEngineHandler.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ public function setPassword(string $password): self;
1919
public function getPassword(): string;
2020
public function sign(): File;
2121
public function getSignedContent(): string;
22+
public function getSignatureText(): string;
23+
public function setSignatureText(string $text): self;
2224
}

lib/Handler/JSignPdfHandler.php

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -121,18 +121,20 @@ private function signUsingVisibleElements(): string {
121121
$param = $this->getJSignParam();
122122
$originalParam = clone $param;
123123
foreach ($visibleElements as $element) {
124-
$param
125-
->setJSignParameters(
126-
$originalParam->getJSignParameters() .
127-
' -pg ' . $element->getFileElement()->getPage() .
128-
' -llx ' . $element->getFileElement()->getLlx() .
129-
' -lly ' . $element->getFileElement()->getLly() .
130-
' -urx ' . $element->getFileElement()->getUrx() .
131-
' -ury ' . $element->getFileElement()->getUry() .
132-
' --l2-text ""' .
133-
' -V' .
134-
' --bg-path ' . $element->getTempFile()
135-
);
124+
$params = [
125+
'-pg' => $element->getFileElement()->getPage(),
126+
'-llx' => $element->getFileElement()->getLlx(),
127+
'-lly' => $element->getFileElement()->getLly(),
128+
'-urx' => $element->getFileElement()->getUrx(),
129+
'-ury' => $element->getFileElement()->getUry(),
130+
'--l2-text' => $this->getSignatureText(),
131+
'-V' => null,
132+
'--bg-path' => $element->getTempFile(),
133+
];
134+
$param->setJSignParameters(
135+
$originalParam->getJSignParameters() .
136+
$this->listParamsToString($params)
137+
);
136138
$jSignPdf->setParam($param);
137139
$signed = $this->signWrapper($jSignPdf);
138140
$param->setPdf($signed);
@@ -142,6 +144,23 @@ private function signUsingVisibleElements(): string {
142144
return '';
143145
}
144146

147+
public function getSignatureText(): string {
148+
$signatureText = parent::getSignatureText();
149+
$signatureText = '"' . str_replace('"', '\"', $signatureText) . '"';
150+
return $signatureText;
151+
}
152+
153+
private function listParamsToString(array $params): string {
154+
$paramString = '';
155+
foreach ($params as $flag => $value) {
156+
$paramString .= ' ' . $flag;
157+
if ($value !== null && $value !== '') {
158+
$paramString .= ' ' . $value;
159+
}
160+
}
161+
return $paramString;
162+
}
163+
145164
private function signWrapper(JSignPDF $jSignPDF): string {
146165
try {
147166
$param = $this->getJSignParam();

lib/Handler/Pkcs12Handler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function updatePassword(string $uid, string $currentPrivateKey, string $n
8181
return $this->savePfx($uid, $content);
8282
}
8383

84-
public function readCertificate(string $uid, string $privateKey): array {
84+
public function readCertificate(?string $uid = null, string $privateKey = ''): array {
8585
$this->setPassword($privateKey);
8686
$pfx = $this->getPfx($uid);
8787
return $this->certificateEngineHandler->getEngine()->readCertificate(
@@ -422,6 +422,7 @@ public function sign(): File {
422422
->setCertificate($this->getCertificate())
423423
->setInputFile($this->getInputFile())
424424
->setPassword($this->getPassword())
425+
->setSignatureText($this->getSignatureText())
425426
->setVisibleElements($this->getVisibleElements())
426427
->getSignedContent();
427428
$this->getInputFile()->putContent($signedContent);

lib/Handler/SignEngineHandler.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ abstract class SignEngineHandler implements ISignEngineHandler {
1717
private string $password = '';
1818
/** @var VisibleElementAssoc[] */
1919
private array $visibleElements = [];
20+
private string $signatureText = '';
2021

2122
/**
2223
* @return static
@@ -70,4 +71,13 @@ public function getVisibleElements(): array {
7071
public function getSignedContent(): string {
7172
return $this->sign()->getContent();
7273
}
74+
75+
public function getSignatureText(): string {
76+
return $this->signatureText;
77+
}
78+
79+
public function setSignatureText(string $text): self {
80+
$this->signatureText = $text;
81+
return $this;
82+
}
7383
}

lib/Service/SignFileService.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public function __construct(
9797
private ITempManager $tempManager,
9898
private IdentifyMethodService $identifyMethodService,
9999
private ITimeFactory $timeFactory,
100+
private SignatureTextService $signatureTextService,
100101
) {
101102
}
102103

@@ -263,11 +264,18 @@ public function sign(): File {
263264
$pfxFileContent = $this->getPfxFile();
264265
switch (strtolower($fileToSign->getExtension())) {
265266
case 'pdf':
267+
$pfxData = $this->getPfxData();
266268
$signedFile = $this->pkcs12Handler
267269
->setInputFile($fileToSign)
268270
->setCertificate($pfxFileContent)
269271
->setVisibleElements($this->elements)
270272
->setPassword($this->password)
273+
->setSignatureText($this->signatureTextService->parse(context: [
274+
'SignerName' => '\${signer}',
275+
'DocumentUUID' => $this->libreSignFile->getUuid(),
276+
'IssuerCommonName' => $pfxData['issuer']['CN'],
277+
'SignatureDate' => '\${timestamp}',
278+
]))
271279
->sign();
272280
break;
273281
default:
@@ -377,6 +385,10 @@ private function getPfxFile(): string {
377385
return $this->pkcs12Handler->getPfx();
378386
}
379387

388+
private function getPfxData(): array {
389+
return $this->pkcs12Handler->readCertificate(privateKey: $this->password);
390+
}
391+
380392
/**
381393
* Get file to sign
382394
*

lib/Service/SignatureTextService.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ public function save(string $template): string {
2525
return $this->parse($template);
2626
}
2727

28-
public function parse(string $template, array $context = []): string {
28+
public function parse(string $template = '', array $context = []): string {
2929
if (empty($template)) {
3030
$template = $this->appConfig->getAppValueString('signature_text_template');
3131
}
3232
if (empty($context)) {
3333
$context = [
3434
'SignerName' => 'John Doe',
3535
'DocumentUUID' => UUIDUtil::getUUID(),
36-
'CommonName' => 'Acme Cooperative',
36+
'IssuerCommonName' => 'Acme Cooperative',
3737
'SignatureDate' => (new \DateTime())->format(DateTimeInterface::ATOM)
3838
];
3939
}
@@ -45,7 +45,7 @@ public function parse(string $template, array $context = []): string {
4545
->createTemplate($template)
4646
->render($context);
4747
} catch (SyntaxError $e) {
48-
return (string) preg_replace('/in "[^"]+" at line \d+/', '', $e->getMessage());
48+
return (string)preg_replace('/in "[^"]+" at line \d+/', '', $e->getMessage());
4949
}
5050
}
5151
}

0 commit comments

Comments
 (0)