diff --git a/lib/Handler/PhpNativeHandler.php b/lib/Handler/PhpNativeHandler.php new file mode 100644 index 0000000000..b8755dd2e6 --- /dev/null +++ b/lib/Handler/PhpNativeHandler.php @@ -0,0 +1,98 @@ + + * + * @author Vitor Mattos + * + * @license GNU AGPL version 3 or any later version + * + * 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 . + */ + +namespace OCA\Libresign\Handler; + +use OCP\AppFramework\Services\IAppConfig; +use Psr\Log\LoggerInterface; + +class PhpNativeHandler extends SignEngineHandler { + public function __construct( + private IAppConfig $appConfig, + private LoggerInterface $logger, + ) { + } + + /** + * @psalm-suppress MixedReturnStatement + */ + public function sign(): string { + $param = $this->getJSignParam() + ->setCertificate($this->getCertificate()) + ->setPdf($this->getInputFile()->getContent()) + ->setPassword($this->getPassword()); + + $signed = $this->signUsingVisibleElements(); + if ($signed) { + return $signed; + } + $jSignPdf = $this->getJSignPdf(); + $jSignPdf->setParam($param); + return $this->signWrapper($jSignPdf); + } + + private function signUsingVisibleElements(): string { + $visibleElements = $this->getvisibleElements(); + if ($visibleElements) { + $jSignPdf = $this->getJSignPdf(); + $param = $this->getJSignParam(); + foreach ($visibleElements as $element) { + $param + ->setJSignParameters( + $param->getJSignParameters() . + ' -pg ' . $element->getFileElement()->getPage() . + ' -llx ' . $element->getFileElement()->getLlx() . + ' -lly ' . $element->getFileElement()->getLly() . + ' -urx ' . $element->getFileElement()->getUrx() . + ' -ury ' . $element->getFileElement()->getUry() . + ' --l2-text ""' . + ' -V' . + ' --bg-path ' . $element->getTempFile() + ); + $jSignPdf->setParam($param); + $signed = $this->signWrapper($jSignPdf); + } + return $signed; + } + return ''; + } + + private function signWrapper(JSignPDF $jSignPDF): string { + try { + return $jSignPDF->sign(); + } catch (\Throwable $th) { + $rows = str_getcsv($th->getMessage()); + $hashAlgorithm = array_filter($rows, fn ($r) => str_contains($r, 'The chosen hash algorithm')); + if (!empty($hashAlgorithm)) { + $hashAlgorithm = current($hashAlgorithm); + $hashAlgorithm = trim($hashAlgorithm, 'INFO '); + $hashAlgorithm = str_replace('\"', '"', $hashAlgorithm); + $hashAlgorithm = preg_replace('/\.( )/', ".\n", $hashAlgorithm); + throw new LibresignException($hashAlgorithm); + } + $this->logger->error('Error at JSignPdf side. LibreSign can not do nothing. Follow the error message: ' . $th->getMessage()); + throw new \Exception($th->getMessage()); + } + } +} diff --git a/lib/Handler/Pkcs12Handler.php b/lib/Handler/Pkcs12Handler.php index 8794bb231e..5a2e5beabf 100644 --- a/lib/Handler/Pkcs12Handler.php +++ b/lib/Handler/Pkcs12Handler.php @@ -107,8 +107,8 @@ public function getPfx(?string $uid = null): string { } private function getHandler(): SignEngineHandler { - $sign_engine = $this->appConfig->getAppValue('sign_engine', 'JSignPdf'); - $property = lcfirst($sign_engine) . 'Handler'; + $signature_engine = $this->appConfig->getAppValue('signature_engine', 'JSignPdf'); + $property = lcfirst($signature_engine) . 'Handler'; if (!property_exists($this, $property)) { throw new LibresignException($this->l10n->t('Invalid Sign engine.'), 400); } diff --git a/lib/Service/Install/ConfigureCheckService.php b/lib/Service/Install/ConfigureCheckService.php index a519454fa6..6bd1c8cd71 100644 --- a/lib/Service/Install/ConfigureCheckService.php +++ b/lib/Service/Install/ConfigureCheckService.php @@ -54,6 +54,10 @@ public function checkSign(): array { * @return ConfigureCheckHelper[] */ public function checkJSignPdf(): array { + $signatureEngine = $this->appConfig->getAppValue('signature_engine', 'jsignpdf'); + if ($signatureEngine !== 'jsignpdf') { + return []; + } $jsignpdJarPath = $this->appConfig->getAppValue('jsignpdf_jar_path'); if ($jsignpdJarPath) { if (file_exists($jsignpdJarPath)) { @@ -166,6 +170,10 @@ public function checkPdftk(): array { * @return ConfigureCheckHelper[] */ private function checkJava(): array { + $signatureEngine = $this->appConfig->getAppValue('signature_engine', 'jsignpdf'); + if ($signatureEngine !== 'jsignpdf') { + return []; + } $javaPath = $this->appConfig->getAppValue('java_path'); if ($javaPath) { if (file_exists($javaPath)) { diff --git a/lib/Service/Install/InstallService.php b/lib/Service/Install/InstallService.php index f2f4997c25..0ec2a0b76b 100644 --- a/lib/Service/Install/InstallService.php +++ b/lib/Service/Install/InstallService.php @@ -308,6 +308,10 @@ public function setResource(string $resource): self { } public function installJava(?bool $async = false): void { + $signatureEngine = $this->appConfig->getAppValue('signature_engine', 'jsignpdf'); + if ($signatureEngine !== 'jsignpdf') { + return; + } $this->setResource('java'); if ($async) { $this->runAsync(); @@ -378,6 +382,10 @@ public function uninstallJava(): void { } public function installJSignPdf(?bool $async = false): void { + $signatureEngine = $this->appConfig->getAppValue('signature_engine', 'jsignpdf'); + if ($signatureEngine !== 'jsignpdf') { + return; + } if (!extension_loaded('zip')) { throw new RuntimeException('Zip extension is not available'); } diff --git a/src/views/Settings/Settings.vue b/src/views/Settings/Settings.vue index 9e64d546cd..80d54901c7 100644 --- a/src/views/Settings/Settings.vue +++ b/src/views/Settings/Settings.vue @@ -24,6 +24,7 @@