libresign/lib/Service/PdfSignatureDetectionService.php
Vitor Mattos cd7db4ea37 feat: add PdfSignatureDetectionService for detecting existing signatures
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>
2025-12-09 00:43:43 +00:00

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);
}
}
}