|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + * Copyright (c) 2017-2018 Tobias Reich |
| 6 | + * Copyright (c) 2018-2025 LycheeOrg. |
| 7 | + */ |
| 8 | + |
| 9 | +namespace App\Actions\Diagnostics\Pipes\Checks; |
| 10 | + |
| 11 | +use App\Contracts\DiagnosticPipe; |
| 12 | +use App\DTO\DiagnosticData; |
| 13 | +use App\Models\Configs; |
| 14 | +use Illuminate\Support\Facades\Schema; |
| 15 | +use Safe\Exceptions\FilesystemException; |
| 16 | +use Safe\Exceptions\PcreException; |
| 17 | +use function Safe\file_get_contents; |
| 18 | +use function Safe\preg_match; |
| 19 | + |
| 20 | +/** |
| 21 | + * Verify that if Imagick is installed, it is allowed to work with pdf files. |
| 22 | + */ |
| 23 | +class ImagickPdfCheck implements DiagnosticPipe |
| 24 | +{ |
| 25 | + /** |
| 26 | + * {@inheritDoc} |
| 27 | + */ |
| 28 | + public function handle(array &$data, \Closure $next): array |
| 29 | + { |
| 30 | + if (!Schema::hasTable('configs')) { |
| 31 | + // @codeCoverageIgnoreStart |
| 32 | + return $next($data); |
| 33 | + // @codeCoverageIgnoreEnd |
| 34 | + } |
| 35 | + |
| 36 | + if (!Configs::hasImagick()) { |
| 37 | + $data[] = DiagnosticData::info('Imagick is not enabled. Thumbs will not be created for pdf files.', self::class); |
| 38 | + |
| 39 | + return $next($data); |
| 40 | + } |
| 41 | + |
| 42 | + try { |
| 43 | + $imagic_policy = file_get_contents('/etc/ImageMagick-6/policy.xml'); |
| 44 | + if (1 === preg_match('/<policy domain="coder" rights="none" pattern="PDF"/', $imagic_policy)) { |
| 45 | + $data[] = DiagnosticData::warn('Imagick is not allowed to create thumbs for pdf files.', self::class, |
| 46 | + ['Verify that the /etc/ImageMagick-6/policy.xml file contains the line <policy domain="coder" rights="read|write" pattern="PDF"/> .']); |
| 47 | + } |
| 48 | + } catch (FilesystemException) { |
| 49 | + $data[] = DiagnosticData::warn('Could not determine whether Imagick is allowed to work with pdf files.', self::class, [ |
| 50 | + 'Make sure to have the policy.xml file in `/etc/ImageMagick-6/policy.xml`.', |
| 51 | + 'Verify that the file contains the line <policy domain="coder" rights="read|write" pattern="PDF"/> .', |
| 52 | + ]); |
| 53 | + } catch (PcreException) { |
| 54 | + // Just ignore. |
| 55 | + } |
| 56 | + |
| 57 | + return $next($data); |
| 58 | + } |
| 59 | +} |
0 commit comments