Skip to content
Snippets Groups Projects
ExtensionConfiguration.php 3.56 KiB
Newer Older
Patrick Schriner's avatar
Patrick Schriner committed
<?php

declare(strict_types=1);

/*
Kai Ole Hartwig's avatar
Kai Ole Hartwig committed
 * This file is part of the package itzbund/gsb-metadata-cleaner of the GSB 11 Project by ITZBund.
 *
 * Copyright (C) 2023 - 2024 Bundesrepublik Deutschland, vertreten durch das
 * BMI/ITZBund. Author: Ole Hartwig, Patrick Schriner
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
Patrick Schriner's avatar
Patrick Schriner committed
 *
 * For the full copyright and license information, please read the
 * LICENSE file that was distributed with this source code.
 */

namespace ITZBund\GsbMetadataCleaner\Configuration;

use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use RuntimeException;
Patrick Schriner's avatar
Patrick Schriner committed
use TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationExtensionNotConfiguredException;
use TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationPathDoesNotExistException;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration as CoreExtensionConfiguration;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\PathUtility;
Patrick Schriner's avatar
Patrick Schriner committed

final class ExtensionConfiguration
Patrick Schriner's avatar
Patrick Schriner committed
{
    final public const EXIFTOOL_BINARY = 'exiftool';
Patrick Schriner's avatar
Patrick Schriner committed

    final public const QPDF_BINARY = 'qpdf';
Patrick Schriner's avatar
Patrick Schriner committed

    public function __construct(
        private readonly CoreExtensionConfiguration $extensionConfiguration,
        private readonly LoggerInterface $logger
    ) {}

    /**
     * Get the path to the exiftool executable, defined by extension configuration or default
     *
     * @return string the path to the exiftool executable
     */
    public function getExifToolPath(): string
    {
        $exifToolPath = trim($this->extensionConfiguration->get('gsb_metadata_cleaner', 'exifToolPath'));
Patrick Schriner's avatar
Patrick Schriner committed

        if (!$this->utilityExistsAndIsSafe($exifToolPath, self::EXIFTOOL_BINARY)) {
            throw new RuntimeException('exitfool path not properly configured', 1710517552);
        return $exifToolPath . self::EXIFTOOL_BINARY;
Patrick Schriner's avatar
Patrick Schriner committed
    }

    /**
     * Get the path to the qpdf executable, defined by extension configuration or default
     *
     * @return string the path to the pdf executable
     */
    public function getQpdfToolPath(): string
    {
        $qpdfToolPath = trim($this->extensionConfiguration->get('gsb_metadata_cleaner', 'qpdfToolPath'));
Patrick Schriner's avatar
Patrick Schriner committed

        if (!$this->utilityExistsAndIsSafe($qpdfToolPath, self::QPDF_BINARY)) {
            throw new RuntimeException('qpdf path not properly configured', 1710517551);
        return $qpdfToolPath . self::QPDF_BINARY;
Patrick Schriner's avatar
Patrick Schriner committed
    }

    public function getUseQpdf(): bool
    {
        $useQpdf = true;
        try {
            $useQpdf = (bool)$this->extensionConfiguration->get('gsb_metadata_cleaner', 'useQpdf');
        } catch (ExtensionConfigurationExtensionNotConfiguredException $ecence) {
            $this->logger->log(LogLevel::INFO, 'Extension not configured');
Patrick Schriner's avatar
Patrick Schriner committed
        } catch (ExtensionConfigurationPathDoesNotExistException $ecpdnee) {
            $this->logger->log(LogLevel::INFO, 'useQpdf not configured');
Patrick Schriner's avatar
Patrick Schriner committed
        }
        return $useQpdf;
    }

    /**
     * @param string $path
     * @param string $binaryName
     * @return bool
     */
    protected function utilityExistsAndIsSafe(string $path, string $binaryName): bool
    {
        if ($path === '' || !PathUtility::isAbsolutePath($path)
            || !GeneralUtility::validPathStr($path) || str_starts_with($path, Environment::getPublicPath())
            || !is_dir($path)
        ) {
            return false;
        }

        if (file_exists($path . $binaryName)) {
            return true;
        }
        return false;
    }
Patrick Schriner's avatar
Patrick Schriner committed
}

Consent

On this website, we use the web analytics service Matomo to analyze and review the use of our website. Through the collected statistics, we can improve our offerings and make them more appealing for you. Here, you can decide whether to allow us to process your data and set corresponding cookies for these purposes, in addition to technically necessary cookies. Further information on data protection—especially regarding "cookies" and "Matomo"—can be found in our privacy policy. You can withdraw your consent at any time.