Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
80 / 80 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| JavaSetupCheck | |
100.00% |
80 / 80 |
|
100.00% |
4 / 4 |
15 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| getName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getCategory | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| run | |
100.00% |
71 / 71 |
|
100.00% |
1 / 1 |
12 | |||
| 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\Service\Install\InstallService; |
| 13 | use OCA\Libresign\Service\Install\SignSetupService; |
| 14 | use OCP\App\IAppManager; |
| 15 | use OCP\IConfig; |
| 16 | use OCP\IL10N; |
| 17 | use OCP\IURLGenerator; |
| 18 | use OCP\SetupCheck\ISetupCheck; |
| 19 | use OCP\SetupCheck\SetupResult; |
| 20 | use Psr\Log\LoggerInterface; |
| 21 | |
| 22 | class JavaSetupCheck implements ISetupCheck { |
| 23 | use SetupCheckUtils; |
| 24 | |
| 25 | private IL10N $l10n; |
| 26 | private JavaHelper $javaHelper; |
| 27 | private IConfig $systemConfig; |
| 28 | |
| 29 | private SignSetupService $signSetupService; |
| 30 | private IURLGenerator $urlGenerator; |
| 31 | private IAppManager $appManager; |
| 32 | private LoggerInterface $logger; |
| 33 | |
| 34 | public function __construct( |
| 35 | IL10N $l10n, |
| 36 | JavaHelper $javaHelper, |
| 37 | SignSetupService $signSetupService, |
| 38 | IURLGenerator $urlGenerator, |
| 39 | IAppManager $appManager, |
| 40 | LoggerInterface $logger, |
| 41 | IConfig $systemConfig, |
| 42 | ) { |
| 43 | $this->l10n = $l10n; |
| 44 | $this->javaHelper = $javaHelper; |
| 45 | $this->signSetupService = $signSetupService; |
| 46 | $this->urlGenerator = $urlGenerator; |
| 47 | $this->appManager = $appManager; |
| 48 | $this->logger = $logger; |
| 49 | $this->systemConfig = $systemConfig; |
| 50 | } |
| 51 | |
| 52 | #[\Override] |
| 53 | public function getName(): string { |
| 54 | return 'Java'; |
| 55 | } |
| 56 | |
| 57 | #[\Override] |
| 58 | public function getCategory(): string { |
| 59 | return 'system'; |
| 60 | } |
| 61 | |
| 62 | #[\Override] |
| 63 | public function run(): SetupResult { |
| 64 | $debugEnabled = $this->systemConfig->getSystemValueBool('debug', false); |
| 65 | $javaPath = $this->javaHelper->getJavaPath(); |
| 66 | |
| 67 | if (!$javaPath) { |
| 68 | return SetupResult::error( |
| 69 | // TRANSLATORS Warning shown in Nextcloud administration overview when Java required by LibreSign is not installed. |
| 70 | $this->l10n->t('Java not installed'), |
| 71 | // TRANSLATORS Tip under a Nextcloud administration overview error. Instructs the administrator to install Java for LibreSign via occ. |
| 72 | $this->l10n->t('Run occ libresign:install --java') |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | $verifyResult = $this->verifyResourceIntegrity('java', $debugEnabled); |
| 77 | if (!empty($verifyResult)) { |
| 78 | [$errorMsg, $tip] = $this->getErrorAndTipFromVerify($verifyResult, 'java', $debugEnabled, $this->l10n); |
| 79 | return SetupResult::error($errorMsg, $tip); |
| 80 | } |
| 81 | |
| 82 | if (!file_exists($javaPath)) { |
| 83 | return SetupResult::error( |
| 84 | // TRANSLATORS Warning shown in Nextcloud administration overview when the configured Java path does not exist. %s is the path. |
| 85 | $this->l10n->t('Java binary not found: %s', [$javaPath]), |
| 86 | // TRANSLATORS Tip under a Nextcloud administration overview error. Instructs the administrator to install Java for LibreSign via occ. |
| 87 | $this->l10n->t('Run occ libresign:install --java') |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | /** @var list<string> $output */ |
| 92 | exec($javaPath . ' -version 2>&1', $output, $returnCode); |
| 93 | if (empty($output)) { |
| 94 | return SetupResult::error( |
| 95 | // TRANSLATORS Warning shown in Nextcloud administration overview when Java cannot be executed, often because the operating system blocks it. |
| 96 | $this->l10n->t('Failed to execute Java. Sounds that your operational system is blocking the JVM.'), |
| 97 | 'https://github.com/LibreSign/libresign/issues/2327#issuecomment-1961988790' |
| 98 | ); |
| 99 | } |
| 100 | if ($returnCode !== 0) { |
| 101 | return SetupResult::error( |
| 102 | // TRANSLATORS Warning shown in Nextcloud administration overview when LibreSign cannot read the installed Java version. |
| 103 | $this->l10n->t('Failure to check Java version.'), |
| 104 | // TRANSLATORS Tip under a Nextcloud administration overview error. Instructs the administrator to install Java for LibreSign via occ. |
| 105 | $this->l10n->t('Run occ libresign:install --java') |
| 106 | ); |
| 107 | } |
| 108 | |
| 109 | $javaVersion = trim($output[0] ?? ''); |
| 110 | if ($javaVersion !== InstallService::JAVA_VERSION) { |
| 111 | return SetupResult::error( |
| 112 | // TRANSLATORS Warning shown in Nextcloud administration overview when the installed Java version does not match the required one. First %s is found; second %s is expected. |
| 113 | $this->l10n->t('Invalid java version. Found: %s expected: %s', [$javaVersion, InstallService::JAVA_VERSION]), |
| 114 | // TRANSLATORS Tip under a Nextcloud administration overview error. Instructs the administrator to install Java for LibreSign via occ. |
| 115 | $this->l10n->t('Run occ libresign:install --java') |
| 116 | ); |
| 117 | } |
| 118 | |
| 119 | exec($javaPath . ' -XshowSettings:properties -version 2>&1', $encodingOutput); |
| 120 | $fullOutput = implode("\n", $encodingOutput); |
| 121 | preg_match('/native.encoding = (?<encoding>.*?)(\n|$)/', $fullOutput, $matches); |
| 122 | $encoding = $matches['encoding'] ?? null; |
| 123 | |
| 124 | if (!$encoding) { |
| 125 | return SetupResult::error( |
| 126 | // TRANSLATORS Warning shown in Nextcloud administration overview when LibreSign cannot detect the Java character encoding. |
| 127 | $this->l10n->t('Java encoding not found.'), |
| 128 | sprintf('The command %s need to have native.encoding', $javaPath . ' -XshowSettings:properties -version') |
| 129 | ); |
| 130 | } |
| 131 | |
| 132 | if (!str_contains($encoding, 'UTF-8')) { |
| 133 | $detectedEncoding = trim($encoding); |
| 134 | $phpLocale = setlocale(LC_CTYPE, '0') ?: 'not set'; |
| 135 | $phpLcAll = getenv('LC_ALL') ?: 'not set'; |
| 136 | $phpLang = getenv('LANG') ?: 'not set'; |
| 137 | |
| 138 | $tip = sprintf( |
| 139 | "Java detected encoding \"%s\" but UTF-8 is required.\n\n" |
| 140 | . "**Current PHP environment:**\n" |
| 141 | . "- LC_CTYPE: %s\n" |
| 142 | . "- LC_ALL: %s\n" |
| 143 | . "- LANG: %s\n\n" |
| 144 | . "**To fix this issue:**\n" |
| 145 | . "1. Set LC_ALL and LANG environment variables (e.g., LC_ALL=en_US.UTF-8) for your web server user\n" |
| 146 | . "2. Restart your web server after making changes\n" |
| 147 | . "3. Verify with command: `locale charmap` (should return UTF-8)\n\n" |
| 148 | . 'For more details, see: [Issue #4872](https://github.com/LibreSign/libresign/issues/4872)', |
| 149 | $detectedEncoding, |
| 150 | $phpLocale, |
| 151 | $phpLcAll, |
| 152 | $phpLang |
| 153 | ); |
| 154 | return SetupResult::info( |
| 155 | // TRANSLATORS Info shown in Nextcloud administration overview when Java is not using UTF-8, which can break accented characters. %s is the detected encoding. |
| 156 | $this->l10n->t('Non-UTF-8 encoding detected: %s. This may cause issues with accented or special characters', [$detectedEncoding]), |
| 157 | $tip |
| 158 | ); |
| 159 | } |
| 160 | |
| 161 | // TRANSLATORS Success detail in Nextcloud administration overview showing the detected Java version. %s is the version string. |
| 162 | $message = $this->l10n->t('Java version: %s', [$javaVersion]) . "\n" |
| 163 | // TRANSLATORS Success detail in Nextcloud administration overview showing the Java executable path. %s is the filesystem path. |
| 164 | . $this->l10n->t('Java binary: %s', [$javaPath]); |
| 165 | return SetupResult::success($message); |
| 166 | } |
| 167 | } |