|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * @copyright Copyright (c) 2023 Vitor Mattos <vitor@php.rio> |
| 6 | + * |
| 7 | + * @author Vitor Mattos <vitor@php.rio> |
| 8 | + * |
| 9 | + * @license GNU AGPL version 3 or any later version |
| 10 | + * |
| 11 | + * This program is free software: you can redistribute it and/or modify |
| 12 | + * it under the terms of the GNU Affero General Public License as |
| 13 | + * published by the Free Software Foundation, either version 3 of the |
| 14 | + * License, or (at your option) any later version. |
| 15 | + * |
| 16 | + * This program is distributed in the hope that it will be useful, |
| 17 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | + * GNU Affero General Public License for more details. |
| 20 | + * |
| 21 | + * You should have received a copy of the GNU Affero General Public License |
| 22 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 23 | + */ |
| 24 | + |
| 25 | +namespace OCA\Libresign\Handler; |
| 26 | + |
| 27 | +use Jeidison\JSignPDF\JSignPDF; |
| 28 | +use Jeidison\JSignPDF\Sign\JSignParam; |
| 29 | +use OCA\Libresign\Exception\LibresignException; |
| 30 | +use OCP\AppFramework\Services\IAppConfig; |
| 31 | +use Psr\Log\LoggerInterface; |
| 32 | + |
| 33 | +class PhpNativeHandler extends SignEngineHandler { |
| 34 | + /** @var JSignPDF */ |
| 35 | + private $jSignPdf; |
| 36 | + /** @var JSignParam */ |
| 37 | + private $jSignParam; |
| 38 | + public const VERSION = '2.2.2'; |
| 39 | + |
| 40 | + public function __construct( |
| 41 | + private IAppConfig $appConfig, |
| 42 | + private LoggerInterface $logger, |
| 43 | + ) { |
| 44 | + } |
| 45 | + |
| 46 | + public function setJSignPdf(JSignPDF $jSignPdf): void { |
| 47 | + $this->jSignPdf = $jSignPdf; |
| 48 | + } |
| 49 | + |
| 50 | + public function getJSignPdf(): JSignPDF { |
| 51 | + if (!$this->jSignPdf) { |
| 52 | + // @codeCoverageIgnoreStart |
| 53 | + $this->setJSignPdf(new JSignPDF()); |
| 54 | + // @codeCoverageIgnoreEnd |
| 55 | + } |
| 56 | + return $this->jSignPdf; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @psalm-suppress MixedReturnStatement |
| 61 | + */ |
| 62 | + public function getJSignParam(): JSignParam { |
| 63 | + if (!$this->jSignParam) { |
| 64 | + $javaPath = $this->appConfig->getAppValue('java_path'); |
| 65 | + $this->jSignParam = (new JSignParam()) |
| 66 | + ->setTempPath( |
| 67 | + $this->appConfig->getAppValue('jsignpdf_temp_path', sys_get_temp_dir() . DIRECTORY_SEPARATOR) |
| 68 | + ) |
| 69 | + ->setIsUseJavaInstalled(empty($javaPath)) |
| 70 | + ->setjSignPdfJarPath( |
| 71 | + $this->appConfig->getAppValue('jsignpdf_jar_path', '/opt/jsignpdf-' . self::VERSION . '/JSignPdf.jar') |
| 72 | + ); |
| 73 | + if (!empty($javaPath)) { |
| 74 | + if (!file_exists($javaPath)) { |
| 75 | + throw new \Exception('Invalid Java binary. Run occ libresign:install --java'); |
| 76 | + } |
| 77 | + $this->jSignParam->setJavaPath($javaPath); |
| 78 | + } |
| 79 | + } |
| 80 | + return $this->jSignParam; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @psalm-suppress MixedReturnStatement |
| 85 | + */ |
| 86 | + public function sign(): string { |
| 87 | + $param = $this->getJSignParam() |
| 88 | + ->setCertificate($this->getCertificate()) |
| 89 | + ->setPdf($this->getInputFile()->getContent()) |
| 90 | + ->setPassword($this->getPassword()); |
| 91 | + |
| 92 | + $signed = $this->signUsingVisibleElements(); |
| 93 | + if ($signed) { |
| 94 | + return $signed; |
| 95 | + } |
| 96 | + $jSignPdf = $this->getJSignPdf(); |
| 97 | + $jSignPdf->setParam($param); |
| 98 | + return $this->signWrapper($jSignPdf); |
| 99 | + } |
| 100 | + |
| 101 | + private function signUsingVisibleElements(): string { |
| 102 | + $visibleElements = $this->getvisibleElements(); |
| 103 | + if ($visibleElements) { |
| 104 | + $jSignPdf = $this->getJSignPdf(); |
| 105 | + $param = $this->getJSignParam(); |
| 106 | + foreach ($visibleElements as $element) { |
| 107 | + $param |
| 108 | + ->setJSignParameters( |
| 109 | + $param->getJSignParameters() . |
| 110 | + ' -pg ' . $element->getFileElement()->getPage() . |
| 111 | + ' -llx ' . $element->getFileElement()->getLlx() . |
| 112 | + ' -lly ' . $element->getFileElement()->getLly() . |
| 113 | + ' -urx ' . $element->getFileElement()->getUrx() . |
| 114 | + ' -ury ' . $element->getFileElement()->getUry() . |
| 115 | + ' --l2-text ""' . |
| 116 | + ' -V' . |
| 117 | + ' --bg-path ' . $element->getTempFile() |
| 118 | + ); |
| 119 | + $jSignPdf->setParam($param); |
| 120 | + $signed = $this->signWrapper($jSignPdf); |
| 121 | + } |
| 122 | + return $signed; |
| 123 | + } |
| 124 | + return ''; |
| 125 | + } |
| 126 | + |
| 127 | + private function signWrapper(JSignPDF $jSignPDF): string { |
| 128 | + try { |
| 129 | + return $jSignPDF->sign(); |
| 130 | + } catch (\Throwable $th) { |
| 131 | + $rows = str_getcsv($th->getMessage()); |
| 132 | + $hashAlgorithm = array_filter($rows, fn ($r) => str_contains($r, 'The chosen hash algorithm')); |
| 133 | + if (!empty($hashAlgorithm)) { |
| 134 | + $hashAlgorithm = current($hashAlgorithm); |
| 135 | + $hashAlgorithm = trim($hashAlgorithm, 'INFO '); |
| 136 | + $hashAlgorithm = str_replace('\"', '"', $hashAlgorithm); |
| 137 | + $hashAlgorithm = preg_replace('/\.( )/', ".\n", $hashAlgorithm); |
| 138 | + throw new LibresignException($hashAlgorithm); |
| 139 | + } |
| 140 | + $this->logger->error('Error at JSignPdf side. LibreSign can not do nothing. Follow the error message: ' . $th->getMessage()); |
| 141 | + throw new \Exception($th->getMessage()); |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments