Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
55 / 55
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
JSignPdfSetupCheck
100.00% covered (success)
100.00%
55 / 55
100.00% covered (success)
100.00%
4 / 4
12
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 getName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCategory
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 run
100.00% covered (success)
100.00%
44 / 44
100.00% covered (success)
100.00%
1 / 1
9
1<?php
2
3declare(strict_types=1);
4/**
5 * SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
6 * SPDX-License-Identifier: AGPL-3.0-or-later
7 */
8
9namespace OCA\Libresign\SetupCheck;
10
11use OCA\Libresign\AppInfo\Application;
12use OCA\Libresign\Handler\SignEngine\JSignPdf\JSignPdfHandler;
13use OCA\Libresign\Helper\JavaHelper;
14use OCA\Libresign\Service\Install\JSignPdfRelease;
15use OCA\Libresign\Service\Install\SignSetupService;
16use OCP\App\IAppManager;
17use OCP\IAppConfig;
18use OCP\IConfig;
19use OCP\IL10N;
20use OCP\IURLGenerator;
21use OCP\SetupCheck\ISetupCheck;
22use OCP\SetupCheck\SetupResult;
23use Psr\Log\LoggerInterface;
24
25class JSignPdfSetupCheck implements ISetupCheck {
26    use SetupCheckUtils;
27
28    private IL10N $l10n;
29    private IAppConfig $appConfig;
30    private JSignPdfHandler $jSignPdfHandler;
31    private IConfig $systemConfig;
32    private JavaHelper $javaHelper;
33
34    private SignSetupService $signSetupService;
35    private IURLGenerator $urlGenerator;
36    private IAppManager $appManager;
37    private LoggerInterface $logger;
38
39    public function __construct(
40        IL10N $l10n,
41        IAppConfig $appConfig,
42        JSignPdfHandler $jSignPdfHandler,
43        SignSetupService $signSetupService,
44        IURLGenerator $urlGenerator,
45        IAppManager $appManager,
46        LoggerInterface $logger,
47        IConfig $systemConfig,
48        JavaHelper $javaHelper,
49    ) {
50        $this->l10n = $l10n;
51        $this->appConfig = $appConfig;
52        $this->jSignPdfHandler = $jSignPdfHandler;
53        $this->signSetupService = $signSetupService;
54        $this->urlGenerator = $urlGenerator;
55        $this->appManager = $appManager;
56        $this->logger = $logger;
57        $this->systemConfig = $systemConfig;
58        $this->javaHelper = $javaHelper;
59    }
60
61    #[\Override]
62    public function getName(): string {
63        return 'JSignPdf';
64    }
65
66    #[\Override]
67    public function getCategory(): string {
68        return 'system';
69    }
70
71    #[\Override]
72    public function run(): SetupResult {
73        $debugEnabled = $this->systemConfig->getSystemValueBool('debug', false);
74        $jsignpdfPath = $this->appConfig->getValueString(Application::APP_ID, 'jsignpdf_path');
75
76        if (!$jsignpdfPath) {
77            return SetupResult::error(
78                // TRANSLATORS Warning shown in Nextcloud administration overview when the optional JSignPdf signing backend is not found.
79                $this->l10n->t('JSignPdf not found'),
80                // TRANSLATORS Command to run into terminal using Nextcloud occ to configure LibreSign using CLI when the sysadmin want to do this by CLI.
81                $this->l10n->t('Run %s', ['occ libresign:install --jsignpdf'])
82            );
83        }
84
85        $verifyResult = $this->verifyResourceIntegrity('jsignpdf', $debugEnabled);
86        if (!empty($verifyResult)) {
87            [$errorMsg, $tip] = $this->getErrorAndTipFromVerify($verifyResult, 'jsignpdf', $debugEnabled, $this->l10n);
88            return SetupResult::error($errorMsg, $tip);
89        }
90
91        if (!is_dir($jsignpdfPath)) {
92            return SetupResult::error(
93                // TRANSLATORS JSignPdf is an optional external signing backend used by LibreSign.
94                // LibreSign also supports other signing methods, including its native PHP signer.
95                // %s is the configured JSignPdf path that could not be found.
96                $this->l10n->t('JSignPdf path not found: %s', [$jsignpdfPath]),
97                // TRANSLATORS Command to run into terminal using Nextcloud occ to configure LibreSign using CLI when the sysadmin want to do this by CLI.
98                $this->l10n->t('Run %s', ['occ libresign:install --jsignpdf'])
99            );
100        }
101
102        $javaPath = $this->javaHelper->getJavaPath();
103        if (!$javaPath || !file_exists($javaPath)) {
104            return SetupResult::error(
105                // TRANSLATORS JSignPdf is an optional external signing backend that requires Java to run.
106                // LibreSign also supports other signing methods, including its native PHP signer.
107                $this->l10n->t('Necessary Java to run JSignPdf'),
108                // TRANSLATORS Command to run into terminal using Nextcloud occ to configure LibreSign using CLI when the sysadmin want to do this by CLI.
109                $this->l10n->t('Run %s', ['occ libresign:install --java'])
110            );
111        }
112
113        $jsignPdf = $this->jSignPdfHandler->getJSignPdf();
114        $jsignPdf->setParam($this->jSignPdfHandler->getJSignParam());
115        $currentVersion = $jsignPdf->getVersion();
116
117        if (!$currentVersion) {
118            // TRANSLATORS JSignPdf is an optional external signing backend.
119            // LibreSign is tested/validated with a specific JSignPdf version. %s is the supported JSignPdf version.
120            $msg = $this->l10n->t('Necessary install the version %s', [JSignPdfRelease::VERSION]);
121            // TRANSLATORS Command to run into terminal using Nextcloud occ to configure LibreSign using CLI when the sysadmin want to do this by CLI.
122            return SetupResult::error($msg, $this->l10n->t('Run %s', ['occ libresign:install --jsignpdf']));
123        }
124
125        if (version_compare($currentVersion, JSignPdfRelease::VERSION, '<')) {
126            // TRANSLATORS JSignPdf is an optional external signing backend.
127            // LibreSign is tested/validated with a specific JSignPdf version.
128            // The first %s is the currently installed JSignPdf version; the second %s is the required supported version.
129            $msg = $this->l10n->t('JSignPdf must be updated from version %s to %s', [$currentVersion, JSignPdfRelease::VERSION]);
130            // TRANSLATORS Command to run into terminal using Nextcloud occ to configure LibreSign using CLI when the sysadmin want to do this by CLI.
131            return SetupResult::error($msg, $this->l10n->t('Run %s', ['occ libresign:install --jsignpdf']));
132        }
133
134        if (version_compare($currentVersion, JSignPdfRelease::VERSION, '>')) {
135            return SetupResult::error(
136                // TRANSLATORS JSignPdf is an optional external signing backend.
137                // LibreSign is tested/validated with a specific JSignPdf version.
138                // The first %s is the currently installed JSignPdf version; the second %s is the required supported version.
139                $this->l10n->t(
140                    'JSignPdf must be downgraded from version %s to %s',
141                    [$currentVersion, JSignPdfRelease::VERSION],
142                ),
143                // TRANSLATORS Command to run into terminal using Nextcloud occ to configure LibreSign using CLI when the sysadmin want to do this by CLI.
144                $this->l10n->t('Run %s', ['occ libresign:install --jsignpdf'])
145            );
146        }
147
148        $messages = [
149            // TRANSLATORS JSignPdf is an optional external signing backend. %s is the detected JSignPdf version.
150            $this->l10n->t('JSignPdf version: %s', [$currentVersion]),
151
152            // TRANSLATORS JSignPdf is an optional external signing backend. %s is the configured or detected directory where JSignPdf is installed.
153            $this->l10n->t('JSignPdf path: %s', [$jsignpdfPath]),
154        ];
155
156        return SetupResult::success(implode("\n", $messages));
157    }
158}