mirror of
https://github.com/LibreSign/libresign.git
synced 2025-12-18 05:20:45 +01:00
This service provides a dedicated, testable way to detect if a PDF already contains signatures. It encapsulates the signature detection logic that was previously scattered across multiple handlers. The service creates a memory resource from PDF content and uses the SignEngineFactory to check for existing certificate chains. It handles exceptions gracefully and returns false for any detection failures. Tests use real PDF fixtures (signed and unsigned) instead of mocks, providing better confidence in the detection logic. Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
48 lines
1.2 KiB
PHP
48 lines
1.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
/**
|
|
* SPDX-FileCopyrightText: 2020-2024 LibreCode coop and contributors
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
*/
|
|
|
|
namespace OCA\Libresign\Service;
|
|
|
|
use OCA\Libresign\Handler\SignEngine\SignEngineFactory;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
class PdfSignatureDetectionService {
|
|
public function __construct(
|
|
private SignEngineFactory $signEngineFactory,
|
|
private LoggerInterface $logger,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* Check if a PDF has existing signatures
|
|
*
|
|
* @param string $pdfContent The PDF file content
|
|
* @return bool True if the file has signatures, false otherwise
|
|
*/
|
|
public function hasSignatures(string $pdfContent): bool {
|
|
$resource = fopen('php://memory', 'r+');
|
|
if ($resource === false) {
|
|
$this->logger->warning('Failed to create resource for signature detection');
|
|
return false;
|
|
}
|
|
|
|
fwrite($resource, $pdfContent);
|
|
rewind($resource);
|
|
|
|
try {
|
|
$engine = $this->signEngineFactory->resolve('pdf');
|
|
$certificates = $engine->getCertificateChain($resource);
|
|
return !empty($certificates);
|
|
} catch (\Throwable $e) {
|
|
$this->logger->debug('Failed to detect signatures: ' . $e->getMessage());
|
|
return false;
|
|
} finally {
|
|
fclose($resource);
|
|
}
|
|
}
|
|
}
|