|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * SPDX-FileCopyrightText: 2026 LibreCode coop and contributors |
| 6 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + */ |
| 8 | + |
| 9 | +namespace OCA\Libresign\SetupCheck; |
| 10 | + |
| 11 | +use OCA\Libresign\Helper\JavaHelper; |
| 12 | +use OCA\Libresign\AppInfo\Application; |
| 13 | +use OCA\Libresign\Service\Install\SignSetupService; |
| 14 | +use OCA\Libresign\Service\Install\InstallService; |
| 15 | +use OCP\IL10N; |
| 16 | +use OCP\IAppConfig; |
| 17 | +use OCP\IURLGenerator; |
| 18 | +use OCP\App\IAppManager; |
| 19 | +use OCP\SetupCheck\ISetupCheck; |
| 20 | +use OCP\SetupCheck\SetupResult; |
| 21 | +use OCP\IConfig; |
| 22 | +use Psr\Log\LoggerInterface; |
| 23 | + |
| 24 | +class PDFtkSetupCheck implements ISetupCheck { |
| 25 | + use SetupCheckUtils; |
| 26 | + |
| 27 | + private IL10N $l10n; |
| 28 | + private IAppConfig $appConfig; |
| 29 | + private IConfig $systemConfig; |
| 30 | + private JavaHelper $javaHelper; |
| 31 | + |
| 32 | + private SignSetupService $signSetupService; |
| 33 | + private IURLGenerator $urlGenerator; |
| 34 | + private IAppManager $appManager; |
| 35 | + private LoggerInterface $logger; |
| 36 | + |
| 37 | + public function __construct( |
| 38 | + IL10N $l10n, |
| 39 | + IAppConfig $appConfig, |
| 40 | + SignSetupService $signSetupService, |
| 41 | + IURLGenerator $urlGenerator, |
| 42 | + IAppManager $appManager, |
| 43 | + LoggerInterface $logger, |
| 44 | + IConfig $systemConfig, |
| 45 | + JavaHelper $javaHelper |
| 46 | + ) { |
| 47 | + $this->l10n = $l10n; |
| 48 | + $this->appConfig = $appConfig; |
| 49 | + $this->signSetupService = $signSetupService; |
| 50 | + $this->urlGenerator = $urlGenerator; |
| 51 | + $this->appManager = $appManager; |
| 52 | + $this->logger = $logger; |
| 53 | + $this->systemConfig = $systemConfig; |
| 54 | + $this->javaHelper = $javaHelper; |
| 55 | + } |
| 56 | + |
| 57 | + public function getName(): string { |
| 58 | + return $this->l10n->t('PDFtk'); |
| 59 | + } |
| 60 | + |
| 61 | + public function getCategory(): string { |
| 62 | + return 'system'; |
| 63 | + } |
| 64 | + |
| 65 | + public function run(): SetupResult { |
| 66 | + $debugEnabled = $this->systemConfig->getSystemValueBool('debug', false); |
| 67 | + $pdftkPath = $this->appConfig->getValueString(Application::APP_ID, 'pdftk_path'); |
| 68 | + |
| 69 | + if (!$pdftkPath) { |
| 70 | + return SetupResult::error( |
| 71 | + $this->l10n->t('PDFtk not found'), |
| 72 | + $this->l10n->t('Run occ libresign:install --pdftk') |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + $verifyResult = $this->verifyResourceIntegrity('pdftk', $debugEnabled); |
| 77 | + if (!empty($verifyResult)) { |
| 78 | + [$errorMsg, $tip] = $this->getErrorAndTipFromVerify($verifyResult, 'pdftk', $debugEnabled, $this->l10n); |
| 79 | + return SetupResult::error($errorMsg, $tip); |
| 80 | + } |
| 81 | + |
| 82 | + if (!file_exists($pdftkPath)) { |
| 83 | + return SetupResult::error( |
| 84 | + $this->l10n->t('PDFtk binary not found: %s', [$pdftkPath]), |
| 85 | + $this->l10n->t('Run occ libresign:install --pdftk') |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + $javaPath = $this->javaHelper->getJavaPath(); |
| 90 | + if (!$javaPath || !file_exists($javaPath)) { |
| 91 | + return SetupResult::error( |
| 92 | + $this->l10n->t('Necessary Java to run PDFtk'), |
| 93 | + $this->l10n->t('Run occ libresign:install --java') |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + exec($javaPath . ' -jar ' . $pdftkPath . ' --version 2>&1', $versionOutput, $resultCode); |
| 98 | + if ($resultCode !== 0) { |
| 99 | + return SetupResult::error( |
| 100 | + $this->l10n->t('Failure to check PDFtk version.'), |
| 101 | + $this->l10n->t('Run occ libresign:install --pdftk') |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + $versionLine = $versionOutput[0] ?? ''; |
| 106 | + preg_match('/pdftk port to java (?<version>.*) a Handy Tool/', $versionLine, $matches); |
| 107 | + $version = $matches['version'] ?? null; |
| 108 | + |
| 109 | + if (!$version) { |
| 110 | + return SetupResult::error( |
| 111 | + $this->l10n->t('PDFtk binary is invalid: %s', [$pdftkPath]), |
| 112 | + $this->l10n->t('Run occ libresign:install --pdftk') |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + if ($version !== InstallService::PDFTK_VERSION) { |
| 117 | + return SetupResult::error( |
| 118 | + $this->l10n->t('Necessary install the version %s', [InstallService::PDFTK_VERSION]), |
| 119 | + $this->l10n->t('Run occ libresign:install --pdftk') |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + $messages = [ |
| 124 | + $this->l10n->t('PDFtk version: %s', [$version]), |
| 125 | + $this->l10n->t('PDFtk path: %s', [$pdftkPath]), |
| 126 | + ]; |
| 127 | + return SetupResult::success(implode("\n", $messages)); |
| 128 | + } |
| 129 | +} |
0 commit comments